Skip to content

Commit

Permalink
Adding all relational operators to the AST for fixing #12
Browse files Browse the repository at this point in the history
  • Loading branch information
andremm committed Jun 20, 2017
1 parent 401d284 commit 47976de
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 63 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ AST format

lhs: `Id{ <string> } | `Index{ expr expr }

opid: -- includes additional operators from Lua 5.3
opid: -- includes additional operators from Lua 5.3 and all relational operators
'add' | 'sub' | 'mul' | 'div'
| 'idiv' | 'mod' | 'pow' | 'concat'
| 'band' | 'bor' | 'bxor' | 'shl' | 'shr'
| 'eq' | 'lt' | 'le' | 'and' | 'or'
| 'unm' | 'len' | 'bnot' | 'not'
| 'eq' | 'ne' | 'lt' | 'gt' | 'le' | 'ge'
| 'and' | 'or' | 'unm' | 'len' | 'bnot' | 'not'


Usage
Expand Down
21 changes: 5 additions & 16 deletions lua-parser/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ apply:
lhs: `Id{ <string> } | `Index{ expr expr }
opid: -- includes additional operators from Lua 5.3
opid: -- includes additional operators from Lua 5.3 and all relational operators
'add' | 'sub' | 'mul' | 'div'
| 'idiv' | 'mod' | 'pow' | 'concat'
| 'band' | 'bor' | 'bxor' | 'shl' | 'shr'
| 'eq' | 'lt' | 'le' | 'and' | 'or'
| 'unm' | 'len' | 'bnot' | 'not'
| 'eq' | 'ne' | 'lt' | 'gt' | 'le' | 'ge'
| 'and' | 'or' | 'unm' | 'len' | 'bnot' | 'not'
]]

local lpeg = require "lpeglabel"
Expand Down Expand Up @@ -197,20 +197,9 @@ end
local function binaryOp (e1, op, e2)
if not op then
return e1
else
return { tag = "Op", pos = e1.pos, [1] = op, [2] = e1, [3] = e2 }
end

local node = { tag = "Op", pos = e1.pos, [1] = op, [2] = e1, [3] = e2 }

if op == "ne" then
node[1] = "eq"
node = unaryOp("not", node)
elseif op == "gt" then
node[1], node[2], node[3] = "lt", e2, e1
elseif op == "ge" then
node[1], node[2], node[3] = "le", e2, e1
end

return node
end

local function sepBy (patt, sep, label)
Expand Down
71 changes: 27 additions & 44 deletions test.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
#!/usr/bin/env lua

local metalua = false

if arg[1] == "metalua" then metalua = true end

local parser
if metalua then
parser = require "metalua.compiler".new()
else
parser = require "lua-parser.parser"
end
local parser = require "lua-parser.parser"
local pp = require "lua-parser.pp"

-- expected result, result, subject
Expand All @@ -18,12 +9,7 @@ local e, r, s
local filename = "test.lua"

local function parse (s)
local t,m
if metalua then
t = parser:src_to_ast(s)
else
t,m = parser.parse(s,filename)
end
local t,m = parser.parse(s,filename)
local r
if not t then
r = m
Expand Down Expand Up @@ -352,8 +338,6 @@ assert(r == e)

-- syntax error

if not metalua then

-- floating points

s = [=[
Expand Down Expand Up @@ -480,8 +464,6 @@ test.lua:4:1: syntax error, unclosed long string
r = parse(s)
assert(r == e)

end

print("> testing parser...")

-- syntax ok
Expand Down Expand Up @@ -670,7 +652,7 @@ repeat
until 1
]=]
e = [=[
{ `Repeat{ { `If{ `Op{ "lt", `Number "1", `Number "2" }, { `Break } } }, `Number "1" } }
{ `Repeat{ { `If{ `Op{ "gt", `Number "2", `Number "1" }, { `Break } } }, `Number "1" } }
]=]

