Parse while-statements
This commit is contained in:
parent
9817d9b8b6
commit
4cf5c9791a
15
src/ast.cpp
15
src/ast.cpp
@ -142,10 +142,10 @@ namespace AST {
|
||||
out << "for (";
|
||||
if (this->m_init)
|
||||
out << (*this->m_init)->formatted();
|
||||
out << ";";
|
||||
out << "; ";
|
||||
if (this->m_cond)
|
||||
out << (*this->m_cond)->formatted();
|
||||
out << ";";
|
||||
out << "; ";
|
||||
if (this->m_after)
|
||||
out << (*this->m_after)->formatted();
|
||||
out << ")";
|
||||
@ -154,6 +154,17 @@ namespace AST {
|
||||
return out.str();
|
||||
}
|
||||
|
||||
std::string WhileStatement::formatted() {
|
||||
std::stringstream out{ "" };
|
||||
out << "while (";
|
||||
if (this->m_cond)
|
||||
out << (*this->m_cond)->formatted();
|
||||
out << ")";
|
||||
|
||||
out << "\n then " << this->m_loop->formatted();
|
||||
return out.str();
|
||||
}
|
||||
|
||||
std::string ReturnStatement::formatted() {
|
||||
std::stringstream out{ "" };
|
||||
out << "return ";
|
||||
|
||||
20
src/ast.h
20
src/ast.h
@ -453,6 +453,26 @@ namespace AST {
|
||||
virtual void typecheck(typecheck::State& state, typecheck::Scope& scope) override;
|
||||
};
|
||||
|
||||
class WhileStatement : public Statement {
|
||||
private:
|
||||
std::optional<std::unique_ptr<Expression>> m_cond;
|
||||
std::unique_ptr<Statement> m_loop;
|
||||
public:
|
||||
WhileStatement(token::Metadata meta,
|
||||
std::optional<std::unique_ptr<Expression>> cond,
|
||||
std::unique_ptr<Statement> loop)
|
||||
: Statement{ meta }
|
||||
, m_cond{ std::move(cond) }
|
||||
, m_loop{ std::move(loop) } {
|
||||
}
|
||||
virtual ~WhileStatement() override = default;
|
||||
virtual std::string formatted() override;
|
||||
virtual void codegen(codegen::Builder& builder, codegen::Scope& scope, codegen::StackAllocator& allocator) override;
|
||||
virtual void codegen_alloca(codegen::StackAllocator& allocator) override;
|
||||
virtual void typecheck_preprocess(typecheck::Scope& scope) override;
|
||||
virtual void typecheck(typecheck::State& state, typecheck::Scope& scope) override;
|
||||
};
|
||||
|
||||
class TopLevelStatement : public Node {
|
||||
public:
|
||||
TopLevelStatement(token::Metadata meta) : Node{ meta } {}
|
||||
|
||||
@ -596,6 +596,15 @@ namespace AST {
|
||||
builder.builder->SetInsertPoint(after_bb);
|
||||
}
|
||||
|
||||
void WhileStatement::codegen(codegen::Builder& builder, codegen::Scope&, codegen::StackAllocator&) {
|
||||
if (!builder.block)
|
||||
return;
|
||||
|
||||
builder.builder->SetInsertPoint(builder.block);
|
||||
|
||||
throw CompileError("TODO", this->m_meta);
|
||||
}
|
||||
|
||||
|
||||
void Function::codegen(codegen::Builder& builder, codegen::Scope& scope) {
|
||||
std::shared_ptr<types::Type> ret_ty_ptr{ this->m_return_ty };
|
||||
|
||||
@ -598,6 +598,30 @@ namespace parsing {
|
||||
};
|
||||
return std::pair{ std::unique_ptr<AST::Statement>{ statement }, false };
|
||||
}
|
||||
else if (inner.peek().type == token::Type::WhileKeyword) {
|
||||
inner.next();
|
||||
inner.expect(token::Type::Symbol, "(");
|
||||
|
||||
std::optional<std::unique_ptr<AST::Expression>> cond;
|
||||
|
||||
if (inner.peek().content != ")")
|
||||
cond = parse_expression(inner, scope).unwrap();
|
||||
inner.expect(token::Type::Symbol, ")");
|
||||
|
||||
auto loop_res = parse_statement(inner, scope).unwrap();
|
||||
if (loop_res.second)
|
||||
inner.expect(token::Type::Symbol, ";");
|
||||
auto loop = std::move(loop_res.first);
|
||||
|
||||
stream.m_position = inner.m_position;
|
||||
|
||||
auto statement = new AST::WhileStatement{
|
||||
before_meta + stream.metadata(),
|
||||
std::move(cond),
|
||||
std::move(loop),
|
||||
};
|
||||
return std::pair{ std::unique_ptr<AST::Statement>{ statement }, false };
|
||||
}
|
||||
else if (auto init = parse_init_statement(inner, scope); init.ok()) {
|
||||
stream.m_position = inner.m_position;
|
||||
return std::pair{ std::unique_ptr<AST::Statement>{ init.unwrap() }, true };
|
||||
|
||||
@ -103,4 +103,10 @@ namespace AST {
|
||||
(*this->m_after)->codegen_alloca(allocator);
|
||||
this->m_loop->codegen_alloca(allocator);
|
||||
}
|
||||
|
||||
void WhileStatement::codegen_alloca(codegen::StackAllocator& allocator) {
|
||||
if (this->m_cond)
|
||||
(*this->m_cond)->codegen_alloca(allocator);
|
||||
this->m_loop->codegen_alloca(allocator);
|
||||
}
|
||||
}
|
||||
@ -61,6 +61,8 @@ namespace token {
|
||||
return "Else";
|
||||
case token::Type::ForKeyword:
|
||||
return "For";
|
||||
case token::Type::WhileKeyword:
|
||||
return "While";
|
||||
|
||||
case token::Type::Whitespace:
|
||||
return "Whitespace";
|
||||
@ -227,6 +229,9 @@ namespace token {
|
||||
else if (content == "for") {
|
||||
type = token::Type::ForKeyword;
|
||||
}
|
||||
else if (content == "while") {
|
||||
type = token::Type::WhileKeyword;
|
||||
}
|
||||
tokens.push_back(token::Token{ type, content, meta + content.size() });
|
||||
}
|
||||
else if (iswhitespace(c)) {
|
||||
|
||||
@ -17,6 +17,7 @@ namespace token {
|
||||
IfKeyword,
|
||||
ElseKeyword,
|
||||
ForKeyword,
|
||||
WhileKeyword,
|
||||
|
||||
Whitespace,
|
||||
|
||||
|
||||
@ -663,6 +663,28 @@ namespace AST {
|
||||
this->m_loop->typecheck(state, inner_scope);
|
||||
}
|
||||
|
||||
void WhileStatement::typecheck_preprocess(typecheck::Scope& scope) {
|
||||
if (this->m_cond)
|
||||
(*this->m_cond)->typecheck_preprocess(scope);
|
||||
this->m_loop->typecheck_preprocess(scope);
|
||||
}
|
||||
|
||||
void WhileStatement::typecheck(typecheck::State& state, typecheck::Scope& scope) {
|
||||
auto bool_ty = std::shared_ptr<types::Type>{
|
||||
new types::FundamentalType{ false, types::FundamentalTypeKind::Bool } };
|
||||
|
||||
typecheck::Scope inner_scope{ scope };
|
||||
|
||||
if (this->m_cond) {
|
||||
auto cond_ty = (*this->m_cond)->typecheck(state, inner_scope, bool_ty).type;
|
||||
|
||||
auto check_res = check_type(state, cond_ty, bool_ty);
|
||||
this->m_cond = handle_res(std::move(*this->m_cond), check_res, state);
|
||||
}
|
||||
|
||||
this->m_loop->typecheck(state, inner_scope);
|
||||
}
|
||||
|
||||
void Function::typecheck_preprocess(typecheck::Scope& scope) {
|
||||
this->m_return_ty = refresh_type(scope, this->m_return_ty);
|
||||
for (auto& param : this->m_params) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user