Exemplo n.º 1
0
		public SliceExpression(Expression root, Token bracketToken, IList<Expression> components)
			: base(root.FirstToken)
		{
			this.Root = root;
			this.BracketToken = bracketToken;
			this.Components = components.ToArray();
		}
Exemplo n.º 2
0
		protected override void X_DrawTriangle(List<string> output, Expression screen, Expression x1, Expression y1, Expression x2, Expression y2, Expression x3, Expression y3, Expression red, Expression green, Expression blue)
		{
			output.Add("pygame.draw.polygon(");
			SerializeExpression(output, screen);
			output.Add(", (");
			SerializeExpression(output, red);
			output.Add(", ");
			SerializeExpression(output, green);
			output.Add(", ");
			SerializeExpression(output, blue);
			output.Add("), ((");
			SerializeExpression(output, x1);
			output.Add(", ");
			SerializeExpression(output, y1);
			output.Add("), (");
			SerializeExpression(output, x2);
			output.Add(", ");
			SerializeExpression(output, y2);
			output.Add("), (");
			SerializeExpression(output, x3);
			output.Add(", ");
			SerializeExpression(output, y3);
			output.Add(")))");

		}
Exemplo n.º 3
0
		private bool IsSimpleEnough(Expression exp)
		{
			if (exp is NullConstant ||
				exp is BooleanConstant ||
				exp is IntegerConstant ||
				exp is FloatConstant ||
				exp is StringConstant ||
				exp is Variable)
			{
				return true;
			}

			if (exp is DotField)
			{
				return IsSimpleEnough(((DotField)exp).Root);
			}

			if (exp is IndexExpression)
			{
				return
					IsSimpleEnough(((IndexExpression)exp).Root) &&
					IsSimpleEnough(((IndexExpression)exp).Index);
			}

			return false;
		}
Exemplo n.º 4
0
		public Negation(Token token, Expression root, PrefixType type)
			: base(token)
		{
			this.Op = token;
			this.Root = root;
			this.Type = type;
		}
Exemplo n.º 5
0
		public override Expression Resolve()
		{
			this.Condition = this.Condition.Resolve();
			this.TrueExpression = this.TrueExpression.Resolve();
			this.FalseExpression = this.FalseExpression.Resolve();
			return this;
		}
Exemplo n.º 6
0
		public ForEachLoop(Token forToken, Token iteratorVariable, Expression iterableExpression, IList<Executable> body)
			: base(forToken)
		{
			this.IteratorVariable = iteratorVariable;
			this.IterableExpression = iterableExpression;
			this.Body = body.ToArray();
		}
Exemplo n.º 7
0
		public Ternary(Expression condition, Expression trueCode, Expression falseCode)
			: base(trueCode.FirstToken)
		{
			this.TrueExpression = trueCode;
			this.FalseExpression = falseCode;
			this.Condition = condition;
		}
Exemplo n.º 8
0
		public Assignment(Expression target, Token assignmentToken, Expression value)
			: base(target.FirstToken)
		{
			this.AssignmentToken = assignmentToken;
			this.Target = target;
			this.Value = value;
		}
Exemplo n.º 9
0
		public IndexExpression(Expression root, Token bracketToken, Expression index)
			: base(root.FirstToken)
		{
			this.Root = root;
			this.BracketToken = bracketToken;
			this.Index = index;
		}
Exemplo n.º 10
0
		public BooleanCombinator(Expression left, Expression right, string type)
			: base(left.FirstToken)
		{
			this.IsAnd = type == "and";
			this.Left = left;
			this.Right = right;
		}
Exemplo n.º 11
0
		public IfStatement(Token ifToken, Expression condition, IList<Executable> trueCode)
			: base(ifToken)
		{
			this.IfToken = ifToken;
			this.Condition = condition;
			this.TrueCode = trueCode.ToArray();
			this.FalseCode = EMPTY_LIST;
		}
