Compare commits

...

6 Commits

10 changed files with 233 additions and 36 deletions

View File

@ -15,7 +15,15 @@ separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS})
add_definitions(${LLVM_DEFINITIONS_LIST})
# Executable
add_executable(${PROJECT_NAME} src/main.cpp src/tokens.cpp src/parsing.cpp src/ast.cpp src/codegen.cpp src/types.cpp)
add_executable(${PROJECT_NAME}
src/main.cpp
src/tokens.cpp
src/parsing.cpp
src/ast.cpp
src/codegen.cpp
src/types.cpp
src/typechecker.cpp
)
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20)
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Weffc++ -Wextra -Wpedantic -Werror)

View File

@ -1,14 +1,15 @@
#ifndef AST_H
#define AST_H
#include "codegen.h"
#include "types.h"
#include "tokens.h"
#include <string>
#include <vector>
#include <memory>
#include "codegen.h"
#include "types.h"
#include "tokens.h"
#include "typechecker.h"
namespace AST {
class Node {
public:
@ -23,12 +24,18 @@ namespace AST {
public:
Expression(token::Metadata meta) : Node{ meta } {}
virtual codegen::StackValue codegen(codegen::Builder& builder, codegen::Scope& scope) = 0;
virtual std::shared_ptr<types::Type> typecheck(
typecheck::State& state,
typecheck::Scope& scope,
std::optional<std::shared_ptr<types::Type>> expected_ty
) = 0;
};
class Statement : public Node {
public:
Statement(token::Metadata meta) : Node{ meta } {}
virtual void codegen(codegen::Builder& builder, codegen::Scope& scope) = 0;
virtual void typecheck(typecheck::State& state, typecheck::Scope& scope) = 0;
};
class IntLiteralExpression : public Expression {
@ -39,6 +46,11 @@ namespace AST {
virtual ~IntLiteralExpression() override = default;
virtual std::string formatted() override;
virtual codegen::StackValue codegen(codegen::Builder& builder, codegen::Scope& scope) override;
virtual std::shared_ptr<types::Type> typecheck(
typecheck::State& state,
typecheck::Scope& scope,
std::optional<std::shared_ptr<types::Type>> expected_ty
) override;
};
class StringLiteralExpression : public Expression {
@ -49,6 +61,11 @@ namespace AST {
virtual ~StringLiteralExpression() override = default;
virtual std::string formatted() override;
virtual codegen::StackValue codegen(codegen::Builder& builder, codegen::Scope& scope) override;
virtual std::shared_ptr<types::Type> typecheck(
typecheck::State& state,
typecheck::Scope& scope,
std::optional<std::shared_ptr<types::Type>> expected_ty
) override;
};
class ValueReferenceExpression : public Expression {
@ -59,6 +76,11 @@ namespace AST {
virtual ~ValueReferenceExpression() override = default;
virtual std::string formatted() override;
virtual codegen::StackValue codegen(codegen::Builder& builder, codegen::Scope& scope) override;
virtual std::shared_ptr<types::Type> typecheck(
typecheck::State& state,
typecheck::Scope& scope,
std::optional<std::shared_ptr<types::Type>> expected_ty
) override;
};
class BinaryOperationExpression : public Expression {
@ -80,6 +102,11 @@ namespace AST {
virtual ~BinaryOperationExpression() override = default;
virtual std::string formatted() override;
virtual codegen::StackValue codegen(codegen::Builder& builder, codegen::Scope& scope) override;
virtual std::shared_ptr<types::Type> typecheck(
typecheck::State& state,
typecheck::Scope& scope,
std::optional<std::shared_ptr<types::Type>> expected_ty
) override;
};
class FunctionCallExpression : public Expression {
@ -98,6 +125,11 @@ namespace AST {
virtual ~FunctionCallExpression() override = default;
virtual std::string formatted() override;
virtual codegen::StackValue codegen(codegen::Builder& builder, codegen::Scope& scope) override;
virtual std::shared_ptr<types::Type> typecheck(
typecheck::State& state,
typecheck::Scope& scope,
std::optional<std::shared_ptr<types::Type>> expected_ty
) override;
};
@ -111,17 +143,18 @@ namespace AST {
virtual ~ReturnStatement() override = default;
virtual std::string formatted() override;
virtual void codegen(codegen::Builder& builder, codegen::Scope& scope) override;
virtual void typecheck(typecheck::State& state, typecheck::Scope& scope) override;
};
class InitializationStatement : public Statement {
private:
std::unique_ptr<types::Type> m_type;
std::shared_ptr<types::Type> m_type;
std::string m_name;
std::optional<std::unique_ptr<Expression>> m_expr;
public:
InitializationStatement(
token::Metadata meta,
std::unique_ptr<types::Type> ty,
std::shared_ptr<types::Type> ty,
std::string name,
std::optional<std::unique_ptr<Expression>> expr)
: Statement{ meta }
@ -132,6 +165,7 @@ namespace AST {
virtual ~InitializationStatement() override = default;
virtual std::string formatted() override;
virtual void codegen(codegen::Builder& builder, codegen::Scope& scope) override;
virtual void typecheck(typecheck::State& state, typecheck::Scope& scope) override;
};
class ExpressionStatement : public Statement {
@ -144,6 +178,7 @@ namespace AST {
virtual ~ExpressionStatement() override = default;
virtual std::string formatted() override;
virtual void codegen(codegen::Builder& builder, codegen::Scope& scope) override;
virtual void typecheck(typecheck::State& state, typecheck::Scope& scope) override;
};
class IfStatement : public Statement {
@ -164,32 +199,34 @@ namespace AST {
virtual ~IfStatement() override = default;
virtual std::string formatted() override;
virtual void codegen(codegen::Builder& builder, codegen::Scope& scope) override;
virtual void typecheck(typecheck::State& state, typecheck::Scope& scope) override;
};
class TopLevelStatement : public Node {
public:
TopLevelStatement(token::Metadata meta) : Node{ meta } {}
virtual void codegen(codegen::Builder& builder, codegen::Scope& scope) = 0;
virtual void typecheck(typecheck::State& state, typecheck::Scope& scope) = 0;
};
class Function : public TopLevelStatement {
private:
std::unique_ptr<types::Type> m_return_ty;
std::vector<std::pair<std::optional<std::string>, std::unique_ptr<types::Type>>> m_params;
std::shared_ptr<types::Type> m_return_ty;
std::vector<std::pair<std::optional<std::string>, std::shared_ptr<types::Type>>> m_params;
bool m_is_vararg;
std::string m_name;
std::optional<std::vector<std::unique_ptr<Statement>>> m_statements;
public:
Function(
token::Metadata meta,
std::unique_ptr<types::Type> return_ty,
std::vector<std::pair<std::optional<std::string>, std::unique_ptr<types::Type>>> params,
std::shared_ptr<types::Type> return_ty,
std::vector<std::pair<std::optional<std::string>, std::shared_ptr<types::Type>>> params,
bool is_vararg,
std::string name,
std::optional<std::vector<std::unique_ptr<Statement>>> statements)
: TopLevelStatement{ meta }
, m_return_ty{ std::move(return_ty) }
, m_params{ std::move(params) }
, m_return_ty{ return_ty }
, m_params{ params }
, m_is_vararg{ is_vararg }
, m_name{ name }
, m_statements{ std::move(statements) } {
@ -197,6 +234,7 @@ namespace AST {
virtual ~Function() override = default;
virtual std::string formatted() override;
virtual void codegen(codegen::Builder& builder, codegen::Scope& scope) override;
virtual void typecheck(typecheck::State& state, typecheck::Scope& scope) override;
};
}

View File

@ -1,6 +1,7 @@
#include "codegen.h"
#include "ast.h"
#include "types.h"
#include "errors.h"
#include <llvm/IR/Module.h>
#include <llvm/IR/Verifier.h>
@ -53,7 +54,7 @@ namespace AST {
}
}
else {
throw codegen::Error("Value " + this->m_name + " not found", this->m_meta);
throw CompileError("Value " + this->m_name + " not found", this->m_meta);
}
}
@ -87,11 +88,11 @@ namespace AST {
std::make_shared<types::FundamentalType>(types::FundamentalTypeKind::Bool),
};
default:
throw codegen::Error("invalid binop", this->m_meta);
throw CompileError("invalid binop", this->m_meta);
}
}
catch (std::runtime_error& error) {
throw codegen::Error(error.what(), this->m_meta);
throw CompileError(error.what(), this->m_meta);
}
}
@ -140,7 +141,7 @@ namespace AST {
builder.builder->CreateStore(value.value, ptr, false);
}
scope.values[this->m_name] = codegen::StackValue{ ptr, std::move(this->m_type) };
scope.values[this->m_name] = codegen::StackValue{ ptr, this->m_type };
}
void IfStatement::codegen(codegen::Builder& builder, codegen::Scope& scope) {
@ -180,11 +181,11 @@ namespace AST {
void Function::codegen(codegen::Builder& builder, codegen::Scope& scope) {
std::shared_ptr<types::Type> ret_ty_ptr{ std::move(this->m_return_ty) };
std::shared_ptr<types::Type> ret_ty_ptr{ this->m_return_ty };
std::vector<std::shared_ptr<types::Type>> param_ty_ptrs{};
for (auto& param : this->m_params) {
param_ty_ptrs.push_back(std::move(param.second));
param_ty_ptrs.push_back(param.second);
}
auto fn_ty_ptr = std::shared_ptr<types::Type>{ new types::FunctionType{ ret_ty_ptr, param_ty_ptrs, this->m_is_vararg } };

View File

@ -11,13 +11,6 @@
#include "tokens.h"
namespace codegen {
class Error : public std::runtime_error {
public:
token::Metadata m_meta;
Error(std::string text, token::Metadata meta)
: std::runtime_error{ text }, m_meta{ meta } {}
};
struct StackValue {
llvm::Value* value = nullptr;
std::shared_ptr<types::Type> ty = nullptr;

15
src/errors.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef ERRORS_H
#define ERRORS_H
#include <memory>
#include "tokens.h"
class CompileError : public std::runtime_error {
public:
token::Metadata m_meta;
CompileError(std::string text, token::Metadata meta)
: std::runtime_error{ text }, m_meta{ meta } {}
};
#endif

View File

@ -19,6 +19,7 @@
#include "tokens.h"
#include "parsing.h"
#include "errors.h"
std::string read_file(std::string_view filepath) {
std::ifstream input{ std::string{filepath} };
@ -103,7 +104,7 @@ std::optional<CompileOutput> compile(std::string_view in_filename) {
tls->codegen(builder, scope);
}
}
catch (codegen::Error& error) {
catch (CompileError& error) {
std::cerr << "FATAL: " << error.what() << std::endl;
std::cerr << " at " << error.m_meta.start.line + 1 << ":" << error.m_meta.start.col + 1;
std::cerr << " to " << error.m_meta.end.line + 1 << ":" << error.m_meta.end.col + 1 << std::endl;

View File

@ -7,7 +7,7 @@ namespace parsing {
namespace {
Result<std::unique_ptr<AST::Expression>, std::string> parse_expression(token::TokenStream& stream);
Result<std::unique_ptr<types::Type>, std::string> parse_type(token::TokenStream& stream) {
Result<std::shared_ptr<types::Type>, std::string> parse_type(token::TokenStream& stream) {
token::TokenStream inner{ stream };
try {
auto token = inner.expect(token::Type::Ident);
@ -15,19 +15,19 @@ namespace parsing {
// TODO eventually make this be potentially more than one word
std::string type_name = token.content;
std::unique_ptr<types::Type> returned{};
std::shared_ptr<types::Type> returned{};
if (type_name == "int") {
auto ty = new types::FundamentalType{ types::FundamentalTypeKind::Int };
returned = std::unique_ptr<types::Type>{ ty };
returned = std::shared_ptr<types::Type>{ ty };
}
else if (type_name == "char") {
auto ty = new types::FundamentalType{ types::FundamentalTypeKind::Char };
returned = std::unique_ptr<types::Type>{ ty };
returned = std::shared_ptr<types::Type>{ ty };
}
else if (type_name == "void") {
auto ty = new types::FundamentalType{ types::FundamentalTypeKind::Void };
returned = std::unique_ptr<types::Type>{ ty };
returned = std::shared_ptr<types::Type>{ ty };
}
else {
throw std::runtime_error("Expected type name, got " + type_name);
@ -36,7 +36,7 @@ namespace parsing {
while (inner.peek().type == token::Type::Symbol && inner.peek().content == "*") {
inner.next();
auto ty = new types::PointerType{ std::move(returned) };
returned = std::unique_ptr<types::Type>{ ty };
returned = std::shared_ptr<types::Type>{ ty };
}
@ -274,7 +274,7 @@ namespace parsing {
auto name_token = inner.expect(token::Type::Ident);
inner.expect(token::Type::Symbol, "(");
std::vector<std::pair<std::optional<std::string>, std::unique_ptr<types::Type>>> params;
std::vector<std::pair<std::optional<std::string>, std::shared_ptr<types::Type>>> params;
bool is_vararg = false;
while (inner.peek().content != ")") {
if (params.size() > 0) {

122
src/typechecker.cpp Normal file
View File

@ -0,0 +1,122 @@
#include "typechecker.h"
#include "ast.h"
#include "errors.h"
namespace AST {
std::shared_ptr<types::Type> IntLiteralExpression::typecheck(
typecheck::State&,
typecheck::Scope&,
std::optional<std::shared_ptr<types::Type>>
) {
return std::shared_ptr<types::Type>{
new types::FundamentalType{ types::FundamentalTypeKind::Int }
};
}
std::shared_ptr<types::Type> StringLiteralExpression::typecheck(
typecheck::State&,
typecheck::Scope&,
std::optional<std::shared_ptr<types::Type>>
) {
auto char_ty = std::shared_ptr<types::Type>{
new types::FundamentalType{ types::FundamentalTypeKind::Char }
};
auto ptr_ty = new types::PointerType{ char_ty };
return std::shared_ptr<types::Type>{ptr_ty};
}
std::shared_ptr<types::Type> ValueReferenceExpression::typecheck(
typecheck::State&,
typecheck::Scope& scope,
std::optional<std::shared_ptr<types::Type>>
) {
return scope.symbols[this->m_name];
}
std::shared_ptr<types::Type> BinaryOperationExpression::typecheck(
typecheck::State& state,
typecheck::Scope& scope,
std::optional<std::shared_ptr<types::Type>>
) {
auto lhs_ty = this->m_lhs->typecheck(state, scope, {});
auto rhs_ty = this->m_rhs->typecheck(state, scope, {});
// TODO actually check binop types properly
return lhs_ty;
}
std::shared_ptr<types::Type> FunctionCallExpression::typecheck(
typecheck::State& state,
typecheck::Scope& scope,
std::optional<std::shared_ptr<types::Type>>
) {
auto expr_ty = this->m_fn_expr->typecheck(state, scope, {});
// TODO make sure function_ty really is a function type
auto fn_ty = dynamic_cast<types::FunctionType*>(expr_ty.get());
if (this->m_args.size() < fn_ty->m_param_tys.size()) {
state.errors.push_back(CompileError("too few arguments", this->m_meta));
}
else if (this->m_args.size() > fn_ty->m_param_tys.size() && !fn_ty->m_vararg) {
state.errors.push_back(CompileError("too many arguments", this->m_meta));
}
else {
for (int i = 0; i < static_cast<int>(fn_ty->m_param_tys.size()); i++) {
auto expected_param_ty = fn_ty->m_param_tys[i];
auto param_ty = this->m_args[i]->typecheck(state, scope, expected_param_ty);
// TODO make sure types actually match
}
}
return fn_ty->m_ret_ty;
}
void ReturnStatement::typecheck(typecheck::State& state, typecheck::Scope& scope) {
this->m_expr->typecheck(state, scope, scope.return_ty);
}
void InitializationStatement::typecheck(typecheck::State& state, typecheck::Scope& scope) {
if (this->m_expr) {
(*this->m_expr)->typecheck(state, scope, this->m_type);
}
scope.symbols[this->m_name] = this->m_type;
}
void ExpressionStatement::typecheck(typecheck::State& state, typecheck::Scope& scope) {
this->m_expr->typecheck(state, scope, {});
}
void IfStatement::typecheck(typecheck::State& state, typecheck::Scope& scope) {
auto bool_ty_ptr = new types::FundamentalType{ types::FundamentalTypeKind::Bool };
this->m_condition->typecheck(state, scope, std::shared_ptr<types::Type>{ bool_ty_ptr });
this->m_then->typecheck(state, scope);
if (this->m_else) {
(*this->m_else)->typecheck(state, scope);
}
}
void Function::typecheck(typecheck::State& state, typecheck::Scope& scope) {
auto return_ty = this->m_return_ty;
std::vector<std::shared_ptr<types::Type>> param_tys{};
for (auto& param : this->m_params) {
param_tys.push_back(param.second);
}
auto function_ty = new types::FunctionType{ return_ty, param_tys, this->m_is_vararg };
scope.symbols[this->m_name] = std::shared_ptr<types::Type>{ function_ty };
typecheck::Scope inner{ scope };
if (this->m_statements) {
for (auto& statement : *this->m_statements) {
statement->typecheck(state, inner);
}
}
}
}

20
src/typechecker.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef TYPECHECK_H
#define TYPECHECK_H
#include <map>
#include "types.h"
#include "errors.h"
namespace typecheck {
struct Scope {
std::map<std::string, std::shared_ptr<types::Type>> symbols;
std::optional<std::shared_ptr<types::Type>> return_ty;
};
struct State {
std::vector<CompileError> errors;
};
}
#endif

View File

@ -55,11 +55,10 @@ namespace types {
class FunctionType : public Type {
private:
public:
std::shared_ptr<Type> m_ret_ty;
std::vector<std::shared_ptr<Type>> m_param_tys;
bool m_vararg;
public:
FunctionType(std::shared_ptr<Type> ret_ty, std::vector<std::shared_ptr<Type>> param_tys, bool vararg)
: m_ret_ty{ std::move(ret_ty) }, m_param_tys{ std::move(param_tys) }, m_vararg{ vararg } {
}