-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMetaCircularParser.mpp
More file actions
131 lines (107 loc) · 5.92 KB
/
MetaCircularParser.mpp
File metadata and controls
131 lines (107 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
export module CppUtils.Language.MetaCircularParser;
import std;
import CppUtils.Language.MetaCircularVirtualMachine;
import CppUtils.String;
import CppUtils.Type;
export namespace CppUtils::Language
{
template<String::StringView Source>
struct MetaCircularParser: public MetaCircularVirtualMachine
{
Source source;
inline MetaCircularParser(std::shared_ptr<Environment> environment, Source input):
MetaCircularVirtualMachine{std::move(environment)},
source{input}
{
initialize();
}
inline MetaCircularParser(Source input):
source{input}
{
initialize();
}
[[nodiscard]] inline auto makeExecutionContext(std::shared_ptr<Environment> environment) const -> std::unique_ptr<MetaCircularVirtualMachine> override
{
return std::make_unique<MetaCircularParser>(std::move(environment), source);
}
inline auto initialize() -> void
{
using namespace std::literals;
using namespace String::Literals;
addFunction<MetaCircularParser>("length"_token, [](MetaCircularParser& interpreter, MeshNodePtr instruction) -> std::expected<MeshNodePtr, std::string_view> {
auto lengthNode = instruction.at("length"_token).and_then([](auto branch) { return branch.front(); });
if (not lengthNode)
return std::unexpected{"length: No result node"sv};
lengthNode->setValue(std::size(interpreter.source));
return next(instruction);
});
addFunction<MetaCircularParser>("read"_token, [](MetaCircularParser& interpreter, MeshNodePtr instruction) -> std::expected<MeshNodePtr, std::string_view> {
auto positionNode = instruction.at("position"_token).and_then([](auto branch) { return branch.front(); });
if (not positionNode)
return std::unexpected{"read: No position"sv};
const auto position = positionNode->getValue().value();
if (position >= std::size(interpreter.source))
return std::unexpected{"read: End of input"sv};
auto charNode = instruction.at("char"_token).and_then([](auto branch) { return branch.front(); });
if (not charNode)
return std::unexpected{"read: No result"sv};
charNode->setValue(static_cast<std::size_t>(interpreter.source[position]));
return next(instruction);
});
addFunction<MetaCircularParser>("hash"_token, [](MetaCircularParser&, MeshNodePtr instruction) -> std::expected<MeshNodePtr, std::string_view> {
auto charNode = instruction.at("char"_token).and_then([](auto branch) { return branch.front(); });
if (not charNode)
return std::unexpected{"hash: No character"sv};
const auto character = charNode->getValue().value();
auto accumulatorNode = instruction.at("accumulator"_token).and_then([](auto branch) { return branch.front(); });
const auto currentHash = accumulatorNode ? accumulatorNode->getValue().value() : Type::HashOffset;
auto hashNode = instruction.at("hash"_token).and_then([](auto branch) { return branch.front(); });
if (not hashNode)
return std::unexpected{"hash: No result"sv};
const auto result = (static_cast<std::uint64_t>(character) ^ currentHash) * Type::HashPrime;
hashNode->setValue(result);
return next(instruction);
});
auto lowLevelTokenParser = context["scope"_token] >> MeshNodePtr::make("lowLevelTokenParser"_token);
auto workingVariablesBranch = lowLevelTokenParser["workingVariables"_token];
auto hashOffset = workingVariablesBranch >> MeshNodePtr::make(CppUtils::Type::HashOffset);
auto currentCharacter = workingVariablesBranch >> MeshNodePtr::make(0uz);
auto currentPosition = workingVariablesBranch >> MeshNodePtr::make(0uz);
auto inputLength = workingVariablesBranch >> MeshNodePtr::make(0uz);
auto result = workingVariablesBranch >> MeshNodePtr::make(0uz);
auto one = workingVariablesBranch >> MeshNodePtr::make(1uz);
auto space = workingVariablesBranch >> MeshNodePtr::make(static_cast<std::size_t>(' '));
auto tab = workingVariablesBranch >> MeshNodePtr::make(static_cast<std::size_t>('\t'));
auto newline = workingVariablesBranch >> MeshNodePtr::make(static_cast<std::size_t>('\n'));
auto verticalTab = workingVariablesBranch >> MeshNodePtr::make(static_cast<std::size_t>('\v'));
auto formFeed = workingVariablesBranch >> MeshNodePtr::make(static_cast<std::size_t>('\f'));
auto carriageReturn = workingVariablesBranch >> MeshNodePtr::make(static_cast<std::size_t>('\r'));
auto parseToken = lowLevelTokenParser["parseToken"_token] >> MeshNodePtr::make("jump"_token);
{
auto currentHash = parseToken["result"_token] >> hashOffset;
auto getInputLength = parseToken["next"_token] >> MeshNodePtr::make("length"_token);
getInputLength["length"_token] >> inputLength;
auto isPositionLessThanInputSize = getInputLength["next"_token] >> MeshNodePtr::make("<"_token);
isPositionLessThanInputSize["lhs"_token] >> currentPosition;
isPositionLessThanInputSize["rhs"_token] >> inputLength;
isPositionLessThanInputSize["result"_token] >> result;
auto conditionForReadingCharacter = isPositionLessThanInputSize["next"_token] >> MeshNodePtr::make("jump"_token);
conditionForReadingCharacter["condition"_token] >> result;
auto readCurrentCharacter = conditionForReadingCharacter["next"_token] >> MeshNodePtr::make("read"_token);
readCurrentCharacter["position"_token] >> currentPosition;
readCurrentCharacter["char"_token] >> currentCharacter;
auto hashCharacter = readCurrentCharacter["next"_token] >> MeshNodePtr::make("hash"_token);
hashCharacter["char"_token] >> currentCharacter;
hashCharacter["accumulator"_token] >> currentHash;
hashCharacter["hash"_token] >> currentHash;
auto incrementPosition = hashCharacter["next"_token] >> MeshNodePtr::make("+"_token);
incrementPosition["lhs"_token] >> currentPosition;
incrementPosition["rhs"_token] >> one;
incrementPosition["result"_token] >> currentPosition;
incrementPosition["next"_token] >> isPositionLessThanInputSize;
}
auto skipWhitespaces = lowLevelTokenParser["skipWhitespaces"_token] >> MeshNodePtr::make("jump"_token);
{}
}
};
}