Add forward declarations
This commit is contained in:
parent
07c0059c5c
commit
7cf752f67b
11
src/ast.cpp
11
src/ast.cpp
@ -85,12 +85,13 @@ namespace AST {
|
|||||||
|
|
||||||
out << ") -> ";
|
out << ") -> ";
|
||||||
out << this->m_return_ty->formatted();
|
out << this->m_return_ty->formatted();
|
||||||
out << " {\n";
|
if (this->m_statements) {
|
||||||
for (auto& statement : this->m_statements) {
|
out << " {\n";
|
||||||
out << " " << statement->formatted() << "\n";
|
for (auto& statement : *this->m_statements) {
|
||||||
|
out << " " << statement->formatted() << "\n";
|
||||||
|
}
|
||||||
|
out << "}";
|
||||||
}
|
}
|
||||||
|
|
||||||
out << "}";
|
|
||||||
return out.str();
|
return out.str();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -167,14 +167,14 @@ namespace AST {
|
|||||||
std::unique_ptr<types::Type> m_return_ty;
|
std::unique_ptr<types::Type> m_return_ty;
|
||||||
std::vector<std::pair<std::string, std::unique_ptr<types::Type>>> m_params;
|
std::vector<std::pair<std::string, std::unique_ptr<types::Type>>> m_params;
|
||||||
std::string m_name;
|
std::string m_name;
|
||||||
std::vector<std::unique_ptr<Statement>> m_statements;
|
std::optional<std::vector<std::unique_ptr<Statement>>> m_statements;
|
||||||
public:
|
public:
|
||||||
Function(
|
Function(
|
||||||
token::Metadata meta,
|
token::Metadata meta,
|
||||||
std::unique_ptr<types::Type> return_ty,
|
std::unique_ptr<types::Type> return_ty,
|
||||||
std::vector<std::pair<std::string, std::unique_ptr<types::Type>>> params,
|
std::vector<std::pair<std::string, std::unique_ptr<types::Type>>> params,
|
||||||
std::string name,
|
std::string name,
|
||||||
std::vector<std::unique_ptr<Statement>> statements)
|
std::optional<std::vector<std::unique_ptr<Statement>>> statements)
|
||||||
: TopLevelStatement{ meta }
|
: TopLevelStatement{ meta }
|
||||||
, m_return_ty{ std::move(return_ty) }
|
, m_return_ty{ std::move(return_ty) }
|
||||||
, m_params{ std::move(params) }
|
, m_params{ std::move(params) }
|
||||||
|
|||||||
@ -187,24 +187,26 @@ namespace AST {
|
|||||||
|
|
||||||
scope.values[this->m_name] = codegen::StackValue{ function, fn_ty_ptr };
|
scope.values[this->m_name] = codegen::StackValue{ function, fn_ty_ptr };
|
||||||
|
|
||||||
auto BB = llvm::BasicBlock::Create(*builder.context, "entry", function, nullptr);
|
if (this->m_statements) {
|
||||||
builder.block = BB;
|
auto BB = llvm::BasicBlock::Create(*builder.context, "entry", function, nullptr);
|
||||||
|
builder.block = BB;
|
||||||
|
|
||||||
codegen::Scope inner_scope{ scope };
|
codegen::Scope inner_scope{ scope };
|
||||||
|
|
||||||
int counter = 0;
|
int counter = 0;
|
||||||
for (auto& param : this->m_params) {
|
for (auto& param : this->m_params) {
|
||||||
auto param_ty_ptr = param_ty_ptrs[counter];
|
auto param_ty_ptr = param_ty_ptrs[counter];
|
||||||
auto arg = function->getArg(counter++);
|
auto arg = function->getArg(counter++);
|
||||||
arg->setName(param.first);
|
arg->setName(param.first);
|
||||||
inner_scope.values[param.first] = codegen::StackValue{
|
inner_scope.values[param.first] = codegen::StackValue{
|
||||||
arg,
|
arg,
|
||||||
param_ty_ptr,
|
param_ty_ptr,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto& statement : this->m_statements) {
|
for (auto& statement : *this->m_statements) {
|
||||||
statement->codegen(builder, inner_scope);
|
statement->codegen(builder, inner_scope);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
llvm::verifyFunction(*function);
|
llvm::verifyFunction(*function);
|
||||||
|
|||||||
@ -252,18 +252,25 @@ namespace parsing {
|
|||||||
}
|
}
|
||||||
|
|
||||||
inner.expect(token::Type::Symbol, ")");
|
inner.expect(token::Type::Symbol, ")");
|
||||||
inner.expect(token::Type::Symbol, "{");
|
|
||||||
|
|
||||||
std::vector<std::unique_ptr<AST::Statement>> statements{};
|
std::optional<std::vector<std::unique_ptr<AST::Statement>>> statements{};
|
||||||
|
if (inner.peek().content == "{") {
|
||||||
|
inner.expect(token::Type::Symbol, "{");
|
||||||
|
|
||||||
auto statement = parse_statement(inner);
|
std::vector<std::unique_ptr<AST::Statement>> statement_list{};
|
||||||
while (statement.ok()) {
|
|
||||||
statements.push_back(statement.unwrap());
|
auto statement = parse_statement(inner);
|
||||||
statement = parse_statement(inner);
|
while (statement.ok()) {
|
||||||
|
statement_list.push_back(statement.unwrap());
|
||||||
|
statement = parse_statement(inner);
|
||||||
|
}
|
||||||
|
|
||||||
|
statements = std::optional{ std::move(statement_list) };
|
||||||
|
inner.expect(token::Type::Symbol, "}");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
inner.expect(token::Type::Symbol, ";");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
inner.expect(token::Type::Symbol, "}");
|
|
||||||
|
|
||||||
stream.m_position = inner.m_position;
|
stream.m_position = inner.m_position;
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user