Skip to content

Commit

Permalink
feat: Arrays (#57)
Browse files Browse the repository at this point in the history
Closes #56
  • Loading branch information
Gashmob authored Feb 8, 2025
2 parents 00baaa0 + 9c7a15d commit 01b63fb
Show file tree
Hide file tree
Showing 40 changed files with 1,179 additions and 148 deletions.
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ file(READ ${CMAKE_CURRENT_SOURCE_DIR}/VERSION FILC_VERSION)
project(
filc
VERSION ${FILC_VERSION}
LANGUAGES CXX
LANGUAGES C CXX
)

include(FetchContent)
Expand Down Expand Up @@ -91,8 +91,8 @@ message(DEBUG SRC_FILES=${SRC_FILES})
add_library(filc_lib ${SRC_FILES} ${ANTLR_Lexer_CXX_OUTPUTS} ${ANTLR_Parser_CXX_OUTPUTS})
target_link_libraries(filc_lib PRIVATE additional_config cxxopts::cxxopts antlr4_static ${llvm_libs} ${llvm_targets})

target_include_directories(filc_lib PUBLIC
"${PROJECT_SOURCE_DIR}/include"
target_include_directories(filc_lib PUBLIC "${PROJECT_SOURCE_DIR}/include")
target_include_directories(filc_lib SYSTEM PUBLIC
${cxxopts_INCLUDE_DIR}
${ANTLR4_INCLUDE_DIR}
${ANTLR_Lexer_OUTPUT_DIR}
Expand Down
4 changes: 4 additions & 0 deletions include/filc/grammar/DumpVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ class DumpVisitor final: public Visitor<void> {

auto visitVariableAddress(VariableAddress *address) -> void override;

auto visitArray(Array *array) -> void override;

auto visitArrayAccess(ArrayAccess *array_access) -> void override;

private:
std::ostream &_out;
int _indent_level;
Expand Down
21 changes: 21 additions & 0 deletions include/filc/grammar/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,27 @@ class PointerType final : public AbstractType {
std::shared_ptr<AbstractType> _pointed_type;
};

class ArrayType final : public AbstractType {
public:
ArrayType(unsigned int size, std::shared_ptr<AbstractType> contained_type);

[[nodiscard]] auto getName() const noexcept -> std::string override;

[[nodiscard]] auto getDisplayName() const noexcept -> std::string override;

[[nodiscard]] auto toDisplay() const noexcept -> std::string override;

[[nodiscard]] auto getSize() const noexcept -> unsigned int;

[[nodiscard]] auto getContainedType() const noexcept -> std::shared_ptr<AbstractType>;

auto generateLLVMType(llvm::LLVMContext *context) -> void override;

private:
unsigned int _size;
std::shared_ptr<AbstractType> _contained_type;
};

class AliasType final : public AbstractType {
public:
AliasType(std::string name, std::shared_ptr<AbstractType> aliased_type);
Expand Down
37 changes: 37 additions & 0 deletions include/filc/grammar/Visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@

#include "filc/grammar/ast.h"

#include <any>
#include <llvm/IR/Value.h>
#include <map>
#include <stack>
#include <stdexcept>

namespace filc {
template<typename Return> class Visitor {
Expand Down Expand Up @@ -59,6 +63,10 @@ template<typename Return> class Visitor {

virtual auto visitVariableAddress(VariableAddress *address) -> Return = 0;

virtual auto visitArray(Array *array) -> Return = 0;

virtual auto visitArrayAccess(ArrayAccess *array_access) -> Return = 0;

protected:
Visitor() = default;
};
Expand All @@ -71,6 +79,35 @@ class Visitable {

virtual auto acceptIRVisitor(Visitor<llvm::Value *> *visitor) -> llvm::Value * = 0;
};

class VisitorContext final {
public:
VisitorContext();

auto stack() -> void;

auto unstack() -> void;

auto set(const std::string &key, const std::any &value) -> void;

auto unset(const std::string &key) -> void;

[[nodiscard]] auto has(const std::string &key) const -> bool;

template<typename T>
auto get(const std::string &key) const -> T {
if (_values.top().find(key) == _values.top().end()) {
throw std::logic_error("There is not value for key: " + key);
}

return std::any_cast<T>(_values.top().at(key));
}

auto clear() -> void;

private:
std::stack<std::map<std::string, std::any>> _values;
};
} // namespace filc

#endif // FILC_VISITOR_H
73 changes: 73 additions & 0 deletions include/filc/grammar/array/Array.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* MIT License
*
* Copyright (c) 2025-Present Kevin Traini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef FILC_ARRAY_H
#define FILC_ARRAY_H

#include "filc/grammar/expression/Expression.h"

#include <memory>
#include <vector>

namespace filc {
class Array final : public Expression {
public:
explicit Array(const std::vector<std::shared_ptr<Expression>> &values);

[[nodiscard]] auto getValues() const -> const std::vector<std::shared_ptr<Expression>> &;

[[nodiscard]] auto getSize() const -> unsigned long;

auto setFullSize(unsigned long full_size) -> void;

[[nodiscard]] auto getFullSize() const -> unsigned long;

auto acceptVoidVisitor(Visitor<void> *visitor) -> void override;

auto acceptIRVisitor(Visitor<llvm::Value *> *visitor) -> llvm::Value * override;

private:
unsigned long _size;
unsigned long _full_size;
std::vector<std::shared_ptr<Expression>> _values;
};

class ArrayAccess final : public Expression {
public:
ArrayAccess(std::shared_ptr<Expression> array, unsigned int index);

[[nodiscard]] auto getArray() const -> std::shared_ptr<Expression>;

[[nodiscard]] auto getIndex() const -> unsigned int;

auto acceptVoidVisitor(Visitor<void> *visitor) -> void override;

auto acceptIRVisitor(Visitor<llvm::Value *> *visitor) -> llvm::Value * override;

private:
std::shared_ptr<Expression> _array;
unsigned int _index;
};
} // namespace filc

#endif // FILC_ARRAY_H
4 changes: 4 additions & 0 deletions include/filc/grammar/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ class Pointer;
class PointerDereferencing;

class VariableAddress;

class Array;

class ArrayAccess;
}

#endif // FILC_AST_H
12 changes: 6 additions & 6 deletions include/filc/grammar/pointer/Pointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,30 +51,30 @@ class Pointer final : public Expression {

class PointerDereferencing final : public Expression {
public:
explicit PointerDereferencing(std::string name);
explicit PointerDereferencing(const std::shared_ptr<Expression> &pointer);

[[nodiscard]] auto getName() const -> std::string;
[[nodiscard]] auto getPointer() const -> std::shared_ptr<Expression>;

auto acceptVoidVisitor(Visitor<void> *visitor) -> void override;

auto acceptIRVisitor(Visitor<llvm::Value *> *visitor) -> llvm::Value * override;

private:
std::string _name;
std::shared_ptr<Expression> _pointer;
};

class VariableAddress final : public Expression {
public:
explicit VariableAddress(std::string name);
explicit VariableAddress(const std::shared_ptr<Expression> &variable);

[[nodiscard]] auto getName() const -> std::string;
[[nodiscard]] auto getVariable() const -> std::shared_ptr<Expression>;

auto acceptVoidVisitor(Visitor<void> *visitor) -> void override;

auto acceptIRVisitor(Visitor<llvm::Value *> *visitor) -> llvm::Value * override;

private:
std::string _name;
std::shared_ptr<Expression> _variable;
};
} // namespace filc

Expand Down
7 changes: 7 additions & 0 deletions include/filc/llvm/IRGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

namespace filc {
class IRGenerator final: public Visitor<llvm::Value *> {
friend class CalculBuilder;

public:
explicit IRGenerator(const std::string &filename, const Environment *environment);

Expand Down Expand Up @@ -68,7 +70,12 @@ class IRGenerator final: public Visitor<llvm::Value *> {

auto visitVariableAddress(VariableAddress *address) -> llvm::Value * override;

auto visitArray(Array *array) -> llvm::Value * override;

auto visitArrayAccess(ArrayAccess *array_access) -> llvm::Value * override;

private:
std::unique_ptr<VisitorContext> _visitor_context;
std::unique_ptr<llvm::LLVMContext> _llvm_context;
std::unique_ptr<llvm::Module> _module;
std::unique_ptr<llvm::IRBuilder<>> _builder;
Expand Down
20 changes: 15 additions & 5 deletions include/filc/validation/CalculValidator.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include "filc/grammar/Type.h"
#include "filc/validation/Environment.h"

#include <memory>
#include <string>

Expand All @@ -34,18 +35,27 @@ class CalculValidator {
public:
explicit CalculValidator(Environment *environment);

[[nodiscard]] auto isCalculValid(const std::shared_ptr<AbstractType> &left_type, const std::string &op,
const std::shared_ptr<AbstractType> &right_type) const -> std::shared_ptr<AbstractType>;
[[nodiscard]] auto isCalculValid(
const std::shared_ptr<AbstractType> &left_type,
const std::string &op,
const std::shared_ptr<AbstractType> &right_type
) const -> std::shared_ptr<AbstractType>;

private:
Environment *_environment;

[[nodiscard]] auto isNumericOperatorValid(const std::shared_ptr<AbstractType> &left_type, const std::string &op) const -> std::shared_ptr<AbstractType>;
[[nodiscard]] auto
isNumericOperatorValid(const std::shared_ptr<AbstractType> &left_type, const std::string &op) const
-> std::shared_ptr<AbstractType>;

[[nodiscard]] auto isBoolOperatorValid(const std::string &op) const -> std::shared_ptr<AbstractType>;

[[nodiscard]] auto isPointerOperatorValid(const std::string &op) const -> std::shared_ptr<AbstractType>;
[[nodiscard]] auto isPointerOperatorValid(
const std::string &op,
const std::shared_ptr<AbstractType> &left_type,
const std::shared_ptr<AbstractType> &right_type
) const -> std::shared_ptr<AbstractType>;
};
}
} // namespace filc

#endif // FILC_CALCULVALIDATOR_H
50 changes: 50 additions & 0 deletions include/filc/validation/TypeBuilder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* MIT License
*
* Copyright (c) 2025-Present Kevin Traini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef FILC_TYPEBUILDER_H
#define FILC_TYPEBUILDER_H

#include "filc/validation/Environment.h"

#include <string>
#include <regex>

namespace filc {
class TypeBuilder final {
public:
explicit TypeBuilder(Environment *environment);

[[nodiscard]] auto tryBuildType(const std::string &type) const noexcept -> bool;

private:
Environment *_environment;
std::regex _pointer_type_regex;
std::regex _array_type_regex;

[[nodiscard]] auto tryBuildPointerType(const std::string &type) const noexcept -> bool;

[[nodiscard]] auto tryBuildArrayType(const std::string &type) const noexcept -> bool;
};
} // namespace filc

#endif // FILC_TYPEBUILDER_H
Loading

0 comments on commit 01b63fb

Please sign in to comment.