diff --git a/compiler.c b/compiler.c index 44e6627..4f1080c 100644 --- a/compiler.c +++ b/compiler.c @@ -227,6 +227,7 @@ define_function() while(!check(TOKEN_LBRACE)) { size += variable_declaration(fn); advance(); + if (check(TOKEN_LBRACE)) break; advance(); } @@ -448,7 +449,14 @@ void cast_type() { SymbolType st = parser.current_type; - TokenType cast_type = parser.current.type; + TokenType cast_type; + if (!(parser.current.type >= TOKEN_TYPE_I8 && + parser.current.type <= TOKEN_TYPE_PTR)) { + emitter.error("Expect type name after 'as'.", 26, parser.current.line); + return; + } + + cast_type = parser.current.type; advance(); switch(st) { @@ -553,24 +561,6 @@ print_statement() emitter.emit_end_statement(); } -void -putchar_statement() -{ - expression(); - consume(TOKEN_SEMICOLON); - emitter.emit_end_statement(); - emitter.emit_putchar(); -} - -void -getchar_statement() -{ - expression(); - consume(TOKEN_SEMICOLON); - emitter.emit_end_statement(); - emitter.emit_putchar(); -} - void if_statement() { @@ -668,10 +658,8 @@ statement() while_statement(); } else if(match(TOKEN_KEYWORD_PRINT)) { print_statement(); - } else if(match(TOKEN_KEYWORD_PUTCHAR)) { - putchar_statement(); - } else if(match(TOKEN_KEYWORD_GETCHAR)) { - getchar_statement(); + } else if (match(TOKEN_KEYWORD_HALT)) { + emitter.emit_halt(); } else { expression(); } @@ -767,8 +755,6 @@ ParseRule rules[] = { /* TOKEN_KEYWORD_NIL */ {literal, nil, PREC_NONE}, /* TOKEN_KEYWORD_TRUE */ {literal, nil, PREC_NONE}, /* TOKEN_KEYWORD_FALSE */ {literal, nil, PREC_NONE}, - /* TOKEN_KEYWORD_PUTCHAR */ {nil, nil, PREC_NONE}, - /* TOKEN_KEYWORD_GETCHAR */ {nil, nil, PREC_NONE}, /* TOKEN_OPERATOR_AND */ {nil, binary, PREC_NONE}, /* TOKEN_OPERATOR_OR */ {nil, binary, PREC_NONE}, /* TOKEN_OPERATOR_XOR */ {nil, binary, PREC_NONE}, @@ -801,7 +787,7 @@ ParseRule rules[] = { /* TOKEN_ARROW_RIGHT */ {nil, nil, PREC_NONE}, /* TOKEN_SLL */ {nil, binary, PREC_NONE}, /* TOKEN_SRL */ {nil, binary, PREC_NONE}, - /*TOKEN_KEYWORD_PRINT*/ {nil, binary, PREC_NONE}}; + /* TOKEN_KEYWORD_PRINT*/ {nil, binary, PREC_NONE}}; void parse_precedence(Precedence precedence) diff --git a/emit.h b/emit.h index b3cfb6f..f720ab4 100644 --- a/emit.h +++ b/emit.h @@ -49,8 +49,6 @@ struct emitter_s { VoidArgEmit emit_trait; VoidArgEmit emit_const; VoidArgEmit emit_print; - VoidArgEmit emit_putchar; - VoidArgEmit emit_getchar; VoidArgEmit emit_neg; VoidArgEmit emit_not; VoidArgEmit emit_open_paren; @@ -99,6 +97,7 @@ struct emitter_s { VoidArgEmit emit_while_postfix; I32ArgEmit emit_patch_while; VoidArgEmit emit_early_return; + VoidArgEmit emit_halt; }; Emitter rer_emitter(); diff --git a/emit/rer/emit.c b/emit/rer/emit.c index 582faff..b8bb2b6 100644 --- a/emit/rer/emit.c +++ b/emit/rer/emit.c @@ -585,6 +585,12 @@ rer_emit_patch_while(i32 local_whiles) printf("!&while.%d } \n", local_whiles); } +void +rer_emit_halt() +{ + printf("POP2r BRK "); +} + Emitter rer_emitter() { @@ -624,8 +630,6 @@ rer_emitter() rer_emit_trait, rer_emit_const, rer_emit_print, - rer_emit_putchar, - rer_emit_getchar, rer_emit_neg, rer_emit_not, rer_emit_open_paren, @@ -674,5 +678,6 @@ rer_emitter() rer_emit_while_postfix, rer_emit_patch_while, rer_emit_arena_fn_return, + rer_emit_halt, }; } diff --git a/emit/uxn/emit.c b/emit/uxn/emit.c index 9665144..bcf80d3 100644 --- a/emit/uxn/emit.c +++ b/emit/uxn/emit.c @@ -184,7 +184,8 @@ uxn_mem(u32 a) void uxn_prolog() { - printf("|100\n\tLIT2r 0000 main_ POP2r BRK\n\n"); + /*printf("|100\n\tLIT2r 0000 main_ POP2r BRK\n\n");*/ + printf("|100\n\tLIT2r 0000\n\n"); } void @@ -519,18 +520,6 @@ uxn_emit_const() { } -void -uxn_emit_putchar() -{ - printf("#18 DEO "); -} - -void -uxn_emit_getchar() -{ - printf("#12 DEI "); -} - void uxn_emit_open_paren() { @@ -786,6 +775,12 @@ uxn_emit_early_return() printf("!&return "); } +void +uxn_emit_halt() +{ + printf("POP2r BRK "); +} + Emitter uxn_emitter() { @@ -825,8 +820,6 @@ uxn_emitter() uxn_emit_trait, uxn_emit_const, uxn_emit_print, - uxn_emit_putchar, - uxn_emit_getchar, uxn_emit_neg, uxn_emit_not, uxn_emit_open_paren, @@ -875,5 +868,6 @@ uxn_emitter() uxn_emit_while_postfix, uxn_emit_patch_while, uxn_emit_early_return, + uxn_emit_halt, }; } diff --git a/lexer.c b/lexer.c index 5c2bb87..dbcd6bf 100644 --- a/lexer.c +++ b/lexer.c @@ -231,8 +231,6 @@ identifierType() return check_keyword(2, 1, "r", TOKEN_TYPE_PTR); case 'l': return check_keyword(2, 2, "ex", TOKEN_KEYWORD_PLEX); - case 'u': - return check_keyword(2, 5, "tchar", TOKEN_KEYWORD_PUTCHAR); case 'r': return check_keyword(2, 3, "int", TOKEN_KEYWORD_PRINT); } @@ -327,8 +325,6 @@ identifierType() } } break; - case 'g': - return check_keyword(1, 6, "etchar", TOKEN_KEYWORD_GETCHAR); case 'l': return check_keyword(1, 3, "oop", TOKEN_KEYWORD_LOOP); case 'd': @@ -339,6 +335,8 @@ identifierType() return check_keyword(1, 2, "or", TOKEN_OPERATOR_XOR); case 'm': return check_keyword(1, 2, "od", TOKEN_OPERATOR_MOD); + case 'h': + return check_keyword(1, 3, "alt", TOKEN_KEYWORD_HALT); } return TOKEN_IDENTIFIER; @@ -519,8 +517,6 @@ token_type_to_string(TokenType type) return "KEYWORD_TRUE"; case TOKEN_KEYWORD_FALSE: return "KEYWORD_FALSE"; - case TOKEN_KEYWORD_GETCHAR: - return "KEYWORD_GETCHAR"; case TOKEN_OPERATOR_AND: return "OPERATOR_AND"; case TOKEN_OPERATOR_OR: diff --git a/lexer.h b/lexer.h index d0f7769..5b21144 100644 --- a/lexer.h +++ b/lexer.h @@ -52,8 +52,6 @@ typedef enum { TOKEN_KEYWORD_NIL, TOKEN_KEYWORD_TRUE, TOKEN_KEYWORD_FALSE, - TOKEN_KEYWORD_PUTCHAR, - TOKEN_KEYWORD_GETCHAR, TOKEN_OPERATOR_AND, TOKEN_OPERATOR_OR, TOKEN_OPERATOR_XOR, @@ -87,7 +85,8 @@ typedef enum { TOKEN_SLL, TOKEN_SRL, TOKEN_KEYWORD_PRINT, - TOKEN_KEYWORD_TRAIT + TOKEN_KEYWORD_TRAIT, + TOKEN_KEYWORD_HALT } TokenType; typedef struct token_s Token; diff --git a/test/attack.c b/test/attack.c index 09c9ede..a019dc0 100755 --- a/test/attack.c +++ b/test/attack.c @@ -1,11 +1,14 @@ #include char* msg = " damage inflicted!\n"; +unsigned AT = 14; +unsigned accuracy = 150; + +unsigned calc_damage(unsigned at, unsigned accuracy) { + return at * accuracy / 20 - 3; +} void main() { - unsigned AT = 14; - unsigned accuracy = 150; - - unsigned dmg = ((AT * accuracy) / 20) - 3; + unsigned dmg = calc_damage(AT, accuracy); printf("%d%s", dmg, msg); } \ No newline at end of file diff --git a/test/attack.tal b/test/attack.tal index 76b0a13..87b0088 100644 --- a/test/attack.tal +++ b/test/attack.tal @@ -1,11 +1,19 @@ |100 + LIT2r 0000 + !{ @msg $2 } ;{ #0002 ADD2 } !{ 20 64 61 6d 61 67 65 20 69 6e 66 6c 69 63 74 65 64 21 5c 6e 00 } ;msg STA2 !{ @AT $2 } #000e ;AT STA2 !{ @accuracy $2 } #0096 ;accuracy STA2 -!{ @dmg $2 } ;AT LDA2 ;accuracy LDA2 MUL2 #0014 DIV2 #0003 SUB2 ;dmg STA2 -;dmg LDA2 nat_to_str_ -str/ ;msg LDA2 -str/ +!{ @dmg $2 } ;AT LDA2 ;accuracy LDA2 calc_damage_ ;dmg STA2 +;dmg LDA2 nat_to_str_ str/ +;msg LDA2 str/ +POP2r BRK @calc_damage_ ( at accuracy -- res ) + OVR2r LIT2r 0004 SUB2r + STH2kr STA2 + STH2kr #0002 ADD2 STA2 + + STH2kr LDA2 STH2kr #0002 ADD2 LDA2 MUL2 #0014 DIV2 #0003 SUB2 !&return &return + POP2r JMP2r @sext #80 ANDk EQU #ff MUL SWP JMP2r diff --git a/test/attack.ul b/test/attack.ul index 8a80fc2..0c18320 100755 --- a/test/attack.ul +++ b/test/attack.ul @@ -2,9 +2,12 @@ str msg = " damage inflicted!\n" nat AT = 14; nat accuracy = 150; -function main() { - nat dmg = ((AT * accuracy) / 20) - 3; +nat dmg = calc_damage(AT, accuracy); - print(dmg as str); - print(msg); +print(dmg as str); +print(msg); +halt; + +function calc_damage(nat at, nat accuracy) nat { + return at * accuracy / 20 - 3; }