Keywords and identifiers

This commit is contained in:
Mariano Uvalle 2023-05-07 00:46:22 +00:00
parent cdab15193a
commit fd21194901
3 changed files with 46 additions and 0 deletions

View file

@ -108,6 +108,10 @@ func (s *Scanner) scanToken() {
s.scanNumber()
return
}
if isIdentAlpha(c) {
s.scanIdentifier()
return
}
lerrors.EmitError(s.line, "Unexpected character.")
}
}
@ -201,6 +205,18 @@ func (s *Scanner) scanNumber() {
)
}
func (s *Scanner) scanIdentifier() {
for isIdentAlphaNumeric(s.peek()) {
s.advance()
}
l := s.source[s.start:s.current]
typ := IDENT
if kTyp, ok := KeywordTypes[l]; ok {
typ = kTyp
}
s.addToken(typ)
}
// addToken produces a single token without a literal value.
func (s *Scanner) addToken(typ TokenType) {
s.addTokenWithLiteral(typ, nil)