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
|
# File 'lib/rucc/parser/enum.rb', line 5
def read_enum_def
tag = nil
tok = get
if tok.kind == T::IDENT
tag = tok.sval
tok = get
end
if tag
ty = @tags[tag]
if ty && ty.kind != Kind::ENUM
Util.errort!(tok, "declarations of #{tag} does not match")
end
end
if !Token.is_keyword?(tok, '{')
if !tag || !@tags[tag]
Util.errort!(tok, "enum tag #{tag} is not defined")
end
@lexer.unget_token(tok)
return Type::INT
end
if tag
@tags[tag] = Type::ENUM
end
val = 0
while true
tok = get
if Token.is_keyword?(tok, '}')
break
end
if tok.kind != T::IDENT
Util.errort!(tok, "identifier expected, but got #{tok}")
end
name = tok.sval
if next_token?('=')
val = read_intexpr
end
constval = Node.ast_inttype(Type::INT, val)
val += 1
env[name] = constval
if next_token?(',')
next
end
if next_token?('}')
break
end
Util.errort!(peek(), "',' or '}' expected, but got #{peek}")
end
Type::INT
end
|