capitalex revisó este gist . Ir a la revisión
2 files changed, 125 insertions
main.lua(archivo creado)
@@ -0,0 +1,14 @@ | |||
1 | + | pp = require "pprint" | |
2 | + | package.path = package.path .. ";./src/?.lua" | |
3 | + | local Lvera = require "src.lvera" | |
4 | + | local lua_generator = require "src.generators.lua" | |
5 | + | ||
6 | + | local lvera = Lvera.new() | |
7 | + | ||
8 | + | lvera:load("samples/hailstone.nv") | |
9 | + | ||
10 | + | lvera:add_pass(require "passes.ports" (lua_generator.ports)) | |
11 | + | lvera:add_pass(require "passes.constants") | |
12 | + | lvera:add_pass(require "passes.eliminate-dead-code") | |
13 | + | ||
14 | + | lvera:compile(lua_generator) |
zz.generated.lua(archivo creado)
@@ -0,0 +1,111 @@ | |||
1 | + | local min, max, huge = math.min, math.max, math.huge | |
2 | + | machine.counters = { | |
3 | + | ["hailstone"] = 1, | |
4 | + | ["x:27"] = 1, | |
5 | + | } | |
6 | + | function match(counters) | |
7 | + | if counters["x:6"] > 0 then | |
8 | + | acc = counters["x:6"] | |
9 | + | counters["x:6"] = max(counters["x:6"] - acc, 0) | |
10 | + | counters["x"] = counters["x"] + acc * 6 | |
11 | + | return true | |
12 | + | end | |
13 | + | if counters["x:3"] > 0 then | |
14 | + | acc = counters["x:3"] | |
15 | + | counters["x:3"] = max(counters["x:3"] - acc, 0) | |
16 | + | counters["x"] = counters["x"] + acc * 3 | |
17 | + | return true | |
18 | + | end | |
19 | + | if counters["move"] > 0 and counters["2"] > 0 then | |
20 | + | local acc = huge | |
21 | + | acc = min(acc, counters["2"]) | |
22 | + | acc = min(acc, counters["move"]) | |
23 | + | counters["move"] = max(counters["move"] - acc, 0) | |
24 | + | counters["2"] = max(counters["2"] - acc, 0) | |
25 | + | counters["x"] = counters["x"] + acc * 1 | |
26 | + | counters["move"] = counters["move"] + acc * 1 | |
27 | + | return true | |
28 | + | end | |
29 | + | if counters["move"] > 0 and counters["1"] > 0 then | |
30 | + | local acc = huge | |
31 | + | acc = min(acc, counters["1"]) | |
32 | + | acc = min(acc, counters["move"]) | |
33 | + | counters["move"] = max(counters["move"] - acc, 0) | |
34 | + | counters["1"] = max(counters["1"] - acc, 0) | |
35 | + | counters["x"] = counters["x"] + acc * 1 | |
36 | + | return true | |
37 | + | end | |
38 | + | if counters["move"] > 0 then | |
39 | + | acc = counters["move"] | |
40 | + | counters["move"] = max(counters["move"] - acc, 0) | |
41 | + | counters["hailstone"] = counters["hailstone"] + acc * 1 | |
42 | + | return true | |
43 | + | end | |
44 | + | if counters["odd"] > 0 and counters["2"] > 0 then | |
45 | + | local acc = huge | |
46 | + | acc = min(acc, counters["2"]) | |
47 | + | acc = min(acc, counters["odd"]) | |
48 | + | counters["odd"] = max(counters["odd"] - acc, 0) | |
49 | + | counters["2"] = max(counters["2"] - acc, 0) | |
50 | + | counters["x:6"] = counters["x:6"] + acc * 1 | |
51 | + | counters["odd"] = counters["odd"] + acc * 1 | |
52 | + | return true | |
53 | + | end | |
54 | + | if counters["odd"] > 0 then | |
55 | + | acc = counters["odd"] | |
56 | + | counters["odd"] = max(counters["odd"] - acc, 0) | |
57 | + | counters["hailstone"] = counters["hailstone"] + acc * 1 | |
58 | + | counters["x"] = counters["x"] + acc * 1 | |
59 | + | return true | |
60 | + | end | |
61 | + | if counters["eval"] > 0 and counters["x"] > 0 and counters["1"] > 0 then | |
62 | + | local acc = huge | |
63 | + | acc = min(acc, counters["1"]) | |
64 | + | acc = min(acc, counters["eval"]) | |
65 | + | acc = min(acc, counters["x"]) | |
66 | + | counters["eval"] = max(counters["eval"] - acc, 0) | |
67 | + | counters["x"] = max(counters["x"] - acc, 0) | |
68 | + | counters["1"] = max(counters["1"] - acc, 0) | |
69 | + | counters["2"] = counters["2"] + acc * 1 | |
70 | + | counters["eval"] = counters["eval"] + acc * 1 | |
71 | + | return true | |
72 | + | end | |
73 | + | if counters["eval"] > 0 and counters["x"] > 0 then | |
74 | + | local acc = huge | |
75 | + | acc = min(acc, counters["x"]) | |
76 | + | acc = min(acc, counters["eval"]) | |
77 | + | counters["eval"] = max(counters["eval"] - acc, 0) | |
78 | + | counters["x"] = max(counters["x"] - acc, 0) | |
79 | + | counters["1"] = counters["1"] + acc * 1 | |
80 | + | counters["eval"] = counters["eval"] + acc * 1 | |
81 | + | return true | |
82 | + | end | |
83 | + | if counters["eval"] > 0 and counters["2"] > 0 and counters["1"] > 0 then | |
84 | + | local acc = huge | |
85 | + | acc = min(acc, counters["1"]) | |
86 | + | acc = min(acc, counters["2"]) | |
87 | + | acc = min(acc, counters["eval"]) | |
88 | + | counters["eval"] = max(counters["eval"] - acc, 0) | |
89 | + | counters["2"] = max(counters["2"] - acc, 0) | |
90 | + | counters["1"] = max(counters["1"] - acc, 0) | |
91 | + | counters["x:3"] = counters["x:3"] + acc * 1 | |
92 | + | counters["2"] = counters["2"] + acc * 1 | |
93 | + | counters["odd"] = counters["odd"] + acc * 1 | |
94 | + | return true | |
95 | + | end | |
96 | + | if counters["eval"] > 0 then | |
97 | + | acc = counters["eval"] | |
98 | + | counters["eval"] = max(counters["eval"] - acc, 0) | |
99 | + | counters["move"] = counters["move"] + acc * 1 | |
100 | + | return true | |
101 | + | end | |
102 | + | if counters["hailstone"] > 0 then | |
103 | + | acc = counters["hailstone"] | |
104 | + | counters["hailstone"] = max(counters["hailstone"] - acc, 0) | |
105 | + | counters["eval"] = counters["eval"] + acc * 1 | |
106 | + | counters["@show counters"] = counters["@show counters"] + acc * 1 | |
107 | + | return true | |
108 | + | end | |
109 | + | return false | |
110 | + | end | |
111 | + | return machine |
Siguiente
Anterior