forked from luna/jorts
add number tokens
This commit is contained in:
parent
9d1d253c94
commit
566d8313f3
1 changed files with 34 additions and 0 deletions
|
@ -11,6 +11,20 @@ pub const TokenError = error{
|
||||||
Unterminated,
|
Unterminated,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
fn isDigit(char: u8) bool {
|
||||||
|
return char >= '0' and char <= '9';
|
||||||
|
}
|
||||||
|
|
||||||
|
fn isAlpha(c: u8) bool {
|
||||||
|
return (c >= 'a' and c <= 'z') or
|
||||||
|
(c >= 'A' and c <= 'Z') or
|
||||||
|
c == '_';
|
||||||
|
}
|
||||||
|
|
||||||
|
fn isAlphaNumeric(char: u8) bool {
|
||||||
|
return isAlpha(char) or isDigit(char);
|
||||||
|
}
|
||||||
|
|
||||||
pub const Scanner = struct {
|
pub const Scanner = struct {
|
||||||
source: []const u8,
|
source: []const u8,
|
||||||
|
|
||||||
|
@ -117,12 +131,32 @@ pub const Scanner = struct {
|
||||||
return self.makeToken(.STRING);
|
return self.makeToken(.STRING);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Consume a number
|
||||||
|
fn doNumber(self: *Scanner) Token {
|
||||||
|
while (isDigit(self.peek())) {
|
||||||
|
_ = self.advance();
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if its a number like 12.34, where the '.' character
|
||||||
|
// exists and the one next to it is a digit.
|
||||||
|
if (self.peek() == '.' and isDigit(self.peekNext())) {
|
||||||
|
_ = self.advance();
|
||||||
|
|
||||||
|
while (isDigit(self.peek())) {
|
||||||
|
_ = self.advance();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return self.makeToken(.NUMBER);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn scanToken(self: *Scanner) !?Token {
|
pub fn scanToken(self: *Scanner) !?Token {
|
||||||
self.skipWhitespace();
|
self.skipWhitespace();
|
||||||
self.start = self.current;
|
self.start = self.current;
|
||||||
if (self.isAtEnd()) return self.makeToken(TokenType.EOF);
|
if (self.isAtEnd()) return self.makeToken(TokenType.EOF);
|
||||||
|
|
||||||
var c = self.advance();
|
var c = self.advance();
|
||||||
|
if (isDigit(c)) return self.doNumber();
|
||||||
|
|
||||||
var token = switch (c) {
|
var token = switch (c) {
|
||||||
'(' => self.makeToken(.LEFT_PAREN),
|
'(' => self.makeToken(.LEFT_PAREN),
|
||||||
|
|
Loading…
Reference in a new issue