From 2909214ee01e30e61b1d1ca68f1932080a9c36a8 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Thu, 2 Jun 2022 22:52:16 +0200 Subject: [PATCH] Add floats --- ccodegen.cpp | 3 +++ main.cpp | 16 ++++++++-------- new_ast.cpp | 8 ++++++++ new_lex.cpp | 37 ++++++++++++++++++++++++++++++------- new_parse.cpp | 1 + order2.kl | 1 + 6 files changed, 51 insertions(+), 15 deletions(-) diff --git a/ccodegen.cpp b/ccodegen.cpp index bec2efd..9501784 100644 --- a/ccodegen.cpp +++ b/ccodegen.cpp @@ -320,6 +320,9 @@ gen_ast(Ast *ast){ gen(";"); } } + else if(sym->type == type_f64){ + gen("// constant int %s = %f;", node->name.str, sym->f64_val); + } else if(sym->type == type_int){ gen("// constant int %s = %lld;", node->name.str, sym->int_val); } diff --git a/main.cpp b/main.cpp index 03f1772..b16c9e7 100644 --- a/main.cpp +++ b/main.cpp @@ -100,14 +100,14 @@ int main(){ String result = {}; #if 1 - result = compile_file("globals.kl"_s); - printf("%s", result.str); - result = compile_file("enums.kl"_s); - printf("%s", result.str); - result = compile_file("order1.kl"_s); - printf("%s", result.str); - result = compile_file("lambdas.kl"_s); - printf("%s", result.str); + // result = compile_file("globals.kl"_s); + // printf("%s", result.str); + // result = compile_file("enums.kl"_s); + // printf("%s", result.str); + // result = compile_file("order1.kl"_s); + // printf("%s", result.str); + // result = compile_file("lambdas.kl"_s); + // printf("%s", result.str); result = compile_file("order2.kl"_s); printf("%s", result.str); #endif diff --git a/new_ast.cpp b/new_ast.cpp index 17a2109..966d441 100644 --- a/new_ast.cpp +++ b/new_ast.cpp @@ -295,6 +295,14 @@ ast_ident(Token *pos, Intern_String string){ return result; } +function Ast_Atom * +ast_float(Token *pos, F64 value){ + AST_NEW(Atom, VALUE, pos, AST_EXPR | AST_ATOM); + result->type = type_f64; // @todo untyped + result->f64_val = value; + return result; +} + function Ast_Atom * ast_int(Token *pos, S64 integer){ AST_NEW(Atom, VALUE, pos, AST_EXPR | AST_ATOM); diff --git a/new_lex.cpp b/new_lex.cpp index ed1f095..ebba091 100644 --- a/new_lex.cpp +++ b/new_lex.cpp @@ -103,7 +103,7 @@ struct Token{ union { U64 int_val; - F64 float_val; + F64 f64_val; String error_val; Intern_String intern_val; S64 indent; @@ -137,8 +137,6 @@ force_inline B32 token_is_assign(Token *token){return token_is_assign(token->kin force_inline B32 token_is_compare(Token_Kind token){return token >= TK_FirstCompare && token <= TK_LastCompare;} force_inline B32 token_is_compare(Token *token){return token_is_compare(token->kind);} - - function U8 lexc(Lex_Stream *s){ return s->stream.str[s->iter]; @@ -209,6 +207,7 @@ token_error(Token *t, String error_val){ function void lex_parse_u64(Token *t){ + t->kind = TK_Integer; U64 result = 0; U64 m = 1; for(S64 i = t->len - 1; i >= 0; --i){ @@ -224,6 +223,16 @@ lex_parse_u64(Token *t){ t->int_val = result; } +function void +lex_parse_f64(Token *t){ + t->kind = TK_Float; + char buffer[128]; + S64 len = clamp_top((int)t->len, 126); + memory_copy(buffer, t->str, len); + buffer[len] = 0; + t->f64_val = strtod(buffer, 0); +} + function void lex_advance(Lex_Stream *s){ if(s->iter >= s->stream.len){ @@ -574,11 +583,25 @@ lex__stream(Intern_Table *table, Array *array, Lex_Stream *s){ case '0':case '1':case '2':case '3':case '4': case '5':case '6':case '7':case '8':case '9':{ - t.kind = TK_Integer; - while(lex_is_numeric(lexc(s))) + B32 found_dot = false; + for(;;){ + if(lex_is_numeric(lexc(s))) + ; + else if(lexc(s) == '.'){ + if(found_dot){ + token_error(&t, "Multiple '.' in float literal"_s); + goto end_of_switch; + } + found_dot = true; + } + else break; + lex_advance(s); + } lex_set_len(s, &t); - lex_parse_u64(&t); + if(found_dot) lex_parse_f64(&t); + else lex_parse_u64(&t); + } break; case 'A':case 'a':case 'M':case 'm':case 'B': @@ -603,7 +626,7 @@ lex__stream(Intern_Table *table, Array *array, Lex_Stream *s){ default: { token_error(&t, "Unknown token"_s); } - } + }end_of_switch: if(t.len==0) lex_set_len(s,&t); diff --git a/new_parse.cpp b/new_parse.cpp index 2f8db87..e4a6454 100644 --- a/new_parse.cpp +++ b/new_parse.cpp @@ -362,6 +362,7 @@ parse_expr(S64 min_bp){ case TK_StringLit : left = ast_str(token, token->intern_val); break; case TK_Identifier : left = ast_ident(token, token->intern_val); break; case TK_Integer : left = ast_int(token, token->int_val); break; + case TK_Float : left = ast_float(token, token->f64_val); break; case TK_Pointer : left = ast_expr_unary(token, TK_Pointer, parse_expr(prefix_bp.right)); break; case TK_Dereference: left = ast_expr_unary(token, TK_Dereference, parse_expr(prefix_bp.right)); break; diff --git a/order2.kl b/order2.kl index c162daa..8618b28 100644 --- a/order2.kl +++ b/order2.kl @@ -58,6 +58,7 @@ test_assignments :: () i = i > 2 CONST :: 23 == 23 + CONST_FLOAT :: 23.52 j: *Int *j = 1