lex the same number syntax in expressions

This commit is contained in:
Connor Olding 2016-12-01 10:19:09 -08:00
parent 9f6252117e
commit 63644df1bc
2 changed files with 24 additions and 5 deletions

2
TODO
View File

@ -1,6 +1,8 @@
unify/optimize ascii/asciiz/byte/halfword/word into BIN directives
also lex strings to binary strings, why not
lex expressions in Lexer instead of its own separate lexer
directive aliases, are these right?
DB = 'BYTE',
DH = 'HALFWORD',

View File

@ -144,21 +144,38 @@ function Expression:lex1(str, tokens)
local here = " (#"..tostring(pos)..")"
if consider(' +') then
consume(#considered)
elseif consider('[0-9.]') then
elseif consider('[0-9.]') or consider('[%%$#]') then
local num
if consider('((0|[1-9][0-9]*)%.[0-9]*|%.[0-9]+)(e0|e[1-9][0-9]*)?') then
num = tonumber(considered)
elseif consider('(0|[1-9][0-9]*)e(0|[1-9][0-9]*)') then
num = tonumber(considered)
elseif consider('[0-1]+b') then
num = tonumber(considered, 2)
elseif consider('%%[0-9]+') then
if considered:match('[2-9]') then
return "bad binary number: "..considered..here
end
num = tonumber(considered:sub(2), 2)
elseif consider('$[0-9A-Fa-f]+') then
num = tonumber(considered:sub(2), 16)
elseif consider('0x[0-9A-Fa-f]+') then
num = tonumber(considered, 16)
num = tonumber(considered:sub(3), 16)
elseif consider('0o[0-9]+') then
if considered:match('[89]') then
return "bad octal number: "..considered..here
end
num = tonumber(considered:sub(3), 8)
elseif consider('0b[0-9]+') then
if considered:match('[2-9]') then
return "bad binary number: "..considered..here
end
num = tonumber(considered:sub(3), 2)
elseif consider('0[0-9]+') then
if considered:match('[89]') then
return "bad octal number: "..considered..here
end
num = tonumber(considered, 8)
num = tonumber(considered:sub(2), 8)
elseif consider('#[0-9]*') then
num = tonumber(considered:sub(2))
elseif consider('[0-9]*') then
num = tonumber(considered)
end