Fix arrays in tuples error in C gen. Better api for getting unique name.
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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));
|
||||
|
||||
@@ -640,59 +640,3 @@ struct Core_Message {
|
||||
int trace_line;
|
||||
char *trace_file;
|
||||
};
|
||||
|
||||
/*
|
||||
Array<Token> core_lex_file(Allocator *allocator, char *filename);
|
||||
Array<Token> core_lex_string(Allocator *allocator, char *string, size_t size);
|
||||
|
||||
Core_Context *core_create_context(Allocator *allocator);
|
||||
Array<Token> 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
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
*/
|
||||
Reference in New Issue
Block a user