Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Started on mew @pattern macro.

  • Loading branch information...
commit b43ad7b31952fe18f2d141b12676d1d2009d18cd 1 parent dd754e1
@toivoh authored
Showing with 56 additions and 1 deletion.
  1. +33 −0 unify/pmatch.jl
  2. +23 −1 unify/test/test_pdisp.jl
View
33 unify/pmatch.jl
@@ -272,6 +272,39 @@ function dispatch(mt::PatternMethodTable, args::Tuple)
error("no dispatch found for pattern function $(m.fname)$args")
end
+
+const __patmethod_tables = Dict{Function,PatternMethodTable}()
+
+macro pattern(fdef)
+ code_pattern_fdef(fdef)
+end
+function code_pattern_fdef(fdef)
+ signature, body = split_fdef(fdef)
+ @expect is_expr(signature, :call)
+ pattern = quoted_tuple(signature.args[2:end])
+ method = patmethod(pattern, body)
+
+ fname = signature.args[1]
+ qfname = quoted_expr(fname)
+ @gensym fun mtable
+ quote
+ ($fun) = nothing
+ try
+ ($fun) = ($fname)
+ end
+ if is(($fun), nothing)
+ ($mtable) = PatternMethodTable($qfname)
+# const ($fname) = create_pattern_function(($mtable))
+ const ($fname) = (args...)->dispatch(($mtable), args)
+ __patmethod_tables[$fname] = ($mtable)
+ else
+ ($mtable) = __patmethod_tables[$fun]
+ end
+ add(($mtable), ($quoted_expr(method)))
+ end
+end
+
+
# function code_pattern_dispatch(mt::PatternMethodTable, fname::Symbol)
# argsname = :args
# code = {}
View
24 unify/test/test_pdisp.jl
@@ -29,4 +29,26 @@ f = (args...)->(dispatch(mtable, args))
println()
@show f(1)
@show f(2)
-@show f(3)
+@show f(3)
+
+
+# defined = true
+# try
+# g(1)
+# catch err
+# defined = false
+# end
+# if !defined
+# const g = x->x
+# end
+# g(1)
+
+fun = nothing
+try
+ fun = g
+end
+if is(fun, nothing)
+ const g = x->x
+end
+g(1)
+
Please sign in to comment.
Something went wrong with that request. Please try again.