+ artimetric blocks

This commit is contained in:
anon 2024-07-05 14:41:56 +02:00
parent fd3a86df7c
commit 7cae7f968f
4 changed files with 71 additions and 58 deletions

View File

@ -18,6 +18,7 @@ endif
CFLAGS += -Wall -Wextra -Wpedantic CFLAGS += -Wall -Wextra -Wpedantic
CPPFLAGS += -Ilibrary/tommyds CPPFLAGS += -Ilibrary/tommyds
LDFLAGS += -lm
OUT := eaxhla OUT := eaxhla

6
debug/artimetrics.eax Normal file
View File

@ -0,0 +1,6 @@
procedure test
u8 a = [8 + 8]
u8 b = [2 ^ 8]
u8 c = [2 ^ 8 + [2 * 3]]
begin
end procedure

View File

@ -24,10 +24,7 @@ uhex [0123456789ABCDEF]
%x IN_COMMENT IN_MULTILINE_COMMENT %x IN_COMMENT IN_MULTILINE_COMMENT
%x IN_STRING %x IN_STRING
%x IN_END %x IN_END
%x IN_ARTIMETRIC_BLOCK
// XXX: ?!
%x IN_DECLARE
%x IN_UGH
%option nodefault %option nodefault
%option yylineno %option yylineno
@ -50,16 +47,6 @@ exit{wsnl}+ { return EXIT; }
end { BEGIN IN_END; } end { BEGIN IN_END; }
<IN_END>{
program { BEGIN INITIAL; return END_PROGRAM; }
procedure { BEGIN INITIAL; return END_PROCEDURE; }
loop { BEGIN INITIAL; return END_LOOP; }
if { BEGIN INITIAL; return END_IF; }
machine { BEGIN INITIAL; return END_MACHINE; }
{wsnl} { ; }
. { yyerror(); }
}
fast{wsnl}+ { return FAST; } fast{wsnl}+ { return FAST; }
unix{wsnl}+ { return UNIX; } unix{wsnl}+ { return UNIX; }
@ -103,28 +90,30 @@ u16{wsnl}+ { return U16; }
u32{wsnl}+ { return U32; } u32{wsnl}+ { return U32; }
u64{wsnl}+ { return U64; } u64{wsnl}+ { return U64; }
-?[[:digit:]]+ {
yylval.intval = strtol(yytext, NULL, 10);
return LITERAL;
}
0b[01]+ {
yylval.intval = strtol(yytext, NULL, 2);
return LITERAL;
}
0x{hex}+ {
yylval.intval = strtol(yytext + 2, NULL, 16);
return LITERAL;
}
0x{uhex}+ {
yylval.intval = strtol(yytext + 2, NULL, 16);
return LITERAL;
}
\" { BEGIN IN_STRING; } \" { BEGIN IN_STRING; }
xor{wsnl}+ { return TXOR; }
inc{wsnl}+ { return TINC; }
fastcall{wsnl}+ { return FASTCALL; }
\/\/ { BEGIN IN_COMMENT; }
\/\* { BEGIN IN_MULTILINE_COMMENT; }
\[ { return '['; }
\] { return ']'; }
\+|\-|\*|\/|\%|\^ { return yytext[0]; }
<IN_END>{
program { BEGIN INITIAL; return END_PROGRAM; }
procedure { BEGIN INITIAL; return END_PROCEDURE; }
loop { BEGIN INITIAL; return END_LOOP; }
if { BEGIN INITIAL; return END_IF; }
machine { BEGIN INITIAL; return END_MACHINE; }
{wsnl} { ; }
. { yyerror(); }
}
<IN_STRING>{ <IN_STRING>{
/* XXX: escapes */ /* XXX: escapes */
\" { \" {
@ -144,14 +133,6 @@ u64{wsnl}+ { return U64; }
} }
} }
xor{wsnl}+ { return TXOR; }
inc{wsnl}+ { return TINC; }
fastcall{wsnl}+ { return FASTCALL; }
\/\/ { BEGIN IN_COMMENT; }
\/\* { BEGIN IN_MULTILINE_COMMENT; }
<IN_COMMENT>{ <IN_COMMENT>{
\n { BEGIN INITIAL; } \n { BEGIN INITIAL; }
.* { ; } .* { ; }
@ -162,12 +143,26 @@ fastcall{wsnl}+ { return FASTCALL; }
.|\n { ; } .|\n { ; }
} }
\[ { BEGIN IN_UGH; } <INITIAL,IN_ARTIMETRIC_BLOCK>{
-?[[:digit:]]+ {
yylval.intval = strtol(yytext, NULL, 10);
return LITERAL;
}
<IN_UGH>{ 0b[01]+ {
\] { BEGIN INITIAL; yylval.strval = strdup(yytext); return IDENTIFIER; } yylval.intval = strtol(yytext, NULL, 2);
/* XXX! */ return LITERAL;
.|\n { ; } }
0x{hex}+ {
yylval.intval = strtol(yytext + 2, NULL, 16);
return LITERAL;
}
0x{uhex}+ {
yylval.intval = strtol(yytext + 2, NULL, 16);
return LITERAL;
}
} }
{identifier} { yylval.strval = strdup(yytext); return IDENTIFIER; } {identifier} { yylval.strval = strdup(yytext); return IDENTIFIER; }
@ -176,14 +171,6 @@ fastcall{wsnl}+ { return FASTCALL; }
%% %%
void set_state(int state) {
switch (state) {
case STATE_DECLARE: {
BEGIN IN_DECLARE;
} break;
}
}
void yyfree_leftovers(void) { void yyfree_leftovers(void) {
if (yyin) { if (yyin) {
fclose(yyin); fclose(yyin);

View File

@ -20,6 +20,7 @@
%{ %{
#include <stdio.h> #include <stdio.h>
#include <math.h>
#include "eaxhla.yy.h" #include "eaxhla.yy.h"
#include "assembler.h" #include "assembler.h"
@ -31,8 +32,6 @@
yyfree_leftovers(); yyfree_leftovers();
} }
extern void set_state(int state);
long new_static(int size) { long new_static(int size) {
(void)size; (void)size;
return 0; return 0;
@ -63,6 +62,8 @@
%token<strval> IDENTIFIER %token<strval> IDENTIFIER
%type<intval> immediate
%type<intval> artimetric_block artimetric_expression artimetric_operand
%token<intval> LITERAL %token<intval> LITERAL
%token<strval> STRING_LITERAL %token<strval> STRING_LITERAL
@ -123,7 +124,7 @@ declaration_section: %empty
; ;
declaration: origin type IDENTIFIER { $2.name = $3; /* add_var($1); */ free($3); } declaration: origin type IDENTIFIER { $2.name = $3; /* add_var($1); */ free($3); }
| origin type IDENTIFIER '=' LITERAL { $2.name = $3; /* add_var($1); */ free($3); } | origin type IDENTIFIER '=' immediate { $2.name = $3; /* add_var($1); */ free($3); }
| origin type IDENTIFIER '=' STRING_LITERAL { $2.name = $3; /* add_var($1); */ free($3); free($5); } | origin type IDENTIFIER '=' STRING_LITERAL { $2.name = $3; /* add_var($1); */ free($3); free($5); }
; ;
@ -231,6 +232,24 @@ register: RAX { $$ = R0; }
; ;
immediate: LITERAL immediate: LITERAL
| artimetric_block
;
artimetric_block: '[' artimetric_expression ']' { $$ = $2; }
;
artimetric_expression: %empty { yyerror(); }
| artimetric_operand
| artimetric_expression '+' artimetric_operand { $$ = $1 + $3; }
| artimetric_expression '-' artimetric_operand { $$ = $1 - $3; }
| artimetric_expression '*' artimetric_operand { $$ = $1 * $3; }
| artimetric_expression '/' artimetric_operand { $$ = $1 / $3; }
| artimetric_expression '%' artimetric_operand { $$ = $1 % $3; }
| artimetric_expression '^' artimetric_operand { $$ = pow($1, $3); }
;
artimetric_operand: immediate
| IDENTIFIER { $$ = 0; /*XXX*/ }
; ;
exit: EXIT immediate exit: EXIT immediate