mirror of
https://github.com/samsonjs/compiler.git
synced 2026-03-25 08:45:52 +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
|
||||
|
||||
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
|
||||
|
|
|
|||
2
test.rb
2
test.rb
|
|
@ -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")
|
||||
|
|
|
|||
Loading…
Reference in a new issue