Skip to content

Commit

Permalink
namespaces (TODO allow with class definitions), use for selfhost util…
Browse files Browse the repository at this point in the history
… functions
  • Loading branch information
ehren committed Aug 28, 2024
1 parent e13f3d8 commit 388134e
Show file tree
Hide file tree
Showing 16 changed files with 219 additions and 191 deletions.
19 changes: 18 additions & 1 deletion ceto/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -2084,6 +2084,23 @@ def codegen_call(node: Call, cx: Scope):
if func_name == "isinstance":
cast_string = "(" + cast_string + " != nullptr)"
return cast_string
elif func_name == "namespace":
if len(node.args) == 0:
raise CodeGenError("empty namespace args", node)
block = node.args[-1]
if not isinstance(block, Block):
raise CodeGenError("namespace last args must be a Block", node)
if len(node.args) > 2:
raise CodeGenError("too many namespace args", node)
if len(node.args) == 2:
name = node.args[0]
if not isinstance(name, (Identifier, ScopeResolution, AttributeAccess)):
raise CodeGenError("bad namespace name", node)
name_code = codegen_node(name, cx)
else:
name_code = ""
block_code = codegen_block(block, cx.enter_scope())
return "namespace " + name_code + " {\n" + block_code + "\n}"
elif func_name == "defmacro":
return "\n"
else:
Expand Down Expand Up @@ -2735,7 +2752,7 @@ def codegen_node(node: Node, cx: Scope):
raise CodeGenError("advanced slicing not supported yet")
func_str = codegen_node(node.func, cx)
idx_str = codegen_node(node.args[0], cx)
return "ceto_bounds_check(" + func_str + "," + idx_str + ")"
return "ceto::bounds_check(" + func_str + "," + idx_str + ")"
#raise CodeGenError("Array accesses should have been lowered in a macro pass! You've probably written your own array[access] defmacro (it's buggy)", node)
elif isinstance(node, BracedCall):
if cx.lookup_class(node.func):
Expand Down
2 changes: 1 addition & 1 deletion ceto/semanticanalysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,7 @@ def on_macro_def(mcd: MacroDefinition, replacements):
dll_options = f"-fPIC -shared -Wl,-soname,{dll_path}.so -ldl"
if sys.platform == "darwin":
dll_options = "-dynamiclib"
build_command = f"c++ -Wall -Wextra -std=c++20 {include_opts} {dll_options} -o {dll_path} {dll_cpp}"
build_command = f"c++ -Wall -Wextra -std=c++20 -O0 -g3 {include_opts} {dll_options} -o {dll_path} {dll_cpp}"

print(build_command)
try:
Expand Down
36 changes: 19 additions & 17 deletions include/ceto_checked_array_access.cth
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
include <iostream>
include <map>

