/// <summary> /// Initializes a new instance of <see cref="Case"/> AST node. /// </summary> /// <param name="expression">The target expression of the case node.</param> /// <param name="caseList">Series of cases for the case node.</param> public Case(Node expression, ExpressionList caseList) { this.expression = expression; this.caseList = caseList; NormalizeCases(); }
/// <summary> /// Initializes a new instance of <see cref="UserDefFunction"/> AST node. /// </summary> /// <param name="name">The name of the user defined function.</param> /// <param name="parameters">The parameters for the user defined function.</param> /// <param name="codeblock">The codeblock for the user defined function.</param> /// <param name="code">The string representation of the user defined function.</param> public UserDefFunction(Identifier name, ExpressionList parameters, Node codeblock, string code) { this.name = name; this.parameters = parameters; this.codeblock = codeblock; this.code = code; }
/// <summary> /// Initializes a new instance of <see cref="Dependency"/> AST node. /// </summary> /// <param name="variable">The name of the dependency definition.</param> /// <param name="functionBody">The body of the dependency definition.</param> /// <param name="codeText">The string representation of the dependency defintion.</param> /// <param name="variables">The variables associated with the dependency definition.</param> public Dependency(Identifier variable, Node functionBody, string codeText, Variables variables) { this.variable = variable; this.functionBody = functionBody; this.codeText = codeText; this.variables = variables; }
/// <summary> /// Initializes a new instance of <see cref="DyadicFunction"/> AST node. /// </summary> /// <param name="token">The <see cref="Token"/> to use for the dyadic function.</param> /// <param name="leftExpression">The left hand argument of the dyadic function.</param> /// <param name="rightExpression">The right hand argument of the dyadic function.</param> public DyadicFunction(Token token, Node leftExpression, Node rightExpression) { this.token = token; this.leftExpression = leftExpression; this.rightExpression = rightExpression; MethodChooser.ConvertToDyadicToken(this.token); }
public static void CreateDotFile(Node input, string outputFileName) { text.Append("digraph APlus {\n"); ToDot("", input); text.Append("\n}"); using (StreamWriter writer = new StreamWriter(new FileStream(outputFileName, FileMode.Create))) { writer.Write(text); } }
public static AST.Node ASCIIString(string input, FunctionInformation functionInfo) { Antlr.Runtime.Lexer lexer = new Grammar.Ascii.AplusLexer(new ANTLRStringStream(input ?? "")); Grammar.AplusParser parser = new Grammar.AplusParser(new CommonTokenStream(lexer)); parser.FunctionInfo = functionInfo; bool parseOk = parser.Parse(); AST.Node tree = parser.Tree; return(tree); }
public DLR.Expression <System.Func <Runtime.Aplus, AType> > ParseToLambda(string code) { AplusScope scope = new AplusScope(null, "__top__", this.aplus, DLR.Expression.Parameter(typeof(Aplus), "__aplusRuntime__"), DLR.Expression.Parameter(typeof(DYN.IDynamicMetaObjectProvider), "__module__") ); FunctionInformation funcionInfo = new FunctionInformation(this.aplus.CurrentContext); if (this.aplus.Context != null) { funcionInfo.LoadInfo((this.aplus.Context as Scope).Storage as ScopeStorage); } DLR.Expression codebody = null; AST.Node tree = Compiler.Parse.String(code, this.aplus.LexerMode, funcionInfo); if (tree == null) { codebody = DLR.Expression.Constant(null); } else { codebody = DLR.Expression.Block( new DLR.ParameterExpression[] { scope.ModuleExpression }, DLR.Expression.Assign( scope.ModuleExpression, DLR.Expression.PropertyOrField(scope.RuntimeExpression, "Context") ), tree.Generate(scope) ); } DLR.Expression <System.Func <Aplus, AType> > method = DLR.Expression.Lambda <Func <Aplus, AType> >( codebody, scope.RuntimeExpression ); return(method); }
/// <summary> /// Builds a <see cref="While"/> node. /// </summary> /// <param name="expression">The expression for the <see cref="While"/> node.</param> /// <param name="codeBlock">The codeblock for the node.</param> /// <returns>Returns a <see cref="While"/> AST node.</returns> public static While While(Node expression, Node codeBlock) { return new While(expression, codeBlock); }
/// <summary> /// Builds a <see cref="MonadicDo"/> node. /// </summary> /// <param name="codeblock">The codeblock for the monadic do.</param> /// <returns>Returns a <see cref="MonadicDo"/> node.</returns> public static MonadicDo MonadicDo(Node codeblock) { return new MonadicDo(codeblock); }
/// <summary> /// Build a AST node representing an assignment. /// </summary> /// <param name="target">The target of the assignment.</param> /// <param name="expression">The value of the assignment.</param> /// <returns><see cref="Assign">Assignment</see> AST node.</returns> public static Assign Assign(Node target, Node expression) { return new Assign(target, expression); }
/// <summary> /// Initializes a new instance of <see cref="DyadicDo"/> AST node. /// </summary> /// <param name="expression">The start expression of the dyadic do.</param> /// <param name="codeblock">The codeblock of the dyadic do.</param> public DyadicDo(Node expression, Node codeblock) { this.expression = expression; this.codeblock = codeblock; }
/// <summary> /// Normalizes the cases. Sets the default case based on the number of cases. /// </summary> private void NormalizeCases() { if (this.caseList.Length % 2 == 1) { // odd number of cases, the last one is the default case this.defaultCase = this.caseList.Items.Last.Value; this.caseList.Items.RemoveLast(); } else { // there is no default case, add a Null (by definition) as default this.defaultCase = Node.NullConstant(); } }
public static UserDefOperatorInvoke UserDefOperatorInvoke(Identifier name, Node condition) { UserDefOperatorInvoke userOp = new UserDefOperatorInvoke(name); userOp.Condition = condition; return userOp; }
/// <summary> /// Test if the <see cref="Node"/> is a <see cref="MonadicFunction"/> node with the specified token type. /// </summary> /// <param name="node">The <see cref="Node"/> to check.</param> /// <param name="tokenType">The <see cref="Tokens">token type</see> to check for.</param> /// <remarks> /// True if the <see cref="Node"/> is a /// <see cref="MonadicFunction"/> with the given <see cref="Tokens">token type</see>. /// Otherwise false. /// </remarks> internal static bool TestMonadicToken(Node node, Tokens tokenType) { return (node is MonadicFunction) && (((MonadicFunction)node).Token.Type == tokenType); }
/// <summary> /// Add the <see cref="Node"/> as the last item to the <see cref="ExpressionList"/>. /// </summary> /// <param name="item">The <see cref="Node"/> to add.</param> public void AddLast(Node item) { this.nodeList.AddLast(item); }
private static string ToDot(string parent, Node node) { string result; switch (node.NodeType) { case NodeTypes.Assign: result = ToDot(parent, (Assign)node); break; case NodeTypes.BuiltInFunction: result = ToDot(parent, (BuiltInFunction)node); break; case NodeTypes.BuiltInOperator: result = ToDot(parent, (BuiltInOperator)node); break; case NodeTypes.Case: result = ToDot(parent, (Case)node); break; case NodeTypes.Constant: result = ToDot(parent, (Constant)node); break; case NodeTypes.ConstantList: result = ToDot(parent, (ConstantList)node); break; case NodeTypes.Dependency: result = ToDot(parent, (Dependency)node); break; case NodeTypes.DyadicDo: result = ToDot(parent, (DyadicDo)node); break; case NodeTypes.DyadicFunction: result = ToDot(parent, (DyadicFunction)node); break; case NodeTypes.EachOperator: result = ToDot(parent, (EachOperator)node); break; case NodeTypes.ExpressionList: result = ToDot(parent, (ExpressionList)node); break; case NodeTypes.Identifier: result = ToDot(parent, (Identifier)node); break; case NodeTypes.If: result = ToDot(parent, (If)node); break; case NodeTypes.Indexing: result = ToDot(parent, (Indexing)node); break; case NodeTypes.MonadicDo: result = ToDot(parent, (MonadicDo)node); break; case NodeTypes.MonadicFunction: result = ToDot(parent, (MonadicFunction)node); break; case NodeTypes.RankOperator: result = ToDot(parent, (RankOperator)node); break; case NodeTypes.Strand: result = ToDot(parent, (Strand)node); break; case NodeTypes.SystemCommand: result = ToDot(parent, (SystemCommand)node); break; case NodeTypes.Token: result = ToDot(parent, (Token)node); break; case NodeTypes.UserDefFunction: result = ToDot(parent, (UserDefFunction)node); break; case NodeTypes.UserDefOperator: result = ToDot(parent, (UserDefOperator)node); break; case NodeTypes.UserDefInvoke: result = ToDot(parent, (UserDefInvoke)node); break; case NodeTypes.While: result = ToDot(parent, (While)node); break; default: throw new Exception("Type"); } return result; }
/// <summary> /// Initializes a new instance of <see cref="While"/> AST Node. /// </summary> /// <param name="expression">The expression for the node.</param> /// <param name="codeBlock">The codeblock of the node.</param> public While(Node expression, Node codeBlock) { this.expression = expression; this.codeBlock = codeBlock; }
/// <summary> /// Builds a <see cref="Case"/> node. /// </summary> /// <param name="expression">The target expression of the <see cref="Case"/> node.</param> /// <param name="caseList">Series of cases for the <see cref="Case"/> node.</param> /// <returns>Returns the <see cref="Case"/> node.</returns> public static Case Case(Node expression, ExpressionList caseList) { return new Case(expression, caseList); }
/// <summary> /// Build a <see cref="DyadicFunction"/> using the given arguments. /// </summary> /// <param name="token">The <see cref="Token"/> to use for the dyadic function.</param> /// <param name="leftExpression">The left hand argument of the dyadic function.</param> /// <param name="rightExpression">The right hand argument of the dyadic function.</param> /// <returns>Returns a <see cref="DyadicFunction"/> representing a built-in function.</returns> public static Node DyadicFunction(Token token, Node leftExpression, Node rightExpression) { return new DyadicFunction(token, leftExpression, rightExpression); }
/// <summary> /// Builds a <see cref="Node"/> representing an indexing expression. /// </summary> /// <param name="item">The target of the indexing.</param> /// <param name="indexExpression">The indexer expressions.</param> /// <returns>Returns a <see cref="Indexing"/> AST node.</returns> public static Indexing Indexing(Node item, ExpressionList indexExpression) { return new Indexing(item, indexExpression); }
/// <summary> /// Builds a <see cref="DyadicDo"/> node. /// </summary> /// <param name="expression">The start expression for the dyadic do.</param> /// <param name="codeblock">The codeblock for the dyadic do.</param> /// <returns>Returns the <see cref="DyadicDo"/> node.</returns> public static DyadicDo DyadicDo(Node expression, Node codeblock) { return new DyadicDo(expression, codeblock); }
public static RankOperator RankOperator(Node function, Node condition) { return new RankOperator(function, condition); }
public override DLR.Expression Generate(AplusScope scope) { Aplus runtime = scope.GetRuntime(); LinkedList<DLR.Expression> result = new LinkedList<DLR.Expression>(); DLR.ParameterExpression scalar = DLR.Expression.Parameter(typeof(AType), "_ScalarResult_"); DLR.ParameterExpression counter = DLR.Expression.Parameter(typeof(int), "COUNTER"); DLR.ParameterExpression exitValue = DLR.Expression.Parameter(typeof(int), "EXITVALUE"); DLR.LabelTarget exitLabel = DLR.Expression.Label(typeof(AType), "EXIT"); DLR.ParameterExpression returnValue = DLR.Expression.Parameter(typeof(AType), "RETURN"); bool incrementMode = true; if (this.expression is MonadicFunction && ((MonadicFunction)this.expression).Token.Type == Tokens.EXPONENTIAL) { // Change the counter's 'way' incrementMode = false; // Remove the Exponential function this.expression = ((MonadicFunction)this.expression).Expression; } if (this.expression is Assign && ((Assign)this.expression).Target is Identifier) { scope.IsAssignment = true; result.AddFirst(this.expression.Generate(scope)); scope.IsAssignment = false; // Remove the assignment and leave the identifier only this.expression = ((Assign)this.expression).Target; } // Save the previous return target DLR.LabelTarget oldTarget = scope.ReturnTarget; // Add an return target for the scope // this will allow the usage of the Result monadic function scope.ReturnTarget = exitLabel; if (this.expression is Identifier) { // Found a case like: VAR do { ... } Identifier variable = (Identifier)this.expression; // Generate a .Dynamic.Get DLR tree (used multiple times so this saves time) DLR.Expression variableGenerated = variable.Generate(scope); DLR.Expression variableAsFloat = DLR.Expression.Property(scalar, "asFloat"); result.AddLast(DLR.Expression.Block( new DLR.ParameterExpression[] { counter, exitValue, returnValue, scalar }, // Test if the constant is an integer DomainTest(variableGenerated, scalar), DLR.Expression.Assign(exitValue, (incrementMode ? // EXITVALUE = round(variable.asFloat) (DLR.Expression)FloatRounding(variableAsFloat) : // EXITVALUE = 0 (DLR.Expression)DLR.Expression.Constant(0, typeof(int)) ) ), DLR.Expression.Assign( counter, (incrementMode ? (DLR.Expression)DLR.Expression.Constant(0, typeof(int)) : (DLR.Expression)DLR.Expression.Decrement(FloatRounding(variableAsFloat)) ) ), // Start the loop DLR.Expression.Loop( DLR.Expression.Block( AST.Assign.GenerateIdentifierAssign( scope, variable, DLR.Expression.Call(typeof(LocalAInteger).GetMethod("Create"), counter), false, false ), DLR.Expression.IfThen( (incrementMode ? // Check if variable >= EXITVALUE is true DLR.Expression.GreaterThanOrEqual( counter, //DLR.Expression.Property(variableGenerated, "asInteger"), exitValue ) : // Check if EXITVALUE(0) > variable is true DLR.Expression.GreaterThan( exitValue, //DLR.Expression.Property(variableGenerated, "asInteger") counter ) ), // The expression was true, exit from the loop with the last value of the expression block DLR.Expression.Break(exitLabel, returnValue) ), // Otherwise run the inner codeblock DLR.Expression.Assign(returnValue, this.codeblock.Generate(scope)), // Update counter (incrementMode ? // ++counter DLR.Expression.PreIncrementAssign(counter) : // --counter DLR.Expression.PreDecrementAssign(counter) ) ), exitLabel ) )); } else { // Simple Iteration DLR.ParameterExpression temp = DLR.Expression.Parameter(typeof(AType), "TMP"); result.AddLast(DLR.Expression.Block( new DLR.ParameterExpression[] { temp, counter, exitValue, returnValue, scalar }, // Save the iteration count into a temporaly variable DLR.Expression.Assign(temp, this.expression.Generate(scope)), // Test if the constant is an integer DomainTest(temp, scalar), // MAXVALUE = temp.asInteger DLR.Expression.Assign(exitValue, FloatRounding(DLR.Expression.Property(scalar, "asFloat"))), // counter = 0 DLR.Expression.Assign(counter, DLR.Expression.Constant(0, typeof(int))), // Start the loop DLR.Expression.Loop( DLR.Expression.Block( // Check if counter >= MAXVALUE is true DLR.Expression.IfThen( DLR.Expression.GreaterThanOrEqual(counter, exitValue), // The expression was true, exit from the loop with the last calculated value DLR.Expression.Break(exitLabel, returnValue) ), // Otherwise run the inner codeblock, save the block's result DLR.Expression.Assign(returnValue, this.codeblock.Generate(scope)), // Increment the counter DLR.Expression.PreIncrementAssign(counter) ), exitLabel ) )); } // Restore the return target scope.ReturnTarget = oldTarget; return DLR.Expression.Block(result); }
public void AddFalseCase(Node falseCase) { this.falseCase = falseCase; }
/// <summary> /// Initializes a new instance of <see cref="Indexing"/> AST node. /// </summary> /// <param name="item">The target of the indexing.</param> /// <param name="indexExpression">The indexer expressions.</param> public Indexing(Node item, ExpressionList indexExpression) { this.item = item; this.indexExpression = indexExpression; }
/// <summary> /// Initializes a new instance of <see cref="UserDefOperator"/> AST node. /// </summary> /// <param name="name">The name of the user defined operator.</param> /// <param name="function">The function parameter of the user defined operator.</param> /// <param name="codeblock">The code block of the user defined operator.</param> /// <param name="codeText">The string representation of the user defined operator.</param> public UserDefOperator(Identifier name, Identifier function, Node codeblock, string codeText) { this.name = name; this.function = function; this.codeblock = codeblock; this.codeText = codeText; }
public RankOperator(Node function, Node condition) : base(function) { this.condition = condition; }
/// <summary> /// Builds a <b>monadic</b> user defined operator with a <b>monadic</b> derived function. /// </summary> /// <remarks> /// Builds a representation of a <see cref="UserDefOperator"/> of the following structure: /// <example> /// (f OP) b: { ... } /// </example> /// </remarks> /// <param name="name">The name of the user defined operator.</param> /// <param name="function">The name of the function in the user defined operator.</param> /// <param name="argument">The name of the monadic argument of the user defined operator.</param> /// <param name="codeblock">The codeblock of the user defined operator.</param> /// <param name="codeText">The string representation of the user defined operator.</param> /// <returns>Returns a monadic case of <see cref="UserDefOperator"/> AST node.</returns> public static UserDefOperator MonadicUserDefOperator( Identifier name, Identifier function, Identifier argument, Node codeblock, string codeText = "") { UserDefOperator result = new UserDefOperator(name, function, codeblock, codeText) { RightArgument = argument }; return result; }
/// <summary> /// Initializes a new instance of <see cref="Assign"/> AST node. /// </summary> /// <param name="target">The target of the assignment.</param> /// <param name="expression">The value of the assignment.</param> public Assign(Node target, Node expression) { this.target = target; this.expression = expression; }
/// <summary> /// Builds a <b>dyadic</b> user defined operator with a <b>monadic</b> derived function. /// </summary> /// <remarks> /// Builds a representation of a <see cref="UserDefOperator"/> of the following structure: /// <example> /// (f OP h) b: { ... } /// </example> /// </remarks> /// <param name="name">The name of the user defined operator.</param> /// <param name="function">The name of the function in the user defined operator.</param> /// <param name="condition">The name of the condition in the user defined operator.</param> /// <param name="argument">The name of the monadic argument of the user defined operator.</param> /// <param name="codeblock">The codeblock of the user defined operator.</param> /// <param name="codeText">The string representation of the user defined operator.</param> /// <returns>Returns a dyadic case of <see cref="UserDefOperator"/> AST node.</returns> public static UserDefOperator DyadicUserDefOperator( Identifier name, Identifier function, Identifier condition, Identifier argument, Node codeblock, string codeText = "") { UserDefOperator result = MonadicUserDefOperator(name, function, argument, codeblock, codeText); result.Condition = condition; return result; }
/// <summary> /// Initializes a new instance of <see cref="MonadicDo"/> AST node. /// </summary> /// <param name="codeblock">The codeblock for the monadic do.</param> public MonadicDo(Node codeblock) { this.codeblock = codeblock; }
/// <summary> /// Builds a <b>dyadic</b> user defined operator with a <b>dyadic</b> derived function. /// </summary> /// <remarks> /// Builds a representation of a <see cref="UserDefOperator"/> of the following structure: /// <example> /// a (f OP h) b: { ... } /// </example> /// </remarks> /// <param name="name">The name of the user defined operator.</param> /// <param name="function">The name of the function in the user defined operator.</param> /// <param name="condition">The name of the condition in the user defined operator.</param> /// <param name="leftArgument">The name of left argument of the user defined operator.</param> /// <param name="rightArgument">The name of the right argument of the user defined operator.</param> /// <param name="codeblock">The codeblock of the user defined operator.</param> /// <param name="codeText">The string representation of the user defined operator.</param> /// <returns>Returns a dyadic case of <see cref="UserDefOperator"/> AST node.</returns> public static UserDefOperator DyadicUserDefOperator( Identifier name, Identifier function, Identifier condition, Identifier leftArgument, Identifier rightArgument, Node codeblock, string codeText = "") { UserDefOperator result = DyadicUserDefOperator(name, function, condition, rightArgument, codeblock, codeText); result.LeftArgument = leftArgument; return result; }