static bool RenderBranch(WasmRenderContext& c, AstBranch& branch) { Op op = branch.op(); MOZ_ASSERT(op == Op::BrIf || op == Op::Br); if (op == Op::BrIf) { if (!RenderExpr(c, branch.cond())) return false; } if (branch.maybeValue()) { if (!RenderExpr(c, *(branch.maybeValue()))) return false; } if (!RenderIndent(c)) return false; MAP_AST_EXPR(c, branch); if (op == Op::BrIf ? !c.buffer.append("br_if ") : !c.buffer.append("br ")) return false; return RenderRef(c, branch.target()); }
static bool RenderBranch(WasmRenderContext& c, AstBranch& branch) { Expr expr = branch.expr(); MOZ_ASSERT(expr == Expr::BrIf || expr == Expr::Br); if (expr == Expr::BrIf ? !c.buffer.append("(br_if ") : !c.buffer.append("(br ")) return false; if (!RenderRef(c, branch.target())) return false; if (expr == Expr::BrIf) { if (!c.buffer.append(" ")) return false; if (!RenderExpr(c, branch.cond())) return false; } if (branch.maybeValue()) { if (!c.buffer.append(" ")) return false; if (!RenderExpr(c, *(branch.maybeValue()))) return false; } return c.buffer.append(")"); }
Expression* Evaluator::eval(Expression* e) { Expression* res_exp = NULL; switch(e->get_type()) { case AST_UNOP: { AstUnOp* b = static_cast<AstUnOp*>(e); res_exp = eval_unop(b); break; } case AST_READ: { AstRead* r = static_cast<AstRead*>(e); string input; getline(cin, input); if(r->read_integer()) { return AstInt::make(string_to_int(input)); } return AstString::make(input); break; } case AST_INT: { res_exp = e; break; } case AST_STRING: { res_exp = e; break; } case AST_IDENTIFIER: { assert(e->get_type() == AST_IDENTIFIER); AstIdentifier* i = static_cast<AstIdentifier*>(e); res_exp = sym_tab.find(i); if(res_exp == NULL){ report_error(e, "Identifier ID is not bound in current context"); } break; } case AST_LET: { sym_tab.push(); assert(e->get_type() == AST_LET); AstLet* l = static_cast<AstLet*>(e); Expression* e1 = l->get_val(); Expression* eval_e = eval(e1); sym_tab.add(l->get_id(), eval_e); Expression* e2 = l->get_body(); Expression* eval_e2 = eval(e2); res_exp = eval_e2; sym_tab.pop(); break; } case AST_BRANCH: { AstBranch* b = static_cast<AstBranch*>(e); Expression* eval_e = eval(b->get_pred()); if(eval_e->get_type() != AST_INT){ report_error(e, "Predicate in conditional must be an integer"); } AstInt* i = static_cast<AstInt*>(eval_e); Expression* eval_e2; if(i->get_int() == 0){ eval_e2 = eval(b->get_else_exp()); } else{ eval_e2 = eval(b->get_then_exp()); } res_exp = eval_e2; break; } case AST_BINOP: { AstBinOp* b = static_cast<AstBinOp*>(e); res_exp = eval_binop(b); break; } case AST_NIL: { res_exp = e; break; } case AST_LAMBDA: { res_exp = e; break; } case AST_EXPRESSION_LIST: { AstExpressionList* l = static_cast<AstExpressionList*>(e); res_exp = eval_expression_list(l); break; } //ADD CASES FOR ALL EXPRESSIONS!! default: assert(false); } return res_exp; }