This commit is contained in:
anon 2024-07-05 04:47:42 +02:00
parent 016ef7f019
commit aa97643d2c
3 changed files with 66 additions and 15 deletions

View File

@ -16,7 +16,9 @@ procedure highlight
s32 byte
begin
loop
if /* byte = 0x90 */ then
if
/* byte = 0x90 */
then
fastcall echo_new_line
fastcall terminal_style EFFECT_NORMAL COLOUR_YELLOW
fastcall echo_byte [buffer + offset]
@ -25,11 +27,13 @@ begin
fastcall echo_byte [buffer + offset]
end if
inc offset
if /* offset = size */ then
if
/* offset = size */
then
break
end if
end loop
end procedure;
end procedure
unix
program xop
@ -39,7 +43,9 @@ program xop
u8 buffer = NULL;
begin
if /* argc != 2 */ then
if
/* argc != 2 */
then
fatal_failure(1, "xop: xop input")
end if
@ -64,4 +70,4 @@ begin
mov buffer rax
exit SUCCESS
end program;
end program

View File

@ -3,7 +3,7 @@
%}
%option noyywrap
identifier [A-z_][A-z0-9_]*
identifier [A-Za-z_][A-z0-9_]*
wsnl [ \t\r\v\f\n]
hex [0123456789abcdef]
@ -13,6 +13,7 @@ uhex [0123456789ABCDEF]
// XXX: ?!
%x IN_DECLARE
%x IN_UGH
%option nodefault
%option yylineno
@ -23,6 +24,7 @@ uhex [0123456789ABCDEF]
begin{wsnl}+ { return MYBEGIN; }
/* XXX 'end' should be its own state, with proper error handling */
program{wsnl}+ { return PROGRAM; }
end{wsnl}+program{wsnl}+ { return END_PROGRAM; }
procedure{wsnl}+ { return PROCEDURE; }
@ -32,6 +34,8 @@ end{wsnl}+loop{wsnl}+ { return END_LOOP; }
if{wsnl}+ { return IF; }
then{wsnl}+ { return THEN; }
end{wsnl}+if{wsnl}+ { return END_IF; }
break{wsnl}+ { return BREAK; }
fast{wsnl}+ { return FAST; }
unix{wsnl}+ { return UNIX; }
@ -55,6 +59,11 @@ u16{wsnl}+ { return U16; }
u32{wsnl}+ { return U32; }
u64{wsnl}+ { return U64; }
-?[[:digit:]]+ {
sscanf(yytext, "%d", &yylval.intval);
return LITERAL;
}
0b[01]+ {
return LITERAL;
}
@ -70,6 +79,9 @@ u64{wsnl}+ { return U64; }
}
xor{wsnl}+ { return TXOR; }
inc{wsnl}+ { return TINC; }
fastcall{wsnl}+ { return FASTCALL; }
\/\/ { BEGIN IN_COMMENT; }
\/\* { BEGIN IN_MULTILINE_COMMENT; }
@ -84,6 +96,14 @@ xor{wsnl}+ { return TXOR; }
.|\n { ; }
}
\[ { BEGIN IN_UGH; }
<IN_UGH>{
\] { BEGIN INITIAL; yylval.strval = strdup(yytext); return IDENTIFIER; }
/* XXX! */
.|\n { ; }
}
{identifier}{wsnl}+ { yylval.strval = strdup(yytext); return IDENTIFIER; }
%%

View File

@ -24,7 +24,8 @@
#include "assembler.h"
void yyerror() {
printf("\033[31mError: syntax error at line %d.\033[0m\n", yylineno);
printf("\033[31mError: syntax error at line %d near '%s'.\033[0m\n", yylineno, yytext);
yyfree_leftovers();
}
extern void set_state(int state);
@ -52,6 +53,7 @@
%token PROCEDURE END_PROCEDURE
%token TLOOP END_LOOP
%token IF THEN END_IF
%token BREAK
%token<strval> IDENTIFIER
@ -74,7 +76,9 @@
%token RBP RSP RIP
// Instructions
%token TADD TOR TADC TBB TXOR TAND TSUB TCMP TSYSCALL
%token TADD TOR TADC TBB TXOR TAND TSUB TCMP TSYSCALL TINC
// Instruction-likes
%token FASTCALL
%%
hla: %empty
@ -82,14 +86,18 @@ hla: %empty
| function hla
;
program: program_specifier PROGRAM IDENTIFIER declaration_section MYBEGIN code END_PROGRAM
program: program_specifier PROGRAM IDENTIFIER declaration_section MYBEGIN code END_PROGRAM {
free($3);
}
;
program_specifier: %empty
| UNIX
;
function: function_specifier PROCEDURE IDENTIFIER declaration_section MYBEGIN code END_PROCEDURE
function: function_specifier PROCEDURE IDENTIFIER declaration_section MYBEGIN code END_PROCEDURE {
free($3);
}
;
function_specifier: %empty
@ -100,7 +108,7 @@ declaration_section: %empty
| declaration declaration_section
;
declaration: origin type IDENTIFIER { $2.name = $3; /* add_var($1); */ }
declaration: origin type IDENTIFIER { $2.name = $3; /* add_var($1); */ free($3); }
;
origin: %empty
@ -118,10 +126,15 @@ type: S8 { $$ = (static_variable){ .is_signed = 1, .size = 8, .address = new
;
code: %empty
| loop
| if
| TXOR register register { /* assemble_xor(size_64b, type_register_register, $2, $3); */ }
| TXOR register immediate { /* assemble_xor(size_64b, type_register_register, $2, $3); */ }
| loop code
| if code
| call code
| BREAK code
| TXOR register register code { /* assemble_xor(size_64b, type_register_register, $2, $3); */ }
| TXOR register immediate code { /* assemble_xor(size_64b, type_register_register, $2, $3); */ }
| TXOR IDENTIFIER register code { /* assemble_xor(size_64b, type_register_register, $2, $3); */ }
| TINC register code
| TINC IDENTIFIER code
;
loop: TLOOP code END_LOOP
@ -133,6 +146,18 @@ if: IF logic THEN code END_IF
logic:
;
call: calltype IDENTIFIER arguments { free($2); }
;
calltype: FASTCALL
;
arguments: %empty
| IDENTIFIER arguments { free($1); }
| register arguments
| immediate arguments
;
register: RAX { $$ = R0; }
| RBX { $$ = R1; }
;