Skip to content

Commit

Permalink
start initial match impl
Browse files Browse the repository at this point in the history
  • Loading branch information
Roger-luo committed Oct 12, 2023
1 parent ca61c9d commit fbfc3fa
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/match/emit/mod.jl
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
function emit(value, expr::Pattern.Type, body)
function emit(info::EmitInfo)
quote
let $(info.value_holder) = $(info.value)
if cond
body
@goto $(info.final_label)
end
end
@label $(info.final_label)
end
end
10 changes: 9 additions & 1 deletion src/match/scan.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ struct EmitInfo
value::Any
patterns::Vector{Pattern.Type}
exprs::Vector{Any}
value_holder::Symbol
final_label::Symbol
return_var::Symbol
source
Expand Down Expand Up @@ -31,7 +32,14 @@ function EmitInfo(mod::Module, value, body, source = nothing)
throw(SyntaxError("invalid pattern table: $body"; source))
end

return EmitInfo(mod, value, patterns, exprs, gensym("final"), gensym("return"), source)
return EmitInfo(
mod, value, patterns,
exprs,
gensym("value"),
gensym("final"),
gensym("return"),
source
)
end

function expr2pattern(expr)
Expand Down
4 changes: 3 additions & 1 deletion test/match/data.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Liang.Match: EmitInfo, Pattern, expr2pattern
using Liang.Match: Match, EmitInfo, Pattern, expr2pattern
using Liang.Expression: Scalar

expr2pattern(:(1:10))
Expand Down Expand Up @@ -53,3 +53,5 @@ info = EmitInfo(Main, :x, quote
(a, b) && c => a + b
[x, y, 2, 3] => x
end)

Match.emit(info)

0 comments on commit fbfc3fa

Please sign in to comment.