Compare commits
2 Commits
0fb63ee703
...
a90259eccc
| Author | SHA1 | Date | |
|---|---|---|---|
| a90259eccc | |||
| 745447265e |
18
src/ast.cpp
18
src/ast.cpp
@ -173,6 +173,24 @@ namespace AST {
|
||||
return out.str();
|
||||
}
|
||||
|
||||
std::string CompoundStatement::formatted() {
|
||||
std::stringstream out{ "" };
|
||||
out << "{\n";
|
||||
for (auto& statement : this->m_statements) {
|
||||
out << " " << statement->formatted() << "\n";
|
||||
}
|
||||
out << "}";
|
||||
return out.str();
|
||||
}
|
||||
|
||||
std::string BreakStatement::formatted() {
|
||||
return "break;";
|
||||
}
|
||||
|
||||
std::string ContinueStatement::formatted() {
|
||||
return "continue;";
|
||||
}
|
||||
|
||||
std::string InitializationStatement::formatted() {
|
||||
std::stringstream out{ "" };
|
||||
out << this->m_type->formatted() << " " << this->m_name;
|
||||
|
||||
46
src/ast.h
46
src/ast.h
@ -473,6 +473,52 @@ namespace AST {
|
||||
virtual void typecheck(typecheck::State& state, typecheck::Scope& scope) override;
|
||||
};
|
||||
|
||||
/** @brief a block e.g. {statement; statement;} */
|
||||
class CompoundStatement : public Statement {
|
||||
private:
|
||||
std::vector<std::unique_ptr<Statement>> m_statements;
|
||||
public:
|
||||
CompoundStatement(token::Metadata meta,
|
||||
std::vector<std::unique_ptr<Statement>> statements)
|
||||
: Statement{ meta }
|
||||
, m_statements{ std::move(statements) } {
|
||||
}
|
||||
virtual ~CompoundStatement() 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;
|
||||
};
|
||||
|
||||
/** @brief break-keyword */
|
||||
class BreakStatement : public Statement {
|
||||
public:
|
||||
BreakStatement(token::Metadata meta)
|
||||
: Statement{ meta } {
|
||||
}
|
||||
virtual ~BreakStatement() 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;
|
||||
};
|
||||
|
||||
/** @brief continue-keyword */
|
||||
class ContinueStatement : public Statement {
|
||||
public:
|
||||
ContinueStatement(token::Metadata meta)
|
||||
: Statement{ meta } {
|
||||
}
|
||||
virtual ~ContinueStatement() 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 } {}
|
||||
|
||||
@ -630,6 +630,32 @@ namespace AST {
|
||||
builder.block = after_bb;
|
||||
}
|
||||
|
||||
void CompoundStatement::codegen(codegen::Builder& builder, codegen::Scope&, codegen::StackAllocator&) {
|
||||
if (!builder.block)
|
||||
return;
|
||||
|
||||
builder.builder->SetInsertPoint(builder.block);
|
||||
|
||||
throw CompileError("TODO", this->m_meta);
|
||||
}
|
||||
|
||||
void BreakStatement::codegen(codegen::Builder& builder, codegen::Scope&, codegen::StackAllocator&) {
|
||||
if (!builder.block)
|
||||
return;
|
||||
|
||||
builder.builder->SetInsertPoint(builder.block);
|
||||
|
||||
throw CompileError("TODO", this->m_meta);
|
||||
}
|
||||
|
||||
void ContinueStatement::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 };
|
||||
|
||||
@ -109,4 +109,13 @@ namespace AST {
|
||||
(*this->m_cond)->codegen_alloca(allocator);
|
||||
this->m_loop->codegen_alloca(allocator);
|
||||
}
|
||||
|
||||
void CompoundStatement::codegen_alloca(codegen::StackAllocator& allocator) {
|
||||
for (auto& statement : this->m_statements)
|
||||
statement->codegen_alloca(allocator);
|
||||
}
|
||||
|
||||
void BreakStatement::codegen_alloca(codegen::StackAllocator&) {}
|
||||
|
||||
void ContinueStatement::codegen_alloca(codegen::StackAllocator&) {}
|
||||
}
|
||||
@ -66,6 +66,8 @@ namespace token {
|
||||
|
||||
case token::Type::Whitespace:
|
||||
return "Whitespace";
|
||||
case token::Type::Comment:
|
||||
return "Comment";
|
||||
|
||||
case token::Type::Eof:
|
||||
return "EOF";
|
||||
@ -130,7 +132,8 @@ namespace token {
|
||||
Token TokenStream::next() {
|
||||
token::Token got = this->peek(0);
|
||||
m_position++;
|
||||
while (m_position < static_cast<int>(m_tokens.size()) && this->peek().type == Type::Whitespace) {
|
||||
while (m_position < static_cast<int>(m_tokens.size())
|
||||
&& (this->peek().type == Type::Whitespace || this->peek().type == Type::Comment)) {
|
||||
m_position++;
|
||||
}
|
||||
return got;
|
||||
@ -179,6 +182,28 @@ namespace token {
|
||||
} while (std::isdigit(c));
|
||||
tokens.push_back(token::Token{ token::Type::LiteralInt, content, meta + content.size() });
|
||||
}
|
||||
// Single-line comments
|
||||
else if (c == '/' && text_length > (i + 2) && text[i + 1] == '/') {
|
||||
std::string content{ };
|
||||
i += 2;
|
||||
while (text_length > i && text[i] != '\n') {
|
||||
content += text[i++];
|
||||
}
|
||||
tokens.push_back(token::Token{ token::Type::Comment, content, meta + content.size() });
|
||||
}
|
||||
// Multi-line comments
|
||||
else if (c == '/' && text_length > (i + 2) && text[i + 1] == '*') {
|
||||
std::string content{ };
|
||||
i += 2;
|
||||
while (text_length > i) {
|
||||
if (text[i] == '*' && text_length > (i + 1) && text[i + 1] == '/') {
|
||||
i += 2;
|
||||
break;
|
||||
}
|
||||
content += text[i++];
|
||||
}
|
||||
tokens.push_back(token::Token{ token::Type::Comment, content, meta + content.size() });
|
||||
}
|
||||
else if (c == '\"') {
|
||||
std::string content{};
|
||||
c = text[++i]; // Skip initial "
|
||||
|
||||
@ -20,6 +20,7 @@ namespace token {
|
||||
WhileKeyword,
|
||||
|
||||
Whitespace,
|
||||
Comment,
|
||||
|
||||
Eof,
|
||||
};
|
||||
|
||||
@ -685,6 +685,26 @@ namespace AST {
|
||||
this->m_loop->typecheck(state, inner_scope);
|
||||
}
|
||||
|
||||
void CompoundStatement::typecheck_preprocess(typecheck::Scope& scope) {
|
||||
typecheck::Scope inner{ scope };
|
||||
for (auto& statement : this->m_statements) {
|
||||
statement->typecheck_preprocess(inner);
|
||||
}
|
||||
}
|
||||
|
||||
void CompoundStatement::typecheck(typecheck::State& state, typecheck::Scope& scope) {
|
||||
typecheck::Scope inner{ scope };
|
||||
for (auto& statement : this->m_statements) {
|
||||
statement->typecheck(state, inner);
|
||||
}
|
||||
}
|
||||
|
||||
void BreakStatement::typecheck_preprocess(typecheck::Scope&) {}
|
||||
void BreakStatement::typecheck(typecheck::State&, typecheck::Scope&) {}
|
||||
|
||||
void ContinueStatement::typecheck_preprocess(typecheck::Scope&) {}
|
||||
void ContinueStatement::typecheck(typecheck::State&, typecheck::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) {
|
||||
|
||||
10
test.c
10
test.c
@ -26,9 +26,11 @@ void update_ptr(char* ptr) {
|
||||
}
|
||||
|
||||
int main() {
|
||||
// Test fibonacci sequence
|
||||
char text[30] = "10th fibonacci number is %d!\n";
|
||||
printf(text, fibonacci(10));
|
||||
|
||||
// Test arrays
|
||||
char somelist[5] = { 1, 2, 3, 4, 5 };
|
||||
char* somelist_ptr = somelist;
|
||||
|
||||
@ -37,26 +39,32 @@ int main() {
|
||||
printf("first element: %d!\n", somelist[0]);
|
||||
printf("first element via ptr: %d!\n", somelist_ptr[0]);
|
||||
|
||||
// Test structs and nested fields
|
||||
struct Otus otus = { 5, { 7, 3 } };
|
||||
update(otus);
|
||||
|
||||
printf("first field: %d!\n", otus.field);
|
||||
printf("second field's second element: %d!\n", otus.second[1]);
|
||||
|
||||
// Test pointers
|
||||
char hello = 10;
|
||||
update_ptr(&hello);
|
||||
printf("hello: %d!\n", hello);
|
||||
|
||||
/** Test nested arrays */
|
||||
char twod_array[5][5];
|
||||
twod_array[0][0] = 50;
|
||||
printf("2d array: %d!\n", twod_array[0][0]);
|
||||
|
||||
// Test for-loops
|
||||
for (int counter = 0; counter < 10; counter++)
|
||||
printf("for-counter: %d\n", counter);
|
||||
|
||||
// Test while-loops
|
||||
int counter = 0;
|
||||
while (counter < 10)
|
||||
while (counter < 10) {
|
||||
printf("while-counter: %d\n", counter++);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user