Exemplo n.º 12
0
		public DotField(Expression root, Token dotToken, Token fieldToken, string fieldName)
			: base(root.FirstToken)
		{
			this.Root = root;
			this.DotToken = dotToken;
			this.FieldToken = fieldToken;
			this.FieldName = fieldName;
		}
Exemplo n.º 13
0
		public ForLoop(Token forToken, ForEachLoop originalForEachLoop, IList<Executable> init, Expression condition, IList<Executable> step, IList<Executable> body)
			: base(forToken)
		{
			this.Init = init.ToArray();
			this.Condition = condition;
			this.Step = step.ToArray();
			this.Body = body.ToArray();
			this.OriginalForEachLoop = originalForEachLoop;
		}
Exemplo n.º 14
0
		protected override void X_DictionaryGetWithDefault(List<string> output, Expression dictionary, Expression key, Expression defaultValue)
		{
			SerializeExpression(output, dictionary);
			output.Add(".get(");
			SerializeExpression(output, key);
			output.Add(", ");
			SerializeExpression(output, defaultValue);
			output.Add(")");
		}
Exemplo n.º 15
0
		public IfRawComponent(Token token, Expression nullableCondition, IList<Executable> body)
			: base(token)
		{
			// I find the following 3 lines mildly humorous.
			if (token.Value == "if") this.Type = ComponentType.IF;
			else if (token.Value == "elif") this.Type = ComponentType.ELIF;
			else this.Type = ComponentType.ELSE;

			this.NullableExpression = nullableCondition;
			this.Body = body.ToArray();
		}
Exemplo n.º 16
0
		public override IList<Executable> Resolve()
		{
			if (this.Value == null)
			{
				this.Value = new NullConstant(null);
			}

			this.Value = this.Value.Resolve();

			return Listify(this);
		}
Exemplo n.º 17
0
		protected override void X_DrawEllipse(List<string> output, Expression screen, Expression left, Expression top, Expression width, Expression height, Expression red, Expression green, Expression blue)
		{
			output.Add("pygame.draw.ellipse(");
			SerializeExpression(output, screen);
			output.Add(", (");
			SerializeExpression(output, red);
			output.Add(", ");
			SerializeExpression(output, green);
			output.Add(", ");
			SerializeExpression(output, blue);
			output.Add("), pygame.Rect(");
			SerializeExpression(output, left);
			output.Add(", ");
			SerializeExpression(output, top);
			output.Add(", ");
			SerializeExpression(output, width);
			output.Add(", ");
			SerializeExpression(output, height);
			output.Add("))");
		}
Exemplo n.º 18
0
		public override IList<Executable> Resolve()
		{
			this.Value = this.Value.Resolve();
			Expression t = this.Target;
			bool targetValid = true;
			if (t is Variable)
			{
				// nothing to do
			}
			else if (t is DotField)
			{
				DotField dotField = (DotField)t;
				this.Target = dotField.Resolve();
				if (!(this.Target is DotField))
				{
					targetValid = false;
				}
			}
			else if (t is IndexExpression)
			{
				IndexExpression index = (IndexExpression)t;
				this.Target = index.Resolve();
				if (!(this.Target is IndexExpression))
				{
					targetValid = false;
				}
			}
			else
			{
				targetValid = false;
			}

			if (!targetValid)
			{
				throw new ParserException(this.FirstToken, "Invalid target for assignment.");
			}

			return Listify(this);
		}
Exemplo n.º 19
0
		protected override void X_Str(List<string> output, Expression value)
		{
			output.Add("str(");
			SerializeExpression(output, value);
			output.Add(")");
		}
