Skip to content
Merged
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
6 changes: 6 additions & 0 deletions include/filc/grammar/DumpVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ class DumpVisitor final: public Visitor<void> {

auto visitAssignation(Assignation *assignation) -> void override;

auto visitPointer(Pointer *pointer) -> void override;

auto visitPointerDereferencing(PointerDereferencing *pointer) -> void override;

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

private:
std::ostream &_out;
int _indent_level;
Expand Down
12 changes: 11 additions & 1 deletion include/filc/grammar/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ class AbstractType {

auto setLLVMType(llvm::Type *type) -> void;

[[nodiscard]] auto getLLVMType() const -> llvm::Type *;
[[nodiscard]] auto getLLVMType(llvm::LLVMContext *context) -> llvm::Type *;

protected:
AbstractType() = default;

virtual auto generateLLVMType(llvm::LLVMContext *context) -> void = 0;

private:
llvm::Type *_llvm_type = nullptr;
};
Expand All @@ -60,6 +62,8 @@ class Type final : public AbstractType {

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

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

private:
std::string _name;
};
Expand All @@ -74,6 +78,10 @@ class PointerType final : public AbstractType {

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

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

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

private:
std::shared_ptr<AbstractType> _pointed_type;
};
Expand All @@ -88,6 +96,8 @@ class AliasType final : public AbstractType {

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

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

private:
std::string _name;
std::shared_ptr<AbstractType> _aliased_type;
Expand Down
6 changes: 6 additions & 0 deletions include/filc/grammar/Visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ template<typename Return> class Visitor {

virtual auto visitAssignation(Assignation *assignation) -> Return = 0;

virtual auto visitPointer(Pointer *pointer) -> Return = 0;

virtual auto visitPointerDereferencing(PointerDereferencing *pointer) -> Return = 0;

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

protected:
Visitor() = default;
};
Expand Down
6 changes: 6 additions & 0 deletions include/filc/grammar/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ class Identifier;
class BinaryCalcul;

class Assignation;

class Pointer;

class PointerDereferencing;

class VariableAddress;
}

#endif // FILC_AST_H
81 changes: 81 additions & 0 deletions include/filc/grammar/pointer/Pointer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* MIT License
*
* Copyright (c) 2024-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_POINTER_H
#define FILC_POINTER_H

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

#include <memory>
#include <string>

namespace filc {
class Pointer final : public Expression {
public:
Pointer(std::string type_name, const std::shared_ptr<Expression> &value);

[[nodiscard]] auto getTypeName() const -> std::string;

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

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

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

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

private:
std::string _type_name;
std::shared_ptr<Expression> _value;
};

class PointerDereferencing final : public Expression {
public:
explicit PointerDereferencing(std::string name);

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

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

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

private:
std::string _name;
};

class VariableAddress final : public Expression {
public:
explicit VariableAddress(std::string name);

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

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

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

private:
std::string _name;
};
} // namespace filc

#endif // FILC_POINTER_H
46 changes: 46 additions & 0 deletions include/filc/llvm/GeneratorContext.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* MIT License
*
* Copyright (c) 2024-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 GENERATORCONTEXT_H
#define GENERATORCONTEXT_H

#include <llvm/IR/IRBuilder.h>
#include <map>
#include <string>

namespace filc {
class GeneratorContext {
public:
GeneratorContext();

auto setValue(const std::string &name, llvm::Value *value) -> void;

[[nodiscard]] auto getValue(const std::string &name) const -> llvm::Value *;

private:
std::map<std::string, llvm::Value *> _values;
};
} // namespace filc

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

#include "filc/grammar/Visitor.h"
#include "filc/validation/Environment.h"
#include "filc/llvm/GeneratorContext.h"
#include <llvm/IR/Module.h>
#include <llvm/IR/IRBuilder.h>
#include <memory>
Expand Down Expand Up @@ -61,10 +62,17 @@ class IRGenerator final: public Visitor<llvm::Value *> {

auto visitAssignation(Assignation *assignation) -> llvm::Value * override;

auto visitPointer(Pointer *pointer) -> llvm::Value * override;

auto visitPointerDereferencing(PointerDereferencing *pointer) -> llvm::Value * override;

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

private:
std::unique_ptr<llvm::LLVMContext> _llvm_context;
std::unique_ptr<llvm::Module> _module;
std::unique_ptr<llvm::IRBuilder<>> _builder;
GeneratorContext _context;
};
}

