aboutsummaryrefslogtreecommitdiff
path: root/examples/math.lua
blob: eed979b0c7745f02023f6d3df6b161bbe1f18740 (plain) (blame)
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
local patok   = require 'patok'
local inspect = require 'inspect'
local pm      = require 'piecemeal'

local lexer = patok {
	op    = '[+*]',
	digit = '%d+',
	space = '%s+',
}()

local lex = {
	-- plus  = pm.lexeme 'plus',
	plus  = pm.value '+',
	times = pm.value '*',
	digit = pm.postp(pm.lexeme 'digit', function(d) return tonumber(d.value) end),
	space = pm.opt(pm.lexeme 'space')
}

local function findnums(d, acc)
	local out = acc or {}
	for _, v in ipairs(d) do -- only ipairs to avoid triggering on tokens
		if type(v) == 'number' then
			table.insert(out, v)
		elseif type(v) == 'table' then
			findnums(v, out)
		end
	end
	return out
end

local mult = pm.postp(
	pm.all(lex.digit, pm.star(pm.all(lex.space, lex.times, lex.space, lex.digit))),
	function (d)
		local acc = 1
		for _, v in ipairs(findnums(d)) do
			acc = acc * v
		end
		return acc
	end)
	
local add = pm.postp(
	pm.all(mult, pm.star(pm.all(lex.space, lex.plus, lex.space, mult))),
	function (d)
		local acc = 0
		for _, v in ipairs(findnums(d)) do
			acc = acc + v
		end
		return acc
	end)

local expr = add

local test
if arg[1] then
	local f = io.open(arg[1], 'r')
	test = f:read '*a'
	f:close()
else
	test = [[10 + 5 * 2 + 10]]
end

local eind, out = pm.parse(test, lexer, expr)
print(eind, out)