-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.rb
More file actions
194 lines (149 loc) · 3.18 KB
/
Copy pathparser.rb
File metadata and controls
194 lines (149 loc) · 3.18 KB
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
class MyParser
class MyParserError < StandardError; end
def initialize(tokenized_list)
@tokenized_list = tokenized_list
@index = 0
end
def run
statement
end
private
# 複文
def statement
ret = func
tmp = []
while !get_next_token.nil? and
get_next_token.first == newline
next_token!
if !get_next_token.nil? and (
get_next_token.first == number_literal or
get_next_token.first == statement_if )
next_token!
tmp.push func
else
break
end
end
ret = tmp.unshift('stmts', ret) if !tmp.empty?
ret
end
# if文
def func
ret = []
if token.first == statement_if
ret.push 'if'
if !get_next_token.nil? and
get_next_token.first == left_parenthesis
next_token!
next_token!
t = vocab
ret.push t if !get_next_token.nil? and
get_next_token.first == right_parenthesis
next_token!
while get_next_token.first == newline
next_token!
end
if !get_next_token.nil? and
get_next_token.first == left_curly_brace
next_token!
while get_next_token.first == newline
next_token!
end
next_token!
t = statement
ret.push t if !get_next_token.nil? and
get_next_token.first == right_curly_brace
end
end
else
ret = vocab
end
ret
end
# 比較演算子
def vocab
ret = expr
while !get_next_token.nil? and
get_next_token.first =~ comparison
next_token!
ret = [token[1], ret]
next_token!
ret.push expr
end
ret
end
# 加算、減算演算子
def expr
ret = term
while !get_next_token.nil? and
get_next_token.first =~ plus_or_minus
next_token!
ret = [token[1], ret]
next_token!
ret.push term
end
ret
end
# 乗算、割算、剰余演算子
def term
ret = factor
while !get_next_token.nil? and
get_next_token.first =~ multi_or_divide
next_token!
ret = [token[1], ret]
next_token!
ret.push factor
end
ret
end
# 数値リテラル
def factor
if token.first == number_literal
token
elsif !get_next_token.nil? and
get_next_token.first == newline
token
else
raise MyParserError
end
end
def token
@tokenized_list[@index]
end
def get_next_token
@tokenized_list[@index + 1]
end
def next_token!
@index += 1
end
def number_literal
'lit'
end
def newline
'newline'
end
def plus_or_minus
%r(operator_plus|operator_minus)
end
def multi_or_divide
%r(operator_multi|operator_divide|operator_surplus)
end
def comparison
%r(operator_gt|operator_ge|operator_eq|operator_le|operator_lt|operator_ne)
end
def statement_if
'reserved_statement_if'
end
def left_parenthesis
'symbol_left_parenthesis'
end
def right_parenthesis
'symbol_right_parenthesis'
end
def left_curly_brace
'symbol_left_curly_brace'
end
def right_curly_brace
'symbol_right_curly_brace'
end
end