mirror of
https://github.com/allankoechke/cpp-expr-eval.git
synced 2026-12-03 09:13:58 +00:00
Initial Commit
This commit is contained in:
36
includes/expr-eval/backend/interpreter.h
Normal file
36
includes/expr-eval/backend/interpreter.h
Normal file
@@ -0,0 +1,36 @@
|
||||
#ifndef INTERPRETER_H
|
||||
#define INTERPRETER_H
|
||||
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <format>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "runtime.h"
|
||||
#include "../frontend/parser.h"
|
||||
|
||||
class Interpreter {
|
||||
public:
|
||||
Interpreter() = default;
|
||||
|
||||
RuntimeVar eval(const std::string& input) {
|
||||
parser.parse(input);
|
||||
return parser.root().eval(m_vars);
|
||||
}
|
||||
|
||||
void addVar(const std::string& ident, RuntimeVar val) {
|
||||
m_vars[ident] = val;
|
||||
}
|
||||
|
||||
RuntimeVar getVar(const std::string& ident) {
|
||||
if(!m_vars.contains(ident))
|
||||
throw std::runtime_error(std::format("Undeclared variable `{}` not found!", ident));
|
||||
return m_vars[ident];
|
||||
}
|
||||
|
||||
private:
|
||||
Parser parser;
|
||||
std::unordered_map<std::string, RuntimeVar> m_vars;
|
||||
};
|
||||
|
||||
#endif // INTERPRETER_H
|
||||
122
includes/expr-eval/backend/runtime.h
Normal file
122
includes/expr-eval/backend/runtime.h
Normal file
@@ -0,0 +1,122 @@
|
||||
#ifndef RUNTIME_H
|
||||
#define RUNTIME_H
|
||||
|
||||
#include <complex>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <format>
|
||||
|
||||
struct RuntimeVar {
|
||||
enum class RuntimeVarType{
|
||||
STRING,
|
||||
NUMBER,
|
||||
NIL,
|
||||
BOOL
|
||||
} type;
|
||||
std::string value;
|
||||
bool b_value = false;
|
||||
double d_value = 0.0;
|
||||
|
||||
RuntimeVar()
|
||||
: type(RuntimeVarType::NIL),
|
||||
value("nil") {}
|
||||
|
||||
explicit RuntimeVar(const std::string& v)
|
||||
: type(RuntimeVarType::STRING),
|
||||
value(v) {}
|
||||
|
||||
explicit RuntimeVar(const bool t)
|
||||
: type(RuntimeVarType::BOOL),
|
||||
b_value(t),
|
||||
value(t ? "true" : "false") {}
|
||||
|
||||
explicit RuntimeVar(const double d)
|
||||
: type(RuntimeVarType::NUMBER),
|
||||
d_value(d),
|
||||
value(std::format("{}", d)) {}
|
||||
|
||||
std::string typeStr() {
|
||||
switch(type) {
|
||||
case RuntimeVarType::STRING: return "STRING";
|
||||
case RuntimeVarType::NUMBER: return "NUMBER";
|
||||
case RuntimeVarType::NIL: return "NIL";
|
||||
case RuntimeVarType::BOOL: return "BOOL";
|
||||
default: return "?";
|
||||
}
|
||||
}
|
||||
|
||||
double toDouble() {
|
||||
if(type == RuntimeVarType::NUMBER)
|
||||
return stod(value);
|
||||
|
||||
throw std::runtime_error("Expected number type");
|
||||
}
|
||||
|
||||
std::string toString() {
|
||||
return value;
|
||||
}
|
||||
|
||||
bool toBool() {
|
||||
if(value=="false" || value == "nil") return false;
|
||||
if(type == RuntimeVarType::NUMBER) return toDouble() != 0.0f;
|
||||
return true;
|
||||
}
|
||||
|
||||
RuntimeVar operator+(RuntimeVar& other) {
|
||||
if(type != other.type)
|
||||
throw std::runtime_error(std::format("Expected same types to op '+' but found {} and {}.",
|
||||
typeStr(), other.typeStr()));
|
||||
|
||||
if(type == RuntimeVarType::STRING) {
|
||||
return RuntimeVar{ value + other.value };
|
||||
}
|
||||
|
||||
if(type == RuntimeVarType::NUMBER) {
|
||||
auto res = toDouble() + other.toDouble();
|
||||
return RuntimeVar{ res };
|
||||
}
|
||||
|
||||
throw std::runtime_error(std::format("Op '+' not supported for type {}", typeStr()));
|
||||
}
|
||||
|
||||
RuntimeVar operator-(RuntimeVar& other) {
|
||||
if(type != other.type)
|
||||
throw std::runtime_error(std::format("Expected same types to op '+' but found {} and {}.",
|
||||
typeStr(), other.typeStr()));
|
||||
|
||||
if(type == RuntimeVarType::NUMBER) {
|
||||
auto res = toDouble() - other.toDouble();
|
||||
return RuntimeVar{ res };
|
||||
}
|
||||
|
||||
throw std::runtime_error(std::format("Op '+' not supported for type {}", typeStr()));
|
||||
}
|
||||
|
||||
RuntimeVar operator*(RuntimeVar& other) {
|
||||
if(type != other.type)
|
||||
throw std::runtime_error(std::format("Expected same types to op '+' but found {} and {}.",
|
||||
typeStr(), other.typeStr()));
|
||||
|
||||
if(type == RuntimeVarType::NUMBER) {
|
||||
auto res = toDouble() * other.toDouble();
|
||||
return RuntimeVar{ res };
|
||||
}
|
||||
|
||||
throw std::runtime_error(std::format("Op '+' not supported for type {}", typeStr()));
|
||||
}
|
||||
|
||||
RuntimeVar operator/(RuntimeVar& other) {
|
||||
if(type != other.type)
|
||||
throw std::runtime_error(std::format("Expected same types to op '+' but found {} and {}.",
|
||||
typeStr(), other.typeStr()));
|
||||
|
||||
if(type == RuntimeVarType::NUMBER) {
|
||||
auto res = toDouble() / other.toDouble();
|
||||
return RuntimeVar{ res };
|
||||
}
|
||||
|
||||
throw std::runtime_error(std::format("Op '+' not supported for type {}", typeStr()));
|
||||
}
|
||||
};
|
||||
|
||||
#endif // RUNTIME_H
|
||||
150
includes/expr-eval/frontend/ast.h
Normal file
150
includes/expr-eval/frontend/ast.h
Normal file
@@ -0,0 +1,150 @@
|
||||
#ifndef AST_H
|
||||
#define AST_H
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "../backend/runtime.h"
|
||||
#include "../picojson.h"
|
||||
|
||||
|
||||
// ----- NODE TYPE ----- //
|
||||
enum class NodeType {
|
||||
PROGRAM,
|
||||
|
||||
// Primitive types
|
||||
STRING_LIT,
|
||||
BOOLEAN_LIT,
|
||||
NUMBER_LIT,
|
||||
NIL_LIT,
|
||||
IDENT_LIT,
|
||||
|
||||
// Composites
|
||||
BINARY_EXPR,
|
||||
|
||||
// EOF
|
||||
END_OF_FILE
|
||||
};
|
||||
|
||||
// ----- NODE ----- //
|
||||
struct Node {
|
||||
std::string name;
|
||||
NodeType type;
|
||||
|
||||
Node(NodeType t, std::string name);
|
||||
|
||||
virtual ~Node();
|
||||
|
||||
virtual picojson::value dump();
|
||||
|
||||
virtual RuntimeVar eval(std::unordered_map<std::string, RuntimeVar> &vars);
|
||||
};
|
||||
|
||||
|
||||
// ----- PROGRAM NODE ----- //
|
||||
struct Program : Node {
|
||||
Program();
|
||||
|
||||
void addNode(std::unique_ptr<Node> node);
|
||||
|
||||
void clear();
|
||||
|
||||
picojson::value dump() override;
|
||||
|
||||
RuntimeVar eval(std::unordered_map<std::string, RuntimeVar> &vars) override;
|
||||
|
||||
private:
|
||||
std::vector<std::unique_ptr<Node> > m_ast;
|
||||
};
|
||||
|
||||
|
||||
// ----- EXPR NODE ----- //
|
||||
struct Expr : Node {
|
||||
Expr(NodeType t, const std::string &name);
|
||||
};
|
||||
|
||||
|
||||
// ----- BINARY EXPR NODE ----- //
|
||||
struct BinaryExpr : Expr {
|
||||
BinaryExpr(std::unique_ptr<Node> left, std::string op, std::unique_ptr<Node> right);
|
||||
|
||||
picojson::value dump() override;
|
||||
|
||||
RuntimeVar eval(std::unordered_map<std::string, RuntimeVar> &vars) override;
|
||||
|
||||
private:
|
||||
std::unique_ptr<Node> left, right;
|
||||
std::string op;
|
||||
};
|
||||
|
||||
|
||||
// ----- NUMBER LITERAL NODE ----- //
|
||||
struct NumberLiteral : Expr {
|
||||
explicit NumberLiteral(const std::string &value);
|
||||
|
||||
picojson::value dump() override;
|
||||
|
||||
RuntimeVar eval(std::unordered_map<std::string, RuntimeVar> &vars) override;
|
||||
|
||||
private:
|
||||
std::string value;
|
||||
double d;
|
||||
};
|
||||
|
||||
|
||||
// ----- BOOLEAN LITERAL NODE ----- //
|
||||
struct BooleanLiteral : Expr {
|
||||
explicit BooleanLiteral(const std::string &value);
|
||||
|
||||
picojson::value dump() override;
|
||||
|
||||
RuntimeVar eval(std::unordered_map<std::string, RuntimeVar> &vars) override;
|
||||
|
||||
private:
|
||||
std::string value;
|
||||
bool b_value;
|
||||
};
|
||||
|
||||
|
||||
// ----- STRING LITERAL NODE ----- //
|
||||
struct StringLiteral : Expr {
|
||||
explicit StringLiteral(std::string value);
|
||||
|
||||
picojson::value dump() override;
|
||||
|
||||
RuntimeVar eval(std::unordered_map<std::string, RuntimeVar> &vars) override;
|
||||
|
||||
private:
|
||||
std::string value;
|
||||
};
|
||||
|
||||
|
||||
// ----- IDENTIFIER LITERAL NODE ----- //
|
||||
struct IdentifierLiteral : Expr {
|
||||
explicit IdentifierLiteral(std::string value);
|
||||
|
||||
picojson::value dump() override;
|
||||
|
||||
RuntimeVar eval(std::unordered_map<std::string, RuntimeVar> &vars) override;
|
||||
|
||||
private:
|
||||
std::string value;
|
||||
};
|
||||
|
||||
|
||||
// ----- NULL LITERAL NODE ----- //
|
||||
struct NullLiteral : Expr {
|
||||
NullLiteral();
|
||||
|
||||
picojson::value dump() override;
|
||||
|
||||
RuntimeVar eval(std::unordered_map<std::string, RuntimeVar> &vars) override;
|
||||
|
||||
private:
|
||||
std::string value = "nil";
|
||||
};
|
||||
|
||||
|
||||
#endif // AST_H
|
||||
68
includes/expr-eval/frontend/lexer.h
Normal file
68
includes/expr-eval/frontend/lexer.h
Normal file
@@ -0,0 +1,68 @@
|
||||
#ifndef LEXER_H
|
||||
#define LEXER_H
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
enum class TokenType {
|
||||
TOK_NUMBERS_LIT,
|
||||
TOK_STRING_LIT,
|
||||
TOK_BOOL_LIT,
|
||||
TOK_NULL_LIT,
|
||||
TOK_IDENT_LIT,
|
||||
|
||||
TOK_ADD_OP,
|
||||
TOK_SUB_OP,
|
||||
TOK_DIV_OP,
|
||||
TOK_MULT_OP,
|
||||
|
||||
TOK_OPEN_PAREN, // (
|
||||
TOK_CLOSE_PAREN, // )
|
||||
|
||||
TOK_EOF
|
||||
};
|
||||
|
||||
struct Token {
|
||||
TokenType type;
|
||||
std::string name, value;
|
||||
|
||||
Token(TokenType _type, std::string _name, std::string _value);
|
||||
};
|
||||
|
||||
class Lexer {
|
||||
public:
|
||||
Lexer() = default;
|
||||
|
||||
// Given user input, convert it to vector of token object
|
||||
const std::vector<Token> &tokenize(const std::string &src);
|
||||
|
||||
// Return the vector of token object
|
||||
const std::vector<Token> &tokens();
|
||||
|
||||
private:
|
||||
void tokenizeNumbers();
|
||||
|
||||
void tokenizeString();
|
||||
|
||||
void tokenizeIdentifiers();
|
||||
|
||||
void tokenizeOperators();
|
||||
|
||||
void skipWhitespaces();
|
||||
|
||||
[[nodiscard]] char peek() const;
|
||||
|
||||
char advance();
|
||||
|
||||
[[nodiscard]] bool eof() const;
|
||||
|
||||
void expect(char c, const std::string &error);
|
||||
|
||||
|
||||
int m_cursor = 0;
|
||||
std::string m_src;
|
||||
std::vector<Token> m_tokens;
|
||||
};
|
||||
|
||||
#endif // LEXER_H
|
||||
45
includes/expr-eval/frontend/parser.h
Normal file
45
includes/expr-eval/frontend/parser.h
Normal file
@@ -0,0 +1,45 @@
|
||||
#ifndef PARSER_H
|
||||
#define PARSER_H
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "../picojson.h"
|
||||
#include "lexer.h"
|
||||
#include "ast.h"
|
||||
|
||||
class Parser {
|
||||
public:
|
||||
Parser() = default;
|
||||
|
||||
void parse(const std::string &src);
|
||||
|
||||
picojson::value dump();
|
||||
|
||||
Program &root();
|
||||
|
||||
private:
|
||||
std::unique_ptr<Node> parseExpr();
|
||||
|
||||
std::unique_ptr<Node> parseAdditives();
|
||||
|
||||
std::unique_ptr<Node> parseFactors();
|
||||
|
||||
std::unique_ptr<Node> parsePrimary();
|
||||
|
||||
Token peek();
|
||||
|
||||
Token advance();
|
||||
|
||||
bool eof();
|
||||
|
||||
void expect(TokenType t, const std::string &error);
|
||||
|
||||
|
||||
int m_cursor = 0;
|
||||
Lexer lexer;
|
||||
|
||||
// Store root node for the AST, in our case the Program node
|
||||
Program m_program;
|
||||
};
|
||||
|
||||
#endif // PARSER_H
|
||||
1200
includes/expr-eval/picojson.h
Normal file
1200
includes/expr-eval/picojson.h
Normal file
File diff suppressed because it is too large
Load Diff
0
includes/expr-eval/utils.h
Normal file
0
includes/expr-eval/utils.h
Normal file
Reference in New Issue
Block a user