Getting rid of globals
This commit is contained in:
@@ -16,7 +16,7 @@ global Intern_String last_filename;
|
|||||||
global int last_line;
|
global int last_line;
|
||||||
CORE_Static void
|
CORE_Static void
|
||||||
gen_line(Ast *node){
|
gen_line(Ast *node){
|
||||||
if(emit_line_directives){
|
if(pctx->emit_line_directives){
|
||||||
last_line = node->pos->line+1;
|
last_line = node->pos->line+1;
|
||||||
genln("#line %d", last_line);
|
genln("#line %d", last_line);
|
||||||
if(node->pos->file != last_filename){
|
if(node->pos->file != last_filename){
|
||||||
@@ -28,7 +28,7 @@ gen_line(Ast *node){
|
|||||||
|
|
||||||
CORE_Static void
|
CORE_Static void
|
||||||
gen_last_line(){
|
gen_last_line(){
|
||||||
if(emit_line_directives){
|
if(pctx->emit_line_directives){
|
||||||
genln("#line %d", last_line);
|
genln("#line %d", last_line);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -223,7 +223,7 @@ gen_value(Token *pos, Value a){
|
|||||||
}break;
|
}break;
|
||||||
case TYPE_STRING:{
|
case TYPE_STRING:{
|
||||||
int length = 0;
|
int length = 0;
|
||||||
gen("(%QString){(uint8_t *)\"", symbol_prefix);
|
gen("(%QString){(uint8_t *)\"", pctx->symbol_prefix);
|
||||||
for(int i = 0; i < a.intern_val.len; i++){
|
for(int i = 0; i < a.intern_val.len; i++){
|
||||||
if(a.intern_val.str[i] == '\n'){length += 2; gen("\\n");}
|
if(a.intern_val.str[i] == '\n'){length += 2; gen("\\n");}
|
||||||
else if(a.intern_val.str[i] == '\r'){length += 2; gen("\\r");}
|
else if(a.intern_val.str[i] == '\r'){length += 2; gen("\\r");}
|
||||||
@@ -518,8 +518,8 @@ gen_ast(Ast *ast){
|
|||||||
switch(ast->kind){
|
switch(ast->kind){
|
||||||
|
|
||||||
CASE(RUNTIME_ASSERT, Builtin){
|
CASE(RUNTIME_ASSERT, Builtin){
|
||||||
if(node->assert_message.len == 0) gen("%QAssert", symbol_prefix);
|
if(node->assert_message.len == 0) gen("%QAssert", pctx->symbol_prefix);
|
||||||
else gen("%QAssertMessage", symbol_prefix);
|
else gen("%QAssertMessage", pctx->symbol_prefix);
|
||||||
gen("(");
|
gen("(");
|
||||||
gen_expr(node->expr);
|
gen_expr(node->expr);
|
||||||
if(node->assert_message.len){
|
if(node->assert_message.len){
|
||||||
@@ -540,7 +540,7 @@ gen_ast(Ast *ast){
|
|||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
For(node->expr){
|
For(node->expr){
|
||||||
genln("%QMemoryCopy(&%Q.m%d, ", symbol_prefix, var_name, i);
|
genln("%QMemoryCopy(&%Q.m%d, ", pctx->symbol_prefix, var_name, i);
|
||||||
if(!is_array(it->resolved_type)) gen("&");
|
if(!is_array(it->resolved_type)) gen("&");
|
||||||
gen("(");
|
gen("(");
|
||||||
gen_expr(it);
|
gen_expr(it);
|
||||||
@@ -619,7 +619,7 @@ gen_ast(Ast *ast){
|
|||||||
if(node->is_array_traversal){
|
if(node->is_array_traversal){
|
||||||
gen("for(int64_t _i%d = 0; _i%d < ", node->pos->line, node->pos->line);
|
gen("for(int64_t _i%d = 0; _i%d < ", node->pos->line, node->pos->line);
|
||||||
if(is_array(node->cond->resolved_type)){
|
if(is_array(node->cond->resolved_type)){
|
||||||
gen("%QBufferSize(", symbol_prefix);
|
gen("%QBufferSize(", pctx->symbol_prefix);
|
||||||
gen_expr(node->cond);
|
gen_expr(node->cond);
|
||||||
gen(")");
|
gen(")");
|
||||||
} else{
|
} else{
|
||||||
@@ -753,7 +753,7 @@ gen_ast(Ast *ast){
|
|||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
For(node->vars){
|
For(node->vars){
|
||||||
gen("%QMemoryCopy((void *)&%Q, (void *)&%Q.m%d, sizeof(%Q));", symbol_prefix, it->name, var_name, i++, it->name);
|
gen("%QMemoryCopy((void *)&%Q, (void *)&%Q.m%d, sizeof(%Q));", pctx->symbol_prefix, it->name, var_name, i++, it->name);
|
||||||
}
|
}
|
||||||
BREAK();
|
BREAK();
|
||||||
}
|
}
|
||||||
@@ -773,8 +773,8 @@ CORE_Static String
|
|||||||
compile_to_c_code(){
|
compile_to_c_code(){
|
||||||
pctx->generating_time_begin = os_time();
|
pctx->generating_time_begin = os_time();
|
||||||
|
|
||||||
prefixed_string_type = string_fmt(pctx->perm, "%QString", symbol_prefix);
|
prefixed_string_type = string_fmt(pctx->perm, "%QString", pctx->symbol_prefix);
|
||||||
if(single_header_library_mode){
|
if(pctx->single_header_library_mode){
|
||||||
gen(R"(
|
gen(R"(
|
||||||
/*
|
/*
|
||||||
Do this:
|
Do this:
|
||||||
@@ -792,10 +792,10 @@ compile_to_c_code(){
|
|||||||
You can #define %QAssertMessage(x) to get more comprehensive error info
|
You can #define %QAssertMessage(x) to get more comprehensive error info
|
||||||
You can #define %QMemoryCopy(x) to avoid using default memory copy
|
You can #define %QMemoryCopy(x) to avoid using default memory copy
|
||||||
*/
|
*/
|
||||||
)", single_header_library_name, single_header_library_name, single_header_library_name,
|
)", pctx->single_header_library_name, pctx->single_header_library_name, pctx->single_header_library_name,
|
||||||
symbol_prefix, symbol_prefix, symbol_prefix);
|
pctx->symbol_prefix, pctx->symbol_prefix, pctx->symbol_prefix);
|
||||||
genln("#ifndef %Q_LIBRARY_HEADER ", single_header_library_name);
|
genln("#ifndef %Q_LIBRARY_HEADER ", pctx->single_header_library_name);
|
||||||
genln("#define %Q_LIBRARY_HEADER ", single_header_library_name);
|
genln("#define %Q_LIBRARY_HEADER ", pctx->single_header_library_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
gen(R"(
|
gen(R"(
|
||||||
@@ -906,7 +906,7 @@ typedef struct String{
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if(emit_type_info){
|
if(pctx->emit_type_info){
|
||||||
// Generate language.core
|
// Generate language.core
|
||||||
for(S32 i = 0; i < pctx->base_language_ordered_decl_len; i++){
|
for(S32 i = 0; i < pctx->base_language_ordered_decl_len; i++){
|
||||||
Ast_Decl *it = get(&pctx->ordered_decls, i);
|
Ast_Decl *it = get(&pctx->ordered_decls, i);
|
||||||
@@ -959,9 +959,9 @@ typedef struct String{
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(single_header_library_mode){
|
if(pctx->single_header_library_mode){
|
||||||
genln("#endif");
|
genln("#endif");
|
||||||
genln("#ifdef %Q_IMPLEMENTATION ", single_header_library_name);
|
genln("#ifdef %Q_IMPLEMENTATION ", pctx->single_header_library_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate actual code
|
// Generate actual code
|
||||||
@@ -973,7 +973,7 @@ typedef struct String{
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if(single_header_library_mode){
|
if(pctx->single_header_library_mode){
|
||||||
genln("#endif");
|
genln("#endif");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,11 @@
|
|||||||
|
|
||||||
CORE_Static void
|
CORE_Static void
|
||||||
core_init_compiler(Core_Ctx *ctx, Allocator *allocator) {
|
core_init_compiler(Core_Ctx *ctx, Allocator *allocator) {
|
||||||
|
ctx->emit_type_info = true;
|
||||||
|
ctx->emit_line_directives = true;
|
||||||
|
ctx->color_codes_enabled = true;
|
||||||
|
ctx->same_scope_token = { SAME_SCOPE };
|
||||||
|
|
||||||
ctx->init_ctx_time_begin = os_time();
|
ctx->init_ctx_time_begin = os_time();
|
||||||
pctx = ctx;
|
pctx = ctx;
|
||||||
ctx->perm_push_only = make_push_arena(allocator);
|
ctx->perm_push_only = make_push_arena(allocator);
|
||||||
@@ -240,21 +245,21 @@ compile_file_to_string(Allocator *allocator, String filename) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
Ast_Scope *scope = ast_decl_scope(&null_token, pctx->perm, get(&module->all_loaded_files, 0));
|
Ast_Scope *scope = ast_decl_scope(&pctx->null_token, pctx->perm, get(&module->all_loaded_files, 0));
|
||||||
Ast_Decl * decl = ast_namespace(&null_token, scope, pctx->intern("Const"_s));
|
Ast_Decl * decl = ast_namespace(&pctx->null_token, scope, pctx->intern("Const"_s));
|
||||||
decl->state = DECL_RESOLVED;
|
decl->state = DECL_RESOLVED;
|
||||||
|
|
||||||
Value v1 = {};
|
Value v1 = {};
|
||||||
v1.type = untyped_string;
|
v1.type = untyped_string;
|
||||||
v1.intern_val = pctx->intern(OS_NAME);
|
v1.intern_val = pctx->intern(OS_NAME);
|
||||||
Ast_Decl *const_os1 = ast_const(&null_token, pctx->intern("OSName"_s), v1);
|
Ast_Decl *const_os1 = ast_const(&pctx->null_token, pctx->intern("OSName"_s), v1);
|
||||||
const_os1->state = DECL_RESOLVED;
|
const_os1->state = DECL_RESOLVED;
|
||||||
insert_into_scope(scope, const_os1);
|
insert_into_scope(scope, const_os1);
|
||||||
|
|
||||||
Value v2 = {};
|
Value v2 = {};
|
||||||
v1.type = untyped_string;
|
v1.type = untyped_string;
|
||||||
v1.intern_val = pctx->intern(OS_NAME_LOWER);
|
v1.intern_val = pctx->intern(OS_NAME_LOWER);
|
||||||
Ast_Decl *const_os2 = ast_const(&null_token, pctx->intern("OSNameLower"_s), v2);
|
Ast_Decl *const_os2 = ast_const(&pctx->null_token, pctx->intern("OSNameLower"_s), v2);
|
||||||
const_os2->state = DECL_RESOLVED;
|
const_os2->state = DECL_RESOLVED;
|
||||||
insert_into_scope(scope, const_os2);
|
insert_into_scope(scope, const_os2);
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
|
@! Separate out the codegen stage cause that can change
|
||||||
@! Change type of Stage allocator
|
@! Change type of Stage allocator
|
||||||
@! Look into stage allocator and perhaps
|
@! Look into stage allocator and perhaps
|
||||||
use it more often to reduce scenarios
|
use it more often to reduce scenarios
|
||||||
@@ -68,8 +69,6 @@ struct Core_Ctx{
|
|||||||
String working_folder;
|
String working_folder;
|
||||||
List<Token *> files_to_link;
|
List<Token *> files_to_link;
|
||||||
|
|
||||||
S64 indent;
|
|
||||||
String_Builder gen;
|
|
||||||
String_Builder helper_builder;
|
String_Builder helper_builder;
|
||||||
|
|
||||||
F64 generating_time_begin;
|
F64 generating_time_begin;
|
||||||
@@ -82,6 +81,22 @@ struct Core_Ctx{
|
|||||||
F64 parsing_time_begin;
|
F64 parsing_time_begin;
|
||||||
F64 parsing_time_end;
|
F64 parsing_time_end;
|
||||||
|
|
||||||
|
bool color_codes_enabled;
|
||||||
|
|
||||||
|
// Codegen stage mostly
|
||||||
|
S64 indent;
|
||||||
|
String_Builder gen;
|
||||||
|
|
||||||
|
// Codegen stage configurables
|
||||||
|
bool emit_line_directives;
|
||||||
|
bool emit_type_info;
|
||||||
|
String symbol_prefix;
|
||||||
|
bool single_header_library_mode;
|
||||||
|
String single_header_library_name;
|
||||||
|
|
||||||
|
Token same_scope_token;
|
||||||
|
Token null_token;
|
||||||
|
|
||||||
Intern_String intern(String string){
|
Intern_String intern(String string){
|
||||||
assert(string.len > 0);
|
assert(string.len > 0);
|
||||||
return intern_string(&interns, string);
|
return intern_string(&interns, string);
|
||||||
|
|||||||
@@ -1,18 +1,8 @@
|
|||||||
global B32 emit_line_directives = true;
|
|
||||||
global B32 emit_type_info = true;
|
|
||||||
global String symbol_prefix = ""_s;
|
|
||||||
global B32 single_header_library_mode = false;
|
|
||||||
global String single_header_library_name = ""_s;
|
|
||||||
global bool color_codes_enabled;
|
|
||||||
|
|
||||||
thread_local Core_Ctx *pctx;
|
thread_local Core_Ctx *pctx;
|
||||||
|
|
||||||
Allocator *bigint_allocator;
|
Allocator *bigint_allocator;
|
||||||
global S64 bigint_allocation_count;
|
global S64 bigint_allocation_count;
|
||||||
|
|
||||||
global Token token_null = {SAME_SCOPE};
|
|
||||||
global Token null_token; // @todo: memes, why the above is called null?
|
|
||||||
|
|
||||||
/*#import meta
|
/*#import meta
|
||||||
for i in meta.keywords: print(f'Intern_String keyword_{i.lower()};')
|
for i in meta.keywords: print(f'Intern_String keyword_{i.lower()};')
|
||||||
for i in meta.interns: print(f'Intern_String intern_{i.lower()};')
|
for i in meta.interns: print(f'Intern_String intern_{i.lower()};')
|
||||||
|
|||||||
@@ -192,10 +192,10 @@ token_make(Core_Ctx *lexer){
|
|||||||
|
|
||||||
CORE_Static Token *
|
CORE_Static Token *
|
||||||
lex_last_indent_token(Lex_Stream *s){
|
lex_last_indent_token(Lex_Stream *s){
|
||||||
if(s->indent_stack.len > 0){
|
if (s->indent_stack.len > 0) {
|
||||||
return *s->indent_stack.last();
|
return *s->indent_stack.last();
|
||||||
}
|
}
|
||||||
return &token_null;
|
return &pctx->same_scope_token;
|
||||||
}
|
}
|
||||||
|
|
||||||
CORE_Static B32
|
CORE_Static B32
|
||||||
@@ -594,7 +594,7 @@ lex_restream(Core_Ctx *lexer, String istream, String file){
|
|||||||
|
|
||||||
Scratch_Scope _scope(lexer->scratch);
|
Scratch_Scope _scope(lexer->scratch);
|
||||||
lexer->stream.indent_stack.allocator = lexer->scratch;
|
lexer->stream.indent_stack.allocator = lexer->scratch;
|
||||||
lexer->stream.indent_stack.add(&token_null);
|
lexer->stream.indent_stack.add(&lexer->same_scope_token);
|
||||||
lex__stream(lexer);
|
lex__stream(lexer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -307,8 +307,9 @@ int main(int argument_count, char **arguments){
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool enable_color_codes = false;
|
||||||
#if OS_UNIX
|
#if OS_UNIX
|
||||||
color_codes_enabled = true;
|
enable_color_codes = true;
|
||||||
#endif
|
#endif
|
||||||
#if OS_WINDOWS
|
#if OS_WINDOWS
|
||||||
// Set output mode to handle virtual terminal sequences
|
// Set output mode to handle virtual terminal sequences
|
||||||
@@ -318,28 +319,27 @@ int main(int argument_count, char **arguments){
|
|||||||
if (GetConsoleMode(hOut, &dwMode)) {
|
if (GetConsoleMode(hOut, &dwMode)) {
|
||||||
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
|
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
|
||||||
if (SetConsoleMode(hOut, dwMode)) {
|
if (SetConsoleMode(hOut, dwMode)) {
|
||||||
color_codes_enabled = true;
|
enable_color_codes = true;
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
log_info("Failed to enable colored terminal output C");
|
printf("Failed to enable colored terminal output C\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
log_info("Failed to enable colored terminal output B");
|
printf("Failed to enable colored terminal output B\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
log_info("Failed to enable colored terminal output A");
|
printf("Failed to enable colored terminal output A\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
test_os_memory();
|
test_os_memory();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// emit_line_directives = false;
|
(void)enable_color_codes;
|
||||||
// emit_type_info = false;
|
|
||||||
For(args){
|
For(args){
|
||||||
|
|
||||||
if(it == "-testing"_s){ // @copy_paste
|
if(it == "-testing"_s){
|
||||||
Scratch_Scope _scope(scratch_arena);
|
Scratch_Scope _scope(scratch_arena);
|
||||||
Array<OS_File_Info> examples = os_list_dir(scratch_arena, scratch_arena, "examples"_s);
|
Array<OS_File_Info> examples = os_list_dir(scratch_arena, scratch_arena, "examples"_s);
|
||||||
Array<OS_File_Info> tests = os_list_dir(scratch_arena, scratch_arena, "tests"_s);
|
Array<OS_File_Info> tests = os_list_dir(scratch_arena, scratch_arena, "tests"_s);
|
||||||
@@ -359,6 +359,6 @@ int main(int argument_count, char **arguments){
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
log_info("End of program");
|
printf("End of program\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ print_token_line(Token *token){
|
|||||||
log_info_no_nl("%.*s", i1, token->line_begin);
|
log_info_no_nl("%.*s", i1, token->line_begin);
|
||||||
|
|
||||||
// Print token part
|
// Print token part
|
||||||
if(color_codes_enabled){
|
if(pctx->color_codes_enabled){
|
||||||
log_info_no_nl( PRINTF_RED "%.*s" PRINTF_RESET, (int)token->len, token->str);
|
log_info_no_nl( PRINTF_RED "%.*s" PRINTF_RESET, (int)token->len, token->str);
|
||||||
} else {
|
} else {
|
||||||
log_info_no_nl("%.*s", (int)token->len, token->str);
|
log_info_no_nl("%.*s", (int)token->len, token->str);
|
||||||
@@ -105,7 +105,7 @@ CORE_Static Token *
|
|||||||
token_get(S64 i = 0){
|
token_get(S64 i = 0){
|
||||||
i += pctx->token_iter;
|
i += pctx->token_iter;
|
||||||
if(i >= pctx->tokens.len){
|
if(i >= pctx->tokens.len){
|
||||||
return &null_token;
|
return &pctx->null_token;
|
||||||
}
|
}
|
||||||
Token *result = &pctx->tokens[i];
|
Token *result = &pctx->tokens[i];
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
@@ -1647,11 +1647,11 @@ resolve_decl(Ast_Decl *ast){
|
|||||||
|
|
||||||
node->unique_name = node->name;
|
node->unique_name = node->name;
|
||||||
if(!is_flag_set(node->expr->flags, AST_FOREIGN)){
|
if(!is_flag_set(node->expr->flags, AST_FOREIGN)){
|
||||||
node->unique_name = pctx->intern(string_fmt(scratch, "%Q%Q%d", symbol_prefix, node->name, pctx->lambda_ids++));
|
node->unique_name = pctx->intern(string_fmt(scratch, "%Q%Q%d", pctx->symbol_prefix, node->name, pctx->lambda_ids++));
|
||||||
}
|
}
|
||||||
|
|
||||||
if(is_flag_set(node->flags, AST_OPERATOR_OVERLOAD)){
|
if(is_flag_set(node->flags, AST_OPERATOR_OVERLOAD)){
|
||||||
node->unique_name = pctx->intern(string_fmt(scratch, "%QOPERATOR_%Q%d", symbol_prefix, node->overload_op_info->name, pctx->lambda_ids++));
|
node->unique_name = pctx->intern(string_fmt(scratch, "%QOPERATOR_%Q%d", pctx->symbol_prefix, node->overload_op_info->name, pctx->lambda_ids++));
|
||||||
}
|
}
|
||||||
|
|
||||||
BREAK();
|
BREAK();
|
||||||
|
|||||||
@@ -252,7 +252,7 @@ type_struct_complete(Ast_Type *type, Ast_Decl *node){
|
|||||||
type->padding = type->size - members_size;
|
type->padding = type->size - members_size;
|
||||||
type->agg.members = members.tight_copy(pctx->perm);
|
type->agg.members = members.tight_copy(pctx->perm);
|
||||||
type->kind = TYPE_STRUCT;
|
type->kind = TYPE_STRUCT;
|
||||||
node->unique_name = pctx->intern(string_fmt(scratch, "%Q%Q", symbol_prefix, node->name));
|
node->unique_name = pctx->intern(string_fmt(scratch, "%Q%Q", pctx->symbol_prefix, node->name));
|
||||||
}
|
}
|
||||||
|
|
||||||
CORE_Static void
|
CORE_Static void
|
||||||
|
|||||||
Reference in New Issue
Block a user