Skip to content

Commit

Permalink
New definition syntax with leading DEF keyword
Browse files Browse the repository at this point in the history
This will enable fixing gbdev#457 later once the old
definition syntax is removed.
  • Loading branch information
Rangi42 committed Mar 18, 2021
1 parent ee900ae commit 3b455ba
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 75 deletions.
2 changes: 1 addition & 1 deletion src/asm/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,10 @@ static struct KeywordMapping {
{"RW", T_POP_RW},
/* Handled before as T_Z80_RL */
/* {"RL", T_POP_RL}, */

{"EQU", T_POP_EQU},
{"EQUS", T_POP_EQUS},
{"REDEF", T_POP_REDEF},

/* Handled before as T_Z80_SET */
/* {"SET", T_POP_SET}, */

Expand Down
132 changes: 95 additions & 37 deletions src/asm/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,8 @@ enum {
%token <tzSym> T_ID "identifier"
%token <tzSym> T_LOCAL_ID "local identifier"
%token <tzSym> T_ANON "anonymous label"
%type <tzSym> def_id
%type <tzSym> redef_id
%type <tzSym> scoped_id
%type <tzSym> scoped_anon_id
%token T_POP_EQU "EQU"
Expand Down Expand Up @@ -694,6 +696,22 @@ endc : T_POP_ENDC {
}
;

def_id : T_OP_DEF {
lexer_ToggleStringExpansion(false);
} T_ID {
lexer_ToggleStringExpansion(true);
strcpy($$, $3);
}
;

redef_id : T_POP_REDEF {
lexer_ToggleStringExpansion(false);
} T_ID {
lexer_ToggleStringExpansion(true);
strcpy($$, $3);
}
;

scoped_id : T_ID | T_LOCAL_ID;
scoped_anon_id : scoped_id | T_ANON;

Expand Down Expand Up @@ -775,8 +793,14 @@ directive : include
| fail
| warn
| assert
| def_equ
| def_set
| def_rb
| def_rw
| def_rl
| def_equs
| redef_equs
| purge
| redef
| pops
| pushs
| popo
Expand All @@ -788,6 +812,36 @@ directive : include
trailing_comma : %empty | T_COMMA
;

equ : T_LABEL T_POP_EQU const { sym_AddEqu($1, $3); }
;

set_or_equal : T_POP_SET | T_POP_EQUAL
;

set : T_LABEL set_or_equal const { sym_AddSet($1, $3); }
;

equs : T_LABEL T_POP_EQUS string { sym_AddString($1, $3); }
;

rb : T_LABEL T_POP_RB rs_uconst {
sym_AddEqu($1, sym_GetConstantValue("_RS"));
sym_AddSet("_RS", sym_GetConstantValue("_RS") + $3);
}
;

rw : T_LABEL T_POP_RW rs_uconst {
sym_AddEqu($1, sym_GetConstantValue("_RS"));
sym_AddSet("_RS", sym_GetConstantValue("_RS") + 2 * $3);
}
;

rl : T_LABEL T_Z80_RL rs_uconst {
sym_AddEqu($1, sym_GetConstantValue("_RS"));
sym_AddSet("_RS", sym_GetConstantValue("_RS") + 4 * $3);
}
;

align : T_OP_ALIGN uconst {
if ($2 > 16)
error("Alignment must be between 0 and 16, not %u\n", $2);
Expand Down Expand Up @@ -937,9 +991,6 @@ macrodef : T_POP_MACRO T_ID T_NEWLINE {
}
;

equs : T_LABEL T_POP_EQUS string { sym_AddString($1, $3); }
;

rsset : T_POP_RSSET uconst { sym_AddSet("_RS", $2); }
;

Expand All @@ -952,24 +1003,6 @@ rs_uconst : %empty {
| uconst
;

rl : T_LABEL T_Z80_RL rs_uconst {
sym_AddEqu($1, sym_GetConstantValue("_RS"));
sym_AddSet("_RS", sym_GetConstantValue("_RS") + 4 * $3);
}
;

rw : T_LABEL T_POP_RW rs_uconst {
sym_AddEqu($1, sym_GetConstantValue("_RS"));
sym_AddSet("_RS", sym_GetConstantValue("_RS") + 2 * $3);
}
;

rb : T_LABEL T_POP_RB rs_uconst {
sym_AddEqu($1, sym_GetConstantValue("_RS"));
sym_AddSet("_RS", sym_GetConstantValue("_RS") + $3);
}
;

union : T_POP_UNION { sect_StartUnion(); }
;

Expand Down Expand Up @@ -1012,19 +1045,51 @@ dl : T_POP_DL { out_Skip(4, false); }
| T_POP_DL constlist_32bit trailing_comma
;

purge : T_POP_PURGE {
lexer_ToggleStringExpansion(false);
} purge_list trailing_comma {
lexer_ToggleStringExpansion(true);
def_equ : def_id T_POP_EQU const {
sym_AddEqu($1, $3);
}
;

def_set : def_id set_or_equal const {
sym_AddSet($1, $3);
}
| redef_id set_or_equal const {
sym_AddSet($1, $3);
}
;

def_rb : def_id T_POP_RB rs_uconst {
sym_AddEqu($1, sym_GetConstantValue("_RS"));
sym_AddSet("_RS", sym_GetConstantValue("_RS") + $3);
}
;

def_rw : def_id T_POP_RW rs_uconst {
sym_AddEqu($1, sym_GetConstantValue("_RS"));
sym_AddSet("_RS", sym_GetConstantValue("_RS") + 2 * $3);
}
;

def_rl : def_id T_Z80_RL rs_uconst {
sym_AddEqu($1, sym_GetConstantValue("_RS"));
sym_AddSet("_RS", sym_GetConstantValue("_RS") + 4 * $3);
}
;

def_equs : def_id T_POP_EQUS string {
sym_AddString($1, $3);
}
;

redef_equs : redef_id T_POP_EQUS string {
sym_RedefString($1, $3);
}
;

redef : T_POP_REDEF {
purge : T_POP_PURGE {
lexer_ToggleStringExpansion(false);
} scoped_id {
} purge_list trailing_comma {
lexer_ToggleStringExpansion(true);
} T_POP_EQUS string {
sym_RedefString($3, $6);
}
;

Expand All @@ -1045,13 +1110,6 @@ export_list : export_list_entry
export_list_entry : scoped_id { sym_Export($1); }
;

equ : T_LABEL T_POP_EQU const { sym_AddEqu($1, $3); }
;

set : T_LABEL T_POP_SET const { sym_AddSet($1, $3); }
| T_LABEL T_POP_EQUAL const { sym_AddSet($1, $3); }
;

include : T_POP_INCLUDE string {
fstk_RunInclude($2);
if (oFailedOnMissingInclude)
Expand Down
Loading

0 comments on commit 3b455ba

Please sign in to comment.