diff options
Diffstat (limited to 'src/ast')
-rw-r--r-- | src/ast/ast.cpp | 102 | ||||
-rw-r--r-- | src/ast/ast.hpp | 339 |
2 files changed, 370 insertions, 71 deletions
diff --git a/src/ast/ast.cpp b/src/ast/ast.cpp index a39bc0d4..29b551ed 100644 --- a/src/ast/ast.cpp +++ b/src/ast/ast.cpp @@ -32,7 +32,7 @@ const ::std::vector<TypeRef>& PathNode::args() const Pattern::Pattern(TagMaybeBind, ::std::string name)
{
}
-Pattern::Pattern(TagValue, ExprNode node)
+Pattern::Pattern(TagValue, ::std::unique_ptr<ExprNode> node)
{
}
Pattern::Pattern(TagEnumVariant, Path path, ::std::vector<Pattern> sub_patterns)
@@ -40,9 +40,6 @@ Pattern::Pattern(TagEnumVariant, Path path, ::std::vector<Pattern> sub_patterns) }
-Function::Function(::std::string name, TypeParams params, Class fcn_class, TypeRef ret_type, ::std::vector<StructItem> args, Expr code)
-{
-}
Impl::Impl(TypeRef impl_type, TypeRef trait_type)
{
@@ -51,8 +48,9 @@ void Impl::add_function(bool is_public, Function fcn) {
}
-void Crate::iterate_functions(Crate::fcn_visitor_t* visitor)
+void Crate::iterate_functions(fcn_visitor_t* visitor)
{
+ m_root_module.iterate_functions(visitor, *this);
}
void Module::add_constant(bool is_public, ::std::string name, TypeRef type, Expr val)
@@ -68,23 +66,106 @@ void Module::add_struct(bool is_public, ::std::string name, TypeParams params, : }
void Module::add_function(bool is_public, Function func)
{
+ for( const auto fcn_item : m_functions )
+ {
+ if( fcn_item.first.name() == func.name() ) {
+ throw ParseError::Todo("duplicate function definition");
+ }
+ }
+ m_functions.push_back( ::std::make_pair(func, is_public) );
}
void Module::add_impl(Impl impl)
{
}
-void Expr::visit_nodes(const NodeVisitor& v)
+void Module::iterate_functions(fcn_visitor_t *visitor, const Crate& crate)
{
- throw ParseError::Todo("Expr::visit_nodes");
+ for( auto fcn_item : this->m_functions )
+ {
+ visitor(crate, *this, fcn_item.first);
+ }
}
-ExprNode::ExprNode()
+void Expr::visit_nodes(NodeVisitor& v)
{
+ m_node->visit(v);
+}
+ExprNode::~ExprNode() {
}
-ExprNode::ExprNode(TagBlock, ::std::vector<ExprNode> nodes)
-{
+
+ExprNode_Block::~ExprNode_Block() {
+}
+void ExprNode_Block::visit(NodeVisitor& nv) {
+ for( auto& node : m_nodes ) {
+ if( node.get() )
+ node->visit(nv);
+ }
+ nv.visit(*this);
+}
+
+void ExprNode_Return::visit(NodeVisitor& nv) {
+ if( m_value.get() )
+ m_value->visit(nv);
+ nv.visit(*this);
+}
+
+void ExprNode_LetBinding::visit(NodeVisitor& nv) {
+ if( m_value.get() )
+ m_value->visit(nv);
+ nv.visit(*this);
+}
+
+void ExprNode_Assign::visit(NodeVisitor& nv) {
+ if( m_slot.get() )
+ m_slot->visit(nv);
+ if( m_value.get() )
+ m_value->visit(nv);
+ nv.visit(*this);
+}
+
+void ExprNode_CallPath::visit(NodeVisitor& nv) {
+ nv.visit(*this);
+}
+
+void ExprNode_CallObject::visit(NodeVisitor& nv) {
+ nv.visit(*this);
}
+
+void ExprNode_Match::visit(NodeVisitor& nv) {
+ nv.visit(*this);
+}
+
+void ExprNode_If::visit(NodeVisitor& nv) {
+ nv.visit(*this);
+}
+
+void ExprNode_Integer::visit(NodeVisitor& nv) {
+ nv.visit(*this);
+}
+
+void ExprNode_StructLiteral::visit(NodeVisitor& nv) {
+ nv.visit(*this);
+}
+
+void ExprNode_NamedValue::visit(NodeVisitor& nv) {
+ nv.visit(*this);
+}
+
+void ExprNode_Field::visit(NodeVisitor& nv) {
+ nv.visit(*this);
+}
+
+void ExprNode_Cast::visit(NodeVisitor& nv) {
+ nv.visit(*this);
+}
+void ExprNode_BinOp::visit(NodeVisitor& nv) {
+ m_left->visit(nv);
+ m_right->visit(nv);
+ nv.visit(*this);
+}
+
+#if 0
ExprNode::ExprNode(TagLetBinding, Pattern pat, ExprNode value)
{
}
@@ -121,6 +202,7 @@ ExprNode::ExprNode(TagField, ::std::string name) ExprNode::ExprNode(TagBinOp, BinOpType type, ExprNode left, ExprNode right)
{
}
+#endif
TypeParam::TypeParam(bool is_lifetime, ::std::string name)
{
diff --git a/src/ast/ast.hpp b/src/ast/ast.hpp index ac153458..58f1e06c 100644 --- a/src/ast/ast.hpp +++ b/src/ast/ast.hpp @@ -11,6 +11,11 @@ namespace AST {
+using ::std::unique_ptr;
+using ::std::move;
+
+class Crate;
+
class MetaItem
{
::std::string m_name;
@@ -39,85 +44,279 @@ public: Pattern(TagMaybeBind, ::std::string name);
struct TagValue {};
- Pattern(TagValue, ExprNode node);
+ Pattern(TagValue, unique_ptr<ExprNode> node);
struct TagEnumVariant {};
Pattern(TagEnumVariant, Path path, ::std::vector<Pattern> sub_patterns);
};
+class NodeVisitor;
+
class ExprNode
{
public:
- ExprNode();
-
- struct TagBlock {};
- ExprNode(TagBlock, ::std::vector<ExprNode> nodes);
-
- struct TagLetBinding {};
- ExprNode(TagLetBinding, Pattern pat, ExprNode value);
-
- struct TagReturn {};
- ExprNode(TagReturn, ExprNode val);
-
- struct TagAssign {};
- ExprNode(TagAssign, ExprNode slot, ExprNode value) {}
-
- struct TagCast {};
- ExprNode(TagCast, ExprNode value, TypeRef dst_type);
-
- struct TagInteger {};
- ExprNode(TagInteger, uint64_t value, enum eCoreType datatype);
-
- struct TagStructLiteral {};
- ExprNode(TagStructLiteral, Path path, ExprNode base_value, ::std::vector< ::std::pair< ::std::string,ExprNode> > values );
-
- struct TagCallPath {};
- ExprNode(TagCallPath, Path path, ::std::vector<ExprNode> args);
+ virtual ~ExprNode() = 0;
+
+ virtual void visit(NodeVisitor& nv) = 0;
+};
- struct TagCallObject {};
- ExprNode(TagCallObject, ExprNode val, ::std::vector<ExprNode> args);
+class ExprNode_Block:
+ public ExprNode
+{
+ ::std::vector< ::std::unique_ptr<ExprNode> > m_nodes;
+public:
+ ExprNode_Block(const ExprNode_Block& x) = delete;
+ ExprNode_Block(::std::vector< ::std::unique_ptr<ExprNode> >&& nodes):
+ m_nodes( move(nodes) )
+ {
+ }
+ virtual ~ExprNode_Block() override;
+
+ virtual void visit(NodeVisitor& nv) override;
+};
- struct TagMatch {};
- ExprNode(TagMatch, ExprNode val, ::std::vector< ::std::pair<Pattern,ExprNode> > arms);
+// Return a value
+class ExprNode_Return:
+ public ExprNode
+{
+ unique_ptr<ExprNode> m_value;
+public:
+ ExprNode_Return(unique_ptr<ExprNode>&& value):
+ m_value( move(value) )
+ {
+ }
+
+ virtual void visit(NodeVisitor& nv) override;
+};
+class ExprNode_LetBinding:
+ public ExprNode
+{
+ Pattern m_pat;
+ unique_ptr<ExprNode> m_value;
+public:
+ ExprNode_LetBinding(Pattern pat, unique_ptr<ExprNode>&& value):
+ m_pat( move(pat) ),
+ m_value( move(value) )
+ {
+ }
+
+ virtual void visit(NodeVisitor& nv) override;
+};
+class ExprNode_Assign:
+ public ExprNode
+{
+ unique_ptr<ExprNode> m_slot;
+ unique_ptr<ExprNode> m_value;
+public:
+ ExprNode_Assign(unique_ptr<ExprNode>&& slot, unique_ptr<ExprNode>&& value):
+ m_slot( move(slot) ),
+ m_value( move(value) )
+ {
+ }
+
+ virtual void visit(NodeVisitor& nv) override;
+};
+class ExprNode_CallPath:
+ public ExprNode
+{
+ Path m_path;
+ ::std::vector<unique_ptr<ExprNode>> m_args;
+public:
+ ExprNode_CallPath(Path&& path, ::std::vector<unique_ptr<ExprNode>>&& args):
+ m_path( move(path) ),
+ m_args( move(args) )
+ {
+ }
+
+ virtual void visit(NodeVisitor& nv) override;
+};
+// Call an object (Fn/FnMut/FnOnce)
+class ExprNode_CallObject:
+ public ExprNode
+{
+ unique_ptr<ExprNode> m_val;
+ ::std::vector<unique_ptr<ExprNode>> m_args;
+public:
+ ExprNode_CallObject(unique_ptr<ExprNode>&& val, ::std::vector< unique_ptr<ExprNode> >&& args):
+ m_val( move(val) ),
+ m_args( move(args) )
+ {
+ }
+ virtual void visit(NodeVisitor& nv) override;
+};
- struct TagIf {};
- ExprNode(TagIf, ExprNode cond, ExprNode true_code, ExprNode false_code);
+class ExprNode_Match:
+ public ExprNode
+{
+ typedef ::std::vector< ::std::pair<Pattern,unique_ptr<ExprNode> > > arm_t;
+ unique_ptr<ExprNode> m_val;
+ arm_t m_arms;
+public:
+ ExprNode_Match(unique_ptr<ExprNode>&& val, arm_t&& arms):
+ m_val( ::std::move(val) ),
+ m_arms( ::std::move(arms) )
+ {
+ }
+ virtual void visit(NodeVisitor& nv) override;
+};
- struct TagNamedValue {};
- ExprNode(TagNamedValue, Path path);
+class ExprNode_If:
+ public ExprNode
+{
+ unique_ptr<ExprNode> m_cond;
+ unique_ptr<ExprNode> m_true;
+ unique_ptr<ExprNode> m_false;
+public:
+ ExprNode_If(unique_ptr<ExprNode>&& cond, unique_ptr<ExprNode>&& true_code, unique_ptr<ExprNode>&& false_code):
+ m_cond( ::std::move(cond) ),
+ m_true( ::std::move(true_code) ),
+ m_false( ::std::move(false_code) )
+ {
+ }
+ virtual void visit(NodeVisitor& nv) override;
+};
+// Literal integer
+class ExprNode_Integer:
+ public ExprNode
+{
+ enum eCoreType m_datatype;
+ uint64_t m_value;
+public:
+ ExprNode_Integer(uint64_t value, enum eCoreType datatype):
+ m_datatype(datatype),
+ m_value(value)
+ {
+ }
+
+ virtual void visit(NodeVisitor& nv) override;
+};
+// Literal structure
+class ExprNode_StructLiteral:
+ public ExprNode
+{
+ typedef ::std::vector< ::std::pair< ::std::string, unique_ptr<ExprNode> > > t_values;
+ Path m_path;
+ unique_ptr<ExprNode> m_base_value;
+ t_values m_values;
+public:
+ ExprNode_StructLiteral(Path path, unique_ptr<ExprNode>&& base_value, t_values&& values ):
+ m_path( move(path) ),
+ m_base_value( move(base_value) ),
+ m_values( move(values) )
+ {}
+
+ virtual void visit(NodeVisitor& nv) override;
+};
+// Variable / Constant
+class ExprNode_NamedValue:
+ public ExprNode
+{
+ Path m_path;
+public:
+ ExprNode_NamedValue(Path&& path):
+ m_path( ::std::move(path) )
+ {
+ }
+ virtual void visit(NodeVisitor& nv) override;
+};
+// Field dereference
+class ExprNode_Field:
+ public ExprNode
+{
+ ::std::unique_ptr<ExprNode> m_obj;
+ ::std::string m_name;
+public:
+ ExprNode_Field(::std::unique_ptr<ExprNode>&& obj, ::std::string&& name):
+ m_obj( ::std::move(obj) ),
+ m_name( ::std::move(name) )
+ {
+ }
+ virtual void visit(NodeVisitor& nv) override;
+};
- struct TagField {};
- ExprNode(TagField, ::std::string name);
+// Type cast ('as')
+class ExprNode_Cast:
+ public ExprNode
+{
+ unique_ptr<ExprNode> m_value;
+ TypeRef m_type;
+public:
+ ExprNode_Cast(unique_ptr<ExprNode>&& value, TypeRef&& dst_type):
+ m_value( move(value) ),
+ m_type( move(dst_type) )
+ {
+ }
+ virtual void visit(NodeVisitor& nv) override;
+};
- enum BinOpType {
- BINOP_CMPEQU,
- BINOP_CMPNEQU,
+// Binary operation
+class ExprNode_BinOp:
+ public ExprNode
+{
+public:
+ enum Type {
+ CMPEQU,
+ CMPNEQU,
- BINOP_BITAND,
- BINOP_BITOR,
- BINOP_BITXOR,
+ BITAND,
+ BITOR,
+ BITXOR,
- BINOP_SHL,
- BINOP_SHR,
+ SHL,
+ SHR,
};
- struct TagBinOp {};
- ExprNode(TagBinOp, BinOpType type, ExprNode left, ExprNode right);
+private:
+ Type m_type;
+ ::std::unique_ptr<ExprNode> m_left;
+ ::std::unique_ptr<ExprNode> m_right;
+public:
+ ExprNode_BinOp(const ExprNode_Block& x) = delete;
+ ExprNode_BinOp(Type type, ::std::unique_ptr<ExprNode> left, ::std::unique_ptr<ExprNode> right):
+ m_type(type),
+ m_left( ::std::move(left) ),
+ m_right( ::std::move(right) )
+ {
+ }
+ virtual ~ExprNode_BinOp() override {}
+
+ virtual void visit(NodeVisitor& nv) override;
};
class NodeVisitor
{
public:
- virtual void visit(ExprNode::TagBlock, ExprNode& node) {}
- virtual void visit(ExprNode::TagNamedValue, ExprNode& node) {}
+ virtual void visit(ExprNode_Block& node) {}
+ virtual void visit(ExprNode_Return& node) {}
+ virtual void visit(ExprNode_LetBinding& node) {}
+ virtual void visit(ExprNode_Assign& node) {}
+ virtual void visit(ExprNode_CallPath& node) {}
+ virtual void visit(ExprNode_CallObject& node) {}
+ virtual void visit(ExprNode_Match& node) {}
+ virtual void visit(ExprNode_If& node) {}
+
+ virtual void visit(ExprNode_Integer& node) {}
+ virtual void visit(ExprNode_StructLiteral& node) {}
+ virtual void visit(ExprNode_NamedValue& node) {}
+
+ virtual void visit(ExprNode_Field& node) {}
+ virtual void visit(ExprNode_Cast& node) {}
+ virtual void visit(ExprNode_BinOp& node) {}
};
class Expr
{
+ ::std::shared_ptr<ExprNode> m_node;
public:
- Expr() {}
- Expr(ExprNode node) {}
+ Expr(unique_ptr<ExprNode> node):
+ m_node(node.release())
+ {
+ }
+ Expr(ExprNode* node):
+ m_node(node)
+ {
+ }
- void visit_nodes(const NodeVisitor& v);
+ void visit_nodes(NodeVisitor& v);
};
class Function
@@ -133,23 +332,36 @@ public: typedef ::std::vector<StructItem> Arglist;
private:
+ ::std::string m_name;
+ TypeParams m_generic_params;
+ Class m_fcn_class;
Expr m_code;
TypeRef m_rettype;
Arglist m_args;
public:
-
- Function(::std::string name, TypeParams params, Class fcn_class, TypeRef ret_type, Arglist args, Expr code);
+ Function(::std::string name, TypeParams params, Class fcn_class, TypeRef ret_type, Arglist args, Expr code):
+ m_name(name),
+ m_generic_params(params),
+ m_fcn_class(fcn_class),
+ m_code(code),
+ m_rettype(ret_type),
+ m_args(args)
+ {
+ }
+
+ const ::std::string& name() const { return m_name; }
+
+ TypeParams& generic_params() { return m_generic_params; }
+ const TypeParams& generic_params() const { return m_generic_params; }
Expr& code() { return m_code; }
- const Expr code() const { return m_code; }
+ const Expr& code() const { return m_code; }
const TypeRef& rettype() const { return m_rettype; }
TypeRef& rettype() { return m_rettype; }
const Arglist& args() const { return m_args; }
Arglist& args() { return m_args; }
-
- const char* name() const { return "TODO"; }
};
class Impl
@@ -160,9 +372,14 @@ public: void add_function(bool is_public, Function fcn);
};
+
+class Module;
+
+typedef void fcn_visitor_t(const AST::Crate& crate, const AST::Module& mod, Function& fcn);
+
class Module
{
- ::std::vector<Function> m_functions;
+ ::std::vector< ::std::pair<Function,bool> > m_functions;
public:
void add_alias(bool is_public, Path path) {}
void add_constant(bool is_public, ::std::string name, TypeRef type, Expr val);
@@ -170,6 +387,8 @@ public: void add_struct(bool is_public, ::std::string name, TypeParams params, ::std::vector<StructItem> items);
void add_function(bool is_public, Function func);
void add_impl(Impl impl);
+
+ void iterate_functions(fcn_visitor_t* visitor, const Crate& crate);
};
class Crate
@@ -181,8 +400,6 @@ public: {
}
- typedef void fcn_visitor_t(const AST::Crate& crate, Function& fcn);
-
void iterate_functions( fcn_visitor_t* visitor );
};
@@ -201,8 +418,8 @@ class Flat ::std::vector<Function> m_functions;
public:
- const ::std::vector<Function>& functions() const;
- const ::std::vector<CStruct>& structs() const;
+ const ::std::vector<Function>& functions() const { return m_functions; }
+ const ::std::vector<CStruct>& structs() const { return m_structs; }
};
}
|