r = parse(s)
Expand Down Expand Up @@ -721,17 +703,13 @@ assert(r == e)

-- empty files

if not metalua then

s = [=[
;
]=]
e = [=[
{ }
]=]

end

r = parse(s)
assert(r == e)

Expand Down Expand Up @@ -823,8 +801,6 @@ assert(r == e)

-- goto

if not metalua then

s = [=[
goto label
:: label :: return
Expand Down Expand Up @@ -913,8 +889,6 @@ e = [=[
r = parse(s)
assert(r == e)

end

-- if-else

s = [=[
Expand Down Expand Up @@ -969,8 +943,6 @@ e = [=[
r = parse(s)
assert(r == e)

if not metalua then

s = [=[
if a then return a
elseif b then return
Expand All @@ -984,8 +956,6 @@ e = [=[
r = parse(s)
assert(r == e)

end

s = [=[
if a then
return
Expand All @@ -1001,8 +971,6 @@ assert(r == e)

-- labels

if not metalua then

s = [=[
::label::
do ::label:: end
Expand All @@ -1027,8 +995,6 @@ e = [=[
r = parse(s)
assert(r == e)

end

-- locals

s = [=[
Expand Down Expand Up @@ -1107,7 +1073,7 @@ s = [=[
relational = 1 < 2 >= 3 == 4 ~= 5 < 6 <= 7
]=]
e = [=[
{ `Set{ { `Id "relational" }, { `Op{ "le", `Op{ "lt", `Op{ "not", `Op{ "eq", `Op{ "eq", `Op{ "le", `Number "3", `Op{ "lt", `Number "1", `Number "2" } }, `Number "4" }, `Number "5" } }, `Number "6" }, `Number "7" } } } }
{ `Set{ { `Id "relational" }, { `Op{ "le", `Op{ "lt", `Op{ "ne", `Op{ "eq", `Op{ "ge", `Op{ "lt", `Number "1", `Number "2" }, `Number "3" }, `Number "4" }, `Number "5" }, `Number "6" }, `Number "7" } } } }
]=]

r = parse(s)
Expand Down Expand Up @@ -1357,8 +1323,6 @@ assert(r == e)

-- syntax error

if not metalua then

-- anonymous functions

s = [=[
Expand Down Expand Up @@ -1860,10 +1824,6 @@ test.lua:3:3: syntax error, expected 'do' after the condition
r = parse(s)
assert(r == e)

end

if not metalua then

print("> testing more syntax errors...")

-- ErrExtra
Expand Down Expand Up @@ -3770,6 +3730,29 @@ test.lua:6:1: syntax error, unclosed long string
r = parse(s)
assert(r == e)

print("> testing issues...")

-- issue #12
s = [===[
gl_f_ct = 0
function f()
if gl_f_ct <= 0 then
gl_f_ct=1
return 1000
end
return -1000
end
print( f("1st call") > f("2nd call") )
gl_f_ct = 0
print( f("1st call") < f("2nd call") )
]===]
e = [=[
{ `Set{ { `Id "gl_f_ct" }, { `Number "0" } }, `Set{ { `Id "f" }, { `Function{ { }, { `If{ `Op{ "le", `Id "gl_f_ct", `Number "0" }, { `Set{ { `Id "gl_f_ct" }, { `Number "1" } }, `Return{ `Number "1000" } } }, `Return{ `Op{ "unm", `Number "1000" } } } } } }, `Call{ `Id "print", `Op{ "gt", `Call{ `Id "f", `String "1st call" }, `Call{ `Id "f", `String "2nd call" } } }, `Set{ { `Id "gl_f_ct" }, { `Number "0" } }, `Call{ `Id "print", `Op{ "lt", `Call{ `Id "f", `String "1st call" }, `Call{ `Id "f", `String "2nd call" } } } }
]=]

r = parse(s)
assert(r == e)

print("OK")

0 comments on commit 47976de

Please sign in to comment.