namespace (ceto:

def (ceto_bounds_check, arr: mut:auto:ref:ref, index: mut:size_t:
if (index >= std.size(arr):
std.cerr << "terminating on out of bounds access"
std.terminate()
)
def (bounds_check, arr: mut:auto:ref:ref, index: mut:size_t:
if (index >= std.size(arr):
std.cerr << "terminating on out of bounds access"
std.terminate()
)

return std.forward<decltype(arr)>(arr).unsafe_at(std.forward<decltype(index)>(index));
) : decltype(auto):requires:requires(std.size(arr))
return std.forward<decltype(arr)>(arr).unsafe_at(std.forward<decltype(index)>(index));
) : decltype(auto):requires:requires(std.size(arr))

def (ceto_bounds_check, o: mut:auto:ref:ref, index: mut:auto:ref:ref:
return std.forward<decltype(o)>(o).unsafe_at(std.forward<decltype(index)>(index));
) : decltype(auto):requires:not std.is_integral_v<std.remove_cvref_t<decltype(index)>>
def (bounds_check, o: mut:auto:ref:ref, index: mut:auto:ref:ref:
return std.forward<decltype(o)>(o).unsafe_at(std.forward<decltype(index)>(index));
) : decltype(auto):requires:not std.is_integral_v<std.remove_cvref_t<decltype(index)>>

# from https://stackoverflow.com/questions/69785562/c-map-and-unordered-map-template-parameter-check-for-common-behavior-using-c/69869007#69869007
ceto_is_map_type: template<class:T>:concept = std.same_as<typename:T.value_type, std.pair<const:typename:T.key_type, typename:T.mapped_type>>
# from https://stackoverflow.com/questions/69785562/c-map-and-unordered-map-template-parameter-check-for-common-behavior-using-c/69869007#69869007
is_map_type: template<class:T>:concept = std.same_as<typename:T.value_type, std.pair<const:typename:T.key_type, typename:T.mapped_type>>

def (ceto_bounds_check, m: mut:auto:ref:ref, key: mut:auto:ref:ref:
return std.forward<decltype(m)>(m).unsafe_at(std.forward<decltype(key)>(key));
) : decltype(auto):requires:std.is_integral_v<std.remove_cvref_t<decltype(key)>> and ceto_is_map_type<std.remove_cvref_t<decltype(m)>>
def (bounds_check, m: mut:auto:ref:ref, key: mut:auto:ref:ref:
return std.forward<decltype(m)>(m).unsafe_at(std.forward<decltype(key)>(key));
) : decltype(auto):requires:std.is_integral_v<std.remove_cvref_t<decltype(key)>> and is_map_type<std.remove_cvref_t<decltype(m)>>
)

# something wacky with current 'decltype_str' ArrayAccess handling (this still needs to be a late transformation in codegen.py)
# TODO something wacky with current 'decltype_str' ArrayAccess handling (or rather the handling of bounds_check with a decltype(auto) return type in decltype_str). This needs to be a late transformation in codegen.py for now.
#defmacro (array[index], array, index:
# if (array.name() == "lambda":
# # not actually an array access (general syntax ftl)
# return None
# )
# return quote(ceto_bounds_check(unquote(array), unquote(index)))
# return quote(ceto.bounds_check(unquote(array), unquote(index)))
#)
12 changes: 8 additions & 4 deletions include/ceto_checked_array_access.donotedit.autogenerated.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@


#include "ceto.h"
#include "ceto_checked_array_access.donotedit.autogenerated.h"

