-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.cpp
150 lines (137 loc) · 4.5 KB
/
main.cpp
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
#include "Ast.ostream.h"
#include "CoFiber.h"
#include "Scope.h"
#include "Task.h"
#include "Token.h"
#include "Token.ostream.h"
#include <array>
#include <iostream>
template<Token::Simple simple>
auto ignoreTokenIf(Token::Span &tokens) -> bool {
if (tokens.empty()) {
return false;
}
auto simplePtr = std::get_if<Token::Simple>(&tokens.front());
if (simplePtr && *simplePtr == simple) {
tokens = tokens.subspan(1);
return true;
}
return false;
}
template<class T>
auto consumeToken(Token::Span &tokens) -> T {
if (tokens.empty()) {
throw 42;
}
auto tok = std::get_if<T>(&tokens.front());
if (!tok) {
throw 32;
}
tokens = tokens.subspan(1);
return *tok;
}
auto parseValue(Scope &scope, Token::Span &tokens) -> Task<Ast::Node> {
if (tokens.empty()) {
throw 42;
}
auto token = tokens.front();
if (auto *numTok = std::get_if<Token::Num>(&token); numTok) {
std::cout << "found number " << *numTok << std::endl;
tokens = tokens.subspan(1);
co_return Ast::Node{Ast::Number{numTok->value}};
}
if (auto *idTok = std::get_if<Token::Id>(&token); idTok) {
auto idv = std::string_view{idTok->name};
auto r = scope.query(idv);
if (std::holds_alternative<std::monostate>(r)) {
std::cout << "await scope for " << idv << std::endl;
co_await scope.waitFor(idTok->name);
std::cout << "resumed for " << idv << std::endl;
r = scope.query(idv);
}
if (auto *variable = std::get_if<const ScopeVariable *>(&r); variable) {
std::cout << "found variable for " << idv << std::endl;
tokens = tokens.subspan(1);
co_return Ast::Node{Ast::RefVariable{idTok->name}};
}
}
throw 32;
}
auto parseOperation(Scope &scope, Token::Span &tokens) -> Task<Ast::Node>;
auto parseMultiplyAdd(Scope &scope, Token::Span &tokens) -> Task<Ast::Node> {
auto left = co_await parseValue(scope, tokens);
if (ignoreTokenIf<Token::Multiply>(tokens)) {
auto right = co_await parseOperation(scope, tokens);
co_return Ast::Node{Ast::Operator{
.type = Ast::Operator::Multiply,
.leftOperand = std::make_unique<Ast::Node>(std::move(left)),
.rightOperand = std::make_unique<Ast::Node>(std::move(right)),
}};
}
if (ignoreTokenIf<Token::Add>(tokens)) {
auto right = co_await parseOperation(scope, tokens);
co_return Ast::Node{Ast::Operator{
.type = Ast::Operator::Add,
.leftOperand = std::make_unique<Ast::Node>(std::move(left)),
.rightOperand = std::make_unique<Ast::Node>(std::move(right)),
}};
}
co_return left;
}
auto parseOperation(Scope &scope, Token::Span &tokens) -> Task<Ast::Node> {
return parseMultiplyAdd(scope, tokens);
}
auto parseDefineVariable(Scope &scope, Token::Span &tokens) -> Task<Ast::Node> {
std::cout << "parseDefineVariable " << tokens << std::endl;
auto id = consumeToken<Token::Id>(tokens);
scope.defineVariable(id.name);
auto value = Ast::NodePtr{};
if (ignoreTokenIf<Token::Assign>(tokens)) {
value = std::make_unique<Ast::Node>(co_await parseOperation(scope, tokens));
}
co_return Ast::Node{Ast::DefineVariable{
.name = id.name,
.valueNode = std::move(value),
}};
}
auto parseLine(Scope &scope, Token::Span tokens) -> Task<Ast::Node> {
std::cout << "parseLine " << tokens << std::endl;
if (tokens.empty()) {
co_return Ast::Node{};
}
if (ignoreTokenIf<Token::Var>(tokens)) {
co_return co_await parseDefineVariable(scope, tokens);
}
else {
co_return co_await parseOperation(scope, tokens);
}
}
auto parse(std::span<Token::Vec> tokenLines) -> Ast::NodeVec {
auto r = Ast::NodeVec{};
auto scope = Scope{};
for (auto &line : tokenLines) {
CoFiber::launch(
[](Ast::NodeVec &r, Scope &scope, Token::Span line) -> Task<> {
auto lineAst = co_await parseLine(scope, line);
std::cout << "finished " << line << ", got " << lineAst << '\n';
r.emplace_back(std::move(lineAst));
},
r, scope, line);
}
// scope.cleanup();
return r;
}
int main() {
auto tokens = []() {
using namespace Token;
return std::array{
Vec{Var, Id{"x"}, Assign, Id{"a"}, Add, Id{"b"}},
Vec{Var, Id{"a"}, Assign, Num{29}},
Vec{Var, Id{"b"}, Assign, Num{32}},
};
}();
std::cout << "Tokens:\n" << tokens << '\n';
auto ast = parse(tokens);
std::cout << "Ast: " << ast << '\n';
std::cout << "Done!" << std::endl;
}