private static IGenericExpression <T> ParseGenericExpression <T>(string line, string fullLine, Dictionary <string, object> variables) { ExpressionContext context = FleeHelper.GetExpression(variables); try { return(context.CompileGeneric <T>(line)); } catch (Exception e) { throw new ParsingException($"Unable to parse expression of {typeof(T).FullName} from '{line}'", line, e); } }
private static IDynamicExpression ParseDynamicExpression(string line, string fullLine, Dictionary <string, object> variables) { ExpressionContext context = FleeHelper.GetExpression(variables); try { return(context.CompileDynamic(line)); } catch (Exception e) { throw new ParsingException($"Unable to parse expression from '{line}'", line, e); } }
public MoveData(string[] args, Dictionary <string, object> variables, string line) : base(variables, line, args) { ExpressionContext expression = FleeHelper.GetExpression(variables); string exceptionMessage = ""; if (args.Length > 2) { throw new ParsingException("Extra agruments supplied, maximum of 2 allowed for this function.", line); } try { exceptionMessage = "Invalid expression for X coordinate!"; x = expression.CompileGeneric <double>(args[0]); exceptionMessage = "Invalid expression for Y coordinate!"; y = expression.CompileGeneric <double>(args[1]); } catch (Exception e) { throw new ParsingException(exceptionMessage, line, e); } }
public override TurtleData Compile(CancellationToken token) { token.ThrowIfCancellationRequested(); Brush brush; if (Parameters.Length == 1) { string colorData = Arg1; foreach (string key in Variables.Keys) { if (colorData.Contains(key) && colorData != "random") { colorData = colorData.Replace(key, Variables[key].ToString()); } } try { brush = (Brush) new BrushConverter().ConvertFromString(Arg1 == "random" ? RandColor() : Arg1); } catch (FormatException e) { throw new ParsingException("Invalid token for 'Color'", Line, e); } } else if (Parameters.Length == 3) { ExpressionContext c = FleeHelper.GetExpression(Variables); try { byte r = Convert.ToByte(c.CompileGeneric <double>(Arg1).Evaluate()); byte g = Convert.ToByte(c.CompileGeneric <double>(Arg2).Evaluate()); byte b = Convert.ToByte(c.CompileGeneric <double>(Arg3).Evaluate()); brush = new SolidColorBrush(new Color() { A = byte.MaxValue, R = r, G = g, B = b }); } catch (Exception e) { throw new ParsingException("Invalid value, expected " + typeof(byte).Name, Line, e); } } else if (Parameters.Length == 4) { ExpressionContext c = FleeHelper.GetExpression(Variables); try { byte a = Convert.ToByte(c.CompileGeneric <double>(Arg1).Evaluate()); byte r = Convert.ToByte(c.CompileGeneric <double>(Arg2).Evaluate()); byte g = Convert.ToByte(c.CompileGeneric <double>(Arg3).Evaluate()); byte b = Convert.ToByte(c.CompileGeneric <double>(Arg4).Evaluate()); brush = new SolidColorBrush(new Color() { A = a, R = r, G = g, B = b }); } catch (Exception e) { throw new ParsingException("Invalid value, expected " + typeof(byte).Name, Line, e); } } else { throw new ParsingException("Non-existent overload for function!", Line); } brush.Freeze(); return(new TurtleData { Brush = brush, Action = Action, }); }