# Copyright (C) 2012 xxx Technology (xx) Co., LTD.
# All rights reserved.
#
# Developed by RD BIOS Team.
#
# Authors: perry
class BasicReader(BaseReader):
def read(self):
while self._getc():
text = ''
if self._equc(" \t\v\0\r"):
continue
elif self._equc("'"):
res = False
while self._getc():
if self._char == '\n':
res = True
break
if res:
if not self._ungetc():
break
continue
break
elif self._equc('\n'):
self._newline = True
return self._set(BSCTK_ENDL, '__end_of_line__', 0)
elif self._equc(basic_identifier_start):
self.pos = self._pos
while self._equc(basic_identifier):
text += self._char
if not self._getc():
break
if not self._ungetc():
break
text = text.upper()
if basic_keywords.count(text):
return self._set(BSCTK_KEYWORD, text, basic_keywords.index(text))
return self._set(BSCTK_IDENT, text, 0)
elif self._equc(string.digits):
self.pos = self._pos
while self._equc('.' + string.digits):
text += self._char
if not self._getc():
break
'''
if not self._equc(basic_operators):
if not self._equc(' \t\v\r\n\''):
self._ungetc()
return self._set(BSCTK_ERROR, 'Illegal character', self._char)
'''
if not self._ungetc():
break
res = text.count('.')
if res == 0:
return self._set(BSCTK_INTEGER, 'int', int(text))
elif res == 1:
return self._set(BSCTK_FLOAT, 'float', float(text))
else:
return self._set(BSCTK_ERROR, 'Invalid float', text)
elif self._equc('&'):
self.pos = self._pos
if not self._getc():
return self._set(BSCTK_ERROR, 'Invalid operator', self._char)
if self._char.upper() != 'H':
self._ungetc()
return self._set(BSCTK_OPERATOR, 'operator', basic_operators.index('&'))
while self._getc():
if not self._equc(string.hexdigits):
break
text += self._char
'''
if not self._equc(basic_operators):
if not self._equc(' \t\v\r\n' + string.hexdigits):
self._ungetc()
return self._set(BSCTK_ERROR, 'Illegal character', self._char)
'''
if not self._ungetc():
break
if text == '':
return self._set(BSCTK_ERROR, 'Invalid hexdigits', '&H')
return self._set(BSCTK_INTEGER, 'int', int(text.upper(), 0x10))
elif self._equc('"'):
self.pos = self._pos
while self._getc():
if self._equc('"\n'):
break
text += self._char
# If the last char isn't a double quotes then indicates it's not end.
if self._char != '"':
return self._set(BSCTK_ERROR, 'Invalid string', text)
return self._set(BSCTK_STRING, 'string', text)
else:
res = False
self.pos = self._pos
for a in basic_operators:
if a.startswith(self._char):
res = True
break
if res:
text = self._char
while self._getc():
text += self._char
res = False
for a in basic_operators:
if a.startswith(text):
res = True
break
if not res:
break
self._ungetc()
return self._set(BSCTK_OPERATOR, 'operator', basic_operators.index(text[:-1]))
self._ungetc()
return self._set(BSCTK_ERROR, 'unknow', text)
return self._set(BSCTK_ENDF, '__end_of_file__', 0)
if __name__ == "__main__":
reader = BasicReader('alex2.bas')
while reader.next():
print('%03d:%02d 0x%02X %018s %s' % (reader.line, reader.pos, reader.type, reader.name, reader.value))
if reader.type == BSCTK_ERROR:
break
# Copyright (C) 2012 xxx Technology (xx) Co., LTD.# All rights reserved.## Developed by RD BIOS Team.## Authors: perry class BasicReader(BaseReader): def read(self): while self._