summaryrefslogtreecommitdiff
path: root/lexer.l
diff options
context:
space:
mode:
authorBobby Bingham <koorogi@koorogi.info>2017-01-14 15:55:57 -0600
committerBobby Bingham <koorogi@koorogi.info>2017-01-15 23:19:11 -0600
commita1486b4f97e8cf8d457bec936879d70b3b92587f (patch)
tree83e2d1fcd2c848b9836ceac63b72b36817c31f94 /lexer.l
parent50e67fb3e640a9e6e1260ca984582c9a9b8d5375 (diff)
nqasm: Handle immediate arguments
Diffstat (limited to 'lexer.l')
-rw-r--r--lexer.l14
1 files changed, 14 insertions, 0 deletions
diff --git a/lexer.l b/lexer.l
index f3a540c..85021c5 100644
--- a/lexer.l
+++ b/lexer.l
@@ -1,9 +1,19 @@
%{
+#include <errno.h>
+#include <stdlib.h>
+
#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]
<args>r{REGNUM} { SETVALUE(yytext[1]-'0'); return T_REG; }
<args>@r{REGNUM} { SETVALUE(yytext[2]-'0'); return T_REGPTR; }
+<args>[-+]?{DEC}+ { return intlit(yytext, 10, &yylval.lval); }
+<args>0x{HEX}+ { return intlit(yytext+2, 16, &yylval.lval); }
<args>{SP}*,{SP}* { return T_COMMA; }
{SP}*{COMMENT}?\n { BEGIN(INITIAL); return T_EOL; }