Permalink
Browse files

Started on simple recode_pattern and added a small test.

  • Loading branch information...
1 parent 07ed35b commit 2112b1f4667ebbc32e8a05085be697a7aa14ce13 @toivoh committed May 17, 2012
Showing with 84 additions and 1 deletion.
  1. +3 −1 .gitignore
  2. +3 −0 dev/patterns.jl
  3. +48 −0 dev/recode.jl
  4. +1 −0 dev/test/test_code_pmatch.jl
  5. +27 −0 dev/test/test_recode.jl
  6. +2 −0 dev/utils/utils.jl
View
@@ -1,2 +1,4 @@
*~
-#*
+\#*\#
+.\#*
+
View
@@ -50,3 +50,6 @@ type DomPattern <: Pattern
new(p, dom)
end
end
+
+restrict(p::DomPattern, dom::Domain) = restrict(dintersect(dom, p.dom), p.p)
+restrict(p, dom::Domain) = DomPattern(p, dom)
View
@@ -0,0 +1,48 @@
+
+load("utils/req.jl")
+req("utils/utils.jl")
+req("patterns.jl")
+
+type RPContext
+ vars::Dict{Symbol,Symbol} # variable name ==> temp var name
+ RPContext() = new(Dict{Symbol,Symbol}())
+end
+
+function getvar(c::RPContext, name::Symbol)
+ has(c.vars, name) ? c.vars[name] : ( c.vars[name] = gensym(string(name)) )
+end
+function code_create_pvars(c::RPContext)
+ { :( ($tmpname)=pvar($quotevalue(name)) ) for (name, tmpname) in c.vars }
+end
+
+function recode_pattern_ex(ex)
+ rpc = RPContext()
+ pattern_ex = recode_pattern_ex(rpc, ex)
+ pvar_defs = code_create_pvars(rpc)
+ :( let ($pvar_defs...)
+ ($pattern_ex)
+ end )
+end
+
+function recode_pattern_ex(c::RPContext, ex::Expr)
+ head, args = ex.head, ex.args
+ nargs = length(args)
+ if head == doublecolon
+ @expect nargs==2
+ arg = recode_pattern_ex(c, args[1])
+# return :( restrict(($arg), ($args[2])) )
+ return :( ($quotevalue(restrict))(($arg), ($args[2])) )
+ elseif contains([:call, :ref, :curly], head)
+ if (head==:call) && (args[1]==:staticvalue)
+ @expect nargs==2
+ return eval(args[2])
+ else
+ return expr(head, args[1],
+ {recode_pattern_ex(c,arg) for arg in ex.args[2:end]}...)
+ end
+ else
+ return expr(head, {recode_pattern_ex(c,arg) for arg in ex.args})
+ end
+end
+recode_pattern_ex(c::RPContext, sym::Symbol) = getvar(c, sym)
+recode_pattern_ex(c::RPContext, ex) = ex # other terminals
@@ -1,4 +1,5 @@
+load("utils/req.jl")
load("pmatch.jl")
req("utils/utils.jl")
req("../prettyshow/prettyshow.jl")
@@ -0,0 +1,27 @@
+
+load("utils/req.jl")
+load("recode.jl")
+req("utils/utils.jl")
+req("../prettyshow/prettyshow.jl")
+
+function show_recode(ex)
+ println()
+ @pshow ex
+ try
+ @pshow eval(recode_pattern_ex(ex))
+ catch err
+ @pshowln recode_pattern_ex(ex)
+ throw(err)
+ end
+ @pshowln recode_pattern_ex(ex)
+end
+
+show_recode(:1)
+show_recode(:x)
+show_recode(:(1,2))
+show_recode(:(x,2))
+show_recode(:(x,x))
+show_recode(:(x,y))
+#show_recode(:(x::Int))
+println()
+@pshowln recode_pattern_ex(:(x::Int))
View
@@ -1,4 +1,6 @@
+const doublecolon = @eval (:(::Int)).head
+
quotevalue(val) = expr(:quote, val)
macro expect(pred)

0 comments on commit 2112b1f

Please sign in to comment.