Skip to content

Commit

Permalink
modular patterns
Browse files Browse the repository at this point in the history
* keep Node#to_pattern primitive
* define patterns via 'pattern(pat): body', where pat is a pattern
matching a Node, and body returns a Pattern
* Module#make_pattern(node) for node -> pattern conversion
* similar semantics to macro expansion
  • Loading branch information
vito committed Apr 22, 2012
1 parent 2579731 commit d1cbb6e
Show file tree
Hide file tree
Showing 21 changed files with 271 additions and 163 deletions.
2 changes: 1 addition & 1 deletion kernel/atomy.ay
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require("operators")
require("patterns")
require("errors")

export(
require("core")
require("patterns")
require("define")
require("comparison")
require("loop")
Expand Down
9 changes: 5 additions & 4 deletions kernel/block.ay
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use("control-flow")
use("meta")
use("interpolation")
use("node")
use("patterns")

Atomy Patterns open:
NamedRedirectedInstance = NamedInstance class:
Expand Down Expand Up @@ -97,11 +98,11 @@ Atomy AST open:
g send(.class-variable-get, 1)


RedirectedInstanceVariable to-pattern :=
Atomy Patterns NamedRedirectedInstance new(@target, @name)
pattern(ri: Atomy AST RedirectedInstanceVariable):
Atomy Patterns NamedRedirectedInstance new(node target, node name)

RedirectedClassVariable to-pattern :=
Atomy Patterns NamedRedirectedClass new(@target, @name)
pattern(rc: Atomy AST RedirectedClassVariable):
Atomy Patterns NamedRedirectedClass new(node target, node name)


macro(~x onto ~(b: Block)):
Expand Down
4 changes: 2 additions & 2 deletions kernel/concurrency.ay
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ Actor <- v := send(v)
macro(receive ~(body: Block)):
names [e]:
bs =
`(~e when('~pat to-pattern) [~pat]: ~exp)
`(~e when(make-pattern('~pat)) [~pat]: ~exp)
for [pat, exp] in pairs-from(body contents)

`(Actor receive [~e]: ~*bs)

macro(receive ~(body: Block) after(~timeout) ~(action: Block)):
names [e]:
bs =
`(~e when('~pat to-pattern) [~pat]: ~exp)
`(~e when(make-pattern('~pat)) [~pat]: ~exp)
for [pat, exp] in pairs-from(body contents)

`(Actor receive [~e]:
Expand Down
2 changes: 1 addition & 1 deletion kernel/control-flow.ay
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ macro(condition ~(b: Block)):

macro(~val match ~(b: Block)):
branches = pairs-from(b contents) collect [x, y]:
MatchBranch new(x line, x to-pattern to-node, y)
MatchBranch new(x line, x, y)

Match new(node line, val, branches)

Expand Down
3 changes: 2 additions & 1 deletion kernel/core.ay
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ macro(false): Primitive new(node line, "false" to-sym)
macro(_): Primitive new(node line, "undefined" to-sym)

-- assignment/pattern-matching
macro(~x = ~y): Assign new(node line, x to-pattern to-node, y)
macro(~x = ~y): Assign new(node line, x, y)

macro(_ ~(c: Constant)):
ToplevelConstant new(node line, c name)
Expand Down Expand Up @@ -237,4 +237,5 @@ macro(~x (~Word)(~*args)!):

node


use("operators")
8 changes: 6 additions & 2 deletions kernel/data.ay
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ use("define")
use("control-flow")
use("comparison")

-- patterns defined by dsl use 'with', so make sure it's exported
export(use("patterns"))


define-class(nil, e: Atomy AST Constant) = `(~e = class:)
define-class(root, e: Atomy AST Constant) = `(~e = ~root class:)
define-class(root, `(~n { ~*cs })) =
Expand All @@ -28,8 +32,8 @@ define-class(root, `((~name)(~*as): ~*cs)) = do:
cons = `((~name)(~*tmps))

pat-def =
`(~(Atomy AST QuasiQuote new(0, cons)) to-pattern :=
~(Atomy AST QuasiQuote new(0, pat)) to-pattern)
`(pattern(~(Atomy AST QuasiQuote new(0, cons))):
pattern(~(Atomy AST QuasiQuote new(0, pat))))

parent = root or 'Object

Expand Down
2 changes: 1 addition & 1 deletion kernel/define.ay
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ infix("&= ||= |= += -= /= ^= %=", 10, .right)
macro(do ~(b: Block)): b body

macro(~x =! ~y):
Set new(node line, x to-pattern to-node, y)
Set new(node line, x, y)

macro((~name)(~*args) = ~body):
Function new(node line, body, args, name text)
Expand Down
1 change: 1 addition & 0 deletions kernel/doc.ay
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use("define")
use("control-flow")
use("comparison")
use("array")
use("patterns")

infix("</> <//> <$> <$$>", 60, .right)
infix("<> <+>", 70, .right)
Expand Down
1 change: 1 addition & 0 deletions kernel/format/formatter.ay
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use("loop")
use("regexp")
use("comparison")
use("particles")
use("patterns")

pretty = require("pretty")

Expand Down
1 change: 1 addition & 0 deletions kernel/interpolation.ay
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use("quotes")
use("range")
use("grammar")
use("node")
use("patterns")

parser(Parser):
%atomy := Atomy Parser
Expand Down
60 changes: 31 additions & 29 deletions kernel/particles.ay
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use("data")
use("control-flow")
use("cosmetics")
use("comparison")
use("patterns")

data(Particle(@receiver, @message, @arguments))

Expand Down Expand Up @@ -120,61 +121,62 @@ Atomy Patterns open:
g send(.arguments, 0)
@arguments deconstruct(g, mod, locals)

`.(~r [~*as]) to-pattern :=

pattern(`.(~r [~*as])):
Atomy Patterns Particle new(
r to-pattern
pattern(r)
.[]
`[~*as] to-pattern)
pattern(`[~*as]))

`.[] to-pattern :=
pattern(`.[]):
Atomy Patterns Literal new(.[])

`.[~*as] to-pattern :=
pattern(`.[~*as]):
Atomy Patterns Particle new(
'_ to-pattern
pattern('_)
.[]
`[~*as] to-pattern)
pattern(`[~*as]))

`.~(x: Atomy AST Binary) to-pattern :=
pattern(`.~(x: Atomy AST Binary)):
Atomy Patterns Particle new(
if(x private)
then: '_ to-pattern
else: x lhs to-pattern
`[~(x rhs)] to-pattern
then: pattern('_)
else: pattern(x lhs)
pattern(`[~(x rhs)])
x operator)

`.((~(x: Atomy AST Word))(~*as)) to-pattern :=
pattern(`.((~(x: Atomy AST Word))(~*as))):
Atomy Patterns Particle new(
'_ to-pattern
`[~*as] to-pattern
pattern('_)
pattern(`[~*as])
x text)

`.((~(x: Atomy AST Word))(~*as)?) to-pattern :=
pattern(`.((~(x: Atomy AST Word))(~*as)?)):
Atomy Patterns Particle new(
'_ to-pattern
`[~*as] to-pattern
pattern('_)
pattern(`[~*as])
`((~x)?) to-word text)

`.((~(x: Atomy AST Word))(~*as)!) to-pattern :=
pattern(`.((~(x: Atomy AST Word))(~*as)!)):
Atomy Patterns Particle new(
'_ to-pattern
`[~*as] to-pattern
pattern('_)
pattern(`[~*as])
`((~x)!) to-word text)

`.(~r (~(x: Atomy AST Word))(~*as)) to-pattern :=
pattern(`.(~r (~(x: Atomy AST Word))(~*as))):
Atomy Patterns Particle new(
r to-pattern
`[~*as] to-pattern
pattern(r)
pattern(`[~*as])
x text)

`.(~r (~(x: Atomy AST Word))(~*as)?) to-pattern :=
pattern(`.(~r (~(x: Atomy AST Word))(~*as)?)):
Atomy Patterns Particle new(
r to-pattern
`[~*as] to-pattern
pattern(r)
pattern(`[~*as])
`((~x)?) to-word text)

`.(~r (~(x: Atomy AST Word))(~*as)!) to-pattern :=
pattern(`.(~r (~(x: Atomy AST Word))(~*as)!)):
Atomy Patterns Particle new(
r to-pattern
`[~*as] to-pattern
pattern(r)
pattern(`[~*as])
`((~x)!) to-word text)
Loading

0 comments on commit d1cbb6e

Please sign in to comment.