void BytecodeVisitor::visitForNode(ForNode *node) { LOG_Visitor("visitForNode"); VariableInContextDescriptor variableDescriptor = context->getVariableDescriptor(node->var()->name()); BinaryOpNode *innerExpression = (BinaryOpNode *) node->inExpr(); if (innerExpression->kind() != tRANGE) { throw TranslationError(string("Incorrect binary operation in for-expression. Exptected: RANGE, got: ") + tokenStr(innerExpression->kind()), node->position()); } if (node->var()->type() != VT_INT) { throw TranslationError(string("Incorrect type of for-variable. Exptected: INT, got: ") + typeToName(node->var()->type()), node->position()); } innerExpression->left()->visit(this); bc()->addInsn(BC_ILOADM1); bc()->addInsn(BC_IADD); storeVariable(variableDescriptor, innerExpression); Label begin(bc()); Label end(bc()); { bc()->bind(begin); loadVariable(variableDescriptor, innerExpression); bc()->addInsn(BC_ILOAD1); bc()->addInsn(BC_IADD); storeVariable(variableDescriptor, innerExpression); //condition innerExpression->right()->visit(this); loadVariable(variableDescriptor, innerExpression); bc()->addInsn(BC_SWAP); // goto end if greater bc()->addBranch(BC_IFICMPG, end); node->body()->visit(this); bc()->addBranch(BC_JA, begin); } bc()->bind(end); }
int BinaryOpNode::getNumberValue() const { assert (hasNumberValue()); int op1, op2; if (_operand1->type == Node_Number) { op1 = (static_cast<NumberNode*>(_operand1))->value; } else { BinaryOpNode *bon = static_cast<BinaryOpNode*>(_operand1); assert(bon!=0); op1 = bon->getNumberValue(); } if (_operand2->type == Node_Number) { op2 = (static_cast<NumberNode*>(_operand2))->value; } else { BinaryOpNode *bon = static_cast<BinaryOpNode*>(_operand2); assert(bon!=0); op2 = bon->getNumberValue(); } return eval(op1,op2); }
void ASTtoByteCodeTranslator::visitForNode(ForNode* node) { Bytecode *bytecode = funStack.top()->bytecode(); uint32_t mem1; BinaryOpNode *range = (BinaryOpNode*) node->inExpr(); range->right()->visit(this); range->left()->visit(this); mem1 = bytecode->current(); bytecode->addInsn(BC_STOREIVAR); bytecode->addInt16(varMap[node->var()]); node->body()->visit(this); bytecode->addInsn(BC_LOADIVAR); bytecode->addInt16(varMap[node->var()]); bytecode->addInsn(BC_ILOAD1); bytecode->addInsn(BC_IADD); bytecode->addInsn(BC_IFICMPLE); bytecode->addInt16(mem1 - bytecode->current()); }
void ByteCodeVisitor::visitForNode(mathvm::ForNode *node) { Label expr(bytecode()); Label outLabel(bytecode()); BinaryOpNode *binaryOpNode = node->inExpr()->asBinaryOpNode(); binaryOpNode->left()->visit(this); store(node->var()); bytecode()->bind(expr); load(node->var()); binaryOpNode->right()->visit(this); branch(BC_IFICMPL, outLabel); popStack(); popStack(); node->body()->visit(this); load(node->var()); insn(BC_ILOAD1); insn(BC_IADD); store(node->var()); branch(BC_JA, expr); bytecode()->bind(outLabel); }
void Generator::visitForNode(ForNode *node) { BinaryOpNode *bin = node->inExpr()->asBinaryOpNode(); assert(type(bin->left()) == VT_INT && type(bin->right()) == VT_INT); AstVar const * const var = node->var(); uint32_t jump, repeat; eval_int(bin->right()); eval_int(bin->left()); repeat = bytecode()->current(); dup_int(); save_variable(var); bytecode()->addInsn(BC_IFICMPG); jump = bytecode()->current(); bytecode()->addInt16(0); node->body()->visit(this); bytecode()->addInsn(BC_ILOAD1); bytecode()->addInsn(BC_IADD); bytecode()->addInsn(BC_JA); bytecode()->addInt16(repeat - bytecode()->current()); bytecode()->setInt16(jump, bytecode()->current() - jump); bytecode()->addInsn(BC_POP); bytecode()->addInsn(BC_POP); }
void PrintEquelleASTVisitor::midVisit(BinaryOpNode& node) { char op = ' '; switch (node.op()) { case Add: op = '+'; break; case Subtract: op = '-'; break; case Multiply: op = '*'; break; case Divide: op = '/'; break; default: break; } std::cout << ' ' << op << ' '; }
void PrintASTVisitor::visit(BinaryOpNode& node) { char op = ' '; switch (node.op()) { case Add: op = '+'; break; case Subtract: op = '-'; break; case Multiply: op = '*'; break; case Divide: op = '/'; break; default: break; } std::cout << indent() << "BinaryOpNode: " << op << '\n'; ++indent_; }
BinaryOpNode* handleBinaryOp(BinaryOp op, ExpressionNode* left, ExpressionNode* right) { BinaryOpNode* node = new BinaryOpNode(op, left, right); node->setLocation(FileLocation(yylineno)); return node; }