This commit is contained in:
Krzosa Karol
2022-06-04 09:12:27 +02:00
parent 4910671ab9
commit c3f235bd44
5 changed files with 95 additions and 93 deletions

View File

@@ -111,7 +111,7 @@ gen_expr(Ast_Expr *ast){
else{ else{
gen("("); gen("(");
gen_expr(node->left); gen_expr(node->left);
gen("%s", token_kind_string(node->op).str); gen("%s", name(node->op));
gen_expr(node->right); gen_expr(node->right);
gen(")"); gen(")");
} }

View File

@@ -32,6 +32,7 @@ For now I don't thing it should be overloadable.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
@todo @todo
[ ] - Character literal
[ ] - Converting from U64 token to S64 Atom introduces unnanounced error (negates) - probably need big int [ ] - Converting from U64 token to S64 Atom introduces unnanounced error (negates) - probably need big int
[ ] - Passing down program to compile through command line [ ] - Passing down program to compile through command line
[ ] - More for loop variations [ ] - More for loop variations
@@ -42,6 +43,7 @@ For now I don't thing it should be overloadable.
[ ] - Array of inferred size [ ] - Array of inferred size
[ ] - Add single line lambda expressions [ ] - Add single line lambda expressions
[ ] - Ternary operator [ ] - Ternary operator
[ ] - Remodel compound from call to {}
[ ] - Field access rewrite [ ] - Field access rewrite
[ ] - Constants embeded in structs should be able to refer to other constants in that namespace without prefix [ ] - Constants embeded in structs should be able to refer to other constants in that namespace without prefix

View File

@@ -676,76 +676,76 @@ lex_stream(Allocator *token_string_arena, Allocator *map_allocator, String istre
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Token metadata // Token metadata
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
function String function const char *
token_kind_string(Token_Kind kind){ name(Token_Kind kind){
switch(kind){ switch(kind){
case TK_End: return "End of stream"_s; case TK_End: return "End of stream";
case TK_Mul: return "*"_s; case TK_Mul: return "*";
case TK_Div: return "/"_s; case TK_Div: return "/";
case TK_Add: return "+"_s; case TK_Add: return "+";
case TK_Sub: return "-"_s; case TK_Sub: return "-";
case TK_Mod: return "%"_s; case TK_Mod: return "%";
case TK_BitAnd: return "&"_s; case TK_BitAnd: return "&";
case TK_BitOr: return "|"_s; case TK_BitOr: return "|";
case TK_BitXor: return "^"_s; case TK_BitXor: return "^";
case TK_Neg: return "~"_s; case TK_Neg: return "~";
case TK_Not: return "!"_s; case TK_Not: return "!";
case TK_OpenParen: return "("_s; case TK_OpenParen: return "(";
case TK_CloseParen: return ")"_s; case TK_CloseParen: return ")";
case TK_OpenBrace: return "{"_s; case TK_OpenBrace: return "{";
case TK_CloseBrace: return "}"_s; case TK_CloseBrace: return "}";
case TK_OpenBracket: return "["_s; case TK_OpenBracket: return "[";
case TK_CloseBracket: return "]"_s; case TK_CloseBracket: return "]";
case TK_ColonAssign: return ":="_s; case TK_ColonAssign: return ":=";
case TK_Comma: return ","_s; case TK_Comma: return ",";
case TK_Pound: return "#"_s; case TK_Pound: return "#";
case TK_Question: return "?"_s; case TK_Question: return "?";
case TK_ThreeDots: return "..."_s; case TK_ThreeDots: return "...";
case TK_Semicolon: return ";"_s; case TK_Semicolon: return ";";
case TK_Dot: return "."_s; case TK_Dot: return ".";
case TK_LesserThen: return "<"_s; case TK_LesserThen: return "<";
case TK_GreaterThen: return ">"_s; case TK_GreaterThen: return ">";
case TK_Colon: return ":"_s; case TK_Colon: return ":";
case TK_Assign: return "="_s; case TK_Assign: return "=";
case TK_DivAssign: return "/="_s; case TK_DivAssign: return "/=";
case TK_MulAssign: return "*="_s; case TK_MulAssign: return "*=";
case TK_ModAssign: return "%="_s; case TK_ModAssign: return "%=";
case TK_SubAssign: return "-="_s; case TK_SubAssign: return "-=";
case TK_AddAssign: return "+="_s; case TK_AddAssign: return "+=";
case TK_AndAssign: return "&="_s; case TK_AndAssign: return "&=";
case TK_OrAssign: return "|="_s; case TK_OrAssign: return "|=";
case TK_XorAssign: return "^="_s; case TK_XorAssign: return "^=";
case TK_LeftShiftAssign: return "<<="_s; case TK_LeftShiftAssign: return "<<=";
case TK_RightShiftAssign: return ">>="_s; case TK_RightShiftAssign: return ">>=";
case TK_DoubleColon: return "::"_s; case TK_DoubleColon: return "::";
case TK_At: return "@"_s; case TK_At: return "@";
case TK_Decrement: return "--"_s; case TK_Decrement: return "--";
case TK_Increment: return "++"_s; case TK_Increment: return "++";
case TK_PostDecrement: return "--"_s; case TK_PostDecrement: return "--";
case TK_PostIncrement: return "++"_s; case TK_PostIncrement: return "++";
case TK_LesserThenOrEqual: return "<="_s; case TK_LesserThenOrEqual: return "<=";
case TK_GreaterThenOrEqual: return ">="_s; case TK_GreaterThenOrEqual: return ">=";
case TK_Equals: return "=="_s; case TK_Equals: return "==";
case TK_And: return "&&"_s; case TK_And: return "&&";
case TK_Or: return "||"_s; case TK_Or: return "||";
case TK_NotEquals: return "!="_s; case TK_NotEquals: return "!=";
case TK_LeftShift: return "<<"_s; case TK_LeftShift: return "<<";
case TK_RightShift: return ">>"_s; case TK_RightShift: return ">>";
case TK_Arrow: return "->"_s; case TK_Arrow: return "->";
case TK_NewLine: return "New_Line"_s; case TK_NewLine: return "New_Line";
case TK_ExprSizeof: return "sizeof"_s; case TK_ExprSizeof: return "sizeof";
case TK_DocComment: return "Doc_Comment"_s; case TK_DocComment: return "Doc_Comment";
case TK_Comment: return "Comment"_s; case TK_Comment: return "Comment";
case TK_Identifier: return "Identifier"_s; case TK_Identifier: return "Identifier";
case TK_StringLit: return "String_Lit"_s; case TK_StringLit: return "String_Lit";
case TK_Character: return "Character"_s; case TK_Character: return "Character";
case TK_Error: return "Error"_s; case TK_Error: return "Error";
case TK_Float: return "Float"_s; case TK_Float: return "Float";
case TK_Integer: return "int"_s; case TK_Integer: return "int";
case TK_Keyword: return "Keyword"_s; case TK_Keyword: return "Keyword";
case CLOSE_SCOPE: return "Close_Scope"_s; case CLOSE_SCOPE: return "Close_Scope";
case OPEN_SCOPE: return "Open_Scope"_s; case OPEN_SCOPE: return "Open_Scope";
case SAME_SCOPE: return "Same_Scope"_s; case SAME_SCOPE: return "Same_Scope";
default: invalid_codepath; return "<Undefined>"_s; default: invalid_codepath; return "<Undefined>";
} }
} }

View File

@@ -106,7 +106,7 @@ function Token *
token_expect(Token_Kind kind){ token_expect(Token_Kind kind){
Token *token = token_get(); Token *token = token_get();
if(token->kind == kind) return token_next(); if(token->kind == kind) return token_next();
parsing_error(token, "Expected token of kind: [%s], got instead token of kind: [%s]", token_kind_string(kind).str, token_kind_string(token->kind).str); parsing_error(token, "Expected token of kind: [%s], got instead token of kind: [%s]", name(kind), name(token->kind));
return 0; return 0;
} }
@@ -257,7 +257,7 @@ parse_block(){
} }
if(result) stmts.add(result); if(result) stmts.add(result);
else parsing_error(token, "Unexpected token [%s] while parsing statement", token_kind_string(token->kind).str); else parsing_error(token, "Unexpected token [%s] while parsing statement", name(token->kind));
} }
} while(token_match(SAME_SCOPE)); } while(token_match(SAME_SCOPE));
@@ -409,7 +409,7 @@ parse_expr(S64 min_bp){
token_expect(TK_CloseParen); token_expect(TK_CloseParen);
} }
}break; }break;
default: parsing_error(token, "Unexpected token of kind: [%s] in expression", token_kind_string(token->kind).str); return 0; default: parsing_error(token, "Unexpected token of kind: [%s] in expression", name(token->kind)); return 0;
} }
for(;;){ for(;;){
@@ -525,38 +525,38 @@ parse_named(B32 is_global){
} }
} }
Token *name = token_get(); Token *tname = token_get();
if(token_match(TK_Identifier, TK_DoubleColon)){ if(token_match(TK_Identifier, TK_DoubleColon)){
// @note parse struct binding // @note parse struct binding
Token *struct_pos = token_get(); Token *struct_pos = token_get();
if(token_match_keyword(keyword_struct)){ if(token_match_keyword(keyword_struct)){
Ast_Struct *struct_val = parse_struct(struct_pos); Ast_Struct *struct_val = parse_struct(struct_pos);
result = ast_const(name, name->intern_val, (Ast_Expr *)struct_val); result = ast_const(tname, tname->intern_val, (Ast_Expr *)struct_val);
} }
else if(token_match_keyword(keyword_enum)){ else if(token_match_keyword(keyword_enum)){
Ast_Enum *enum_val = parse_enum(struct_pos); Ast_Enum *enum_val = parse_enum(struct_pos);
result = ast_const(name, name->intern_val, (Ast_Expr *)enum_val); result = ast_const(tname, tname->intern_val, (Ast_Expr *)enum_val);
} }
// @note parse constant expression // @note parse constant expression
else{ else{
Ast_Expr *expr = parse_expr(); Ast_Expr *expr = parse_expr();
result = ast_const(name, name->intern_val, expr); result = ast_const(tname, tname->intern_val, expr);
} }
} }
else if(token_match(TK_Identifier, TK_Colon)){ else if(token_match(TK_Identifier, TK_Colon)){
Ast_Expr *typespec = typespec = parse_expr(); Ast_Expr *typespec = typespec = parse_expr();
Ast_Expr *expr = parse_assign_expr(); Ast_Expr *expr = parse_assign_expr();
result = ast_var(name, typespec, name->intern_val, expr); result = ast_var(tname, typespec, tname->intern_val, expr);
} }
else if(token_match(TK_Identifier, TK_ColonAssign)){ else if(token_match(TK_Identifier, TK_ColonAssign)){
Ast_Expr *expr = parse_expr(); Ast_Expr *expr = parse_expr();
result = ast_var(name, 0, name->intern_val, expr); result = ast_var(tname, 0, tname->intern_val, expr);
} }
else if(is_global && name->kind != TK_End){ else if(is_global && tname->kind != TK_End){
parsing_error(name, "Unexpected token: [%s] when parsing a declaration", token_kind_string(name->kind).str); parsing_error(tname, "Unexpected token: [%s] when parsing a declaration", name(tname->kind));
} }
return result; return result;

