aboutsummaryrefslogtreecommitdiff
path: root/include/ast.h
blob: b63dab58e64f32db0e5bf0746948d7d5f90047d2 (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
#ifndef AMALGAM_AST_H
#define AMALGAM_AST_H

#include "defs.h"
#include "std/buffer_view.h"
#include "std/buffer.h"
#include "std/misc.h"
#include "std/scoped_allocator.h"
#include "std/hash_map.h"
#include "binop_type.h"

#include <setjmp.h>

/* General error */
#define AST_ERR -1
#define AST_ERR_DEF_DUP -20

typedef struct FunctionDecl FunctionDecl;
typedef struct FunctionCall FunctionCall;
typedef struct LhsExpr LhsExpr;
typedef struct Import Import;
typedef struct String String;
typedef struct Variable Variable;
typedef struct Number Number;
typedef struct Binop Binop;

typedef union {
    void *data;
    FunctionDecl *func_decl;
    FunctionCall *func_call;
    LhsExpr *lhs_expr;
    Import *import;
    String *string;
    Number *number;
    Variable *variable;
    Binop *binop;
} AstValue;

typedef enum {
    AST_NONE,
    AST_FUNCTION_DECL,
    AST_FUNCTION_CALL,
    AST_LHS,
    AST_IMPORT,
    AST_STRING,
    AST_NUMBER,
    AST_VARIABLE,
    AST_BINOP
} AstType;

typedef enum {
    AST_NOT_RESOLVED,
    AST_RESOLVING,
    AST_RESOLVED
} AstResolveStatus;

typedef struct {
    AstValue value;
    AstType type;
    AstResolveStatus resolve_status;
} Ast;

struct Scope {
    Buffer ast_objects;
    HashMap/*(key=BufferView, value=Ast)*/ named_objects;
};

struct FunctionDecl {
    Scope body;
};

struct FunctionCall {
    BufferView name;
    Buffer/*Ast*/ args;
};

struct LhsExpr {
    int is_const;
    BufferView type_name;
    BufferView var_name;
    Ast rhs_expr;
};

struct Import {
    BufferView path;
};

struct String {
    BufferView str;
};

struct Number {
    union {
        i64 integer;
        f64 floating;
    } value;
    bool is_integer;
};

struct Variable {
    BufferView name;
    Ast resolved_variable;
};

struct Binop {
    Ast lhs;
    Ast rhs;
    BinopType type;
    /* Is the binop already ordered - no need to reorder it */
    bool grouped;
};

typedef struct {
    jmp_buf env;
    Parser *parser;
} AstCompilerContext;

Ast ast_none();
void ast_init(Ast *self, void *value, AstType type);
BufferView ast_get_name(Ast *self);

CHECK_RESULT int funcdecl_init(FunctionDecl *self, ScopedAllocator *allocator);
CHECK_RESULT int funccall_init(FunctionCall *self, BufferView name, ScopedAllocator *allocator);
void lhsexpr_init(LhsExpr *self, int isConst, BufferView var_name);
void import_init(Import *self, BufferView path);
CHECK_RESULT int string_init(String *self, BufferView str);
void number_init(Number *self, i64 value, bool is_integer);
void variable_init(Variable *self, BufferView name);
void binop_init(Binop *self);

CHECK_RESULT int scope_init(Scope *self, ScopedAllocator *allocator);
CHECK_RESULT int scope_add_child(Scope *self, Ast *child);
/* longjump to compiler env on failure */
void scope_resolve(Scope *self, AstCompilerContext *context);

#endif