-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscanner.rb
177 lines (155 loc) · 3.13 KB
/
scanner.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
#tokens = [:EOF, :INT(type), :PLUS, :MINUS, :STAR, :SLASH, :EQ_EQ, :EQUALS, :NE, :LT, :LE, :GT, :GE, :SEMI, :PRINT, :IDENT, :NNUMBER, :LBRACE, :RBRACE, :IF, :ELSE, :WHILE, :FOR, :VOID, :CHAR, :LONG, :RETURN, :LOGAND, :AMPER]
$keywords = {
"print" => :PRINT,
"int" => :INT,
"if" => :IF,
"else" => :ELSE,
"while" => :WHILE,
"for" => :FOR,
"void" => :VOID,
"char" => :CHAR,
"long" => :LONG,
"return" => :RETURN
}
class Token
attr_reader :type, :literal, :line
def initialize(type, literal, line)
@type = type
@literal = literal
@line = line
end
def to_s
"#{type} #{literal} #{line}"
end
def inspect
to_s
end
end
class Scanner
def initialize(source)
@source = source
@current = 0
@tokens = []
@line = 1
end
def scanTokens
while (!isAtEnd())
scanToken()
end
addToken(:EOF)
@tokens
end
def scanToken
c = advance()
case c
when '+'
addToken(:PLUS)
when '-'
addToken(:MINUS)
when '*'
addToken(:STAR)
when '/'
if match('/')
while peek != "\n"
advance
end
else
addToken(:SLASH)
end
when ';'
addToken(:SEMI)
when ','
addToken(:COMMA)
when '{'
addToken(:LBRACE)
when '}'
addToken(:RBRACE)
when '('
addToken(:LPAREN)
when ')'
addToken(:RPAREN)
when '='
addToken(if match('=') then :EQ_EQ else :EQUALS end)
when '!'
addToken(if match('=') then :NE else error(@line, "unexpected character #{c}") end)
when '>'
addToken(if match('=') then :GE else :GT end)
when '<'
addToken(if match('=') then :LE else :LT end)
when '&'
addToken(if match('&') then :LOGAND else :AMPER end)
when " ", "\r", "\t", "\f"
when "\n"
@line += 1
else
if isDigit(c)
number()
elsif isAlpha(c)
identifier()
else
error(@line, "unexpected character #{c}")
end
end
end
def isAtEnd
@current >= @source.length
end
def advance
c = @source[@current]
@current += 1
c
end
def peek
if isAtEnd
return '\0'
end
@source[@current]
end
def match(expected)
if isAtEnd
return false
end
if @source[@current] != expected
return false
end
advance
true
end
def isDigit(c)
code = c.ord
48 <= code && code <= 57
end
def isAlpha(c)
code = c.ord
(code >= 65 && code <= 90) || (code >= 97 && code <= 122) || code == 95
end
def isAlphaNumeric(c)
isAlpha(c) || isDigit(c)
end
def number
start = @current - 1
while isDigit(peek)
advance
end
addToken(:NUMBER, @source[start..@current-1])
end
def identifier
start = @current - 1
while isAlphaNumeric(peek)
advance
end
literal = @source[start..@current-1]
if $keywords[literal] != nil
addToken($keywords[literal], literal)
else
addToken(:IDENT, literal)
end
end
def addToken(type, literal=nil)
@tokens.push Token.new(type,literal,@line)
end
def error(line, message)
puts "Line #{line}: #{message}"
exit(1)
end
end