#include "asm.h" #include "util.h" #include "tpu.h" #include #include #include #include #define TEXTALPH "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_" #define NUMALPH "0123456789" #define NAMEALPH TEXTALPH NUMALPH #define WHITESPACE " \t\v\r\n," enum asm_tok { /* Global */ TOK_STDIN, TOK_STDOUT, TOK_TPU, TOK_END, /* Operands (order like OP_*) */ TOK_ACC, TOK_NIL, TOK_LEFT, TOK_RIGHT, TOK_UP, TOK_DOWN, TOK_ANY, TOK_LAST, TOK_LIT, TOK_NAME, /* Instructions (order like INST_*) */ TOK_NOP, TOK_MOV, TOK_SWP, TOK_SAV, TOK_ADD, TOK_SUB, TOK_NEG, TOK_JMP, TOK_JEZ, TOK_JNZ, TOK_JGZ, TOK_JLZ, TOK_JRO, /* Misc */ TOK_COMMENT, TOK_LABEL, TOK_XPOS, TOK_YPOS, TOK_NL, TOK_EOF }; struct asm_tokenizer { const char *filepath; FILE *file; enum asm_tok tok; char *tokstr; size_t lineno, off; char linebuf[256]; }; static const char *tok_strs[] = { /* Global */ "stdin", "stdout", "tpu", "end", /* Operands (order like OP_*) */ "acc", "nil", "left", "right", "up", "down", "any", "last", NULL, NULL, /* Instructions (order like INST_*) */ "nop", "mov", "swp", "sav", "add", "sub", "neg", "jmp", "jez", "jnz", "jgz", "jlz", "jro", /* Misc */ NULL, NULL, NULL, NULL, NULL, NULL }; static const char *tok_reprs[] = { /* Global */ "'STDIN'", "'STDOUT'", "'TPU'", "'END'", /* Operands (order like OP_*) */ "'ACC'", "'NIL'", "'LEFT'", "'RIGHT'", "'UP'", "'DOWN'", "'ANY'", "'LAST'", "", "", /* Instructions (order like INST_*) */ "'NOP'", "'MOV'", "'SWP'", "'SAV'", "'ADD'", "'SUB'", "'NEG'", "'JMP'", "'JEZ'", "'JNZ'", "'JGZ'", "'JLZ'", "'JRO'", /* Misc */ "#", "