Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

250 lines (178 sloc) 4.67 kb
module(Atomy::Patterns):
class(Predicate < Pattern):
children(#pattern)
attributes(#test)
generate
construct(g) := do:
get(g)
@pattern construct(g)
@test construct(g)
g send(#new, 2)
target(g) := @pattern target(g)
matches?(g) := do:
mismatch = g new-label
done = g new-label
g dup
@pattern matches?(g)
g gif(mismatch)
`{ ~@test } bytecode(g)
g send(#block, 0)
g swap
g send(#call-on-instance, 1)
g goto(done)
mismatch set!
g pop
g push-false
done set!
deconstruct(g, locals = ::Hash new) :=
@pattern deconstruct(g, locals)
precision := @pattern precision + 1
class(With < Pattern):
children(#pattern, #sub-pattern)
attributes(#expression)
generate
construct(g) := do:
get(g)
@pattern construct(g)
@sub-pattern construct(g)
@expression construct(g)
g send(#new, 3)
target(g) := @pattern target(g)
matches?(g) := do:
mismatch = g new-label
done = g new-label
@result = g new-stack-local
g dup
@pattern matches?(g)
g gif(mismatch)
`{ ~@expression } bytecode(g)
g send(#block, 0)
g swap
g push-scope
g send(#call-under, 2)
g set-stack-local(@result)
@sub-pattern matches?(g)
g dup
g git(done)
mismatch set!
g pop
g push-false
done set!
matches-self?(g) := do:
mismatch = g new-label
done = g new-label
@result = g new-stack-local
@pattern matches-self?(g)
g gif(mismatch)
@expression compile(g)
g set-stack-local(@result)
@sub-pattern matches?(g)
g dup
g git(done)
g pop
mismatch set!
g push-false
done set!
deconstruct(g, locals = ::Hash new) := do:
unless(@result): g dup
@pattern deconstruct(g, locals)
if(@result)
then: g push-stack-local(@result)
else:
`{ ~@expression } bytecode(g)
g send(#block, 0)
g swap
g push-scope
g send(#call-under, 2)
@sub-pattern deconstruct(g, locals)
class(And < Pattern):
children(#a, #b)
generate
target(g) := @a target(g)
matches?(g) := do:
mismatch = g new-label
done = g new-label
g dup
@a matches?(g)
g gif(mismatch)
@b matches?(g)
g dup
g git(done)
mismatch set!
g pop
g push-false
done set!
deconstruct(g, locals = ::Hash new) := do:
g dup
@a deconstruct(g, locals)
@b deconstruct(g, locals)
class(Or < Pattern):
children(#a, #b)
generate
target(g) := @a target(g)
matches?(g) := do:
matched = g new-label
done = g new-label
g dup
@a matches?(g)
g git(matched)
@b matches?(g)
g dup
g gif(done)
matched set!
g pop
g push-true
done set!
deconstruct(g, locals = ::Hash new) := do:
b = g new-label
done = g new-label
g dup
g dup
@a matches?(g)
g gif(b)
@a deconstruct(g, locals)
g pop
g goto(done)
b set!
g pop
@b deconstruct(g, locals)
done set!
-- base patterns
module(Atomy::AST):
Word to-pattern :=
Atomy::Patterns::Named new $:
Atomy::Patterns::Any new
@text
Primitive to-pattern :=
Atomy::Patterns::Match new(@value)
Literal to-pattern :=
Atomy::Patterns::Literal new(@value)
List to-pattern :=
Atomy::Patterns::List new $:
@elements collect [e]: e to-pattern
Constant to-pattern :=
Atomy::Patterns::Constant new(self)
ScopedConstant to-pattern :=
Atomy::Patterns::Constant new(self)
ToplevelConstant to-pattern :=
Atomy::Patterns::Constant new(self)
Quote to-pattern :=
Atomy::Patterns::Quote new(@expression)
Block to-pattern :=
Atomy::Patterns::SingletonClass new(body)
QuasiQuote to-pattern :=
Atomy::Patterns::QuasiQuote new(self)
`(~x { ~y }) to-pattern :=
Atomy::Patterns::Named new(y to-pattern, x text)
`(~x ~(y: Atomy::AST::Word)) to-pattern :=
Atomy::Patterns::Attribute new(x, y text, [])
`(~x ~(y: Atomy::AST::Word)(~*as)) to-pattern :=
Atomy::Patterns::Attribute new(x, y text, as)
`(~x [~*as]) to-pattern :=
Atomy::Patterns::Attribute new(x, #"[]", as)
String to-pattern :=
Atomy::Patterns::Literal new(@value)
Symbol to-pattern :=
Atomy::Patterns::Literal new(@name to-sym)
Node to-pattern :=
raise("unknown pattern: " + to-sexp inspect)
Jump to Line
Something went wrong with that request. Please try again.