Parse while-statements
This commit is contained in:
parent
9817d9b8b6
commit
4cf5c9791a
11
src/ast.cpp
11
src/ast.cpp
@ -154,6 +154,17 @@ namespace AST {
|
|||||||
return out.str();
|
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::string ReturnStatement::formatted() {
|
||||||
std::stringstream out{ "" };
|
std::stringstream out{ "" };
|
||||||
out << "return ";
|
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;
|
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 {
|
class TopLevelStatement : public Node {
|
||||||
public:
|
public:
|
||||||
TopLevelStatement(token::Metadata meta) : Node{ meta } {}
|
TopLevelStatement(token::Metadata meta) : Node{ meta } {}
|
||||||
|
|||||||
@ -596,6 +596,15 @@ namespace AST {
|
|||||||
builder.builder->SetInsertPoint(after_bb);
|
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) {
|
void Function::codegen(codegen::Builder& builder, codegen::Scope& scope) {
|
||||||
std::shared_ptr<types::Type> ret_ty_ptr{ this->m_return_ty };
|
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 };
|
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()) {
|
else if (auto init = parse_init_statement(inner, scope); init.ok()) {
|
||||||
stream.m_position = inner.m_position;
|
stream.m_position = inner.m_position;
|
||||||
return std::pair{ std::unique_ptr<AST::Statement>{ init.unwrap() }, true };
|
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_after)->codegen_alloca(allocator);
|
||||||
this->m_loop->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";
|
return "Else";
|
||||||
case token::Type::ForKeyword:
|
case token::Type::ForKeyword:
|
||||||
return "For";
|
return "For";
|
||||||
|
case token::Type::WhileKeyword:
|
||||||
|
return "While";
|
||||||
|
|
||||||
case token::Type::Whitespace:
|
case token::Type::Whitespace:
|
||||||
return "Whitespace";
|
return "Whitespace";
|
||||||
@ -227,6 +229,9 @@ namespace token {
|
|||||||
else if (content == "for") {
|
else if (content == "for") {
|
||||||
type = token::Type::ForKeyword;
|
type = token::Type::ForKeyword;
|
||||||
}
|
}
|
||||||
|
else if (content == "while") {
|
||||||
|
type = token::Type::WhileKeyword;
|
||||||
|
}
|
||||||
tokens.push_back(token::Token{ type, content, meta + content.size() });
|
tokens.push_back(token::Token{ type, content, meta + content.size() });
|
||||||
}
|
}
|
||||||
else if (iswhitespace(c)) {
|
else if (iswhitespace(c)) {
|
||||||
|
|||||||
@ -17,6 +17,7 @@ namespace token {
|
|||||||
IfKeyword,
|
IfKeyword,
|
||||||
ElseKeyword,
|
ElseKeyword,
|
||||||
ForKeyword,
|
ForKeyword,
|
||||||
|
WhileKeyword,
|
||||||
|
|
||||||
Whitespace,
|
Whitespace,
|
||||||
|
|
||||||
|
|||||||
@ -663,6 +663,28 @@ namespace AST {
|
|||||||
this->m_loop->typecheck(state, inner_scope);
|
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) {
|
void Function::typecheck_preprocess(typecheck::Scope& scope) {
|
||||||
this->m_return_ty = refresh_type(scope, this->m_return_ty);
|
this->m_return_ty = refresh_type(scope, this->m_return_ty);
|
||||||
for (auto& param : this->m_params) {
|
for (auto& param : this->m_params) {
|
||||||
|
|||||||
4
test.c
4
test.c
@ -54,5 +54,9 @@ int main() {
|
|||||||
for (int counter = 0; counter < 10; counter++)
|
for (int counter = 0; counter < 10; counter++)
|
||||||
printf("counter: %d\n", counter);
|
printf("counter: %d\n", counter);
|
||||||
|
|
||||||
|
int counter = 0;
|
||||||
|
while (counter < 10)
|
||||||
|
printf("counter: %d\n", counter++);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user