111 lines
3.0 KiB
C
111 lines
3.0 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 "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 "parse_expr.c"
|
|
#include "parse_decl.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("cast(S64)5"),
|
|
lit("cast(S64)5+3"),
|
|
lit("534>43?435:42"),
|
|
lit("(534>43?435:42,234,cast(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");
|
|
}
|
|
|
|
}
|
|
|
|
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();
|
|
|
|
fclose(global_output_file);
|
|
|
|
return 0;
|
|
} |