public SliceExpression(Expression root, Token bracketToken, IList<Expression> components) : base(root.FirstToken) { this.Root = root; this.BracketToken = bracketToken; this.Components = components.ToArray(); }
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(")))"); }
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; }
public Negation(Token token, Expression root, PrefixType type) : base(token) { this.Op = token; this.Root = root; this.Type = type; }
public override Expression Resolve() { this.Condition = this.Condition.Resolve(); this.TrueExpression = this.TrueExpression.Resolve(); this.FalseExpression = this.FalseExpression.Resolve(); return this; }
public ForEachLoop(Token forToken, Token iteratorVariable, Expression iterableExpression, IList<Executable> body) : base(forToken) { this.IteratorVariable = iteratorVariable; this.IterableExpression = iterableExpression; this.Body = body.ToArray(); }
public Ternary(Expression condition, Expression trueCode, Expression falseCode) : base(trueCode.FirstToken) { this.TrueExpression = trueCode; this.FalseExpression = falseCode; this.Condition = condition; }
public Assignment(Expression target, Token assignmentToken, Expression value) : base(target.FirstToken) { this.AssignmentToken = assignmentToken; this.Target = target; this.Value = value; }
public IndexExpression(Expression root, Token bracketToken, Expression index) : base(root.FirstToken) { this.Root = root; this.BracketToken = bracketToken; this.Index = index; }
public BooleanCombinator(Expression left, Expression right, string type) : base(left.FirstToken) { this.IsAnd = type == "and"; this.Left = left; this.Right = right; }
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; }
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; }
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; }
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(")"); }
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(); }
public override IList<Executable> Resolve() { if (this.Value == null) { this.Value = new NullConstant(null); } this.Value = this.Value.Resolve(); return Listify(this); }
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("))"); }
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); }
protected override void X_Str(List<string> output, Expression value) { output.Add("str("); SerializeExpression(output, value); output.Add(")"); }
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); }
protected override void X_DictionaryKeys(List<string> output, Expression dict) { SerializeExpression(output, dict); output.Add(".keys()"); }
public override IList<Executable> Resolve() { this.Expression = this.Expression.Resolve(); return Listify(this); }
public ExpressionAsExecutable(Expression expression) : base(expression.FirstToken) { this.Expression = expression; }
protected override void X_ReadTextResource(List<string> output, Expression path) { output.Add("_CRYTHON_get_text_resource("); SerializeExpression(output, path); output.Add(")"); }
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("))"); }
protected override void X_StringUpper(List<string> output, Expression str) { SerializeExpression(output, str); output.Add(".upper()"); }
protected override void X_StringTrim(List<string> output, Expression str) { SerializeExpression(output, str); output.Add(".strip()"); }
protected override void X_DictionarySize(List<string> output, Expression dict) { throw new NotImplementedException(); }
protected override void X_StringSplit(List<string> output, Expression str, Expression sep) { SerializeExpression(output, str); output.Add(".split("); SerializeExpression(output, sep); output.Add(")"); }
protected override void X_StringLength(List<string> output, Expression str) { output.Add("len("); SerializeExpression(output, str); output.Add(")"); }