// generates code to implement node's action static void implement_node(ast_node node){ if (node != NULL){ if (node->node_type == ROOT){ process_root(node); } // generate code for assignment operator else if (node->node_type == OP_ASSIGN){ process_assign(node); } // generate code for negate operator else if (node->node_type == OP_NEG) { process_negate(node); } // generate code for +, -, *, /, %, =, !=, <, <=, >, >= else if (node->node_type > 0 && node->node_type <= 16 && node->node_type != 14 && node->node_type != 15){ process_math(node); } else if (node->node_type == OP_INC){ process_inc(node, "1"); } else if (node->node_type == OP_DEC){ process_inc(node, "-1"); } else if (node->node_type == IF_STMT){ process_if(node); } else if (node->node_type == IF_ELSE_STMT){ process_ifelse(node); } else if (node->node_type == CMPD){ process_cmpd(node); } else if (node->node_type == WHILE_STMT){ process_while(node); } else if (node->node_type == DO_WHILE_STMT){ process_dowhile(node); } else if (node->node_type == OP_AND){ process_and(node); } else if (node->node_type == OP_OR){ process_or(node); } else if (node->node_type == FOR_STRT || node->node_type == FOR_COND || node->node_type == FOR_UPDT){ process_for_header(node); } else if (node->node_type == FOR_STMT){ process_for(node); } else if (node->node_type == READ_STMT){ process_read(node); } else if (node->node_type == PRINT_STMT){ process_print(node); } else if (node->node_type == RETURN_STMT){ process_return(node); } else if (node->node_type == FUNCDEC){ process_function(node); } else if (node->node_type == PARAMS){ process_params(node); } else if (node->node_type == INT_TYPE || node->node_type == DOUBLE_TYPE){ process_vardec(node); } else if (node->node_type == CALL){ process_call(node); } else if (node->node_type == IDENT){ process_id(node); } else if (node->node_type == ARRAY){ process_array(node); } } }
// tokenise the input file (or stdin if filename is empty), outputing the appropriate Python code void process(const std::string &filename, const Options &opt, const std::set<std::string> &restrict_vars) { std::ifstream file_stream; if (!filename.empty()) { file_stream.open(filename.c_str()); if (!file_stream) { std::cerr << exec_name << ": cannot open " << filename << '\n'; exit(1); } } std::istream &infile = (filename.empty() ? std::cin : file_stream); std::string line; std::vector<Token> tokens; // map from demangled (original) variable name to mangled name for variables on lhs of an assignment statement; // not used for --assign or --test Varmap assigned_vars; // set of "seen" variable names (e.g. "a", "a.b", "a.b["); only used for --assign Varset variable_hierarchy; // variables that appear in expressions (other than as arguments to function calls); only used for --test // (key = mangled id, value = demangled id) Varmap test_vars; // with the input to --test is raw python code (vs just being a list of boolean expressions, one per line) bool test_is_raw_python = false; std::string pending_line; int pending_line_num = 0; for (int line_num = 1;std::getline(infile, line);++line_num) { size_t len = line.length(); if (len != 0 && line[len - 1] == '\\') { if (pending_line.empty()) { pending_line_num = line_num; } pending_line += line.substr(0, len - 1); pending_line += ' '; continue; } int actual_line_num = line_num; if (!pending_line.empty()) { line = pending_line + line; pending_line.clear(); actual_line_num = pending_line_num; } tokens.resize(0); tokenise(line, filename, line_num, opt, tokens, test_is_raw_python); // debug_tokens(tokens); if (opt.assign) { process_assign(tokens, line, filename, actual_line_num, variable_hierarchy); } else if (opt.test) { process_test(tokens, line, filename, actual_line_num, &test_is_raw_python, test_vars); } else { process_command(tokens, assigned_vars, opt, restrict_vars); } } if (opt.command) { print_assigned_variables(assigned_vars); } if (opt.test && !test_is_raw_python) { validate_test_variables(test_vars); } }