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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
cmake-build-debug
.idea
.DS_store
/third-party/
/cmake-build-debug-vm/
14 changes: 12 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@

#Required Env's, example:
#set(ENV{CMAKE_INCLUDE_PATH} "/usr/local/opt/flex/include:ENV{CMAKE_INCLUDE_PATH}")
#set(ENV{CMAKE_LIBRARY_PATH} "/usr/local/opt/flex/lib;/usr/local/opt/bison/lib:ENV{CMAKE_LIBRARY_PATH}")
#set(ENV{PATH} "/usr/local/opt/flex/bin:/usr/local/opt/bison/bin:ENV{PATH}")
#set(ENV{LLVM_DIR} "/usr/local/Cellar/llvm@11/lib")

cmake_minimum_required(VERSION 3.10)

add_subdirectory(third-party/yaml-cpp EXCLUDE_FROM_ALL)
Expand All @@ -6,7 +13,8 @@ add_subdirectory(third-party/googletest EXCLUDE_FROM_ALL)
project(compiler)


set(CMAKE_CXX_CLANG_TIDY clang-tidy -checks=-*,readability-*,-warnings-as-errors=*)
# Sad :(
#set(CMAKE_CXX_CLANG_TIDY clang-tidy -checks=-*,readability-*,warnings-as-errors=*,)

set(CMAKE_CXX_STANDARD 20)
set(COMPILE_FLAGS -Wall -Wextra -pedantic -Werror)
Expand All @@ -17,14 +25,16 @@ add_subdirectory(visitors)
add_subdirectory(symbol_table)
add_subdirectory(scope_table)
add_subdirectory(types)
add_subdirectory(ir_builder)
add_subdirectory(jit)


add_library(
Compiler
INTERFACE
)

target_link_libraries(Compiler INTERFACE Driver Visitors SymbolTable ScopeTable Types)
target_link_libraries(Compiler INTERFACE Driver Visitors SymbolTable ScopeTable Types IRBuilder JIT)

