commit a5b70ff5ec49e8926f5eea753b938153bae552b5
parent 122d645d8136c4e830f7f3a89003869b34e97bd2
Author: Andrew Laack <andrew@laack.co>
Date: Tue, 20 Oct 2026 23:37:14 -0500
String and number lexing
Diffstat:
3 files changed, 144 insertions(+), 6 deletions(-)
diff --git a/lox/main.lox b/lox/main.lox
@@ -1 +1 @@
-(){}()
+2."test"
diff --git a/lox/scanner.cpp b/lox/scanner.cpp
@@ -26,15 +26,90 @@ class Scanner {
std::string source;
std::vector<Token> tokens;
- bool isDone() {
- return current >= source.size();
- }
char advance() {
char c = source[current];
current += 1;
return c;
}
+ bool isDone() {
+ return current >= source.size();
+ }
+
+ char peek() {
+ if(current < source.size()) {
+ return source[current];
+ }
+ return '\0';
+ }
+
+ bool isDigit() {
+ return (peek() <= '9' && peek() >= '0');
+ }
+ bool isDigit(char ch) {
+ return (ch <= '9' && ch >= '0');
+ }
+
+ bool match(char c) {
+ if(source.size() > current) {
+ bool res = (c == source[current]);
+ if(res) {
+ advance();
+ }
+ return res;
+ }
+ return false;
+ }
+
+ void number() {
+
+ std::string numStr = "";
+ numStr += source[current-1];
+
+ char ch = peek();
+ bool firstDot = false;
+ while(isDigit() || (ch == '.' && !firstDot && isDigit(peek()))) {
+ if(ch == '.') {
+ firstDot = true;
+ }
+ numStr += ch;
+ if(current < source.size()) {
+ ch = advance();
+ } else {
+ break; // eof
+ }
+ }
+ double res = std::stod(numStr);
+ Literal lit = Literal("", res, false);
+ addToken(TokenType::NUMBER,lit);
+ }
+
+ void string() {
+ std::string strLiteral = "";
+ char ch;
+
+ if(current < source.size()) {
+ ch = advance();
+ } else {
+ lRef->error(line, "Unterminated string.");
+ return;
+ }
+
+ while(ch != '"') {
+ if(ch == '\n') {
+ line++;
+ }
+ strLiteral += ch;
+ if(current < source.size()) {
+ ch = advance();
+ } else {
+ lRef->error(line, "Unterminated string.");
+ return;
+ }
+ }
+ addToken(TokenType::STRING,{strLiteral});
+ }
+
void addToken(TokenType type, Literal lit) {
std::string text = source.substr(start,current-start);
Token tk = Token(type, text, lit, line);
@@ -78,8 +153,59 @@ class Scanner {
case '*':
addToken(TokenType::STAR);
break;
+ case '\n':
+ line += 1;
+ break;
+ case ' ':
+ case '\t':
+ case '\r':
+ break;
+ case '!':
+ if(match('=')){
+ addToken(TokenType::BANG_EQUAL);
+ } else {
+ addToken(TokenType::BANG);
+ }
+ break;
+ case '=':
+ if(match('=')){
+ addToken(TokenType::EQUAL_EQUAL);
+ } else {
+ addToken(TokenType::EQUAL);
+ }
+ break;
+ case '<':
+ if(match('=')){
+ addToken(TokenType::LESS_EQUAL);
+ } else {
+ addToken(TokenType::LESS);
+ }
+ break;
+ case '>':
+ if(match('=')){
+ addToken(TokenType::GREATER_EQUAL);
+ } else {
+ addToken(TokenType::GREATER);
+ }
+ break;
+ case '/':
+ if(match('/')){
+ while(peek() != '\n' && current < source.size()) {
+ advance();
+ }
+ } else {
+ addToken(TokenType::SLASH);
+ }
+ break;
+ case '"':
+ string();
+ break;
default:
- lRef->error(line, "Unexpected character.");
+ if(isDigit(c)) {
+ number();
+ } else {
+ lRef->error(line, "Unexpected character.");
+ }
break;
}
}
diff --git a/lox/token.cpp b/lox/token.cpp
@@ -18,8 +18,20 @@ enum class TokenType {
class Literal {
public:
+ std::string repString;
+ double repNum;
+ bool isString;
+ Literal(std::string st = "", double num = 0.0, bool isSt = true) {
+ repString = st;
+ isString = isSt;
+ repNum = num;
+ }
std::string toString() {
- return "";
+ if(isString) {
+ return repString;
+ } else {
+ return std::to_string(repNum);
+ }
}
};