Compare commits
4 Commits
941e19d4dd
...
0fb63ee703
| Author | SHA1 | Date | |
|---|---|---|---|
| 0fb63ee703 | |||
| 52614a7f01 | |||
| 4cf5c9791a | |||
| 9817d9b8b6 |
15
src/ast.cpp
15
src/ast.cpp
@ -142,10 +142,10 @@ namespace AST {
|
|||||||
out << "for (";
|
out << "for (";
|
||||||
if (this->m_init)
|
if (this->m_init)
|
||||||
out << (*this->m_init)->formatted();
|
out << (*this->m_init)->formatted();
|
||||||
out << ";";
|
out << "; ";
|
||||||
if (this->m_cond)
|
if (this->m_cond)
|
||||||
out << (*this->m_cond)->formatted();
|
out << (*this->m_cond)->formatted();
|
||||||
out << ";";
|
out << "; ";
|
||||||
if (this->m_after)
|
if (this->m_after)
|
||||||
out << (*this->m_after)->formatted();
|
out << (*this->m_after)->formatted();
|
||||||
out << ")";
|
out << ")";
|
||||||
@ -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,40 @@ namespace AST {
|
|||||||
builder.builder->SetInsertPoint(after_bb);
|
builder.builder->SetInsertPoint(after_bb);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void WhileStatement::codegen(codegen::Builder& builder, codegen::Scope& scope, codegen::StackAllocator& allocator) {
|
||||||
|
if (!builder.block)
|
||||||
|
return;
|
||||||
|
|
||||||
|
builder.builder->SetInsertPoint(builder.block);
|
||||||
|
|
||||||
|
auto function = builder.block->getParent();
|
||||||
|
auto cond_bb = llvm::BasicBlock::Create(*builder.context, "while-cond", function);
|
||||||
|
auto inner_bb = llvm::BasicBlock::Create(*builder.context, "while-inner", function);
|
||||||
|
auto after_bb = llvm::BasicBlock::Create(*builder.context, "while-after", function);
|
||||||
|
|
||||||
|
builder.builder->CreateBr(cond_bb);
|
||||||
|
|
||||||
|
// While condition
|
||||||
|
builder.builder->SetInsertPoint(cond_bb);
|
||||||
|
if (this->m_cond) {
|
||||||
|
auto cond_value = (*this->m_cond)->codegen(builder, scope, allocator);
|
||||||
|
builder.builder->CreateCondBr(cond_value.value, inner_bb, after_bb);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
builder.builder->CreateBr(inner_bb);
|
||||||
|
}
|
||||||
|
|
||||||
|
// While loop
|
||||||
|
builder.builder->SetInsertPoint(inner_bb);
|
||||||
|
builder.block = inner_bb;
|
||||||
|
this->m_loop->codegen(builder, scope, allocator);
|
||||||
|
builder.builder->CreateBr(cond_bb);
|
||||||
|
|
||||||
|
// After
|
||||||
|
builder.builder->SetInsertPoint(after_bb);
|
||||||
|
builder.block = after_bb;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
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,
|
||||||
|
|
||||||
|
|||||||
@ -645,20 +645,44 @@ namespace AST {
|
|||||||
auto bool_ty = std::shared_ptr<types::Type>{
|
auto bool_ty = std::shared_ptr<types::Type>{
|
||||||
new types::FundamentalType{ false, types::FundamentalTypeKind::Bool } };
|
new types::FundamentalType{ false, types::FundamentalTypeKind::Bool } };
|
||||||
|
|
||||||
|
typecheck::Scope inner_scope{ scope };
|
||||||
|
|
||||||
if (this->m_init)
|
if (this->m_init)
|
||||||
(*this->m_init)->typecheck(state, scope);
|
(*this->m_init)->typecheck(state, inner_scope);
|
||||||
|
|
||||||
if (this->m_cond) {
|
if (this->m_cond) {
|
||||||
auto cond_ty = (*this->m_cond)->typecheck(state, scope, bool_ty).type;
|
auto cond_ty = (*this->m_cond)->typecheck(state, inner_scope, bool_ty).type;
|
||||||
|
|
||||||
auto check_res = check_type(state, cond_ty, bool_ty);
|
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_cond = handle_res(std::move(*this->m_cond), check_res, state);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this->m_after)
|
if (this->m_after)
|
||||||
(*this->m_after)->typecheck(state, scope, {});
|
(*this->m_after)->typecheck(state, inner_scope, {});
|
||||||
|
|
||||||
this->m_loop->typecheck(state, 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) {
|
||||||
|
|||||||
6
test.c
6
test.c
@ -52,7 +52,11 @@ int main() {
|
|||||||
printf("2d array: %d!\n", twod_array[0][0]);
|
printf("2d array: %d!\n", twod_array[0][0]);
|
||||||
|
|
||||||
for (int counter = 0; counter < 10; counter++)
|
for (int counter = 0; counter < 10; counter++)
|
||||||
printf("counter: %d\n", counter);
|
printf("for-counter: %d\n", counter);
|
||||||
|
|
||||||
|
int counter = 0;
|
||||||
|
while (counter < 10)
|
||||||
|
printf("while-counter: %d\n", counter++);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user