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

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

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

@ -24,7 +24,8 @@
#include "assembler.h" #include "assembler.h"
void yyerror() { 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); extern void set_state(int state);
@ -52,6 +53,7 @@
%token PROCEDURE END_PROCEDURE %token PROCEDURE END_PROCEDURE
%token TLOOP END_LOOP %token TLOOP END_LOOP
%token IF THEN END_IF %token IF THEN END_IF
%token BREAK
%token<strval> IDENTIFIER %token<strval> IDENTIFIER
@ -74,7 +76,9 @@
%token RBP RSP RIP %token RBP RSP RIP
// Instructions // 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 hla: %empty
@ -82,14 +86,18 @@ hla: %empty
| function hla | 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 program_specifier: %empty
| UNIX | 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 function_specifier: %empty
@ -100,7 +108,7 @@ declaration_section: %empty
| declaration declaration_section | 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 origin: %empty
@ -118,10 +126,15 @@ type: S8 { $$ = (static_variable){ .is_signed = 1, .size = 8, .address = new
; ;
code: %empty code: %empty
| loop | loop code
| if | if code
| TXOR register register { /* assemble_xor(size_64b, type_register_register, $2, $3); */ } | call code
| TXOR register immediate { /* assemble_xor(size_64b, type_register_register, $2, $3); */ } | 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 loop: TLOOP code END_LOOP
@ -133,6 +146,18 @@ if: IF logic THEN code END_IF
logic: logic:
; ;
call: calltype IDENTIFIER arguments { free($2); }
;
calltype: FASTCALL
;
arguments: %empty
| IDENTIFIER arguments { free($1); }
| register arguments
| immediate arguments
;
register: RAX { $$ = R0; } register: RAX { $$ = R0; }
| RBX { $$ = R1; } | RBX { $$ = R1; }
; ;