Skip to content

Commit

Permalink
Merge pull request #50 from P4-ACMMMRW/new-value-system
Browse files Browse the repository at this point in the history
New value system
  • Loading branch information
mtygesen authored May 2, 2024
2 parents 0a3b3a1 + 6650279 commit c3f772d
Show file tree
Hide file tree
Showing 15 changed files with 95 additions and 233 deletions.
6 changes: 0 additions & 6 deletions Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,9 @@ int main(int argc, char **argv) {
root->accept(evaluator);

if (debug) {
// std::cout << tree->toStringTree(&parser, true) << "\n\n";

// Ast print
builder.getRoot()->print();

// Visitor
// 7std::shared_ptr<AstTestVisitor> visitor = std::make_shared<AstTestVisitor>();
// root->accept(visitor);

// print eval
evaluator->getVtable().print();
evaluator->getPtable().print();
Expand Down
5 changes: 4 additions & 1 deletion include/src/Evaluator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace dplsrc {
class Evaluator : public AstVisitor {
public:
Evaluator(bool verbose = false) : verbose(verbose) { initPtable(); }

void visit(const std::shared_ptr<AndExprNode> &node) override;
void visit(const std::shared_ptr<AssignNode> &node) override;
void visit(const std::shared_ptr<ColumnNode> &node) override;
Expand Down Expand Up @@ -60,6 +62,7 @@ class Evaluator : public AstVisitor {
private:
VariableTable vtable = VariableTable();
ProcedureTable ptable = ProcedureTable();
bool verbose;

/**
* Initializes the procedures from the standard library of the language
Expand All @@ -69,4 +72,4 @@ class Evaluator : public AstVisitor {

} // namespace dplsrc

#endif
#endif
15 changes: 11 additions & 4 deletions include/src/Value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Value {

/**
* Get the value as type T mutable
*/
*/
template <typename T>
T &getMut() {
return std::get<T>(innerValue);
Expand All @@ -49,13 +49,20 @@ class Value {
*/
std::string toString() const;

std::string toTypeString(bool verbose = false) const;

/**
* List value type
* DPL Types
*/
using List = std::vector<Value>;
using INT = long;
using FLOAT = double;
using BOOL = bool;
using STR = std::string;
using NONETYPE = std::nullptr_t;
using LIST = std::vector<Value>;

private:
mutable std::variant<long, double, std::string, bool, List, std::nullptr_t> innerValue;
mutable std::variant<INT, FLOAT, STR, BOOL, LIST, NONETYPE> innerValue;
};
} // namespace dplsrc

Expand Down
2 changes: 0 additions & 2 deletions include/src/ast/nodes/ListNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#include <UnaryNodeList.hpp>
#include <vector>

#include "Type.hpp"

class ListNode : public UnaryNodeList {
public:
ListNode(std::shared_ptr<AstNode> parent) : UnaryNodeList(std::move(parent)) {}
Expand Down
5 changes: 0 additions & 5 deletions include/src/ast/nodes/abstract/AstNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <string>
#include <utility>

#include "Type.hpp"
#include "Value.hpp"

class AstNode : public std::enable_shared_from_this<AstNode> {
Expand All @@ -29,9 +28,6 @@ class AstNode : public std::enable_shared_from_this<AstNode> {
void setText(std::string text) { this->text = std::move(text); }
void setParent(std::shared_ptr<AstNode> parent) { this->parent = std::move(parent); }

void setType(dplsrc::Type type) { this->type = type; }
dplsrc::Type getType() const { return type; }

void setVal(dplsrc::Value val) { this->val = std::move(val); }
dplsrc::Value getVal() { return val; }

Expand All @@ -43,7 +39,6 @@ class AstNode : public std::enable_shared_from_this<AstNode> {
size_t rule{};
std::shared_ptr<AstNode> parent;
std::string text;
dplsrc::Type type;
dplsrc::Value val;
};

Expand Down
2 changes: 1 addition & 1 deletion include/src/symbol_table/Procedure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Procedure : public Symbol {
* Function type for built-in procedures that take a vector of shared pointers to AstNodes as
* input and return any type
*/
using ProcType = std::function<std::pair<Type, Value>(std::vector<std::shared_ptr<AstNode>>)>;
using ProcType = std::function<Value(std::vector<std::shared_ptr<AstNode>>)>;

/**
* Constructor for built-in procedures
Expand Down
1 change: 0 additions & 1 deletion include/src/symbol_table/Symbol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <string>

#include "AstNode.hpp"
#include "Type.hpp"

namespace dplsrc {
class Symbol {
Expand Down
65 changes: 0 additions & 65 deletions include/src/symbol_table/Type.hpp

This file was deleted.

17 changes: 2 additions & 15 deletions include/src/symbol_table/Variable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,22 @@ class Variable : public Symbol {
/**
* @param id the id of the variable
* @param val the value of the variable
* @param type the type of the variable
*/
Variable(std::string id, Value val, Type type)
: Symbol(std::move(id)), val(std::move(val)), type(type) {}
Variable(std::string id, Value val) : Symbol(std::move(id)), val(std::move(val)) {}

/**
* @return the value of the variable
*/
Value getVal() const { return val; }

/**
* @return the type of the variable
*/
Type getType() const { return type; }

/**
* @param val the value of the variable
*/
void setVal(Value val) { this->val = std::move(val); }

/**
* @param type the type of the variable
*/
void setType(Type type) { this->type = type; }

private:
Value val;
Type type = Type::Primitive::NONETYPE;
};
} // namespace dplsrc

#endif
#endif
1 change: 0 additions & 1 deletion include/src/symbol_table/VariableTable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class VariableTable : public SymbolTable {
*/
void enterScope(Scope scope);


/**
* Exits the current variable scope
*/
Expand Down
Loading

0 comments on commit c3f772d

Please sign in to comment.