Skip to content

Commit

Permalink
Allow keywords to be used as instruction names
Browse files Browse the repository at this point in the history
  • Loading branch information
positively-charged committed May 24, 2017
1 parent 973aee8 commit 4ba2f5d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 14 deletions.
28 changes: 18 additions & 10 deletions doc/grammar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ statement:
<palette-translation>
<assert>
<buildmsg>
<inline-assembly>
<empty-statement>
<expression-statement>

Expand Down Expand Up @@ -589,19 +590,26 @@ buildmsg-statement:
buildmsg:
buildmsg ( <expression> ) <block-statement>

inline-asm:
> <id> <nl>
> <id> <inline-asm-args> <nl>
inline-assembly:
> <inline-assembly-opcode> <nl>
> <inline-assembly-opcode> <inline-assembly-argument-list> <nl>

inline-asm-args:
inline-assembly-opcode:
<identifier>
terminate
restart
suspend
goto

inline-assembly-argument-list:
<inline-assembly-argument>
<inline-assembly-argument-list> , <inline-assembly-argument>

inline-assembly-argument:
<number>
<id>
<identifier>
<string>
( <expr )
<inline-asm-args> , <number>
<inline-asm-args> , <id>
<inline-asm-args> , <string>
<inline-asm-args> , ( <expr> )
( <expression> )

empty-statement:
;
Expand Down
24 changes: 20 additions & 4 deletions src/parse/asm.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "codegen/pcode.h"

static struct inline_asm* alloc_inline_asm( void );
static void read_opcode( struct parse* parse, struct inline_asm* inline_asm );
static void read_arg( struct parse* parse, struct inline_asm* inline_asm );
static struct inline_asm_arg* alloc_inline_asm_arg( struct pos* pos );

Expand All @@ -13,10 +14,7 @@ void p_read_asm( struct parse* parse, struct stmt_reading* reading ) {
struct inline_asm* inline_asm = alloc_inline_asm();
p_test_tk( parse, TK_GT );
p_read_tk( parse );
p_test_tk( parse, TK_ID );
inline_asm->name = parse->tk_text;
inline_asm->pos = parse->tk_pos;
p_read_tk( parse );
read_opcode( parse, inline_asm );
if ( parse->tk != TK_NL ) {
while ( true ) {
read_arg( parse, inline_asm );
Expand Down Expand Up @@ -45,6 +43,24 @@ static struct inline_asm* alloc_inline_asm( void ) {
return inline_asm;
}

static void read_opcode( struct parse* parse, struct inline_asm* inline_asm ) {
switch ( parse->tk ) {
case TK_ID:
case TK_TERMINATE:
case TK_RESTART:
case TK_SUSPEND:
case TK_GOTO:
inline_asm->name = parse->tk_text;
inline_asm->pos = parse->tk_pos;
p_read_tk( parse );
break;
default:
p_unexpect_diag( parse );
p_unexpect_last_name( parse, NULL, "instruction name" );
p_bail( parse );
}
}

static void read_arg( struct parse* parse, struct inline_asm* inline_asm ) {
struct inline_asm_arg* arg = alloc_inline_asm_arg( &parse->tk_pos );
list_append( &inline_asm->args, arg );
Expand Down
4 changes: 4 additions & 0 deletions src/semantic/asm.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ static const struct mnemonic* find_mnemonic( const char* name ) {
{ "giveactorinventory", PCD_GIVEACTORINVENTORY },
{ "giveinventory", PCD_GIVEINVENTORY },
{ "giveinventorydirect", PCD_GIVEINVENTORYDIRECT },
{ "goto", PCD_GOTO },
{ "goto_", PCD_GOTO },
{ "gotostack", PCD_GOTOSTACK },
{ "gt", PCD_GT },
Expand Down Expand Up @@ -327,6 +328,7 @@ static const struct mnemonic* find_mnemonic( const char* name ) {
{ "redteamcount", PCD_REDTEAMCOUNT },
{ "redteamscore", PCD_REDTEAMSCORE },
{ "replacetextures", PCD_REPLACETEXTURES },
{ "restart", PCD_RESTART },
{ "restart_", PCD_RESTART },
{ "returnval", PCD_RETURNVAL },
{ "returnvoid", PCD_RETURNVOID },
Expand Down Expand Up @@ -397,6 +399,7 @@ static const struct mnemonic* find_mnemonic( const char* name ) {
{ "subtract", PCD_SUBTRACT },
{ "subworldarray", PCD_SUBWORLDARRAY },
{ "subworldvar", PCD_SUBWORLDVAR },
{ "suspend", PCD_SUSPEND },
{ "suspend_", PCD_SUSPEND },
{ "swap", PCD_SWAP },
{ "tagstring", PCD_TAGSTRING },
Expand All @@ -405,6 +408,7 @@ static const struct mnemonic* find_mnemonic( const char* name ) {
{ "takeactorinventory", PCD_TAKEACTORINVENTORY },
{ "takeinventory", PCD_TAKEINVENTORY },
{ "takeinventorydirect", PCD_TAKEINVENTORYDIRECT },
{ "terminate", PCD_TERMINATE },
{ "terminate_", PCD_TERMINATE },
{ "thingcount", PCD_THINGCOUNT },
{ "thingcountdirect", PCD_THINGCOUNTDIRECT },
Expand Down

0 comments on commit 4ba2f5d

Please sign in to comment.