atomy 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING +30 -0
- data/README.md +1 -0
- data/bin/atomy +134 -0
- data/kernel/block.ay +30 -0
- data/kernel/boot.ay +10 -0
- data/kernel/comparison.ay +61 -0
- data/kernel/concurrency.ay +84 -0
- data/kernel/condition.ay +277 -0
- data/kernel/control-flow.ay +222 -0
- data/kernel/cosmetics.ay +3 -0
- data/kernel/data-delta.ay +105 -0
- data/kernel/data.ay +56 -0
- data/kernel/define.ay +93 -0
- data/kernel/doc.ay +453 -0
- data/kernel/documentation.ay +135 -0
- data/kernel/dynamic.ay +42 -0
- data/kernel/errors.ay +6 -0
- data/kernel/format.ay +13 -0
- data/kernel/format/data.ay +89 -0
- data/kernel/format/formatter.ay +345 -0
- data/kernel/format/parser.ay +13 -0
- data/kernel/hashes.ay +39 -0
- data/kernel/io.ay +244 -0
- data/kernel/namespaces.ay +63 -0
- data/kernel/node.ay +48 -0
- data/kernel/operators.ay +28 -0
- data/kernel/particles.ay +53 -0
- data/kernel/patterns.ay +256 -0
- data/kernel/precision.ay +148 -0
- data/kernel/pretty.ay +283 -0
- data/kernel/repl.ay +140 -0
- data/kernel/therie.ay +204 -0
- data/lib/ast/binary_send.rb +44 -0
- data/lib/ast/block.rb +268 -0
- data/lib/ast/constant.rb +88 -0
- data/lib/ast/internal/assign.rb +19 -0
- data/lib/ast/internal/block_pass.rb +21 -0
- data/lib/ast/internal/catch.rb +247 -0
- data/lib/ast/internal/class.rb +30 -0
- data/lib/ast/internal/class_variable.rb +23 -0
- data/lib/ast/internal/define.rb +174 -0
- data/lib/ast/internal/ensure.rb +135 -0
- data/lib/ast/internal/file.rb +14 -0
- data/lib/ast/internal/global_variable.rb +20 -0
- data/lib/ast/internal/if_then_else.rb +24 -0
- data/lib/ast/internal/instance_variable.rb +17 -0
- data/lib/ast/internal/let_macro.rb +35 -0
- data/lib/ast/internal/macro_quote.rb +23 -0
- data/lib/ast/internal/match.rb +53 -0
- data/lib/ast/internal/module.rb +30 -0
- data/lib/ast/internal/pattern.rb +17 -0
- data/lib/ast/internal/return.rb +29 -0
- data/lib/ast/internal/set.rb +19 -0
- data/lib/ast/internal/singleton_class.rb +18 -0
- data/lib/ast/internal/splat.rb +14 -0
- data/lib/ast/internal/when.rb +24 -0
- data/lib/ast/list.rb +25 -0
- data/lib/ast/macro.rb +37 -0
- data/lib/ast/node.rb +599 -0
- data/lib/ast/operator.rb +21 -0
- data/lib/ast/particle.rb +13 -0
- data/lib/ast/primitive.rb +20 -0
- data/lib/ast/quasi_quote.rb +20 -0
- data/lib/ast/quote.rb +13 -0
- data/lib/ast/send.rb +104 -0
- data/lib/ast/splice.rb +32 -0
- data/lib/ast/string.rb +23 -0
- data/lib/ast/unary.rb +44 -0
- data/lib/ast/unquote.rb +45 -0
- data/lib/ast/variable.rb +64 -0
- data/lib/atomy.kpeg.rb +3995 -0
- data/lib/code_loader.rb +137 -0
- data/lib/compiler/compiler.rb +155 -0
- data/lib/compiler/stages.rb +81 -0
- data/lib/formatter.kpeg.rb +1394 -0
- data/lib/macros.rb +317 -0
- data/lib/method.rb +261 -0
- data/lib/namespace.rb +236 -0
- data/lib/parser.rb +28 -0
- data/lib/patterns.rb +276 -0
- data/lib/patterns/any.rb +21 -0
- data/lib/patterns/attribute.rb +59 -0
- data/lib/patterns/block_pass.rb +54 -0
- data/lib/patterns/constant.rb +33 -0
- data/lib/patterns/default.rb +44 -0
- data/lib/patterns/head_tail.rb +63 -0
- data/lib/patterns/list.rb +77 -0
- data/lib/patterns/match.rb +45 -0
- data/lib/patterns/named.rb +55 -0
- data/lib/patterns/named_class.rb +46 -0
- data/lib/patterns/named_global.rb +46 -0
- data/lib/patterns/named_instance.rb +46 -0
- data/lib/patterns/particle.rb +29 -0
- data/lib/patterns/quasi_quote.rb +184 -0
- data/lib/patterns/quote.rb +33 -0
- data/lib/patterns/singleton_class.rb +31 -0
- data/lib/patterns/splat.rb +57 -0
- data/lib/util.rb +37 -0
- metadata +164 -0
@@ -0,0 +1,46 @@
|
|
1
|
+
module Atomy::Patterns
|
2
|
+
class NamedClass < Pattern
|
3
|
+
attr_reader :identifier
|
4
|
+
|
5
|
+
def initialize(n)
|
6
|
+
@identifier = n
|
7
|
+
end
|
8
|
+
|
9
|
+
def name
|
10
|
+
("@@" + @identifier).to_sym
|
11
|
+
end
|
12
|
+
|
13
|
+
def construct(g)
|
14
|
+
get(g)
|
15
|
+
g.push_literal @identifier
|
16
|
+
g.send :new, 1
|
17
|
+
end
|
18
|
+
|
19
|
+
def ==(b)
|
20
|
+
b.kind_of?(NamedClass) and \
|
21
|
+
@identifier == b.identifier
|
22
|
+
end
|
23
|
+
|
24
|
+
def target(g)
|
25
|
+
g.push_const :Object
|
26
|
+
end
|
27
|
+
|
28
|
+
def matches?(g)
|
29
|
+
g.pop
|
30
|
+
g.push_true
|
31
|
+
end
|
32
|
+
|
33
|
+
def deconstruct(g, locals = {})
|
34
|
+
Rubinius::AST::ClassVariableAssignment.new(0, name, nil).bytecode(g)
|
35
|
+
g.pop
|
36
|
+
end
|
37
|
+
|
38
|
+
def local_names
|
39
|
+
[]
|
40
|
+
end
|
41
|
+
|
42
|
+
def bindings
|
43
|
+
1
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module Atomy::Patterns
|
2
|
+
class NamedGlobal < Pattern
|
3
|
+
attr_reader :identifier
|
4
|
+
|
5
|
+
def initialize(n)
|
6
|
+
@identifier = n
|
7
|
+
end
|
8
|
+
|
9
|
+
def name
|
10
|
+
("$" + @identifier).to_sym
|
11
|
+
end
|
12
|
+
|
13
|
+
def construct(g)
|
14
|
+
get(g)
|
15
|
+
g.push_literal @identifier
|
16
|
+
g.send :new, 1
|
17
|
+
end
|
18
|
+
|
19
|
+
def ==(b)
|
20
|
+
b.kind_of?(NamedGlobal) and \
|
21
|
+
@identifier == b.identifier
|
22
|
+
end
|
23
|
+
|
24
|
+
def target(g)
|
25
|
+
g.push_const :Object
|
26
|
+
end
|
27
|
+
|
28
|
+
def matches?(g)
|
29
|
+
g.pop
|
30
|
+
g.push_true
|
31
|
+
end
|
32
|
+
|
33
|
+
def deconstruct(g, locals = {})
|
34
|
+
Rubinius::AST::GlobalVariableAssignment.new(0, name, nil).bytecode(g)
|
35
|
+
g.pop
|
36
|
+
end
|
37
|
+
|
38
|
+
def local_names
|
39
|
+
[]
|
40
|
+
end
|
41
|
+
|
42
|
+
def bindings
|
43
|
+
1
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module Atomy::Patterns
|
2
|
+
class NamedInstance < Pattern
|
3
|
+
attr_reader :identifier
|
4
|
+
|
5
|
+
def initialize(n)
|
6
|
+
@identifier = n
|
7
|
+
end
|
8
|
+
|
9
|
+
def name
|
10
|
+
("@" + @identifier).to_sym
|
11
|
+
end
|
12
|
+
|
13
|
+
def construct(g)
|
14
|
+
get(g)
|
15
|
+
g.push_literal @identifier
|
16
|
+
g.send :new, 1
|
17
|
+
end
|
18
|
+
|
19
|
+
def ==(b)
|
20
|
+
b.kind_of?(NamedInstance) and \
|
21
|
+
@identifier == b.identifier
|
22
|
+
end
|
23
|
+
|
24
|
+
def target(g)
|
25
|
+
g.push_const :Object
|
26
|
+
end
|
27
|
+
|
28
|
+
def matches?(g)
|
29
|
+
g.pop
|
30
|
+
g.push_true
|
31
|
+
end
|
32
|
+
|
33
|
+
def deconstruct(g, locals = {})
|
34
|
+
Rubinius::AST::InstanceVariableAssignment.new(0, name, nil).bytecode(g)
|
35
|
+
g.pop
|
36
|
+
end
|
37
|
+
|
38
|
+
def local_names
|
39
|
+
[]
|
40
|
+
end
|
41
|
+
|
42
|
+
def bindings
|
43
|
+
1
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Atomy::Patterns
|
2
|
+
class Particle < Pattern
|
3
|
+
attr_reader :value
|
4
|
+
|
5
|
+
def initialize(x)
|
6
|
+
@value = x
|
7
|
+
end
|
8
|
+
|
9
|
+
def construct(g)
|
10
|
+
get(g)
|
11
|
+
g.push_literal @value
|
12
|
+
g.send :new, 1
|
13
|
+
end
|
14
|
+
|
15
|
+
def ==(b)
|
16
|
+
b.kind_of?(Match) and \
|
17
|
+
@value == b.value
|
18
|
+
end
|
19
|
+
|
20
|
+
def target(g)
|
21
|
+
g.push_const :Symbol # TODO
|
22
|
+
end
|
23
|
+
|
24
|
+
def matches?(g)
|
25
|
+
g.push_literal @value
|
26
|
+
g.send :==, 1
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,184 @@
|
|
1
|
+
# TODO: fix splice unquotes
|
2
|
+
module Atomy::Patterns
|
3
|
+
class QuasiQuote < Pattern
|
4
|
+
attr_reader :quoted
|
5
|
+
|
6
|
+
def initialize(x)
|
7
|
+
@quoted = x
|
8
|
+
end
|
9
|
+
|
10
|
+
def construct(g)
|
11
|
+
get(g)
|
12
|
+
@quoted.construct(g, nil)
|
13
|
+
g.send :new, 1
|
14
|
+
end
|
15
|
+
|
16
|
+
def ==(b)
|
17
|
+
b.kind_of?(QuasiQuote) and \
|
18
|
+
@quoted == b.expression
|
19
|
+
end
|
20
|
+
|
21
|
+
def expression
|
22
|
+
@quoted.expression
|
23
|
+
end
|
24
|
+
|
25
|
+
def target(g)
|
26
|
+
names = expression.class.name.split("::")
|
27
|
+
g.push_const names.slice!(0).to_sym
|
28
|
+
names.each do |n|
|
29
|
+
g.find_const n.to_sym
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def context(g, w)
|
34
|
+
w.each do |c|
|
35
|
+
# TODO: fail if out of bounds?
|
36
|
+
# e.g. `(foo(~bar, ~baz)) = '(foo(1))
|
37
|
+
if c.kind_of?(Array)
|
38
|
+
g.send c[0], 0
|
39
|
+
g.push_int c[1]
|
40
|
+
g.send :[], 1
|
41
|
+
else
|
42
|
+
g.send c, 0
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def matches?(g)
|
48
|
+
mismatch = g.new_label
|
49
|
+
done = g.new_label
|
50
|
+
|
51
|
+
them = g.new_stack_local
|
52
|
+
g.set_stack_local them
|
53
|
+
g.pop
|
54
|
+
|
55
|
+
where = nil
|
56
|
+
splice = false
|
57
|
+
|
58
|
+
pre = proc { |e, c, d|
|
59
|
+
where << c if c && where
|
60
|
+
|
61
|
+
where = [] if !where and c == :expression
|
62
|
+
|
63
|
+
splice = true if e.kind_of?(Atomy::AST::Splice)
|
64
|
+
|
65
|
+
if !(e.unquote? && d == 1) && where && c != :unquoted
|
66
|
+
e.get(g)
|
67
|
+
g.push_stack_local them
|
68
|
+
context(g, where)
|
69
|
+
g.kind_of
|
70
|
+
g.gif mismatch
|
71
|
+
|
72
|
+
e.details.each do |a|
|
73
|
+
val = e.send(a)
|
74
|
+
if val.kind_of?(Array)
|
75
|
+
val.each do |v|
|
76
|
+
g.push_literal v
|
77
|
+
end
|
78
|
+
g.make_array val.size
|
79
|
+
else
|
80
|
+
g.push_literal val
|
81
|
+
end
|
82
|
+
g.push_stack_local them
|
83
|
+
context(g, where)
|
84
|
+
g.send a, 0
|
85
|
+
g.send :==, 1
|
86
|
+
g.gif mismatch
|
87
|
+
end
|
88
|
+
|
89
|
+
if e.bottom?
|
90
|
+
e.construct(g)
|
91
|
+
g.push_stack_local them
|
92
|
+
context(g, where)
|
93
|
+
g.send :==, 1
|
94
|
+
g.gif mismatch
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
true
|
99
|
+
}
|
100
|
+
|
101
|
+
post = proc { where.pop }
|
102
|
+
|
103
|
+
@quoted.through_quotes(pre, post) do |e|
|
104
|
+
ctx = where.last == :unquoted ? where[0..-2] : where
|
105
|
+
g.push_stack_local them
|
106
|
+
if splice
|
107
|
+
g.send ctx.last[0], 0
|
108
|
+
g.push_int ctx.last[1]
|
109
|
+
g.send :drop, 1
|
110
|
+
splice = false
|
111
|
+
else
|
112
|
+
context(g, ctx)
|
113
|
+
end
|
114
|
+
e.to_pattern.matches?(g)
|
115
|
+
g.gif mismatch
|
116
|
+
e
|
117
|
+
end
|
118
|
+
|
119
|
+
g.push_true
|
120
|
+
g.goto done
|
121
|
+
|
122
|
+
mismatch.set!
|
123
|
+
g.push_false
|
124
|
+
|
125
|
+
done.set!
|
126
|
+
end
|
127
|
+
|
128
|
+
def deconstruct(g, locals = {})
|
129
|
+
them = g.new_stack_local
|
130
|
+
g.set_stack_local them
|
131
|
+
g.pop
|
132
|
+
|
133
|
+
where = nil
|
134
|
+
splice = false
|
135
|
+
|
136
|
+
pre = proc { |n, c|
|
137
|
+
where << c if c && where
|
138
|
+
|
139
|
+
where = [] if !where and c == :expression
|
140
|
+
|
141
|
+
splice = true if n.kind_of?(Atomy::AST::Splice)
|
142
|
+
|
143
|
+
true
|
144
|
+
}
|
145
|
+
|
146
|
+
post = proc { where.pop }
|
147
|
+
|
148
|
+
@quoted.through_quotes(pre, post) do |e|
|
149
|
+
ctx = where.last == :unquoted ? where[0..-2] : where
|
150
|
+
g.push_stack_local them
|
151
|
+
if splice
|
152
|
+
g.send ctx.last[0], 0
|
153
|
+
g.push_int ctx.last[1]
|
154
|
+
g.send :drop, 1
|
155
|
+
splice = false
|
156
|
+
else
|
157
|
+
context(g, ctx)
|
158
|
+
end
|
159
|
+
e.to_pattern.deconstruct(g)
|
160
|
+
e
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
def local_names
|
165
|
+
names = []
|
166
|
+
|
167
|
+
@quoted.through_quotes(proc { true }) do |e|
|
168
|
+
names += e.to_pattern.local_names
|
169
|
+
end
|
170
|
+
|
171
|
+
names
|
172
|
+
end
|
173
|
+
|
174
|
+
def bindings
|
175
|
+
bindings = 0
|
176
|
+
|
177
|
+
@quoted.through_quotes(proc { true }) do |e|
|
178
|
+
bindings += e.to_pattern.bindings
|
179
|
+
end
|
180
|
+
|
181
|
+
bindings
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Atomy::Patterns
|
2
|
+
class Quote < Pattern
|
3
|
+
attr_reader :expression
|
4
|
+
|
5
|
+
def initialize(x)
|
6
|
+
@expression = x
|
7
|
+
end
|
8
|
+
|
9
|
+
def construct(g)
|
10
|
+
get(g)
|
11
|
+
@expression.construct(g, nil)
|
12
|
+
g.send :new, 1
|
13
|
+
end
|
14
|
+
|
15
|
+
def ==(b)
|
16
|
+
b.kind_of?(Quote) and \
|
17
|
+
@expression == b.expression
|
18
|
+
end
|
19
|
+
|
20
|
+
def target(g)
|
21
|
+
names = @expression.class.name.split("::")
|
22
|
+
g.push_const names.slice!(0).to_sym
|
23
|
+
names.each do |n|
|
24
|
+
g.find_const n.to_sym
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def matches?(g)
|
29
|
+
@expression.construct(g, nil)
|
30
|
+
g.send :==, 1
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Atomy::Patterns
|
2
|
+
class SingletonClass < Pattern
|
3
|
+
attr_reader :body
|
4
|
+
|
5
|
+
def initialize(body)
|
6
|
+
@body = body
|
7
|
+
end
|
8
|
+
|
9
|
+
def construct(g)
|
10
|
+
get(g)
|
11
|
+
@body.construct(g, nil)
|
12
|
+
g.send(:new, 1)
|
13
|
+
end
|
14
|
+
|
15
|
+
def ==(b)
|
16
|
+
b.kind_of?(SingletonClass) and \
|
17
|
+
@body == b.body
|
18
|
+
end
|
19
|
+
|
20
|
+
def target(g)
|
21
|
+
@body.compile(g)
|
22
|
+
g.send :call, 0
|
23
|
+
g.send :singleton_class, 0
|
24
|
+
end
|
25
|
+
|
26
|
+
def matches?(g)
|
27
|
+
g.pop
|
28
|
+
g.push_true
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module Atomy::Patterns
|
2
|
+
class Splat < Pattern
|
3
|
+
attr_accessor :pattern
|
4
|
+
|
5
|
+
def initialize(p)
|
6
|
+
@pattern = p
|
7
|
+
end
|
8
|
+
|
9
|
+
def construct(g)
|
10
|
+
get(g)
|
11
|
+
@pattern.construct(g)
|
12
|
+
g.send :new, 1
|
13
|
+
end
|
14
|
+
|
15
|
+
def ==(b)
|
16
|
+
b.kind_of?(Splat) and \
|
17
|
+
@pattern == b.pattern
|
18
|
+
end
|
19
|
+
|
20
|
+
def target(g)
|
21
|
+
g.push_const :Object
|
22
|
+
end
|
23
|
+
|
24
|
+
def matches?(g)
|
25
|
+
g.pop
|
26
|
+
g.push_true
|
27
|
+
end
|
28
|
+
|
29
|
+
def deconstruct(g, locals = {})
|
30
|
+
singleton = g.new_label
|
31
|
+
done = g.new_label
|
32
|
+
|
33
|
+
g.dup
|
34
|
+
g.push_const :Array
|
35
|
+
g.swap
|
36
|
+
g.instance_of
|
37
|
+
g.git singleton
|
38
|
+
|
39
|
+
g.cast_array
|
40
|
+
g.goto done
|
41
|
+
|
42
|
+
singleton.set!
|
43
|
+
g.make_array 1
|
44
|
+
|
45
|
+
done.set!
|
46
|
+
@pattern.deconstruct(g, locals)
|
47
|
+
end
|
48
|
+
|
49
|
+
def local_names
|
50
|
+
@pattern.local_names
|
51
|
+
end
|
52
|
+
|
53
|
+
def bindings
|
54
|
+
@pattern.bindings
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|