fix casting, remove get/put char, add halt, move to "mainless" style?
This commit is contained in:
parent
251101ecb8
commit
c5ce5179f0
36
compiler.c
36
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},
|
||||
|
|
|
|||
3
emit.h
3
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();
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
};
|
||||
}
|
||||
|
|
|
|||
8
lexer.c
8
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:
|
||||
|
|
|
|||
5
lexer.h
5
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;
|
||||
|
|
|
|||
|
|
@ -1,11 +1,14 @@
|
|||
#include <stdio.h>
|
||||
char* msg = " damage inflicted!\n";
|
||||
|
||||
void main() {
|
||||
unsigned AT = 14;
|
||||
unsigned accuracy = 150;
|
||||
|
||||
unsigned dmg = ((AT * accuracy) / 20) - 3;
|
||||
unsigned calc_damage(unsigned at, unsigned accuracy) {
|
||||
return at * accuracy / 20 - 3;
|
||||
}
|
||||
|
||||
void main() {
|
||||
unsigned dmg = calc_damage(AT, accuracy);
|
||||
|
||||
printf("%d%s", dmg, msg);
|
||||
}
|
||||
|
|
@ -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/<print> ;msg LDA2
|
||||
str/<print>
|
||||
!{ @dmg $2 } ;AT LDA2 ;accuracy LDA2 calc_damage_ ;dmg STA2
|
||||
;dmg LDA2 nat_to_str_ str/<print>
|
||||
;msg LDA2 str/<print>
|
||||
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
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
halt;
|
||||
|
||||
function calc_damage(nat at, nat accuracy) nat {
|
||||
return at * accuracy / 20 - 3;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue