-
Notifications
You must be signed in to change notification settings - Fork 2
/
pyra.rb
195 lines (178 loc) · 4.84 KB
/
pyra.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
def indices(str, chr)
(0 ... str.length).find_all { |i| str[i] == chr }
end
def unwrap(t)
t.size == 1 ? t[0] : t
end
$TOP = "^"
$BOTTOM = "-"
$L_SIDE = "/"
$R_SIDE = "\\"
def triangle_from(lines, ptr_inds = nil)
raise "no triangle found" if !lines.first
ptr_inds = ptr_inds || indices(lines.first, $TOP)
row = ""
ptr_inds.map { |pt|
x1 = x2 = pt # left and right sides
y = 0
data = []
loop {
x1 -= 1
x2 += 1
y += 1
row = lines[y]
raise "unexpected triangle end" if !row or x2 > row.size
# are we done?
if row[x1] != $L_SIDE
# look for end
if row[x2] == $R_SIDE # mismatched!
raise "left side too short"
else
# both sides are exhausted--look for a bottom
# p (x1 + 1 .. x2 - 1).map { |e| row[e] }
# p [x1, x2, pt]
if (x1 + 1 .. x2 - 1).all? { |x| row[x] == $BOTTOM }
break
else
raise "malformed bottom"
end
end
elsif row[x2] != $R_SIDE
# look for end
if row[x1] == $L_SIDE # mismatched!
raise "right side too short"
else
# both sides are exhausted--look for a bottom
if (x1 + 1 .. x2 - 1).all? { |x| row[x] == $BOTTOM }
break
else
raise "malformed bottom"
end
end
# elsif x1 == 0 # we must have found our bottom...
end
#todo: do for other side
# we aren't done.
data.push row[x1 + 1 .. x2 - 1]
}
op = data.join("").gsub(/\s+/, "")
args = []
if row[x1] == $TOP or row[x2] == $TOP
next_inds = [x1, x2].find_all { |x| row[x] == $TOP }
args.push triangle_from(lines[y..-1], next_inds)
end
unwrap [op, *args]
}
end
$vars = {"eps" => ""}
$UNDEF = :UNDEF
def parse(str)
# find ^s on first line
lns = str.lines
triangle_from(lns)
end
# converts a string to a pyramid value
def str_to_val(str)
# todo: expand
if $vars.has_key? str
$vars[str]
elsif str == "line" or str == "stdin" or str == "readline"
$stdin.gets
else
str.to_f
end
end
def val_to_str(val)
sanatize(val).to_s
end
def falsey(val)
[0, [], "", $UNDEF, "\x00", nil].include? val
end
def truthy(val)
!falsey val
end
class TrueClass; def to_i; 1; end; end
class FalseClass; def to_i; 0; end; end
$outted = false
$uneval_ops = {
"set" => -> (left, right) {
$vars[left] = eval_chain right
$UNDEF
},
# condition: left
# body: right
"do" => -> (left, right) {
loop {
eval_chain right
break unless truthy eval_chain left
}
$UNDEF
},
# condition: left
# body: right
"loop" => -> (left, right) {
loop {
break unless truthy eval_chain left
eval_chain right
}
$UNDEF
},
# condition: left
# body: right
"?" => -> (left, right) {
truthy(eval_chain left) ? eval_chain(right) : 0
}
}
$ops = {
"+" => -> (a, b) { a + b },
"*" => -> (a, b) { a * b },
"-" => -> (a, b) { a - b },
"/" => -> (a, b) { 1.0 * a / b },
"^" => -> (a, b) { a ** b },
"=" => -> (a, b) { (a == b).to_i },
"<=>" => -> (a, b) { a <=> b },
"out" => -> (*a) { $outted = true; a.each { |e| print e }; },
"chr" => -> (a) { a.to_i.chr },
"arg" => -> (*a) { a.size == 1 ? ARGV[a[0]] : a[0][a[1]] },
"#" => -> (a) { str_to_val a },
"\"" => -> (a) { val_to_str a },
"" => -> (*a) { unwrap a },
"!" => -> (a) { falsey(a).to_i },
"[" => -> (a, b) { a },
"]" => -> (a, b) { b },
}
def eval_chain(chain)
if chain.is_a? String
return str_to_val chain
else
op, args = chain
if $uneval_ops.has_key? op
return $uneval_ops[op][*args]
end
raise "undefined operation `#{op}`" unless $ops.has_key? op
return sanatize $ops[op][*sanatize(args.map { |ch| eval_chain ch })]
end
end
def sanatize(arg)
if arg.is_a? Array
arg.map { |e| sanatize e }
elsif arg.is_a? Float
arg == arg.to_i ? arg.to_i : arg
else
arg
end
end
prog = File.read(ARGV[0]).gsub(/\r/, "")
parsed = parse(prog)
res = parsed.map { |ch| eval_chain ch }
res = res.reject { |e| e == $UNDEF } if res.is_a? Array
res = res.is_a?(Array) && res.length == 1 ? res.pop : res
to_print = sanatize(res)
unless $outted
if ARGV[1] && ARGV[1][1] == "d"
p to_print
else
puts to_print
end
end
# p $vars