-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDenotationSemantic.rb
82 lines (70 loc) · 1.42 KB
/
DenotationSemantic.rb
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
require_relative 'SmallstepSemantic'
class Number
def to_ruby
"-> e { #{value.inspect} }"
end
end
class Boolean
def to_ruby
"-> e { #{value.inspect} }"
end
end
class Variable
def to_ruby
"-> e { e[#{name.inspect}] }"
end
end
class Add
def to_ruby
"-> e { (#{left.to_ruby}).call(e) + (#{right.to_ruby}).call(e) }"
end
end
class Multiply
def to_ruby
"-> e { (#{left.to_ruby}).call(e) * (#{right.to_ruby}).call(e) }"
end
end
class LessThan
def to_ruby
"-> e { (#{left.to_ruby}).call(e) < (#{right.to_ruby}).call(e) }"
end
end
class Assign
def to_ruby
"-> e { e.merge({ #{name.inspect} => (#{expression.to_ruby}).call(e) }) }"
end
end
class DoNothing
def to_ruby
"-> e { e }"
end
end
class If
def to_ruby
"-> e {" +
"if (#{condition.to_ruby}).call(e)" +
" then (#{consequence.to_ruby}).call(e)" +
" else (#{alternative.to_ruby}).call(e)" +
" end }"
end
end
class Sequence
def to_ruby
"-> e { (#{second.to_ruby}).call((#{first.to_ruby}).call(e)) }"
end
end
class While
def to_ruby
"-> e {" +
" while (#{condition.to_ruby}).call(e); e = (#{body.to_ruby}).call(e); end;" +
" e " +
" }"
end
end
stmt = While.new(
LessThan.new(Variable.new(:x), Number.new(5)),
Assign.new(:x, Multiply.new(Variable.new(:x), Number.new(3)))
)
puts stmt.to_ruby
proc = eval(stmt.to_ruby)
puts proc.call({ :x => 1})