- Add function declarations, calls, and return statements - Implement lexical scoping with Environment class and closures - Convert print from statement to standard library function - Add assert() function to standard library for testing - Add time() function for microsecond precision benchmarking - Create StdLib class and BuiltinFunction wrapper for standard library - Implement first-class functions and higher-order functions - Add function parameter support (tested up to 100 parameters) - Support alphanumeric identifiers in variable and function names - Add underscore support in variable names and identifiers - Implement string + number and number + string concatenation - Add boolean + string and string + boolean concatenation - Support string multiplication (string * number) - Fix decimal truncation issue by using std::stod for all number parsing - Add comprehensive number formatting with proper precision handling - Support huge numbers (epoch timestamps) without integer overflow - Clean number display (no trailing zeros on integers) - Add basic error handling with program termination on errors - Add comprehensive test suite covering all features - Add escape sequence support (\n, \t, \", \\) - Add comprehensive documentation and language reference - Update development roadmap with completed features
84 lines
2.0 KiB
C++
84 lines
2.0 KiB
C++
#pragma once
|
|
|
|
#include <string>
|
|
#include <map>
|
|
#include <vector>
|
|
|
|
enum TokenType{
|
|
OPEN_PAREN, CLOSE_PAREN, OPEN_BRACE, CLOSE_BRACE,
|
|
COMMA, DOT, MINUS, PLUS, SEMICOLON, SLASH, STAR, PERCENT,
|
|
|
|
BIN_OR, BIN_AND, BIN_NOT, BIN_XOR, BIN_SLEFT, BIN_SRIGHT,
|
|
|
|
BANG, BANG_EQUAL,
|
|
EQUAL, DOUBLE_EQUAL,
|
|
GREATER, GREATER_EQUAL,
|
|
LESS, LESS_EQUAL,
|
|
|
|
IDENTIFIER, STRING, NUMBER, BOOL,
|
|
|
|
AND, OR, TRUE, FALSE, IF, ELSE, FUNCTION, FOR,
|
|
WHILE, VAR, CLASS, SUPER, THIS, NONE, RETURN,
|
|
|
|
END_OF_FILE
|
|
};
|
|
|
|
inline std::string enum_mapping[] = {"OPEN_PAREN", "CLOSE_PAREN", "OPEN_BRACE", "CLOSE_BRACE",
|
|
"COMMA", "DOT", "MINUS", "PLUS", "SEMICOLON", "SLASH", "STAR", "PERCENT",
|
|
|
|
"BINARY_OP",
|
|
|
|
"BANG", "BANG_EQUAL",
|
|
"EQUAL", "DOUBLE_EQUAL",
|
|
"GREATER", "GREATER_EQUAL",
|
|
"LESS", "LESS_EQUAL",
|
|
|
|
"IDENTIFIER", "STRING", "NUMBER", "BOOL",
|
|
|
|
"AND", "OR", "TRUE", "FALSE", "IF", "ELSE", "FUNCTION", "FOR",
|
|
"WHILE", "VAR", "CLASS", "SUPER", "THIS", "NONE", "RETURN",
|
|
|
|
"END_OF_FILE"};
|
|
|
|
const std::map<std::string, TokenType> KEYWORDS {
|
|
{"and", AND},
|
|
{"or", OR},
|
|
{"true", TRUE},
|
|
{"false", FALSE},
|
|
{"if", IF},
|
|
{"else", ELSE},
|
|
{"func", FUNCTION},
|
|
{"for", FOR},
|
|
{"while", WHILE},
|
|
{"var", VAR},
|
|
{"class", CLASS},
|
|
{"super", SUPER},
|
|
{"this", THIS},
|
|
{"none", NONE},
|
|
{"return", RETURN},
|
|
};
|
|
|
|
struct Token
|
|
{
|
|
TokenType type;
|
|
std::string lexeme;
|
|
int line;
|
|
};
|
|
|
|
|
|
class Lexer{
|
|
public:
|
|
std::vector<Token> Tokenize(std::string source);
|
|
private:
|
|
int line;
|
|
std::vector<char> src;
|
|
private:
|
|
bool matchOn(char expected);
|
|
|
|
char peekNext();
|
|
|
|
void advance(int by = 1);
|
|
|
|
std::string parseEscapeCharacters(const std::string &input);
|
|
};
|