mirror of
https://github.com/samsonjs/compiler.git
synced 2026-04-27 14:57:45 +00:00
[NEW] Parse identifiers of any length, instead of only one char.
This commit is contained in:
parent
268c6f6c29
commit
42ca4451a8
2 changed files with 20 additions and 11 deletions
29
compiler.rb
29
compiler.rb
|
|
@ -23,7 +23,8 @@ class Compiler
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse
|
def parse
|
||||||
statement until eof?
|
statement
|
||||||
|
expression; newline
|
||||||
[@data, @bss, @code]
|
[@data, @bss, @code]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -59,25 +60,33 @@ class Compiler
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Recognize an alphabetical character
|
# Recognize an alphabetical character.
|
||||||
def alpha?(char)
|
def alpha?(char)
|
||||||
('A'..'Z') === char.upcase
|
('A'..'Z') === char.upcase
|
||||||
end
|
end
|
||||||
|
|
||||||
# Recognize a decimal digit
|
# Recognize a decimal digit.
|
||||||
def digit?(char)
|
def digit?(char)
|
||||||
('0'..'9') === char
|
('0'..'9') === char
|
||||||
end
|
end
|
||||||
|
|
||||||
# Get an identifier
|
# Recognize an alphanumeric character.
|
||||||
def get_name
|
def alnum?(char)
|
||||||
expected('identifier') unless alpha?(@look)
|
alpha?(char) || digit?(char)
|
||||||
c = @look
|
|
||||||
get_char
|
|
||||||
return c
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Get a number
|
# Get an identifier.
|
||||||
|
def get_name
|
||||||
|
expected('identifier') unless alpha?(@look)
|
||||||
|
token = ''
|
||||||
|
while alnum?(@look)
|
||||||
|
token << @look
|
||||||
|
get_char
|
||||||
|
end
|
||||||
|
token
|
||||||
|
end
|
||||||
|
|
||||||
|
# Get a number.
|
||||||
def get_num
|
def get_num
|
||||||
expected('integer') unless digit?(@look)
|
expected('integer') unless digit?(@look)
|
||||||
c = @look
|
c = @look
|
||||||
|
|
|
||||||
2
test.rb
2
test.rb
|
|
@ -25,7 +25,7 @@ def main(arg)
|
||||||
File.open(arg)
|
File.open(arg)
|
||||||
else
|
else
|
||||||
# StringIO.new("5*(3-5)*2+2-9/3-8/2-4*(5+5+5)\n")
|
# StringIO.new("5*(3-5)*2+2-9/3-8/2-4*(5+5+5)\n")
|
||||||
StringIO.new("a=9\n")
|
StringIO.new("abc=9\nabc-9\n")
|
||||||
end
|
end
|
||||||
data, bss, code = *parse(input)
|
data, bss, code = *parse(input)
|
||||||
template = File.read("template.asm")
|
template = File.read("template.asm")
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue