Parse listinitializer
This commit is contained in:
parent
aa698c7ed6
commit
55388bc6e3
@ -71,9 +71,49 @@ namespace parsing {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Result<std::unique_ptr<AST::Expression>, std::string> parse_list_initializer(token::TokenStream& stream) {
|
||||||
|
token::TokenStream inner{ stream };
|
||||||
|
try {
|
||||||
|
auto before_meta = inner.metadata();
|
||||||
|
|
||||||
|
inner.expect(token::Type::Symbol, "{");
|
||||||
|
|
||||||
|
std::vector<std::unique_ptr<AST::Expression>> expressions{};
|
||||||
|
|
||||||
|
int counter = 0;
|
||||||
|
while (inner.peek().content != "}") {
|
||||||
|
if (counter++ > 0) {
|
||||||
|
inner.expect(token::Type::Symbol, ",");
|
||||||
|
}
|
||||||
|
expressions.push_back(parse_expression(inner).unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
|
inner.expect(token::Type::Symbol, "}");
|
||||||
|
|
||||||
|
stream.m_position = inner.m_position;
|
||||||
|
return std::unique_ptr<AST::Expression> {
|
||||||
|
new AST::ListInitializerExpression(
|
||||||
|
before_meta + inner.metadata(),
|
||||||
|
std::move(expressions),
|
||||||
|
std::shared_ptr<types::Type> {
|
||||||
|
new types::FundamentalType{ types::FundamentalTypeKind::Any }
|
||||||
|
})
|
||||||
|
};
|
||||||
|
}
|
||||||
|
catch (std::runtime_error& error) {
|
||||||
|
return std::string{ error.what() };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Result<std::unique_ptr<AST::Expression>, std::string> parse_plain_expression(token::TokenStream& stream) {
|
Result<std::unique_ptr<AST::Expression>, std::string> parse_plain_expression(token::TokenStream& stream) {
|
||||||
token::TokenStream inner{ stream };
|
token::TokenStream inner{ stream };
|
||||||
try {
|
try {
|
||||||
|
if (auto list_init = parse_list_initializer(inner); list_init.ok()) {
|
||||||
|
stream.m_position = inner.m_position;
|
||||||
|
|
||||||
|
return std::unique_ptr<AST::Expression> { list_init.unwrap() };
|
||||||
|
}
|
||||||
|
|
||||||
auto token = inner.next();
|
auto token = inner.next();
|
||||||
if (token.type == token::Type::LiteralInt) {
|
if (token.type == token::Type::LiteralInt) {
|
||||||
stream.m_position = inner.m_position;
|
stream.m_position = inner.m_position;
|
||||||
|
|||||||
3
test.c
3
test.c
@ -14,9 +14,8 @@ int main() {
|
|||||||
char text[29] = "10th fibonacci number is %d!";
|
char text[29] = "10th fibonacci number is %d!";
|
||||||
printf(text, fibonacci(10));
|
printf(text, fibonacci(10));
|
||||||
|
|
||||||
char somelist[5];
|
char somelist[5] = { 1, 2, 3, 4, 5 };
|
||||||
|
|
||||||
somelist[0] = 15;
|
|
||||||
change_first(somelist);
|
change_first(somelist);
|
||||||
|
|
||||||
return somelist[0];
|
return somelist[0];
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user