Added unary ++ --, prefix and postfix, working on running a program

This commit is contained in:
Krzosa Karol
2022-06-07 09:05:02 +02:00
parent d3da979d64
commit c5b82c0532
6 changed files with 51 additions and 15 deletions

View File

@@ -132,8 +132,10 @@ gen_expr(Ast_Expr *ast){
} }
CASE(UNARY, Unary){ CASE(UNARY, Unary){
gen("(%s", name(node->op)); gen("(");
if(node->op != TK_PostIncrement && node->op != TK_PostDecrement) gen("%s", name(node->op));
gen_expr(node->expr); gen_expr(node->expr);
if(node->op == TK_PostIncrement || node->op == TK_PostDecrement) gen("%s", name(node->op));
gen(")"); gen(")");
BREAK(); BREAK();
} }
@@ -388,7 +390,11 @@ gen_ast(Ast *ast){
BREAK(); BREAK();
} }
invalid_default_case; default: {
assert(is_flag_set(ast->flags, AST_EXPR));
gen_expr((Ast_Expr *)ast);
gen(";");
}
} }
} }

View File

@@ -77,11 +77,11 @@ Expr:
@todo @todo
[ ] - Make sure pointer arithmetic works [ ] - We need ++ -- operators
[ ] - Passing down program to compile through command line [ ] - Passing down program to compile through command line
[ ] - More for loop variations
[ ] - Write up on order independent declarations
[ ] - Switch [ ] - Switch
[ ] - Arrays with size passed
[ ] - Some way to call foreign functions
[ ] - Comma notation when declaring variables thing1, thing2: S32 [ ] - Comma notation when declaring variables thing1, thing2: S32
[ ] - Array of inferred size [ ] - Array of inferred size
@@ -95,6 +95,7 @@ Expr:
[ ] - Order independent constants in structs [ ] - Order independent constants in structs
[ ] - Fix recursive lambdas in structs [ ] - Fix recursive lambdas in structs
[ ] - Fixing access to functions/structs, in C we cant have functons inside of structs / functions so we need to rewrite the tree [ ] - Fixing access to functions/structs, in C we cant have functons inside of structs / functions so we need to rewrite the tree
[ ] - Write up on order independent declarations
[ ] - Casting to basic types by call S64(x) [ ] - Casting to basic types by call S64(x)
[ ] - Default values in structs??? Should compound stmts bring values from default values?? Maybe not? Whats the alternative [ ] - Default values in structs??? Should compound stmts bring values from default values?? Maybe not? Whats the alternative
@@ -105,6 +106,7 @@ Expr:
[ ] - Constant arrays that evaluate fully at compile time [ ] - Constant arrays that evaluate fully at compile time
[ ] - Rust like enum where you associate values(other structs) with keys [ ] - Rust like enum where you associate values(other structs) with keys
[ ] - Compound that zeros values - .{} , Compound that assumes defaults from struct definition - {} [ ] - Compound that zeros values - .{} , Compound that assumes defaults from struct definition - {}
[ ] - Inject stack traces into the program
@donzo @donzo
[x] - We are parsing wrong here: (t.str=(&string_to_lex.str)[i]); [x] - We are parsing wrong here: (t.str=(&string_to_lex.str)[i]);
@@ -113,11 +115,13 @@ Expr:
[x] - More basic types [x] - More basic types
[x] - Implementing required operations int128 [x] - Implementing required operations int128
[x] - Fix casting [x] - Fix casting
[x] - More for loop variations
[x] - Add basic support for floats [x] - Add basic support for floats
[x] - Converting from U64 token to S64 Atom introduces unnanounced error (negates) - probably need big int [x] - Converting from U64 token to S64 Atom introduces unnanounced error (negates) - probably need big int
[x] - Add basic setup for new type system [x] - Add basic setup for new type system
[x] - Access through struct names to constants Arena.CONSTANT [x] - Access through struct names to constants Arena.CONSTANT
[x] - Enums [x] - Enums
[x] - Make sure pointer arithmetic works
[x] - Initial for loop [x] - Initial for loop
[x] - Enum . access to values [x] - Enum . access to values
[x] - Character literal [x] - Character literal
@@ -190,7 +194,7 @@ int main(){
assert(f); assert(f);
fprintf(f, "%.*s", (int)result.len, result.str); fprintf(f, "%.*s", (int)result.len, result.str);
fclose(f); fclose(f);
system("clang.exe program.c -g -o program.exe && program.exe"); system("clang.exe program.c -g -o program.exe");
#endif #endif
__debugbreak(); __debugbreak();

View File

@@ -263,8 +263,13 @@ parse_block(){
result = parse_init_stmt((Ast_Expr *)result); result = parse_init_stmt((Ast_Expr *)result);
} }
if(result) stmts.add(result); if(result) {
else parsing_error(token, "Unexpected token [%s] while parsing statement", name(token->kind)); result->flags = set_flag(result->flags, AST_STMT);
stmts.add(result);
}
else {
parsing_error(token, "Unexpected token [%s] while parsing statement", name(token->kind));
}
} }
} while(token_match(SAME_SCOPE)); } while(token_match(SAME_SCOPE));
@@ -320,6 +325,8 @@ binding_power(Binding binding, Token_Kind kind){
else invalid_codepath; else invalid_codepath;
Prefix: switch(kind){ Prefix: switch(kind){
case TK_Increment:
case TK_Decrement:
case TK_Pointer: case TK_Pointer:
case TK_Dereference: case TK_Dereference:
case TK_OpenBracket: case TK_OpenBracket:
@@ -361,6 +368,8 @@ binding_power(Binding binding, Token_Kind kind){
default: return {}; default: return {};
} }
Postfix: switch(kind){ Postfix: switch(kind){
case TK_Increment:
case TK_Decrement:
case TK_OpenBracket: case TK_OpenBracket:
case TK_OpenParen: case TK_OpenParen:
return {21, -2}; return {21, -2};
@@ -387,6 +396,8 @@ parse_expr(S64 min_bp){
case TK_Add : left = ast_expr_unary(token, TK_Add, parse_expr(prefix_bp.right)); break; case TK_Add : left = ast_expr_unary(token, TK_Add, parse_expr(prefix_bp.right)); break;
case TK_Not : left = ast_expr_unary(token, TK_Not, parse_expr(prefix_bp.right)); break; case TK_Not : left = ast_expr_unary(token, TK_Not, parse_expr(prefix_bp.right)); break;
case TK_Neg : left = ast_expr_unary(token, TK_Neg, parse_expr(prefix_bp.right)); break; case TK_Neg : left = ast_expr_unary(token, TK_Neg, parse_expr(prefix_bp.right)); break;
case TK_Increment : left = ast_expr_unary(token, TK_Increment, parse_expr(prefix_bp.right)); break;
case TK_Decrement : left = ast_expr_unary(token, TK_Decrement, parse_expr(prefix_bp.right)); break;
case TK_OpenBracket: { case TK_OpenBracket: {
Ast_Array *result = ast_array(token, parse_expr(0)); Ast_Array *result = ast_array(token, parse_expr(0));
@@ -482,7 +493,7 @@ parse_struct(Token *pos){
token_match(OPEN_SCOPE); token_match(OPEN_SCOPE);
do{ do{
Token *token = token_get(); Token *token = token_get();
Ast_Named *named = parse_named(false); Ast_Named *named = parse_named(false);
if(!named) parsing_error(token, "Failed to parse struct member"); if(!named) parsing_error(token, "Failed to parse struct member");
named->flags = set_flag(named->flags, AST_AGGREGATE_CHILD); named->flags = set_flag(named->flags, AST_AGGREGATE_CHILD);

View File

@@ -39,13 +39,19 @@ Bool is_numeric(U8 c){
} }
void entry(){ void entry(){
String string_to_lex = LIT("Identifier 2425525 Not_Number"); String string_to_lex = LIT("Identifier 2425525 Not_Number");
Token token_array[32];
U32 token_count;
Token t; Token t;
for(S64 i = 0;(i<string_to_lex.len);(i+=1)){ for(S64 i = 0;(i<string_to_lex.len);(i+=1)){
if(is_numeric((string_to_lex.str[i]))){ if(is_numeric((string_to_lex.str[i]))){
(t.str=(&(string_to_lex.str[i]))); (t.str=(&(string_to_lex.str[i])));
(t.len=i);
for(;is_numeric((string_to_lex.str[i]));){ for(;is_numeric((string_to_lex.str[i]));){
(i+=1); (i+=1);
} }
(t.len=(i-t.len));
((token_array[(token_count++)])=t);
(token_count+=1);
} }
} }
} }

View File

@@ -3,19 +3,25 @@ Token :: struct
str: *U8 str: *U8
len: S64 len: S64
is_numeric :: (c: U8): Bool is_numeric :: (c: U8): Bool
result := c >= '0 && c <= '9 result := c >= '0 && c <= '9
return result return result
entry :: () entry :: ()
string_to_lex := "Identifier 2425525 Not_Number" string_to_lex := "Identifier 2425525 Not_Number"
token_array: [32]Token
token_count: U32
t: Token t: Token
for i := 0, i < string_to_lex.len, i+=1 for i := 0, i < string_to_lex.len, i+=1
if is_numeric(string_to_lex.str[i]) if is_numeric(string_to_lex.str[i])
t.str = &string_to_lex.str[i] t.str = &string_to_lex.str[i]
t.len = i
for is_numeric(string_to_lex.str[i]) for is_numeric(string_to_lex.str[i])
i+=1 i+=1
t.len = i - t.len
token_array[token_count++] = t
token_count+=1

View File

@@ -216,7 +216,8 @@ digit_count(const Value *a){
} }
function void function void
eval_unary(Token *pos, Token_Kind op, Value *a, bool is_const){ eval_unary(Token *pos, Token_Kind op, Operand *operand){
Value *a = &operand->value;
Ast_Resolved_Type *type = a->type; Ast_Resolved_Type *type = a->type;
if(!is_numeric(type)) if(!is_numeric(type))
parsing_error(pos, "Unary [%s] cant be applied to value of type %s", name(op), docname(type)); parsing_error(pos, "Unary [%s] cant be applied to value of type %s", name(op), docname(type));
@@ -224,7 +225,10 @@ eval_unary(Token *pos, Token_Kind op, Value *a, bool is_const){
if(op == TK_Not) if(op == TK_Not)
a->type = untyped_bool; a->type = untyped_bool;
if(!is_const) if(op == TK_Increment || op == TK_Decrement || op == TK_PostIncrement || op == TK_PostDecrement)
if(!operand->is_lvalue) parsing_error(pos, "Unary [%s] requires an assignable value(lvalue)");
if(!operand->is_const)
return; return;
BigInt result = {}; BigInt result = {};
@@ -738,16 +742,15 @@ resolve_expr(Ast_Expr *ast, Ast_Resolved_Type *expected_type, Sym *lambda_to_res
case TK_Dereference:{ case TK_Dereference:{
return operand_lvalue(type_pointer(value.type)); return operand_lvalue(type_pointer(value.type));
}break; }break;
case TK_Neg:case TK_Not:case TK_Add:case TK_Sub:{ default:{
Operand op = resolve_expr(node->expr); Operand op = resolve_expr(node->expr);
eval_unary(node->pos, node->op, &op.value, op.is_const); eval_unary(node->pos, node->op, &op);
if(op.is_const){ if(op.is_const){
rewrite_into_const(node, Ast_Unary, op.value); rewrite_into_const(node, Ast_Unary, op.value);
return operand_const_rvalue(op.value); return operand_const_rvalue(op.value);
} }
return operand_rvalue(op.value.type); return operand_rvalue(op.value.type);
}break; }break;
invalid_default_case; return {};
} }
BREAK(); BREAK();