From cf366387449dcf9d34875a7d76f3b79c1e231d6f Mon Sep 17 00:00:00 2001 From: Bobby Bingham Date: Thu, 12 Jan 2017 20:05:45 -0600 Subject: nqasm: Initial stab at a lexer and parser So far, this only understands instructions, but no operands or labels. It doesn't handle bad input gracefully. It's just enough to test the basics. --- parser.y | 156 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 parser.y (limited to 'parser.y') diff --git a/parser.y b/parser.y new file mode 100644 index 0000000..78bf123 --- /dev/null +++ b/parser.y @@ -0,0 +1,156 @@ +%{ + +#include + +#include "lexer.h" +#include "mnemonics.h" + +void yyerror(const char *msg) +{ + fprintf(stderr, "%s\n", msg); +} + +#define INSTRUCTION(mnem) (struct instruction) { MNEM_ ## mnem } + +%} + +%output "parser.c" +%defines "parser.h" + +%code requires { + #include "nqasm.h" +} + +%union { + struct instruction inst; +} + +%token END 0 "end of file" + +%token T_SPACE +%token T_EOL + +%token T_ADD +%token T_SUB +%token T_MUL +%token T_DIV +%token T_AND +%token T_OR +%token T_XOR +%token T_LSL +%token T_LSR +%token T_ASL +%token T_ASR +%token T_ROL +%token T_ROR +%token T_NOT +%token T_NEG +%token T_BTC +%token T_BTS +%token T_MOV +%token T_ST_B +%token T_ST_W +%token T_LD_BL +%token T_LD_BH +%token T_LD_W +%token T_LDI_BL +%token T_LDI_BH +%token T_BEQ +%token T_BNE +%token T_BGT +%token T_BGE +%token T_BLT +%token T_BLE +%token T_BRA +%token T_JMP +%token T_ADDPC +%token T_NOP + +%type inst +%% + +input: %empty + | input line + ; + +line: T_SPACE inst eol + { add_instruction(&$2); } + | eol + ; + +inst: T_ADD + { $$ = INSTRUCTION(ADD); } + | T_SUB + { $$ = INSTRUCTION(SUB); } + | T_MUL + { $$ = INSTRUCTION(MUL); } + | T_DIV + { $$ = INSTRUCTION(DIV); } + | T_AND + { $$ = INSTRUCTION(AND); } + | T_OR + { $$ = INSTRUCTION(OR); } + | T_XOR + { $$ = INSTRUCTION(XOR); } + | T_LSL + { $$ = INSTRUCTION(LSL); } + | T_LSR + { $$ = INSTRUCTION(LSR); } + | T_ASL + { $$ = INSTRUCTION(ASL); } + | T_ASR + { $$ = INSTRUCTION(ASR); } + | T_ROL + { $$ = INSTRUCTION(ROL); } + | T_ROR + { $$ = INSTRUCTION(ROR); } + | T_NOT + { $$ = INSTRUCTION(NOT); } + | T_NEG + { $$ = INSTRUCTION(NEG); } + | T_BTC + { $$ = INSTRUCTION(BTC); } + | T_BTS + { $$ = INSTRUCTION(BTS); } + | T_MOV + { $$ = INSTRUCTION(MOV); } + | T_ST_B + { $$ = INSTRUCTION(ST_B); } + | T_ST_W + { $$ = INSTRUCTION(ST_W); } + | T_LD_BL + { $$ = INSTRUCTION(LD_BL); } + | T_LD_BH + { $$ = INSTRUCTION(LD_BH); } + | T_LD_W + { $$ = INSTRUCTION(LD_W); } + | T_LDI_BL + { $$ = INSTRUCTION(LDI_BL); } + | T_LDI_BH + { $$ = INSTRUCTION(LDI_BH); } + | T_BEQ + { $$ = INSTRUCTION(BEQ); } + | T_BNE + { $$ = INSTRUCTION(BNE); } + | T_BGT + { $$ = INSTRUCTION(BGT); } + | T_BGE + { $$ = INSTRUCTION(BGE); } + | T_BLT + { $$ = INSTRUCTION(BLT); } + | T_BLE + { $$ = INSTRUCTION(BLE); } + | T_BRA + { $$ = INSTRUCTION(BRA); } + | T_JMP + { $$ = INSTRUCTION(JMP); } + | T_ADDPC + { $$ = INSTRUCTION(ADDPC); } + | T_NOP + { $$ = INSTRUCTION(NOP); } + ; + +eol: T_EOL + | T_SPACE END + | END + ; -- cgit v1.2.3