fancy 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (242) hide show
  1. data/AUTHORS +7 -0
  2. data/LICENSE +19 -0
  3. data/README +173 -0
  4. data/Rakefile +255 -0
  5. data/bin/fancy +40 -0
  6. data/bin/fdoc +23 -0
  7. data/bin/fyi +22 -0
  8. data/bin/ifancy +46 -0
  9. data/boot/README +12 -0
  10. data/boot/code_loader.rb +165 -0
  11. data/boot/compile.fy +3 -0
  12. data/boot/fancy_ext.rb +13 -0
  13. data/boot/fancy_ext/block_env.rb +29 -0
  14. data/boot/fancy_ext/class.rb +26 -0
  15. data/boot/fancy_ext/kernel.rb +12 -0
  16. data/boot/fancy_ext/module.rb +89 -0
  17. data/boot/fancy_ext/object.rb +34 -0
  18. data/boot/fancy_ext/string_helper.rb +10 -0
  19. data/boot/load.rb +72 -0
  20. data/boot/rbx-compiler/README +12 -0
  21. data/boot/rbx-compiler/compiler.rb +24 -0
  22. data/boot/rbx-compiler/compiler/ast.rb +23 -0
  23. data/boot/rbx-compiler/compiler/ast/README +11 -0
  24. data/boot/rbx-compiler/compiler/ast/array_literal.rb +13 -0
  25. data/boot/rbx-compiler/compiler/ast/assign.rb +57 -0
  26. data/boot/rbx-compiler/compiler/ast/block.rb +70 -0
  27. data/boot/rbx-compiler/compiler/ast/class_def.rb +35 -0
  28. data/boot/rbx-compiler/compiler/ast/expression_list.rb +57 -0
  29. data/boot/rbx-compiler/compiler/ast/hash_literal.rb +11 -0
  30. data/boot/rbx-compiler/compiler/ast/identifier.rb +120 -0
  31. data/boot/rbx-compiler/compiler/ast/match.rb +81 -0
  32. data/boot/rbx-compiler/compiler/ast/message_send.rb +71 -0
  33. data/boot/rbx-compiler/compiler/ast/method_def.rb +116 -0
  34. data/boot/rbx-compiler/compiler/ast/node.rb +6 -0
  35. data/boot/rbx-compiler/compiler/ast/range_literal.rb +22 -0
  36. data/boot/rbx-compiler/compiler/ast/require.rb +20 -0
  37. data/boot/rbx-compiler/compiler/ast/return.rb +29 -0
  38. data/boot/rbx-compiler/compiler/ast/ruby_args.rb +35 -0
  39. data/boot/rbx-compiler/compiler/ast/script.rb +56 -0
  40. data/boot/rbx-compiler/compiler/ast/singleton_method_def.rb +39 -0
  41. data/boot/rbx-compiler/compiler/ast/string_literal.rb +14 -0
  42. data/boot/rbx-compiler/compiler/ast/super.rb +25 -0
  43. data/boot/rbx-compiler/compiler/ast/try_catch_block.rb +220 -0
  44. data/boot/rbx-compiler/compiler/ast/tuple_literal.rb +33 -0
  45. data/boot/rbx-compiler/compiler/command.rb +39 -0
  46. data/boot/rbx-compiler/compiler/compiler.rb +83 -0
  47. data/boot/rbx-compiler/compiler/stages.rb +99 -0
  48. data/boot/rbx-compiler/parser.rb +2 -0
  49. data/boot/rbx-compiler/parser/README +15 -0
  50. data/boot/rbx-compiler/parser/Rakefile +54 -0
  51. data/boot/rbx-compiler/parser/extconf.rb +3 -0
  52. data/boot/rbx-compiler/parser/fancy_parser.bundle +0 -0
  53. data/boot/rbx-compiler/parser/fancy_parser.c +46 -0
  54. data/boot/rbx-compiler/parser/fancy_parser.h +8 -0
  55. data/boot/rbx-compiler/parser/lexer.lex +180 -0
  56. data/boot/rbx-compiler/parser/parser.rb +356 -0
  57. data/boot/rbx-compiler/parser/parser.y +711 -0
  58. data/boot/rsexp_pretty_printer.rb +76 -0
  59. data/doc/api/fancy.css +93 -0
  60. data/doc/api/fancy.jsonp +1 -0
  61. data/doc/api/fdoc.js +187 -0
  62. data/doc/api/index.html +57 -0
  63. data/doc/api/underscore-min.js +18 -0
  64. data/doc/features.md +228 -0
  65. data/examples/argv.fy +8 -0
  66. data/examples/arithmetic.fy +7 -0
  67. data/examples/armstrong_numbers.fy +33 -0
  68. data/examples/array.fy +52 -0
  69. data/examples/blocks.fy +15 -0
  70. data/examples/boolean.fy +24 -0
  71. data/examples/call_with_receiver.fy +9 -0
  72. data/examples/class.fy +68 -0
  73. data/examples/closures.fy +24 -0
  74. data/examples/constant_access.fy +15 -0
  75. data/examples/default_args.fy +17 -0
  76. data/examples/define_methods.fy +15 -0
  77. data/examples/documentation.fy +57 -0
  78. data/examples/documentation_formatters.fy +25 -0
  79. data/examples/echo.fy +16 -0
  80. data/examples/empty_catch.fy +4 -0
  81. data/examples/exception.fy +9 -0
  82. data/examples/factorial.fy +12 -0
  83. data/examples/fibonacci.fy +16 -0
  84. data/examples/files.fy +23 -0
  85. data/examples/finally.fy +5 -0
  86. data/examples/game_of_life.fy +148 -0
  87. data/examples/hashes.fy +7 -0
  88. data/examples/hello_world.fy +6 -0
  89. data/examples/html_generator.fy +54 -0
  90. data/examples/implicit_return.fy +3 -0
  91. data/examples/matchers.fy +6 -0
  92. data/examples/methods.fy +29 -0
  93. data/examples/nested_classes.fy +27 -0
  94. data/examples/nested_try.fy +9 -0
  95. data/examples/numbers.fy +12 -0
  96. data/examples/pattern_matching.fy +40 -0
  97. data/examples/person.fy +65 -0
  98. data/examples/project-euler/01.fy +8 -0
  99. data/examples/project-euler/02.fy +21 -0
  100. data/examples/project-euler/28.fy +33 -0
  101. data/examples/rbx/and_or.fy +7 -0
  102. data/examples/rbx/blocks.fy +22 -0
  103. data/examples/rbx/classes.fy +32 -0
  104. data/examples/rbx/hello.fy +8 -0
  105. data/examples/rbx/include.fy +12 -0
  106. data/examples/rbx/inherit.fy +11 -0
  107. data/examples/rbx/methods.fy +15 -0
  108. data/examples/rbx/nested_classes.fy +9 -0
  109. data/examples/rbx/require.fy +3 -0
  110. data/examples/rbx/strings.fy +5 -0
  111. data/examples/regex.fy +7 -0
  112. data/examples/require.fy +7 -0
  113. data/examples/retry.fy +12 -0
  114. data/examples/return.fy +13 -0
  115. data/examples/ruby_require.fy +7 -0
  116. data/examples/ruby_send.fy +3 -0
  117. data/examples/singleton_methods.fy +21 -0
  118. data/examples/stupid_quicksort.fy +12 -0
  119. data/examples/threads.fy +18 -0
  120. data/examples/tuple.fy +8 -0
  121. data/examples/webserver/webserver.fy +18 -0
  122. data/lib/argv.fy +36 -0
  123. data/lib/array.fy +207 -0
  124. data/lib/block.fy +88 -0
  125. data/lib/boot.fy +41 -0
  126. data/lib/class.fy +106 -0
  127. data/lib/compiler.fy +14 -0
  128. data/lib/compiler/ast.fy +40 -0
  129. data/lib/compiler/ast/assign.fy +96 -0
  130. data/lib/compiler/ast/block.fy +84 -0
  131. data/lib/compiler/ast/class_def.fy +33 -0
  132. data/lib/compiler/ast/expression_list.fy +47 -0
  133. data/lib/compiler/ast/identifier.fy +113 -0
  134. data/lib/compiler/ast/literals.fy +122 -0
  135. data/lib/compiler/ast/match.fy +88 -0
  136. data/lib/compiler/ast/message_send.fy +110 -0
  137. data/lib/compiler/ast/method_def.fy +90 -0
  138. data/lib/compiler/ast/node.fy +7 -0
  139. data/lib/compiler/ast/range.fy +16 -0
  140. data/lib/compiler/ast/require.fy +15 -0
  141. data/lib/compiler/ast/return.fy +23 -0
  142. data/lib/compiler/ast/script.fy +52 -0
  143. data/lib/compiler/ast/singleton_method_def.fy +35 -0
  144. data/lib/compiler/ast/super.fy +17 -0
  145. data/lib/compiler/ast/try_catch.fy +176 -0
  146. data/lib/compiler/ast/tuple_literal.fy +34 -0
  147. data/lib/compiler/command.fy +51 -0
  148. data/lib/compiler/compiler.fy +73 -0
  149. data/lib/compiler/stages.fy +81 -0
  150. data/lib/directory.fy +17 -0
  151. data/lib/documentation.fy +115 -0
  152. data/lib/enumerable.fy +269 -0
  153. data/lib/eval.fy +31 -0
  154. data/lib/fancy_spec.fy +202 -0
  155. data/lib/fdoc.fy +359 -0
  156. data/lib/fdoc_hook.fy +10 -0
  157. data/lib/file.fy +54 -0
  158. data/lib/hash.fy +56 -0
  159. data/lib/main.fy +80 -0
  160. data/lib/method.fy +22 -0
  161. data/lib/nil_class.fy +56 -0
  162. data/lib/number.fy +87 -0
  163. data/lib/object.fy +170 -0
  164. data/lib/package.fy +61 -0
  165. data/lib/package/dependency.fy +24 -0
  166. data/lib/package/installer.fy +180 -0
  167. data/lib/package/specification.fy +55 -0
  168. data/lib/package/uninstaller.fy +15 -0
  169. data/lib/parser.fy +4 -0
  170. data/lib/parser/ext/README +15 -0
  171. data/lib/parser/ext/ext.c +42 -0
  172. data/lib/parser/ext/ext.h +8 -0
  173. data/lib/parser/ext/extconf.rb +3 -0
  174. data/lib/parser/ext/lexer.lex +187 -0
  175. data/lib/parser/ext/parser.y +744 -0
  176. data/lib/parser/methods.fy +297 -0
  177. data/lib/rbx.fy +37 -0
  178. data/lib/rbx/array.fy +237 -0
  179. data/lib/rbx/bignum.fy +23 -0
  180. data/lib/rbx/block.fy +9 -0
  181. data/lib/rbx/class.fy +129 -0
  182. data/lib/rbx/code_loader.fy +192 -0
  183. data/lib/rbx/console.fy +63 -0
  184. data/lib/rbx/directory.fy +46 -0
  185. data/lib/rbx/documentation.fy +64 -0
  186. data/lib/rbx/environment_variables.fy +3 -0
  187. data/lib/rbx/exception.fy +30 -0
  188. data/lib/rbx/false_class.fy +58 -0
  189. data/lib/rbx/fiber.fy +25 -0
  190. data/lib/rbx/file.fy +191 -0
  191. data/lib/rbx/fixnum.fy +25 -0
  192. data/lib/rbx/float.fy +14 -0
  193. data/lib/rbx/hash.fy +38 -0
  194. data/lib/rbx/integer.fy +15 -0
  195. data/lib/rbx/io.fy +30 -0
  196. data/lib/rbx/match_data.fy +9 -0
  197. data/lib/rbx/method.fy +22 -0
  198. data/lib/rbx/name_error.fy +3 -0
  199. data/lib/rbx/no_method_error.fy +15 -0
  200. data/lib/rbx/object.fy +117 -0
  201. data/lib/rbx/range.fy +15 -0
  202. data/lib/rbx/regexp.fy +9 -0
  203. data/lib/rbx/string.fy +63 -0
  204. data/lib/rbx/symbol.fy +12 -0
  205. data/lib/rbx/system.fy +37 -0
  206. data/lib/rbx/tcp_server.fy +6 -0
  207. data/lib/rbx/tcp_socket.fy +7 -0
  208. data/lib/rbx/thread.fy +75 -0
  209. data/lib/rbx/tuple.fy +37 -0
  210. data/lib/set.fy +61 -0
  211. data/lib/stack.fy +51 -0
  212. data/lib/string.fy +58 -0
  213. data/lib/struct.fy +13 -0
  214. data/lib/symbol.fy +23 -0
  215. data/lib/true_class.fy +43 -0
  216. data/lib/tuple.fy +68 -0
  217. data/lib/version.fy +6 -0
  218. data/tests/argv.fy +13 -0
  219. data/tests/array.fy +343 -0
  220. data/tests/assignment.fy +53 -0
  221. data/tests/block.fy +103 -0
  222. data/tests/class.fy +409 -0
  223. data/tests/control_flow.fy +79 -0
  224. data/tests/documentation.fy +24 -0
  225. data/tests/exception.fy +115 -0
  226. data/tests/file.fy +86 -0
  227. data/tests/hash.fy +101 -0
  228. data/tests/method.fy +131 -0
  229. data/tests/nil_class.fy +55 -0
  230. data/tests/number.fy +128 -0
  231. data/tests/object.fy +125 -0
  232. data/tests/parsing/sexp.fy +50 -0
  233. data/tests/pattern_matching.fy +82 -0
  234. data/tests/range.fy +11 -0
  235. data/tests/set.fy +10 -0
  236. data/tests/stack.fy +22 -0
  237. data/tests/string.fy +102 -0
  238. data/tests/symbol.fy +17 -0
  239. data/tests/true_class.fy +63 -0
  240. data/tests/tuple.fy +21 -0
  241. data/tools/fancy-mode.el +63 -0
  242. metadata +321 -0
