From 2323ca6c9ec3c8ee76b9acf13745b80b92952a6a Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 18 Mar 2019 23:47:45 +0100 Subject: Add struct, import caching, binop ops etc --- include/ast.h | 31 ++++++++++++++++++++++++++++--- include/compiler.h | 13 +++++++++---- include/defs.h | 1 + include/std/buffer.h | 2 +- include/std/file.h | 4 ++++ include/std/log.h | 1 - include/std/misc.h | 16 ++++++++++++++-- include/std/scoped_allocator.h | 2 +- include/std/thread.h | 3 +++ include/tokenizer.h | 1 + 10 files changed, 62 insertions(+), 12 deletions(-) (limited to 'include') diff --git a/include/ast.h b/include/ast.h index 6077d5d..e49ad08 100644 --- a/include/ast.h +++ b/include/ast.h @@ -18,6 +18,8 @@ typedef struct FunctionDecl FunctionDecl; typedef struct FunctionCall FunctionCall; +typedef struct StructDecl StructDecl; +typedef struct StructField StructField; typedef struct LhsExpr LhsExpr; typedef struct Import Import; typedef struct String String; @@ -29,6 +31,8 @@ typedef union { void *data; FunctionDecl *func_decl; FunctionCall *func_call; + StructDecl *struct_decl; + StructField *struct_field; LhsExpr *lhs_expr; Import *import; String *string; @@ -41,6 +45,8 @@ typedef enum { AST_NONE, AST_FUNCTION_DECL, AST_FUNCTION_CALL, + AST_STRUCT_DECL, + AST_STRUCT_FIELD, AST_LHS, AST_IMPORT, AST_STRING, @@ -59,6 +65,7 @@ typedef struct { AstValue value; AstType type; AstResolveStatus resolve_status; + StructDecl *resolved_type; } Ast; struct Scope { @@ -67,6 +74,11 @@ struct Scope { Scope *parent; }; +struct FileScopeReference { + Scope *scope; + Buffer canonical_path; +}; + struct Variable { BufferView name; Ast resolved_variable; @@ -82,15 +94,25 @@ struct FunctionCall { Buffer/*Ast*/ args; }; +struct StructDecl { + Scope body; +}; + +struct StructField { + BufferView name; + Variable type; +}; + struct LhsExpr { - int is_const; - BufferView type_name; + bool is_const; BufferView var_name; + Variable type; Ast rhs_expr; }; struct Import { BufferView path; + FileScopeReference *file_scope; }; struct String { @@ -125,7 +147,9 @@ BufferView ast_get_name(Ast *self); CHECK_RESULT int funcdecl_init(FunctionDecl *self, Scope *parent, ScopedAllocator *allocator); CHECK_RESULT int funccall_init(FunctionCall *self, BufferView name, ScopedAllocator *allocator); -void lhsexpr_init(LhsExpr *self, int isConst, BufferView var_name); +CHECK_RESULT int structdecl_init(StructDecl *self, Scope *parent, ScopedAllocator *allocator); +void structfield_init(StructField *self, BufferView name, BufferView type_name); +void lhsexpr_init(LhsExpr *self, bool is_const, 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); @@ -133,6 +157,7 @@ void variable_init(Variable *self, BufferView name); void binop_init(Binop *self); 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); CHECK_RESULT int scope_add_child(Scope *self, Ast *child); /* longjump to compiler env on failure */ void scope_resolve(Scope *self, AstCompilerContext *context); diff --git a/include/compiler.h b/include/compiler.h index 32a5eab..47d4471 100644 --- a/include/compiler.h +++ b/include/compiler.h @@ -7,6 +7,7 @@ #include "std/scoped_allocator.h" #include "std/thread.h" #include "defs.h" +#include "ast.h" #define AMAL_COMPILER_OK 0 /* General error */ @@ -14,9 +15,10 @@ struct amal_compiler { ScopedAllocator allocator; - ScopedAllocator main_thread_allocator; + Scope root_scope; Buffer/**/ parsers; - Buffer queued_files; + Buffer/**/ queued_files; + HashMap/**/ file_scopes; ParserThreadData *threads; int usable_thread_count; bool started; @@ -26,8 +28,11 @@ struct amal_compiler { CHECK_RESULT int amal_compiler_init(amal_compiler *self); CHECK_RESULT int amal_compiler_deinit(amal_compiler *self); -/* Not thread-safe */ -CHECK_RESULT int amal_compiler_load_file(amal_compiler *self, BufferView filepath); +/* +This function creates a copy of @filepath in the same thread it's called from +so it doesn't have to survive longer than this function call. +*/ +CHECK_RESULT int amal_compiler_load_file(amal_compiler *self, const char *filepath, FileScopeReference **file_scope); /* TODO: amal_compiler_unload_file */ #endif diff --git a/include/defs.h b/include/defs.h index 74f2411..76e5e0d 100644 --- a/include/defs.h +++ b/include/defs.h @@ -5,5 +5,6 @@ typedef struct ParserThreadData ParserThreadData; typedef struct amal_compiler amal_compiler; typedef struct Parser Parser; typedef struct Scope Scope; +typedef struct FileScopeReference FileScopeReference; #endif diff --git a/include/std/buffer.h b/include/std/buffer.h index c961b6e..a1bfb01 100644 --- a/include/std/buffer.h +++ b/include/std/buffer.h @@ -21,7 +21,7 @@ CHECK_RESULT int buffer_append(Buffer *self, const void *data, usize size); void* buffer_get(Buffer *self, usize index, usize type_size); CHECK_RESULT int buffer_pop(Buffer *self, void *data, usize size); void* buffer_start(Buffer *self); -void *buffer_end(Buffer *self); +void* buffer_end(Buffer *self); usize __buffer_get_size(Buffer *self, usize type_size); #define buffer_get_size(self, type) __buffer_get_size((self), sizeof(type)) diff --git a/include/std/file.h b/include/std/file.h index 120e7bb..c6753fd 100644 --- a/include/std/file.h +++ b/include/std/file.h @@ -4,6 +4,7 @@ #include "misc.h" #include "types.h" #include "buffer_view.h" +#include "buffer.h" /* Return bool_true if you want to continue scanning, otherwise return bool_false */ typedef bool (*scan_dir_callback_func)(const char *filepath, int filepath_length, void *userdata); @@ -31,7 +32,10 @@ CHECK_RESULT int mapped_file_init(MappedFile *self, const char *filepath, Mapped CHECK_RESULT int mapped_file_deinit(MappedFile *self); #endif +/* @data will be allocated with am_malloc, should be deallocated with am_free */ CHECK_RESULT int read_whole_file(const char *filepath, char **data, usize *size); +/* @result_path will be allocated with am_malloc, should be deallocated with am_free */ +CHECK_RESULT int file_get_canonical_path(const char *filepath, char **result_path, usize *result_path_size); BufferView file_get_parent_directory(BufferView filepath); BufferView file_get_name(BufferView filepath); diff --git a/include/std/log.h b/include/std/log.h index d13c5bf..cd376c6 100644 --- a/include/std/log.h +++ b/include/std/log.h @@ -8,6 +8,5 @@ void amal_log_debug(const char *fmt, ...); void amal_log_error(const char *fmt, ...); void amal_log_info(const char *fmt, ...); void amal_log_warning(const char *fmt, ...); -void amal_log_perror(const char *prefix); #endif diff --git a/include/std/misc.h b/include/std/misc.h index b979d9c..e28ed48 100644 --- a/include/std/misc.h +++ b/include/std/misc.h @@ -1,15 +1,27 @@ #ifndef AMALGAM_MISC_H #define AMALGAM_MISC_H +#include + +#ifdef AMAL_PEDANTIC + #define return_if_debug_msg do {} while(0) + #define cleanup_if_debug_msg do {} while(0) +#else + #define return_if_debug_msg do { fprintf(stderr, "Return from %s:%d\n", __FUNCTION__, __LINE__); } while(0) + #define cleanup_if_debug_msg do { fprintf(stderr, "cleanup from %s:%d\n", __FUNCTION__, __LINE__); } while(0) +#endif + #define return_if_error(result) \ do { \ int return_if_result; \ return_if_result = (result); \ - if((return_if_result) != 0) \ + if((return_if_result) != 0) { \ + return_if_debug_msg; \ return return_if_result; \ + } \ } while(0) -#define cleanup_if_error(result) do { if((result) != 0) goto cleanup; } while(0) +#define cleanup_if_error(result) do { if((result) != 0) { cleanup_if_debug_msg; goto cleanup; } } while(0) #if defined(__GNUC__) && __GNUC__ >= 4 #define CHECK_RESULT __attribute__ ((warn_unused_result)) diff --git a/include/std/scoped_allocator.h b/include/std/scoped_allocator.h index 0de4f04..9090767 100644 --- a/include/std/scoped_allocator.h +++ b/include/std/scoped_allocator.h @@ -15,7 +15,7 @@ struct ScopedAllocatorNode { struct ScopedAllocator { ScopedAllocatorNode head; ScopedAllocatorNode *current; - Buffer buffers; + Buffer/**/ buffers; }; CHECK_RESULT int scoped_allocator_node_init(ScopedAllocatorNode *self); diff --git a/include/std/thread.h b/include/std/thread.h index 915d6a9..6eedb08 100644 --- a/include/std/thread.h +++ b/include/std/thread.h @@ -29,6 +29,8 @@ typedef enum { struct amal_mutex { pthread_mutex_t mutex; const char *lock_identifier; + bool locked; + pthread_t owner_thread; }; CHECK_RESULT int amal_thread_create(amal_thread *self, amal_thread_type thread_type, const char *name, AmalThreadCallbackFunc callback_func, void *userdata); @@ -40,6 +42,7 @@ CHECK_RESULT int amal_thread_join(amal_thread *self, void **result); void amal_mutex_init(amal_mutex *self); void amal_mutex_deinit(amal_mutex *self); CHECK_RESULT int amal_mutex_lock(amal_mutex *self, const char *lock_identifier); +/* Safe to call unlock when another thread owns the lock or if the lock is not locked */ CHECK_RESULT int amal_mutex_unlock(amal_mutex *self); void amal_mutex_tryunlock(amal_mutex *self); diff --git a/include/tokenizer.h b/include/tokenizer.h index a06b689..c4c3725 100644 --- a/include/tokenizer.h +++ b/include/tokenizer.h @@ -19,6 +19,7 @@ typedef enum { TOK_VAR, TOK_STRING, TOK_FN, + TOK_STRUCT, TOK_EQUALS, TOK_OPEN_PAREN, TOK_CLOSING_PAREN, -- cgit v1.2.3