private AST ValueFactor() { AST valueAST = ValueEntity(); while (currentToken.Type == TokenType.MUL || currentToken.Type == TokenType.DIV || currentToken.Type == TokenType.BCMUL || currentToken.Type == TokenType.BCDIV) { OperatorToken binOpToken = Eat(currentToken.Type) as OperatorToken; valueAST = new ValBinOpAST(valueAST, binOpToken, ValueEntity()); } return(valueAST); }
private AST ValueEntity() { AST valueAST = Value(); while (currentToken.Type == TokenType.CARET || currentToken.Type == TokenType.BCPOW) { OperatorToken binOpToken = Eat(currentToken.Type) as OperatorToken; valueAST = new ValBinOpAST(valueAST, binOpToken, Value()); } return(valueAST); }
private AST ValueExpr() { AST valueFactorAST = ValueFactor(); while (currentToken.Type == TokenType.PLUS || currentToken.Type == TokenType.MINUS) { OperatorToken binOpToken = Eat(currentToken.Type) as OperatorToken; valueFactorAST = new ValBinOpAST(valueFactorAST, binOpToken, ValueFactor()); } return(valueFactorAST); }
private object Visit(ValBinOpAST valBinOpAST) { Signal left = new Signal(Visit(valBinOpAST.Left)); Signal right = new Signal(Visit(valBinOpAST.Right)); try { switch (valBinOpAST.Operator.Value) { case "+": return((left + right).Unpack()); case "-": return((left - right).Unpack()); case "*": return((left * right).Unpack()); case "/": if (right.Unpack() is double v && v == 0) { throw logger.Error(new SemanticException(valBinOpAST.Right.FindToken(), "0 Division")); } return((left / right).Unpack()); case "^": return((left.RaiseToPower(right)).Unpack()); case ".*": return((left.BroadcastMultiply(right)).Unpack()); case "./": return((left.BroadcastDivide(right)).Unpack()); case ".^": return((left.BroadcastPower(right)).Unpack()); default: throw logger.Error(new SemanticException(valBinOpAST.FindToken(), "Invalid operator")); } } catch (LigralException) { throw logger.Error(new SemanticException(valBinOpAST.FindToken(), "Calculation fault")); } catch (System.Exception e) { throw logger.Error(new SemanticException(valBinOpAST.FindToken(), e.Message)); } }