Files
highground-fork/src/parser.h

93 lines
2.7 KiB
C
Raw Normal View History

2025-12-21 12:06:55 +11:00
#ifndef PARSER_H
#define PARSER_H
#include <groundvm.h>
#include <vector>
#include <optional>
#include <string>
#include <variant>
namespace Solstice {
// External variables referenced in parser logic
extern int tmpIdIterator;
extern int labelIterator;
namespace Parser {
enum class SolNodeType {
2025-12-22 13:49:44 +11:00
Add, Subtract, Equal, Inequal, Greater, Lesser, EqGreater, EqLesser, Set, While, If, Value, Identifier, None, Root, CodeBlock, CodeBlockStart, CodeBlockEnd, FunctionCall, Expression, BracketStart, BracketEnd, Puts
2025-12-21 12:06:55 +11:00
};
enum class SolDataType {
Int, String, Double, Bool, Char, None
};
class SolNode;
class SolGroundCodeBlock {
public:
std::vector<GroundInstruction> code;
std::vector<std::string> toBeDropped;
2025-12-21 12:06:55 +11:00
SolGroundCodeBlock() = default;
};
class SolData {
typedef std::variant<int64_t, std::string, double, bool, char> varData;
varData data;
public:
SolDataType type = SolDataType::Int;
SolData() = default;
SolData(int64_t in);
SolData(double in);
SolData(std::string in);
SolData(char in);
SolData(bool in);
std::optional<int64_t> getInt();
std::optional<double> getDouble();
std::optional<std::string> getString();
std::optional<char> getChar();
std::optional<bool> getBool();
};
class SolNode {
SolData data;
public:
SolNodeType nodeType = SolNodeType::None;
std::vector<SolNode> children;
std::string outputId;
SolNode(SolNodeType nodeType);
SolNode(SolNodeType nodeType, SolData data);
SolNode() = default;
void addNode(SolNode in);
void setValue(SolData in);
const std::vector<SolGroundCodeBlock> generateCode();
};
class Parser {
std::vector<std::string> tokensToParse;
size_t current;
size_t size;
std::optional<std::string> peek(int ahead = 1);
std::optional<std::string> consume();
bool isInt(std::string in);
bool isDouble(std::string in);
bool isString(std::string in);
bool isChar(std::string in);
bool isBool(std::string in);
SolDataType getDataType(std::string in);
SolNodeType getNodeType(std::string in);
public:
Parser(std::vector<std::string> in);
SolNode parse();
};
GroundProgram assembleProgram(SolNode& rootNode);
} // namespace Parser
}
#endif