aboutsummaryrefslogtreecommitdiff
path: root/include/ast.h
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2019-06-07 10:47:47 +0200
committerdec05eba <dec05eba@protonmail.com>2020-07-25 14:36:46 +0200
commit53a331bc8b2fc33bd2b7e25a23b4128f89ee0b52 (patch)
tree2e5c0f4cda85b2afdb6142145fa7ded61d100f02 /include/ast.h
parent1b68fdcf5aebf2bc53bbd9234c77aea243c0decd (diff)
Add assignment, while, extern, function signature type, start on bytecode
Diffstat (limited to 'include/ast.h')
-rw-r--r--include/ast.h67
1 files changed, 63 insertions, 4 deletions
diff --git a/include/ast.h b/include/ast.h
index c40be22..34e62b8 100644
--- a/include/ast.h
+++ b/include/ast.h
@@ -17,16 +17,22 @@
#define AST_ERR -1
#define AST_ERR_DEF_DUP -20
+typedef struct FunctionSignature FunctionSignature;
typedef struct FunctionCall FunctionCall;
typedef struct StructDecl StructDecl;
typedef struct StructField StructField;
typedef struct LhsExpr LhsExpr;
+typedef struct AssignmentExpr AssignmentExpr;
typedef struct Import Import;
typedef struct String String;
typedef struct Variable Variable;
typedef struct Number Number;
typedef struct Binop Binop;
+typedef struct IfStatement IfStatement;
+typedef struct ElseIfStatement ElseIfStatement;
+typedef struct WhileStatement WhileStatement;
+/* TODO: Instead of using pointers, use the data directly */
typedef union {
void *data;
FunctionDecl *func_decl;
@@ -34,11 +40,14 @@ typedef union {
StructDecl *struct_decl;
StructField *struct_field;
LhsExpr *lhs_expr;
+ AssignmentExpr *assign_expr;
Import *import;
String *string;
Number *number;
Variable *variable;
Binop *binop;
+ IfStatement *if_stmt;
+ WhileStatement *while_stmt;
} AstValue;
typedef enum {
@@ -47,11 +56,14 @@ typedef enum {
AST_STRUCT_DECL,
AST_STRUCT_FIELD,
AST_LHS,
+ AST_ASSIGN,
AST_IMPORT,
AST_STRING,
AST_NUMBER,
AST_VARIABLE,
- AST_BINOP
+ AST_BINOP,
+ AST_IF_STATEMENT,
+ AST_WHILE_STATEMENT
} AstType;
typedef enum {
@@ -88,9 +100,15 @@ struct FileScopeReference {
struct Variable {
BufferView name;
+ Ast *resolved_var; /* resolved_var will always be a LhsExpr after resolved */
+};
+
+struct FunctionSignature {
+ int params; /* TODO: Implement signatures */
};
struct FunctionDecl {
+ FunctionSignature *signature;
Scope body;
SsaFuncIndex ssa_func_index;
};
@@ -109,11 +127,25 @@ struct StructField {
Variable type;
};
+typedef struct {
+ enum {
+ VARIABLE_TYPE_NONE,
+ VARIABLE_TYPE_VARIABLE,
+ VARIABLE_TYPE_SIGNATURE
+ } type;
+
+ union {
+ Variable *variable;
+ FunctionSignature *signature;
+ } value;
+} VariableType;
+
struct LhsExpr {
+ bool is_extern;
bool is_pub;
bool is_const;
BufferView var_name;
- Variable type;
+ VariableType type;
Ast *rhs_expr;
/*
TODO: Find a better way to store this. This most likely will use too much memory.
@@ -121,6 +153,11 @@ struct LhsExpr {
amal_mutex *mutex;
};
+struct AssignmentExpr {
+ Ast *lhs_expr;
+ Ast *rhs_expr;
+};
+
struct Import {
BufferView path;
FileScopeReference *file_scope;
@@ -147,6 +184,24 @@ struct Binop {
bool grouped;
};
+struct IfStatement {
+ Ast *condition;
+ Scope body;
+ ElseIfStatement *else_if_stmt;
+};
+
+/* ElseIfStatement where condition == NULL is an Else statement */
+struct ElseIfStatement {
+ Ast *condition;
+ Scope body;
+ ElseIfStatement *next_else_if_stmt;
+};
+
+struct WhileStatement {
+ Ast *condition;
+ Scope body;
+};
+
typedef struct {
jmp_buf env;
amal_compiler *compiler;
@@ -156,16 +211,20 @@ typedef struct {
CHECK_RESULT int ast_create(ScopedAllocator *allocator, void *value, AstType type, Ast **result);
BufferView ast_get_name(Ast *self);
-CHECK_RESULT int funcdecl_init(FunctionDecl *self, Scope *parent, ScopedAllocator *allocator);
+CHECK_RESULT int funcdecl_init(FunctionDecl *self, FunctionSignature *signature, Scope *parent, ScopedAllocator *allocator);
CHECK_RESULT int funccall_init(FunctionCall *self, BufferView name, ScopedAllocator *allocator);
CHECK_RESULT int structdecl_init(StructDecl *self, Scope *parent, ScopedAllocator *allocator);
void structfield_init(StructField *self, BufferView name, BufferView type_name);
-CHECK_RESULT int lhsexpr_init(LhsExpr *self, bool is_pub, bool is_const, BufferView var_name, ScopedAllocator *allocator);
+CHECK_RESULT int lhsexpr_init(LhsExpr *self, bool is_extern, bool is_pub, bool is_const, BufferView var_name, ScopedAllocator *allocator);
+void assignmentexpr_init(AssignmentExpr *self, Ast *lhs_expr, Ast *rhs_expr);
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, BufferView code_ref);
void variable_init(Variable *self, BufferView name);
void binop_init(Binop *self);
+CHECK_RESULT int if_statement_init(IfStatement *self, Scope *parent, ScopedAllocator *allocator);
+CHECK_RESULT int else_if_statement_init(ElseIfStatement *self, Scope *parent, ScopedAllocator *allocator);
+CHECK_RESULT int while_statement_init(WhileStatement *self, Scope *parent, ScopedAllocator *allocator);
CHECK_RESULT int scope_init(Scope *self, Scope *parent, ScopedAllocator *allocator);
CHECK_RESULT int file_scope_reference_init(FileScopeReference *self, BufferView canonical_path, ScopedAllocator *allocator);