From 11dc4b81935e3dfee997c421d8d6fa166edd7a05 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 24 Feb 2019 02:10:58 +0100 Subject: Initial commit, Function declaration work somewhat --- include/alloc.h | 14 +++++++++++++ include/ast.h | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++ include/buffer.h | 22 ++++++++++++++++++++ include/buffer_view.h | 14 +++++++++++++ include/mem.h | 10 +++++++++ include/misc.h | 19 +++++++++++++++++ include/parser.h | 23 +++++++++++++++++++++ include/tokenizer.h | 46 +++++++++++++++++++++++++++++++++++++++++ include/types.h | 20 ++++++++++++++++++ 9 files changed, 225 insertions(+) create mode 100644 include/alloc.h create mode 100644 include/ast.h create mode 100644 include/buffer.h create mode 100644 include/buffer_view.h create mode 100644 include/mem.h create mode 100644 include/misc.h create mode 100644 include/parser.h create mode 100644 include/tokenizer.h create mode 100644 include/types.h (limited to 'include') diff --git a/include/alloc.h b/include/alloc.h new file mode 100644 index 0000000..35223b3 --- /dev/null +++ b/include/alloc.h @@ -0,0 +1,14 @@ +#ifndef AMALGAM_ALLOC_H +#define AMALGAM_ALLOC_H + +#include "types.h" +#include "misc.h" + +#define ALLOC_OK 0 +#define ALLOC_FAIL -1 + +WARN_UNUSED_RESULT int am_malloc(usize size, void **mem); +WARN_UNUSED_RESULT int am_realloc(void *mem, usize new_size, void **new_mem); +void am_free(void *mem); + +#endif diff --git a/include/ast.h b/include/ast.h new file mode 100644 index 0000000..edbe70f --- /dev/null +++ b/include/ast.h @@ -0,0 +1,57 @@ +#ifndef AMALGAM_AST_H +#define AMALGAM_AST_H + +#include "buffer_view.h" +#include "buffer.h" +#include "misc.h" + +typedef struct FunctionDecl FunctionDecl; +typedef struct FunctionCall FunctionCall; +typedef struct LhsExpr LhsExpr; + +typedef union { + FunctionDecl *func_decl; + FunctionCall *func_call; + LhsExpr *lhs_expr; +} AstValue; + +typedef enum { + AST_NONE, + AST_FUNCTION_DECL, + AST_FUNCTION_CALL, + AST_LHS +} AstType; + +typedef struct { + AstValue value; + AstType type; +} Ast; + +struct FunctionDecl { + BufferView name; + Buffer body; +}; + +struct FunctionCall { + BufferView name; +}; + +struct LhsExpr { + int isConst; + BufferView var_name; + Ast rhs_expr; +}; + +Ast ast_none(); +void ast_deinit(Ast *ast); + +void funcdecl_init(FunctionDecl *self); +void funcdecl_deinit(FunctionDecl *self); +WARN_UNUSED_RESULT int funcdecl_add_to_body(FunctionDecl *self, Ast ast); + +void funccall_init(FunctionCall *self, BufferView name); + +void lhsexpr_init(LhsExpr *self, int isConst, BufferView var_name); +void lhsexpr_deinit(LhsExpr *self); + +#endif diff --git a/include/buffer.h b/include/buffer.h new file mode 100644 index 0000000..57efe9e --- /dev/null +++ b/include/buffer.h @@ -0,0 +1,22 @@ +#ifndef AMALGAM_BUFFER_H +#define AMALGAM_BUFFER_H + +#include "types.h" +#include "misc.h" + +#define BUFFER_OK 0 +#define BUFFER_ALLOC_FAIL -1 + +typedef struct { + char* data; + usize size; + usize capacity; +} Buffer; + +void buffer_init(Buffer *self); +void buffer_deinit(Buffer *self); + +WARN_UNUSED_RESULT int buffer_append(Buffer *self, void *data, usize size); +void* buffer_get(Buffer *self, usize index, usize type_size); + +#endif \ No newline at end of file diff --git a/include/buffer_view.h b/include/buffer_view.h new file mode 100644 index 0000000..4993dc2 --- /dev/null +++ b/include/buffer_view.h @@ -0,0 +1,14 @@ +#ifndef AMALGAM_BUFFER_VIEW_H +#define AMALGAM_BUFFER_VIEW_H + +#include "types.h" + +typedef struct { + const char* data; + usize size; +} BufferView; + +BufferView create_buffer_view_null(); +BufferView create_buffer_view(const char *data, usize size); + +#endif diff --git a/include/mem.h b/include/mem.h new file mode 100644 index 0000000..bad6353 --- /dev/null +++ b/include/mem.h @@ -0,0 +1,10 @@ +#ifndef AMALGAM_MEM_H +#define AMALGAM_MEM_H + +#include "types.h" +#include "misc.h" + +void am_memcpy(void *dest, const void *src, usize size); +bool am_memeql(const void *lhs, const void *rhs, usize size); + +#endif \ No newline at end of file diff --git a/include/misc.h b/include/misc.h new file mode 100644 index 0000000..c83eeb2 --- /dev/null +++ b/include/misc.h @@ -0,0 +1,19 @@ +#ifndef AMALGAM_MISC_H +#define AMALGAM_MISC_H + +#define return_if_error(result) \ +do { \ + int return_if_result; \ + return_if_result = (result); \ + if((return_if_result) != 0) \ + return return_if_result; \ +} while(0) +#define cleanup_if_error(result) do { if((result) != 0) goto cleanup; } while(0) +#define WARN_UNUSED_RESULT __attribute__((warn_unused_result)) + +typedef enum { + bool_false, + bool_true +} bool; + +#endif \ No newline at end of file diff --git a/include/parser.h b/include/parser.h new file mode 100644 index 0000000..8207381 --- /dev/null +++ b/include/parser.h @@ -0,0 +1,23 @@ +#ifndef AMALGAM_PARSER_H +#define AMALGAM_PARSER_H + +#include "buffer.h" +#include "buffer_view.h" +#include "tokenizer.h" + +#define PARSER_OK 0 +/* General error */ +#define PARSER_ERR -1 +#define PARSER_UNEXPECTED_TOKEN -2 + +typedef struct { + Buffer ast_objects; + Tokenizer tokenizer; +} Parser; + +WARN_UNUSED_RESULT int parser_init(Parser *self); +void parser_deinit(Parser *self); + +WARN_UNUSED_RESULT int parser_parse_buffer(Parser *self, BufferView code_buffer); + +#endif diff --git a/include/tokenizer.h b/include/tokenizer.h new file mode 100644 index 0000000..7dd377f --- /dev/null +++ b/include/tokenizer.h @@ -0,0 +1,46 @@ +#ifndef AMALGAM_TOKENIZER_H +#define AMALGAM_TOKENIZER_H + +#include "buffer_view.h" +#include "misc.h" + +#define TOKENIZER_OK 0 +#define TOKENIZER_UNEXPECTED_TOKEN -1 + +typedef enum { + TOK_NONE, + TOK_END_OF_FILE, + TOK_IDENTIFIER, + TOK_CONST, + TOK_VAR, + TOK_EQUALS, + TOK_OPEN_PAREN, + TOK_CLOSING_PAREN, + TOK_OPEN_BRACE, + TOK_CLOSING_BRACE +} Token; + +typedef struct { + BufferView code; + int index; + int prev_index; + int line; + + union { + BufferView identifier; + } value; +} Tokenizer; + +WARN_UNUSED_RESULT int tokenizer_init(Tokenizer *self, BufferView code); +void tokenizer_deinit(Tokenizer *self); + +WARN_UNUSED_RESULT int tokenizer_next(Tokenizer *self, Token *token); +WARN_UNUSED_RESULT int tokenizer_accept(Tokenizer *self, Token expected_token); +/* + @result is set to 0 if the next token is equal to @expected_token, + otherwise @result is set to 1 +*/ +WARN_UNUSED_RESULT int tokenizer_consume_if(Tokenizer *self, Token expected_token, bool *result); +void tokenizer_print_error(Tokenizer *self, const char *fmt, ...); + +#endif diff --git a/include/types.h b/include/types.h new file mode 100644 index 0000000..68e2d0f --- /dev/null +++ b/include/types.h @@ -0,0 +1,20 @@ +#ifndef AMALGAM_TYPES_H +#define AMALGAM_TYPES_H + +#include +#include + +typedef int8_t i8; +typedef int16_t i16; +typedef int32_t i32; +typedef int64_t i64; + +typedef uint8_t u8; +typedef uint16_t u16; +typedef uint32_t u32; +typedef uint64_t u64; + +typedef ptrdiff_t isize; +typedef size_t usize; + +#endif -- cgit v1.2.3