diff options
author | dec05eba <dec05eba@protonmail.com> | 2019-03-09 00:43:13 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-07-25 14:36:46 +0200 |
commit | 9428fceb0cacf5ff9e19116de658bcffb98efc6f (patch) | |
tree | b281b6e047195c4f9c49de98919c8b99171dccec /include | |
parent | 18fb99add7f0d4b710debcbf5c154f9c1d841845 (diff) |
Add check for duplicate variable names
Diffstat (limited to 'include')
-rw-r--r-- | include/ast.h | 19 | ||||
-rw-r--r-- | include/tokenizer.h | 10 |
2 files changed, 21 insertions, 8 deletions
diff --git a/include/ast.h b/include/ast.h index 7911a59..1d40a89 100644 --- a/include/ast.h +++ b/include/ast.h @@ -5,8 +5,13 @@ #include "std/buffer.h" #include "std/misc.h" #include "std/scoped_allocator.h" +#include "std/hash_map.h" #include "binop_type.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; @@ -45,9 +50,14 @@ typedef struct { AstType type; } Ast; +struct Scope { + Buffer ast_objects; + HashMap/*(key=BufferView, value=Ast)*/ named_objects; +}; + struct FunctionDecl { BufferView name; - Buffer body; + Scope body; }; struct FunctionCall { @@ -90,11 +100,8 @@ struct Binop { bool grouped; }; -struct Scope { - Buffer ast_objects; -}; - Ast ast_none(); +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); @@ -103,7 +110,9 @@ 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 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); #endif diff --git a/include/tokenizer.h b/include/tokenizer.h index 21e93ee..b38cc58 100644 --- a/include/tokenizer.h +++ b/include/tokenizer.h @@ -3,6 +3,7 @@ #include "std/buffer_view.h" #include "std/misc.h" +#include "std/defs.h" #include "binop_type.h" #define TOKENIZER_OK 0 @@ -52,14 +53,15 @@ typedef struct { BinopType binop_type; } value; bool number_is_integer; + ScopedAllocator *allocator; /* borrowed */ } Tokenizer; typedef struct { int index; - const char* str; + char* str; } TokenizerError; -CHECK_RESULT int tokenizer_init(Tokenizer *self, BufferView code, BufferView code_name); +CHECK_RESULT int tokenizer_init(Tokenizer *self, ScopedAllocator *allocator, BufferView code, BufferView code_name); CHECK_RESULT int tokenizer_accept(Tokenizer *self, Token expected_token); /* @result is set to 0 if the next token is equal to @expected_token, @@ -68,6 +70,8 @@ CHECK_RESULT int tokenizer_accept(Tokenizer *self, Token expected_token); 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_object(Tokenizer *self, TokenizerError *error); -TokenizerError tokenizer_create_error(Tokenizer *tokenizer, const char *err_str); +TokenizerError tokenizer_create_error(Tokenizer *self, int index, const char *fmt, ...); +int tokenizer_get_error_index(Tokenizer *self); +int tokenizer_get_code_reference_index(Tokenizer *self, const char *ref); #endif |