From c0c4d81b5a1ea7c91f26af209bab06691418dc9c Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Fri, 15 Jan 2016 07:49:43 -0800 Subject: [PATCH] handle parenthesis after lexing instead --- lips/Lexer.lua | 18 +++++------------- lips/Muncher.lua | 10 +++++++++- lips/Parser.lua | 2 +- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lips/Lexer.lua b/lips/Lexer.lua index 7f29441..a834703 100644 --- a/lips/Lexer.lua +++ b/lips/Lexer.lua @@ -318,18 +318,14 @@ function Lexer:lex(_yield) end self:nextc() yield('DEF', buff) + elseif self.chr == ']' then + self:error('unmatched closing bracket') elseif self.chr == '(' then self:nextc() - local buff = self:read_chars('[%w_]') - if self.chr ~= ')' then - self:error('invalid register name') - end + yield('OPEN', '(') + elseif self.chr == ')' then self:nextc() - local up = buff:upper() - if not data.all_registers[up] then - self:error('not a register') - end - yield('DEREF', up) + yield('CLOSE', ')') elseif self.chr == '.' then self:nextc() local buff = self:read_chars('[%w]') @@ -373,10 +369,6 @@ function Lexer:lex(_yield) end yield('LABELSYM', buff) end - elseif self.chr == ']' then - self:error('unmatched closing bracket') - elseif self.chr == ')' then - self:error('unmatched closing parenthesis') elseif self.chr == '+' or self.chr == '-' then local sign_chr = self.chr local sign = sign_chr == '+' and 1 or -1 diff --git a/lips/Muncher.lua b/lips/Muncher.lua index 1a71bc9..fbf774d 100644 --- a/lips/Muncher.lua +++ b/lips/Muncher.lua @@ -68,11 +68,19 @@ function Muncher:register(t) end function Muncher:deref() - if self.tt ~= 'DEREF' then + if self.tt ~= 'OPEN' then + self:error('expected opening parenthesis for dereferencing') + end + self:advance() + if self.tt ~= 'REG' then self:error('expected register to dereference') end local reg = self.tok self:advance() + if self.tt ~= 'CLOSE' then + self:error('expected closing parenthesis for dereferencing') + end + self:advance() return reg end diff --git a/lips/Parser.lua b/lips/Parser.lua index 132bbcb..26fcfc9 100644 --- a/lips/Parser.lua +++ b/lips/Parser.lua @@ -182,7 +182,7 @@ function Parser:instruction() local args = {} args.rt = self:register() self:optional_comma() - if self.tt == 'DEREF' then + if self.tt == 'OPEN' then args.offset = {'NUM', 0} args.base = self:deref() else -- NUM or LABELSYM