View File

@@ -128,12 +128,12 @@ value_get_float(Value value){
#include <math.h> // fmod #include <math.h> // fmod
function Value function Value
eval_binary(Token *pos, Token_Kind op, Value a, Value b){ eval_binary(Token *pos, Token_Kind op, Value a, Value b){
if(!(is_numeric(a.type) && is_numeric(b.type))) parsing_error(pos, "Constant application of binary %s on values of type %s and %s is not allowed", token_kind_string(op).str, docname(a.type), docname(a.type)); if(!(is_numeric(a.type) && is_numeric(b.type))) parsing_error(pos, "Constant application of binary %s on values of type %s and %s is not allowed", name(op), docname(a.type), docname(a.type));
// @warning: this bool path returns early, always should return untyped bool // @warning: this bool path returns early, always should return untyped bool
if(is_bool(a.type) || is_bool(b.type)){ if(is_bool(a.type) || is_bool(b.type)){
if(!is_bool(a.type)) parsing_error(pos, "Type mismatch in binary operation %s - left: %s right: %s", token_kind_string(op).str, docname(a.type), docname(b.type)); if(!is_bool(a.type)) parsing_error(pos, "Type mismatch in binary operation %s - left: %s right: %s", name(op), docname(a.type), docname(b.type));
if(!is_bool(b.type)) parsing_error(pos, "Type mismatch in binary operation %s - left: %s right: %s", token_kind_string(op).str, docname(a.type), docname(b.type)); if(!is_bool(b.type)) parsing_error(pos, "Type mismatch in binary operation %s - left: %s right: %s", name(op), docname(a.type), docname(b.type));
Value result; Value result;
result.type = untyped_bool; result.type = untyped_bool;
@@ -163,7 +163,7 @@ eval_binary(Token *pos, Token_Kind op, Value a, Value b){
} }
if(is_typed(a.type) && is_typed(b.type)){ if(is_typed(a.type) && is_typed(b.type)){
if(a.type != b.type) parsing_error(pos, "Type mismatch in binary operation %s - left: %s right: %s", token_kind_string(op).str, docname(a.type), docname(b.type)); if(a.type != b.type) parsing_error(pos, "Type mismatch in binary operation %s - left: %s right: %s", name(op), docname(a.type), docname(b.type));
else final_type = a.type; else final_type = a.type;
} }
@@ -249,18 +249,18 @@ eval_binary(Token *pos, Token_Kind op, Value a, Value b){
// @WARNING: When introducing big ints & | ^ will be problematic // @WARNING: When introducing big ints & | ^ will be problematic
case TK_BitAnd: { case TK_BitAnd: {
left_int = left_int & right_int; left_int = left_int & right_int;
if(before_conversion.type == untyped_float) parsing_error(pos, "%s cant be performed on [Untyped_Float]", token_kind_string(op).str); if(before_conversion.type == untyped_float) parsing_error(pos, "%s cant be performed on [Untyped_Float]", name(op));
} break; } break;
case TK_BitOr: { case TK_BitOr: {
left_int = left_int | right_int; left_int = left_int | right_int;
if(before_conversion.type == untyped_float) parsing_error(pos, "%s cant be performed on [Untyped_Float]", token_kind_string(op).str); if(before_conversion.type == untyped_float) parsing_error(pos, "%s cant be performed on [Untyped_Float]", name(op));
} break; } break;
case TK_BitXor: { case TK_BitXor: {
left_int = left_int ^ right_int; left_int = left_int ^ right_int;
if(before_conversion.type == untyped_float) parsing_error(pos, "%s cant be performed on [Untyped_Float]", token_kind_string(op).str); if(before_conversion.type == untyped_float) parsing_error(pos, "%s cant be performed on [Untyped_Float]", name(op));
} break; } break;
default: parsing_error(pos, "Binary operation %s is not allowed on types: left: %s right: %s", token_kind_string(op).str, docname(a.type), docname(b.type)); default: parsing_error(pos, "Binary operation %s is not allowed on types: left: %s right: %s", name(op), docname(a.type), docname(b.type));
} }
if(before_conversion.type == untyped_float){ if(before_conversion.type == untyped_float){
@@ -288,7 +288,7 @@ try_untyping(Operand *op){
function Value function Value
eval_unary(Token *pos, Token_Kind op, Value a){ eval_unary(Token *pos, Token_Kind op, Value a){
if(!is_numeric(a.type)) parsing_error(pos, "Constant application of binary %s on values of type %s is not allowed", token_kind_string(op).str, docname(a.type)); if(!is_numeric(a.type)) parsing_error(pos, "Constant application of binary %s on values of type %s is not allowed", name(op), docname(a.type));
S64 left_int = value_get_int(a); S64 left_int = value_get_int(a);
F64 left_float = value_get_float(a); F64 left_float = value_get_float(a);
@@ -710,7 +710,7 @@ resolve_expr(Ast_Expr *ast, Ast_Resolved_Type *expected_type, Sym *lambda_to_res
case TK_Dereference:{return operand_lvalue(type_pointer(value.type));}break; case TK_Dereference:{return operand_lvalue(type_pointer(value.type));}break;
case TK_Neg:case TK_Not:case TK_Add:case TK_Sub:{ case TK_Neg:case TK_Not:case TK_Add:case TK_Sub:{
Operand op = resolve_expr(node->expr); Operand op = resolve_expr(node->expr);
if(!is_numeric(op.type)) parsing_error(node->pos, "Unary [%s] cant be applied to value of type %s", token_kind_string(node->op).str, docname(op.type)); if(!is_numeric(op.type)) parsing_error(node->pos, "Unary [%s] cant be applied to value of type %s", name(node->op), docname(op.type));
if(op.is_const){ if(op.is_const){
Value value = eval_unary(node->pos, node->op, op.value); Value value = eval_unary(node->pos, node->op, op.value);
rewrite_into_const(node, Ast_Unary, value); rewrite_into_const(node, Ast_Unary, value);
@@ -858,7 +858,7 @@ resolve_expr(Ast_Expr *ast, Ast_Resolved_Type *expected_type, Sym *lambda_to_res
if(left.type != right.type){ if(left.type != right.type){
parsing_error(node->pos, "Type mismatch in binary operation %s - left: %s right: %s", token_kind_string(node->op).str, docname(left.type), docname(right.type)); parsing_error(node->pos, "Type mismatch in binary operation %s - left: %s right: %s", name(node->op), docname(left.type), docname(right.type));
} }
else{ else{
result = operand_rvalue(left.type); result = operand_rvalue(left.type);