Parse function calls
This commit is contained in:
parent
66f9dac5a6
commit
8dc0a6bf12
15
src/ast.cpp
15
src/ast.cpp
@ -21,6 +21,21 @@ namespace AST {
|
||||
return out.str();
|
||||
}
|
||||
|
||||
std::string FunctionCallExpression::formatted() {
|
||||
std::stringstream out{ "" };
|
||||
out << this->m_fn_expr->formatted() << "(";
|
||||
|
||||
int counter = 0;
|
||||
for (auto& arg : this->m_args) {
|
||||
if (counter++ > 0)
|
||||
out << ", ";
|
||||
out << arg->formatted();
|
||||
}
|
||||
|
||||
out << ")";
|
||||
return out.str();
|
||||
}
|
||||
|
||||
std::string ExpressionStatement::formatted() {
|
||||
std::stringstream out{ "" };
|
||||
out << this->m_expr->formatted();
|
||||
|
||||
13
src/ast.h
13
src/ast.h
@ -59,6 +59,19 @@ namespace AST {
|
||||
virtual codegen::StackValue codegen(codegen::Builder& builder, codegen::Scope& scope) override;
|
||||
};
|
||||
|
||||
class FunctionCallExpression : public Expression {
|
||||
private:
|
||||
std::unique_ptr<Expression> m_fn_expr;
|
||||
std::vector<std::unique_ptr<Expression>> m_args;
|
||||
public:
|
||||
FunctionCallExpression(std::unique_ptr<Expression> fn_expr, std::vector<std::unique_ptr<Expression>> args)
|
||||
: m_fn_expr{ std::move(fn_expr) }, m_args{ std::move(args) } {
|
||||
}
|
||||
virtual ~FunctionCallExpression() override = default;
|
||||
virtual std::string formatted() override;
|
||||
virtual codegen::StackValue codegen(codegen::Builder& builder, codegen::Scope& scope) override;
|
||||
};
|
||||
|
||||
|
||||
class ReturnStatement : public Statement {
|
||||
private:
|
||||
|
||||
@ -76,6 +76,10 @@ namespace AST {
|
||||
}
|
||||
}
|
||||
|
||||
codegen::StackValue FunctionCallExpression::codegen(codegen::Builder& builder, codegen::Scope& scope) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
void ReturnStatement::codegen(codegen::Builder& builder, codegen::Scope& scope) {
|
||||
if (!builder.block)
|
||||
return;
|
||||
|
||||
@ -4,6 +4,8 @@
|
||||
|
||||
namespace parsing {
|
||||
namespace {
|
||||
Result<std::unique_ptr<AST::Expression>, std::string> parse_expression(token::TokenStream& stream);
|
||||
|
||||
Result<std::unique_ptr<types::Type>, std::string> parse_type(token::TokenStream& stream) {
|
||||
token::TokenStream inner{ stream };
|
||||
try {
|
||||
@ -19,7 +21,7 @@ namespace parsing {
|
||||
}
|
||||
}
|
||||
|
||||
Result<std::unique_ptr<AST::Expression>, std::string> parse_primary_expression(token::TokenStream& stream) {
|
||||
Result<std::unique_ptr<AST::Expression>, std::string> parse_plain_expression(token::TokenStream& stream) {
|
||||
token::TokenStream inner{ stream };
|
||||
try {
|
||||
auto token = inner.next();
|
||||
@ -44,6 +46,37 @@ namespace parsing {
|
||||
}
|
||||
}
|
||||
|
||||
Result<std::unique_ptr<AST::Expression>, std::string> parse_primary_expression(token::TokenStream& stream) {
|
||||
token::TokenStream inner{ stream };
|
||||
try {
|
||||
auto plain_expr = parse_plain_expression(inner);
|
||||
while (inner.peek().content == "(") {
|
||||
inner.next();
|
||||
|
||||
std::vector<std::unique_ptr<AST::Expression>> args{};
|
||||
|
||||
int counter = 0;
|
||||
while (inner.peek().content != ")") {
|
||||
if (counter++ > 0)
|
||||
inner.expect(token::Type::Symbol, ",");
|
||||
args.push_back(parse_expression(inner).unwrap());
|
||||
}
|
||||
|
||||
inner.expect(token::Type::Symbol, ")");
|
||||
|
||||
auto fn_call = new AST::FunctionCallExpression{ std::move(plain_expr.unwrap()), std::move(args) };
|
||||
plain_expr = new std::unique_ptr<AST::Expression>{ fn_call };
|
||||
}
|
||||
|
||||
|
||||
stream.m_position = inner.m_position;
|
||||
return plain_expr;
|
||||
}
|
||||
catch (std::runtime_error error) {
|
||||
return new std::string{ error.what() };
|
||||
}
|
||||
}
|
||||
|
||||
Result<types::BinOp, std::string> parse_binop(token::TokenStream& stream) {
|
||||
token::TokenStream inner{ stream };
|
||||
try {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user