Skip to content

Commit

Permalink
grammar and macro/syntax definition simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Suraci committed Aug 28, 2011
1 parent 64327af commit 7b0a1fb
Show file tree
Hide file tree
Showing 32 changed files with 460 additions and 545 deletions.
6 changes: 3 additions & 3 deletions kernel/block.ay
@@ -1,4 +1,4 @@
macro(x onto ~(b: Block)):
macro(~x onto ~(b: Block)):
names [ctx]:
shadowed? = false

Expand All @@ -12,8 +12,8 @@ macro(x onto ~(b: Block)):
shadowed? =! true
ctx

Compose ? @headless ->
n dup tap [p]:
Call ->
n to-send tap [p]:
p receiver = 'self

Binary ? @private ->
Expand Down
93 changes: 63 additions & 30 deletions kernel/boot.ay
@@ -1,70 +1,103 @@
-- a macro so we can actually, you know, send messages
macro(x ~Variable): to-send
syntax(_ Variable): to-send

macro(nil): Primitive new(line, "nil" to-sym)
syntax(_ `(~(Variable)(~*_))): to-send

macro(self): Primitive new(line, "self" to-sym)
syntax(Variable(*_)): to-send

macro(true): Primitive new(line, "true" to-sym)
syntax(nil): Primitive new(line, "nil" to-sym)

macro(false): Primitive new(line, "false" to-sym)
syntax(self): Primitive new(line, "self" to-sym)

macro(@x): InstanceVariable new(line, x name)
syntax(true): Primitive new(line, "true" to-sym)

macro(x = y): Assign new(line, x to-pattern to-node, y)
syntax(false): Primitive new(line, "false" to-sym)

syntax(@(x: Variable)): InstanceVariable new(line, x name)

syntax(x = y): Assign new(line, x to-pattern to-node, y)

-- [x, y] { x + y }
syntax(List Block):
@right arguments = @left elements
@right

-- foo { bar }
macro(Variable ~Block):
x = `(~(@receiver)()) to-send
x block = @message
syntax(Variable Block):
x = `(~(@left)()) to-send
x block = @right
x

-- [x, y] { x + y }
macro(List ~Block):
@message arguments = @receiver elements
@message
-- foo(a) { bar }
syntax(`(~(Variable)(~*args)) Block):
x = @left to-send
x block = @right
x

-- x foo { bar }
macro(x ~(n: Variable) ~Block):
x = @receiver to-send
x block = @message
syntax(`(~_ ~Variable) Block):
x = @left to-send
x block = @right
x

-- x foo(a) { bar }
syntax(`(~_ ~(Variable)(~*args)) Block):
x = @left to-send
x block = @right
x

