summaryrefslogtreecommitdiff
path: root/src/ast/ast.hpp
blob: ac1534580a6c3f594a267cf36016f6890cc912ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#ifndef AST_HPP_INCLUDED
#define AST_HPP_INCLUDED

#include <string>
#include <vector>
#include <stdexcept>
#include "../coretypes.hpp"
#include <memory>

#include "../types.hpp"

namespace AST {

class MetaItem
{
    ::std::string   m_name;
    ::std::vector<MetaItem> m_items;
    ::std::string   m_str_val;
public:
    MetaItem(::std::string name):
        m_name(name)
    {
    }
    MetaItem(::std::string name, ::std::vector<MetaItem> items):
        m_name(name),
        m_items(items)
    {
    }
};

class ExprNode;

class Pattern
{
public:
    Pattern();

    struct TagMaybeBind {};
    Pattern(TagMaybeBind, ::std::string name);

    struct TagValue {};
    Pattern(TagValue, ExprNode node);

    struct TagEnumVariant {};
    Pattern(TagEnumVariant, Path path, ::std::vector<Pattern> sub_patterns);
};

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);

    struct TagCallObject {};
    ExprNode(TagCallObject, ExprNode val, ::std::vector<ExprNode> args);

    struct TagMatch {};
    ExprNode(TagMatch, ExprNode val, ::std::vector< ::std::pair<Pattern,ExprNode> > arms);

    struct TagIf {};
    ExprNode(TagIf, ExprNode cond, ExprNode true_code, ExprNode false_code);

    struct TagNamedValue {};
    ExprNode(TagNamedValue, Path path);

    struct TagField {};
    ExprNode(TagField, ::std::string name);

    enum BinOpType {
        BINOP_CMPEQU,
        BINOP_CMPNEQU,

        BINOP_BITAND,
        BINOP_BITOR,
        BINOP_BITXOR,

        BINOP_SHL,
        BINOP_SHR,
    };
    struct TagBinOp {};
    ExprNode(TagBinOp, BinOpType type, ExprNode left, ExprNode right);
};

class NodeVisitor
{
public:
    virtual void visit(ExprNode::TagBlock, ExprNode& node) {}
    virtual void visit(ExprNode::TagNamedValue, ExprNode& node) {}
};

class Expr
{
public:
    Expr() {}
    Expr(ExprNode node) {}

    void visit_nodes(const NodeVisitor& v);
};

class Function
{
public:
    enum Class
    {
        CLASS_UNBOUND,
        CLASS_REFMETHOD,
        CLASS_MUTMETHOD,
        CLASS_VALMETHOD,
    };
    typedef ::std::vector<StructItem>   Arglist;

private:
    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);

    Expr& code() { 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
{
public:
    Impl(TypeRef impl_type, TypeRef trait_type);

    void add_function(bool is_public, Function fcn);
};

class Module
{
    ::std::vector<Function> m_functions;
public:
    void add_alias(bool is_public, Path path) {}
    void add_constant(bool is_public, ::std::string name, TypeRef type, Expr val);
    void add_global(bool is_public, bool is_mut, ::std::string name, TypeRef type, Expr val);
    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);
};

class Crate
{
    Module  m_root_module;
public:
    Crate(Module root_module):
        m_root_module(root_module)
    {
    }

    typedef void fcn_visitor_t(const AST::Crate& crate, Function& fcn);

    void iterate_functions( fcn_visitor_t* visitor );
};

class CStruct
{
    ::std::vector<StructItem>   m_fields;
public:
    const char* name() const { return "TODO"; }
    const char* mangled_name() const { return "TODO"; }
    const ::std::vector<StructItem>& fields() const { return m_fields; }
};

class Flat
{
    ::std::vector<CStruct>  m_structs;
    ::std::vector<Function> m_functions;
public:
    
    const ::std::vector<Function>& functions() const;
    const ::std::vector<CStruct>& structs() const;
};

}

#endif // AST_HPP_INCLUDED