Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- This file: -- http://angg.twu.net/LUA/Monkey1.lua.html -- http://angg.twu.net/LUA/Monkey1.lua -- (find-angg "LUA/Monkey1.lua") -- Author: Eduardo Ochs <eduardoochs@gmail.com> -- -- (defun m () (interactive) (find-angg "LUA/Monkey1.lua")) -- See: (find-anggfile "LUA/Tree1.lua") treetor = function (o) return SynTree.from(o):torect() end treetos = function (o) return tostring(treetor(o)) end trees = function (...) local conc = function (a, b) return a.." "..b end local torect = function (o) return SynTree.from(o):torect() end return foldl1(conc, map(torect, {...})) end Tree = Class { type = "Tree", __tostring = function (o) return treetos(o) end, __index = { }, } evall = function (str, ...) return assert(loadstring(str))(...) end bin = function (a, op, b) return Tree {[0]=op, a, b} end mul = function (a, b) return bin(a, "*", b) end add = function (a, b) return bin(a, "+", b) end minus = function (a, b) return bin(a, "-", b) end frac = function (a, b) return bin(a, "/", b) end pown = function (a, n) return bin(a, "^", n) end Eq = function (a, b) return bin(a, "=", b) end app = function (f, a) return Tree {[0]=f, a} end ddx = function (f) return Tree {[0]="ddx", f} end by2 = function (rule, a, b) return Tree {[0]="by2", rule, a, b} end matches = function (t, o) if not otype(t) == otype(o) then return false end if type(t) == "table" then return t[0] == o[0] end return t == o end acton = function (o) if matches(ddx(), o) and matches(add(), o[1]) then local f,g = o[1][1], o[1][2] return add(ddx(f), ddx(g)), by2("+", f, g) end if matches(ddx(), o) and matches(minus(), o[1]) then local f,g = o[1][1], o[1][2] return minus(ddx(f), ddx(g)), by2("-", f, g) end if matches(ddx(), o) and matches(mul(), o[1]) then local f,g = o[1][1], o[1][2] return add(mul(ddx(f),g), mul(f,ddx(g))), by2("*", f, g) end if matches(ddx(), o) and matches(frac(), o[1]) then local f,g = o[1][1], o[1][2] return frac(minus(mul(ddx(f),g), mul(f,ddx(g))), pown(g,2)), by2("/", f, g) end error() end --[[ * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) dofile "Monkey1.lua" o = ddx(add(2, 3)) = trees(acton(o)) o = ddx(minus(2, 3)) = trees(acton(o)) o = ddx(mul(2, 3)) o = ddx(mul("ff", "gg")) = trees(acton(o)) o = ddx(frac("ff", "gg")) = trees(acton(o)) = foo[1][1] = foo[1][2] PPP(mul()) = trees(ddx(app("f", app("g", "x")))) = trees({1, 2, {3, 4}}, {22, 33}) A = HTable {11, 22, 33} evall([=[ local _ = ... print(_[1], _[2]) _[3] = _[3] * 10 ]=], A) = A --]] -- Local Variables: -- coding: utf-8-unix -- End: