// 2 * 5 + 4 * 3 // binop: * 2 5, binop: * 4 3, binop: binop + binop public object DoForBinOp(BinOpAST bo, object lhsCarried = null) { ObjExpr lhsc = (ObjExpr)lhsCarried; ObjExpr rhs = (ObjExpr)bo.right.Accept(this, null); if (lhsCarried != null) { if (lhsc.isSimplifiedToNum && rhs.isSimplifiedToNum) { return(BinOpTwoLiterals(lhsc, rhs, bo.op)); } else { } } else { ObjExpr lhs = (ObjExpr)bo.left.Accept(this, null); if (lhs.isSimplifiedToNum && rhs.isSimplifiedToNum) { return(BinOpTwoLiterals(lhs, rhs, bo.op)); } else { } } Console.WriteLine("(BinOpAST)" + bo.token.value); return(null); }
public object DoForBinOp(BinOpAST bo, object options = null) { PositionOutput(bo.token.indent); ConsoleEx.WriteLine("{0}(BinOpAST) {1}{2}", ConsoleColor.DarkBlue, ConsoleColor.Gray, bo.token.value); bo.left.Accept(this, options); bo.right.Accept(this, options); return(options); }
private AST NodeEntity(bool isFirstNode) { AST nodeAST = Node(isFirstNode); while (currentToken.Type == TokenType.CARET || currentToken.Type == TokenType.BCPOW) { OperatorToken binOpToken = Eat(currentToken.Type) as OperatorToken; nodeAST = new BinOpAST(nodeAST, binOpToken, Node(isFirstNode)); } return(nodeAST); }
private AST NodeFactor(bool isFirstNode) { AST nodeAST = NodeEntity(isFirstNode); while (currentToken.Type == TokenType.MUL || currentToken.Type == TokenType.DIV || currentToken.Type == TokenType.BCMUL || currentToken.Type == TokenType.BCDIV) { OperatorToken binOpToken = Eat(currentToken.Type) as OperatorToken; nodeAST = new BinOpAST(nodeAST, binOpToken, NodeEntity(isFirstNode)); } return(nodeAST); }
private AST NodeExpr(bool isFirstNode) { AST nodeFactorAST = NodeFactor(isFirstNode); while (currentToken.Type == TokenType.PLUS || currentToken.Type == TokenType.MINUS) { OperatorToken binOpToken = Eat(currentToken.Type) as OperatorToken; nodeFactorAST = new BinOpAST(nodeFactorAST, binOpToken, NodeFactor(isFirstNode)); } return(nodeFactorAST); }
private Group Visit(BinOpAST binOpAST) { object leftObject = Visit(binOpAST.Left); ILinkable left = leftObject as ILinkable; object rightObject = Visit(binOpAST.Right); ILinkable right = rightObject as ILinkable; try { switch (binOpAST.Operator.Value) { case "+": return(left.Add(right)); case "-": return(left.Subtract(right)); case "*": return(left.Multiply(right)); case "/": return(left.Divide(right)); case "^": return(left.Power(right)); case ".*": return(left.BroadcastMultiply(right)); case "./": return(left.BroadcastDivide(right)); case ".^": return(left.BroadcastPower(right)); default: throw logger.Error(new SemanticException(binOpAST.FindToken(), "Invalid operator")); } } catch (LigralException) { throw logger.Error(new SemanticException(binOpAST.FindToken(), "Calculation fault")); } catch (System.Exception e) { throw logger.Error(new SemanticException(binOpAST.FindToken(), e.Message)); } }