atomy 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (99) hide show
  1. data/COPYING +30 -0
  2. data/README.md +1 -0
  3. data/bin/atomy +134 -0
  4. data/kernel/block.ay +30 -0
  5. data/kernel/boot.ay +10 -0
  6. data/kernel/comparison.ay +61 -0
  7. data/kernel/concurrency.ay +84 -0
  8. data/kernel/condition.ay +277 -0
  9. data/kernel/control-flow.ay +222 -0
  10. data/kernel/cosmetics.ay +3 -0
  11. data/kernel/data-delta.ay +105 -0
  12. data/kernel/data.ay +56 -0
  13. data/kernel/define.ay +93 -0
  14. data/kernel/doc.ay +453 -0
  15. data/kernel/documentation.ay +135 -0
  16. data/kernel/dynamic.ay +42 -0
  17. data/kernel/errors.ay +6 -0
  18. data/kernel/format.ay +13 -0
  19. data/kernel/format/data.ay +89 -0
  20. data/kernel/format/formatter.ay +345 -0
  21. data/kernel/format/parser.ay +13 -0
  22. data/kernel/hashes.ay +39 -0
  23. data/kernel/io.ay +244 -0
  24. data/kernel/namespaces.ay +63 -0
  25. data/kernel/node.ay +48 -0
  26. data/kernel/operators.ay +28 -0
  27. data/kernel/particles.ay +53 -0
  28. data/kernel/patterns.ay +256 -0
  29. data/kernel/precision.ay +148 -0
  30. data/kernel/pretty.ay +283 -0
  31. data/kernel/repl.ay +140 -0
  32. data/kernel/therie.ay +204 -0
  33. data/lib/ast/binary_send.rb +44 -0
  34. data/lib/ast/block.rb +268 -0
  35. data/lib/ast/constant.rb +88 -0
  36. data/lib/ast/internal/assign.rb +19 -0
  37. data/lib/ast/internal/block_pass.rb +21 -0
  38. data/lib/ast/internal/catch.rb +247 -0
  39. data/lib/ast/internal/class.rb +30 -0
  40. data/lib/ast/internal/class_variable.rb +23 -0
  41. data/lib/ast/internal/define.rb +174 -0
  42. data/lib/ast/internal/ensure.rb +135 -0
  43. data/lib/ast/internal/file.rb +14 -0
  44. data/lib/ast/internal/global_variable.rb +20 -0
  45. data/lib/ast/internal/if_then_else.rb +24 -0
  46. data/lib/ast/internal/instance_variable.rb +17 -0
  47. data/lib/ast/internal/let_macro.rb +35 -0
  48. data/lib/ast/internal/macro_quote.rb +23 -0
  49. data/lib/ast/internal/match.rb +53 -0
  50. data/lib/ast/internal/module.rb +30 -0
  51. data/lib/ast/internal/pattern.rb +17 -0
  52. data/lib/ast/internal/return.rb +29 -0
  53. data/lib/ast/internal/set.rb +19 -0
  54. data/lib/ast/internal/singleton_class.rb +18 -0
  55. data/lib/ast/internal/splat.rb +14 -0
  56. data/lib/ast/internal/when.rb +24 -0
  57. data/lib/ast/list.rb +25 -0
  58. data/lib/ast/macro.rb +37 -0
  59. data/lib/ast/node.rb +599 -0
  60. data/lib/ast/operator.rb +21 -0
  61. data/lib/ast/particle.rb +13 -0
  62. data/lib/ast/primitive.rb +20 -0
  63. data/lib/ast/quasi_quote.rb +20 -0
  64. data/lib/ast/quote.rb +13 -0
  65. data/lib/ast/send.rb +104 -0
  66. data/lib/ast/splice.rb +32 -0
  67. data/lib/ast/string.rb +23 -0
  68. data/lib/ast/unary.rb +44 -0
  69. data/lib/ast/unquote.rb +45 -0
  70. data/lib/ast/variable.rb +64 -0
  71. data/lib/atomy.kpeg.rb +3995 -0
  72. data/lib/code_loader.rb +137 -0
  73. data/lib/compiler/compiler.rb +155 -0
  74. data/lib/compiler/stages.rb +81 -0
  75. data/lib/formatter.kpeg.rb +1394 -0
  76. data/lib/macros.rb +317 -0
  77. data/lib/method.rb +261 -0
  78. data/lib/namespace.rb +236 -0
  79. data/lib/parser.rb +28 -0
  80. data/lib/patterns.rb +276 -0
  81. data/lib/patterns/any.rb +21 -0
  82. data/lib/patterns/attribute.rb +59 -0
  83. data/lib/patterns/block_pass.rb +54 -0
  84. data/lib/patterns/constant.rb +33 -0
  85. data/lib/patterns/default.rb +44 -0
  86. data/lib/patterns/head_tail.rb +63 -0
  87. data/lib/patterns/list.rb +77 -0
  88. data/lib/patterns/match.rb +45 -0
  89. data/lib/patterns/named.rb +55 -0
  90. data/lib/patterns/named_class.rb +46 -0
  91. data/lib/patterns/named_global.rb +46 -0
  92. data/lib/patterns/named_instance.rb +46 -0
  93. data/lib/patterns/particle.rb +29 -0
  94. data/lib/patterns/quasi_quote.rb +184 -0
  95. data/lib/patterns/quote.rb +33 -0
  96. data/lib/patterns/singleton_class.rb +31 -0
  97. data/lib/patterns/splat.rb +57 -0
  98. data/lib/util.rb +37 -0
  99. metadata +164 -0