add_executable(
CompilerExe
Expand Down
3 changes: 2 additions & 1 deletion ast_components/ast_forward_declaration.hh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class TClassDeclaration;
class TClassMemberDeclarationList;
class TDivExpression;
class TEqExpression;
class TNEqExpression;
class TExpressionList;
class TFieldInvocation;
class TFieldInvocationExpression;
Expand All @@ -31,7 +32,6 @@ class TMethodInvocationExpression;
class TMethodInvocationStatement;
class TModExpression;
class TMulExpression;
class TNewArrayExpression;
class TNewExpression;
class TNotExpression;
class TOrExpression;
Expand All @@ -51,5 +51,6 @@ class TIntTypeNode;
class TBooleanExpression;
class TVoidTypeNode;
class TIdentifierTypeNode;
class TArrayTypeNode;

#endif//COMPILER_AST_FORWARD_DECLARATION_HH
2 changes: 1 addition & 1 deletion ast_components/declarations/variable.hh
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#ifndef COMPILER_VARIABLE_HH
#define COMPILER_VARIABLE_HH

#include <memory>
#include <string>
#include <vector>
#include <memory>

#include "types/type.hh"

Expand Down
11 changes: 11 additions & 0 deletions ast_components/expressions/binary_expressions.hh
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ class TEqExpression : public TBinaryExpression {
public:
explicit TEqExpression(TExpressionPtr&& lhs, TExpressionPtr&& rhs)
: TBinaryExpression(std::move(lhs), std::move(rhs)) {}

void Accept(IVisitor* visitor) override {
visitor->Visit(this);
}
Expand All @@ -129,6 +130,16 @@ public:
using TEqExpressionPtr = std::unique_ptr<TEqExpression>;


class TNEqExpression : public TBinaryExpression {
public:
explicit TNEqExpression(TExpressionPtr&& lhs, TExpressionPtr&& rhs)
: TBinaryExpression(std::move(lhs), std::move(rhs)) {}

void Accept(IVisitor* visitor) override {
visitor->Visit(this);
}
};

class TLeExpression : public TBinaryExpression {
public:
explicit TLeExpression(TExpressionPtr&& lhs, TExpressionPtr&& rhs)
Expand Down
28 changes: 15 additions & 13 deletions ast_components/expressions/expressions.hh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public:
visitor->Visit(this);
}

TExpression* Expression() const {
[[nodiscard]] TExpression* Expression() const {
return Expression_.get();
}

Expand All @@ -54,11 +54,11 @@ public:
visitor->Visit(this);
}

TExpression* Expression() const {
[[nodiscard]] TExpression* Expression() const {
return Expression_.get();
}

TExpression* Index() const {
[[nodiscard]] TExpression* Index() const {
return Index_.get();
}

Expand Down Expand Up @@ -93,7 +93,7 @@ public:
visitor->Visit(this);
}

TTypeNode* Type() const {
[[nodiscard]] TTypeNode* Type() const {
return Type_.get();
}

Expand All @@ -109,15 +109,16 @@ class TNewArrayExpression : public TExpression {
public:
explicit TNewArrayExpression(TTypeNodePtr&& type, TExpressionPtr&& size)
: Type_(std::move(type)), Size_(std::move(size)) {}

void Accept(IVisitor* visitor) override {
visitor->Visit(this);
}

TTypeNode* Type() const {
[[nodiscard]] TTypeNode* Type() const {
return Type_.get();
}

TExpression* Size() const {
[[nodiscard]] TExpression* Size() const {
return Size_.get();
}

Expand All @@ -137,7 +138,7 @@ public:
visitor->Visit(this);
}

const std::string& Identifier() const {
[[nodiscard]] const std::string& Identifier() const {
return Identifier_;
}

Expand All @@ -153,7 +154,7 @@ class TIntExpression : public TExpression {
public:
explicit TIntExpression(int value) : Value_(value) {}

int GetValue() const {
[[nodiscard]] int GetValue() const {
return Value_;
}
void Accept(IVisitor* visitor) override {
Expand Down Expand Up @@ -184,9 +185,10 @@ class TBooleanExpression : public TExpression {
public:
explicit TBooleanExpression(bool value) : Value_(value) {}

bool GetValue() const {
[[nodiscard]] bool GetValue() const {
return Value_;
}

void Accept(IVisitor* visitor) override {
visitor->Visit(this);
}
Expand Down Expand Up @@ -219,17 +221,17 @@ using TMethodInvocationExpressionPtr = std::unique_ptr<TMethodInvocationExpressi

class TFieldInvocationExpression : public TExpression {
public:
explicit TFieldInvocationExpression(TFieldInvocationPtr&& method) : Method_(std::move(method)) {}
explicit TFieldInvocationExpression(TFieldInvocationPtr&& field) : Invocation_(std::move(field)) {}
void Accept(IVisitor* visitor) override {
visitor->Visit(this);
}

TFieldInvocation* Method() {
return Method_.get();
TFieldInvocation* Invocation() {
return Invocation_.get();
}

private:
TFieldInvocationPtr Method_;
TFieldInvocationPtr Invocation_;
};

using TMethodInvocationExpressionPtr = std::unique_ptr<TMethodInvocationExpression>;
Expand Down
12 changes: 11 additions & 1 deletion ast_components/invocation.hh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <memory>
#include <string>
#include <utility>

#include "expressions/expression_base.hh"
#include "i_node.hh"
Expand Down Expand Up @@ -39,23 +40,31 @@ using TMethodInvocationPtr = std::unique_ptr<TMethodInvocation>;

class TFieldInvocation : public INode {
public:
explicit TFieldInvocation(std::string&& identifier) : Identifier_(identifier) {}
explicit TFieldInvocation(TExpressionPtr&& expression, std::string identifier)
: Expression_(std::move(expression)), Identifier_(std::move(identifier)) {}

void Accept(IVisitor* visitor) override {
visitor->Visit(this);
}

[[nodiscard]] TExpression* Expression() const {
return Expression_.get();
}

[[nodiscard]] const std::string& Identifier() const {
return Identifier_;
}

private:
TExpressionPtr Expression_;
std::string Identifier_;
};


using TFieldInvocationPtr = std::unique_ptr<TFieldInvocation>;


/*
class TFieldInvocationIndexed : public TFieldInvocation {
public:
explicit TFieldInvocationIndexed(std::string&& identifier, TExpressionPtr&& index)
Expand All @@ -74,5 +83,6 @@ private:


using TFieldInvocationIndexedPtr = std::unique_ptr<TFieldInvocationIndexed>;
*/

#endif//COMPILER_INVOCATION_HH
31 changes: 18 additions & 13 deletions ast_components/types/type.hh
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,7 @@

#include "i_node.hh"

class TTypeNode : public INode {
public:
void MakeArray() {
IsArray_ = true;
}

[[nodiscard]] bool IsArray() const {
return IsArray_;
}

private:
bool IsArray_ = false;
};
class TTypeNode : public INode {};


using TTypeNodePtr = std::unique_ptr<TTypeNode>;
Expand Down Expand Up @@ -75,4 +63,21 @@ private:

using TIdentifierTypeNodePtr = std::unique_ptr<TIdentifierTypeNode>;


class TArrayTypeNode : public TTypeNode {
public:
explicit TArrayTypeNode(TTypeNodePtr&& type) : Type_(std::move(type)) {}

void Accept(IVisitor* visitor) override {
visitor->Visit(this);
}

[[nodiscard]] TTypeNode* Type() const {
return Type_.get();
}

private:
TTypeNodePtr Type_;
};

#endif//COMPILER_TYPE_HH
6 changes: 0 additions & 6 deletions driver/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
cmake_minimum_required(VERSION 3.10)


if (${APPLE})
set(ENV{CMAKE_INCLUDE_PATH} "/usr/local/opt/flex/include:ENV{CMAKE_INCLUDE_PATH}")
set(ENV{CMAKE_LIBRARY_PATH} "/usr/local/opt/flex/lib;/usr/local/opt/bison/lib:ENV{CMAKE_LIBRARY_PATH}")
set(ENV{PATH} "/usr/local/opt/flex/bin:/usr/local/opt/bison/bin:ENV{PATH}")
endif ()

find_package(BISON 3.5 REQUIRED)
BISON_TARGET(
Parser
Expand Down
19 changes: 10 additions & 9 deletions driver/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@
PERCENT "%"
GT ">"
LT "<"
GE "=>"
GE ">="
LE "<="
EQ "=="
NEQ "!="

NOT "!"
AND "&&"
Expand Down Expand Up @@ -243,8 +244,7 @@ simple_type:

array_type:
simple_type "[]" {
$$ = std::move($1);
$$->MakeArray();
$$ = std::make_unique<TArrayTypeNode>(std::move($1));
}


Expand All @@ -260,7 +260,7 @@ type_identifier:
%right "||";
%right "&&";
%precedence "!";
%left ">" "<" "<=" ">=" "==";
%left ">" "<" "<=" ">=" "==" "!=";
%left "+" "-";
%left "*" "/" "%";
%left UMINUS;
Expand Down Expand Up @@ -293,6 +293,9 @@ method_invocation:
expr "." IDENTIFIER "(" expr_comma_separated_list ")" {
$$ = std::make_unique<TMethodInvocation>(std::move($1), std::move($3), std::move($5));
}
| IDENTIFIER "(" expr_comma_separated_list ")" {
$$ = std::make_unique<TMethodInvocation>(std::make_unique<TThisExpression>(), std::move($1), std::move($3));
}


expr_comma_separated_list:
Expand All @@ -317,10 +320,7 @@ expr_comma_separated_list_prefix:

field_invocation:
expr "." IDENTIFIER %prec NO_BRACKET {
$$ = std::make_unique<TFieldInvocation>(std::move($3));
}
| expr "." IDENTIFIER "[" expr "]" {
$$ = std::make_unique<TFieldInvocationIndexed>(std::move($3), std::move($5));
$$ = std::make_unique<TFieldInvocation>(std::move($1), std::move($3));
}


Expand All @@ -333,6 +333,7 @@ expr:
| expr ">=" expr {$$ = std::make_unique<TGeqExpression>(std::move($1), std::move($3));}
| expr "<=" expr {$$ = std::make_unique<TLeqExpression>(std::move($1), std::move($3));}
| expr "==" expr {$$ = std::make_unique<TEqExpression>(std::move($1), std::move($3));}
| expr "!=" expr {$$ = std::make_unique<TNEqExpression>(std::move($1), std::move($3));}

| expr "+" expr {$$ = std::make_unique<TSumExpression>(std::move($1), std::move($3));}
| expr "-" expr {$$ = std::make_unique<TSubExpression>(std::move($1), std::move($3));}
Expand All @@ -343,8 +344,8 @@ expr:

| expr "[" expr "]" {$$ = std::make_unique<TIndexExpression>(std::move($1), std::move($3));}
| expr "." "length" {$$ = std::make_unique<TLengthExpression>(std::move($1));}
| "new" simple_type "(" ")" {$$ = std::make_unique<TNewExpression>(std::move($2));}
| "new" simple_type "[" expr "]" {$$ = std::make_unique<TNewArrayExpression>(std::move($2), std::move($4));}
| "new" type_identifier "(" ")" {$$ = std::make_unique<TNewExpression>(std::move($2));}
| "!" expr {$$ = std::make_unique<TNotExpression>(std::move($2));}
| "(" expr ")" {$$ = std::move($2);}
| IDENTIFIER {$$ = std::make_unique<TIdentifierExpression>(std::move($1));}
Expand Down
Loading