Skip to content

Commit

Permalink
update the terminologies
Browse files Browse the repository at this point in the history
  • Loading branch information
ytsao committed Dec 4, 2024
1 parent 6c67e1e commit 6a2f197
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
20 changes: 9 additions & 11 deletions abstract_interpreter/include/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
#include <variant>
#include <math.h>

enum class BinOp {ADD, SUB, MUL, DIV, EXP};
enum class BinOp {ADD, SUB, MUL, DIV};
std::ostream& operator<<(std::ostream& os, BinOp op) {
switch (op) {
case BinOp::ADD: os << "+"; break;
case BinOp::SUB: os << "-"; break;
case BinOp::MUL: os << "*"; break;
case BinOp::DIV: os << "/"; break;
case BinOp::EXP: os << "^"; break;
}
return os;
}
Expand All @@ -31,13 +30,13 @@ std::ostream& operator<<(std::ostream& os, LogicOp lop){
return os;
}

enum class NodeType {VARIABLE, NUMBER, PRE_CON, BINARY_OP, LOGIC_OP, DECLARATION, ASSIGNMENT, IFELSE, WHILELOOP, SINGLESTATE, BLOCKCBODY};
enum class NodeType {VARIABLE, INTEGER, PRE_CON, ARITHM_OP, LOGIC_OP, DECLARATION, ASSIGNMENT, IFELSE, WHILELOOP, SINGLESTATE, BLOCKCBODY};
std::ostream& operator<<(std::ostream& os, NodeType type) {
switch (type) {
case NodeType::VARIABLE: os << "Variable"; break;
case NodeType::NUMBER: os << "Number"; break;
case NodeType::INTEGER: os << "Integer"; break;
case NodeType::PRE_CON: os << "Pre-conditions"; break;
case NodeType::BINARY_OP: os << "Binary Operation"; break;
case NodeType::ARITHM_OP: os << "Arithmetic Operation"; break;
case NodeType::LOGIC_OP: os << "Logic Operation"; break;
case NodeType::DECLARATION: os << "Declaration"; break;
case NodeType::ASSIGNMENT: os << "Assignment"; break;
Expand All @@ -57,11 +56,11 @@ struct ASTNode {
VType value;
ASTNodes children;

ASTNode(): type(NodeType::NUMBER), value(0) {}
ASTNode(): type(NodeType::INTEGER), value(0) {}
ASTNode(const std::string& name): type(NodeType::VARIABLE), value(name){}
ASTNode(const int num): type(NodeType::NUMBER), value(num) {}
ASTNode(const int num): type(NodeType::INTEGER), value(num) {}
ASTNode(BinOp bop, ASTNode left, ASTNode right)
: type(NodeType::BINARY_OP), value(bop){
: type(NodeType::ARITHM_OP), value(bop){
children.push_back(left);
children.push_back(right);
}
Expand All @@ -87,14 +86,13 @@ void printVariant(const std::variant<std::string, int, BinOp, LogicOp>& value) {

// Recursive AST printing
void printAST(const ASTNode& node, int depth = 0) {
// if (!node) return;
std::string indent(depth * 2, ' ');
std::cout << indent << "NodeType: ";
switch (node.type) {
case NodeType::VARIABLE: std::cout << "Variable"; break;
case NodeType::NUMBER: std::cout << "Number"; break;
case NodeType::INTEGER: std::cout << "Integer"; break;
case NodeType::PRE_CON: std::cout << "Pre-condition"; break;
case NodeType::BINARY_OP: std::cout << "Binary Operator"; break;
case NodeType::ARITHM_OP: std::cout << "Arithmetic Operator"; break;
case NodeType::LOGIC_OP: std::cout << "Logic Operator"; break;
case NodeType::DECLARATION: std::cout << "Declaration"; break;
case NodeType::ASSIGNMENT: std::cout << "Assignment"; break;
Expand Down
17 changes: 8 additions & 9 deletions abstract_interpreter/include/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ class AbstractInterpreterParser{

// // setup actions
parser["Program"] = [this](const SV& sv){return make_program(sv);};
// parser["Statements"] = [this](const SV& sv){return make_program(sv);}; //TODO: bad_any_cast<>
parser["Integer"] = [](const SV& sv){return ASTNode(sv.token_to_number<int>());};
parser["Identifier"] = [](const SV& sv){return ASTNode(sv.token_to_string());};
parser["SeqOp"] = [this](const SV& sv){return make_seq_op(sv);};
Expand Down Expand Up @@ -123,15 +122,15 @@ class AbstractInterpreterParser{
}

ASTNode make_seq_op(const SV& sv){
ASTNode op_node(NodeType::BINARY_OP);
ASTNode op_node(NodeType::ARITHM_OP);
std::string op = sv.token_to_string();
if (op == "+") op_node.value = BinOp::ADD;
else if (op == "-") op_node.value = BinOp::SUB;
return op_node;
}

ASTNode make_pre_op(const SV& sv){
ASTNode op_node(NodeType::BINARY_OP);
ASTNode op_node(NodeType::ARITHM_OP);
std::string op = sv.token_to_string();
if (op == "*") op_node.value = BinOp::MUL;
else if (op == "/") op_node.value = BinOp::DIV;
Expand Down Expand Up @@ -162,7 +161,7 @@ class AbstractInterpreterParser{
return expr;
}
else{
ASTNode expr(NodeType::BINARY_OP, std::any_cast<ASTNode>(sv[1]).value);
ASTNode expr(NodeType::ARITHM_OP, std::any_cast<ASTNode>(sv[1]).value);
expr.children.push_back(std::any_cast<ASTNode>(sv[0]));
ASTNode op;
size_t i = 3;
Expand All @@ -183,17 +182,17 @@ class AbstractInterpreterParser{
return std::any_cast<ASTNode>(sv[0]);
}
else if (sv.size() == 3){
ASTNode term(NodeType::BINARY_OP, std::any_cast<ASTNode>(sv[1]).value);
ASTNode term(NodeType::ARITHM_OP, std::any_cast<ASTNode>(sv[1]).value);
term.children.push_back(std::any_cast<ASTNode>(sv[0]));
term.children.push_back(std::any_cast<ASTNode>(sv[2]));
return term;
}
else{
ASTNode term(NodeType::BINARY_OP, std::any_cast<ASTNode>(sv[1]).value);
ASTNode term(NodeType::ARITHM_OP, std::any_cast<ASTNode>(sv[1]).value);
term.children.push_back(std::any_cast<ASTNode>(sv[0]));
size_t i = 3;
for (i; i < sv.size(); i+=2){
ASTNode op(NodeType::BINARY_OP, std::any_cast<ASTNode>(sv[i]).value);
ASTNode op(NodeType::ARITHM_OP, std::any_cast<ASTNode>(sv[i]).value);
term.children.push_back(op);
term.children.push_back(std::any_cast<ASTNode>(sv[i-1]));
}
Expand All @@ -206,7 +205,7 @@ class AbstractInterpreterParser{
if (sv.choice() == 0){
// for the case: x = -y;
// we're going to transform it into x = 0 - y;
ASTNode sign(NodeType::BINARY_OP, std::string("-"));
ASTNode sign(NodeType::ARITHM_OP, std::string("-"));
sign.children.push_back(ASTNode(0));
sign.children.push_back(std::any_cast<ASTNode>(sv[0]));
return sign;
Expand All @@ -229,7 +228,7 @@ class AbstractInterpreterParser{
ASTNode increment_node(NodeType::ASSIGNMENT, std::string("="));

ASTNode var = std::any_cast<ASTNode>(sv[0]);
ASTNode plus_op(NodeType::BINARY_OP, std::string("+"));
ASTNode plus_op(NodeType::ARITHM_OP, std::string("+"));
plus_op.children.push_back(var);
plus_op.children.push_back(ASTNode(1));

Expand Down

0 comments on commit 6a2f197

Please sign in to comment.