forked from solstice/solstice
refactor
This commit is contained in:
91
src/parser.h
Normal file
91
src/parser.h
Normal file
@@ -0,0 +1,91 @@
|
||||
#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 {
|
||||
Add, Subtract, Equal, Inequal, Set, While, If, Value, Identifier, None, Root, CodeBlock, CodeBlockStart, CodeBlockEnd, FunctionCall, Expression, BracketStart, BracketEnd, Puts
|
||||
};
|
||||
|
||||
enum class SolDataType {
|
||||
Int, String, Double, Bool, Char, None
|
||||
};
|
||||
|
||||
class SolNode;
|
||||
|
||||
class SolGroundCodeBlock {
|
||||
public:
|
||||
std::vector<GroundInstruction> code;
|
||||
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
|
||||
Reference in New Issue
Block a user