-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlsystem.lua
67 lines (56 loc) · 1.26 KB
/
lsystem.lua
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
local function str_split(str)
local t = {}
for i=1, str:len() do
t[#t+1] = str:sub(i, i)
end
return t
end
local function deepcopy(orig)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in next, orig, nil do
copy[deepcopy(orig_key)] = deepcopy(orig_value)
end
setmetatable(copy, deepcopy(getmetatable(orig)))
else
copy = orig
end
return copy
end
local lsystem = function( axiom, rules )
local stringt = ''
for i,v in ipairs(axiom) do
stringt = stringt..v[1]
end
--sprint(stringt)
local state_rules = {}
for k, rule in pairs(rules) do
if type(rule) == 'string' then
state_rules[k] = str_split(rule)
else
state_rules[k] = rule
end
end
local state_axiom = {}
for i,v in ipairs(axiom) do
local char = v[1]
local args = deepcopy(v[2])
local rule = state_rules[char]
if type(rule) == 'table' then
for ii,vv in ipairs(rule) do
table.insert( state_axiom, {vv,args} )
end
elseif type(rule) == 'function' then
local t = rule(args)
for ii,vv in ipairs(t) do
table.insert( state_axiom, vv )
end
else
table.insert(state_axiom, {char,args})
end
end
return state_axiom
end
return lsystem