178 lines
4.8 KiB
C
178 lines
4.8 KiB
C
#define _CRT_SECURE_NO_WARNINGS
|
|
#include <stdio.h>
|
|
#include <windows.h>
|
|
|
|
#include "lang.h"
|
|
#include "os.h"
|
|
#include "memory.h"
|
|
|
|
#include "lex.h"
|
|
#include "ast.h"
|
|
#include "parser.h"
|
|
#include "expr.h"
|
|
|
|
global FILE *global_output_file;
|
|
#define lex_print(...) fprintf(global_output_file, __VA_ARGS__)
|
|
#define lex_new_line() lex_print("\n")
|
|
|
|
#include "common.c"
|
|
#include "memory.c"
|
|
#include "parser.c"
|
|
#include "os_win32.c"
|
|
|
|
#include "lex.c"
|
|
#include "expr.c"
|
|
#include "ast.c"
|
|
#include "parse_expr.c"
|
|
#include "parse_decl.c"
|
|
#include "print.c"
|
|
|
|
function void
|
|
lex_test(){
|
|
Tokens t;
|
|
t = lex_stream(lit("3252342510 42524 \"U8Literal\""), lit("test"));
|
|
//tokens_print(t);
|
|
assert(t.len == 3);
|
|
assert(t.tokens[0].int_val == 3252342510);
|
|
assert(t.tokens[1].int_val == 42524);
|
|
assert(t.tokens[2].kind == TK_U8Lit);
|
|
assert(token_compare(t.tokens + 2, lit("U8Literal")));
|
|
|
|
t = lex_stream(lit("_identifier Thing Thing2 lit(\"String_Test\")"), lit("test"));
|
|
//tokens_print(t);
|
|
assert(t.tokens[0].kind == TK_Identifier);
|
|
assert(t.tokens[1].kind == TK_Identifier);
|
|
assert(t.tokens[2].kind == TK_Identifier);
|
|
assert(t.tokens[3].kind == TK_StringLit);
|
|
assert(token_compare(t.tokens, lit("_identifier")));
|
|
assert(token_compare(t.tokens+1, lit("Thing")));
|
|
assert(token_compare(t.tokens+2, lit("Thing2")));
|
|
assert(token_compare(t.tokens+3, lit("String_Test")));
|
|
|
|
t = lex_stream(lit("lit(\"String_Test\"{})(324*=+=-/ *% // Comment \n"
|
|
"Thing /*Thing*/ += -= =- +/%^&*&&|| |>> << <<= >>=/*Error"),
|
|
lit("test"));
|
|
assert(t.tokens[0].kind == TK_Error);
|
|
//tokens_print(t);
|
|
}
|
|
|
|
function void
|
|
parser_test(){
|
|
Parser p = {};
|
|
{
|
|
parser_init(&p);
|
|
Intern_String a = intern_string(&p, lit("Thing"));
|
|
Intern_String b = intern_string(&p, lit("Thing"));
|
|
assert(a.s.str == b.s.str);
|
|
}
|
|
|
|
parser_lex_stream(&p, lit("S64 thing; S64 second_var = 10;"), lit("File"));
|
|
assert(token_match(&p, TK_Identifier));
|
|
assert(token_match(&p, TK_Identifier));
|
|
assert(token_match(&p, TK_Semicolon));
|
|
assert(token_match(&p, TK_Identifier));
|
|
assert(token_match(&p, TK_Identifier));
|
|
assert(token_match(&p, TK_Assign));
|
|
assert(token_match(&p, TK_Int));
|
|
assert(token_match(&p, TK_Semicolon));
|
|
assert(token_match(&p, TK_End));
|
|
assert(token_match(&p, TK_End));
|
|
assert(token_match(&p, TK_End));
|
|
|
|
String exprs[] = {
|
|
lit("(4+2*53)"),
|
|
lit("((4+2)*53)"),
|
|
lit("++5"),
|
|
lit("5--"), // @Todo(Krzosa):
|
|
lit("-5"),
|
|
lit("(+5)"),
|
|
lit("(S64)5"),
|
|
lit("(S64)5+3"),
|
|
lit("534>43?435:42"),
|
|
lit("(534>43?435:42,234,(S64)42,Thing[10][2],Thing(1,2))"),
|
|
};
|
|
for(S64 i = 0; i < buff_cap(exprs); i++){
|
|
parser_lex_stream(&p, exprs[i], lit("File"));
|
|
Expr *expr = parse_expr(&p);
|
|
assert(expr);
|
|
//expr_print(expr);
|
|
//lex_print("\n");
|
|
}
|
|
|
|
String decls = os_read_file(lit("ast.h"));
|
|
parser_lex_stream(&p, decls, lit("File"));
|
|
AST_Node *node = parse(&p);
|
|
assert(node->first_child);
|
|
assert(node->last_child);
|
|
ast_print(node);
|
|
|
|
for(AST_Node *n = node->first_child; n; n=n->next){
|
|
if(n->kind == AK_Enum){
|
|
AST_Node *prefix = ast_find_note(n, lit("prefix"));
|
|
lex_print("global String %s_metadata[] = {\n", n->name.s.str);
|
|
for(AST_Node *member = n->first_child; member; member=member->next){
|
|
lex_print("[");
|
|
if(prefix) expr_print(prefix->expr);
|
|
lex_print("%s] = ", member->name.s.str);
|
|
AST_Node *str = ast_find_note(member, lit("str"));
|
|
lex_print("lit(\"");
|
|
if(str){
|
|
expr_print(str->expr);
|
|
}
|
|
else {
|
|
lex_print("%s", member->name.s.str);
|
|
}
|
|
lex_print("\")");
|
|
lex_print(",\n");
|
|
}
|
|
lex_print("};\n");
|
|
|
|
}
|
|
else if(n->kind == AK_Struct){
|
|
gen_struct(n, (Intern_String){});
|
|
}
|
|
}
|
|
}
|
|
|
|
function S32
|
|
os_main(){
|
|
global_output_file = fopen("output.cc", "w");
|
|
assert_msg(global_output_file, "Failed to open output.txt");
|
|
lex_test();
|
|
parser_test();
|
|
|
|
String_Map maps[] = {
|
|
{lit("cap"), lit("cap")},
|
|
{lit("len"), lit("len")},
|
|
{lit("data"), lit("tokens")},
|
|
{lit("parent_type"), lit("Tokens")},
|
|
{lit("parent_type_lower"), lit("tokens")},
|
|
{lit("var_type_lower"), lit("token")},
|
|
{lit("var_type"), lit("Token")},
|
|
{lit("var_name"), lit("tokens")},
|
|
};
|
|
|
|
|
|
/*
|
|
String keywords[]={
|
|
lit("S64"),
|
|
lit("U64"),
|
|
lit("void"),
|
|
lit("SizeU"),
|
|
lit("struct"),
|
|
lit("union"),
|
|
lit("function"),
|
|
lit("global"),
|
|
};
|
|
|
|
for(S64 i = 0; i < buff_cap(keywords); i++){
|
|
lex_print("global Intern_String keyword_%s;\n", keywords[i].str);
|
|
}
|
|
for(S64 i = 0; i < buff_cap(keywords); i++){
|
|
lex_print("keyword_%s = intern_string(p, lit(\"%s\"));\n", keywords[i].str, keywords[i].str);
|
|
}
|
|
*/
|
|
fclose(global_output_file);
|
|
|
|
return 0;
|
|
} |