private static void DumpValueExpression(StringWriter s, SBExpression expression, string ident = " ") { if (expression is NegativeExpression) { NegativeExpression negativeExpression = (NegativeExpression)expression; s.WriteLine(ident + $"-----> Expression: {negativeExpression.Expression.Dump()}"); DumpValueExpression(s, negativeExpression.Expression, ident + " "); } else if (expression is BinaryExpression) { BinaryExpression binaryExpression = (BinaryExpression)expression; s.WriteLine(ident + $"-----> Operator: {binaryExpression.Operator.Dump()}"); s.WriteLine(ident + $"-----> LeftHandSide: {binaryExpression.LeftHandSide.Dump()}"); DumpValueExpression(s, binaryExpression.LeftHandSide, ident + " "); s.WriteLine(ident + $"-----> RightHandSide: {binaryExpression.RightHandSide.Dump()}"); DumpValueExpression(s, binaryExpression.RightHandSide, ident + " "); } else if (expression is MethodCallExpression) { MethodCallExpression methodCallExpression = (MethodCallExpression)expression; s.WriteLine($"{ident}-----> MethodName: {methodCallExpression.MethodName.Dump()}"); s.WriteLine($"{ident}-----> TypeName: {methodCallExpression.TypeName.Dump()}"); s.WriteLine($"{ident}-----> Arguments: {methodCallExpression.Arguments}"); } else if (expression is PropertyExpression) { PropertyExpression propertyExpression = (PropertyExpression)expression; s.WriteLine($"{ident}-----> PropertyName: {propertyExpression.PropertyName.Dump()}"); s.WriteLine($"{ident}-----> TypeName: {propertyExpression.TypeName.Dump()}"); } }
public static IEnumerable <SBExpression> GetSubExpressions(this SBExpression expression) { if (expression is NegativeExpression) { NegativeExpression negativeExpression = (NegativeExpression)expression; return(new SBExpression[] { negativeExpression.Expression }.Concat(negativeExpression.Expression.GetSubExpressions())); } else if (expression is BinaryExpression) { BinaryExpression binaryExpression = (BinaryExpression)expression; return(new SBExpression[] { binaryExpression.LeftHandSide, binaryExpression.RightHandSide } .Concat(binaryExpression.LeftHandSide.GetSubExpressions()) .Concat(binaryExpression.RightHandSide.GetSubExpressions())); } else if (expression is ArrayExpression) { ArrayExpression arrayExpression = (ArrayExpression)expression; return(new SBExpression[] { arrayExpression.LeftHand, arrayExpression.Indexer } .Concat(arrayExpression.LeftHand.GetSubExpressions()) .Concat(arrayExpression.Indexer.GetSubExpressions())); } else if (expression is MethodCallExpression) { MethodCallExpression methodCallExpression = (MethodCallExpression)expression; return(methodCallExpression.Arguments .Concat(methodCallExpression.Arguments.SelectMany(a => a.GetSubExpressions()))); } else { return(new SBExpression[0]); } }
public static string Dump(this SBExpression expression) { if (expression == null) { return("NO EXPRESSION"); } else { return($"{expression.GetType().Name}[{expression.StartToken},{expression.EndToken}]:{expression} --> {(expression.HasCompiler() ? expression.Compiler().ToString() : "NO COMPILER")}"); } }
public static bool IsVariable(this SBExpression expression, EV3Variable variable) { if (expression is IdentifierExpression) { return(variable.Name.Equals(((IdentifierExpression)expression).VariableName(), StringComparison.InvariantCultureIgnoreCase)); } else if (expression is ArrayExpression) { return(variable.Name.Equals(((ArrayExpression)expression).VariableName(), StringComparison.InvariantCultureIgnoreCase)); } return(false); }
private static void AttachCompiler(this SBExpression expression, EV3CompilerContext context) { if (expression == null) { return; } if (expression is ArrayExpression) { expression.AttachCompiler(new ArrayExpressionCompiler((ArrayExpression)expression, context)); } else if (expression is BinaryExpression) { BinaryExpression binaryExpression = (BinaryExpression)expression; if (binaryExpression.IsBooleanOperator()) { expression.AttachCompiler(new BooleanExpressionCompiler(binaryExpression, context)); } else if (binaryExpression.IsBooleanCompareOperator()) { expression.AttachCompiler(new ComparisonExpressionCompiler(binaryExpression, context)); } else { expression.AttachCompiler(new BinaryExpressionCompiler(binaryExpression, context)); } } else if (expression is IdentifierExpression) { expression.AttachCompiler(new IdentifierExpressionCompiler((IdentifierExpression)expression, context)); } else if (expression is LiteralExpression) { expression.AttachCompiler(new LiteralExpressionCompiler((LiteralExpression)expression, context)); } else if (expression is MethodCallExpression) { expression.AttachCompiler(new MethodCallExpressionCompiler((MethodCallExpression)expression, context)); } else if (expression is NegativeExpression) { expression.AttachCompiler(new NegativeExpressionCompiler((NegativeExpression)expression, context)); } else if (expression is PropertyExpression) { expression.AttachCompiler(new PropertyExpressionCompiler((PropertyExpression)expression, context)); } else { throw new Exception("Unknown expression " + expression.GetType().Name); } }
public static T Compiler <T>(this SBExpression expression) where T : class, IExpressionCompiler { return(expressionCompilers[expression] as T); }
public static IExpressionCompiler Compiler(this SBExpression expression) { return(expressionCompilers[expression]); }
public static bool HasCompiler(this SBExpression expression) { return(expressionCompilers.ContainsKey(expression)); }
private static void AttachCompiler(this SBExpression expression, IExpressionCompiler compiler) { expressionCompilers.Add(expression, compiler); }
public static bool IsNumericLiteral(this SBExpression expression) { return(expression.StartToken.TokenType == TokenType.NumericLiteral || expression.EndToken.TokenType == TokenType.NumericLiteral); }
public static bool IsLiteral(this SBExpression expression) { return(expression is LiteralExpression || (expression is NegativeExpression && ((NegativeExpression)expression).Expression.IsLiteral())); }
private EV3Variable FindVariable(SBExpression expression) { return(variables.Values.FirstOrDefault(v => expression.IsVariable(v))); }