diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..c29797c Binary files /dev/null and b/.DS_Store differ diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..b6501eb --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,19 @@ +{ + "configurations": [ + { + "name": "Mac", + "includePath": [ + "${workspaceFolder}/**" + ], + "defines": [], + "macFrameworkPath": [ + "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks" + ], + "compilerPath": "/usr/bin/clang", + "cStandard": "c17", + "cppStandard": "c++98", + "intelliSenseMode": "macos-clang-x64" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..69ffce4 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,66 @@ +{ + "files.associations": { + "__bit_reference": "cpp", + "__config": "cpp", + "__debug": "cpp", + "__errc": "cpp", + "__functional_base": "cpp", + "__hash_table": "cpp", + "__locale": "cpp", + "__mutex_base": "cpp", + "__node_handle": "cpp", + "__nullptr": "cpp", + "__split_buffer": "cpp", + "__string": "cpp", + "__threading_support": "cpp", + "__tree": "cpp", + "__tuple": "cpp", + "algorithm": "cpp", + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "bitset": "cpp", + "cctype": "cpp", + "chrono": "cpp", + "cmath": "cpp", + "complex": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "exception": "cpp", + "functional": "cpp", + "initializer_list": "cpp", + "ios": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "iterator": "cpp", + "limits": "cpp", + "locale": "cpp", + "map": "cpp", + "memory": "cpp", + "mutex": "cpp", + "new": "cpp", + "optional": "cpp", + "ostream": "cpp", + "ratio": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "string": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "typeinfo": "cpp", + "unordered_map": "cpp", + "utility": "cpp", + "vector": "cpp" + } +} \ No newline at end of file diff --git a/Algorithm-visualizer/AST/assignment.cpp b/Algorithm-visualizer/AST/assignment.cpp new file mode 100644 index 0000000..4e99a47 --- /dev/null +++ b/Algorithm-visualizer/AST/assignment.cpp @@ -0,0 +1,6 @@ +#include "assignment.hpp" + +Assignment::Assignment() +{ + +} diff --git a/Algorithm-visualizer/AST/assignment.hpp b/Algorithm-visualizer/AST/assignment.hpp new file mode 100644 index 0000000..c3d203a --- /dev/null +++ b/Algorithm-visualizer/AST/assignment.hpp @@ -0,0 +1,63 @@ +#include "ast.hpp" +#include "statement.hpp" +#include "expression.hpp" +#ifndef ASSIGNMENT_HPP +#define ASSIGNMENT_HPP + +//enum var_type { +// unknown_var_type = 0, +// string = 1, +// integer = 2, +// character = 3, +// floating_point = 4 + +//}; + +//enum un_op { +// unknown_un_op = 0, +// negation = 1, +// plusplus = 2 +// //inverse = 2 (if we add this as a unary operation for number -> -number) +//}; + +//enum bin_op { +// unknown_bin_op = 0, +// conjunction = 1, +// disjunction = 2, +// addition = 3, +// subtraction = 4, +// multiplication = 5, +// division = 6, +// lthan = 7, +// mthan = 8, +// leq = 9, +// meq = 10, +// eq = 11, +// eqeq = 12 +//}; + +//enum jump_type { +// unknown_jump_type = 0, +// br = 1, +// cont = 2 +//}; +//enum expression_type { +// unknown_expression_type = 0 +//}; + + +class Assignment : public Statement { + public: + Assignment(); + ~Assignment(); + Expression* get_value(){return value;}; + void set_value(Expression* v){value= v;} + std::string get_name(){return name;}; + void set_name(std::string n){name= n;} + //x = 5; x = y + std::string get_subtype(){return "Assignment";}; +private: + std::string name; + Expression* value; +}; +#endif // ASSIGNMENT_HPP diff --git a/Algorithm-visualizer/AST/ast.hpp b/Algorithm-visualizer/AST/ast.hpp index fbd6763..a953118 100644 --- a/Algorithm-visualizer/AST/ast.hpp +++ b/Algorithm-visualizer/AST/ast.hpp @@ -1,29 +1,57 @@ #ifndef AST_H #define AST_H +#include +#include #include +using namespace std; -class AST : public QAbstractItemModel -{ - Q_OBJECT +enum un_op { + unknown_un_op = 0, + negation = 1, + plusplus = 2 + //inverse = 2 (if we add this as a unary operation for number -> -number) +}; -public: - explicit AST(QObject *parent = nullptr); +enum bin_op { + unknown_bin_op = 0, + conjunction = 1, + disjunction = 2, + addition = 3, + subtraction = 4, + multiplication = 5, + division = 6, + lthan = 7, + mthan = 8, + leq = 9, + meq = 10, + eq = 11, + eqeq = 12 +}; - // Header: - QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; +enum jump_type { + unknown_jump_type = 0, + br = 1, + cont = 2 - // Basic functionality: - QModelIndex index(int row, int column, - const QModelIndex &parent = QModelIndex()) const override; - QModelIndex parent(const QModelIndex &index) const override; +}; +enum expression_type { + unknown_expression_type = 0 - int rowCount(const QModelIndex &parent = QModelIndex()) const override; - int columnCount(const QModelIndex &parent = QModelIndex()) const override; +}; - QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; -private: +//AST CLASS + +class AST { +protected : AST(); +protected : ~AST(); +virtual std::string get_type() = 0; // Statement or Expression (this is an abstract method which will be defined in subclasses Statement +//and Expression and inherited by every other subclass below) +virtual std::string get_subtype() = 0; // Block, Declaration, UnOp, BinOp, ... +//(this is an abstract method which will be defined in subclasses Block, Declaration, ...) }; -#endif // AST_H + + +#endif diff --git a/Algorithm-visualizer/AST/binop.cpp b/Algorithm-visualizer/AST/binop.cpp new file mode 100644 index 0000000..111d0ff --- /dev/null +++ b/Algorithm-visualizer/AST/binop.cpp @@ -0,0 +1,6 @@ +#include "binop.hpp" + +BinOp::BinOp() +{ + +} diff --git a/Algorithm-visualizer/AST/binop.hpp b/Algorithm-visualizer/AST/binop.hpp new file mode 100644 index 0000000..90daf89 --- /dev/null +++ b/Algorithm-visualizer/AST/binop.hpp @@ -0,0 +1,143 @@ +#include "expression.hpp" +#include "ast.hpp" +#ifndef BINOP_HPP +#define BINOP_HPP + +class Addition : public Expression { +public : Addition(); +public : ~Addition(); +public : double get_value(){ + if (left_exp->get_exp_type() == "bool" or right_exp->get_exp_type() == "bool") {return 0;}; + return left_exp->get_value() + right_exp->get_value(); + }; +private : + Expression* left_exp; + Expression* right_exp; +}; + +class Subtraction : public Expression { +public : Subtraction(); +public : ~Subtraction(); +public : double get_value(){ + if (left_exp->get_exp_type() == "bool" or right_exp->get_exp_type() == "bool") {return 0;}; + return left_exp->get_value() - right_exp->get_value(); + }; +private : + Expression* left_exp; + Expression* right_exp; +}; + +class Multiplication : public Expression { +public : Multiplication(); +public : ~Multiplication(); +public : double get_value(){ + if (left_exp->get_exp_type() == "bool" or right_exp->get_exp_type() == "bool") {return 0;}; + return left_exp->get_value() * right_exp->get_value(); + }; +private : + Expression* left_exp; + Expression* right_exp; +}; + +class Division : public Expression { +public : Division(); +public : ~Division(); +public : double get_value(){ + if (left_exp->get_exp_type() == "bool" or right_exp->get_exp_type() == "bool" or right_exp->get_value() == 0) return 0; + return left_exp->get_value() / right_exp->get_value(); + }; +private : + Expression* left_exp; + Expression* right_exp; +}; + +class Mthan : public Expression { +public : Mthan(); +public : ~Mthan(); +public : double get_value(){ + if (left_exp->get_exp_type() == "bool" or right_exp->get_exp_type() == "bool") return 0; + if (left_exp->get_value() > right_exp->get_value()) return 1; + return 0; + }; +private : + Expression* left_exp; + Expression* right_exp; +}; + +class Lthan : public Expression { +public : Lthan(); +public : ~Lthan(); +public : double get_value(){ + if (left_exp->get_exp_type() == "bool" or right_exp->get_exp_type() == "bool") return 0; + if (left_exp->get_value() < right_exp->get_value()) return 1; + return 0; + }; +private : + Expression* left_exp; + Expression* right_exp; +}; + +class Leq : public Expression { +public : Leq(); +public : ~Leq(); +public : double get_value(){ + if (left_exp->get_exp_type() == "bool" or right_exp->get_exp_type() == "bool") return 0; + if (left_exp->get_value() <= right_exp->get_value()) return 1; + return 0; + }; +private : + Expression* left_exp; + Expression* right_exp; +}; + +class Meq : public Expression { +public : Meq(); +public : ~Meq(); +public : double get_value(){ + if (left_exp->get_exp_type() == "bool" or right_exp->get_exp_type() == "bool") return 0; + if (left_exp->get_value() >= right_exp->get_value()) return 1; + return 0; + }; +private : + Expression* left_exp; + Expression* right_exp; +}; + +class Eqeq : public Expression { +public : Eqeq(); +public : ~Eqeq(); +public : double get_value(){ + if (left_exp->get_value() == right_exp->get_value()) return 1; + return 0; + }; +private : + Expression* left_exp; + Expression* right_exp; +}; + +class AndOp : public Expression { +public : AndOp(); +public : ~AndOp(); +public : double get_value(){ + if (left_exp->get_value() && right_exp->get_value()) return 1; + return 0; + }; +private : + Expression* left_exp; + Expression* right_exp; +}; + +class OrOp : public Expression { +public : OrOp(); +public : ~OrOp(); +public : double get_value(){ + if (left_exp->get_value() || right_exp->get_value()) return 1; + return 0; + }; +private : + Expression* left_exp; + Expression* right_exp; +}; + + +#endif // BINOP_HPP diff --git a/Algorithm-visualizer/AST/binop_template.cpp b/Algorithm-visualizer/AST/binop_template.cpp new file mode 100644 index 0000000..5025fe4 --- /dev/null +++ b/Algorithm-visualizer/AST/binop_template.cpp @@ -0,0 +1,6 @@ +#include "binop_template.hpp" + +binop_template::binop_template() +{ + +} diff --git a/Algorithm-visualizer/AST/binop_template.hpp b/Algorithm-visualizer/AST/binop_template.hpp new file mode 100644 index 0000000..86b03b7 --- /dev/null +++ b/Algorithm-visualizer/AST/binop_template.hpp @@ -0,0 +1,36 @@ +#ifndef BINOP_TEMPLATE_HPP +#define BINOP_TEMPLATE_HPP + +#include "expression_template.hpp" + +template void set_expression(Expression* expression, Expression* e){ + e = expression; +}; + +class BoolBinOp : public Expression { + BoolBinOp(); + ~BoolBinOp(); + void set_operation(bin_op op){operation = op;}; + void set_left_expression(Expression* l_e){left_exp = l_e;}; + void set_right_expression(Expression* r_e){right_exp = r_e;}; +private: + bin_op operation = unknown_bin_op; + Expression* left_exp; + Expression* right_exp; +}; + +/* +class NumBinOp : public Expression, public Expression, public Expression { + NumBinOp(); + ~NumBinOp(); + void set_operation(bin_op op){operation = op;}; + void set_left_expression(Expression* l_e){left_exp = l_e;}; + void set_right_expression(Expression* r_e){right_exp = r_e;}; +private: + bin_op operation = unknown_bin_op; + Expression* left_exp; + Expression* right_exp; +}; +*/ + +#endif // BINOP_TEMPLATE_HPP diff --git a/Algorithm-visualizer/AST/block.cpp b/Algorithm-visualizer/AST/block.cpp new file mode 100644 index 0000000..0943fbe --- /dev/null +++ b/Algorithm-visualizer/AST/block.cpp @@ -0,0 +1,6 @@ +#include "block.hpp" + +Block::Block() +{ + +} diff --git a/Algorithm-visualizer/AST/block.hpp b/Algorithm-visualizer/AST/block.hpp new file mode 100644 index 0000000..33f07d5 --- /dev/null +++ b/Algorithm-visualizer/AST/block.hpp @@ -0,0 +1,16 @@ +#include "ast.hpp" +#include "statement.hpp" +#ifndef BLOCK_HPP +#define BLOCK_HPP + +class Block : public AST{ +protected : Block(); +protected : ~Block(); +std::string get_type(){return "Block";}; +public: +list statements; // list of statements in the block +std::map variables; +Block* parent_block; //nullptr if it is the program block +}; + +#endif // BLOCK_HPP diff --git a/Algorithm-visualizer/AST/boolean.cpp b/Algorithm-visualizer/AST/boolean.cpp new file mode 100644 index 0000000..aaf4e98 --- /dev/null +++ b/Algorithm-visualizer/AST/boolean.cpp @@ -0,0 +1,6 @@ +#include "boolean.hpp" + +Boolean::Boolean() +{ + +} diff --git a/Algorithm-visualizer/AST/boolean.hpp b/Algorithm-visualizer/AST/boolean.hpp new file mode 100644 index 0000000..45efe98 --- /dev/null +++ b/Algorithm-visualizer/AST/boolean.hpp @@ -0,0 +1,18 @@ + +#include "expression.hpp" +#include "ast.hpp" +#ifndef BOOLEAN_HPP +#define BOOLEAN_HPP + +class Boolean : public Expression { + //not sure if this is necessary but putting + //it down for now + Boolean(); + ~Boolean(); + void set_value(bool v){value = v;}; + bool is_true(){return value;}; + std::string get_subtype(){return "Bool";}; +private: + bool value; +}; +#endif // BOOLEAN_HPP diff --git a/Algorithm-visualizer/AST/decision.cpp b/Algorithm-visualizer/AST/decision.cpp new file mode 100644 index 0000000..b8979e2 --- /dev/null +++ b/Algorithm-visualizer/AST/decision.cpp @@ -0,0 +1,6 @@ +#include "decision.hpp" + +Decision::Decision() +{ + +} diff --git a/Algorithm-visualizer/AST/decision.hpp b/Algorithm-visualizer/AST/decision.hpp new file mode 100644 index 0000000..dd88f0e --- /dev/null +++ b/Algorithm-visualizer/AST/decision.hpp @@ -0,0 +1,20 @@ +#include "expression.hpp" +#include "statement.hpp" +#ifndef DECISION_H +#define DECISION_H + + +class Decision { +public : Decision(); +public : ~Decision(); +public : void set_condition(Expression* c){condition = c;}; +public : string get_subtype(){return "Decision";}; +public : double get_value(){ + if (condition->get_value()) return 1; + return 0; + }; +private: + Expression* condition; +}; + +#endif // DECISION_H diff --git a/Algorithm-visualizer/AST/declaration.cpp b/Algorithm-visualizer/AST/declaration.cpp new file mode 100644 index 0000000..6d58d50 --- /dev/null +++ b/Algorithm-visualizer/AST/declaration.cpp @@ -0,0 +1,6 @@ +#include "declaration.hpp" + +Declaration::Declaration() +{ + +} diff --git a/Algorithm-visualizer/AST/declaration.hpp b/Algorithm-visualizer/AST/declaration.hpp new file mode 100644 index 0000000..d6d447a --- /dev/null +++ b/Algorithm-visualizer/AST/declaration.hpp @@ -0,0 +1,23 @@ +#include "ast.hpp" +#include "statement.hpp" +#include "variable.hpp" +#include "block.hpp" +#ifndef DECLARATION_HPP +#define DECLARATION_HPP + +class Declaration : public Statement { +public: + Declaration(); + ~Declaration(); + void set_variable(Variable var){variable = var;}; + char get_value(){return value;}; + void set_value(char v){value = v;}; + Variable get_variable(){return variable;}; + std::string get_subtype(){return "Declaration";}; + //void add_variable_to_list(){Block variables; variables.insert({variable, value});}; +private: + Variable variable; + char value; +}; + +#endif // DECLARATION_HPP diff --git a/Algorithm-visualizer/AST/declaration_template.hpp b/Algorithm-visualizer/AST/declaration_template.hpp new file mode 100644 index 0000000..1e2a5a1 --- /dev/null +++ b/Algorithm-visualizer/AST/declaration_template.hpp @@ -0,0 +1,31 @@ +#include "ast.hpp" +#include "statement.hpp" +#include "variable.hpp" +#ifndef DECLARATION_HPP +#define DECLARATION_HPP + + +//enum var_type { +// unknown_var_type = 0, +// string = 1, +// integer = 2, +// character = 3, +// floating_point = 4 + +//}; + +class Declaration : public Statement { +public: + Declaration(); + ~Declaration(); + void set_variable(Variable var){variable = var;}; + char get_value(){return value;}; + void set_value(char v){value = v;}; + Variable get_variable(){return variable;}; + std::string get_subtype(){return "Declaration";}; +private: + Variable variable; + char value; +}; + +#endif // DECLARATION_HPP diff --git a/Algorithm-visualizer/AST/expression.cpp b/Algorithm-visualizer/AST/expression.cpp new file mode 100644 index 0000000..06f2b21 --- /dev/null +++ b/Algorithm-visualizer/AST/expression.cpp @@ -0,0 +1,6 @@ +#include "expression.hpp" + +Expression::Expression() +{ + +} diff --git a/Algorithm-visualizer/AST/expression.hpp b/Algorithm-visualizer/AST/expression.hpp new file mode 100644 index 0000000..b719bc8 --- /dev/null +++ b/Algorithm-visualizer/AST/expression.hpp @@ -0,0 +1,18 @@ +#include "ast.hpp" +#ifndef EXPRESSION_HPP +#define EXPRESSION_HPP + +class Expression : public AST { +public : Expression(); +public : Expression(expression_type t) { + type = t; + }; +public : ~Expression(); +std::string get_type(){return "Expression";}; +virtual double get_value() = 0; +virtual std::string get_exp_type() = 0; +private: + expression_type type = unknown_expression_type; +}; + +#endif // EXPRESSION_HPP diff --git a/Algorithm-visualizer/AST/expression_template.cpp b/Algorithm-visualizer/AST/expression_template.cpp new file mode 100644 index 0000000..e69de29 diff --git a/Algorithm-visualizer/AST/expression_template.hpp b/Algorithm-visualizer/AST/expression_template.hpp new file mode 100644 index 0000000..6ced2d4 --- /dev/null +++ b/Algorithm-visualizer/AST/expression_template.hpp @@ -0,0 +1,12 @@ +#ifndef EXPRESSION_TEMPLATE_H +#define EXPRESSION_TEMPLATE_H +#include "ast.hpp" + +template class Expression : public AST{ +protected : Expression(); + ~Expression(); +std::string get_type(){return "Expression";}; +T get_value() = 0; +}; + +#endif // EXPRESSION_TEMPLATE_H diff --git a/Algorithm-visualizer/AST/ifelse.cpp b/Algorithm-visualizer/AST/ifelse.cpp new file mode 100644 index 0000000..af071aa --- /dev/null +++ b/Algorithm-visualizer/AST/ifelse.cpp @@ -0,0 +1,6 @@ +#include "ifelse.hpp" + +IfElse::IfElse() +{ + +} diff --git a/Algorithm-visualizer/AST/ifelse.hpp b/Algorithm-visualizer/AST/ifelse.hpp new file mode 100644 index 0000000..b8ce871 --- /dev/null +++ b/Algorithm-visualizer/AST/ifelse.hpp @@ -0,0 +1,22 @@ +#include "assignment.hpp" +#include "decision.hpp" +#include "ifrest.hpp" +#include "ast.hpp" +#ifndef IFELSE_HPP +#define IFELSE_HPP + +class IfElse : public Decision { + IfElse(); + ~IfElse(); + //attributes: condition(expression), IfRest + void set_condition(Expression* c){condition = c;}; + Expression* get_condition(){return condition;}; + void set_else_stmt(IfRest* stmt){else_stmt = stmt;}; + IfRest* get_else_stmt(){return else_stmt;}; + std::string get_subtype(){return "IfElse";}; +private: + Expression* condition; + IfRest* else_stmt; +}; + +#endif // IFELSE_HPP diff --git a/Algorithm-visualizer/AST/ifrest.cpp b/Algorithm-visualizer/AST/ifrest.cpp new file mode 100644 index 0000000..73b1ea0 --- /dev/null +++ b/Algorithm-visualizer/AST/ifrest.cpp @@ -0,0 +1,6 @@ +#include "ifrest.h" + +IfRest::IfRest() +{ + +} diff --git a/Algorithm-visualizer/AST/ifrest.hpp b/Algorithm-visualizer/AST/ifrest.hpp new file mode 100644 index 0000000..c428b2c --- /dev/null +++ b/Algorithm-visualizer/AST/ifrest.hpp @@ -0,0 +1,17 @@ +#include "block.hpp" +#include "ast.hpp" +#ifndef IFREST_H +#define IFREST_H + +class IfRest : public Statement { +public: + IfRest(); + ~IfRest(); + Block* get_block_stmt(){return block_stmt;}; + void set_block_stmt(Block* stmt){block_stmt = stmt;}; + std::string get_subtype(){return "IfRest";}; +private: + Block* block_stmt; +}; + +#endif // IFREST_H diff --git a/Algorithm-visualizer/AST/jump.cpp b/Algorithm-visualizer/AST/jump.cpp new file mode 100644 index 0000000..2b8cbcb --- /dev/null +++ b/Algorithm-visualizer/AST/jump.cpp @@ -0,0 +1,6 @@ +#include "jump.hpp" + +Jump::Jump() +{ + +} diff --git a/Algorithm-visualizer/AST/jump.hpp b/Algorithm-visualizer/AST/jump.hpp new file mode 100644 index 0000000..3e2aa2e --- /dev/null +++ b/Algorithm-visualizer/AST/jump.hpp @@ -0,0 +1,34 @@ +#include "statement.hpp" +#include "ast.hpp" +#ifndef JUMP_HPP +#define JUMP_HPP + +//enum jump_type { +// unknown_jump_type = 0, +// br = 1, +// cont = 2 +//}; +//enum expression_type { +// unknown_expression_type = 0 +//}; + + +class Jump : public Statement { + Jump(); + Jump(char value); + ~Jump(); + void set_value(jump_type v){value = v;}; + std::string get_value(){ + switch(value) { + case 0: return "unknown jump type"; + case 1: return "break"; + case 2: return "continue"; + } + }; + std::string get_subtype(){return "Jump";}; +private: + jump_type value = unknown_jump_type; +}; + + +#endif // JUMP_HPP diff --git a/Algorithm-visualizer/AST/number.hpp b/Algorithm-visualizer/AST/number.hpp new file mode 100644 index 0000000..f5a6d8f --- /dev/null +++ b/Algorithm-visualizer/AST/number.hpp @@ -0,0 +1,27 @@ +#ifndef NUMBER_HPP +#define NUMBER_HPP +#include "expression.hpp" +#include "ast.hpp" + +//enum var_type { +// unknown_var_type = 0, +// string = 1, +// integer = 2, +// character = 3, +// floating_point = 4 + +//}; + +class Number : Expression { + Number(); + ~Number(); + std::string get_subtype(){return "Number";}; + double get_value(){return std::stod(value);}; //returns the double of the value + //we keep all the values as doubles for simplicity because we don't want + //some random internal clash of types to happen +private: + var_type type; + std::string value; +}; + +#endif // NUMBER_HPP diff --git a/Algorithm-visualizer/AST/print.cpp b/Algorithm-visualizer/AST/print.cpp new file mode 100644 index 0000000..b654672 --- /dev/null +++ b/Algorithm-visualizer/AST/print.cpp @@ -0,0 +1,6 @@ +#include "print.hpp" + +Print::Print() +{ + +} diff --git a/Algorithm-visualizer/AST/print.hpp b/Algorithm-visualizer/AST/print.hpp new file mode 100644 index 0000000..a9c4276 --- /dev/null +++ b/Algorithm-visualizer/AST/print.hpp @@ -0,0 +1,17 @@ +#include "statement.hpp" +#include "expression.hpp" +#include "ast.hpp" +#ifndef PRINT_HPP +#define PRINT_HPP + +class Print : public Statement { + Print(); + ~Print(); + void set_exp(Expression* e){exp = e;}; + Expression* get_exp(){return exp;}; + std::string get_subtype(){return "Print";}; +private: + Expression* exp; +}; + +#endif // PRINT_HPP diff --git a/Algorithm-visualizer/AST/return.cpp b/Algorithm-visualizer/AST/return.cpp new file mode 100644 index 0000000..121d8de --- /dev/null +++ b/Algorithm-visualizer/AST/return.cpp @@ -0,0 +1,6 @@ +#include "return.hpp" + +Class::Class() +{ + +} diff --git a/Algorithm-visualizer/AST/return.hpp b/Algorithm-visualizer/AST/return.hpp new file mode 100644 index 0000000..41d9607 --- /dev/null +++ b/Algorithm-visualizer/AST/return.hpp @@ -0,0 +1,18 @@ + +#include "expression.hpp" +#include "statement.hpp" +#include "ast.hpp" +#ifndef RETURN_H +#define RETURN_H + +class Return : public Statement { + Return(); + ~Return(); + void set_exp(Expression* e){exp = e;}; + Expression* get_exp(){return exp;}; + std::string get_subtype(){return "Return";}; +private: + Expression* exp; +}; + +#endif // RETURN_H diff --git a/Algorithm-visualizer/AST/statement.cpp b/Algorithm-visualizer/AST/statement.cpp new file mode 100644 index 0000000..8b60f6f --- /dev/null +++ b/Algorithm-visualizer/AST/statement.cpp @@ -0,0 +1,6 @@ +#include "statement.hpp" + +Statement::Statement() +{ + +} diff --git a/Algorithm-visualizer/AST/statement.hpp b/Algorithm-visualizer/AST/statement.hpp new file mode 100644 index 0000000..9c13288 --- /dev/null +++ b/Algorithm-visualizer/AST/statement.hpp @@ -0,0 +1,14 @@ +#include "ast.hpp" +#include "block.hpp" +#ifndef STATEMENT_HPP +#define STATEMENT_HPP + +class Statement : public AST { +protected : Statement(); +protected : ~Statement(); + std::string get_type(){return "Statement";}; +Block* block; //reference to the block the statement is in: we want to be able to access the variables +}; + + +#endif // STATEMENT_HPP diff --git a/Algorithm-visualizer/AST/unop.cpp b/Algorithm-visualizer/AST/unop.cpp new file mode 100644 index 0000000..2eaf41f --- /dev/null +++ b/Algorithm-visualizer/AST/unop.cpp @@ -0,0 +1,6 @@ +#include "unop.hpp" + +UnOp::UnOp() +{ + +} diff --git a/Algorithm-visualizer/AST/unop.hpp b/Algorithm-visualizer/AST/unop.hpp new file mode 100644 index 0000000..eab621c --- /dev/null +++ b/Algorithm-visualizer/AST/unop.hpp @@ -0,0 +1,25 @@ +#include "expression.hpp" +#include "ast.hpp" +#ifndef UNOP_HPP +#define UNOP_HPP + +//enum un_op { +// unknown_un_op = 0, +// negation = 1, +// plusplus = 2 +// //inverse = 3 (if we add this as a unary operation for number -> -number) +//}; + +class UnOp : public Expression { + UnOp(); + ~UnOp(); + void set_operation(un_op op){operation = op;}; + void set_expression(Expression* e){expression = e;}; + un_op get_operation(){return operation;}; + Expression* get_expression(){return expression;}; + std::string get_subtype(){return "UnOp";}; +private: + un_op operation = unknown_un_op; + Expression* expression; +}; +#endif // UNOP_HPP diff --git a/Algorithm-visualizer/AST/variable.cpp b/Algorithm-visualizer/AST/variable.cpp new file mode 100644 index 0000000..d874ee2 --- /dev/null +++ b/Algorithm-visualizer/AST/variable.cpp @@ -0,0 +1,6 @@ +#include "variable.hpp" + +Variable::Variable() +{ + +} diff --git a/Algorithm-visualizer/AST/variable.hpp b/Algorithm-visualizer/AST/variable.hpp new file mode 100644 index 0000000..0137aeb --- /dev/null +++ b/Algorithm-visualizer/AST/variable.hpp @@ -0,0 +1,28 @@ +#ifndef VARIABLE_HPP +#define VARIABLE_HPP +#include "expression.hpp" + +class Variable : public Expression { +public : Variable(); +public : ~Variable(); +public : void set_name(std::string n){name = n;}; +public : void set_value(bool val){ + if (val) value = 1; + else value = 0; + type = "bool"; + }; +public : void set_value(double val){ + value = val; + type = "double"; + }; +public : std::string get_name(){return name;}; +public : double get_value(){return value;}; +public : std::string get_exp_type(){return type;}; +public : string get_type(){return "Variable";}; +private: + std::string name; + double value = 0; + std::string type; +}; + +#endif // VARIABLE_HPP diff --git a/Algorithm-visualizer/AST/variable_template.cpp b/Algorithm-visualizer/AST/variable_template.cpp new file mode 100644 index 0000000..dfa54d7 --- /dev/null +++ b/Algorithm-visualizer/AST/variable_template.cpp @@ -0,0 +1,21 @@ +#include "variable_template.hpp" + +template<> std::string Variable::type() { + return "Bool"; +}; + +template<> std::string Variable::type() { + return "Integer"; +}; + +template<> std::string Variable::type() { + return "Float"; +}; + +template<> std::string Variable::type() { + return "Double"; +}; + +template<> std::string Variable::type() { + return "String"; +}; diff --git a/Algorithm-visualizer/AST/variable_template.hpp b/Algorithm-visualizer/AST/variable_template.hpp new file mode 100644 index 0000000..6a51854 --- /dev/null +++ b/Algorithm-visualizer/AST/variable_template.hpp @@ -0,0 +1,19 @@ +#ifndef VARIABLE_TEMPLATE_H +#define VARIABLE_TEMPLATE_H + +#include "expression_template.hpp" + +template class Variable : Expression { + Variable(); + ~Variable(); + void set_name(char n){name = n;}; + std::string get_name(){return name;}; + std::string get_subtype(){return "Variable";}; + T get_value(){return value;}; + std::string type(); +private: + std::string name; + T value; +}; + +#endif // VARIABLE_TEMPLATE_H diff --git a/Algorithm-visualizer/AST/while.cpp b/Algorithm-visualizer/AST/while.cpp new file mode 100644 index 0000000..1f64951 --- /dev/null +++ b/Algorithm-visualizer/AST/while.cpp @@ -0,0 +1,6 @@ +#include "while.hpp" + +While::While() +{ + +} diff --git a/Algorithm-visualizer/AST/while.hpp b/Algorithm-visualizer/AST/while.hpp new file mode 100644 index 0000000..b5721b1 --- /dev/null +++ b/Algorithm-visualizer/AST/while.hpp @@ -0,0 +1,22 @@ +#include "decision.hpp" +#include "block.hpp" +#include "ast.hpp" +#ifndef WHILE_HPP +#define WHILE_HPP + +class While : public Decision { +public: + While(); + ~While(); + void set_condition(Expression* c){condition = c;}; + Expression* get_condition(){return condition;}; + Block* get_block_stmt(){return block_stmt;}; + void set_block_stmt(Block* stmt){block_stmt = stmt;}; + std::string get_subtype(){return "While";}; + //attributes: condition, block +private: + Expression* condition; + Block* block_stmt; +}; + +#endif // WHILE_HPP diff --git a/Algorithm-visualizer/Algorithm-visualizer.pro b/Algorithm-visualizer/Algorithm-visualizer.pro index b0b1b11..d8eb6a2 100644 --- a/Algorithm-visualizer/Algorithm-visualizer.pro +++ b/Algorithm-visualizer/Algorithm-visualizer.pro @@ -9,15 +9,60 @@ CONFIG += c++11 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ + AST/assignment.cpp \ AST/ast.cpp \ + AST/binop.cpp \ + AST/binop_template.cpp \ + AST/block.cpp \ + AST/boolean.cpp \ + AST/decision.cpp \ + AST/declaration.cpp \ + AST/expression.cpp \ + AST/expression_template.cpp \ + AST/ifelse.cpp \ + AST/ifrest.cpp \ + AST/jump.cpp \ + AST/print.cpp \ + AST/return.cpp \ + AST/statement.cpp \ + AST/unop.cpp \ + AST/variable.cpp \ + AST/variable_template.cpp \ + AST/while.cpp \ + GUI/codecell.cpp \ GUI/gui.cpp \ + GUI/varcell.cpp \ + GUI/viewer.cpp \ Parsing/parsing.cpp \ main.cpp \ mainwindow.cpp HEADERS += \ + AST/assignment.hpp \ AST/ast.hpp \ + AST/binop.hpp \ + AST/binop_template.hpp \ + AST/block.hpp \ + AST/boolean.hpp \ + AST/decision.hpp \ + AST/declaration.hpp \ + AST/declaration_template.hpp \ + AST/expression.hpp \ + AST/expression_template.hpp \ + AST/ifelse.hpp \ + AST/ifrest.hpp \ + AST/jump.hpp \ + AST/print.hpp \ + AST/return.hpp \ + AST/statement.hpp \ + AST/unop.hpp \ + AST/variable.hpp \ + AST/variable_template.hpp \ + AST/while.hpp \ + GUI/codecell.h \ GUI/gui.hpp \ + GUI/varcell.h \ + GUI/viewer.h \ Parsing/parsing.hpp \ mainwindow.h diff --git a/Algorithm-visualizer/GUI/codecell.cpp b/Algorithm-visualizer/GUI/codecell.cpp new file mode 100644 index 0000000..583709b --- /dev/null +++ b/Algorithm-visualizer/GUI/codecell.cpp @@ -0,0 +1,6 @@ +#include "codecell.h" + +CodeCell::CodeCell(QWidget *parent) : QWidget(parent) +{ + +} diff --git a/Algorithm-visualizer/GUI/codecell.h b/Algorithm-visualizer/GUI/codecell.h new file mode 100644 index 0000000..6f8c8ec --- /dev/null +++ b/Algorithm-visualizer/GUI/codecell.h @@ -0,0 +1,16 @@ +#ifndef CODECELL_H +#define CODECELL_H + +#include + +class CodeCell : public QWidget +{ + Q_OBJECT +public: + explicit CodeCell(QWidget *parent = nullptr); + +signals: + +}; + +#endif // CODECELL_H diff --git a/Algorithm-visualizer/GUI/varcell.cpp b/Algorithm-visualizer/GUI/varcell.cpp new file mode 100644 index 0000000..81a04f2 --- /dev/null +++ b/Algorithm-visualizer/GUI/varcell.cpp @@ -0,0 +1,23 @@ + #include "varcell.h" + +VarCell::VarCell(QWidget *parent) : QGraphicsView(parent) +{ + +} + +void VarCell::wheelEvent(QWheelEvent *event) +{ + setTransformationAnchor(QGraphicsView::AnchorUnderMouse); + double scaleFactor = 1.15; + + if (event->delta() > 0) { + + scale(scaleFactor,scaleFactor); + } + + else { + + scale(1/scaleFactor,1/scaleFactor); + } + +} diff --git a/Algorithm-visualizer/GUI/varcell.h b/Algorithm-visualizer/GUI/varcell.h new file mode 100644 index 0000000..fa384ae --- /dev/null +++ b/Algorithm-visualizer/GUI/varcell.h @@ -0,0 +1,17 @@ +#ifndef VARCELL_H +#define VARCELL_H + +#include +#include +#include + +class VarCell : public QGraphicsView +{ +public: + VarCell(QWidget* parent = 0); + +protected: + virtual void wheelEvent(QWheelEvent *event); +}; + +#endif // VARCELL_H diff --git a/Algorithm-visualizer/GUI/viewer.cpp b/Algorithm-visualizer/GUI/viewer.cpp new file mode 100644 index 0000000..8e71339 --- /dev/null +++ b/Algorithm-visualizer/GUI/viewer.cpp @@ -0,0 +1,140 @@ +#include "viewer.h" +#include "math.h" +#include +#include + +Viewer::Viewer(QWidget *parent) : QWidget(parent), mBackgroundColor(0,0,255),mShapeColor(255,255,255),mShape(Process) +{ + on_shape_changed(); +} + +QSize Viewer::minimumSizeHint() const +{ + return QSize(100,100); +} +QSize Viewer::sizeHint() const +{ + return QSize(400,200); +} + +void Viewer::on_shape_changed() +{ + switch (mShape) { + case Arrow: + mScale =50; + mIntervalLength = 1; + mStepCount = 128; + mBackgroundColor = Qt::blue; + break; + + case UnitLine: + mScale =50; + mIntervalLength = 1; + mStepCount = 128; + mBackgroundColor = Qt::blue; + break; + + case Process: + mScale =4; + mIntervalLength = 4*M_PI; + mStepCount = 256; + mBackgroundColor = Qt::blue; + break; + + case Decision: + mScale =40; + mIntervalLength = 2*M_PI; + mStepCount = 256; + mBackgroundColor = Qt::blue; + break; + + default: + break; + } +} + +QPointF Viewer::compute(float t) +{ + switch (mShape) { + case Arrow: + return compute_arrow(t); + break; + + case UnitLine: + //mBackgroundColor = Qt::green; + return compute_unitline(t); + break; + + case Process: + //mBackgroundColor = Qt::blue; + return compute_process(t); + break; + + case Decision: + //mBackgroundColor = Qt::yellow; + return compute_decision(t); + break; + + default: + break; + } + return QPointF(0,0); +} + +QPointF Viewer::compute_arrow(float t) +{ + return QPointF(1-t,1-t); //X,Y +} + +QPointF Viewer::compute_unitline(float t) +{ + return QPointF(1-t,1-t); //X,Y +} + +QPointF Viewer::compute_decision(float t) +{ + return QPointF( + (t-4), //X + (2*t*t*t - 24*t*t + 96*t - 128) //Y + ); +} + +QPointF Viewer::compute_process(float t) +{ + float cos_t = cos(t); + float sin_t = sin(t); + float x = 2*cos_t*cos_t*cos_t; + float y=2*sin_t*sin_t*sin_t; + return QPointF(x,y); +} + +void Viewer::paintEvent(QPaintEvent *event) +{ + QPainter painter(this); + painter.setRenderHint(QPainter::Antialiasing,true); + + painter.setBrush(mBackgroundColor); + painter.setPen(mShapeColor); + painter.drawRect(this->rect()); + + QPoint center = this->rect().center(); + + QPointF prevPoint = compute(0); + QPoint prevPixel; + prevPixel.setX(prevPoint.x()*mScale + center.x()); + prevPixel.setY(prevPoint.y()*mScale + center.y()); + + float step = mIntervalLength/mStepCount; + for(float t=0; t< mIntervalLength; t+=step){ + QPointF point = compute(t); + + QPoint pixel; + pixel.setX(point.x()*mScale + center.x()); + pixel.setY(point.y()*mScale + center.y()); + + painter.drawLine(pixel,prevPixel); + prevPixel =pixel; + } +} + + diff --git a/Algorithm-visualizer/GUI/viewer.h b/Algorithm-visualizer/GUI/viewer.h new file mode 100644 index 0000000..75f7e46 --- /dev/null +++ b/Algorithm-visualizer/GUI/viewer.h @@ -0,0 +1,50 @@ +#ifndef VIEWER_H +#define VIEWER_H + +#include + +class Viewer : public QWidget +{ + Q_OBJECT +public: + explicit Viewer(QWidget *parent = nullptr); + + QSize minimumSizeHint() const Q_DECL_OVERRIDE; + QSize sizeHint() const Q_DECL_OVERRIDE; + + //declaring all the shapes we will use + enum ShapeType {Arrow, UnitLine, Decision, Process}; + + void setBackgroundColor(QColor color){mBackgroundColor=color;} //setter function + QColor backgroudColor() const {return mBackgroundColor; } //getter, const function so that it doesn't modify the class variables + + void setShape(ShapeType shape){mShape=shape; on_shape_changed();} //declare rhe setter and getter fcts for shapes + ShapeType shape() const {return mShape; } + + void setScale(float scale) {mScale=scale; repaint();} + float scale() const {return mScale;} + +protected: + void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE; + +private: + QPointF compute_arrow(float t); + QPointF compute_unitline(float t); + QPointF compute_decision(float t); + QPointF compute_process(float t); + void on_shape_changed(); + QPointF compute(float t); //dispatch function based on mShape type +private: + QColor mBackgroundColor; + QColor mShapeColor; + ShapeType mShape; + + float mIntervalLength; + float mScale; + int mStepCount; + +signals: + +}; + +#endif // VIEWER_H diff --git a/Algorithm-visualizer/Interpreter practice/calc1.cpp b/Algorithm-visualizer/Interpreter practice/calc1.cpp new file mode 100644 index 0000000..80a30ba --- /dev/null +++ b/Algorithm-visualizer/Interpreter practice/calc1.cpp @@ -0,0 +1,97 @@ + +#include +#include +#include +#include + +char* INTEGER = "INTEGER"; +char* PLUS = "PLUS"; +char* eof = "EOF"; + +class Token{ +public: + Token(char* t, int v){type = t; value = v;}; + Token(){}; + Token(char* t){type = t;}; + char* get_type(){return type;}; + int get_value(){ + if (type != "PLUS"){ + return int(value); + } + }; + + + +private: + char* type; + int value; + + +}; + +class Interpreter{ +public: + Interpreter(char* t){text = t;}; + void error(){ + throw std::invalid_argument( "wrong format" ); + + } + Token get_next_token(){ + int len = 0; + int i = 0; + while (text[i] != '\n'){ + len ++; + i++; + } + if (pos > len - 1){ + return Token(eof); + } + if (isdigit(text[pos])){ + return Token(INTEGER, int(text[pos])); + } + if (text[pos] == 43){ + return Token(PLUS, text[pos]); + } + + error(); + } + + void eat(char* type){ + if (current_token.get_type() == type){ + current_token = (*this).get_next_token(); + } + else{ + error(); + } + } + + int expr(){ + current_token = (*this).get_next_token(); + Token left = current_token; + (*this).eat(INTEGER); + Token op = current_token; + (*this).eat(PLUS); + Token right = current_token; + (*this).eat(INTEGER); + return left.get_value() + right.get_value(); + + } + + + + +private: + char* text; + int pos = 0; + Token current_token; +}; + + +int main(){ + char* text; + std::cin>>text; + Interpreter interpreter = Interpreter(text); + interpreter.expr(); + return 0; + +} \ No newline at end of file diff --git a/Algorithm-visualizer/Interpreter practice/calc1.hpp b/Algorithm-visualizer/Interpreter practice/calc1.hpp new file mode 100644 index 0000000..cdd4e28 --- /dev/null +++ b/Algorithm-visualizer/Interpreter practice/calc1.hpp @@ -0,0 +1,85 @@ +#include +#include +#include + +char* INTEGER = "INTEGER"; +char* PLUS = "PLUS"; +char* eof = "EOF"; + +class Token{ +public: + Token(char* t, int v){type = t; value = v;}; + Token(){}; + Token(char* t){type = t;}; + char* get_type(){return type;}; + int get_value(){ + if (type != "PLUS"){ + return int(value); + } + }; + + + +private: + char* type; + int value; + + +}; + +class Interpreter{ +public: + Interpreter(char* t){text = t;}; + void error(){ + throw std::invalid_argument( "wrong format" ); + + } + Token get_next_token(){ + int len = 0; + int i = 0; + while (text[i] != '\n'){ + len ++; + i++; + } + if (pos > len - 1){ + return Token(eof); + } + if (isdigit(text[pos])){ + return Token(INTEGER, int(text[pos])); + } + if (text[pos] == 43){ + return Token(PLUS, text[pos]); + } + + error(); + } + + void eat(char* type){ + if (current_token.get_type() == type){ + current_token = (*this).get_next_token(); + } + else{ + error(); + } + } + + int expr(){ + current_token = (*this).get_next_token(); + Token left = current_token; + (*this).eat(INTEGER); + Token op = current_token; + (*this).eat(PLUS); + Token right = current_token; + (*this).eat(INTEGER); + return left.get_value() + right.get_value(); + + } + + + + +private: + char* text; + int pos = 0; + Token current_token; +}; \ No newline at end of file diff --git a/Algorithm-visualizer/Interpreter practice/calc1.py b/Algorithm-visualizer/Interpreter practice/calc1.py new file mode 100644 index 0000000..b745ec6 --- /dev/null +++ b/Algorithm-visualizer/Interpreter practice/calc1.py @@ -0,0 +1,86 @@ +INTEGER, MINUS, EOF = 'INTEGER', 'MINUS', 'EOF' + +class Token(object): + def __init__(self, type, value): + self.type = type + self.value = value + + def __str__(self): + return 'Token({type}, {value})'.format( + type=self.type, + value=repr(self.value) + ) + + def __repr__(self): + return self.__str__() + + + +class Interpreter(object): + def __init__(self, text): + self.text = text + self.pos = 0 + self.current_token = None + + def error(self): + raise Exception('Parsing error') + + + def get_next_token(self): + text = self.text + if self.pos > len(self.text) -1: + return Token(EOF, None) + while self.pos ') + except EOFError: + break + if not text: + continue + interpreter = Interpreter(text) + result = interpreter.expr() + print(result) + + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/Algorithm-visualizer/Interpreter practice/calc2.py b/Algorithm-visualizer/Interpreter practice/calc2.py new file mode 100644 index 0000000..1dc7552 --- /dev/null +++ b/Algorithm-visualizer/Interpreter practice/calc2.py @@ -0,0 +1,155 @@ + +# Token types +# EOF (end-of-file) token is used to indicate that +# there is no more input left for lexical analysis +INTEGER, PLUS, MINUS, EOF = 'INTEGER', 'PLUS', 'MINUS', 'EOF' + + +class Token(object): + def __init__(self, type, value): + # token type: INTEGER, PLUS, MINUS, or EOF + self.type = type + # token value: non-negative integer value, '+', '-', or None + self.value = value + + def __str__(self): + """String representation of the class instance. + + Examples: + Token(INTEGER, 3) + Token(PLUS '+') + """ + return 'Token({type}, {value})'.format( + type=self.type, + value=repr(self.value) + ) + + def __repr__(self): + return self.__str__() + + +class Interpreter(object): + def __init__(self, text): + # client string input, e.g. "3 + 5", "12 - 5", etc + self.text = text + # self.pos is an index into self.text + self.pos = 0 + # current token instance + self.current_token = None + self.current_char = self.text[self.pos] + + def error(self): + raise Exception('Error parsing input') + + def advance(self): + """Advance the 'pos' pointer and set the 'current_char' variable.""" + self.pos += 1 + if self.pos > len(self.text) - 1: + self.current_char = None # Indicates end of input + else: + self.current_char = self.text[self.pos] + + def skip_whitespace(self): + while self.current_char is not None and self.current_char.isspace(): + self.advance() + + def integer(self): + """Return a (multidigit) integer consumed from the input.""" + result = '' + while self.current_char is not None and self.current_char.isdigit(): + result += self.current_char + self.advance() + return int(result) + + def get_next_token(self): + """Lexical analyzer (also known as scanner or tokenizer) + + This method is responsible for breaking a sentence + apart into tokens. + """ + while self.current_char is not None: + + if self.current_char.isspace(): + self.skip_whitespace() + continue + + if self.current_char.isdigit(): + return Token(INTEGER, self.integer()) + + if self.current_char == '+': + self.advance() + return Token(PLUS, '+') + + if self.current_char == '-': + self.advance() + return Token(MINUS, '-') + + self.error() + + return Token(EOF, None) + + def eat(self, token_type): + # compare the current token type with the passed token + # type and if they match then "eat" the current token + # and assign the next token to the self.current_token, + # otherwise raise an exception. + if self.current_token.type == token_type: + self.current_token = self.get_next_token() + else: + self.error() + + def expr(self): + """Parser / Interpreter + + expr -> INTEGER PLUS INTEGER + expr -> INTEGER MINUS INTEGER + """ + # set current token to the first token taken from the input + self.current_token = self.get_next_token() + + # we expect the current token to be an integer + left = self.current_token + self.eat(INTEGER) + + # we expect the current token to be either a '+' or '-' + op = self.current_token + if op.type == PLUS: + self.eat(PLUS) + else: + self.eat(MINUS) + + # we expect the current token to be an integer + right = self.current_token + self.eat(INTEGER) + # after the above call the self.current_token is set to + # EOF token + + # at this point either the INTEGER PLUS INTEGER or + # the INTEGER MINUS INTEGER sequence of tokens + # has been successfully found and the method can just + # return the result of adding or subtracting two integers, + # thus effectively interpreting client input + if op.type == PLUS: + result = left.value + right.value + else: + result = left.value - right.value + return result + + +def main(): + while True: + try: + # To run under Python3 replace 'raw_input' call + # with 'input' + text = raw_input('calc> ') + except EOFError: + break + if not text: + continue + interpreter = Interpreter(text) + result = interpreter.expr() + print(result) + + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/Algorithm-visualizer/mainwindow.cpp b/Algorithm-visualizer/mainwindow.cpp index 41a26bd..0c5b216 100644 --- a/Algorithm-visualizer/mainwindow.cpp +++ b/Algorithm-visualizer/mainwindow.cpp @@ -2,8 +2,7 @@ #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) - : QMainWindow(parent) - , ui(new Ui::MainWindow) + : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } @@ -11,5 +10,11 @@ MainWindow::MainWindow(QWidget *parent) MainWindow::~MainWindow() { delete ui; + + //the following comm is if u want picture in the scrollable widget + //QImage image("path to the pciture"); + //item = new QGraphicsPixmapItem (QPixmap::fromImage(image)); + //scene = new QGraphicsScene(this); //it does make a problem with QgraphicsScene that is "not defined"? + } diff --git a/Algorithm-visualizer/mainwindow.h b/Algorithm-visualizer/mainwindow.h index 4643e32..0cc17f8 100644 --- a/Algorithm-visualizer/mainwindow.h +++ b/Algorithm-visualizer/mainwindow.h @@ -2,6 +2,10 @@ #define MAINWINDOW_H #include +#include //for scrollable widgets +#include //for scrollable widgets +#include //for scrollable widgets +#include "ui_mainwindow.h" QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } @@ -16,6 +20,156 @@ class MainWindow : public QMainWindow ~MainWindow(); private: - Ui::MainWindow *ui; + Ui::MainWindow ui; + + + //this can be added for including a photo in varcell scrollable + //QGraphicsItem *item; + //QGraphicsScene *scene; + //ui->graphicsView->setScene(scene); + //scene->addItem(item); + + + +}; + +class Program { + Program(); + ~Program(); + //list of statements or the + //program block - not sure yet +}; + +class Block { + Block(); + ~Block(); + //list of statements +}; + +class Statement { + Statement(); + ~Statement(); +}; + +class Expression { + Expression(); + ~Expression(); }; + +enum var_type { + string = 0, + integer = 1, + character = 2, + floating_point = 3 + +}; + +enum un_op { + negation = 0, + plusplus = 1 + //inverse = 2 (if we add this as a unary operation for number -> -number) +}; + +enum bin_op { + conjunction = 0, + disjunction = 1, + addition = 2, + subtraction = 3, + multiplication = 4, + division = 5, + lthan = 6, + mthan = 7, + leq = 8, + meq = 9, + eq = 10, + eqeq = 11 +}; + +class Declaration : Statement { + Declaration(); + ~Declaration(); + //int x = 7, bool y = False + //attributes: + //name + value of the variable or Variable + sth +}; + +class Assignment : Statement { + Assignment(); + ~Assignment(); + //x = 5; x = y +}; + +class Return : Statement { + Return(); + ~Return(); + //attribute: expression being returned +}; + +class Print : Statement { + Print(); + ~Print(); + //attirbute: expression to be printed +}; + +class Jump : Statement { + Jump(); + Jump(char value); + ~Jump(); + void set_value(); + private: + char value; // values are CONT or BREAK +}; + +class IfElse : Statement { + IfElse(); + ~IfElse(); + //attributes: condition(expression), IfRest +}; + +class IfRest : Statement { + IfRest(); + ~IfRest(); + //atrributes: a block, or another IfElse +}; + +class While : Statement { + While(); + ~While(); + //attributes: condition, block +}; + +class Variable : Expression { + Variable(); + ~Variable(); +private: + char name; + var_type type; +}; + +class UnOp : Expression { + UnOp(); + ~UnOp(); +private: + un_op operation; + Expression expression; +}; + +class BinOp : Expression { + BinOp(); + ~BinOp(); +private: + bin_op operation; + Expression left_exp; + Expression right_exp; +}; + +class Boolean : Expression { + //not sure if this is necessary but putting + //it down for now + Boolean(); + ~Boolean(); +private: + bool value; +}; + #endif // MAINWINDOW_H diff --git a/Algorithm-visualizer/mainwindow.ui b/Algorithm-visualizer/mainwindow.ui index b232854..6d7610c 100644 --- a/Algorithm-visualizer/mainwindow.ui +++ b/Algorithm-visualizer/mainwindow.ui @@ -6,17 +6,94 @@ 0 0 - 800 - 600 + 825 + 604 MainWindow - - + + + + + 30 + 40 + 771 + 491 + + + + + + + + + 5 + 0 + 371 + 231 + + + + + + + + + + + + + + + Visualize + + + + + + + + + + + + + 0 + 0 + 825 + 22 + + + + + + CodeCell + QWidget +
codecell.h
+ 1 +
+ + Viewer + QWidget +
viewer.h
+ 1 +
+ + VarCell + QWidget +
varcell.h
+ 1 +
+ + varcell + QGraphicsView +
varcell.h
+
+
diff --git a/Reports/Week2/reports.md b/Reports/Week2/reports.md index eaeeaf7..e1d5129 100644 --- a/Reports/Week2/reports.md +++ b/Reports/Week2/reports.md @@ -1,19 +1,21 @@ # Week 2 - Reports #### Elena Mrdja -This week I worked on grasping all the necessary knowledge in Qt graphics I will need to continue with the graphics part of the project. As planned, I finished the Qt Graphics course we found, and implemented the gathered skills through making a trial project where I created a main window with different shapes and their class methods, which will be of great help as I progress with coding the project. -After everyone in GUI team was done with the course, I set up a meeting to decide on all the custom classes and the shape types we want to have in our project. When this was decided, I started coding the main window of our project's GUI, adding the classes and creating a simple structure we plan to build upon next week. Next week our goal is to finish the text editor cell and implement all the shapes we decided to use. - -As the git leader, I created a common branch for all of us to collaborate, and made sure everyone has their own branch if they need it. I also created a tutorial document with all of the important git hub commands, so that everyone was sure on how to use it, and was there to help my teammates out with any git hub problems. - +This week I worked on grasping all the necessary knowledge in Qt graphics we will need. As planned, I finished the Qt Graphics course we found, and I implemented the gathered skills through making a trial project where I created a main window with different shapes and their class methods, which will be of great help as I progress with coding the project. +After everyone in GUI team was done with the course, I set up a meeting to decide on all the custom classes and the shape types we want to have in our project. When this was decided, I started coding the main window of our project's GUI, adding the classes and creating a simple structure we plan to build upon next week. Next week our goal is to finish the text editor cell and implement all the shapes we decided to use. +As the git leader, I created a tutorial document with all of the important git hub commands, so that everyone was sure on how to use it, and was there to help my teammates out with any git hub problems. #### Matea Gjika This week was dedicated to research. Since this is a complex project, where we need to build a parser and interpreter and learn about multiple libraries and complex data structures, it is very important for everyone to take some time to study and understand the main principles. I had 3 jobs this week: research a parses, research the antlr library, and research parser trees. The first resource I studied was a 19-part article “Let’s build an Interpreter”, which walks you through parsing, tokenising, grammars, parser trees and ASTs. I have completed 8/19 parts, so my goal is to finish this series by Tuesday. I also browsed the ANTLR documentation, and looked at some functions that specifically deal with parser trees. +The next step is to start building the AST classes and methods with the rest of my team. +The first resource I studied was a 19-part article “Let’s build an Interpreter”, which walks you through parsing, tokenising, grammars, parser trees and ASTs. I have completed 8/19 parts, so my goal is to finish this series by Tuesday. +I also browsed the ANTLR documentation, and looked at some functions that specifically deal with parser trees. The next step is to start building the AST classes and methods with the rest of my team. After a team meeting, we started to work on our AST classes. I was tasked with writing the setter and getter functions, as well as editing the classes that inherited from the Statement file found under ast.hpp. + #### Elsa Bismuth This week, I deepened my knowledge in ANTLR and tokenization by doing some research on internet. I defined more in details the grammar and I started to code the first token. Next week, I will try to finish to code the first 2 tokens. @@ -33,16 +35,25 @@ and we all need to give input into which attributes we need) #### Darya Todoskova During the second week, I did a more profound research on ANTLR, in order to know what output we will be receiving from Team 1. I also met a couple of times with my team to discuss our progress and to define specific tasks for each one of us to do. Indeed, I defined all the AST-inherited getters and setters in the ast.hpp file. Finally, I am currently trying to set up ANTLR on mac0S just like team 1, who has been trying to set it up on Windows. For the following week, I plan on refining our ast.hpp file and working on the exact output we are going to give to the GUI team, in order to make the transition smoother. -#### John Levy - #### Evdokia Gneusheva This week Team 1 came up with the idea to use ANTLR library for parsing so as a member of Team 2 my job was to get familier with this library in order to be able to convert parser trees created by Team 1 into AST. Precisely, to convert a parser tree into AST we need to use ANTLR4 visitor. Thus, I accomplished an online tutorial on how to use visitor class. But also, the most importantly, Team 2 started defining AST by creating multiple classes in ast.hpp file (on Workspace branch), where I coded set() and get() methods for all classes inherited from Expression class. Next week I will continue working on AST classes and start the conversion of parser tree created by Team 1 into AST, as well as working on the output of our AST which we will pass to the Team 3. #### Stefan Vayl +Throughout the whole week I have been working on installation of ANTRL library. I have managed to install it on my computer even though there were many difficulties that I had to face. I managed to test this library on small grammar files that I found online. From those grammar files it created Lexer and Parser files. Although, there is still work to be done next week especially I am planning to find out how can I be using those automatically generated files in order to create parsing tree. #### Ekaterina Borisova +This week I have been working on installing ANTRLv.4 library. There were many ideas on how we should be implementing our backend but in the end we came to an understanding that ANTRL library should work well for our project. I have managed to install it on my computer and try to run it on different libraries including a C++ library. I have found some grammar files that I can use for testing and from which I managed to create tokens using the library. Next week I am planning to get better understanding on how to use this library in order to create parsing tree because right now I am struggling to get it. + #### Mina Goranovic +This week I found us resources and tutorial for antlr library the parsing team will use. I downloaded all of the needed setup for the library, but it is still not working. What I need to do next week is to learn how to get the graph output of the parse tree instead of the graphical representation. +Next, I finished the course on the Qt graphics and decided with the GUI team on how we want our main window to look like. +I made us a grammar file for parsing. +For next week I need to find out how exactly we will make an interpreter from AST that we will connect to flowchart elements. + +#### John Levy +This week, I completed my learning of the Qt Graphics and Qt Designer libraries. The whole GUI Team then met to decide the general aspect of our main window, we quickly identified some challenges that we would have to work on. We decided on the main classes we will use for the display of our visualization tree. +Next week, I plan on working a lot on the Text Editor part of our UI in order to display the algorithms as in a code developement environement, and decide on the final shapes we will use for the visualization cell in order to start their implementation. diff --git a/Reports/week 3 - personal report b/Reports/week 3 - personal report new file mode 100644 index 0000000..0a2aa20 --- /dev/null +++ b/Reports/week 3 - personal report @@ -0,0 +1,4 @@ +This week wes dedicated to finalizing the AST classes so that they are ready to be used. OUr work had to be manually merged which me and Matea finished at the TD. +We now have a good basis to conitnue working on the interpreter. I communicated with the GUI team about creating test ASTs. +Writing the interpreter and the tests are the main goals for my team for the next week. +Creating the AST from the parsing tree wes handed over to Team 1 and I have their group leader the explanation of our classes. diff --git a/doc/project_design.md b/doc/project_design.md index 26d2c56..9ea41ac 100644 --- a/doc/project_design.md +++ b/doc/project_design.md @@ -16,7 +16,7 @@ Algorithm Visualizer will be a tool where user will be able to input C++ code in - How are you sub-dividing your project? Which modules are you implementing? -We will have one team working on making the parser. Basically, this team will, in the first stage of the project define initial tokens and “grammar” we will use. With help of the library, they will allow the lexical analysis of the code typed by the user (that will have restrictions we will talk later about). After making the parser, this team will work on advanced functionalities and potentially help GUI team. +We will have one team working on making the parser and the AST. Basically, this team will, in the first stage of the project define initial tokens and “grammar” we will use. With help of the library ANTLR4, they will allow the lexical analysis and parsing of the code typed by the user (that will have restrictions we will talk later about). After making the parser, this team will create from the parse tree the AST. The other team will, in the first time, be working on creating the signatures of the classes inheritance for grammar we use and writing methods that wil be used for merging backend with GUI. In the second time, this team will be wokring on the interpreter that will work with AST and allow our varCell to interpret state of variables. Last but not the least, the GUI team will firstly learn basics of QtGraphics and then create simple version of the mainwindow. In the second part of the project, GUI team will work on merging graphics with the backend. - What is the interface of each module? @@ -24,7 +24,7 @@ We will have one team working on making the parser. Basically, this team will, i - What external dependencies do you have? Why? -We will use a library during the lexing process and for now that seems to be the only external dependency we have. +We will use a library during the lexing process and parsing. The library we use is ANTLR4. ## Initial grammar we defined: @@ -43,3 +43,4 @@ We will use a library during the lexing process and for now that seems to be the +