-
Notifications
You must be signed in to change notification settings - Fork 100
/
irb.txt
170 lines (148 loc) · 4.17 KB
/
irb.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
Add.new(
Multiply.new(Number.new(1), Number.new(2)),
Multiply.new(Number.new(3), Number.new(4))
)
Add.new(
Multiply.new(Number.new(1), Number.new(2)),
Multiply.new(Number.new(3), Number.new(4))
)
Number.new(5)
Multiply.new(
Number.new(1),
Multiply.new(
Add.new(Number.new(2), Number.new(3)),
Number.new(4)
)
)
Number.new(1).reducible?
Add.new(Number.new(1), Number.new(2)).reducible?
expression =
Add.new(
Multiply.new(Number.new(1), Number.new(2)),
Multiply.new(Number.new(3), Number.new(4))
)
expression.reducible?
expression = expression.reduce
expression.reducible?
expression = expression.reduce
expression.reducible?
expression = expression.reduce
expression.reducible?
ExpressionMachine.new(
Add.new(
Multiply.new(Number.new(1), Number.new(2)),
Multiply.new(Number.new(3), Number.new(4))
)
).run
ExpressionMachine.new(
LessThan.new(Number.new(5), Add.new(Number.new(2), Number.new(2)))
).run
ExpressionMachine.new(
Add.new(Variable.new(:x), Variable.new(:y)),
{ x: Number.new(3), y: Number.new(4) }
).run
old_environment = { y: Number.new(5) }
new_environment = old_environment.merge({ x: Number.new(3) })
old_environment
statement = Assign.new(:x, Add.new(Variable.new(:x), Number.new(1)))
environment = { x: Number.new(2) }
statement.reducible?
statement, environment = statement.reduce(environment)
statement, environment = statement.reduce(environment)
statement, environment = statement.reduce(environment)
statement.reducible?
StatementMachine.new(
Assign.new(:x, Add.new(Variable.new(:x), Number.new(1))),
{ x: Number.new(2) }
).run
StatementMachine.new(
If.new(
Variable.new(:x),
Assign.new(:y, Number.new(1)),
Assign.new(:y, Number.new(2))
),
{ x: Boolean.new(true) }
).run
StatementMachine.new(
If.new(Variable.new(:x), Assign.new(:y, Number.new(1)), DoNothing.new),
{ x: Boolean.new(false) }
).run
StatementMachine.new(
Sequence.new(
Assign.new(:x, Add.new(Number.new(1), Number.new(1))),
Assign.new(:y, Add.new(Variable.new(:x), Number.new(3)))
),
{}
).run
StatementMachine.new(
While.new(
LessThan.new(Variable.new(:x), Number.new(5)),
Assign.new(:x, Multiply.new(Variable.new(:x), Number.new(3)))
),
{ x: Number.new(1) }
).run
StatementMachine.new(
Sequence.new(
Assign.new(:x, Boolean.new(true)),
Assign.new(:x, Add.new(Variable.new(:x), Number.new(1)))
),
{}
).run
Number.new(23).evaluate({})
Variable.new(:x).evaluate({ x: Number.new(23) })
LessThan.new(
Add.new(Variable.new(:x), Number.new(2)),
Variable.new(:y)
).evaluate({ x: Number.new(2), y: Number.new(5) })
statement =
Sequence.new(
Assign.new(:x, Add.new(Number.new(1), Number.new(1))),
Assign.new(:y, Add.new(Variable.new(:x), Number.new(3)))
)
statement.evaluate({})
statement =
While.new(
LessThan.new(Variable.new(:x), Number.new(5)),
Assign.new(:x, Multiply.new(Variable.new(:x), Number.new(3)))
)
statement.evaluate({ x: Number.new(1) })
Number.new(5).to_ruby
Boolean.new(false).to_ruby
proc = eval(Number.new(5).to_ruby)
proc.call({})
proc = eval(Boolean.new(false).to_ruby)
proc.call({})
expression = Variable.new(:x)
expression.to_ruby
proc = eval(expression.to_ruby)
proc.call({ x: 7 })
Add.new(Variable.new(:x), Number.new(1)).to_ruby
LessThan.new(Add.new(Variable.new(:x), Number.new(1)), Number.new(3)).to_ruby
environment = { x: 3 }
proc = eval(Add.new(Variable.new(:x), Number.new(1)).to_ruby)
proc.call(environment)
proc = eval(
LessThan.new(Add.new(Variable.new(:x), Number.new(1)), Number.new(3)).to_ruby
)
proc.call(environment)
statement = Assign.new(:y, Add.new(Variable.new(:x), Number.new(1)))
statement.to_ruby
proc = eval(statement.to_ruby)
proc.call({ x: 3 })
statement =
While.new(
LessThan.new(Variable.new(:x), Number.new(5)),
Assign.new(:x, Multiply.new(Variable.new(:x), Number.new(3)))
)
statement.to_ruby
proc = eval(statement.to_ruby)
proc.call({ x: 1 })
require 'treetop'
Treetop.load('simple')
parse_tree = SimpleParser.new.parse('while (x < 5) { x = x * 3 }')
statement = parse_tree.to_ast
statement.evaluate({ x: Number.new(1) })
statement.to_ruby
expression = SimpleParser.new.parse('1 * 2 * 3 * 4', root: :expression).to_ast
expression.left
expression.right