@@ -0,0 +1,297 @@
1
+ class Fancy {
2
+ class Parser {
3
+
4
+ def self parse_file: filename line: line (1) {
5
+ new: filename line: line . parse_file . script
6
+ }
7
+
8
+ def self parse_code: code file: filename line: line (1) {
9
+ new: filename line: line . parse_string: code . script
10
+ }
11
+
12
+ read_write_slots: ['filename, 'line, 'script]
13
+
14
+ def initialize: @filename line: @line { }
15
+
16
+ def body: body {
17
+ @script = AST Script new: @line file: @filename body: body
18
+ }
19
+
20
+ def ast: line exp_list: expr into: list (AST ExpressionList new: line) {
21
+ { list expressions << expr } if: expr
22
+ list
23
+ }
24
+
25
+ def ast: line identity: identity { identity }
26
+
27
+ def ast: line concat: object into: ary ([]) {
28
+ if: (object kind_of?(Array)) then: {
29
+ ary concat(object)
30
+ } else: {
31
+ { ary << object } if: object
32
+ }
33
+ ary
34
+ }
35
+
36
+ def ast: line key: key value: value into: ary {
37
+ ary if_nil: { ary = [] }
38
+ ary << key
39
+ ary << value
40
+ ary
41
+ }
42
+
43
+ def ast: line fixnum: text base: base (10) {
44
+ AST FixnumLiteral new: line value: (text to_i(base))
45
+ }
46
+
47
+ def ast: line number: text base: base (10) {
48
+ AST NumberLiteral new: line value: (text to_f())
49
+ }
50
+
51
+ def ast: line symbol: text {
52
+ str = text from: 1 to: -1
53
+ AST SymbolLiteral new: line value: (str to_sym())
54
+ }
55
+
56
+ def ast: line regexp: text {
57
+ regexp = text from: 1 to: -2
58
+ AST RegexpLiteral new: line value: regexp
59
+ }
60
+
61
+ def ast: line string: text {
62
+ str = text from: 1 to: -2
63
+ match str -> {
64
+ # OK, I know this is ugly. But it works for now, so let's just go with it.
65
+ # TODO: Clean this up or make it simpler...
66
+
67
+ # this case handles string interpolation
68
+ case /(.*)#{(.*)}(.*)/ -> |matches|
69
+ prefix = matches[1]
70
+ interpol_str = matches[2]
71
+ suffix = matches[3]
72
+
73
+ binding = AST MessageSend new: line message: (ast: line identifier: "binding") to: (AST Self new: line) args: (AST RubyArgs new: line args: [])
74
+ evalstr = AST StringLiteral new: line value: interpol_str
75
+ msg = ast: line identifier: "eval:binding:"
76
+ binding_send = AST MessageSend new: line message: msg to: (ast: line identifier: "Fancy") \
77
+ args: (AST MessageArgs new: line args: [evalstr, binding])
78
+
79
+ prefix_str = ast: line string: (" " + prefix + " ") # hack, pre- & append " " since it gets removed
80
+ suffix_str = ast: line string: (" " + suffix + " ")
81
+ # create messagesend to concatenate:
82
+ concat_ident = ast: line identifier: "++"
83
+ concat_prefix_send = AST MessageSend new: line message: concat_ident to: prefix_str args: (AST MessageArgs new: line args: [binding_send])
84
+ concat_suffix_send = AST MessageSend new: line message: concat_ident to: concat_prefix_send args: (AST MessageArgs new: line args: [suffix_str])
85
+
86
+ concat_suffix_send # this shall get returned, yo
87
+ case _ ->
88
+ AST StringLiteral new: line value: str
89
+ }
90
+ }
91
+
92
+ def ast: line array: expr_ary {
93
+ AST ArrayLiteral new: line array: expr_ary
94
+ }
95
+
96
+ def ast: line hash: key_values {
97
+ AST HashLiteral new: line entries: key_values
98
+ }
99
+
100
+ def ast: line tuple: expr_ary {
101
+ if: (expr_ary size == 1) then: {
102
+ expr_ary first
103
+ } else: {
104
+ AST TupleLiteral new: line entries: expr_ary
105
+ }
106
+ }
107
+
108
+ def ast: line range: from to: to {
109
+ AST RangeLiteral new: line from: from to: to
110
+ }
111
+
112
+ def ast: line identifier: text {
113
+ AST Identifier from: text line: line filename: @filename
114
+ }
115
+
116
+ def ast: line constant: identifier parent: parent {
117
+ AST NestedConstant new: line const: identifier parent: parent
118
+ }
119
+
120
+ def ast: line super_exp: text { AST Super new: line }
121
+
122
+ def ast: line retry_exp: text { AST Retry new: line }
123
+
124
+ def ast: line return_stmt: exp {
125
+ { exp = AST NilLiteral new: exp } if: (exp nil?)
126
+ AST Return new: line expr: exp
127
+ }
128
+
129
+ def ast: line return_local_stmt: exp {
130
+ { exp = AST NilLiteral new: exp } if: (exp nil?)
131
+ AST ReturnLocal new: line expr: exp
132
+ }
133
+
134
+ def ast: line assign: rvalue to: lvalue many: many (false) {
135
+ ast = many if_do: { AST MultipleAssignment } else: { AST Assignment }
136
+ ast new: line var: lvalue value: rvalue
137
+ }
138
+
139
+ def ast: line param: selector var: variable default: default (nil) {
140
+ Struct.new('selector, 'variable, 'default) new(selector, variable, default)
141
+ }
142
+
143
+ def ast: line send: selector arg: value ary: ary ([]) {
144
+ ary << $ Struct new('selector, 'value) new(selector, value)
145
+ }
146
+
147
+ def ast: line oper: oper arg: arg to: receiver (AST Self new: line) {
148
+ message = ast: line send: oper arg: arg
149
+ ast: line send: message to: receiver
150
+ }
151
+
152
+ def ast: line send: message to: receiver (AST Self new: line) ruby: ruby (nil) {
153
+ args = ruby if_do: {
154
+ unless: receiver do: {
155
+ receiver = AST Self new: line
156
+ }
157
+ ruby
158
+ } else: {
159
+ AST MessageArgs new: line args: []
160
+ }
161
+ name = message
162
+ if: (message kind_of?(String)) then: {
163
+ name = AST Identifier from: message line: line
164
+ }
165
+ if: (message kind_of?(Array)) then: {
166
+ name = message map: |m| { m selector() string } . join
167
+ name = AST Identifier new: line string: name
168
+ args = message map: |m| { m value() }
169
+ args = AST MessageArgs new: line args: args
170
+ }
171
+ AST MessageSend new: line message: name to: receiver args: args
172
+ }
173
+
174
+ def method_name: margs {
175
+ margs map: |a| { a selector() string } . join("")
176
+ }
177
+
178
+ def method: margs delegators: block {
179
+ idx = margs index() |m| { m default() != nil }
180
+ if: idx then: {
181
+ line = margs first selector() line
182
+ target = method_name: margs
183
+ (margs size - idx) times: |pos| {
184
+ required = margs from: 0 to: (idx + pos - 1)
185
+ default = margs from: (idx + pos) to: -1
186
+ params = required map: |r| { r variable() } . + $ default map: |d| { d default() }
187
+
188
+ forward = AST MessageSend new: line \
189
+ message: (AST Identifier from: target line: line) \
190
+ to: (AST Self new: line) \
191
+ args:(AST MessageArgs new: line args: params)
192
+
193
+ doc = AST StringLiteral new: line value: ("Forward to message " ++ target)
194
+ body = AST ExpressionList new: line list: [doc, forward]
195
+ block call: [[required, body]]
196
+ }
197
+ }
198
+ }
199
+
200
+ def ast: line oper: op arg: arg body: body access: access ('public) owner: owner (nil) {
201
+ margs = [ast: line param: op var: arg]
202
+ ast: line method: margs body: body access: access owner: owner
203
+ }
204
+
205
+ def ast: line method: margs body: body access: access ('public) owner: owner (nil) {
206
+ if: (margs is_a?(AST Identifier)) then: {
207
+ args = AST MethodArgs new: line args: []
208
+ if: owner then: {
209
+ AST SingletonMethodDef new: line name: margs args: args \
210
+ body: body access: access owner: owner
211
+ } else: {
212
+ AST MethodDef new: line name: margs args: args body: body access: access
213
+ }
214
+ } else: {
215
+ name = method_name: margs
216
+ name = AST Identifier new: line string: name
217
+ args = margs map() |m| { m variable() string }
218
+ args = AST MethodArgs new: line args: args
219
+ if: owner then: {
220
+ AST SingletonMethodDef new: line name: name args: args \
221
+ body: body access: access owner: owner
222
+ } else: {
223
+ AST MethodDef new: line name: name args: args body: body access: access
224
+ }
225
+ }
226
+ }
227
+
228
+ def ast: line method: margs expand: body access: access ('public) owner: owner (nil) {
229
+ defs = []
230
+ method: margs delegators: |sel fwd| {
231
+ defs << $ ast: line method: sel body: fwd access: access owner: owner
232
+ }
233
+ defs << $ ast: line method: margs body: body access: access owner: owner
234
+ AST ExpressionList new: line list: defs
235
+ }
236
+
237
+ def ast: line block: body {
238
+ args = AST BlockArgs new: line
239
+ AST BlockLiteral new: line args: args body: body
240
+ }
241
+
242
+ def ast: line partial_block: body {
243
+ gen_blockarg = AST Identifier generate: line
244
+ args = AST BlockArgs new: line args: [gen_blockarg]
245
+ AST BlockLiteral new: line args: args body: body partial: true
246
+ }
247
+
248
+ def ast: line block: body args: args {
249
+ args = AST BlockArgs new: line args: args
250
+ AST BlockLiteral new: line args: args body: body
251
+ }
252
+
253
+ def ast: line require_: file {
254
+ AST Require new: line file: file
255
+ }
256
+
257
+ def ast: line class: name parent: parent body: body {
258
+ AST ClassDef new: line name: name parent: parent body: body
259
+ }
260
+
261
+ def ast: line match_expr: expr body: match_body {
262
+ AST Match new: line expr: expr body: match_body
263
+ }
264
+
265
+ def ast: line match_clause: expr body: body args: match_args ([]) {
266
+ AST MatchClause new: line expr: expr body: body args: match_args
267
+ }
268
+
269
+ def ast: line ex_handler: expr_list cond: cond (AST Identifier from: "Object" line: line) var: var (nil) {
270
+ AST ExceptionHandler new: line condition: cond var: var body: expr_list
271
+ }
272
+
273
+ def ast: line try_block: body ex_handlers: handlers finally_block: finaly (AST NilLiteral new: line) {
274
+ AST TryCatch new: line body: body handlers: handlers ensure: finaly
275
+ }
276
+
277
+ def ast: line ruby_send: text {
278
+ name = text from: 0 to: -2 # remove the open left paren
279
+ ast: line identifier: name
280
+ }
281
+
282
+ def ast: line ruby_args: args block: block (nil) {
283
+ { args = [] } unless: args
284
+ AST RubyArgs new: line args: args block: block
285
+ }
286
+
287
+ def ast: line parse_error: text {
288
+ ("Parse error near `" ++ text ++ "' at line " ++ line ++ " at " ++ @filename) . raise!
289
+ }
290
+
291
+ def ast: line file_error: text {
292
+ ("File error `" ++ text ++ "' while trying to parse " ++ @filename) . raise!
293
+ }
294
+
295
+ }
296
+ }
297
+
data/lib/rbx.fy ADDED
@@ -0,0 +1,37 @@
1
+ # rbx.fy
2
+ # This file loads all the rubinius-specific class & method definition
3
+ # files from the lib/rbx/ directory in the correct order.
4
+
5
+ # NOTE:
6
+ # Don't change the order in here, unless you know what you're doing.
7
+
8
+ require: "rbx/documentation"
9
+ require: "rbx/object"
10
+ require: "rbx/class"
11
+ require: "rbx/console"
12
+ require: "rbx/array"
13
+ require: "rbx/hash"
14
+ require: "rbx/false_class"
15
+ require: "rbx/string"
16
+ require: "rbx/symbol"
17
+ require: "rbx/integer"
18
+ require: "rbx/fixnum"
19
+ require: "rbx/float"
20
+ require: "rbx/bignum"
21
+ require: "rbx/block"
22
+ require: "rbx/tuple"
23
+ require: "rbx/range"
24
+ require: "rbx/system"
25
+ require: "rbx/exception"
26
+ require: "rbx/io"
27
+ require: "rbx/file"
28
+ require: "rbx/tcp_server"
29
+ require: "rbx/tcp_socket"
30
+ require: "rbx/regexp"
31
+ require: "rbx/directory"
32
+ require: "rbx/method"
33
+ require: "rbx/environment_variables"
34
+ require: "rbx/name_error"
35
+ require: "rbx/no_method_error"
36
+ require: "rbx/match_data"
37
+ require: "rbx/thread"
data/lib/rbx/array.fy ADDED
@@ -0,0 +1,237 @@
1
+ class Array {
2
+ ruby_alias: '==
3
+ ruby_alias: '<<
4
+ ruby_alias: 'clear
5
+ ruby_alias: 'size
6
+ ruby_alias: 'reverse
7
+ ruby_alias: 'reverse!
8
+ ruby_alias: 'sort
9
+ ruby_alias: 'pop
10
+ ruby_alias: 'last
11
+
12
+ def Array new: size with: default {
13
+ "Creates a new Array with a given size and default-value."
14
+
15
+ Array new(size, default)
16
+ }
17
+
18
+ def Array new: size {
19
+ "Creates a new Array with a given size (default value is nil)."
20
+
21
+ Array new: size with: nil
22
+ }
23
+
24
+ def append: arr {
25
+ "Appends another Array onto this one."
26
+
27
+ arr each: |x| {
28
+ self << x
29
+ }
30
+ }
31
+
32
+ def includes?: obj {
33
+ "Indicates, if an Array includes a given value."
34
+
35
+ self include?(obj)
36
+ }
37
+
38
+ def clone {
39
+ "Clones (shallow copy) the Array."
40
+ new = []
41
+ each: |x| {
42
+ new << x
43
+ }
44
+ new
45
+ }
46
+
47
+ def each: block {
48
+ "Calls a given Block with each element in the Array."
49
+
50
+ val = nil
51
+ each() |x| { val = block call: [x] }
52
+ val
53
+ }
54
+
55
+ def remove_at: index {
56
+ """Removes an element at a given index.
57
+ If given an Array of indices, removes all the elements with these indices.
58
+ Returns the deleted object if an index was given, the last deleted object for an Array given."""
59
+
60
+ if: (index is_a?: Fixnum) then: {
61
+ deleted = self at: index
62
+ delete_at(index)
63
+ return deleted
64
+ } else: {
65
+ if: (index is_a?: Array) then: {
66
+ count = 0
67
+ deleted_values = []
68
+ index each: |idx| {
69
+ deleted_values << (self at: (idx - count))
70
+ delete_at(idx - count)
71
+ count = count + 1
72
+ }
73
+ return deleted_values
74
+ }
75
+ }
76
+ nil
77
+ }
78
+
79
+ def at: idx {
80
+ "Returns the element in the Array at a given index."
81
+
82
+ ruby: '[] args: [idx]
83
+ }
84
+
85
+ def at: idx put: obj {
86
+ "Inserts a given object at a given index (position) in the Array."
87
+
88
+ ruby: '[]= args: [idx, obj]
89
+ }
90
+
91
+ def first {
92
+ "Returns the first element in the Array."
93
+ at: 0
94
+ }
95
+
96
+ def second {
97
+ "Returns the second element in the Array"
98
+ at: 1
99
+ }
100
+
101
+ def third {
102
+ "Returns the third element in the Array"
103
+ at: 2
104
+ }
105
+
106
+ def fourth {
107
+ "Returns the fourth element in the Array"
108
+ at: 3
109
+ }
110
+
111
+ def each_with_index: block {
112
+ "Iterate over all elements in Array. Calls a given Block with each element and its index."
113
+
114
+ i = 0
115
+ each: |x| {
116
+ block call: [x, i]
117
+ i = i + 1
118
+ }
119
+ }
120
+
121
+ def index: item {
122
+ "Returns the index of an item (or nil, if it isn't in the Array)."
123
+ index(item)
124
+ }
125
+
126
+ def indices_of: item {
127
+ "Returns an Array of all indices of this item. Empty Array if item does not occur."
128
+
129
+ tmp = []
130
+ each_with_index: |obj, idx| {
131
+ if: (item == obj) then: {
132
+ tmp << idx
133
+ }
134
+ }
135
+ tmp
136
+ }
137
+
138
+ def from: from to: to {
139
+ "Returns sub-array starting at from: and going to to:"
140
+
141
+ if: (from < 0) then: {
142
+ from = self size + from
143
+ }
144
+ if: (to < 0) then: {
145
+ to = self size + to
146
+ }
147
+ subarr = []
148
+ from upto: to do_each: |i| {
149
+ subarr << (self at: i)
150
+ }
151
+ subarr
152
+ }
153
+
154
+ def last: count {
155
+ "Returns new Array with last n elements specified."
156
+ last(count)
157
+ }
158
+
159
+ def any?: block {
160
+ "Takes condition-block and returns true if any element meets it."
161
+ any?(&block)
162
+ }
163
+
164
+ def all?: block {
165
+ "Takes condition-block and returns true if all elements meet it."
166
+ all?(&block)
167
+ }
168
+
169
+ def select: block {
170
+ """
171
+ Returns a new Array with all the elements in self that yield a
172
+ true-ish value when called with the given Block.
173
+ """
174
+
175
+ tmp = []
176
+ each: |x| {
177
+ if: (block call: [x]) then: {
178
+ tmp << x
179
+ }
180
+ }
181
+ return tmp
182
+ }
183
+
184
+ def select_with_index: block {
185
+ """
186
+ Same as select, just gets also called with an additional argument
187
+ for each element's index value.
188
+ """
189
+
190
+ tmp = []
191
+ each_with_index: |obj idx| {
192
+ if: (block call: [obj, idx]) then: {
193
+ tmp << [obj, idx]
194
+ }
195
+ }
196
+ tmp
197
+ }
198
+
199
+ def reject: block {
200
+ """
201
+ Returns a new Array with all the elements which yield nil or false
202
+ when called with the given Block.
203
+ """
204
+
205
+ reject(&block)
206
+ }
207
+
208
+ def reject!: block {
209
+ "Same as Array#reject: but doing so in-place (destructive)."
210
+
211
+ reject!(&block)
212
+ return self
213
+ }
214
+
215
+ def join: join_str {
216
+ """Joins all elements in the Array by a given String.
217
+ E.g.: [1,2,3] join: ', ' # => '1,2,3'"""
218
+
219
+ join(join_str)
220
+ }
221
+
222
+ def sum {
223
+ """
224
+ Calculates the sum of all the elements in the Enumerable
225
+ (assuming them to be Numbers (implementing '+' & '*')).
226
+ """
227
+
228
+ reduce: |x y| { x + y } init_val: 0
229
+ }
230
+
231
+ def product {
232
+ """Calculates the product of all the elements in the Enumerable
233
+ (assuming them to be Numbers (implementing '+' & '*'))."""
234
+
235
+ reduce: |x y| { x * y } init_val: 1
236
+ }
237
+ }