All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.codetaco.math.operator.OpAssignmentMultiply Maven / Gradle / Ivy

package com.codetaco.math.operator;

import com.codetaco.math.*;
import com.codetaco.math.token.TokVariable;
import com.codetaco.math.token.TokVariableWithValue;

import java.text.ParseException;

public class OpAssignmentMultiply extends Operator {
    public OpAssignmentMultiply() {
        super();
    }

    public OpAssignmentMultiply(final EquPart opTok) {
        super(opTok);
    }

    @Override
    protected int precedence() {
        return 990;
    }

    @Override
    public boolean preceeds(final Operation rightOp) {
        /*
         * This allows for assigning the same value to multiple variables. 
* a := b := 1 */ if (rightOp instanceof OpAssignmentMultiply) { return false; } return super.preceeds(rightOp); } @Override public void resolve(final ValueStack values) throws Exception { if (values.size() < 2) { throw new Exception("missing operands for " + toString()); } try { final Object op2 = values.popExactly(); final Object op1 = values.popExactly(); if (op2 instanceof TokVariable) { throw new Exception("invalid assignment value: " + op2.toString()); } if (op1 instanceof TokVariableWithValue) { TokVariableWithValue varWithValue1 = (TokVariableWithValue) op1; if (varWithValue1.getCurrentValue() instanceof Long) { varWithValue1.setCurrentValue(((Long) varWithValue1.getCurrentValue()) * ((Long) op2)); } else if (varWithValue1.getCurrentValue() instanceof Double) { varWithValue1.setCurrentValue(((Double) varWithValue1.getCurrentValue()) * ((Double) op2)); } else { throw new Exception("invalid types"); } /* * Assign the value on the right side of the assignment to the * variable on the left. */ Equ.getInstance().getSupport().assignVariable( varWithValue1.getVariable().getValue().toString(), varWithValue1); /* * leave the result (value of the assignment) on the stack. */ values.push(varWithValue1); return; } throw new Exception("invalid assignment target: " + op1.toString()); } catch (final ParseException e) { e.fillInStackTrace(); throw new Exception(toString() + "; " + e.getMessage(), e); } } @Override public String toString() { return "op(*=)"; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy