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. --- lexer.l | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 lexer.l (limited to 'lexer.l') diff --git a/lexer.l b/lexer.l new file mode 100644 index 0000000..9a34d66 --- /dev/null +++ b/lexer.l @@ -0,0 +1,61 @@ +%{ + +#include "parser.h" + +%} + +%option outfile="lexer.c" header-file="lexer.h" +%option noyywrap never-interactive + +%s inst +%s args + +COMMENT #[^\n]* +SP [ \t] +EOI [ \t\n#] + +%% + +{SP}+ { BEGIN(inst); return T_SPACE; } + +add/{EOI} { BEGIN(args); return T_ADD; } +sub/{EOI} { BEGIN(args); return T_SUB; } +mul/{EOI} { BEGIN(args); return T_MUL; } +div/{EOI} { BEGIN(args); return T_DIV; } +and/{EOI} { BEGIN(args); return T_AND; } +or/{EOI} { BEGIN(args); return T_OR; } +xor/{EOI} { BEGIN(args); return T_XOR; } +lsl/{EOI} { BEGIN(args); return T_LSL; } +lsr/{EOI} { BEGIN(args); return T_LSR; } +asl/{EOI} { BEGIN(args); return T_ASL; } +asr/{EOI} { BEGIN(args); return T_ASR; } +rol/{EOI} { BEGIN(args); return T_ROL; } +ror/{EOI} { BEGIN(args); return T_ROR; } +not/{EOI} { BEGIN(args); return T_NOT; } +neg/{EOI} { BEGIN(args); return T_NEG; } +btc/{EOI} { BEGIN(args); return T_BTC; } +bts/{EOI} { BEGIN(args); return T_BTS; } +mov/{EOI} { BEGIN(args); return T_MOV; } +st\.b/{EOI} { BEGIN(args); return T_ST_B; } +st\.w/{EOI} { BEGIN(args); return T_ST_W; } +ld\.bl/{EOI} { BEGIN(args); return T_LD_BL; } +ld\.bh/{EOI} { BEGIN(args); return T_LD_BH; } +ld\.w/{EOI} { BEGIN(args); return T_LD_W; } +ldi\.bl/{EOI} { BEGIN(args); return T_LDI_BL; } +ldi\.bh/{EOI} { BEGIN(args); return T_LDI_BH; } +beq/{EOI} { BEGIN(args); return T_BEQ; } +bne/{EOI} { BEGIN(args); return T_BNE; } +bgt/{EOI} { BEGIN(args); return T_BGT; } +bge/{EOI} { BEGIN(args); return T_BGE; } +blt/{EOI} { BEGIN(args); return T_BLT; } +ble/{EOI} { BEGIN(args); return T_BLE; } +bra/{EOI} { BEGIN(args); return T_BRA; } +jmp/{EOI} { BEGIN(args); return T_JMP; } +addpc/{EOI} { BEGIN(args); return T_ADDPC; } +nop/{EOI} { BEGIN(args); return T_NOP; } + +{SP}*{COMMENT}?\n { BEGIN(INITIAL); return T_EOL; } +{SP}*{COMMENT} { BEGIN(INITIAL); return T_SPACE; } +{SP}+ { return T_SPACE; } + +%% -- cgit v1.2.3