Move operator infos to Core_Ctx

This commit is contained in:
Krzosa Karol
2023-01-01 16:14:47 +01:00
parent 34928e3977
commit d441abcddb
5 changed files with 153 additions and 172 deletions

View File

@@ -23,72 +23,72 @@ core_init_compiler(Core_Ctx *ctx, Allocator *allocator) {
ctx->tokens = array_make<Token>(ctx->heap, 4096 * 4); ctx->tokens = array_make<Token>(ctx->heap, 4096 * 4);
ctx->interns = intern_table_make(ctx->perm, ctx->heap, 2048); ctx->interns = intern_table_make(ctx->perm, ctx->heap, 2048);
/*#import meta /*#import meta
for i in meta.keywords: for i in meta.keywords:
print(f'pctx->keyword_{i.lower()} = pctx->intern("{i}"_s);') print(f'pctx->keyword_{i.lower()} = pctx->intern("{i}"_s);')
print(f'pctx->interns.first_keyword = keyword_{meta.keywords[0].lower()}.str;') print(f'pctx->interns.first_keyword = pctx->keyword_{meta.keywords[0].lower()}.str;')
print(f'pctx->interns.last_keyword = keyword_{meta.keywords[-1].lower()}.str;') print(f'pctx->interns.last_keyword = pctx->keyword_{meta.keywords[-1].lower()}.str;')
for i in meta.interns: for i in meta.interns:
print(f'pctx->intern_{i.lower()} = pctx->intern("{i}"_s);') print(f'pctx->intern_{i.lower()} = pctx->intern("{i}"_s);')
index = 0 index = 0
for i in meta.token_simple_expr: for i in meta.token_simple_expr:
if i[1] != "SPECIAL": if i[1] != "SPECIAL":
print(f'op_info_table[{index}].op = pctx->intern("{i[1]}"_s);') print(f'pctx->op_info_table[{index}] = {{pctx->intern("{i[1]}"_s), "{i[0].upper()}"_s, TK_{i[0]}, {int(i[2]&meta.BINARY_EXPR>0)}, {int(i[2]&meta.UNARY_EXPR>0)}}};')
index += 1 index += 1
*/ */
pctx->keyword_struct = ctx->intern("struct"_s); pctx->keyword_struct = pctx->intern("struct"_s);
pctx->keyword_union = ctx->intern("union"_s); pctx->keyword_union = pctx->intern("union"_s);
pctx->keyword_true = ctx->intern("true"_s); pctx->keyword_true = pctx->intern("true"_s);
pctx->keyword_default = ctx->intern("default"_s); pctx->keyword_default = pctx->intern("default"_s);
pctx->keyword_break = ctx->intern("break"_s); pctx->keyword_break = pctx->intern("break"_s);
pctx->keyword_false = ctx->intern("false"_s); pctx->keyword_false = pctx->intern("false"_s);
pctx->keyword_return = ctx->intern("return"_s); pctx->keyword_return = pctx->intern("return"_s);
pctx->keyword_switch = ctx->intern("switch"_s); pctx->keyword_switch = pctx->intern("switch"_s);
pctx->keyword_assert = ctx->intern("Assert"_s); pctx->keyword_assert = pctx->intern("Assert"_s);
pctx->keyword_if = ctx->intern("if"_s); pctx->keyword_if = pctx->intern("if"_s);
pctx->keyword_elif = ctx->intern("elif"_s); pctx->keyword_elif = pctx->intern("elif"_s);
pctx->keyword_pass = ctx->intern("pass"_s); pctx->keyword_pass = pctx->intern("pass"_s);
pctx->keyword_else = ctx->intern("else"_s); pctx->keyword_else = pctx->intern("else"_s);
pctx->keyword_for = ctx->intern("for"_s); pctx->keyword_for = pctx->intern("for"_s);
pctx->keyword_enum = ctx->intern("enum"_s); pctx->keyword_enum = pctx->intern("enum"_s);
pctx->interns.first_keyword = pctx->keyword_struct.str; pctx->interns.first_keyword = pctx->keyword_struct.str;
pctx->interns.last_keyword = pctx->keyword_enum.str; pctx->interns.last_keyword = pctx->keyword_enum.str;
pctx->intern_typeof = ctx->intern("TypeOf"_s); pctx->intern_typeof = pctx->intern("TypeOf"_s);
pctx->intern_sizeof = ctx->intern("SizeOf"_s); pctx->intern_sizeof = pctx->intern("SizeOf"_s);
pctx->intern_len = ctx->intern("Len"_s); pctx->intern_len = pctx->intern("Len"_s);
pctx->intern_alignof = ctx->intern("AlignOf"_s); pctx->intern_alignof = pctx->intern("AlignOf"_s);
pctx->intern_foreign = ctx->intern("foreign"_s); pctx->intern_foreign = pctx->intern("foreign"_s);
pctx->intern_strict = ctx->intern("strict"_s); pctx->intern_strict = pctx->intern("strict"_s);
pctx->intern_void = ctx->intern("void"_s); pctx->intern_void = pctx->intern("void"_s);
pctx->intern_flag = ctx->intern("flag"_s); pctx->intern_flag = pctx->intern("flag"_s);
pctx->intern_it = ctx->intern("it"_s); pctx->intern_it = pctx->intern("it"_s);
pctx->intern_load = ctx->intern("load"_s); pctx->intern_load = pctx->intern("load"_s);
pctx->intern_import = ctx->intern("import"_s); pctx->intern_import = pctx->intern("import"_s);
pctx->intern_link = ctx->intern("link"_s); pctx->intern_link = pctx->intern("link"_s);
op_info_table[0].op = ctx->intern("*"_s); pctx->op_info_table[0] = {pctx->intern("*"_s), "MUL"_s, TK_Mul, 1, 0};
op_info_table[1].op = ctx->intern("/"_s); pctx->op_info_table[1] = {pctx->intern("/"_s), "DIV"_s, TK_Div, 1, 0};
op_info_table[2].op = ctx->intern("%"_s); pctx->op_info_table[2] = {pctx->intern("%"_s), "MOD"_s, TK_Mod, 1, 0};
op_info_table[3].op = ctx->intern("<<"_s); pctx->op_info_table[3] = {pctx->intern("<<"_s), "LEFTSHIFT"_s, TK_LeftShift, 1, 0};
op_info_table[4].op = ctx->intern(">>"_s); pctx->op_info_table[4] = {pctx->intern(">>"_s), "RIGHTSHIFT"_s, TK_RightShift, 1, 0};
op_info_table[5].op = ctx->intern("+"_s); pctx->op_info_table[5] = {pctx->intern("+"_s), "ADD"_s, TK_Add, 1, 1};
op_info_table[6].op = ctx->intern("-"_s); pctx->op_info_table[6] = {pctx->intern("-"_s), "SUB"_s, TK_Sub, 1, 1};
op_info_table[7].op = ctx->intern("=="_s); pctx->op_info_table[7] = {pctx->intern("=="_s), "EQUALS"_s, TK_Equals, 1, 0};
op_info_table[8].op = ctx->intern("<="_s); pctx->op_info_table[8] = {pctx->intern("<="_s), "LESSERTHENOREQUAL"_s, TK_LesserThenOrEqual, 1, 0};
op_info_table[9].op = ctx->intern(">="_s); pctx->op_info_table[9] = {pctx->intern(">="_s), "GREATERTHENOREQUAL"_s, TK_GreaterThenOrEqual, 1, 0};
op_info_table[10].op = ctx->intern("<"_s); pctx->op_info_table[10] = {pctx->intern("<"_s), "LESSERTHEN"_s, TK_LesserThen, 1, 0};
op_info_table[11].op = ctx->intern(">"_s); pctx->op_info_table[11] = {pctx->intern(">"_s), "GREATERTHEN"_s, TK_GreaterThen, 1, 0};
op_info_table[12].op = ctx->intern("!="_s); pctx->op_info_table[12] = {pctx->intern("!="_s), "NOTEQUALS"_s, TK_NotEquals, 1, 0};
op_info_table[13].op = ctx->intern("&"_s); pctx->op_info_table[13] = {pctx->intern("&"_s), "BITAND"_s, TK_BitAnd, 1, 0};
op_info_table[14].op = ctx->intern("|"_s); pctx->op_info_table[14] = {pctx->intern("|"_s), "BITOR"_s, TK_BitOr, 1, 0};
op_info_table[15].op = ctx->intern("^"_s); pctx->op_info_table[15] = {pctx->intern("^"_s), "BITXOR"_s, TK_BitXor, 1, 0};
op_info_table[16].op = ctx->intern("&&"_s); pctx->op_info_table[16] = {pctx->intern("&&"_s), "AND"_s, TK_And, 1, 0};
op_info_table[17].op = ctx->intern("||"_s); pctx->op_info_table[17] = {pctx->intern("||"_s), "OR"_s, TK_Or, 1, 0};
op_info_table[18].op = ctx->intern("~"_s); pctx->op_info_table[18] = {pctx->intern("~"_s), "NEG"_s, TK_Neg, 0, 1};
op_info_table[19].op = ctx->intern("!"_s); pctx->op_info_table[19] = {pctx->intern("!"_s), "NOT"_s, TK_Not, 0, 1};
/*END*/ /*END*/
init_type(); init_type();

View File

@@ -2,6 +2,7 @@
/* /*
@! Separate out the codegen stage cause that can change @! Separate out the codegen stage cause that can change
@! Change type of Stage allocator @! Change type of Stage allocator
@! Look into String_Builders in Core_Ctx
@! 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
with 2 allocators, and simplify stuff with 2 allocators, and simplify stuff
@@ -96,40 +97,48 @@ struct Core_Ctx{
Token same_scope_token; Token same_scope_token;
Token null_token; Token null_token;
/*#import meta
for i in meta.keywords: print(f'Intern_String keyword_{i.lower()};')
for i in meta.interns: print(f'Intern_String intern_{i.lower()};')
*/
Intern_String keyword_struct;
Intern_String keyword_union;
Intern_String keyword_true;
Intern_String keyword_default;
Intern_String keyword_break;
Intern_String keyword_false;
Intern_String keyword_return;
Intern_String keyword_switch;
Intern_String keyword_assert;
Intern_String keyword_if;
Intern_String keyword_elif;
Intern_String keyword_pass;
Intern_String keyword_else;
Intern_String keyword_for;
Intern_String keyword_enum;
Intern_String intern_typeof;
Intern_String intern_sizeof;
Intern_String intern_len;
Intern_String intern_alignof;
Intern_String intern_foreign;
Intern_String intern_strict;
Intern_String intern_void;
Intern_String intern_flag;
Intern_String intern_it;
Intern_String intern_load;
Intern_String intern_import;
Intern_String intern_link;
/*END*/
/*#import meta /*#import meta
for i in meta.keywords: print(f'Intern_String keyword_{i.lower()};') size = 0
for i in meta.interns: print(f'Intern_String intern_{i.lower()};') for i in meta.token_simple_expr:
if i[1] != "SPECIAL":
*/ size += 1
Intern_String keyword_struct; print(f" Ast_Operator_Info op_info_table[{size}];")
Intern_String keyword_union; */
Intern_String keyword_true; Ast_Operator_Info op_info_table[20];
Intern_String keyword_default; /*END*/
Intern_String keyword_break;
Intern_String keyword_false;
Intern_String keyword_return;
Intern_String keyword_switch;
Intern_String keyword_assert;
Intern_String keyword_if;
Intern_String keyword_elif;
Intern_String keyword_pass;
Intern_String keyword_else;
Intern_String keyword_for;
Intern_String keyword_enum;
Intern_String intern_typeof;
Intern_String intern_sizeof;
Intern_String intern_len;
Intern_String intern_alignof;
Intern_String intern_foreign;
Intern_String intern_strict;
Intern_String intern_void;
Intern_String intern_flag;
Intern_String intern_it;
Intern_String intern_load;
Intern_String intern_import;
Intern_String intern_link;
/*END*/
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);

View File

@@ -1,82 +1,53 @@
/*#import meta /*#import meta
print("""CORE_Static Ast_Operator_Info *
print("Operator_Info op_info_table[] = {")
for i in meta.token_simple_expr:
if i[1] != "SPECIAL":
print(f""" {{{{}}, "{i[0].upper()}"_s, TK_{i[0]}, {int(i[2]&meta.BINARY_EXPR>0)}, {int(i[2]&meta.UNARY_EXPR>0)}}},""")
print("};")
print("""CORE_Static Operator_Info *
get_operator_info(Token_Kind op){ get_operator_info(Token_Kind op){
switch(op){""") switch(op){""")
index = 0 index = 0
for i in meta.token_simple_expr: for i in meta.token_simple_expr:
if i[1] != "SPECIAL": if i[1] != "SPECIAL":
print(f""" case TK_{i[0]}: return op_info_table + {index};""") print(f""" case TK_{i[0]}: return pctx->op_info_table + {index};""")
index += 1 index += 1
print(" default: {}\n }") print(" default: {}\n }")
print(" return 0;\n}") print(" return 0;\n}")
print("""CORE_Static Operator_Info * print("""CORE_Static Ast_Operator_Info *
get_operator_info(Intern_String op){ get_operator_info(Intern_String op){
if(0){}""") if(0){}""")
index = 0 index = 0
for i in meta.token_simple_expr: for i in meta.token_simple_expr:
if i[1] != "SPECIAL": if i[1] != "SPECIAL":
print(f""" else if(op_info_table[{index}].op.str == op.str) return op_info_table + {index};""") print(f""" else if(pctx->op_info_table[{index}].op.str == op.str) return pctx->op_info_table + {index};""")
index += 1 index += 1
print(" return 0;\n}") print(" return 0;\n}")
*/ */
Ast_Operator_Info op_info_table[] = {
{{}, "MUL"_s, TK_Mul, 1, 0},
{{}, "DIV"_s, TK_Div, 1, 0},
{{}, "MOD"_s, TK_Mod, 1, 0},
{{}, "LEFTSHIFT"_s, TK_LeftShift, 1, 0},
{{}, "RIGHTSHIFT"_s, TK_RightShift, 1, 0},
{{}, "ADD"_s, TK_Add, 1, 1},
{{}, "SUB"_s, TK_Sub, 1, 1},
{{}, "EQUALS"_s, TK_Equals, 1, 0},
{{}, "LESSERTHENOREQUAL"_s, TK_LesserThenOrEqual, 1, 0},
{{}, "GREATERTHENOREQUAL"_s, TK_GreaterThenOrEqual, 1, 0},
{{}, "LESSERTHEN"_s, TK_LesserThen, 1, 0},
{{}, "GREATERTHEN"_s, TK_GreaterThen, 1, 0},
{{}, "NOTEQUALS"_s, TK_NotEquals, 1, 0},
{{}, "BITAND"_s, TK_BitAnd, 1, 0},
{{}, "BITOR"_s, TK_BitOr, 1, 0},
{{}, "BITXOR"_s, TK_BitXor, 1, 0},
{{}, "AND"_s, TK_And, 1, 0},
{{}, "OR"_s, TK_Or, 1, 0},
{{}, "NEG"_s, TK_Neg, 0, 1},
{{}, "NOT"_s, TK_Not, 0, 1},
};
CORE_Static Ast_Operator_Info * CORE_Static Ast_Operator_Info *
get_operator_info(Token_Kind op){ get_operator_info(Token_Kind op){
switch(op){ switch(op){
case TK_Mul: return op_info_table + 0; case TK_Mul: return pctx->op_info_table + 0;
case TK_Div: return op_info_table + 1; case TK_Div: return pctx->op_info_table + 1;
case TK_Mod: return op_info_table + 2; case TK_Mod: return pctx->op_info_table + 2;
case TK_LeftShift: return op_info_table + 3; case TK_LeftShift: return pctx->op_info_table + 3;
case TK_RightShift: return op_info_table + 4; case TK_RightShift: return pctx->op_info_table + 4;
case TK_Add: return op_info_table + 5; case TK_Add: return pctx->op_info_table + 5;
case TK_Sub: return op_info_table + 6; case TK_Sub: return pctx->op_info_table + 6;
case TK_Equals: return op_info_table + 7; case TK_Equals: return pctx->op_info_table + 7;
case TK_LesserThenOrEqual: return op_info_table + 8; case TK_LesserThenOrEqual: return pctx->op_info_table + 8;
case TK_GreaterThenOrEqual: return op_info_table + 9; case TK_GreaterThenOrEqual: return pctx->op_info_table + 9;
case TK_LesserThen: return op_info_table + 10; case TK_LesserThen: return pctx->op_info_table + 10;
case TK_GreaterThen: return op_info_table + 11; case TK_GreaterThen: return pctx->op_info_table + 11;
case TK_NotEquals: return op_info_table + 12; case TK_NotEquals: return pctx->op_info_table + 12;
case TK_BitAnd: return op_info_table + 13; case TK_BitAnd: return pctx->op_info_table + 13;
case TK_BitOr: return op_info_table + 14; case TK_BitOr: return pctx->op_info_table + 14;
case TK_BitXor: return op_info_table + 15; case TK_BitXor: return pctx->op_info_table + 15;
case TK_And: return op_info_table + 16; case TK_And: return pctx->op_info_table + 16;
case TK_Or: return op_info_table + 17; case TK_Or: return pctx->op_info_table + 17;
case TK_Neg: return op_info_table + 18; case TK_Neg: return pctx->op_info_table + 18;
case TK_Not: return op_info_table + 19; case TK_Not: return pctx->op_info_table + 19;
default: {} default: {}
} }
return 0; return 0;
@@ -84,26 +55,26 @@ get_operator_info(Token_Kind op){
CORE_Static Ast_Operator_Info * CORE_Static Ast_Operator_Info *
get_operator_info(Intern_String op){ get_operator_info(Intern_String op){
if(0){} if(0){}
else if(op_info_table[0].op.str == op.str) return op_info_table + 0; else if(pctx->op_info_table[0].op.str == op.str) return pctx->op_info_table + 0;
else if(op_info_table[1].op.str == op.str) return op_info_table + 1; else if(pctx->op_info_table[1].op.str == op.str) return pctx->op_info_table + 1;
else if(op_info_table[2].op.str == op.str) return op_info_table + 2; else if(pctx->op_info_table[2].op.str == op.str) return pctx->op_info_table + 2;
else if(op_info_table[3].op.str == op.str) return op_info_table + 3; else if(pctx->op_info_table[3].op.str == op.str) return pctx->op_info_table + 3;
else if(op_info_table[4].op.str == op.str) return op_info_table + 4; else if(pctx->op_info_table[4].op.str == op.str) return pctx->op_info_table + 4;
else if(op_info_table[5].op.str == op.str) return op_info_table + 5; else if(pctx->op_info_table[5].op.str == op.str) return pctx->op_info_table + 5;
else if(op_info_table[6].op.str == op.str) return op_info_table + 6; else if(pctx->op_info_table[6].op.str == op.str) return pctx->op_info_table + 6;
else if(op_info_table[7].op.str == op.str) return op_info_table + 7; else if(pctx->op_info_table[7].op.str == op.str) return pctx->op_info_table + 7;
else if(op_info_table[8].op.str == op.str) return op_info_table + 8; else if(pctx->op_info_table[8].op.str == op.str) return pctx->op_info_table + 8;
else if(op_info_table[9].op.str == op.str) return op_info_table + 9; else if(pctx->op_info_table[9].op.str == op.str) return pctx->op_info_table + 9;
else if(op_info_table[10].op.str == op.str) return op_info_table + 10; else if(pctx->op_info_table[10].op.str == op.str) return pctx->op_info_table + 10;
else if(op_info_table[11].op.str == op.str) return op_info_table + 11; else if(pctx->op_info_table[11].op.str == op.str) return pctx->op_info_table + 11;
else if(op_info_table[12].op.str == op.str) return op_info_table + 12; else if(pctx->op_info_table[12].op.str == op.str) return pctx->op_info_table + 12;
else if(op_info_table[13].op.str == op.str) return op_info_table + 13; else if(pctx->op_info_table[13].op.str == op.str) return pctx->op_info_table + 13;
else if(op_info_table[14].op.str == op.str) return op_info_table + 14; else if(pctx->op_info_table[14].op.str == op.str) return pctx->op_info_table + 14;
else if(op_info_table[15].op.str == op.str) return op_info_table + 15; else if(pctx->op_info_table[15].op.str == op.str) return pctx->op_info_table + 15;
else if(op_info_table[16].op.str == op.str) return op_info_table + 16; else if(pctx->op_info_table[16].op.str == op.str) return pctx->op_info_table + 16;
else if(op_info_table[17].op.str == op.str) return op_info_table + 17; else if(pctx->op_info_table[17].op.str == op.str) return pctx->op_info_table + 17;
else if(op_info_table[18].op.str == op.str) return op_info_table + 18; else if(pctx->op_info_table[18].op.str == op.str) return pctx->op_info_table + 18;
else if(op_info_table[19].op.str == op.str) return op_info_table + 19; else if(pctx->op_info_table[19].op.str == op.str) return pctx->op_info_table + 19;
return 0; return 0;
} }
/*END*/ /*END*/

View File

@@ -11,7 +11,7 @@ union {
Ast_Decl *resolved_decl; Ast_Decl *resolved_decl;
union { union {
bool bool_val; bool bool_val;
double f64_val; double f64_val;
Intern_String intern_val; Intern_String intern_val;
BigInt big_int_val; BigInt big_int_val;
Ast_Type *type_val; Ast_Type *type_val;

View File

@@ -3,9 +3,10 @@ import sys
import os import os
files = os.listdir(".") files = os.listdir(".")
files = [i for i in files if (i.endswith(".cpp") or i.endswith(".h")) and i != "base_unicode.cpp"] files = [i for i in files if (i.endswith(".cpp") or i.endswith(".h")) and i != "test.cpp"]
for file_to_modify in files: for file_to_modify in files:
print(file_to_modify)
fd = open(file_to_modify, "r+") fd = open(file_to_modify, "r+")
f = fd.read() f = fd.read()
END = "/*END*/" END = "/*END*/"
@@ -49,7 +50,7 @@ for file_to_modify in files:
with open(temp_filename, "w") as meta_file: with open(temp_filename, "w") as meta_file:
meta_file.write(program) meta_file.write(program)
result = subprocess.run(["py", temp_filename], stdout=subprocess.PIPE) result = subprocess.run([sys.executable, temp_filename], stdout=subprocess.PIPE)
program_result = result.stdout.decode('utf-8').replace('\r\n', '\n') + END program_result = result.stdout.decode('utf-8').replace('\r\n', '\n') + END
f = before + program + "*/\n" + program_result + after f = before + program + "*/\n" + program_result + after
os.remove(temp_filename) os.remove(temp_filename)