Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/ast/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ namespace kore {
_statements.emplace_back(std::move(statement));
}

Ast::ConstIter Ast::begin() const {
Ast::const_iterator Ast::begin() const {
return _statements.cbegin();
}

Ast::ConstIter Ast::end() const {
Ast::const_iterator Ast::end() const {
return _statements.cend();
}

Expand Down
10 changes: 5 additions & 5 deletions src/ast/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ namespace kore {
class AstWriter;

/// Represents an abstract syntax tree
class Ast {
class Ast final {
public:
using ConstIter = std::vector<Statement::pointer>::const_iterator;
using const_iterator = std::vector<Statement::pointer>::const_iterator;

Ast();
Ast(Ast&& ast);
Ast(const fs::path& path);
virtual ~Ast();
~Ast();

/// The module name for the parsed file
std::string module_name() const;
Expand All @@ -34,8 +34,8 @@ namespace kore {
/// Add a statement to this AST
void add_statement(Owned<Statement> statement);

ConstIter begin() const;
ConstIter end() const;
const_iterator begin() const;
const_iterator end() const;

Ast& operator=(Ast&& other);

Expand Down
4 changes: 4 additions & 0 deletions src/ast/ast_visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ namespace kore {
UNUSED_PARAM(expr);
}

void AstVisitor::visit(TypeExpression& expr) {
UNUSED_PARAM(expr);
}

void AstVisitor::visit(Branch& statement) {
UNUSED_PARAM(statement);
}
Expand Down
2 changes: 2 additions & 0 deletions src/ast/ast_visitor.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef KORE_AST_VISITOR_HPP
#define KORE_AST_VISITOR_HPP

#include "ast/expressions/type_expression.hpp"
namespace kore {
enum class ValueContext;

Expand Down Expand Up @@ -55,6 +56,7 @@ namespace kore {
virtual void visit(IntegerExpression& expr);
virtual void visit(StringExpression& expr);
virtual void visit(UnaryExpression& expr);
virtual void visit(TypeExpression& expr);

// Statements
virtual void visit(Branch& statement);
Expand Down
1 change: 1 addition & 0 deletions src/ast/expressions/expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace kore {
case ExpressionType::Literal: return os << "literal";
case ExpressionType::Parameter: return os << "parameter";
case ExpressionType::Unary: return os << "unary";
case ExpressionType::Type: return os << "type";
}
}

Expand Down
1 change: 1 addition & 0 deletions src/ast/expressions/expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace kore {
Literal,
Parameter,
Unary,
Type,
};

std::ostream& operator<<(std::ostream& os, ExpressionType expr_type);
Expand Down
22 changes: 16 additions & 6 deletions src/ast/expressions/parameter.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
#include "ast/ast_visitor.hpp"
#include "ast/ast_writer.hpp"
#include "ast/expressions/parameter.hpp"
#include "types/unknown_type.hpp"

namespace kore {
Parameter::Parameter(const Token& token) : Identifier(token, nullptr) {
set_type(Type::unknown());
}
Parameter::Parameter(const std::string& name, const SourceLocation& location)
: Expression(ExpressionType::Identifier, location),
_name(name),
_type_expr(nullptr) {}

Parameter::Parameter(const Token& token, const Type* type)
: Identifier(token, type) {
Parameter::Parameter(const std::string& name, Owned<TypeExpression> type, const SourceLocation& location)
: Expression(ExpressionType::Identifier, location),
_name(name),
_type_expr(std::move(type)) {
}

Parameter::~Parameter() {}

std::string Parameter::name() const {
return _name;
}

const TypeExpression* Parameter::type_expr() const {
return _type_expr.get();
}

KORE_AST_VISITOR_ACCEPT_METHOD_DEFAULT_IMPL(Parameter)
}
17 changes: 12 additions & 5 deletions src/ast/expressions/parameter.hpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
#ifndef KORE_PARAMETER_HPP
#define KORE_PARAMETER_HPP

#include "ast/expressions/identifier.hpp"
#include "ast/expressions/expression.hpp"
#include "ast/expressions/type_expression.hpp"

namespace kore {
/// Ast node for function parameters
// TODO: Make it independent of identifiers as its type is part of the function type
class Parameter : public Identifier {
class Parameter : public Expression {
public:
Parameter(const Token& token);
Parameter(const Token& token, const Type* type);
Parameter(const std::string& name, const SourceLocation& location);
Parameter(const std::string& name, Owned<TypeExpression> type, const SourceLocation& location);
virtual ~Parameter();

std::string name() const;
const TypeExpression* type_expr() const;

KORE_AST_VISITOR_ACCEPT_METHOD_DEFAULT_DEFINITION

private:
std::string _name;
Owned<TypeExpression> _type_expr;
};
}

Expand Down
19 changes: 19 additions & 0 deletions src/ast/expressions/type_expression.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "ast/ast_writer.hpp"
#include "ast/expressions/type_expression.hpp"

namespace kore {
TypeExpression::TypeExpression(const std::string& type, SourceLocation location, bool optional, int rank)
: Expression(ExpressionType::Type, location),
_type(type),
_optional(optional),
_rank(rank)
{}

TypeExpression::~TypeExpression() {}

std::string TypeExpression::value() const {
return _type;
}

KORE_AST_VISITOR_ACCEPT_METHOD_DEFAULT_IMPL(TypeExpression)
}
24 changes: 24 additions & 0 deletions src/ast/expressions/type_expression.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef KORE_TYPE_EXPRESION_HPP
#define KORE_TYPE_EXPRESION_HPP

#include "ast/expressions/expression.hpp"

namespace kore {
/// An expression for a builtin or user-defined type
class TypeExpression : public Expression {
public:
TypeExpression(const std::string& type, SourceLocation location, bool optional, int rank);
virtual ~TypeExpression();

std::string value() const;

KORE_AST_VISITOR_ACCEPT_METHOD_DEFAULT_DEFINITION

private:
std::string _type;
bool _optional;
int _rank;
};
}

#endif // KORE_TYPE_EXPRESION_HPP
Loading