From a1486b4f97e8cf8d457bec936879d70b3b92587f Mon Sep 17 00:00:00 2001 From: Bobby Bingham Date: Sat, 14 Jan 2017 15:55:57 -0600 Subject: nqasm: Handle immediate arguments --- lexer.l | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'lexer.l') diff --git a/lexer.l b/lexer.l index f3a540c..85021c5 100644 --- a/lexer.l +++ b/lexer.l @@ -1,9 +1,19 @@ %{ +#include +#include + #include "parser.h" #define SETVALUE(v) yylval.arg = (struct argument) { .value = (v) } +static int intlit(const char *s, int base, long *out) +{ + errno = 0; + *out = strtol(s, NULL, base); + return errno == ERANGE ? T_BADINT : T_INT; +} + %} %option outfile="lexer.c" header-file="lexer.h" @@ -16,6 +26,8 @@ COMMENT #[^\n]* SP [ \t] EOI [ \t\n#] REGNUM [0-7] +DEC [0-9] +HEX [0-9a-f] %% @@ -59,6 +71,8 @@ REGNUM [0-7] r{REGNUM} { SETVALUE(yytext[1]-'0'); return T_REG; } @r{REGNUM} { SETVALUE(yytext[2]-'0'); return T_REGPTR; } +[-+]?{DEC}+ { return intlit(yytext, 10, &yylval.lval); } +0x{HEX}+ { return intlit(yytext+2, 16, &yylval.lval); } {SP}*,{SP}* { return T_COMMA; } {SP}*{COMMENT}?\n { BEGIN(INITIAL); return T_EOL; } -- cgit v1.2.3