Expand Down
2 changes: 2 additions & 0 deletions include/filc/validation/Environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class Environment {

auto addName(const Name &name) -> void;

auto setName(const Name &name) -> void;

private:
std::map<std::string, std::shared_ptr<AbstractType>> _types;
std::map<std::string, Name> _names;
Expand Down
7 changes: 6 additions & 1 deletion include/filc/validation/Name.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,21 @@ class Name {
public:
Name();

Name(bool constant, std::string name, std::shared_ptr<AbstractType> type);
Name(bool constant, std::string name, std::shared_ptr<AbstractType> type, bool has_value);

[[nodiscard]] auto isConstant() const -> bool;

[[nodiscard]] auto hasValue() const -> bool;

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

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

auto hasValue(bool has_value) -> void;

private:
bool _constant;
bool _has_value;
std::string _name;
std::shared_ptr<AbstractType> _type;
};
Expand Down
6 changes: 6 additions & 0 deletions include/filc/validation/ValidationVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ class ValidationVisitor final : public Visitor<void> {

auto visitAssignation(Assignation *assignation) -> void override;

auto visitPointer(Pointer *pointer) -> void override;

auto visitPointerDereferencing(PointerDereferencing *pointer) -> void override;

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

private:
std::unique_ptr<ValidationContext> _context;
std::unique_ptr<Environment> _environment;
Expand Down
19 changes: 19 additions & 0 deletions src/grammar/DumpVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "filc/grammar/calcul/Calcul.h"
#include "filc/grammar/identifier/Identifier.h"
#include "filc/grammar/literal/Literal.h"
#include "filc/grammar/pointer/Pointer.h"
#include "filc/grammar/program/Program.h"
#include "filc/grammar/variable/Variable.h"

Expand Down Expand Up @@ -144,6 +145,24 @@ auto DumpVisitor::visitAssignation(Assignation *assignation) -> void {
_indent_level--;
}

auto DumpVisitor::visitPointer(Pointer *pointer) -> void {
printIdent();
_out << "[Pointer:" << pointer->getTypeName() << "]\n";
_indent_level++;
pointer->getValue()->acceptVoidVisitor(this);
_indent_level--;
}

auto DumpVisitor::visitPointerDereferencing(PointerDereferencing *pointer) -> void {
printIdent();
_out << "[PointerDereferencing:" << pointer->getName() << "]\n";
}

auto DumpVisitor::visitVariableAddress(VariableAddress *address) -> void {
printIdent();
_out << "[VariableAddress:" << address->getName() << "]\n";
}

auto DumpVisitor::printIdent() const -> void {
_out << std::string(_indent_level, '\t');
}
2 changes: 2 additions & 0 deletions src/grammar/FilLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ VAR: 'var';
VAL: 'val';
TRUE: 'true';
FALSE: 'false';
NEW: 'new';

// Operators
EQ: '=';
Expand All @@ -48,6 +49,7 @@ GT: '>';
GTE: '>=';
LPAREN: '(';
RPAREN: ')';
AMP: '&';

// Assignation operators
PLUS_EQ: '+=';
Expand Down
30 changes: 26 additions & 4 deletions src/grammar/FilParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ options {
#include "filc/grammar/calcul/Calcul.h"
#include "filc/grammar/identifier/Identifier.h"
#include "filc/grammar/assignation/Assignation.h"
#include "filc/grammar/pointer/Pointer.h"
#include <memory>
#include <vector>
}
Expand Down Expand Up @@ -63,6 +64,12 @@ expression returns[std::shared_ptr<filc::Expression> tree]
| i=IDENTIFIER {
$tree = std::make_shared<filc::Identifier>($i.text);
}
| p=pointer {
$tree = $p.tree;
}
| po=pointer_operation {
$tree = $po.tree;
}

// === Binary calcul ===
| el3=expression op3=MOD er3=expression {
Expand Down Expand Up @@ -135,20 +142,22 @@ number returns[std::shared_ptr<filc::Expression> tree]
variable_declaration returns[std::shared_ptr<filc::VariableDeclaration> tree]
@init {
bool is_constant = true;
std::string type;
std::string type_name;
std::shared_ptr<filc::Expression> value = nullptr;
}
@after {
$tree = std::make_shared<filc::VariableDeclaration>(is_constant, $name.text, type, value);
$tree = std::make_shared<filc::VariableDeclaration>(is_constant, $name.text, type_name, value);
}
: (VAL | VAR {
is_constant = false;
}) name=IDENTIFIER (COLON type=IDENTIFIER {
type = $type.text;
}) name=IDENTIFIER (COLON type {
type_name = $type.text;
})? (EQ value=expression {
value = $value.tree;
})?;

type : IDENTIFIER STAR?;

assignation returns[std::shared_ptr<filc::Assignation> tree]
: i1=IDENTIFIER EQ e1=expression {
$tree = std::make_shared<filc::Assignation>($i1.text, $e1.tree);
Expand All @@ -158,3 +167,16 @@ assignation returns[std::shared_ptr<filc::Assignation> tree]
calcul->setPosition(filc::Position($op, $e2.stop));
$tree = std::make_shared<filc::Assignation>($i2.text, calcul);
};

pointer returns[std::shared_ptr<filc::Pointer> tree]
: NEW t=IDENTIFIER LPAREN e=expression RPAREN {
$tree = std::make_shared<filc::Pointer>($t.text, $e.tree);
};

pointer_operation returns[std::shared_ptr<filc::Expression> tree]
: STAR i=IDENTIFIER {
$tree = std::make_shared<filc::PointerDereferencing>($i.text);
}
| AMP i=IDENTIFIER {
$tree = std::make_shared<filc::VariableAddress>($i.text);
};
Loading
Loading