Permalink
Browse files

add memoization to kernel

  • Loading branch information...
vito committed Dec 30, 2012
1 parent 9483d86 commit ecd63658d9de29c3cbe7a7d9cc617125334bf62f
Showing with 34 additions and 4 deletions.
  1. +2 −1 kernel/condition.ay
  2. +4 −3 kernel/grammar.ay
  3. +28 −0 kernel/memo.ay
View
@@ -5,12 +5,13 @@ use("control-flow")
use("loop")
use("interpolation")
use("array")
+use("memo")
use("dynamic")
io = use("io")
pairs = require("pairs")
-pretty() = @pretty ||= require("pretty")
+memo(pretty): require("pretty")
data(Handler(@callback, @rest = nil)):
BaseHandler()
View
@@ -5,15 +5,16 @@ use("dynamic")
use("particles")
use("node")
use("init")
+use("memo")
require("kpeg")
require("kpeg/compiled_parser")
require("set")
--- lazily load this module to prevent circular dependency
-pretty() = @pretty ||= require("pretty")
-intp() = @intp ||= require("interpolation")
+-- lazily load these modules to prevent circular dependency
+memo(pretty): require("pretty")
+memo(intp): require("interpolation")
actions = dynamic
View
@@ -0,0 +1,28 @@
+use("core")
+use("array")
+use("control-flow")
+use("define")
+
+macro(memo(~(name ? to-word)): ~*body):
+ `(memo((~name)()): ~*body)
+
+macro(memo((~name)(~*args)): ~*body):
+ -- create an alias for each arg, to be used for the branch's patterns
+ fakes = names(args size)
+ kept-args = `(~f: ~a) for [f, a] in args zip(fakes)
+
+ names [tmp]:
+ define-memo =
+ if(args empty?)
+ -- no args; just redefine function
+ then: `((~name)() =! ~tmp)
+
+ -- has args; add pattern-matching branch
+ else: `eval(`((~'~name)(~*~fakes) =! ~~tmp))
+
+ `((~name)(~*kept-args) = do:
+ ~tmp = do: ~*body
+
+ ~define-memo
+
+ ~tmp)

0 comments on commit ecd6365

Please sign in to comment.