Exemplo n.º 1
0
        public static BinaryOpChain Build(IList <Expression> expressions, IList <Token> ops, Executable owner)
        {
            // TODO: don't pop from the front, you fool. that's silly slow.
            List <Expression> mutableList = new List <Expression>(expressions);
            List <Token>      mutableOps  = new List <Token>(ops);
            Expression        left        = mutableList[0];
            Expression        right       = mutableList[1];

            // Pop! Pop! \o/
            mutableList.RemoveAt(0);
            mutableList.RemoveAt(0);

            Token op = mutableOps[0];

            mutableOps.RemoveAt(0);

            BinaryOpChain boc = new BinaryOpChain(left, op, right, owner);

            while (mutableList.Count > 0)
            {
                right = mutableList[0];
                mutableList.RemoveAt(0);
                op = mutableOps[0];
                mutableOps.RemoveAt(0);
                boc = new BinaryOpChain(boc, op, right, owner);
            }

            return(boc);
        }
Exemplo n.º 2
0
		public static BinaryOpChain Build(IList<Expression> expressions, IList<Token> ops, Executable owner)
		{
			// TODO: don't pop from the front, you fool. that's silly slow. 
			List<Expression> mutableList = new List<Expression>(expressions);
			List<Token> mutableOps = new List<Token>(ops);
			Expression left = mutableList[0];
			Expression right = mutableList[1];

			// Pop! Pop! \o/
			mutableList.RemoveAt(0);
			mutableList.RemoveAt(0);

			Token op = mutableOps[0];
			mutableOps.RemoveAt(0);

			BinaryOpChain boc = new BinaryOpChain(left, op, right, owner);
			while (mutableList.Count > 0)
			{
				right = mutableList[0];
				mutableList.RemoveAt(0);
				op = mutableOps[0];
				mutableOps.RemoveAt(0);
				boc = new BinaryOpChain(boc, op, right, owner);
			}

			return boc;
		}
Exemplo n.º 3
0
        public static BinaryOpChain Build(IList <Expression> expressions, IList <Token> ops, TopLevelConstruct owner)
        {
            int        expressionIndex = 0;
            int        opIndex         = 0;
            Expression left            = expressions[expressionIndex++];
            Expression right           = expressions[expressionIndex++];

            Token op = ops[opIndex++];

            BinaryOpChain boc = new BinaryOpChain(left, op, right, owner);

            while (expressionIndex < expressions.Count)
            {
                right = expressions[expressionIndex++];
                op    = ops[opIndex++];
                boc   = new BinaryOpChain(boc, op, right, owner);
            }

            return(boc);
        }
Exemplo n.º 4
0
		private void CompileBinaryOpChain(Parser parser, ByteBuffer buffer, BinaryOpChain opChain, bool outputUsed)
		{
			if (!outputUsed) throw new ParserException(opChain.FirstToken, "This expression isn't valid here.");

			this.CompileExpressionList(parser, buffer, new Expression[] { opChain.Left, opChain.Right }, true);

			Token opToken = opChain.Op;
			switch (opToken.Value)
			{
				case "+": buffer.Add(opToken, OpCode.BINARY_OP, (int)BinaryOps.ADDITION); break;
				case "<": buffer.Add(opToken, OpCode.BINARY_OP, (int)BinaryOps.LESS_THAN); break;
				case "==": buffer.Add(opToken, OpCode.BINARY_OP, (int)BinaryOps.EQUALS); break;
				case "<=": buffer.Add(opToken, OpCode.BINARY_OP, (int)BinaryOps.LESS_THAN_OR_EQUAL); break;
				case ">": buffer.Add(opToken, OpCode.BINARY_OP, (int)BinaryOps.GREATER_THAN); break;
				case ">=": buffer.Add(opToken, OpCode.BINARY_OP, (int)BinaryOps.GREATER_THAN_OR_EQUAL); break;
				case "-": buffer.Add(opToken, OpCode.BINARY_OP, (int)BinaryOps.SUBTRACTION); break;
				case "*": buffer.Add(opToken, OpCode.BINARY_OP, (int)BinaryOps.MULTIPLICATION); break;
				case "/": buffer.Add(opToken, OpCode.BINARY_OP, (int)BinaryOps.DIVISION); break;
				case "%": buffer.Add(opToken, OpCode.BINARY_OP, (int)BinaryOps.MODULO); break;
				case "!=": buffer.Add(opToken, OpCode.BINARY_OP, (int)BinaryOps.NOT_EQUALS); break;
				case "**": buffer.Add(opToken, OpCode.BINARY_OP, (int)BinaryOps.EXPONENT); break;
				case "|": buffer.Add(opToken, OpCode.BINARY_OP, (int)BinaryOps.BITWISE_OR); break;
				case "&": buffer.Add(opToken, OpCode.BINARY_OP, (int)BinaryOps.BITWISE_AND); break;
				case "^": buffer.Add(opToken, OpCode.BINARY_OP, (int)BinaryOps.BITWISE_XOR); break;
				case "<<": buffer.Add(opToken, OpCode.BINARY_OP, (int)BinaryOps.BIT_SHIFT_LEFT); break;
				case ">>": buffer.Add(opToken, OpCode.BINARY_OP, (int)BinaryOps.BIT_SHIFT_RIGHT); break;
				default: throw new NotImplementedException("Binary op: " + opChain.Op.Value);
			}

			if (!outputUsed)
			{
				buffer.Add(null, OpCode.POP);
			}
		}
Exemplo n.º 5
0
		private static Expression ParseExponents(TokenStream tokens, Executable owner)
		{
			Expression expr = ParseIncrement(tokens, owner);
			string next = tokens.PeekValue();
			if (next == "**")
			{
				Token op = tokens.Pop();
				Expression right = ParseNegate(tokens, owner);
				expr = new BinaryOpChain(expr, op, right, owner);
			}
			return expr;
		}
Exemplo n.º 6
0
		private static Expression ParseMultiplication(TokenStream tokens, Executable owner)
		{
			Expression expr = ParseNegate(tokens, owner);
			string next = tokens.PeekValue();
			while (MULTIPLICATION_OPS.Contains(next))
			{
				Token op = tokens.Pop();
				Expression right = ParseNegate(tokens, owner);
				expr = new BinaryOpChain(expr, op, right, owner);
				next = tokens.PeekValue();
			}
			return expr;
		}
Exemplo n.º 7
0
		private void TranslateDefaultBinaryOp(List<string> output, BinaryOpChain binOp)
		{
			output.Add("(");
			this.TranslateExpression(output, binOp.Left);
			output.Add(this.Shorten(" "));
			output.Add(binOp.Op.Value);
			output.Add(this.Shorten(" "));
			this.TranslateExpression(output, binOp.Right);
			output.Add(")");
		}
Exemplo n.º 8
0
		protected void TranslateBinaryOpChain(List<string> output, BinaryOpChain binaryOp)
		{
			// TODO: something about the parenthesis epidemic
			switch (binaryOp.Op.Value)
			{
				case "+":
				case "-":
				case "*":
				case "%":
				case "<<":
				case ">>":
				case "<":
				case ">":
				case "==":
				case "!=":
				case "<=":
				case ">=":
				case "&":
				case "|":
				case "^":
					this.TranslateDefaultBinaryOp(output, binaryOp);
					break;

				case "**":
					throw new ParserException(binaryOp.Op, "Use a framework function instead to indicate whether you want a float or int output.");

				case "/":
					throw new ParserException(binaryOp.Op, "Due to varying behavior of / on different languages, please use a framework function instead.");

				default:
					throw new ParserException(binaryOp.Op, "How did this happen?");
			}
		}