-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.rb
52 lines (44 loc) · 798 Bytes
/
common.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
require "pp"
class Token
attr_reader :kind, :value
# kind:
# str: string
# kw: keyword
# int: integer
# sym: symbol
# ident: identifier
def initialize(kind, value)
@kind = kind
@value = value
end
def to_line
"#{@kind}:#{@value}"
end
def get_value
case @kind
when :int then @value.to_i
when :ident then @value
else
raise "invalid kind"
end
end
def self.from_line(line)
if /^(.+?):(.+)$/ =~ line
Token.new($1.to_sym, $2)
else
nil
end
end
end
def p_e(*args)
args.each { |arg| $stderr.puts arg.inspect }
end
def pp_e(*args)
args.each { |arg| $stderr.puts arg.pretty_inspect }
end
def panic(*args)
"PANIC" +
args
.map { |arg| " (#{ arg.inspect })" }
.join("")
end