-- collect [x] { x + 1 }
macro((x: Variable) [*ys] ~(z: Block)):
syntax(`(~Variable [~*ys]) (z: Block)):
z arguments = ys
x = `(~(x)()) to-send
x = @left left to-send
x block = z
x

-- collect(a) [x] { x + 1 }
syntax(`(~(Variable)(~*args) [~*ys]) (z: Block)):
z arguments = ys
x = @left left to-send
x block = z
x

-- [1, 2, 3] collect [x] { x + 1 }
macro(x ~(n: Variable) [*ys] ~(z: Block)):
syntax(`(~x ~Variable [~*ys]) (z: Block)):
z arguments = ys
x = @left left to-send
x block = z
x

-- [1, 2, 3] collect(a) [x] { x + 1 }
syntax(`(~_ ~(Variable)(~*args) [~*ys]) (z: Block)):
z arguments = ys
x = @receiver receiver to-send
x = @left left to-send
x block = z
x

-- foo [bar, ...]
macro(x [*ys]):
syntax(x `[~*ys]):
to-send tap [x]:
x message-name = "[]"
x arguments = ys + x arguments

-- Foo(...)
macro(x ~Constant):
syntax(Constant(*args)):
to-send tap [x]:
x message-name = @message identifier
x message-name = @name identifier

-- Bar::Foo(...)
macro(x ~ScopedConstant):
syntax(ScopedConstant(*args)):
to-send tap [x]:
x receiver = @message parent
x message-name = @message identifier
x receiver = @name parent
x message-name = @name identifier

syntax(_LINE): line
syntax(_FILE): File new(line)

macro(_LINE): line
macro(_FILE): File new(line)
syntax(`(macro(~pattern)) `{ ~*body }):
`(syntax(~(pattern macro-pattern)): ~*body)

macro(import(name)): `(Atomy import(~name, "load" to-sym))
macro(import(~name)): `(Atomy import(~name, "load" to-sym))

"operators define cosmetics data comparison dynamic control-flow patterns
precision node data-delta particles maps block documentation namespaces doc
Expand Down
10 changes: 5 additions & 5 deletions kernel/comparison.ay
Expand Up @@ -54,8 +54,8 @@ module(Atomy::AST):

done set!

macro(a && b): And new(line, a, b)
macro(a || b): Or new(line, a, b)
macro(!a): Negate new(line, a)
macro(a != b): `!(~a == ~b)
macro(a !~ b): `!(~a =~ ~b)
syntax(a && b): And new(line, a, b)
syntax(a || b): Or new(line, a, b)
syntax(!a): Negate new(line, a)
syntax(a != b): `!(~a == ~b)
syntax(a !~ b): `!(~a =~ ~b)
4 changes: 2 additions & 2 deletions kernel/concurrency.ay
Expand Up @@ -28,7 +28,7 @@ section("Sending & Receiving"):
" spec {
=> any
} for {
macro('receive ~(body: Block)):
macro(receive ~(body: Block)):
names [e]:
bs =
`(~e when('~pat to-pattern) [~pat]: ~exp)
Expand All @@ -47,7 +47,7 @@ section("Sending & Receiving"):
" spec {
=> any
} for {
macro('receive ~(body: Block) after(timeout) ~(action: Block)):
macro(receive ~(body: Block) after(~timeout) ~(action: Block)):
names [e]:
bs =
`(~e when('~pat to-pattern) [~pat]: ~exp)
Expand Down
9 changes: 6 additions & 3 deletions kernel/condition.ay
Expand Up @@ -18,7 +18,10 @@ for-macro:
"restart:" + x name

Atomy::AST::Compose ->
"restart:" + x message name
"restart:" + x left name

Atomy::AST::Call ->
"restart:" + x name name

data(Restart(@name, @action))

Expand Down Expand Up @@ -93,7 +96,7 @@ section("Handling"):
" spec {
=> any
} for {
macro(body bind { *handlers }):
macro(~body bind { ~*handlers }):
names [a]:
callback = `([~a]: ~a match: ~*handlers)
`(let(handlers = ^handlers cons(~callback)):
Expand All @@ -115,7 +118,7 @@ section("Handling"):
" spec {
=> any
} for {
macro(with-restarts(*restarts) ~(block: Block)):
macro(with-restarts(~*restarts) ~(block: Block)):
pairs = pairs-from(restarts)

rs = pairs collect [[n, e]]:
Expand Down
48 changes: 24 additions & 24 deletions kernel/control-flow.ay
@@ -1,50 +1,50 @@
dynamic(labels, Hash new)

macro(if(x) then ~(y: Block) else ~(z: Block)):
macro(if(~x) then ~(y: Block) else ~(z: Block)):
IfThenElse new(line, x, y body, z body)

macro(otherwise): 'true

macro(return(y)):
macro(return(~y)):
Return new(line, y)

macro(x ensuring ~(y: Block)):
macro(~x ensuring ~(y: Block)):
Ensure new(line, x caller, y body)

macro(when(c) ~(b: Block)):
macro(when(~c) ~(b: Block)):
When new(line, c, b body)

macro(unless(c) ~(b: Block)):
macro(unless(~c) ~(b: Block)):
When new(line, `!~c, b body)

macro(super):
ZSuper new(line)

macro('super ~(blk: Block)):
macro(super ~(blk: Block)):
Super new(line, [], blk)

macro(super(*args) ~(blk: Block)):
macro(super(~*args) ~(blk: Block)):
Super new(line, args, blk)

macro(super(*args)):
macro(super(~*args)):
Super new(line, args, nil)

macro(yield):
Yield new(line, [])

macro(yield(*args)):
macro(yield(~*args)):
Yield new(line, args)

macro(break):
Break new(line, 'nil)

macro(break(x)):
macro(break(~x)):
Break new(line, x)

macro(next):
Next new(line, 'nil)

macro(next(x)):
macro(next(~x)):
Next new(line, x)

evaluate-when(compile, run, load):
Expand Down Expand Up @@ -101,14 +101,14 @@ evaluate-when(compile, run, load):
@value compile(g)
g ret

macro('labels ~(body: Block)):
macro(labels ~(body: Block)):
branches = body contents collect [`(~(n) ~(b))]:
LabelBranch new(n line, b body, n name)

`(let-macro(go(name) = GoTo new(line, name name)):
`(let-macro(go(~'~name) = GoTo new(line, name name)):
~(Labels new(line, branches)))

macro(while(test) ~(body: Block)):
macro(while(~test) ~(body: Block)):
names [loop, done]:
`(labels:
~loop:
Expand All @@ -123,7 +123,7 @@ macro(while(test) ~(body: Block)):

~done {})

macro(until(test) ~(body: Block)):
macro(until(~test) ~(body: Block)):
names [loop, done]:
`(labels:
~loop:
Expand All @@ -138,7 +138,7 @@ macro(until(test) ~(body: Block)):

~done {})

macro('loop ~(body: Block)):
macro(loop ~(body: Block)):
names [loop, done]:
`(labels:
~loop:
Expand All @@ -153,9 +153,9 @@ macro('loop ~(body: Block)):
for-macro:
key-from(x) := x
key-from(x: Atomy::AST::Compose) :=
if(x message is-a?(Atomy::AST::Block))
if(x right is-a?(Atomy::AST::Block))
then: x
else: x message
else: x right

pair-from(x) :=
raise("unknown pair `" + x inspect + "'")
Expand All @@ -166,30 +166,30 @@ for-macro:
pair-from(p: `(~x { ~*ys })) := do:
this = [[key-from(x), `(do: ~*ys)]]

if(x is-a?(Atomy::AST::Compose) && !(x message is-a?(Atomy::AST::Block)))
then: pair-from(x receiver) + this
if(x is-a?(Atomy::AST::Compose) && !(x right is-a?(Atomy::AST::Block)))
then: pair-from(x left) + this
else: this

pairs-from(es) :=
pair-from(e) for(e) in(es) flatten

macro('condition ~(b: Block)):
macro(condition ~(b: Block)):
pairs-from(b contents) reverse inject('nil) [acc, [x, y]]:
`(if(~x) then: ~y; else: ~acc)

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

Match new(line, x, branches)

macro(x rescue ~(y: Block)):
macro(~x rescue ~(y: Block)):
branches = pairs-from(y contents) collect [[p, e]]:
RescueHandler new(p line, p, e)

Catch new(line, x caller, branches)

macro(x rescue ~(y: Block) else ~(z: Block)):
macro(~x rescue ~(y: Block) else ~(z: Block)):
branches = pairs-from(y contents) collect [[p, e]]:
RescueHandler new(p line, p, e)

Expand Down
9 changes: 2 additions & 7 deletions kernel/cosmetics.ay
@@ -1,8 +1,3 @@
macro((x: Compose) $(y: Block)):
x expand dup tap [z]:
macro(~x $~(y: Block)):
x to-send tap [z]:
z arguments += y contents

macro((x: Variable) $(y: Block)):
x = `(~(x)()) to-send
x arguments = y contents
x

0 comments on commit 7b0a1fb

Please sign in to comment.