@@ -0,0 +1,222 @@
1
+ dynamic(labels, Hash new)
2
+
3
+ macro(if(x) then(&y) else(&z)):
4
+ Atomy::AST::IfThenElse new(line, x, y body, z body)
5
+
6
+ macro(otherwise): 'true
7
+
8
+ macro(condition(&bs)):
9
+ bs contents reverse inject('nil) [acc, `(~x -> ~y)]:
10
+ `(if(~x) then: ~y; else: ~acc)
11
+
12
+ macro(return(y)):
13
+ Atomy::AST::Return new(line, y)
14
+
15
+ macro(x rescue(&y)):
16
+ Atomy::AST::Catch new(line, x caller, y contents)
17
+
18
+ macro(x rescue(&y) else(&z)):
19
+ Atomy::AST::Catch new(line, x caller, y contents, z body)
20
+
21
+ macro(x ensuring(&y)):
22
+ Atomy::AST::Ensure new(line, x caller, y body)
23
+
24
+ macro(when(c, &b)):
25
+ Atomy::AST::When new(line, c, b body)
26
+
27
+ macro(unless(c, &b)):
28
+ Atomy::AST::When new(line, `!~c, b body)
29
+
30
+ macro(super):
31
+ Atomy::AST::ZSuper new(line)
32
+
33
+ macro(super(*args, &blk)):
34
+ Atomy::AST::Super new(line, args, blk)
35
+
36
+ macro(yield):
37
+ Atomy::AST::Yield new(line, [])
38
+
39
+ macro(yield(*args)):
40
+ Atomy::AST::Yield new(line, args)
41
+
42
+ macro(break):
43
+ Atomy::AST::Break new(line, 'nil)
44
+
45
+ macro(break(x)):
46
+ Atomy::AST::Break new(line, x)
47
+
48
+ macro(next):
49
+ Atomy::AST::Next new(line, 'nil)
50
+
51
+ macro(next(x)):
52
+ Atomy::AST::Next new(line, x)
53
+
54
+ module(Atomy::AST):
55
+ class(Labels < Node):
56
+ children([#tags])
57
+ generate
58
+
59
+ bytecode(g) := do:
60
+ pos(g)
61
+
62
+ tags = ::Hash new
63
+
64
+ labels =
65
+ @tags collect [t]:
66
+ tags [t method-name] = g new-label
67
+
68
+ let(labels = tags):
69
+ labels zip(@tags) each [[l, t]]:
70
+ l set!
71
+ t block body bytecode(g)
72
+ g pop
73
+
74
+ g push-nil
75
+
76
+ class(GoTo < Node):
77
+ attributes(#name)
78
+ generate
79
+
80
+ bytecode(g) := do:
81
+ pos(g)
82
+ g goto(^labels fetch(@name))
83
+
84
+ class(ZSuper < Node):
85
+ generate
86
+
87
+ bytecode(g) := do:
88
+ pos(g)
89
+
90
+ g push-block
91
+
92
+ if(g state super?)
93
+ then:
94
+ g zsuper(g state super name)
95
+ else:
96
+ g zsuper(nil)
97
+
98
+ class(Super < Node):
99
+ children([#arguments], #block?)
100
+ generate
101
+
102
+ bytecode(g) := do:
103
+ pos(g)
104
+
105
+ block = @block
106
+ splat = nil
107
+
108
+ args = 0
109
+ @arguments each [a]:
110
+ e = a prepare
111
+ e match:
112
+ BlockPass -> do:
113
+ block =! e
114
+ -- break
115
+
116
+ Splat -> do:
117
+ splat =! e
118
+ -- break
119
+
120
+ _ -> do:
121
+ e bytecode(g)
122
+ args += 1
123
+
124
+ when(splat):
125
+ splat compile(g)
126
+ g cast-array
127
+
128
+ if(block)
129
+ then: block compile(g)
130
+ else: g push-nil
131
+
132
+ g send-super(nil, args, splat)
133
+
134
+ class(Yield < Node):
135
+ children([#arguments])
136
+ generate
137
+
138
+ bytecode(g) := do:
139
+ pos(g)
140
+
141
+ splat = nil
142
+
143
+ args = 0
144
+ @arguments each [a]:
145
+ e = a prepare
146
+ e match:
147
+ Splat -> do:
148
+ splat =! e
149
+ -- break
150
+
151
+ _ -> do:
152
+ e bytecode(g)
153
+ args += 1
154
+
155
+ if(splat)
156
+ then:
157
+ splat compile(g)
158
+ g cast-array
159
+ g yield-splat(args)
160
+ else:
161
+ g yield-stack(args)
162
+
163
+ class(Break < Node):
164
+ children(#value)
165
+ generate
166
+
167
+ bytecode(g) := do:
168
+ pos(g)
169
+ @value compile(g)
170
+ g raise-break
171
+
172
+ class(Next < Node):
173
+ children(#value)
174
+ generate
175
+
176
+ bytecode(g) := do:
177
+ pos(g)
178
+ @value compile(g)
179
+ g ret
180
+
181
+ macro(labels(&body)):
182
+ `(let-macro(go(name) = Atomy::AST::GoTo new(line, name name)):
183
+ ~(Atomy::AST::Labels new(line, body contents)))
184
+
185
+ macro(while(test, &body)):
186
+ names [loop, done]:
187
+ `(let-macro(break = 'go(~done),
188
+ next = 'go(~loop)):
189
+ labels:
190
+ ~loop:
191
+ unless(~test):
192
+ go(~done)
193
+
194
+ ~(body caller)
195
+ go(~loop)
196
+
197
+ ~done {})
198
+
199
+ macro(until(test, &body)):
200
+ names [loop, done]:
201
+ `(let-macro(break = 'go(~done),
202
+ next = 'go(~loop)):
203
+ labels:
204
+ ~loop:
205
+ when(~test):
206
+ go(~done)
207
+
208
+ ~(body caller)
209
+ go(~loop)
210
+
211
+ ~done {})
212
+
213
+ macro(loop(&body)):
214
+ names [loop, done]:
215
+ `(let-macro(break = 'go(~done),
216
+ next = 'go(~loop)):
217
+ labels:
218
+ ~loop:
219
+ ~(body caller)
220
+ go(~loop)
221
+
222
+ ~done {})
@@ -0,0 +1,3 @@
1
+ `($~(x: Atomy::AST::Block)) as-message(send) :=
2
+ send receiver to-send dup tap [s]:
3
+ s arguments += x contents
@@ -0,0 +1,105 @@
1
+ namespace(atomy)
2
+
3
+ macro(data(root, &children)):
4
+ cs = children contents map [e]:
5
+ e match:
6
+ Atomy::AST::Send -> do:
7
+ name =
8
+ Atomy::AST::Constant new(0, e method-name)
9
+
10
+ pat = name
11
+
12
+ tmps = names(e arguments size) collect [t]:
13
+ Atomy::AST::Unquote new(0, t)
14
+
15
+ e arguments zip(tmps) [[a, t]]:
16
+ pat =! `(~pat & with(~a, ~t))
17
+
18
+ cons = `(~(name)(~*tmps))
19
+
20
+ pat-def =
21
+ `(~(Atomy::AST::QuasiQuote new(0, cons)) pattern :=
22
+ ~(Atomy::AST::QuasiQuote new(0, pat)) pattern)
23
+
24
+ define =
25
+ `(export:
26
+ class(~name < ~root):
27
+ attr-accessor $:
28
+ ~*(e arguments collect [a]:
29
+ `#~(a receiver))
30
+
31
+ initialize(~*(e arguments)) := nil
32
+
33
+ ~pat-def)
34
+
35
+ if(e block)
36
+ then:
37
+ `(do:
38
+ ~define
39
+
40
+ data(~name):
41
+ ~*(e block contents))
42
+ else:
43
+ define
44
+
45
+ Atomy::AST::Constant ->
46
+ `(class(~e < ~root):)
47
+
48
+ Atomy::AST::ScopedConstant ->
49
+ `(class(~e < ~root):)
50
+
51
+ Atomy::AST::ToplevelConstant ->
52
+ `(class(~e < ~root):)
53
+
54
+ _ -> raise("unknown module name: " + e to-s)
55
+
56
+ `(do: class(~root):;, ~*cs, nil)
57
+
58
+
59
+ macro(macro-quoter(n, &x)):
60
+ `(evaluate-when(compile, load):
61
+ Atomy::Macro::Environment quoter(#~n, &~x))
62
+
63
+ macro-quoter(w) [c]: c split
64
+
65
+ macro-quoter(r) [c, fs]:
66
+ flags = 0
67
+
68
+ when(fs include?("m")):
69
+ flags |= Regexp::MULTILINE
70
+
71
+ when(fs include?("i")):
72
+ flags |= Regexp::IGNORECASE
73
+
74
+ when(fs include?("x")):
75
+ flags |= Regexp::EXTENDED
76
+
77
+ when(fs include?("u")):
78
+ flags |= Regexp::KCODE_UTF8
79
+
80
+ `(Regexp new(~c, ~flags))
81
+
82
+ macro-quoter(raw) [c]: c
83
+
84
+ macro(_undefined):
85
+ Atomy::AST::Undefined new(line)
86
+
87
+ macro($(x: Primitive ? @value is-a?(::Integer) && @value > 0)):
88
+ Atomy::AST::NthRef new(line, x value)
89
+
90
+ export:
91
+ module(Atomy::AST):
92
+ class(Undefined < Node):
93
+ generate
94
+
95
+ bytecode(g) := do:
96
+ pos(g)
97
+ g push-undef
98
+
99
+ pattern := Atomy::Patterns::Any new
100
+
101
+ class(NthRef < Rubinius::AST::NthRef):
102
+ include(NodeLike)
103
+ extend(SentientNode)
104
+ attributes(#which)
105
+ generate
data/kernel/data.ay ADDED
@@ -0,0 +1,56 @@
1
+ macro(&x):
2
+ Atomy::AST::BlockPass new(x line, x)
3
+
4
+ macro(*x):
5
+ Atomy::AST::Splat new(x line, x)
6
+
7
+ macro(@(x: Variable)):
8
+ Atomy::AST::InstanceVariable new(x line, x name)
9
+
10
+ macro(@@(x: Variable)):
11
+ Atomy::AST::ClassVariable new(x line, x name)
12
+
13
+ macro($'exception):
14
+ Atomy::AST::GlobalVariable new(line, "!")
15
+
16
+ macro($'path):
17
+ Atomy::AST::GlobalVariable new(line, ":")
18
+
19
+ macro($'separator):
20
+ Atomy::AST::GlobalVariable new(line, "/")
21
+
22
+ macro($'0):
23
+ Atomy::AST::GlobalVariable new(line, "0")
24
+
25
+ macro($(x: String)):
26
+ Atomy::AST::GlobalVariable new(line, x value)
27
+
28
+ macro($(x: Constant)):
29
+ Atomy::AST::GlobalVariable new(x line, x identifier)
30
+
31
+ macro($(x: Variable)):
32
+ Atomy::AST::GlobalVariable new(x line, x name)
33
+
34
+ macro(#(x: Constant)):
35
+ Atomy::AST::Particle new(x line, x identifier)
36
+
37
+ macro(#(x: Variable)):
38
+ Atomy::AST::Particle new(x line, x name)
39
+
40
+ macro(#(x: String)):
41
+ Atomy::AST::Particle new(x line, x value)
42
+
43
+ macro(#(x: Primitive)):
44
+ Atomy::AST::Particle new(x line, x value to-s)
45
+
46
+ macro(a .. b): `(Range new(~a, ~b))
47
+ macro(a ... b): `(Range new(~a, ~b, true))
48
+
49
+ macro(x for(e) in(c)): `(~c collect [~e]: ~x)
50
+
51
+ macro(x for(e) in(c) if(t)):
52
+ names [tmp]:
53
+ `([] tap [~tmp]:
54
+ ~c each [~e]:
55
+ when(~t):
56
+ ~tmp << ~x)
data/kernel/define.ay ADDED
@@ -0,0 +1,93 @@
1
+ macro(do(&b)): b body
2
+
3
+ macro(x = y):
4
+ Atomy::AST::Assign new(x line, x, y)
5
+
6
+ macro(x =! y):
7
+ Atomy::AST::Set new(x line, x, y)
8
+
9
+ macro(x := y):
10
+ where = Atomy::Namespace define-target
11
+ Atomy::Namespace register(x namespace-symbol, where)
12
+
13
+ Atomy::CodeLoader when-load <<
14
+ [`(Atomy::Namespace register(~(x namespace-symbol), ~where)), true]
15
+
16
+ Atomy::AST::Define new(x line, x, y, where)
17
+
18
+ macro(define(x, &y)):
19
+ `(~x := ~(y block-body))
20
+
21
+ macro(x **= y): `(~x =! (~x ** ~y))
22
+ macro(x *= y): `(~x =! (~x * ~y))
23
+ macro(x <<= y): `(~x =! (~x << ~y))
24
+ macro(x >>= y): `(~x =! (~x >> ~y))
25
+ macro(x &&= y): `(~x =! (~x && ~y))
26
+ macro(x &= y): `(~x =! (~x & ~y))
27
+ macro(x ||= y): `(~x =! (~x || ~y))
28
+ macro(x |= y): `(~x =! (~x | ~y))
29
+ macro(x += y): `(~x =! (~x + ~y))
30
+ macro(x -= y): `(~x =! (~x - ~y))
31
+ macro(x /= y): `(~x =! (~x / ~y))
32
+ macro(x ^= y): `(~x =! (~x ^ ~y))
33
+ macro(x %= y): `(~x =! (~x % ~y))
34
+
35
+ macro(x match(&b)):
36
+ Atomy::AST::Match new(line, x, b)
37
+
38
+ macro(class(`(<< ~obj), &body)):
39
+ Atomy::AST::SingletonClass new(obj line, obj, body block-body)
40
+
41
+ macro(class(`(~name < ~sup), &body)):
42
+ Atomy::AST::Class new(name line, name, body block-body, sup)
43
+
44
+ macro(class(name, &body)):
45
+ Atomy::AST::Class new(name line, name, body block-body)
46
+
47
+ macro(module(name, &body)):
48
+ Atomy::AST::Module new(name line, name, body block-body)
49
+
50
+ macro(evaluate-when('compile, 'run, 'load, &x)):
51
+ x body evaluate
52
+ Atomy::CodeLoader when-load << [x block-body, true]
53
+ `(when(!(Atomy::CodeLoader compiled?) &&
54
+ Atomy::CodeLoader reason == #run):
55
+ ~*(x contents))
56
+
57
+ macro(evaluate-when('run, 'compile, &x)):
58
+ x body evaluate
59
+ `(when(!(Atomy::CodeLoader compiled?) &&
60
+ Atomy::CodeLoader reason == #run):
61
+ ~*(x contents))
62
+
63
+ macro(evaluate-when('compile, 'run, &x)):
64
+ x body evaluate
65
+ `(when(!(Atomy::CodeLoader compiled?) &&
66
+ Atomy::CodeLoader reason == #run):
67
+ ~*(x contents))
68
+
69
+ macro(evaluate-when('compile, 'load, &x)):
70
+ x body evaluate
71
+ Atomy::CodeLoader when-load << [x block-body, true]
72
+ 'nil
73
+
74
+ macro(evaluate-when('compile, &x)):
75
+ x body evaluate
76
+ 'nil
77
+
78
+ macro(evaluate-when('run, &x)):
79
+ `(when(Atomy::CodeLoader reason == #run) { ~*(x contents) })
80
+
81
+ macro(evaluate-when('load, &x)):
82
+ Atomy::CodeLoader when-load << [x block-body, false]
83
+ 'nil
84
+
85
+ macro(for-macro(&b)):
86
+ `(evaluate-when(compile, load):
87
+ ~b block call-on-instance(Atomy::Macro::Environment singleton-class))
88
+
89
+ macro(let-macro(*ms, &body)):
90
+ macros = ms collect [`(~p = ~b)]:
91
+ Atomy::AST::Macro new(p line, p, b)
92
+
93
+ Atomy::AST::LetMacro new(line, body caller, macros)