generate-code.lua
· 611 B · Lua
原始文件
Playground
function run_rules(state)
if state["flour"] >= 1 and state["sugar"] >= 1 and state["apples"] >= 1 then
emit(state, "apple cake")
return run_rules(state)
elseif state["apples"] >= 1 and state["oranges"] >= 1 and state["cherries"] >= 1 then
emit(state, "fruit salad")
return run_rules(state)
elseif state["fruit salad"] >= 1 and state["apple cake"] >= 1 then
emit(state, "fruit cake")
return run_rules(state)
else
end
end
tally(state, "sugar")
tally(state, "oranges")
tally(state, "apples")
tally(state, "cherries")
tally(state, "flour")
tally(state, "apples")
run_rules(state)
| 1 | |
| 2 | function run_rules(state) |
| 3 | if state["flour"] >= 1 and state["sugar"] >= 1 and state["apples"] >= 1 then |
| 4 | emit(state, "apple cake") |
| 5 | return run_rules(state) |
| 6 | elseif state["apples"] >= 1 and state["oranges"] >= 1 and state["cherries"] >= 1 then |
| 7 | emit(state, "fruit salad") |
| 8 | return run_rules(state) |
| 9 | elseif state["fruit salad"] >= 1 and state["apple cake"] >= 1 then |
| 10 | emit(state, "fruit cake") |
| 11 | return run_rules(state) |
| 12 | else |
| 13 | end |
| 14 | end |
| 15 | |
| 16 | tally(state, "sugar") |
| 17 | tally(state, "oranges") |
| 18 | tally(state, "apples") |
| 19 | tally(state, "cherries") |
| 20 | tally(state, "flour") |
| 21 | tally(state, "apples") |
| 22 | run_rules(state) |
linear-multiset-catlang.txt
· 361 B · Text
原始文件
Playground
function (run rules) [flour sugar apples]
(> (apple cake)) (run rules)
end
function (run rules) [apples oranges cherries]
(> (fruit salad)) (run rules)
end
function (run rules) [(fruit salad) (apple cake)]
(> (fruit cake)) (run rules)
end
function (run rules) []
end
(> sugar) (> oranges)
(> apples) (> cherries)
(> flour) (> apples)
(run rules)
| 1 | function (run rules) [flour sugar apples] |
| 2 | (> (apple cake)) (run rules) |
| 3 | end |
| 4 | |
| 5 | function (run rules) [apples oranges cherries] |
| 6 | (> (fruit salad)) (run rules) |
| 7 | end |
| 8 | |
| 9 | function (run rules) [(fruit salad) (apple cake)] |
| 10 | (> (fruit cake)) (run rules) |
| 11 | end |
| 12 | |
| 13 | function (run rules) [] |
| 14 | end |
| 15 | |
| 16 | (> sugar) (> oranges) |
| 17 | (> apples) (> cherries) |
| 18 | (> flour) (> apples) |
| 19 | (run rules) |
| 20 |
something.lua
· 316 B · Lua
原始文件
Playground
local operation_stack = { ... }
local fact_pool = { ... }
local operations = { ... }
while #ops > 0 do
local op = pop(operation_stack)
if operations[op] then
operations[op](operation_stack, fact_pool)
else
if not fact_pool[op] then fact_pool[op] = 0 end
fact_pool[op] = fact_pool[op] + 1
end
end
| 1 | local operation_stack = { ... } |
| 2 | local fact_pool = { ... } |
| 3 | local operations = { ... } |
| 4 | |
| 5 | while #ops > 0 do |
| 6 | local op = pop(operation_stack) |
| 7 | if operations[op] then |
| 8 | operations[op](operation_stack, fact_pool) |
| 9 | else |
| 10 | if not fact_pool[op] then fact_pool[op] = 0 end |
| 11 | fact_pool[op] = fact_pool[op] + 1 |
| 12 | end |
| 13 | end |