Repository navigation
Expand file tree
/
Copy pathMetaCircularParser.mpp
More file actions
168 lines (135 loc) · 7.64 KB
/
Copy pathMetaCircularParser.mpp
File metadata and controls
168 lines (135 loc) · 7.64 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
export module CppUtils.Language.MetaCircularParser;
import std;
import CppUtils.Thread.SharedPtr;
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(Thread::SharedPtr<Environment> environment, Source input):
MetaCircularVirtualMachine{std::move(environment)},
source{input}
{
initialize();
}
inline MetaCircularParser(Source input):
source{input}
{
initialize();
}
[[nodiscard]] inline auto makeExecutionContext(Thread::SharedPtr<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::ranges::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::ranges::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 constants = lowLevelTokenParser["constants"_token] >> MeshNodePtr::make("constants"_token);
auto hashOffset = constants["hashOffset"_token] >> MeshNodePtr::make(CppUtils::Type::HashOffset);
auto zero = constants["zero"_token] >> MeshNodePtr::make(0uz);
auto one = constants["one"_token] >> MeshNodePtr::make(1uz);
auto spacePlusOne = constants["spacePlusOne"_token] >> MeshNodePtr::make(static_cast<std::size_t>(' ') + 1uz); // 33uz : tout char ASCII <= 32 (' ', '\t', '\n', '\r', '\v', '\f') est un whitespace
auto variables = lowLevelTokenParser["variables"_token] >> MeshNodePtr::make("variables"_token);
auto currentCharacter = variables["currentCharacter"_token] >> MeshNodePtr::make(0uz);
auto currentPosition = variables["currentPosition"_token] >> MeshNodePtr::make(0uz);
auto inputLength = variables["inputLength"_token] >> MeshNodePtr::make(std::ranges::size(source));
auto conditionResult = variables["conditionResult"_token] >> MeshNodePtr::make(0uz);
auto currentHash = variables["currentHash"_token] >> MeshNodePtr::make(CppUtils::Type::HashOffset);
auto skipWhitespaces = lowLevelTokenParser["skipWhitespaces"_token] >> MeshNodePtr::make("<"_token);
{
skipWhitespaces["lhs"_token] >> currentPosition;
skipWhitespaces["rhs"_token] >> inputLength;
skipWhitespaces["result"_token] >> conditionResult;
auto branchBounds = skipWhitespaces["next"_token] >> MeshNodePtr::make("jump"_token);
branchBounds["condition"_token] >> conditionResult;
auto readChar = branchBounds["next"_token] >> MeshNodePtr::make("read"_token);
readChar["position"_token] >> currentPosition;
readChar["char"_token] >> currentCharacter;
auto isWhitespace = readChar["next"_token] >> MeshNodePtr::make("<"_token);
isWhitespace["lhs"_token] >> currentCharacter;
isWhitespace["rhs"_token] >> spacePlusOne;
isWhitespace["result"_token] >> conditionResult;
auto branchWhitespace = isWhitespace["next"_token] >> MeshNodePtr::make("jump"_token);
branchWhitespace["condition"_token] >> conditionResult;
auto advanceCursor = branchWhitespace["next"_token] >> MeshNodePtr::make("+"_token);
advanceCursor["lhs"_token] >> currentPosition;
advanceCursor["rhs"_token] >> one;
advanceCursor["result"_token] >> currentPosition;
advanceCursor["next"_token] >> skipWhitespaces;
}
auto parseToken = lowLevelTokenParser["parseToken"_token] >> MeshNodePtr::make("set"_token);
{
parseToken["result"_token] >> currentHash;
parseToken["target"_token] >> currentHash;
parseToken["value"_token] >> hashOffset;
auto callSkip = parseToken["next"_token] >> MeshNodePtr::make("call"_token);
callSkip["function"_token] >> skipWhitespaces;
auto isPositionLessThanInputSize = callSkip["next"_token] >> MeshNodePtr::make("<"_token);
isPositionLessThanInputSize["lhs"_token] >> currentPosition;
isPositionLessThanInputSize["rhs"_token] >> inputLength;
isPositionLessThanInputSize["result"_token] >> conditionResult;
auto conditionForReadingCharacter = isPositionLessThanInputSize["next"_token] >> MeshNodePtr::make("jump"_token);
conditionForReadingCharacter["condition"_token] >> conditionResult;
auto readCurrentCharacter = conditionForReadingCharacter["next"_token] >> MeshNodePtr::make("read"_token);
readCurrentCharacter["position"_token] >> currentPosition;
readCurrentCharacter["char"_token] >> currentCharacter;
auto isWhitespace = readCurrentCharacter["next"_token] >> MeshNodePtr::make("<"_token);
isWhitespace["lhs"_token] >> currentCharacter;
isWhitespace["rhs"_token] >> spacePlusOne;
isWhitespace["result"_token] >> conditionResult;
auto stopIfWhitespace = isWhitespace["next"_token] >> MeshNodePtr::make("jump"_token);
stopIfWhitespace["condition"_token] >> conditionResult;
stopIfWhitespace["next"_token] >> MeshNodePtr::make("return"_token);
auto hashCharacter = stopIfWhitespace["else"_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;
}
}
};
}