Skip to content

Commit

Permalink
fix the bugs in the code and visualization
Browse files Browse the repository at this point in the history
  • Loading branch information
ytsao committed Dec 3, 2024
1 parent eedb050 commit 6c67e1e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
7 changes: 4 additions & 3 deletions abstract_interpreter/example/abstract_interpreter/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
#include "../../include/ast.hpp"


int main(){
for (int i = 0; i < 20; ++i){
int main(int argc, char* argv[]){
int id = std::stoi(argv[1]);
for (int i = id; i < id + 1; ++i){
std::string filename = (i < 10 ? "0" : "") + std::to_string(i); // Add "0" prefix if i < 10
std::ifstream f("./tests/" + filename + ".c");
if (!f.is_open()){
Expand All @@ -23,7 +24,7 @@ int main(){
std::cout << "Program: " << filename << ".c" << std::endl;
AbstractInterpreterParser AIParser;
auto ast = AIParser.parse(input);
// printAST(ast);
printAST(ast);
}

return 0;
Expand Down
4 changes: 3 additions & 1 deletion abstract_interpreter/include/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ 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, BLOCKCBODY};
enum class NodeType {VARIABLE, NUMBER, PRE_CON, BINARY_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;
Expand All @@ -43,6 +43,7 @@ std::ostream& operator<<(std::ostream& os, NodeType type) {
case NodeType::ASSIGNMENT: os << "Assignment"; break;
case NodeType::IFELSE: os << "If-Else"; break;
case NodeType::WHILELOOP: os << "While-Loop"; break;
case NodeType::SINGLESTATE: os << "Single-State"; break;
case NodeType::BLOCKCBODY: os << "BlockBody"; break;
}
return os;
Expand Down Expand Up @@ -99,6 +100,7 @@ void printAST(const ASTNode& node, int depth = 0) {
case NodeType::ASSIGNMENT: std::cout << "Assignment"; break;
case NodeType::IFELSE: std::cout << "If-Else"; break;
case NodeType::WHILELOOP: std::cout << "While-Loop"; break;
case NodeType::SINGLESTATE: std::cout << "Single-State"; break;
case NodeType::BLOCKCBODY: std::cout << "BlockBody"; break;
}
std::cout << ", Value: ";
Expand Down
49 changes: 41 additions & 8 deletions abstract_interpreter/include/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ 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 @@ -153,29 +154,52 @@ class AbstractInterpreterParser{
if (sv.size() == 1){
return std::any_cast<ASTNode>(sv[0]);
}
else{
assert(sv.size()>=3);

else if (sv.size() == 3){
ASTNode op = std::any_cast<ASTNode>(sv[1]);
ASTNode expr(op.type, op.value);
expr.children.push_back(std::any_cast<ASTNode>(sv[0]));
expr.children.push_back(std::any_cast<ASTNode>(sv[2]));
return expr;
}
else{
ASTNode expr(NodeType::BINARY_OP, std::any_cast<ASTNode>(sv[1]).value);
expr.children.push_back(std::any_cast<ASTNode>(sv[0]));
ASTNode op;
size_t i = 3;
for (i; i < sv.size(); i+=2){
op = std::any_cast<ASTNode>(sv[i]);
op.children.push_back(std::any_cast<ASTNode>(sv[i-1]));
if (i+2 < sv.size())
expr.children.push_back(op);
}
op.children.push_back(std::any_cast<ASTNode>(sv[i-1]));
expr.children.push_back(op);
return expr;
}
}

ASTNode make_term(const SV& sv){
if (sv.size() == 1){
return std::any_cast<ASTNode>(sv[0]);
}
else{
assert(sv.size()>=3);

else if (sv.size() == 3){
ASTNode term(NodeType::BINARY_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);
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);
term.children.push_back(op);
term.children.push_back(std::any_cast<ASTNode>(sv[i-1]));
}
term.children.push_back(std::any_cast<ASTNode>(sv[i-1]));
return term;
}
}

ASTNode make_factor(const SV& sv){
Expand Down Expand Up @@ -238,17 +262,26 @@ class AbstractInterpreterParser{
ASTNode make_ifelse(const SV& sv){
ASTNode ifelse_node(NodeType::IFELSE, std::string("IfElse"));
for (size_t i = 0; i < sv.size(); ++i){
ASTNode mid_node;
if (i == 0) mid_node = ASTNode(NodeType::IFELSE, std::string("Condition"));
else if (i == 1) mid_node = ASTNode(NodeType::IFELSE, std::string("If-Body"));
else if (i == 2) mid_node = ASTNode(NodeType::IFELSE, std::string("Else-Body"));
ASTNode node = std::any_cast<ASTNode>(sv[i]);
ifelse_node.children.push_back(node);
mid_node.children.push_back(node);
ifelse_node.children.push_back(mid_node);
}
return ifelse_node;
}

ASTNode make_whileloop(const SV& sv){
ASTNode whileloop_node(NodeType::WHILELOOP, std::string("WhileLoop"));
for (size_t i = 0; i < sv.size(); ++i){
ASTNode mid_node;
if (i == 0) mid_node = ASTNode(NodeType::WHILELOOP, std::string("Condition"));
else if (i == 1) mid_node = ASTNode(NodeType::WHILELOOP, std::string("While-Body"));
ASTNode node = std::any_cast<ASTNode>(sv[i]);
whileloop_node.children.push_back(node);
mid_node.children.push_back(node);
whileloop_node.children.push_back(mid_node);
}
return whileloop_node;
}
Expand Down

0 comments on commit 6c67e1e

Please sign in to comment.