diff --git a/core_codegen_c_language.cpp b/core_codegen_c_language.cpp index 3020026..76f7805 100644 --- a/core_codegen_c_language.cpp +++ b/core_codegen_c_language.cpp @@ -50,12 +50,14 @@ gen_scope_name(Ast_Scope *scope){ gen("%.*s", (int)string.len, string.str); } -CORE_Static String -unique_name_scratch(Allocator *scratch, Ast *ast){ - String result = string_scope_name(scratch, ast->parent_scope); +CORE_Static Intern_String +get_unique_name(Ast *ast){ + Scratch_Scope _scope(pctx->scratch); + String result = string_scope_name(pctx->scratch, ast->parent_scope); assert(result.len); - result = string_fmt(scratch, "%Q%d", result, ast->pos->line); - return result; + result = string_fmt(pctx->scratch, "%Q%d", result, ast->pos->line); + Intern_String result2 = pctx->intern(result); + return result2; } global String prefixed_string_type; @@ -519,6 +521,11 @@ gen_expr(Ast_Expr *ast){ return true; } +/* +- [ ] Assigning arrays to arrays doesn't work in C. Being able to do that seems like a good idea though + +*/ + CORE_Static void gen_ast(Ast *ast){ switch(ast->kind){ @@ -540,6 +547,31 @@ gen_ast(Ast *ast){ Arena *scratch = pctx->scratch; Scratch_Scope _scope(scratch); + Intern_String tuple_name = get_unique_name(node); + gen_simple_decl(node->resolved_type, tuple_name); + gen(";"); + + int i = 0; + For(node->expr){ + + // We cant assign to array in C so we need a special case + if (is_array(it->resolved_type)) { + genln("%QMemoryCopy(&%Q.m%d, ", pctx->symbol_prefix, tuple_name, i); + gen_expr(it); + gen(", sizeof(%Q.m%d));", tuple_name, i); + } + + else { + genln("%Q.m%d = ", tuple_name, i); + gen_expr(it); + gen(";"); + } + + i += 1; + } + gen_line(node); + genln("return %Q;", tuple_name); +#if 0 gen("return ("); gen_simple_decl(node->resolved_type); gen("){"); @@ -556,6 +588,7 @@ gen_ast(Ast *ast){ global_indent--; genln(""); gen("};"); +#endif return; } @@ -752,7 +785,7 @@ gen_ast(Ast *ast){ Arena *scratch = pctx->scratch; Scratch_Scope _scope(scratch); - Intern_String var_name = pctx->intern(unique_name_scratch(scratch, node)); + Intern_String var_name = get_unique_name(node); gen_simple_decl(node->resolved_type, var_name); gen(" = "); gen_expr(node->expr); diff --git a/core_compiler.cpp b/core_compiler.cpp index 308c10b..66cb38a 100644 --- a/core_compiler.cpp +++ b/core_compiler.cpp @@ -273,9 +273,6 @@ resolve_everything_in_module(Ast_Module *module) { resolve_name(file, decl->pos, decl->name); if (decl->kind == AST_STRUCT) { - // if (decl->name == pctx->intern("CreateFileW"_s)) { - // __debugbreak(); - // } type_complete(decl->type_val); } } @@ -377,11 +374,6 @@ GetTypeInfo :: (type: Type): *Type_Info insert_builtin_type_into_scope(module, "Type"_s, pctx->type_type); } - /* - Ast_Decl *namespace = core_create_namespace("name"); - Ast_Decl *core_create_constant("name", - */ - { Ast_Scope *scope = ast_decl_scope(&pctx->null_token, pctx->perm, get(&module->all_loaded_files, 0)); Ast_Decl * decl = ast_namespace(&pctx->null_token, scope, pctx->intern("Const"_s)); diff --git a/core_compiler_interface.hpp b/core_compiler_interface.hpp index 271469a..2fe7c60 100644 --- a/core_compiler_interface.hpp +++ b/core_compiler_interface.hpp @@ -162,7 +162,7 @@ struct Token{ union{ String string; struct{ - uint8_t *str; + uint8_t *str; int64_t len; }; }; @@ -232,11 +232,11 @@ struct Value { Ast_Type *type; Ast_Decl *resolved_decl; union { - bool bool_val; - double f64_val; - Intern_String intern_val; + bool bool_val; + double f64_val; + Intern_String intern_val; BigInt big_int_val; - Ast_Type *type_val; + Ast_Type *type_val; }; /*END*/ }; @@ -254,11 +254,11 @@ struct Ast_Resolved_Member{ Ast_Type *type; Ast_Decl *resolved_decl; union { - bool bool_val; - double f64_val; - Intern_String intern_val; + bool bool_val; + double f64_val; + Intern_String intern_val; BigInt big_int_val; - Ast_Type *type_val; + Ast_Type *type_val; }; }; }; @@ -398,11 +398,11 @@ struct Ast_Atom: Ast_Expr{ Ast_Type *type; Ast_Decl *resolved_decl; union { - bool bool_val; - double f64_val; - Intern_String intern_val; + bool bool_val; + double f64_val; + Intern_String intern_val; BigInt big_int_val; - Ast_Type *type_val; + Ast_Type *type_val; }; }; }; @@ -614,11 +614,11 @@ meta.inline_value_fields() Ast_Type *type; Ast_Decl *resolved_decl; union { - bool bool_val; - double f64_val; - Intern_String intern_val; + bool bool_val; + double f64_val; + Intern_String intern_val; BigInt big_int_val; - Ast_Type *type_val; + Ast_Type *type_val; }; }; }; @@ -636,63 +636,7 @@ struct Core_Message { Core_Message_Kind kind; String string; Token *tokens[2]; - + int trace_line; char *trace_file; }; - -/* - Array core_lex_file(Allocator *allocator, char *filename); - Array core_lex_string(Allocator *allocator, char *string, size_t size); - - Core_Context *core_create_context(Allocator *allocator); - Array core_lex_string(Core_Context *ctx, char *string, size_t size); - - AST Modification API - AST Query API - - Tree walk interpreter / Bytecode interpreter - C code generation backend - - Control of compilation with many hooks - Ast tags - - * Accessing and modifying the AST - * Using as a scripting language - * Tree walk interp or Bytecode - * Using as a compiler - * C code backend - * C++ code backend ? - * Hooking into raw strings to preprocess - * Hooking into tokens to preprocess - * Hooking into AST to manipulate - * Hooking into Parsing to add syntax sugar - * Hooking into Parsing and typechecking to add language features - * Hooking into typechecking to add for example builtin functions - * Creating a custom code generation backend - * Performing compilation passes multiple times to get some sort of effect - * Accessing the typechecked AST and modifying it then typechecking modifications ?? - * Tagging syntax for creating language features - - Possible use cases: - I want to use it as a scripting language for my game - It should be able to not leak memory while rerunning scripts contnously - It should allow me to constrain certain features (pointer arithmetic) - It should allow me to add possible functions - I want to use this as a code generation tool - I should be able to access and modify the AST - I should be able to access the tokens for pre processing - I should be able to add my own syntax - I should be albe to add my own features - I should be able to create or modify a code generating back end - - - - Code generation - Making DSLs - - - - - - */ \ No newline at end of file