protected virtual void EmitIdentifierListNode(ref ProtoCore.AST.AssociativeAST.IdentifierListNode identList) { Validity.Assert(null != identList); ProtoCore.AST.AssociativeAST.AssociativeNode left = identList.LeftNode; DFSTraverse(ref left); ProtoCore.AST.AssociativeAST.AssociativeNode right = identList.RightNode; DFSTraverse(ref right); }
protected virtual void EmitBinaryNode(ref ProtoCore.AST.AssociativeAST.BinaryExpressionNode binaryExprNode) { Validity.Assert(null != binaryExprNode); ProtoCore.AST.AssociativeAST.AssociativeNode leftNode = binaryExprNode.LeftNode; DFSTraverse(ref leftNode); ProtoCore.AST.AssociativeAST.AssociativeNode rightNode = binaryExprNode.RightNode; DFSTraverse(ref rightNode); }
private void EmitExprListNode(ref ProtoCore.AST.AssociativeAST.ExprListNode exprListNode) { for (int i = 0; i < exprListNode.list.Count; i++) { ProtoCore.AST.AssociativeAST.AssociativeNode node = exprListNode.list[i]; DFSTraverse(ref node); exprListNode.list[i] = node; } }
public static ProtoCore.AST.AssociativeAST.ExprListNode BuildArrayExprList(ProtoCore.AST.AssociativeAST.AssociativeNode arrayNode) { ProtoCore.AST.AssociativeAST.ExprListNode exprlist = new ProtoCore.AST.AssociativeAST.ExprListNode(); while (arrayNode is ProtoCore.AST.AssociativeAST.ArrayNode) { ProtoCore.AST.AssociativeAST.ArrayNode array = arrayNode as ProtoCore.AST.AssociativeAST.ArrayNode; exprlist.list.Add(array.Expr); arrayNode = array.Type; } return(exprlist); }
protected void EmitIdentifierNode(ref ProtoCore.AST.AssociativeAST.AssociativeNode identNode) { ProtoCore.AST.AssociativeAST.IdentifierNode iNode = ChildTree.LeftNode as ProtoCore.AST.AssociativeAST.IdentifierNode; Validity.Assert(iNode != null); if ((identNode as ProtoCore.AST.AssociativeAST.IdentifierNode).Value == iNode.Value) { //ProtoCore.AST.AssociativeAST.ArrayNode temp = (identNode as ProtoCore.AST.AssociativeAST.IdentifierNode).ArrayDimensions; identNode = ChildTree; //if ((identNode as ProtoCore.AST.AssociativeAST.BinaryExpressionNode).LeftNode is ProtoCore.AST.AssociativeAST.IdentifierNode) // ((identNode as ProtoCore.AST.AssociativeAST.BinaryExpressionNode).LeftNode // as AST.AssociativeAST.IdentifierNode).ArrayDimensions = temp; } }
//======================= /// <summary> /// Depth first traversal of an AST node /// </summary> /// <param name="node"></param> /// <summary> /// These functions emit the DesignScript code on the destination stream /// </summary> /// <param name="identNode"></param> #region ASTNODE_CODE_EMITTERS private void EmitArrayIndexerNode(ref ProtoCore.AST.AssociativeAST.ArrayIndexerNode arrIdxNode) { if (arrIdxNode.Array is ProtoCore.AST.AssociativeAST.IdentifierNode) { EmitIdentifierNode(ref arrIdxNode.Array); } else if (arrIdxNode.Array is ProtoCore.AST.AssociativeAST.BinaryExpressionNode) { ProtoCore.AST.AssociativeAST.AssociativeNode ben = (arrIdxNode.Array as ProtoCore.AST.AssociativeAST.BinaryExpressionNode).LeftNode; EmitIdentifierNode(ref ben); ProtoCore.AST.AssociativeAST.AssociativeNode rightNode = (arrIdxNode.Array as ProtoCore.AST.AssociativeAST.BinaryExpressionNode).RightNode; DFSTraverse(ref rightNode); } }
public static bool IsReturnExpressionNode(ProtoCore.AST.AssociativeAST.AssociativeNode node) { ProtoCore.AST.AssociativeAST.BinaryExpressionNode binaryNode = node as ProtoCore.AST.AssociativeAST.BinaryExpressionNode; if (null == binaryNode) { return(false); } ProtoCore.AST.AssociativeAST.IdentifierNode retNode = binaryNode.LeftNode as ProtoCore.AST.AssociativeAST.IdentifierNode; if (null == retNode) { return(false); } return(retNode.Value == ProtoCore.DSDefinitions.Keyword.Return); }
protected virtual void EmitFunctionDotCallNode(ref ProtoCore.AST.AssociativeAST.FunctionDotCallNode dotCall) { Validity.Assert(null != dotCall); ProtoCore.AST.AssociativeAST.AssociativeNode identNode = dotCall.DotCall.FormalArguments[0]; if (identNode is ProtoCore.AST.AssociativeAST.BinaryExpressionNode) { ProtoCore.AST.AssociativeAST.AssociativeNode idNode = (identNode as ProtoCore.AST.AssociativeAST.BinaryExpressionNode).LeftNode; EmitIdentifierNode(ref idNode); (identNode as ProtoCore.AST.AssociativeAST.BinaryExpressionNode).LeftNode = idNode; } else { EmitIdentifierNode(ref identNode); } dotCall.DotCall.FormalArguments[0] = identNode; ProtoCore.AST.AssociativeAST.FunctionCallNode funcDotCall = dotCall.FunctionCall; EmitFunctionCallNode(ref funcDotCall); }
protected virtual void EmitFunctionCallNode(ref ProtoCore.AST.AssociativeAST.FunctionCallNode funcCallNode) { Validity.Assert(null != funcCallNode); Validity.Assert(funcCallNode.Function is ProtoCore.AST.AssociativeAST.IdentifierNode); string functionName = (funcCallNode.Function as ProtoCore.AST.AssociativeAST.IdentifierNode).Value; Validity.Assert(!string.IsNullOrEmpty(functionName)); for (int n = 0; n < funcCallNode.FormalArguments.Count; ++n) { ProtoCore.AST.AssociativeAST.AssociativeNode argNode = funcCallNode.FormalArguments[n]; DFSTraverse(ref argNode); funcCallNode.FormalArguments[n] = argNode; if (n + 1 < funcCallNode.FormalArguments.Count) { } } }
public static string GenerateIdentListNameString(ProtoCore.AST.AssociativeAST.AssociativeNode node) { ProtoCore.AST.AssociativeAST.IdentifierListNode iNode; ProtoCore.AST.AssociativeAST.AssociativeNode leftNode = node; List <string> stringList = new List <string>(); while (leftNode is ProtoCore.AST.AssociativeAST.IdentifierListNode) { iNode = leftNode as ProtoCore.AST.AssociativeAST.IdentifierListNode; leftNode = iNode.LeftNode; if (iNode.RightNode is ProtoCore.AST.AssociativeAST.IdentifierNode) { stringList.Add((iNode.RightNode as ProtoCore.AST.AssociativeAST.IdentifierNode).Value); } else if (iNode.RightNode is ProtoCore.AST.AssociativeAST.FunctionCallNode) { ProtoCore.AST.AssociativeAST.FunctionCallNode fCall = iNode.RightNode as ProtoCore.AST.AssociativeAST.FunctionCallNode; stringList.Add(fCall.Function.Name); } else { return(string.Empty); } } stringList.Add(leftNode.Name); stringList.Reverse(); string retString = string.Empty; foreach (string s in stringList) { retString += s; retString += '.'; } // Remove the last dot retString = retString.Remove(retString.Length - 1); return(retString); }
public static ProtoCore.AST.AssociativeAST.AssociativeNode Clone(ProtoCore.AST.AssociativeAST.AssociativeNode rhsNode) { if (rhsNode is ProtoCore.AST.AssociativeAST.IdentifierNode) { return(new ProtoCore.AST.AssociativeAST.IdentifierNode(rhsNode as ProtoCore.AST.AssociativeAST.IdentifierNode)); } else if (rhsNode is ProtoCore.AST.AssociativeAST.IdentifierListNode) { return(new ProtoCore.AST.AssociativeAST.IdentifierListNode(rhsNode as ProtoCore.AST.AssociativeAST.IdentifierListNode)); } else if (rhsNode is ProtoCore.AST.AssociativeAST.BinaryExpressionNode) { return(new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(rhsNode as ProtoCore.AST.AssociativeAST.BinaryExpressionNode)); } else if (rhsNode is ProtoCore.AST.AssociativeAST.FunctionCallNode) { return(new ProtoCore.AST.AssociativeAST.FunctionCallNode(rhsNode as ProtoCore.AST.AssociativeAST.FunctionCallNode)); } else if (rhsNode is ProtoCore.AST.AssociativeAST.CodeBlockNode) { return(new ProtoCore.AST.AssociativeAST.CodeBlockNode(rhsNode as ProtoCore.AST.AssociativeAST.CodeBlockNode)); } else if (rhsNode is ProtoCore.AST.AssociativeAST.ArrayNode) { return(new ProtoCore.AST.AssociativeAST.ArrayNode(rhsNode as ProtoCore.AST.AssociativeAST.ArrayNode)); } else if (rhsNode is ProtoCore.AST.AssociativeAST.FunctionDotCallNode) { return(new ProtoCore.AST.AssociativeAST.FunctionDotCallNode(rhsNode as ProtoCore.AST.AssociativeAST.FunctionDotCallNode)); } else if (rhsNode is ProtoCore.AST.AssociativeAST.ExprListNode) { return(new ProtoCore.AST.AssociativeAST.ExprListNode(rhsNode as ProtoCore.AST.AssociativeAST.ExprListNode)); } else if (rhsNode is ProtoCore.AST.AssociativeAST.LanguageBlockNode) { return(new ProtoCore.AST.AssociativeAST.LanguageBlockNode(rhsNode as ProtoCore.AST.AssociativeAST.LanguageBlockNode)); } else if (rhsNode is ProtoCore.AST.AssociativeAST.ThisPointerNode) { return(new ProtoCore.AST.AssociativeAST.ThisPointerNode(rhsNode as ProtoCore.AST.AssociativeAST.ThisPointerNode)); } else if (rhsNode is ProtoCore.AST.AssociativeAST.InlineConditionalNode) { return(new ProtoCore.AST.AssociativeAST.InlineConditionalNode(rhsNode as ProtoCore.AST.AssociativeAST.InlineConditionalNode)); } else if (rhsNode is ProtoCore.AST.AssociativeAST.RangeExprNode) { return(new ProtoCore.AST.AssociativeAST.RangeExprNode(rhsNode as ProtoCore.AST.AssociativeAST.RangeExprNode)); } else if (rhsNode is ProtoCore.AST.AssociativeAST.ModifierStackNode) { return(new ProtoCore.AST.AssociativeAST.ModifierStackNode(rhsNode as ProtoCore.AST.AssociativeAST.ModifierStackNode)); } else if (rhsNode is ProtoCore.AST.AssociativeAST.GroupExpressionNode) { return(new ProtoCore.AST.AssociativeAST.GroupExpressionNode(rhsNode as ProtoCore.AST.AssociativeAST.GroupExpressionNode)); } else if (rhsNode is ProtoCore.AST.AssociativeAST.IntNode) { return(new ProtoCore.AST.AssociativeAST.IntNode(rhsNode as ProtoCore.AST.AssociativeAST.IntNode)); } else if (rhsNode is ProtoCore.AST.AssociativeAST.DoubleNode) { return(new ProtoCore.AST.AssociativeAST.DoubleNode(rhsNode as ProtoCore.AST.AssociativeAST.DoubleNode)); } else if (rhsNode is ProtoCore.AST.AssociativeAST.BooleanNode) { return(new ProtoCore.AST.AssociativeAST.BooleanNode(rhsNode as ProtoCore.AST.AssociativeAST.BooleanNode)); } else if (rhsNode is ProtoCore.AST.AssociativeAST.CharNode) { return(new ProtoCore.AST.AssociativeAST.CharNode(rhsNode as ProtoCore.AST.AssociativeAST.CharNode)); } else if (rhsNode is ProtoCore.AST.AssociativeAST.StringNode) { return(new ProtoCore.AST.AssociativeAST.StringNode(rhsNode as ProtoCore.AST.AssociativeAST.StringNode)); } else if (rhsNode is ProtoCore.AST.AssociativeAST.NullNode) { return(new ProtoCore.AST.AssociativeAST.NullNode()); } // Comment Jun: Leaving this as an assert to can catch unhandled nodes Validity.Assert(false); return(null); }
public void DFSTraverse(ref ProtoCore.AST.AssociativeAST.AssociativeNode node) { if (node is ProtoCore.AST.AssociativeAST.IdentifierNode) { EmitIdentifierNode(ref node); } else if (node is ProtoCore.AST.AssociativeAST.IdentifierListNode) { ProtoCore.AST.AssociativeAST.IdentifierListNode identList = node as ProtoCore.AST.AssociativeAST.IdentifierListNode; EmitIdentifierListNode(ref identList); } else if (node is ProtoCore.AST.AssociativeAST.IntNode) { ProtoCore.AST.AssociativeAST.IntNode intNode = node as ProtoCore.AST.AssociativeAST.IntNode; EmitIntNode(ref intNode); } else if (node is ProtoCore.AST.AssociativeAST.DoubleNode) { ProtoCore.AST.AssociativeAST.DoubleNode doubleNode = node as ProtoCore.AST.AssociativeAST.DoubleNode; EmitDoubleNode(ref doubleNode); } else if (node is ProtoCore.AST.AssociativeAST.FunctionCallNode) { ProtoCore.AST.AssociativeAST.FunctionCallNode funcCallNode = node as ProtoCore.AST.AssociativeAST.FunctionCallNode; EmitFunctionCallNode(ref funcCallNode); } else if (node is ProtoCore.AST.AssociativeAST.FunctionDotCallNode) { ProtoCore.AST.AssociativeAST.FunctionDotCallNode funcDotCall = node as ProtoCore.AST.AssociativeAST.FunctionDotCallNode; EmitFunctionDotCallNode(ref funcDotCall); } else if (node is ProtoCore.AST.AssociativeAST.BinaryExpressionNode) { ProtoCore.AST.AssociativeAST.BinaryExpressionNode binaryExpr = node as ProtoCore.AST.AssociativeAST.BinaryExpressionNode; if (binaryExpr.Optr != ProtoCore.DSASM.Operator.assign) { ; } EmitBinaryNode(ref binaryExpr); if (binaryExpr.Optr == ProtoCore.DSASM.Operator.assign) { } if (binaryExpr.Optr != ProtoCore.DSASM.Operator.assign) { ; } } else if (node is ProtoCore.AST.AssociativeAST.FunctionDefinitionNode) { ProtoCore.AST.AssociativeAST.FunctionDefinitionNode funcDefNode = node as ProtoCore.AST.AssociativeAST.FunctionDefinitionNode; EmitFunctionDefNode(ref funcDefNode); } else if (node is ProtoCore.AST.AssociativeAST.ClassDeclNode) { ProtoCore.AST.AssociativeAST.ClassDeclNode classDeclNode = node as ProtoCore.AST.AssociativeAST.ClassDeclNode; EmitClassDeclNode(ref classDeclNode); } else if (node is ProtoCore.AST.AssociativeAST.NullNode) { ProtoCore.AST.AssociativeAST.NullNode nullNode = node as ProtoCore.AST.AssociativeAST.NullNode; EmitNullNode(ref nullNode); } else if (node is ProtoCore.AST.AssociativeAST.ArrayIndexerNode) { ProtoCore.AST.AssociativeAST.ArrayIndexerNode arrIdxNode = node as ProtoCore.AST.AssociativeAST.ArrayIndexerNode; EmitArrayIndexerNode(ref arrIdxNode); } else if (node is ProtoCore.AST.AssociativeAST.ExprListNode) { ProtoCore.AST.AssociativeAST.ExprListNode exprListNode = node as ProtoCore.AST.AssociativeAST.ExprListNode; EmitExprListNode(ref exprListNode); } }
protected virtual void EmitReturnNode(ref ProtoCore.AST.AssociativeAST.ReturnNode returnNode) { ProtoCore.AST.AssociativeAST.AssociativeNode rightNode = returnNode.ReturnExpr; DFSTraverse(ref rightNode); }
public static ProtoCore.AST.AssociativeAST.FunctionDotCallNode GenerateCallDotNode(ProtoCore.AST.AssociativeAST.AssociativeNode lhs, ProtoCore.AST.AssociativeAST.FunctionCallNode rhsCall, Core core = null) { // The function name to call string rhsName = rhsCall.Function.Name; int argNum = rhsCall.FormalArguments.Count; ProtoCore.AST.AssociativeAST.ExprListNode argList = new ProtoCore.AST.AssociativeAST.ExprListNode(); foreach (ProtoCore.AST.AssociativeAST.AssociativeNode arg in rhsCall.FormalArguments) { // The function arguments argList.list.Add(arg); } ProtoCore.AST.AssociativeAST.FunctionCallNode funCallNode = new ProtoCore.AST.AssociativeAST.FunctionCallNode(); ProtoCore.AST.AssociativeAST.IdentifierNode funcName = new ProtoCore.AST.AssociativeAST.IdentifierNode { Value = ProtoCore.DSASM.Constants.kDotArgMethodName, Name = ProtoCore.DSASM.Constants.kDotArgMethodName }; funCallNode.Function = funcName; funCallNode.Name = ProtoCore.DSASM.Constants.kDotArgMethodName; NodeUtils.CopyNodeLocation(funCallNode, lhs); int rhsIdx = ProtoCore.DSASM.Constants.kInvalidIndex; string lhsName = null; if (lhs is ProtoCore.AST.AssociativeAST.IdentifierNode) { lhsName = (lhs as ProtoCore.AST.AssociativeAST.IdentifierNode).Name; if (lhsName == ProtoCore.DSDefinitions.Kw.kw_this) { lhs = new ProtoCore.AST.AssociativeAST.ThisPointerNode(); } } if (core != null) { if (argNum >= 0) { ProtoCore.DSASM.DynamicFunctionNode dynamicFunctionNode = new ProtoCore.DSASM.DynamicFunctionNode(rhsName, new List <ProtoCore.Type>()); core.DynamicFunctionTable.functionTable.Add(dynamicFunctionNode); rhsIdx = core.DynamicFunctionTable.functionTable.Count - 1; } else { DSASM.DyanmicVariableNode dynamicVariableNode = new DSASM.DyanmicVariableNode(rhsName); core.DynamicVariableTable.variableTable.Add(dynamicVariableNode); rhsIdx = core.DynamicVariableTable.variableTable.Count - 1; } } // The first param to the dot arg (the pointer or the class name) ProtoCore.AST.AssociativeAST.IntNode rhs = new ProtoCore.AST.AssociativeAST.IntNode() { value = rhsIdx.ToString() }; funCallNode.FormalArguments.Add(lhs); // The second param which is the dynamic table index of the function to call funCallNode.FormalArguments.Add(rhs); // The array dimensions ProtoCore.AST.AssociativeAST.ExprListNode arrayDimExperList = new ProtoCore.AST.AssociativeAST.ExprListNode(); int dimCount = 0; if (rhsCall.Function is ProtoCore.AST.AssociativeAST.IdentifierNode) { // Number of dimensions ProtoCore.AST.AssociativeAST.IdentifierNode fIdent = rhsCall.Function as ProtoCore.AST.AssociativeAST.IdentifierNode; if (fIdent.ArrayDimensions != null) { arrayDimExperList = ProtoCore.Utils.CoreUtils.BuildArrayExprList(fIdent.ArrayDimensions); dimCount = arrayDimExperList.list.Count; } else if (rhsCall.ArrayDimensions != null) { arrayDimExperList = ProtoCore.Utils.CoreUtils.BuildArrayExprList(rhsCall.ArrayDimensions); dimCount = arrayDimExperList.list.Count; } else { arrayDimExperList = new ProtoCore.AST.AssociativeAST.ExprListNode(); } } funCallNode.FormalArguments.Add(arrayDimExperList); // Number of dimensions ProtoCore.AST.AssociativeAST.IntNode dimNode = new ProtoCore.AST.AssociativeAST.IntNode() { value = dimCount.ToString() }; funCallNode.FormalArguments.Add(dimNode); if (argNum >= 0) { funCallNode.FormalArguments.Add(argList); funCallNode.FormalArguments.Add(new ProtoCore.AST.AssociativeAST.IntNode() { value = argNum.ToString() }); } ProtoCore.AST.AssociativeAST.FunctionDotCallNode funDotCallNode = new ProtoCore.AST.AssociativeAST.FunctionDotCallNode(rhsCall); funDotCallNode.DotCall = funCallNode; funDotCallNode.FunctionCall.Function = rhsCall.Function; // Consider the case of "myClass.Foo(a, b)", we will have "DotCall" being // equal to "myClass" (in terms of its starting line/column), and "rhsCall" // matching with the location of "Foo(a, b)". For execution cursor to cover // this whole statement, the final "DotCall" function call node should // range from "lhs.col" to "rhs.col". // NodeUtils.SetNodeEndLocation(funDotCallNode.DotCall, rhsCall); NodeUtils.CopyNodeLocation(funDotCallNode, funDotCallNode.DotCall); return(funDotCallNode); }
protected virtual void EmitReturnNode(ProtoCore.AST.AssociativeAST.ReturnNode returnNode) { EmitCode("return = "); ProtoCore.AST.AssociativeAST.AssociativeNode rightNode = returnNode.ReturnExpr; DFSTraverse(rightNode); }
protected virtual void EmitFunctionCallNode(ProtoCore.AST.AssociativeAST.FunctionCallNode funcCallNode) { Validity.Assert(null != funcCallNode); Validity.Assert(funcCallNode.Function is ProtoCore.AST.AssociativeAST.IdentifierNode); string functionName = (funcCallNode.Function as ProtoCore.AST.AssociativeAST.IdentifierNode).Value; Validity.Assert(!string.IsNullOrEmpty(functionName)); if (functionName.StartsWith("%")) { EmitCode("("); DFSTraverse(funcCallNode.FormalArguments[0], true); switch (functionName) { case "%add": EmitCode("+"); break; case "%sub": EmitCode("-"); break; case "%mul": EmitCode("*"); break; case "%div": EmitCode("/"); break; case "%mod": EmitCode("%"); break; case "%Not": EmitCode("!"); break; } if (funcCallNode.FormalArguments.Count > 1) { DFSTraverse(funcCallNode.FormalArguments[1], true); } EmitCode(")"); } else { EmitCode(functionName); EmitCode("("); for (int n = 0; n < funcCallNode.FormalArguments.Count; ++n) { ProtoCore.AST.AssociativeAST.AssociativeNode argNode = funcCallNode.FormalArguments[n]; DFSTraverse(argNode, true); if (n + 1 < funcCallNode.FormalArguments.Count) { EmitCode(","); } } EmitCode(")"); } }