generated from InvoluteHell/Scoring
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathdemo.rb
187 lines (170 loc) · 3.21 KB
/
demo.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
#!/usr/bin/ruby
#
# Betterave -- Programmation fonctionnelle obscure
#
class Source
attr_reader :index, :stuff
def initialize(stuff)
@stuff = stuff
@index = -1
@vars = Hash.new(0)
@strings = []
@tacks = []
end
def oops(mess)
print mess
exit 1
end
def parse()
@index += 1
if @index >= @stuff.length then
exit
end
case @stuff[@index,1]
# Commentaires
when "~"
temp = @stuff.index("~",@index + 1)
if temp == nil then
oops "Unmatched comment!\n"
end
@index = temp
# Opérations mathématiques de base
when /\d/
return @stuff[@index,1].to_i
when "+"
return parse() + parse()
when "-"
return parse() - parse()
when "*"
return parse() * parse()
when "/"
return parse() / parse()
when "%"
return parse() % parse()
when "="
return parse() == parse() ? 1 : 0
when "<"
return parse() < parse() ? 1 : 0
when ">"
return parse() > parse() ? 1 : 0
# Manipulation de strings (léopard)
when "\""
temp = @stuff.index("\"", @index + 1)
if temp == nil then
oops "Unmatched string!\n"
end
@strings << @stuff[ (@index + 1) ... temp ]
@index = temp
return @strings.length
when "#"
temp = parse()
if temp < 0 or temp >= @strings.length then
return -1
else
@strings[temp] << parse().to_s
end
when "&"
temp = parse()
if temp < 0 or temp >= @strings.length then
return -1
else
char = parse()
unless char < 0 or char > 255 then
@strings[temp] << char.chr
end
return char
end
when "\\"
temp = parse()
if temp < 0 or temp >= @strings.length then
return -1
else
if @strings[temp].empty? then
return -1
else
char = @strings[temp][0]
@strings[temp] = @strings[temp][1..-1]
return char
end
end
when "_"
temp = parse()
if temp < 0 or temp >= @strings.length then
return -1
else
@strings.delete_at(temp)
return temp
end
# Structure
when "["
@tacks << @index
when "|"
temp = parse()
if temp != 0 then
if @tacks.length == 0 then
@index = -1
else
@index = @tacks.last
end
else
endtack = @stuff.index("]", @index)
if endtack == nil then
oops "Unmatched tack!\n"
end
@tacks.pop
@index = endtack
end
when "?"
if parse() == 0 then
ifelse = @stuff.index("!", @index)
if ifelse == nil then
oops "Unmatched if!\n"
end
@index = ifelse
end
when "@"
exit
# Input
when ":"
return gets.to_i
when ";"
temp = @strings.length
@strings << gets
return temp
# Output
when "."
temp = parse()
print temp
return temp
when ","
temp = parse()
if temp < 0 or temp > 255 then
return 0
end
print temp.chr
return temp
when "$"
temp = parse()
if temp < 0 or temp >= @strings.length then
return -1
else
string = @strings[temp]
unless string == nil then
print string
end
return temp
end
# Manipulation de variables
when /[A-Z]/
@vars[@stuff[@index,1]] = parse()
when /[a-z]/
return @vars[@stuff[@index,1].upcase]
else
return parse()
end
end
end
source = Source.new(File.readlines(ARGV[0]).join)
while source.index < (source.stuff.length - 1)
source.parse()
end