Exemplo n.º 20
0
		private ForLoop ResolveWithRange(Expression[] rangeArgs)
		{
			List<Executable> init = new List<Executable>();

			Expression increment = new IntegerConstant(null, 1);
			if (rangeArgs.Length == 3)
			{
				if (rangeArgs[2] is IntegerConstant || rangeArgs[2] is Variable)
				{
					increment = rangeArgs[2];
				}
				string variableName = "crayon_conversion_step_" + VARIABLE_ALLOCATION_COUNTER++;
				init.Add(new Assignment(new Variable(null, variableName), new Token("=", null, 0, 0, TokenType.OTHER), rangeArgs[2]));
				increment = new Variable(null, variableName);
			}

			Expression start = new IntegerConstant(null, 0);
			// if 2 or 3 are present, then an explicit start is given
			if (rangeArgs.Length > 1)
			{
				start = rangeArgs[0];
			}

			Expression end = null;
			if (rangeArgs.Length == 1)
			{
				end = rangeArgs[0];
			}
			else
			{
				end = rangeArgs[1];
			}

			if (end is IntegerConstant || end is Variable)
			{
				// this is fine
			}
			else
			{
				string variableName = "crayon_conversion_length_" + VARIABLE_ALLOCATION_COUNTER++;
				init.Add(new Assignment(new Variable(null, variableName), new Token("=", null, 0, 0, TokenType.OTHER), end));
				end = new Variable(null, variableName);
			}

			init.Add(new Assignment(new Variable(this.IteratorVariable, this.IteratorVariable.Value), new Token("=", null, 0, 0, TokenType.OTHER), start));
			Expression condition = new BinaryOpChain(
				new Expression[] { 
					new Variable(this.IteratorVariable, this.IteratorVariable.Value), 
					end },
				new Token[] { new Token("<", null, 0, 0, TokenType.OTHER) });

			List<Executable> step = new List<Executable>()
			{
				new Assignment(
					new Variable(this.IteratorVariable, this.IteratorVariable.Value),
					new Token("+=", null, 0, 0, TokenType.OTHER),
					increment)
			};

			return new ForLoop(this.FirstToken, this, init, condition, step, this.Body);
		}
Exemplo n.º 21
0
		protected override void X_DictionaryKeys(List<string> output, Expression dict)
		{
			SerializeExpression(output, dict);
			output.Add(".keys()");
		}
Exemplo n.º 22
0
		public override IList<Executable> Resolve()
		{
			this.Expression = this.Expression.Resolve();
			return Listify(this);
		}
Exemplo n.º 23
0
		public ExpressionAsExecutable(Expression expression)
			: base(expression.FirstToken)
		{
			this.Expression = expression;
		}
Exemplo n.º 24
0
		protected override void X_ReadTextResource(List<string> output, Expression path)
		{
			output.Add("_CRYTHON_get_text_resource(");
			SerializeExpression(output, path);
			output.Add(")");
		}
Exemplo n.º 25
0
		protected override void X_ScreenFill(List<string> output, Expression screen, Expression red, Expression green, Expression blue)
		{
			SerializeExpression(output, screen);
			output.Add(".fill((");
			SerializeExpression(output, red);
			output.Add(", ");
			SerializeExpression(output, green);
			output.Add(", ");
			SerializeExpression(output, blue);
			output.Add("))");
		}
Exemplo n.º 26
0
		protected override void X_StringUpper(List<string> output, Expression str)
		{
			SerializeExpression(output, str);
			output.Add(".upper()");
		}
Exemplo n.º 27
0
		protected override void X_StringTrim(List<string> output, Expression str)
		{
			SerializeExpression(output, str);
			output.Add(".strip()");
		}
Exemplo n.º 28
0
		protected override void X_DictionarySize(List<string> output, Expression dict)
		{
			throw new NotImplementedException();
		}
Exemplo n.º 29
0
		protected override void X_StringSplit(List<string> output, Expression str, Expression sep)
		{
			SerializeExpression(output, str);
			output.Add(".split(");
			SerializeExpression(output, sep);
			output.Add(")"); 
		}
Exemplo n.º 30
0
		protected override void X_StringLength(List<string> output, Expression str)
		{
			output.Add("len(");
			SerializeExpression(output, str);
			output.Add(")");
		}