[NEW] Parse identifiers of any length, instead of only one char.

This commit is contained in:
sjs 2009-05-13 23:27:47 -07:00
parent 268c6f6c29
commit 42ca4451a8
2 changed files with 20 additions and 11 deletions

View file

@ -23,7 +23,8 @@ class Compiler
end
def parse
statement until eof?
statement
expression; newline
[@data, @bss, @code]
end
@ -59,25 +60,33 @@ class Compiler
end
end
# Recognize an alphabetical character
# Recognize an alphabetical character.
def alpha?(char)
('A'..'Z') === char.upcase
end
# Recognize a decimal digit
# Recognize a decimal digit.
def digit?(char)
('0'..'9') === char
end
# Get an identifier
def get_name
expected('identifier') unless alpha?(@look)
c = @look
get_char
return c
# Recognize an alphanumeric character.
def alnum?(char)
alpha?(char) || digit?(char)
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
expected('integer') unless digit?(@look)
c = @look

View file

@ -25,7 +25,7 @@ def main(arg)
File.open(arg)
else
# 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
data, bss, code = *parse(input)
template = File.read("template.asm")