Skip to content

Instantly share code, notes, and snippets.

@UplinkCoder
Last active August 1, 2022 17:26
Show Gist options
  • Save UplinkCoder/ce6731b9fda949d5e4e86bf1e9169848 to your computer and use it in GitHub Desktop.
Save UplinkCoder/ce6731b9fda949d5e4e86bf1e9169848 to your computer and use it in GitHub Desktop.
#define ACCEL ACCEL_TABLE
#define NO_FIBERS
#define NO_PREPROCESSOR
#include "compat.h"
#include "metac_driver.c"
#include "utils/read_file.c"
#include "metac_parser_obj.c"
#include "metac_semantic_obj.c"
#include "metac_lpp.c"
#include "repl/exp_eval.c"
DeclarationArray ReadLexParse(const char* filename, metac_lpp_t* lpp)
{
DeclarationArray result = {0};
read_result_t readResult =
ReadFileAndZeroTerminate(filename);
LexFile(&lpp->Lexer, filename,
readResult.FileContent0, readResult.FileLength
);
MetaCParser_InitFromLexer(&lpp->Parser, &lpp->Lexer);
ParseFile(&lpp->Parser, filename, &result);
return result;
}
metac_declaration_t* FindDeclaration(DeclarationArray decls,
metac_parser_t* parser, const char* name)
{
const uint32_t len = cast(uint32_t) strlen(name);
const uint32_t hash = crc32c_nozero(~0, name, len);
const uint32_t key = IDENTIFIER_KEY(hash, len);
metac_identifier_ptr_t NameId =
IsIdentifierInTable(&parser->IdentifierTable, key, name);
if (NameId.v == 0)
return 0;
for(uint32_t idx = 0;
idx < decls.Length;
idx++)
{
metac_declaration_t* decl = decls.Ptr[idx];
// printf("decl: %s\n",
// MetaCPrinter_PrintDeclaration(&parser->DebugPrinter, decl));
metac_identifier_ptr_t idPtr;
if (decl->DeclKind == decl_type_enum)
{
idPtr = (cast(decl_type_enum_t*) decl)->Identifier;
}
else if (decl->DeclKind == decl_function)
{
idPtr = (cast(decl_function_t*) decl)->Identifier;
}
else if (decl->DeclKind == decl_type_typedef)
{
decl_type_typedef_t* typdef = cast(decl_type_typedef_t*) decl;
idPtr = typdef->Identifier;
decl = cast(metac_declaration_t*) typdef->Type;
}
if (idPtr.v == NameId.v)
return decl;
}
return 0;
}
void main(int argc, const char* argv[])
{
metac_lpp_t LPP;
MetaCLPP_Init(&LPP);
if (argc == 1)
argv[1] = "/home/uplink/dev/metac/lexer_exp.c";
DeclarationArray decls = ReadLexParse(argv[1], &LPP);
printf("Read %u declarations\n", decls.Length);
decl_type_enum_t* tokenEnum = cast(decl_type_enum_t*)
FindDeclaration(decls, &LPP.Parser, "metac_token_enum_t");
// printf("tokenEnum: %p\n", tokenEnum);
assert(tokenEnum && tokenEnum->DeclKind == decl_type_enum);
metac_semantic_state_t Sema;
MetaCSemantic_Init(&Sema, &LPP.Parser, 0);
metac_scope_t* scope =
MetaCSemantic_PushNewScope(&Sema, scope_parent_module, (metac_node_t)1);
sema_decl_type_enum_t semaTokenEnum = {0};
semaTokenEnum.Members = cast(sema_decl_enum_member_t*)
calloc(sizeof(sema_decl_enum_member_t),
tokenEnum->MemberCount);
semaTokenEnum.MemberCount = tokenEnum->MemberCount;
MetaCSemantic_ComputeEnumValues(&Sema, tokenEnum, &semaTokenEnum);
// determine min and max
int64_t min = 0xffffffff;
int64_t max = -0xffffffff;
for(uint32_t memberIdx = 0;
memberIdx < semaTokenEnum.MemberCount;
memberIdx++)
{
metac_sema_expression_t* value =
semaTokenEnum.Members[memberIdx].Value;
if (value->ValueI64 > max)
max = value->ValueI64;
if (value->ValueI64 < min)
min = value->ValueI64;
}
printf("minVal = %ld, maxVal %ld\n", min, max);
decl_function_t* lengthFunc = cast(decl_function_t*)
FindDeclaration(decls, &LPP.Parser, "MetaCStaticTokenLength");
assert(lengthFunc && lengthFunc->DeclKind == decl_function);
sema_decl_function_t* semaFunc =
MetaCSemantic_doFunctionSemantic(&Sema, lengthFunc);
printf("Func: %s\n",
MetaCPrinter_PrintSemaNode(&LPP.Parser.DebugPrinter, &Sema, semaFunc));
}
typedef enum metac_token_enum_t
{
tok_invalid, tok_identifier, tok_uint, tok_string, tok_char, tok_char_uni, tok_comment_single, tok_comment_multi,
tok_macro_parameter, tok_bang, tok_question, tok_hash, tok_at, tok_lParen, tok_rParen, tok_lBrace, tok_rBrace, tok_lBracket,
tok_rBracket, tok_semicolon, tok_colon, tok_dollar, tok_cat, tok_comma, tok_dot, tok_plus, tok_minus, tok_star, tok_div,
tok_rem, tok_xor, tok_or, tok_and, tok_lsh, tok_rsh, tok_oror, tok_andand, tok_arrow, tok_dotdot, tok_assign, tok_add_ass,
tok_sub_ass, tok_mul_ass, tok_div_ass, tok_rem_ass, tok_xor_ass, tok_or_ass, tok_and_ass, tok_lsh_ass, tok_rsh_ass,
tok_equals_equals, tok_not_equal, tok_lt, tok_le, tok_gt, tok_ge, tok_spaceship, tok_dotdotdot, tok_kw_struct, tok_kw_union,
tok_kw_enum, tok_kw_typedef, tok_kw_auto, tok_kw_void, tok_kw_bool, tok_kw_char, tok_kw_short, tok_kw_int, tok_kw_long,
tok_kw_size_t, tok_kw_float, tok_kw_double, tok_kw_signed, tok_kw_unsigned, tok_kw_const, tok_kw_volatile,
tok_kw___shared, tok_kw_extern, tok_kw_for, tok_kw_sizeof, tok_kw_return, tok_kw_switch, tok_kw_while, tok_kw_do,
tok_kw_typeof, tok_kw_inject, tok_kw_eject, tok_kw_assert, tok_kw_case, tok_kw_default, tok_kw_goto, tok_kw_static,
tok_kw_inline, tok_kw_if, tok_kw_else, tok_kw_break, tok_kw_continue, tok_kw_until, tok_kw_yield,
tok_kw___attribute__, tok_comment_begin_multi, tok_comment_end_multi, tok_comment_begin_single, tok_plusplus,
tok_minusminus, tok_full_slice, tok_hashhash, tok_newline, tok_eof, tok_error,
} metac_token_enum_t;
static int MetaCStaticTokenLength(metac_token_enum_t t)
{
switch (t) {
default : return 2;
case tok_eof : return 0;
case tok_bang : return 1;
case tok_question : return 1;
case tok_hash : return 1;
case tok_at : return 1;
case tok_lParen : return 1;
case tok_rParen : return 1;
case tok_lBrace : return 1;
case tok_rBrace : return 1;
case tok_lBracket : return 1;
case tok_rBracket : return 1;
case tok_semicolon : return 1;
case tok_colon : return 1;
case tok_dollar : return 1;
case tok_comma : return 1;
case tok_dot : return 1;
case tok_plus : return 1;
case tok_minus : return 1;
case tok_star : return 1;
case tok_rem : return 1;
case tok_div : return 1;
case tok_xor : return 1;
case tok_or : return 1;
case tok_and : return 1;
case tok_cat : return 1;
case tok_assign : return 1;
case tok_lt : return 1;
case tok_gt : return 1;
case tok_newline : return 1;
case tok_lsh_ass : return 3;
case tok_rsh_ass : return 3;
case tok_spaceship : return 3;
case tok_dotdotdot : return 3;
case tok_kw_struct : return 6;
case tok_kw_union : return 5;
case tok_kw_enum : return 4;
case tok_kw_const : return 5;
case tok_kw_return : return 6;
case tok_kw_switch : return 6;
case tok_kw_while : return 5;
case tok_kw_typeof : return 6;
case tok_kw_inject : return 6;
case tok_kw_eject : return 5;
case tok_kw_assert : return 6;
case tok_kw_typedef : return 7;
case tok_kw_case : return 4;
case tok_kw_default : return 7;
case tok_kw_static : return 6;
case tok_kw_inline : return 6;
case tok_kw_else : return 4;
case tok_kw_break : return 5;
case tok_kw_continue : return 8;
case tok_kw_until : return 5;
case tok_kw_auto : return 4;
case tok_kw_bool : return 4;
case tok_kw_double : return 6;
case tok_kw_float : return 5;
case tok_kw_long : return 4;
case tok_kw_int : return 3;
case tok_kw_short : return 5;
case tok_kw_char : return 4;
case tok_kw_void : return 4;
case tok_kw_signed : return 6;
case tok_kw_unsigned : return 8;
case tok_kw_volatile : return 8;
case tok_kw___shared : return 8;
case tok_kw_extern : return 6;
case tok_kw_for : return 3;
case tok_kw_sizeof : return 6;
case tok_kw_size_t : return 6;
case tok_kw_goto : return 4;
case tok_kw_yield : return 5;
case tok_kw___attribute__ : return 13;
}
}
Read 2 declarations
minVal = 0, maxVal 109
Func: MetaCStaticTokenLength() {
switch (t)
{
default:
return 2;
return 2;
case 108:
return 0;
return 0;
case 9:
return 1;
return 1;
case 10:
return 1;
return 1;
case 11:
return 1;
return 1;
case 12:
return 1;
return 1;
case 13:
return 1;
return 1;
case 14:
return 1;
return 1;
case 15:
return 1;
return 1;
case 16:
return 1;
return 1;
case 17:
return 1;
return 1;
case 18:
return 1;
return 1;
case 19:
return 1;
return 1;
case 20:
return 1;
return 1;
case 21:
return 1;
return 1;
case 23:
return 1;
return 1;
case 24:
return 1;
return 1;
case 25:
return 1;
return 1;
case 26:
return 1;
return 1;
case 27:
return 1;
return 1;
case 29:
return 1;
return 1;
case 28:
return 1;
return 1;
case 30:
return 1;
return 1;
case 31:
return 1;
return 1;
case 32:
return 1;
return 1;
case 22:
return 1;
return 1;
case 39:
return 1;
return 1;
case 52:
return 1;
return 1;
case 54:
return 1;
return 1;
case 107:
return 1;
return 1;
case 48:
return 3;
return 3;
case 49:
return 3;
return 3;
case 56:
return 3;
return 3;
case 57:
return 3;
return 3;
case 58:
return 6;
return 6;
case 59:
return 5;
return 5;
case 60:
return 4;
return 4;}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment