From a52fdf470aa2c164108aeccc2c83bad62208913c Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 9 Mar 2019 14:14:09 +0100 Subject: Start on resolving ast. Add recursive declaration check --- include/ast.h | 25 +++++++++++++++++++++---- include/defs.h | 1 + include/parser.h | 4 ++-- include/tokenizer.h | 3 +-- 4 files changed, 25 insertions(+), 8 deletions(-) (limited to 'include') diff --git a/include/ast.h b/include/ast.h index 1d40a89..f0020d7 100644 --- a/include/ast.h +++ b/include/ast.h @@ -1,6 +1,7 @@ #ifndef AMALGAM_AST_H #define AMALGAM_AST_H +#include "defs.h" #include "std/buffer_view.h" #include "std/buffer.h" #include "std/misc.h" @@ -8,6 +9,8 @@ #include "std/hash_map.h" #include "binop_type.h" +#include + /* General error */ #define AST_ERR -1 #define AST_ERR_DEF_DUP -20 @@ -45,9 +48,16 @@ typedef enum { 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 { @@ -56,17 +66,16 @@ struct Scope { }; struct FunctionDecl { - BufferView name; Scope body; }; struct FunctionCall { BufferView name; - Buffer args; + Buffer/*Ast*/ args; }; struct LhsExpr { - int isConst; + int is_const; BufferView type_name; BufferView var_name; Ast rhs_expr; @@ -90,6 +99,7 @@ struct Number { struct Variable { BufferView name; + Ast resolved_variable; }; struct Binop { @@ -100,7 +110,13 @@ struct Binop { bool grouped; }; +typedef struct { + jmp_buf env; + Parser *parser; +} AstCompilerContext; + Ast ast_none(); +void ast_init(Ast *self, AstValue value, AstType type); BufferView ast_get_name(Ast *self); CHECK_RESULT int funcdecl_init(FunctionDecl *self, ScopedAllocator *allocator); @@ -109,10 +125,11 @@ 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); -void scope_resolve(Scope *self); +void scope_resolve(Scope *self, AstCompilerContext *context); #endif diff --git a/include/defs.h b/include/defs.h index 2baceb4..6a9bca4 100644 --- a/include/defs.h +++ b/include/defs.h @@ -3,5 +3,6 @@ typedef struct ParserThreadData ParserThreadData; typedef struct amal_compiler amal_compiler; +typedef struct Parser Parser; #endif diff --git a/include/parser.h b/include/parser.h index 5c055d9..20f02fe 100644 --- a/include/parser.h +++ b/include/parser.h @@ -32,7 +32,7 @@ typedef enum { ERROR_CONTEXT_RHS_START } ErrorContext; -typedef struct { +struct Parser { Tokenizer tokenizer; Scope scope; ScopedAllocator *allocator; /* borrowed. Copied from @compiler for faster access to allocator */ @@ -41,7 +41,7 @@ typedef struct { TokenizerError error; ErrorContext error_context; jmp_buf parse_env; -} Parser; +}; CHECK_RESULT int parser_thread_data_init(ParserThreadData *self); CHECK_RESULT int parser_thread_data_deinit(ParserThreadData *self); diff --git a/include/tokenizer.h b/include/tokenizer.h index b38cc58..a06b689 100644 --- a/include/tokenizer.h +++ b/include/tokenizer.h @@ -36,7 +36,6 @@ typedef struct { BufferView code; int index; int prev_index; - int line; Token token; /* @needs_update is an optimization when running tokenizer_consume_if. If expected_token is wrong and tokenizer_consume_if is called again, @@ -68,7 +67,7 @@ CHECK_RESULT int tokenizer_accept(Tokenizer *self, Token expected_token); otherwise @result is set to 1 */ CHECK_RESULT int tokenizer_consume_if(Tokenizer *self, Token expected_token, bool *result); -void tokenizer_print_error(Tokenizer *self, const char *fmt, ...); +void tokenizer_print_error(Tokenizer *self, int index, const char *fmt, ...); void tokenizer_print_error_object(Tokenizer *self, TokenizerError *error); TokenizerError tokenizer_create_error(Tokenizer *self, int index, const char *fmt, ...); int tokenizer_get_error_index(Tokenizer *self); -- cgit v1.2.3