aboutsummaryrefslogtreecommitdiff
path: root/examples/expRecAut.lua
blob: 18d7606c14c3990a686a4a07f7a5775c791c5b07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
local m = require"lpeglabel"
local re = require"relabel"

local num = m.R("09")^1 / tonumber
local op = m.S("+-")

local labels = {}

local function newError(lab, msg, psync, pcap)
	psync = psync or m.P(-1)
	pcap = pcap or m.P""
	labels[lab] = { msg = msg, psync = psync, pcap = pcap }
end

newError("ExpTermFirst", "expected an expression", op + ")", m.Cc(1000)) 
newError("ExpTermOp", "expected a term after the operator", op + ")", m.Cc(1000))
newError("MisClose",  "missing a closing ')' after the expression",  m.P")")
newError("Extra", "extra characters found after the expression") 

local errors, subject

local function expect(patt, lab)
  return patt + m.T(lab)
end

local function compute(tokens)
  local result = tokens[1]
  for i = 2, #tokens, 2 do
    if tokens[i] == '+' then
      result = result + tokens[i+1]
    elseif tokens[i] == '-' then
      result = result - tokens[i+1]
    else
      error('unknown operation: ' .. tokens[i])
    end
  end
  return result
end

function recorderror(pos, lab)
	local line, col = re.calcline(subject, pos)
	table.insert(errors, { line = line, col = col, msg = labels[lab].msg })
end

function record (labname)
	return (m.Cp() * m.Cc(labname)) / recorderror
end

function sync (p)
	return (-p * m.P(1))^0
end

function defaultValue (p)
	return p or m.Cc(1000) 
end

local g = {
	"Exp",
	Exp = m.Ct(m.V"OperandFirst" * (m.C(op) * m.V"Operand")^0) / compute,
	OperandFirst = expect(m.V"Term", "ExpTermFirst"),
	Operand = expect(m.V"Term", "ExpTermOp"),
	Term = num + m.V"Group",
	Group = "(" * m.V"Exp" * expect(")", "MisClose"),
}

-- set first rule
g[1] = "S"
g["S"] = g["Exp"] * expect(m.P(-1), "Extra")
for k, v in pairs(labels) do
  g[k] = record(k) * sync(v.psync) * v.pcap 
end

g = m.P(g)

local function eval(input)
	errors = {}
	io.write("Input: ", input, "\n")
	subject = input
  local result, label, suffix = g:match(input)
  io.write("Syntactic errors found: " .. #errors, "\n")
	if #errors > 0 then
    local out = {}
    for i, err in ipairs(errors) do
      local pos = err.col
      local msg = err.msg
      table.insert(out, "syntax error: " .. msg .. " (at index " .. pos .. ")")
    end
    print(table.concat(out, "\n"))
  end
	io.write("Result = ")
	return result	
end

print(eval "90-70-(5)+3")
--> 18

print(eval "15+")
--> 2 + 0

print(eval "-2")
--> 0 - 2 

print(eval "1+3+-9")
--> 1 + 3 + [0] - 9

print(eval "1+()3+")
--> 1 + ([0]) [+] 3 + [0]

print(eval "8-(2+)-5")
--> 8 - (2 + [0]) - 5 

print(eval "()")

print(eval "")

print(eval "1+()+")

print(eval "1+(")

print(eval "3)")

print(eval "11+()3")
--> 1 + ([0]) [+] 3 + [0]