Compare commits

..

No commits in common. "a90259eccc640e99cd0efe73eaca7f711f62c459" and "0fb63ee7038320e4665ce6060a2c63776a30fb8a" have entirely different histories.

8 changed files with 2 additions and 155 deletions

View File

@ -173,24 +173,6 @@ 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;

View File

@ -473,52 +473,6 @@ 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 } {}

View File

@ -630,32 +630,6 @@ 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 };

View File

@ -109,13 +109,4 @@ 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&) {}
}

View File

@ -66,8 +66,6 @@ namespace token {
case token::Type::Whitespace:
return "Whitespace";
case token::Type::Comment:
return "Comment";
case token::Type::Eof:
return "EOF";
@ -132,8 +130,7 @@ 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 || this->peek().type == Type::Comment)) {
while (m_position < static_cast<int>(m_tokens.size()) && this->peek().type == Type::Whitespace) {
m_position++;
}
return got;
@ -182,28 +179,6 @@ 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 "

View File

@ -20,7 +20,6 @@ namespace token {
WhileKeyword,
Whitespace,
Comment,
Eof,
};

View File

@ -685,26 +685,6 @@ 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
View File

@ -26,11 +26,9 @@ 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;
@ -39,32 +37,26 @@ 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;
}