commit 88f2f9885887916d3d699c62abc091396341bdec
parent e7faebb6ba14f20418524dd98261081bffad9d60
Author: Pranav Prabhu Kumble <kumblepranavprabhu@gmail.com>
Date: Thu, 26 Mar 2026 14:36:55 +0530
Week 2: Implement Lexer (Part 1: Initial definitions)
Diffstat:
2 files changed, 41 insertions(+), 0 deletions(-)
diff --git a/src/lexer.cpp b/src/lexer.cpp
@@ -0,0 +1,26 @@
+#include "lexer.h"
+#include <stdexcept>
+
+std::string tokenTypeToString(TokenType type) {
+ switch (type) {
+ case TokenType::LITERAL: return "LITERAL";
+ case TokenType::DOT: return "DOT";
+ case TokenType::STAR: return "STAR";
+ case TokenType::PLUS: return "PLUS";
+ case TokenType::QUESTION: return "QUESTION";
+ case TokenType::PIPE: return "PIPE";
+ case TokenType::LPAREN: return "LPAREN";
+ case TokenType::RPAREN: return "RPAREN";
+ case TokenType::END_OF_INPUT: return "END_OF_INPUT";
+ default: return "UNKNOWN";
+ }
+}
+
+Lexer::Lexer(const std::string& input, int lineNum)
+ : input(input), pos(0), line(lineNum), col(1) {}
+
+char Lexer::peek() const {
+ if (isAtEnd()) return '\0';
+ return input[pos];
+}
+
diff --git a/src/lexer.h b/src/lexer.h
@@ -0,0 +1,15 @@
+#ifndef LEXER_H
+#define LEXER_H
+
+#include <string>
+#include <vector>
+#include <ostream>
+
+enum class TokenType {
+ LITERAL,
+ DOT,
+ STAR,
+ PLUS,
+ QUESTION,
+ PIPE,
+ LPAREN,