From 0f26e1d204d3a3026ca3edfc4c6bd9638b2632e7 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 31 Jul 2019 22:29:24 +0200 Subject: Add nullable, add bytecode documentation --- include/ast.h | 14 +++++++++++--- include/bytecode/bytecode.h | 2 +- include/nullable.h | 29 +++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 include/nullable.h (limited to 'include') diff --git a/include/ast.h b/include/ast.h index 4c54766..6a40980 100644 --- a/include/ast.h +++ b/include/ast.h @@ -2,6 +2,7 @@ #define AMALGAM_AST_H #include "defs.h" +#include "nullable.h" #include "std/defs.h" #include "std/buffer_view.h" #include "std/buffer.h" @@ -17,6 +18,7 @@ #define AST_ERR -1 #define AST_ERR_DEF_DUP -20 +typedef struct Ast Ast; typedef struct FunctionSignature FunctionSignature; typedef struct FunctionCall FunctionCall; typedef struct StructDecl StructDecl; @@ -79,12 +81,14 @@ typedef struct { Parser *parser; /* Borrowed. This is the parser that is currently parsing the expression */ } AstResolveData; -typedef struct { +struct Ast { AstValue value; AstType type; AstResolveData resolve_data; SsaRegister ssa_reg; -} Ast; +}; + +DefineNullablePtrType(Ast); struct Scope { Buffer/**/ ast_objects; @@ -156,7 +160,7 @@ struct LhsExpr { bool is_const; BufferView var_name; VariableType type; - Ast *rhs_expr; + NullablePtr(Ast) rhs_expr; }; struct AssignmentExpr { @@ -244,4 +248,8 @@ CHECK_RESULT int scope_add_child(Scope *self, Ast *child); /* longjump to compiler env on failure */ void scope_resolve(Scope *self, AstCompilerContext *context); + + +CHECK_RESULT bool resolved_type_is_func_decl(Ast *self); + #endif diff --git a/include/bytecode/bytecode.h b/include/bytecode/bytecode.h index 32488cc..4c54fbd 100644 --- a/include/bytecode/bytecode.h +++ b/include/bytecode/bytecode.h @@ -11,7 +11,7 @@ /*doc(Opcode) Variable length opcodes. Sizes range from 1 to 4 bytes. # Instruction formats - Instructions can be in 6 different formats: + Instructions can be in 7 different formats: 1. 1 byte: Opcode(u8) 2. 2 bytes: Opcode(u8) + register(u8) 3. 3 bytes: Opcode(u8) + register(u8) + register(u8) diff --git a/include/nullable.h b/include/nullable.h new file mode 100644 index 0000000..6c4d3a7 --- /dev/null +++ b/include/nullable.h @@ -0,0 +1,29 @@ +#ifndef AMAL_NULLABLE_H +#define AMAL_NULLABLE_H + +struct __nullable_type_dummy{ int _; }; + +int assert_not_null(void *val); + +#ifdef DEBUG +#define DefineNullablePtrType(type) \ + typedef struct type##_nullable type##_nullable; \ + struct type##_nullable { \ + type *value; \ + } +#define NullablePtr(type) type##_nullable +#define nullable_unwrap(nullable_type) \ + (assert_not_null((nullable_type).value) ? ((nullable_type).value) : NULL) +#define nullable_assign(nullable_type, new_value) ((nullable_type).value = (new_value)) +#define is_not_null(nullable_type) ((nullable_type).value != NULL) +#define nullable_raw(nullable_type) ((nullable_type).value) +#else +#define DefineNullablePtrType(type) +#define NullablePtr(type) type* +#define nullable_unwrap(value) value +#define nullable_assign(nullable_type, new_value) ((nullable_type) = (new_value)) +#define is_not_null(nullable_type) ((nullable_type) != NULL) +#define nullable_raw(nullable_type) (nullable_type) +#endif + +#endif -- cgit v1.2.3