rbf 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rbf/interpreter.rb +13 -8
- data/lib/rbf.rb +7 -7
- metadata +1 -1
data/lib/rbf/interpreter.rb
CHANGED
@@ -89,6 +89,15 @@ class Interpreter
|
|
89
89
|
@output = STDOUT
|
90
90
|
end
|
91
91
|
|
92
|
+
cycle(tree)
|
93
|
+
|
94
|
+
if options[:catch]
|
95
|
+
@output.rewind
|
96
|
+
@output.read
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def cycle (tree)
|
92
101
|
tree.each {|token|
|
93
102
|
if token.is_a?(Array)
|
94
103
|
self.loop(token)
|
@@ -96,10 +105,11 @@ class Interpreter
|
|
96
105
|
self.send(token)
|
97
106
|
end
|
98
107
|
}
|
108
|
+
end
|
99
109
|
|
100
|
-
|
101
|
-
|
102
|
-
|
110
|
+
def loop (tree)
|
111
|
+
while @storage.get != 0
|
112
|
+
cycle(tree)
|
103
113
|
end
|
104
114
|
end
|
105
115
|
|
@@ -128,11 +138,6 @@ class Interpreter
|
|
128
138
|
@storage.set @input.read_char
|
129
139
|
end
|
130
140
|
|
131
|
-
define_method :loop do |code|
|
132
|
-
while @storage.get != 0
|
133
|
-
evaluate(code)
|
134
|
-
end
|
135
|
-
end
|
136
141
|
end
|
137
142
|
|
138
143
|
end
|
data/lib/rbf.rb
CHANGED
@@ -18,18 +18,18 @@ require 'rbf/interpreter'
|
|
18
18
|
require 'parslet/convenience'
|
19
19
|
|
20
20
|
module RBF
|
21
|
-
def self.parse (text,
|
22
|
-
Transform.new.apply(Parser.apply(
|
21
|
+
def self.parse (text, syntax=nil)
|
22
|
+
Transform.new.apply(Parser.apply(syntax || Syntax::Default).new.parse_with_debug(text)) or
|
23
23
|
raise SyntaxError, 'There is a syntax error'
|
24
24
|
end
|
25
25
|
|
26
|
-
def self.evaluate (text,
|
27
|
-
tree = text.is_a?(Array) ? text : parse(text.to_s,
|
26
|
+
def self.evaluate (text, options={})
|
27
|
+
tree = text.is_a?(Array) ? text : parse(text.to_s, options[:syntax])
|
28
28
|
|
29
|
-
Interpreter.new.evaluate(tree)
|
29
|
+
Interpreter.new.evaluate(tree, options)
|
30
30
|
end
|
31
31
|
|
32
|
-
def self.execute (file,
|
33
|
-
evaluate(File.read(file),
|
32
|
+
def self.execute (file, options={})
|
33
|
+
evaluate(File.read(file), options)
|
34
34
|
end
|
35
35
|
end
|