babel_bridge 0.5.1 → 0.5.3
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGE_LOG +165 -0
- data/Gemfile +4 -0
- data/Guardfile +7 -0
- data/LICENCE +24 -0
- data/README.md +244 -0
- data/Rakefile +8 -2
- data/TODO +100 -0
- data/babel_bridge.gemspec +11 -3
- data/examples/json/json_parser.rb +23 -0
- data/examples/json/json_parser2.rb +37 -0
- data/lib/babel_bridge.rb +3 -2
- data/lib/{nodes.rb → babel_bridge/nodes.rb} +0 -0
- data/lib/{nodes → babel_bridge/nodes}/empty_node.rb +0 -0
- data/lib/{nodes → babel_bridge/nodes}/node.rb +1 -1
- data/lib/{nodes → babel_bridge/nodes}/non_terminal_node.rb +0 -8
- data/lib/{nodes → babel_bridge/nodes}/root_node.rb +0 -0
- data/lib/{nodes → babel_bridge/nodes}/rule_node.rb +0 -0
- data/lib/{nodes → babel_bridge/nodes}/terminal_node.rb +0 -0
- data/lib/{parser.rb → babel_bridge/parser.rb} +7 -14
- data/lib/{pattern_element.rb → babel_bridge/pattern_element.rb} +27 -25
- data/lib/babel_bridge/pattern_element_hash.rb +22 -0
- data/lib/{rule.rb → babel_bridge/rule.rb} +0 -0
- data/lib/{rule_variant.rb → babel_bridge/rule_variant.rb} +0 -4
- data/lib/{shell.rb → babel_bridge/shell.rb} +0 -0
- data/lib/{string.rb → babel_bridge/string.rb} +0 -0
- data/lib/{tools.rb → babel_bridge/tools.rb} +0 -0
- data/lib/babel_bridge/version.rb +3 -0
- data/spec/advanced_parsers_spec.rb +1 -0
- data/spec/basic_parsing_spec.rb +43 -0
- data/spec/bb_spec.rb +19 -0
- data/spec/compound_patterns_spec.rb +61 -0
- data/spec/node_spec.rb +3 -3
- data/spec/pattern_generators_spec.rb +4 -4
- data/spec/spec_helper.rb +3 -0
- metadata +115 -33
- data/README +0 -144
- data/examples/turing/examples.turing +0 -33
- data/examples/turing/notes.rb +0 -111
- data/examples/turing/turing_demo.rb +0 -71
- data/lib/version.rb +0 -4
@@ -1,33 +0,0 @@
|
|
1
|
-
# this language doesn't support comments, should copy-paste the examples between
|
2
|
-
# the comments
|
3
|
-
|
4
|
-
# calculate 2^[0]
|
5
|
-
[0]=10;[1]=1;while [0]>0 do [0]=[0]-1;[1]=[1]*2 end;[1]
|
6
|
-
|
7
|
-
# return the [0]th number in the fibonancci sequence:
|
8
|
-
# [0] == fib number to output
|
9
|
-
# [1] == current fib number
|
10
|
-
# [2] == fib[[1]]
|
11
|
-
# [3] == fib[[1]-1]
|
12
|
-
# [4] == temp
|
13
|
-
[0]=5;[1]=2;[2]=[3]=1;while [1]<[0] do [4]=[2]+[3];[2]=[3];[3]=[4];[1]=[1]+1 end;[3]
|
14
|
-
|
15
|
-
# simulate a turing machine
|
16
|
-
# [0] == current state
|
17
|
-
# [1] == head position
|
18
|
-
# [2] == number of symbols this machine recognizes
|
19
|
-
# [3] == current state + read value offset
|
20
|
-
# [state*[2]*3+10] == write at the current head position(nil or any number)
|
21
|
-
# [state*[2]*3+11] == move head amount (-1, 0, 1)
|
22
|
-
# [state*[2]*3+12] == next-state (states starting from 0)
|
23
|
-
# [10000+] == tape - pick a large enough constant that the turing machine doesn't corrupt your states
|
24
|
-
# -1 is the stop-state
|
25
|
-
# Assumes you initalized your turing program for all your states.
|
26
|
-
[0]=-1;
|
27
|
-
[1]=10000;
|
28
|
-
while [0]>=0 do
|
29
|
-
[3] = 10 + ([0]*[2]+[[1]]) * 3;
|
30
|
-
[[1]] = [[3]];
|
31
|
-
[1] = [1] + [[3]+1];
|
32
|
-
[0] = [[3]+2];
|
33
|
-
end
|
data/examples/turing/notes.rb
DELETED
@@ -1,111 +0,0 @@
|
|
1
|
-
turing0: #babel-bridge setup
|
2
|
-
turing1: #introducing rules -> show interactive shell in action
|
3
|
-
rule :int, /[-]?[0-9]+/
|
4
|
-
|
5
|
-
turing2: #the "add" rule (with 2 variants to support recursive matching)
|
6
|
-
rule :add, :int, "+", :add
|
7
|
-
rule :add, :int
|
8
|
-
|
9
|
-
turing3: #add evaluate methods
|
10
|
-
rule :add, :int, "+", :add do
|
11
|
-
def evaluate
|
12
|
-
int.evaluate + add.evaluate
|
13
|
-
end
|
14
|
-
end
|
15
|
-
rule :add, :int
|
16
|
-
|
17
|
-
rule :int, /[-]?[0-9]+/ do
|
18
|
-
def evaluate
|
19
|
-
to_s.to_i
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
turing4: #multiple operators, but "3*10+2" or "2+10*3" breaks!
|
24
|
-
# add :operator rule, update evaluate
|
25
|
-
rule :add, :int, /[-+\/*]/, :add do
|
26
|
-
def evaluate
|
27
|
-
int.evaluate.send match[1].to_s.to_sym, add.evaluate
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
turing5: # binary_operators_rule
|
32
|
-
# replace all but the int rule with:
|
33
|
-
binary_operators_rule :statement, :int, [[:/, :*], [:+, :-]] do
|
34
|
-
def evaluate
|
35
|
-
left.evaluate.send operator, right.evaluate
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
turing6: # add parenthesis
|
40
|
-
# rename :int to :operand
|
41
|
-
rule :operand, "(", :statement, ")"
|
42
|
-
|
43
|
-
turing7: # show "5 + 2" fails -> ignore_whitespace
|
44
|
-
ignore_whitespace
|
45
|
-
|
46
|
-
turing8: # add comparators
|
47
|
-
binary_operators_rule :statement, :operand, [[:/, :*], [:+, :-],
|
48
|
-
[:<, :<=, :>, :>=, :==]] do
|
49
|
-
def evaluate
|
50
|
-
case operator
|
51
|
-
when :<, :<=, :>, :>=, :==
|
52
|
-
(left.evaluate.send operator, right.evaluate) ? 1 : nil
|
53
|
-
else
|
54
|
-
left.evaluate.send operator, right.evaluate
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
turing9: # add if
|
60
|
-
rule :statement, "if", :statement, "then", :statement, :else_clause?, "end" do
|
61
|
-
def evaluate
|
62
|
-
if matches[1].evaluate
|
63
|
-
matches[3].evaluate
|
64
|
-
else
|
65
|
-
else_clause.evaluate if else_clause
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
69
|
-
rule :else_clause, "else", :statement
|
70
|
-
|
71
|
-
turing10: #add store
|
72
|
-
|
73
|
-
def store
|
74
|
-
@store||=[]
|
75
|
-
end
|
76
|
-
|
77
|
-
rule :operand, "[", :statement, "]", "=", :statement do
|
78
|
-
def evaluate
|
79
|
-
parser.store[statement[0].evaluate] = statement[1].evaluate
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
rule :operand, "[", :statement, "]" do
|
84
|
-
def evaluate
|
85
|
-
parser.store[statement.evaluate]
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
turing11: #statements
|
90
|
-
# update "if" bodies to have statements
|
91
|
-
|
92
|
-
rule :statements, many(:statement,";"), match?(";") do
|
93
|
-
def evaluate
|
94
|
-
ret = nil
|
95
|
-
statement.each do |s|
|
96
|
-
ret = s.evaluate
|
97
|
-
end
|
98
|
-
ret
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
turing12: # while
|
103
|
-
|
104
|
-
rule :statement, "while", :statement, "do", :statements, "end" do
|
105
|
-
def evaluate
|
106
|
-
while statement.evaluate
|
107
|
-
statements.evaluate
|
108
|
-
end
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
@@ -1,71 +0,0 @@
|
|
1
|
-
require "babel_bridge"
|
2
|
-
|
3
|
-
class TuringParser < BabelBridge::Parser
|
4
|
-
ignore_whitespace
|
5
|
-
|
6
|
-
def store
|
7
|
-
@store||=[]
|
8
|
-
end
|
9
|
-
|
10
|
-
rule :statements, many(:statement,";"), match?(";") do
|
11
|
-
def evaluate
|
12
|
-
ret = nil
|
13
|
-
statement.each do |s|
|
14
|
-
ret = s.evaluate
|
15
|
-
end
|
16
|
-
ret
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
rule :statement, "if", :statement, "then", :statements, :else_clause?, "end" do
|
21
|
-
def evaluate
|
22
|
-
if matches[1].evaluate
|
23
|
-
matches[3].evaluate
|
24
|
-
else
|
25
|
-
else_clause.evaluate if else_clause
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
rule :else_clause, "else", :statements
|
30
|
-
|
31
|
-
rule :statement, "while", :statement, "do", :statements, "end" do
|
32
|
-
def evaluate
|
33
|
-
while statement.evaluate
|
34
|
-
statements.evaluate
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
binary_operators_rule :statement, :operand, [[:/, :*], [:+, :-], [:<, :<=, :>, :>=, :==]] do
|
40
|
-
def evaluate
|
41
|
-
res = left.evaluate.send operator, right.evaluate
|
42
|
-
case operator
|
43
|
-
when :<, :<=, :>, :>=, :==
|
44
|
-
res ? 1 : nil
|
45
|
-
else
|
46
|
-
res
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
rule :operand, "[", :statement, "]", "=", :statement do
|
52
|
-
def evaluate
|
53
|
-
parser.store[statement[0].evaluate] = statement[1].evaluate
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
rule :operand, "[", :statement, "]" do
|
58
|
-
def evaluate
|
59
|
-
parser.store[statement.evaluate]
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
rule :operand, "(", :statement, ")"
|
64
|
-
rule :operand, /[-]?[0-9]+/ do
|
65
|
-
def evaluate
|
66
|
-
to_s.to_i
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
BabelBridge::Shell.new(TuringParser.new).start
|
data/lib/version.rb
DELETED