// don't call base protected override IExpression CompileExpressionNode(IExpressionNodeFactory factory, ParseTreeNode astNode) { switch (astNode.Term.Name) { case "Number": var num = TypeNormalizer.EnsureType <double>(astNode.Token.Value); return(factory.CreateLiteral(num)); case "String": var str = TypeNormalizer.EnsureType <string>(astNode.Token.Value); return(factory.CreateLiteral(str)); case "Boolean": var bln = TypeNormalizer.EnsureType <bool>(astNode.Token.Value); return(factory.CreateLiteral(bln)); case "Identifier": var variable = astNode.Token.Value; if (variable != null) { return(factory.CreateVariable(variable.ToString())); } break; case "Symbol": var str_1 = TypeNormalizer.EnsureType <string>(astNode.Token.Value); return(factory.CreateLiteral(str_1)); case "UserSectionStmt": return(factory.CreateFunction(astNode.ChildNodes[2].Token.Text, null)); case "GlobalSectionStmt": return(factory.CreateLiteral(this.SinglePage.EvaluatePropertyPath(astNode.ChildNodes[2].Token.Text))); case "ParameterSectionStmt": return(CompileExpressionNode(factory, astNode.ChildNodes[2])); case "FieldsSectionStmt": //return CompileExpressionNode (factory,astNode.ChildNodes[2]); IExpression l = CompileExpressionNode(factory, astNode.ChildNodes[2]); return(factory.CreateFunction(astNode.ChildNodes[0].Token.Text, l)); case "ParExpr": return(CompileExpressionNode(factory, astNode.ChildNodes[0])); case "BinExpr": if (astNode.ChildNodes.Count == 3 && astNode.ChildNodes[1].Term is SymbolTerminal) { IExpression left = CompileExpressionNode(factory, astNode.ChildNodes[0]); IExpression right = CompileExpressionNode(factory, astNode.ChildNodes[2]); return(factory.CreateBinaryOperator(astNode.ChildNodes[1].Term.Name, left, right)); } if (astNode.ChildNodes.Count == 2 && astNode.ChildNodes[0].Term is SymbolTerminal) { IExpression arg = CompileExpressionNode(factory, astNode.ChildNodes[1]); return(factory.CreateUnaryOperator(astNode.ChildNodes[0].Term.Name, arg)); } if (astNode.ChildNodes.Count == 2 && astNode.ChildNodes[1].Term is SymbolTerminal) { IExpression arg = CompileExpressionNode(factory, astNode.ChildNodes[0]); return(factory.CreateUnaryOperator(astNode.ChildNodes[1].Term.Name, arg)); } break; case "QualifiedName": var parts = new List <string>(); if (astNode.ChildNodes.Count == 2) { return(new QualifiedName(new[] { astNode.ChildNodes[1].Token.ValueString })); } /* * //Condition ought to be impossible * if (astNode.ChildNodes.Count != 3 ) * throw new Exception("Malformed QualifiedName - should have 3 child nodes"); */ if (astNode.ChildNodes.Count == 1) { return(CompileExpressionNode(factory, astNode.ChildNodes[0])); } SimpleExpressionLanguageCompiler.ExtractQualifiedName(astNode, parts); return(new QualifiedName(parts.ToArray())); case "FunctionExpression": IExpression [] args = null; string functionName = (astNode.ChildNodes[0].ChildNodes[0].Token.ValueString); if (astNode.ChildNodes.Count == 1) { args = new IExpression[astNode.ChildNodes[0].ChildNodes[0].ChildNodes.Count]; } else { args = new IExpression[astNode.ChildNodes[1].ChildNodes.Count]; for (int i = 0; i < astNode.ChildNodes[1].ChildNodes.Count; i++) { args[i] = CompileExpressionNode(factory, astNode.ChildNodes[1].ChildNodes[i]); } } return(factory.CreateFunction(functionName, args)); case "IfThen": IExpression condition = CompileExpressionNode(factory, astNode.ChildNodes[1].ChildNodes[0]); IExpression trueExpr = CompileExpressionNode(factory, astNode.ChildNodes[3]); // IExpression falseExpr = CompileExpressionNode(factory, astNode.ChildNodes[3]); IExpression falseExpr = null; if (astNode.ChildNodes.Count == 6) { falseExpr = CompileExpressionNode(factory, astNode.ChildNodes[5]); } var func = new IfThen(); if (falseExpr != null) { func.AcceptArguments(condition, trueExpr, falseExpr); } else { func.AcceptArguments(condition, trueExpr); } return(func); /* * case "ArrayExpression": * IExpression context = CompileExpressionNode(factory, astNode.ChildNodes[0]); * IExpression index = CompileExpressionNode(factory, astNode.ChildNodes[1]); * var indexer = new ItemAtIndex(); * indexer.AcceptArguments(context, index); * return indexer; * */ } return(null); }
protected virtual IExpression CompileExpressionNode(IExpressionNodeFactory factory, ParseTreeNode astNode) { switch (astNode.Term.Name) { case "Number": var num = TypeNormalizer.EnsureType <double>(astNode.Token.Value); return(factory.CreateLiteral(num)); case "String": var str = TypeNormalizer.EnsureType <string>(astNode.Token.Value); return(factory.CreateLiteral(str)); case "Boolean": var bln = TypeNormalizer.EnsureType <bool>(astNode.Token.Value); return(factory.CreateLiteral(bln)); case "Identifier": var variable = astNode.Token.Value; if (variable != null) { return(factory.CreateVariable(variable.ToString())); } break; case "Expr": if (astNode.ChildNodes.Count == 3 && astNode.ChildNodes[1].Term is SymbolTerminal) { IExpression left = CompileExpressionNode(factory, astNode.ChildNodes[0]); IExpression right = CompileExpressionNode(factory, astNode.ChildNodes[2]); return(factory.CreateBinaryOperator(astNode.ChildNodes[1].Term.Name, left, right)); } if (astNode.ChildNodes.Count == 2 && astNode.ChildNodes[0].Term is SymbolTerminal) { IExpression arg = CompileExpressionNode(factory, astNode.ChildNodes[1]); return(factory.CreateUnaryOperator(astNode.ChildNodes[0].Term.Name, arg)); } if (astNode.ChildNodes.Count == 2 && astNode.ChildNodes[1].Term is SymbolTerminal) { IExpression arg = CompileExpressionNode(factory, astNode.ChildNodes[0]); return(factory.CreateUnaryOperator(astNode.ChildNodes[1].Term.Name, arg)); } break; case "QualifiedName": var parts = new List <string>(); if (astNode.ChildNodes.Count == 2) { return(new QualifiedName(new[] { astNode.ChildNodes[1].Token.ValueString })); } //Condition ought to be impossible if (astNode.ChildNodes.Count != 3) { throw new Exception("Malformed QualifiedName - should have 3 child nodes"); } ExtractQualifiedName(astNode, parts); return(new QualifiedName(parts.ToArray())); case "FunctionExpression": string functionName = (astNode.ChildNodes[0].Token.ValueString); var args = new IExpression[astNode.ChildNodes[1].ChildNodes.Count]; for (int i = 0; i < astNode.ChildNodes[1].ChildNodes.Count; i++) { args[i] = CompileExpressionNode(factory, astNode.ChildNodes[1].ChildNodes[i]); } return(factory.CreateFunction(functionName, args)); case "IfThen": IExpression condition = CompileExpressionNode(factory, astNode.ChildNodes[1]); IExpression trueExpr = CompileExpressionNode(factory, astNode.ChildNodes[3]); IExpression falseExpr = null; if (astNode.ChildNodes.Count == 6) { falseExpr = CompileExpressionNode(factory, astNode.ChildNodes[5]); } var func = new IfThen(); if (falseExpr != null) { func.AcceptArguments(condition, trueExpr, falseExpr); } else { func.AcceptArguments(condition, trueExpr); } return(func); case "ArrayExpression": IExpression context = CompileExpressionNode(factory, astNode.ChildNodes[0]); IExpression index = CompileExpressionNode(factory, astNode.ChildNodes[1]); var indexer = new ItemAtIndex(); indexer.AcceptArguments(context, index); return(indexer); } return(null); }
protected virtual IExpression CompileExpressionNode(IExpressionNodeFactory factory, ParseNode astNode) { switch (astNode.Token.Type) { case TokenType.Atom: if (astNode.Nodes[0].Token.Type == TokenType.BROPEN) { return(CompileExpressionNode(factory, astNode.Nodes[1])); } return(CompileExpressionNode(factory, astNode.Nodes[0])); case TokenType.NUMBER: case TokenType.INTEGER: var num = TypeNormalizer.EnsureType <double>(astNode.Token.Text); return(factory.CreateLiteral(num)); case TokenType.STRING: var str = TypeNormalizer.EnsureType <string>(astNode.Token.Text.Replace("\"", String.Empty)); return(factory.CreateLiteral(str)); case TokenType.TRUE: case TokenType.FALSE: var bln = TypeNormalizer.EnsureType <bool>(astNode.Token.Text); return(factory.CreateLiteral(bln)); case TokenType.NULL: return(null); case TokenType.BooleanExpr: case TokenType.CompareExpr: case TokenType.AddExpr: case TokenType.MultExpr: case TokenType.PowerExpr: return(CompileBinaryOperator(factory, astNode)); case TokenType.QualifiedName: return(CompileQualifiedName(factory, null, Select(astNode.Nodes, TokenType.Identifier), 0)); case TokenType.Conditional: var pieces = Select(astNode.Nodes, TokenType.BooleanExpr); IExpression condition = CompileExpressionNode(factory, pieces[0]); IExpression trueExpr = CompileExpressionNode(factory, pieces[1]); IExpression falseExpr = (pieces.Count == 3) ? CompileExpressionNode(factory, pieces[2]) : null; var func = new IfThen(); if (falseExpr != null) { func.AcceptArguments(condition, trueExpr, falseExpr); } else { func.AcceptArguments(condition, trueExpr); } return(func); case TokenType.UnaryExpr: string token = astNode.Nodes[0].Token.Text; IExpression left = CompileBinaryOperator(factory, astNode.Nodes[1]); return(factory.CreateUnaryOperator(token, left)); } return(null); }