#include <iostream>
;
#include <map>
;
inline auto ceto_bounds_check( auto && arr, size_t index) -> decltype(auto) requires (requires () { std::size(arr);
namespace ceto {
inline auto bounds_check( auto && arr, size_t index) -> decltype(auto) requires (requires () { std::size(arr);
}) {
if (index >= std::size(arr)) {
std::cerr << "terminating on out of bounds access";
Expand All @@ -28,12 +30,14 @@
return std::forward<decltype(arr)>(arr)[std::forward<decltype(index)>(index)];
}

inline auto ceto_bounds_check( auto && o, auto && index) -> decltype(auto) requires (!std::is_integral_v<std::remove_cvref_t<decltype(index)>>) {
inline auto bounds_check( auto && o, auto && index) -> decltype(auto) requires (!std::is_integral_v<std::remove_cvref_t<decltype(index)>>) {
return std::forward<decltype(o)>(o)[std::forward<decltype(index)>(index)];
}

template<class T> concept ceto_is_map_type = std::same_as<typename T::value_type,std::pair<const typename T::key_type,typename T::mapped_type>>;
inline auto ceto_bounds_check( auto && m, auto && key) -> decltype(auto) requires ((std::is_integral_v<std::remove_cvref_t<decltype(key)>> && ceto_is_map_type<std::remove_cvref_t<decltype(m)>>)) {
template<class T> concept is_map_type = std::same_as<typename T::value_type,std::pair<const typename T::key_type,typename T::mapped_type>>;
inline auto bounds_check( auto && m, auto && key) -> decltype(auto) requires ((std::is_integral_v<std::remove_cvref_t<decltype(key)>> && is_map_type<std::remove_cvref_t<decltype(m)>>)) {
return std::forward<decltype(m)>(m)[std::forward<decltype(key)>(key)];
}


};
22 changes: 11 additions & 11 deletions selfhost/ast.cth
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ class (Node:
_parent: Node:weak = {}

def (classname: virtual:
return typeid_name(*this)
return ceto.util.typeid_name(*this)
) : std.string

def (repr: virtual:
classname = self.classname()
csv = join(self.args, lambda(a, a.repr()), ", ")
csv = ceto.util.join(self.args, lambda(a, a.repr()), ", ")
return classname + "(" + self.func.repr() + ")([" + csv + "])"
) : std.string

Expand Down Expand Up @@ -94,7 +94,7 @@ class (Node:
return False
)

for (i in range(self.args.size()):
for (i in ceto.util.range(self.args.size()):
if (not self.args[i].equals(other.args[i]):
return False
)
Expand Down Expand Up @@ -280,7 +280,7 @@ class (Assign(BinOp):

class (NamedParameter(Assign):
def (repr: override:
return "NamedParameter("s + join(self.args, lambda(a, a.repr()), ", ") + ")"
return "NamedParameter("s + ceto.util.join(self.args, lambda(a, a.repr()), ", ") + ")"
) : std.string

def (accept: override, visitor: Visitor:mut:ref:
Expand Down Expand Up @@ -353,7 +353,7 @@ class (Call(Node):
is_one_liner_if = false

def (repr: override:
csv = join(self.args, lambda (a, a.repr()), ", ")
csv = ceto.util.join(self.args, lambda (a, a.repr()), ", ")
return self.func.repr() + "(" + csv + ")"
) : std.string

Expand All @@ -369,7 +369,7 @@ class (Call(Node):

class (ArrayAccess(Node):
def (repr: override:
csv = join(self.args, lambda (a, a.repr()), ", ")
csv = ceto.util.join(self.args, lambda (a, a.repr()), ", ")
return self.func.repr() + "[" + csv + "]"
) : std.string

Expand All @@ -385,7 +385,7 @@ class (ArrayAccess(Node):

class (BracedCall(Node):
def (repr: override:
csv = join(self.args, lambda (a, a.repr()), ", ")
csv = ceto.util.join(self.args, lambda (a, a.repr()), ", ")
return self.func.repr() + "{" + csv + "}"
) : std.string

Expand All @@ -401,7 +401,7 @@ class (BracedCall(Node):

class (Template(Node):
def (repr: override:
csv = join(self.args, lambda (a, a.repr()), ", ")
csv = ceto.util.join(self.args, lambda (a, a.repr()), ", ")
return self.func.repr() + "<" + csv + ">"
) : std.string

Expand Down Expand Up @@ -596,7 +596,7 @@ class (ListLike_(Node):

def (repr: override:
classname = self.classname()
return classname + "(" + join(self.args, lambda (a, a.repr()), ", ") + ")"
return classname + "(" + ceto.util.join(self.args, lambda (a, a.repr()), ", ") + ")"
) : std.string

def (accept: override, visitor: Visitor:mut:ref:
Expand Down Expand Up @@ -673,7 +673,7 @@ class (RedundantParens(Node):

def (repr: override:
classname = self.classname()
return classname + "(" + join(self.args, lambda (a, a.repr()), ", ") + ")"
return classname + "(" + ceto.util.join(self.args, lambda (a, a.repr()), ", ") + ")"
) : std.string

def (accept: override, visitor: Visitor:mut:ref:
Expand All @@ -693,7 +693,7 @@ class (InfixWrapper_(Node):

def (repr: override:
classname = self.classname()
return classname + "(" + join(self.args, lambda (a, a.repr()), ", ") + ")"
return classname + "(" + ceto.util.join(self.args, lambda (a, a.repr()), ", ") + ")"
) : std.string

def (accept: override, visitor: Visitor:mut:ref:
Expand Down
32 changes: 16 additions & 16 deletions selfhost/ast.donotedit.autogenerated.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ struct Node : public ceto::shared_object, public std::enable_shared_from_this<No
std::weak_ptr<const Node> _parent = {};

virtual inline auto classname() const -> std::string {
return typeid_name((*this));
return ceto::util::typeid_name((*this));
}

virtual inline auto repr() const -> std::string {
const auto classname = this -> classname();
const auto csv = join(this -> args, [](const auto &a) {
const auto csv = ceto::util::join(this -> args, [](const auto &a) {
if constexpr (!std::is_void_v<decltype((*ceto::mad(a)).repr())>) { return (*ceto::mad(a)).repr(); } else { static_cast<void>((*ceto::mad(a)).repr()); };
}, ", ");
return (((((classname + "(") + (*ceto::mad(this -> func)).repr()) + ")([") + csv) + "])");
Expand Down Expand Up @@ -123,8 +123,8 @@ struct Node : public ceto::shared_object, public std::enable_shared_from_this<No
if ((*ceto::mad(this -> args)).size() != (*ceto::mad((*ceto::mad(other)).args)).size()) {
return false;
}
for(const auto& i : range((*ceto::mad(this -> args)).size())) {
if (!(*ceto::mad(ceto_bounds_check(this -> args,i))).equals(ceto_bounds_check((*ceto::mad(other)).args,i))) {
for(const auto& i : ceto::util::range((*ceto::mad(this -> args)).size())) {
if (!(*ceto::mad(ceto::bounds_check(this -> args,i))).equals(ceto::bounds_check((*ceto::mad(other)).args,i))) {
return false;
}
}
Expand Down Expand Up @@ -155,7 +155,7 @@ struct UnOp : public Node {
std::string op;

inline auto repr() const -> std::string override {
return ((((std::string {"("} + (this -> op)) + " ") + (*ceto::mad(ceto_bounds_check(this -> args,0))).repr()) + ")");
return ((((std::string {"("} + (this -> op)) + " ") + (*ceto::mad(ceto::bounds_check(this -> args,0))).repr()) + ")");
}

inline auto accept( Visitor & visitor) const -> void override {
Expand All @@ -179,7 +179,7 @@ struct LeftAssociativeUnOp : public Node {
std::string op;

inline auto repr() const -> std::string override {
return (((("(" + (*ceto::mad(ceto_bounds_check(this -> args,0))).repr()) + " ") + (this -> op)) + ")");
return (((("(" + (*ceto::mad(ceto::bounds_check(this -> args,0))).repr()) + " ") + (this -> op)) + ")");
}

inline auto accept( Visitor & visitor) const -> void override {
Expand All @@ -203,11 +203,11 @@ struct BinOp : public Node {
std::string op;

inline auto lhs() const -> auto {
return ceto_bounds_check(this -> args,0);
return ceto::bounds_check(this -> args,0);
}

inline auto rhs() const -> auto {
return ceto_bounds_check(this -> args,1);
return ceto::bounds_check(this -> args,1);
}

inline auto repr() const -> std::string override {
Expand Down Expand Up @@ -342,7 +342,7 @@ struct NamedParameter : public Assign {
using Assign::Assign;

inline auto repr() const -> std::string override {
return ((std::string {"NamedParameter("} + join(this -> args, [](const auto &a) {
return ((std::string {"NamedParameter("} + ceto::util::join(this -> args, [](const auto &a) {
if constexpr (!std::is_void_v<decltype((*ceto::mad(a)).repr())>) { return (*ceto::mad(a)).repr(); } else { static_cast<void>((*ceto::mad(a)).repr()); };
}, ", ")) + ")");
}
Expand Down Expand Up @@ -431,7 +431,7 @@ using Node::Node;
decltype(false) is_one_liner_if = false;

inline auto repr() const -> std::string override {
const auto csv = join(this -> args, [](const auto &a) {
const auto csv = ceto::util::join(this -> args, [](const auto &a) {
if constexpr (!std::is_void_v<decltype((*ceto::mad(a)).repr())>) { return (*ceto::mad(a)).repr(); } else { static_cast<void>((*ceto::mad(a)).repr()); };
}, ", ");
return ((((*ceto::mad(this -> func)).repr() + "(") + csv) + ")");
Expand All @@ -453,7 +453,7 @@ struct ArrayAccess : public Node {
using Node::Node;

inline auto repr() const -> std::string override {
const auto csv = join(this -> args, [](const auto &a) {
const auto csv = ceto::util::join(this -> args, [](const auto &a) {
if constexpr (!std::is_void_v<decltype((*ceto::mad(a)).repr())>) { return (*ceto::mad(a)).repr(); } else { static_cast<void>((*ceto::mad(a)).repr()); };
}, ", ");
return ((((*ceto::mad(this -> func)).repr() + "[") + csv) + "]");
Expand All @@ -475,7 +475,7 @@ struct BracedCall : public Node {
using Node::Node;

inline auto repr() const -> std::string override {
const auto csv = join(this -> args, [](const auto &a) {
const auto csv = ceto::util::join(this -> args, [](const auto &a) {
if constexpr (!std::is_void_v<decltype((*ceto::mad(a)).repr())>) { return (*ceto::mad(a)).repr(); } else { static_cast<void>((*ceto::mad(a)).repr()); };
}, ", ");
return ((((*ceto::mad(this -> func)).repr() + "{") + csv) + "}");
Expand All @@ -497,7 +497,7 @@ struct Template : public Node {
using Node::Node;

inline auto repr() const -> std::string override {
const auto csv = join(this -> args, [](const auto &a) {
const auto csv = ceto::util::join(this -> args, [](const auto &a) {
if constexpr (!std::is_void_v<decltype((*ceto::mad(a)).repr())>) { return (*ceto::mad(a)).repr(); } else { static_cast<void>((*ceto::mad(a)).repr()); };
}, ", ");
return ((((*ceto::mad(this -> func)).repr() + "<") + csv) + ">");
Expand Down Expand Up @@ -728,7 +728,7 @@ struct ListLike_ : public Node {

inline auto repr() const -> std::string override {
const auto classname = this -> classname();
return (((classname + "(") + join(this -> args, [](const auto &a) {
return (((classname + "(") + ceto::util::join(this -> args, [](const auto &a) {
if constexpr (!std::is_void_v<decltype((*ceto::mad(a)).repr())>) { return (*ceto::mad(a)).repr(); } else { static_cast<void>((*ceto::mad(a)).repr()); };
}, ", ")) + ")");
}
Expand Down Expand Up @@ -830,7 +830,7 @@ struct RedundantParens : public Node {

inline auto repr() const -> std::string override {
const auto classname = this -> classname();
return (((classname + "(") + join(this -> args, [](const auto &a) {
return (((classname + "(") + ceto::util::join(this -> args, [](const auto &a) {
if constexpr (!std::is_void_v<decltype((*ceto::mad(a)).repr())>) { return (*ceto::mad(a)).repr(); } else { static_cast<void>((*ceto::mad(a)).repr()); };
}, ", ")) + ")");
}
Expand All @@ -855,7 +855,7 @@ struct InfixWrapper_ : public Node {

inline auto repr() const -> std::string override {
const auto classname = this -> classname();
return (((classname + "(") + join(this -> args, [](const auto &a) {
return (((classname + "(") + ceto::util::join(this -> args, [](const auto &a) {
if constexpr (!std::is_void_v<decltype((*ceto::mad(a)).repr())>) { return (*ceto::mad(a)).repr(); } else { static_cast<void>((*ceto::mad(a)).repr()); };
}, ", ")) + ")");
}
Expand Down
Loading

0 comments on commit 388134e

Please sign in to comment.