99 lines
2.7 KiB
C++
99 lines
2.7 KiB
C++
#pragma once
|
|
|
|
#include <memory>
|
|
#include <optional>
|
|
#include <vector>
|
|
#include <string>
|
|
#include <variant>
|
|
|
|
class ASTValue;
|
|
class ASTFunction;
|
|
class ASTFunctionCall;
|
|
class ASTCodeBlock;
|
|
class ASTIdentifier;
|
|
|
|
typedef std::variant<std::shared_ptr<ASTValue>, std::shared_ptr<ASTFunction>, std::shared_ptr<ASTFunctionCall>, std::shared_ptr<ASTCodeBlock>, std::shared_ptr<ASTIdentifier>> ASTNode;
|
|
typedef std::variant<long long, double, std::string, bool> RealValue;
|
|
|
|
enum class ValueType {
|
|
Int, Float, String, Bool, None
|
|
};
|
|
|
|
enum class TokenType {
|
|
Identifier, Value, Function, FunctionCallStart, OpenParen, CloseParen, CodeBlockStart, CodeBlockEnd, NewLine, None
|
|
};
|
|
|
|
/**
|
|
* @class ASTValue
|
|
* @brief Represents a value in the Abstract Syntax Tree (AST).
|
|
*
|
|
* The ASTValue class encapsulates different types of values, including integers,
|
|
* floating-point numbers, strings, and boolean. It provides methods for type identification
|
|
* and value retrieval.
|
|
*/
|
|
class ASTValue {
|
|
private:
|
|
RealValue value;
|
|
public:
|
|
ValueType type;
|
|
ValueType getValueType(std::string in);
|
|
std::optional<std::string> getString();
|
|
std::optional<int> getInt();
|
|
std::optional<double> getFloat();
|
|
std::optional<bool> getBool();
|
|
explicit ASTValue(std::string in);
|
|
explicit ASTValue(long long in);
|
|
explicit ASTValue(double in);
|
|
explicit ASTValue(bool in);
|
|
ASTValue();
|
|
};
|
|
|
|
/**
|
|
* @class ASTCodeBlock
|
|
* @brief Represents a block of code in the Abstract Syntax Tree (AST).
|
|
*
|
|
* The ASTCodeBlock class is responsible for encapsulating and parsing a block
|
|
* of code represented as a sequence of strings. It maintains the raw content
|
|
* of the block, processes its elements, and classifies tokens to construct a
|
|
* structured representation. The parsed elements are stored as AST nodes.
|
|
*
|
|
* Functions provided include utilities for token parsing, peeking into
|
|
* upcoming tokens, identifying token types, and managing the iterator for
|
|
* sequential token processing.
|
|
*/
|
|
class ASTCodeBlock {
|
|
private:
|
|
std::vector<std::string> content;
|
|
size_t iterator = 0;
|
|
void parseBlock();
|
|
std::optional<std::string> consume();
|
|
std::optional<std::string> peek(int ahead = 1);
|
|
TokenType getTokenType();
|
|
public:
|
|
std::vector<ASTNode> nodes;
|
|
explicit ASTCodeBlock(std::vector<std::string>);
|
|
ASTCodeBlock();
|
|
};
|
|
|
|
class ASTFunction {
|
|
public:
|
|
ASTCodeBlock body;
|
|
explicit ASTFunction(ASTCodeBlock body);
|
|
ASTFunction();
|
|
};
|
|
|
|
class ASTFunctionCall {
|
|
public:
|
|
std::string func;
|
|
std::vector<ASTNode> args;
|
|
ASTFunctionCall(std::string func, std::vector<ASTNode> args);
|
|
};
|
|
|
|
class ASTIdentifier {
|
|
public:
|
|
std::string name;
|
|
explicit ASTIdentifier(std::string in);
|
|
};
|
|
|
|
ASTNode parser(std::vector<std::string> in);
|