Commit 3f9077a4 authored by Jamie A. Jennings's avatar Jamie A. Jennings

Simple examples now compile. E.g.

src = 'x = --1\n"W" "Y" / "Z"'; p = stmt:match(src); x = (infix.to_prefix(p))
a = rosie.env.ast.from_parse_tree(x, rosie.env.common.source.new{text=src})
messages = {}
e.compiler.compile_block(a, e.env, nil, messages)
rplx = e:compile('x')

then:

    > rplx:match("WY")
    table: 0x7f9e81fa27e0	0	false	4	3
    > rplx:match("WZ")
    table: 0x7f9e81cab080	0	false	5	4
    > rplx:match("W")
    false	1	false	4	4
    >
parent bdefad39
Pipeline #61496927 passed with stage
in 2 minutes and 2 seconds
......@@ -31,9 +31,10 @@
-- parenthetical expressions can be recognized by a unique prefix (an open
-- paren), the RPL grammar will parse one as a TERM.
local DEBUG=false
local infix = {}
infix.DEBUG=false -- temporary
local list = require("list")
map = list.map; filter = list.filter
......@@ -117,7 +118,7 @@ end
local shunting_yard_explist;
local function shunting_yard_exp(exp, attribute_table)
if DEBUG then print("*** entering shunting_yard_exp with: ", exp.type, " ", exp.data) end
if infix.DEBUG then print("*** entering shunting_yard_exp with: ", exp.type, " ", exp.data) end
local function map_shunting_yard_exp(ls)
return map(function(e) return shunting_yard_exp(e, attribute_table) end, ls)
end
......@@ -156,7 +157,7 @@ local function shunting_yard_exp(exp, attribute_table)
data=exp.data,
subs=newsubs}
else
if DEBUG then print("*** skipping: ", exp.type, " ", exp.data) end
if infix.DEBUG then print("*** skipping: ", exp.type, " ", exp.data) end
return exp
end
end
......@@ -179,9 +180,9 @@ end
-- expressions, is implemented by shunting_yard_exp() here.
local OPERATOR_ATTRIBUTES =
{ ["SEQUENCE"] = { associativity="right", precedence=10 },
["/"] = { associativity="right", precedence=10 },
["&"] = { associativity="right", precedence=10 } }
{ ["SEQUENCE"] = { associativity="right", precedence=10, nodetype="form.sequence" },
["/"] = { associativity="right", precedence=10, nodetype="form.choice"},
["&"] = { associativity="right", precedence=10, nodetype="form.and_exp"} }
local function should_push(op, stack_top, attribute_table)
local attr = attribute_table[op]
......@@ -222,12 +223,16 @@ end
local function make_prefix_tree(op, exps)
local right = pop_exp(exps)
local left = pop_exp(exps)
if DEBUG then
if infix.DEBUG then
print("right operand: "); table.print(right)
print("left operand: "); table.print(left)
end
assert( right[1] and left[1] )
op.subs = list.append(left, right)
-- Convert generic "operator" nodes to choice, sequence, or and_exp
local attr = OPERATOR_ATTRIBUTES[op.data]
assert(attr)
op.type = attr.nodetype
return op
end
......@@ -239,15 +244,19 @@ end
function shunting_yard_explist(subs, attribute_table)
if not attribute_table then attribute_table = OPERATOR_ATTRIBUTES end
if DEBUG then
if infix.DEBUG then
io.stdout:write(" entering: ")
for _,v in ipairs(subs) do io.stdout:write(v.type, " ") end
print()
subs = attach_quantifiers(subs)
end
subs = attach_quantifiers(subs)
if infix.DEBUG then
io.stdout:write(" after attaching quantifiers: ")
for _,v in ipairs(subs) do io.stdout:write(v.type, " ") end
print()
subs = insert_seq_operators(subs)
end
subs = insert_seq_operators(subs)
if infix.DEBUG then
io.stdout:write(" after inserting seq operators: ")
for _,v in ipairs(subs) do io.stdout:write(v.type, " ", v.data, " ") end
print()
......@@ -278,16 +287,12 @@ function shunting_yard_explist(subs, attribute_table)
return exps.to_list()
end
function shunting_yard(ls)
local exps = {}
for _, exp in ipairs(ls) do
if exp.type=="form.exp" then
table.insert(exps, shunting_yard_exp(exp))
else
table.insert(exps, exp)
end
end -- for
return exps
function shunting_yard(exp)
if exp.type=="form.exp" then
return shunting_yard_exp(exp)
else
return exp
end
end
--------------------------------------------------------------------------------
......@@ -311,7 +316,7 @@ local function handle_statement(s)
local n = #s.subs
assert( s.subs[n].type=="form.exp" )
for i=1,n-1 do newsubs[i] = s.subs[i] end
newsubs[n] = shunting_yard({s.subs[n]})
newsubs[n] = shunting_yard(s.subs[n])
return {type=s.type,
s=s.s,
e=s.e,
......@@ -360,7 +365,7 @@ function infix.to_prefix(pt)
s = pt.s,
e = pt.e,
data = pt.data,
subs = shunting_yard(pt.subs)}
subs = map(shunting_yard, pt.subs)}
elseif pt.type=="rpl_statements" then
assert(pt.subs)
return {type = pt.type,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment