/// <summary> /// Retrieves the string format of the identifier list from left to right, leaving out any symbols after the last identifier. /// Given: A.B() /// Return: "A.B" /// Given: A.B.C()[0] /// Return: "A.B.C" /// Given: A.B().C /// Return: "A.B" /// Given: A.B[0].C /// Return: "A.B[0].C" /// </summary> /// <param name="identList"></param> /// <returns></returns> public static string GetIdentifierStringUntilFirstParenthesis(ProtoCore.AST.AssociativeAST.IdentifierListNode identList) { Validity.Assert(null != identList); string identListString = identList.ToString(); int removeIndex = identListString.IndexOf('('); if (removeIndex > 0) { identListString = identListString.Remove(removeIndex); } return(identListString); }
/// <summary> /// Retrieves the string format of the identifier list from left to right, leaving out any symbols after the last identifier. /// Given: A.B() /// Return: "A" /// Given: A.B.C()[0] /// Return: "A.B" /// Given: A.B().C /// Return: "A" /// Given: A.B[0].C /// Return: "A.B[0].C" /// </summary> /// <param name="identList"></param> /// <returns></returns> public static string GetIdentifierExceptMethodName(ProtoCore.AST.AssociativeAST.IdentifierListNode identList) { Validity.Assert(null != identList); string identListString = identList.ToString(); int removeIndex = identListString.IndexOf('('); if (removeIndex > 0) { identListString = identListString.Remove(removeIndex); identListString = identListString.Remove(identListString.LastIndexOf('.')); } return(identListString); }
private BinaryExpressionNode BuildSSAIdentListAssignmentNode(IdentifierListNode identList) { // Build the final binary expression BinaryExpressionNode bnode = new BinaryExpressionNode(); bnode.Optr = ProtoCore.DSASM.Operator.assign; // Left node var identNode = AstFactory.BuildIdentifier(CoreUtils.BuildSSATemp(core)); bnode.LeftNode = identNode; //Right node bnode.RightNode = identList; bnode.isSSAAssignment = true; return bnode; }
public override IEnumerable<AssociativeNode> BuildOutputAst(List<AssociativeNode> inputAstNodes) { if (HasUnconnectedInput()) { var connectedInput = Enumerable.Range(0, InPortData.Count) .Where(HasConnectedInput) .Select(x => new IntNode(x) as AssociativeNode) .ToList(); var paramNumNode = new IntNode(InPortData.Count); var positionNode = AstFactory.BuildExprList(connectedInput); var arguments = AstFactory.BuildExprList(inputAstNodes); var functionNode = new IdentifierListNode { LeftNode = new IdentifierNode("DSCore.List"), RightNode = new IdentifierNode("__Create") }; var inputParams = new List<AssociativeNode> { functionNode, paramNumNode, positionNode, arguments, AstFactory.BuildBooleanNode(false) }; return new[] { AstFactory.BuildAssignment( GetAstIdentifierForOutputIndex(0), AstFactory.BuildFunctionCall("_SingleFunctionObject", inputParams)) }; } else { return new[] { AstFactory.BuildAssignment( GetAstIdentifierForOutputIndex(0), AstFactory.BuildExprList(inputAstNodes)) }; } }
/* proc dfs_ssa_identlist(node) if node is not null dfs_ssa_identlist(node.left) end if node is functioncall foreach arg in functioncallArgs def ssastack[] def astlist[] DFSEmit_SSA_AST(ref arg, ssastack, astlist) end end end */ private BinaryExpressionNode BuildSSAIdentListAssignmentNode(IdentifierListNode identList) { // Build the final binary expression BinaryExpressionNode bnode = new BinaryExpressionNode(); bnode.Optr = ProtoCore.DSASM.Operator.assign; // Left node var identNode = nodeBuilder.BuildIdentfier(ProtoCore.Utils.CoreUtils.GetSSATemp(compileStateTracker)); bnode.LeftNode = identNode; //Right node bnode.RightNode = identList; bnode.isSSAAssignment = true; return bnode; }
private ProtoCore.AST.AssociativeAST.IdentifierListNode BuildIdentifierList(List<ProtoCore.AST.AssociativeAST.AssociativeNode> astIdentList) { // TODO Jun: Replace this condition or handle this case prior to this call if (astIdentList.Count < 2) { return null; } AST.AssociativeAST.IdentifierListNode identList = null; // Build the first ident list identList = new AST.AssociativeAST.IdentifierListNode(); identList.LeftNode = astIdentList[0]; identList.RightNode = astIdentList[1]; // Build the rest for (int n = 2; n < astIdentList.Count; ++n) { // Build a new identllist for the prev identlist AST.AssociativeAST.IdentifierListNode subIdentList = new AST.AssociativeAST.IdentifierListNode(identList); subIdentList.Optr = Operator.dot; // Build a new ident and assign it the prev identlist and the next identifier identList = new AST.AssociativeAST.IdentifierListNode(); identList.LeftNode = subIdentList; identList.RightNode = astIdentList[n]; } return identList; }
/// <summary> /// Given a name or string of names, this creates an IdentifierNode or IdentifierListNode /// e.g. Creates an IdentifierNode from A and IdentifierListNode from A.B /// </summary> /// <param name="name"></param> /// <returns></returns> public static AssociativeNode CreateNodeFromString(string name) { string[] strIdentList = name.Split('.'); if (strIdentList.Length == 1) { return new IdentifierNode(strIdentList[0]); } var newIdentList = new IdentifierListNode { LeftNode = new IdentifierNode(strIdentList[0]), RightNode = new IdentifierNode(strIdentList[1]), Optr = Operator.dot }; for (var n = 2; n < strIdentList.Length; ++n) { var subIdentList = new IdentifierListNode { LeftNode = newIdentList, RightNode = new IdentifierNode(strIdentList[n]), Optr = Operator.dot }; newIdentList = subIdentList; } return newIdentList; }
/// <summary> /// Traverses the identifierlist argument until class name resolution succeeds or fails. /// </summary> /// <param name="classTable"></param> /// <param name="identList"></param> /// <returns></returns> public static string[] GetResolvedClassName(ProtoCore.DSASM.ClassTable classTable, ProtoCore.AST.AssociativeAST.IdentifierListNode identList) { string[] classNames = classTable.GetAllMatchingClasses(ProtoCore.Utils.CoreUtils.GetIdentifierStringUntilFirstParenthesis(identList)); // Failed to find the first time // Attempt to remove identifiers in the identifierlist until we find a class or not while (0 == classNames.Length) { // Move to the left node AssociativeNode leftNode = identList.LeftNode; if (leftNode is IdentifierListNode) { identList = leftNode as IdentifierListNode; classNames = classTable.GetAllMatchingClasses(ProtoCore.Utils.CoreUtils.GetIdentifierStringUntilFirstParenthesis(identList)); } if (leftNode is IdentifierNode) { IdentifierNode identNode = leftNode as IdentifierNode; classNames = classTable.GetAllMatchingClasses(identNode.Name); break; } else { break; } } return(classNames); }
void Associative_IdentifierList(out ProtoCore.AST.AssociativeAST.AssociativeNode node) { node = null; if (isInClass && IsIdentList()) { disableKwCheck = true; } Associative_NameReference(out node); disableKwCheck = false; ProtoCore.AST.AssociativeAST.AssociativeNode inode = node; while (la.kind == 6) { Get(); ProtoCore.AST.AssociativeAST.AssociativeNode rnode = null; Associative_NameReference(out rnode); if ((inode is ProtoCore.AST.AssociativeAST.IdentifierNode) && (inode as ProtoCore.AST.AssociativeAST.IdentifierNode).Name == ProtoCore.DSDefinitions.Keyword.This && (rnode is ProtoCore.AST.AssociativeAST.FunctionCallNode)) { node = rnode; return; } ProtoCore.AST.AssociativeAST.IdentifierListNode bnode = new ProtoCore.AST.AssociativeAST.IdentifierListNode(); bnode.LeftNode = node; bnode.Optr = Operator.dot; bnode.RightNode = rnode; node = bnode; NodeUtils.SetNodeLocation(bnode, bnode.LeftNode, bnode.RightNode); if (!core.Options.GenerateSSA) { bool isNeitherIdentOrFunctionCall = !(rnode is ProtoCore.AST.AssociativeAST.IdentifierNode || rnode is ProtoCore.AST.AssociativeAST.FunctionCallNode); if (isLeft || isNeitherIdentOrFunctionCall) { node = inode; } else { if (rnode is ProtoCore.AST.AssociativeAST.IdentifierNode) { ProtoCore.AST.AssociativeAST.FunctionCallNode rcall = new ProtoCore.AST.AssociativeAST.FunctionCallNode(); rcall.Function = rnode; rcall.Function.Name = ProtoCore.DSASM.Constants.kGetterPrefix + rcall.Function.Name; bnode.RightNode = rcall; NodeUtils.SetNodeLocation(rcall, rnode, rnode); node = ProtoCore.Utils.CoreUtils.GenerateCallDotNode(bnode.LeftNode, rcall, core); } else { string rhsName = null; ProtoCore.AST.AssociativeAST.ExprListNode dimList = null; int dim = 0; if (rnode is ProtoCore.AST.AssociativeAST.FunctionCallNode) { ProtoCore.AST.AssociativeAST.FunctionCallNode rhsFNode = rnode as ProtoCore.AST.AssociativeAST.FunctionCallNode; node = ProtoCore.Utils.CoreUtils.GenerateCallDotNode(node, rhsFNode, core); } } } } } { if (!isModifier && withinModifierCheckScope) { if (isLeftVarIdentList) { if (inode is ProtoCore.AST.AssociativeAST.IdentifierListNode) { isModifier = false; if (node is ProtoCore.AST.AssociativeAST.FunctionDotCallNode) { ProtoCore.AST.AssociativeAST.FunctionDotCallNode fdotCall = node as ProtoCore.AST.AssociativeAST.FunctionDotCallNode; string checkVar = ProtoCore.Utils.CoreUtils.GenerateIdentListNameString(fdotCall.GetIdentList()); isModifier = (leftVar == checkVar); } } } else if (inode is ProtoCore.AST.AssociativeAST.IdentifierNode) { isModifier = (leftVar == inode.Name); } // The LHS is an identifier else { // It is a modifier if the lhs is: // 1. the same as the current node // 2. the current node starts with the lhs identifier isModifier = (leftVar == inode.Name); if (!isModifier) { string rhsString = ProtoCore.Utils.CoreUtils.GenerateIdentListNameString(inode); isModifier = rhsString.StartsWith(leftVar); } } } } }
private bool IsMatchingResolvedName(IdentifierListNode identifierList, out AssociativeNode newIdentList) { newIdentList = null; var resolvedName = ResolveClassName(identifierList); if (string.IsNullOrEmpty(resolvedName)) return false; newIdentList = CoreUtils.CreateNodeFromString(resolvedName); var symbol = new Symbol(resolvedName); return symbol.Matches(identifierList.ToString()); }
/// <summary> /// Retrieves the string format of the identifier list from left to right, leaving out any symbols after the last identifier. /// Given: A.B() /// Return: "A.B" /// Given: A.B.C()[0] /// Return: "A.B.C" /// Given: A.B().C /// Return: "A.B" /// Given: A.B[0].C /// Return: "A.B[0].C" /// </summary> /// <param name="identList"></param> /// <returns></returns> public static string GetIdentifierStringUntilFirstParenthesis(IdentifierListNode identList) { Validity.Assert(null != identList); string identListString = identList.ToString(); int removeIndex = identListString.IndexOf('('); if (removeIndex > 0) { identListString = identListString.Remove(removeIndex); } return identListString; }
// // proc SSAIdentList(node, ssastack, ast) // { // if node is ident // t = SSATemp() // tmpIdent = new Ident(t) // binexpr = new BinaryExpr(tmpIdent, node) // ast.push(binexpr) // ssastack.push(tmpIdent) // else if node is identlist // SSAIdentList(node.left, ssastack, ast) // rhs = new identlist(new Ident(ssastack.pop), node.right) // t = SSATemp() // tmpIdent = new Ident(t) // binexpr = new BinaryExpr(tmpIdent, rhs) // ast.push(binexpr) // ssastack.push(tmpIdent) // end // } // private void SSAIdentList(AssociativeNode node, ref Stack<AssociativeNode> ssaStack, ref List<AssociativeNode> astlist) { if (node is IdentifierNode) { IdentifierNode ident = node as IdentifierNode; if (null == ident.ArrayDimensions) { // Build the temp pointer BinaryExpressionNode bnode = new BinaryExpressionNode(); bnode.Optr = ProtoCore.DSASM.Operator.assign; bnode.isSSAAssignment = true; bnode.isSSAPointerAssignment = true; // Left node var identNode = nodeBuilder.BuildIdentfier(ProtoCore.Utils.CoreUtils.BuildSSATemp(core)); (identNode as IdentifierNode).ReplicationGuides = GetReplicationGuides(ident); bnode.LeftNode = identNode; // Right node bnode.RightNode = ident; astlist.Add(bnode); ssaStack.Push(bnode); } else { EmitSSAArrayIndex(ident, ssaStack, ref astlist, true); } } else if (node is ExprListNode) { //ExprListNode exprList = node as ExprListNode; DFSEmitSSA_AST(node, ssaStack, ref astlist); } else if (node is FunctionCallNode) { FunctionCallNode fcall = node as FunctionCallNode; if (null == fcall.ArrayDimensions) { // Build the temp pointer BinaryExpressionNode bnode = new BinaryExpressionNode(); bnode.Optr = ProtoCore.DSASM.Operator.assign; bnode.isSSAAssignment = true; bnode.isSSAPointerAssignment = true; // Left node var identNode = nodeBuilder.BuildIdentfier(ProtoCore.Utils.CoreUtils.BuildSSATemp(core)); (identNode as IdentifierNode).ReplicationGuides = fcall.ReplicationGuides; bnode.LeftNode = identNode; // Right node bnode.RightNode = fcall; astlist.Add(bnode); ssaStack.Push(bnode); } else { EmitSSAArrayIndex(fcall, ssaStack, ref astlist, true); } } else if (node is IdentifierListNode) { IdentifierListNode identList = node as IdentifierListNode; //Check if the LeftNode for given IdentifierList represents a class. string[] classNames = ProtoCore.Utils.CoreUtils.GetResolvedClassName(core.ClassTable, identList); if (classNames.Length > 1) { // There is a namespace conflict // TODO Jun: Move this warning handler to after the SSA transform // http://adsk-oss.myjetbrains.com/youtrack/issue/MAGN-5221 buildStatus.LogSymbolConflictWarning(identList.LeftNode.ToString(), classNames); } else if(classNames.Length == 1) { // A matching class has been found var leftNode = nodeBuilder.BuildIdentfier(classNames[0]); SSAIdentList(leftNode, ref ssaStack, ref astlist); } else { // There is no matching class name, continue traversing the identlist // Check if the lhs is an identifier and if it has any namespace conflicts // We want to handle this here because we know ident lists can potentially contain namespace resolving var ident = identList.LeftNode as IdentifierNode; // Check if this is the last ident in the identlist if(ident != null) { // TODO Jun: Move this warning handler to after the SSA transform // http://adsk-oss.myjetbrains.com/youtrack/issue/MAGN-5221 classNames = core.ClassTable.GetAllMatchingClasses(ident.Value); if (classNames.Length > 1) { // There is a namespace conflict buildStatus.LogSymbolConflictWarning(ident.Value, classNames); // Continue traversing the expression even after a namespace conflict // TODO: Determine if we want to terminate traversal of this identlist // http://adsk-oss.myjetbrains.com/youtrack/issue/MAGN-5221 } } // Recursively traversse the left of the ident list SSAIdentList(identList.LeftNode, ref ssaStack, ref astlist); } // Build the rhs identifier list containing the temp pointer IdentifierListNode rhsIdentList = new IdentifierListNode(); rhsIdentList.Optr = Operator.dot; AssociativeNode lhsNode = ssaStack.Pop(); if (lhsNode is BinaryExpressionNode) { rhsIdentList.LeftNode = (lhsNode as BinaryExpressionNode).LeftNode; } else { rhsIdentList.LeftNode = lhsNode; } ArrayNode arrayDimension = null; AssociativeNode rnode = null; if (identList.RightNode is IdentifierNode) { IdentifierNode identNode = identList.RightNode as IdentifierNode; arrayDimension = identNode.ArrayDimensions; rnode = identNode; } else if (identList.RightNode is FunctionCallNode) { FunctionCallNode fcNode = new FunctionCallNode(identList.RightNode as FunctionCallNode); arrayDimension = fcNode.ArrayDimensions; List<AssociativeNode> astlistArgs = new List<AssociativeNode>(); for (int idx = 0; idx < fcNode.FormalArguments.Count; idx++) { AssociativeNode arg = fcNode.FormalArguments[idx]; var replicationGuides = GetReplicationGuides(arg); if (replicationGuides == null) { replicationGuides = new List<AssociativeNode> { }; } else { RemoveReplicationGuides(arg); } DFSEmitSSA_AST(arg, ssaStack, ref astlistArgs); var argNode = ssaStack.Pop(); var argBinaryExpr = argNode as BinaryExpressionNode; if (argBinaryExpr != null) { var newArgNode = NodeUtils.Clone(argBinaryExpr.LeftNode); (newArgNode as IdentifierNode).ReplicationGuides = replicationGuides; fcNode.FormalArguments[idx] = newArgNode; } else { fcNode.FormalArguments[idx] = argNode; } astlist.AddRange(astlistArgs); astlistArgs.Clear(); } astlist.AddRange(astlistArgs); rnode = fcNode; } else { Validity.Assert(false); } Validity.Assert(null != rnode); rhsIdentList.RightNode = rnode; if (null == arrayDimension) { // New SSA expr for the current dot call string ssatemp = ProtoCore.Utils.CoreUtils.BuildSSATemp(core); var tmpIdent = nodeBuilder.BuildIdentfier(ssatemp); BinaryExpressionNode bnode = new BinaryExpressionNode(tmpIdent, rhsIdentList, Operator.assign); bnode.isSSAPointerAssignment = true; astlist.Add(bnode); //ssaStack.Push(tmpIdent); ssaStack.Push(bnode); } else { EmitSSAArrayIndex(rhsIdentList, ssaStack, ref astlist, true); } } }
public override AssociativeNode VisitIdentifierListNode(IdentifierListNode node) { // First pass attempt to resolve the node before traversing it deeper AssociativeNode newIdentifierListNode = null; if (IsMatchingResolvedName(node, out newIdentifierListNode)) return newIdentifierListNode; var rightNode = node.RightNode; var leftNode = node.LeftNode; rightNode = rightNode.Accept(this); leftNode = leftNode.Accept(this); node = new IdentifierListNode { LeftNode = leftNode, RightNode = rightNode, Optr = Operator.dot }; return RewriteIdentifierListNode(node); }
private AssociativeNode RewriteIdentifierListNode(AssociativeNode identifierList) { var resolvedName = ResolveClassName(identifierList); if (string.IsNullOrEmpty(resolvedName)) return identifierList; var identListNode = identifierList as IdentifierListNode; var newIdentList = CoreUtils.CreateNodeFromString(resolvedName); // If the original input node matches with the resolved name, simply return // the identifier list constructed from the resolved name var symbol = new Symbol(resolvedName); if (symbol.Matches(identifierList.ToString())) return newIdentList; // Remove partialName from identListNode and replace with newIdentList AssociativeNode leftNode = identListNode != null ? identListNode.LeftNode : identifierList; AssociativeNode rightNode = identListNode != null ? identListNode.RightNode : identifierList; var intermediateNodes = new List<AssociativeNode>(); while (leftNode is IdentifierListNode && !symbol.Matches(leftNode.ToString())) { intermediateNodes.Insert(0, ((IdentifierListNode)leftNode).RightNode); leftNode = ((IdentifierListNode)leftNode).LeftNode; } intermediateNodes.Insert(0, newIdentList); var lNode = CoreUtils.CreateNodeByCombiningIdentifiers(intermediateNodes); // The last ident list for the functioncall or identifier rhs var lastIdentList = new IdentifierListNode { LeftNode = lNode, RightNode = rightNode, Optr = Operator.dot }; return lastIdentList; }
public AssociativeNode BuildIdentList(AssociativeNode leftNode, AssociativeNode rightNode) { var identList = new IdentifierListNode(); identList.LeftNode = leftNode; identList.RightNode = rightNode; identList.Optr = Operator.dot; return identList; }
/// <summary> /// Converts lhs ident lists to a function call /// a.x = 10 /// -> t = a.%set_x(10) /// /// a.x.y = b + c /// -> a.x.%set_y(b + c) /// /// a.x[0] = 10 /// -> tval = a.%get_x() /// tval[0] = 10 /// tmp = a.%set_x(tval) /// </summary> /// <param name="astList"></param> /// <returns></returns> private List<AssociativeNode> TransformLHSIdentList(List<AssociativeNode> astList) { List<AssociativeNode> newAstList = new List<AssociativeNode>(); foreach (AssociativeNode node in astList) { BinaryExpressionNode bNode = node as BinaryExpressionNode; if (bNode == null) { newAstList.Add(node); } else { bool isLHSIdentList = bNode.LeftNode is IdentifierListNode; if (!isLHSIdentList) { newAstList.Add(node); } else { IdentifierNode lhsTemp = new IdentifierNode(Constants.kTempVar); IdentifierListNode identList = bNode.LeftNode as IdentifierListNode; Validity.Assert(identList != null); AssociativeNode argument = bNode.RightNode; IdentifierNode identFunctionCall = identList.RightNode as IdentifierNode; string setterName = ProtoCore.DSASM.Constants.kSetterPrefix + identList.RightNode.Name; bool isArrayIndexed = identFunctionCall.ArrayDimensions != null; if (isArrayIndexed) { // a.x[0] = 10 // tval = a.%get_x() string getterName = ProtoCore.DSASM.Constants.kGetterPrefix + identList.RightNode.Name; ProtoCore.AST.AssociativeAST.FunctionCallNode fcall = new ProtoCore.AST.AssociativeAST.FunctionCallNode(); fcall.Function = new IdentifierNode(identList.RightNode.Name); fcall.Function.Name = getterName; IdentifierListNode identList1 = new IdentifierListNode(); identList1.LeftNode = identList.LeftNode; identList1.RightNode = fcall; BinaryExpressionNode bnodeGet = new BinaryExpressionNode( lhsTemp, identList1, Operator.assign ); newAstList.Add(bnodeGet); // tval[0] = 10 IdentifierNode lhsTempIndexed = new IdentifierNode(Constants.kTempVar); lhsTempIndexed.ArrayDimensions = identFunctionCall.ArrayDimensions; BinaryExpressionNode bnodeAssign = new BinaryExpressionNode( lhsTempIndexed, argument, Operator.assign ); newAstList.Add(bnodeAssign); // tmp = a.%set_x(tval) ProtoCore.AST.AssociativeAST.FunctionCallNode fcallSet = new ProtoCore.AST.AssociativeAST.FunctionCallNode(); fcallSet.Function = identFunctionCall; fcallSet.Function.Name = setterName; List<AssociativeNode> args = new List<AssociativeNode>(); IdentifierNode lhsTempAssignBack = new IdentifierNode(Constants.kTempVar); args.Add(lhsTempAssignBack); fcallSet.FormalArguments = args; IdentifierListNode identList2 = new IdentifierListNode(); identList2.LeftNode = identList.LeftNode; identList2.RightNode = fcallSet; IdentifierNode lhsTempAssign = new IdentifierNode(Constants.kTempPropertyVar); BinaryExpressionNode bnodeSet = new BinaryExpressionNode( lhsTempAssign, identList2, Operator.assign ); newAstList.Add(bnodeSet); } else { List<AssociativeNode> args = new List<AssociativeNode>(); args.Add(argument); ProtoCore.AST.AssociativeAST.FunctionCallNode fcall = new ProtoCore.AST.AssociativeAST.FunctionCallNode(); fcall.Function = identFunctionCall; fcall.Function.Name = setterName; fcall.FormalArguments = args; identList.RightNode = fcall; BinaryExpressionNode convertedAssignNode = new BinaryExpressionNode(lhsTemp, identList, Operator.assign); NodeUtils.CopyNodeLocation(convertedAssignNode, bNode); newAstList.Add(convertedAssignNode); } } } } return newAstList; }
public void GraphILTest_FFIClassUsage_01() { List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); //============================================== // Build the import Nodes //============================================== ProtoScript.Runners.ILiveRunner liveRunner = new ProtoScript.Runners.LiveRunner(); List<string> libs = new List<string>(); libs.Add("ProtoGeometry.dll"); liveRunner.ResetVMAndResyncGraph(libs); //============================================== // Build the constructor call nodes // Point.ByCoordinates(10,10,10) //============================================== astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); ProtoCore.AST.AssociativeAST.FunctionCallNode constructorCall = new ProtoCore.AST.AssociativeAST.FunctionCallNode(); constructorCall.Function = new ProtoCore.AST.AssociativeAST.IdentifierNode("ByCoordinates"); List<ProtoCore.AST.AssociativeAST.AssociativeNode> listArgs = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); listArgs.Add(new ProtoCore.AST.AssociativeAST.DoubleNode(10.0)); listArgs.Add(new ProtoCore.AST.AssociativeAST.DoubleNode(10.0)); listArgs.Add(new ProtoCore.AST.AssociativeAST.DoubleNode(10.0)); constructorCall.FormalArguments = listArgs; string className = "Point"; ProtoCore.AST.AssociativeAST.IdentifierNode inode = new ProtoCore.AST.AssociativeAST.IdentifierNode(className); ProtoCore.AST.AssociativeAST.FunctionDotCallNode dotCall = ProtoCore.Utils.CoreUtils.GenerateCallDotNode(inode, constructorCall, liveRunner.Core); //============================================== // Build the binary expression // p = Point.ByCoordinates(10,10,10) //============================================== ProtoCore.AST.AssociativeAST.BinaryExpressionNode stmt1 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("p"), dotCall, ProtoCore.DSASM.Operator.assign); astList.Add(stmt1); //============================================== // Build a binary expression to retirieve the x property // xval = p.X; //============================================== ProtoCore.AST.AssociativeAST.IdentifierListNode identListNode = new ProtoCore.AST.AssociativeAST.IdentifierListNode(); identListNode.LeftNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("p"); identListNode.Optr = ProtoCore.DSASM.Operator.dot; identListNode.RightNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("X"); ProtoCore.AST.AssociativeAST.BinaryExpressionNode stmt2 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("xval"), identListNode, ProtoCore.DSASM.Operator.assign); astList.Add(stmt2); //============================================== // emit the DS code from the AST tree // // import("ProtoGeometry.dll"); // p = Point.Bycoordinates(10.0, 10.0, 10.0); // xval = p.X; // //============================================== // Instantiate GraphSyncData List<Subtree> addedList = new List<Subtree>(); addedList.Add(new Subtree(astList, System.Guid.NewGuid())); GraphSyncData syncData = new GraphSyncData(null, addedList, null); // emit the DS code from the AST tree liveRunner.UpdateGraph(syncData); ProtoCore.Mirror.RuntimeMirror mirror = liveRunner.InspectNodeValue("xval"); Assert.IsTrue((double)mirror.GetData().Data == 10.0); /////////////////////////////////////////////////////////////////////////////// libs = new List<string>(); libs.Add("ProtoGeometry.dll"); liveRunner.ResetVMAndResyncGraph(libs); astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); constructorCall = new ProtoCore.AST.AssociativeAST.FunctionCallNode(); constructorCall.Function = new ProtoCore.AST.AssociativeAST.IdentifierNode("ByCoordinates"); listArgs = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); listArgs.Add(new ProtoCore.AST.AssociativeAST.DoubleNode(10.0)); listArgs.Add(new ProtoCore.AST.AssociativeAST.DoubleNode(10.0)); listArgs.Add(new ProtoCore.AST.AssociativeAST.DoubleNode(10.0)); constructorCall.FormalArguments = listArgs; className = "Point"; inode = new ProtoCore.AST.AssociativeAST.IdentifierNode(className); dotCall = ProtoCore.Utils.CoreUtils.GenerateCallDotNode(inode, constructorCall, liveRunner.Core); //============================================== // Build the binary expression // p = Point.ByCoordinates(10,10,10) //============================================== stmt1 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("p"), dotCall, ProtoCore.DSASM.Operator.assign); astList.Add(stmt1); //============================================== // Build a binary expression to retirieve the x property // xval = p.X; //============================================== identListNode = new ProtoCore.AST.AssociativeAST.IdentifierListNode(); identListNode.LeftNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("p"); identListNode.Optr = ProtoCore.DSASM.Operator.dot; identListNode.RightNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("X"); stmt2 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("xval"), identListNode, ProtoCore.DSASM.Operator.assign); astList.Add(stmt2); //============================================== // emit the DS code from the AST tree // // import("ProtoGeometry.dll"); // p = Point.Bycoordinates(10.0, 10.0, 10.0); // xval = p.X; // //============================================== // Instantiate GraphSyncData addedList = new List<Subtree>(); addedList.Add(new Subtree(astList, System.Guid.NewGuid())); syncData = new GraphSyncData(null, addedList, null); liveRunner.UpdateGraph(syncData); mirror = liveRunner.InspectNodeValue("xval"); Assert.IsTrue((double)mirror.GetData().Data == 10.0); }
public void TestBuildAST_01() { //============================================== // // import("ProtoGeometry.dll"); // p = Point.Bycoordinates(0.0, 2.0, 1.0); // xval = p.X; // //============================================== //============================================== // Build the import Nodes //============================================== ProtoScript.Runners.ILiveRunner liveRunner = new ProtoScript.Runners.LiveRunner(); List <string> libs = new List <string>(); libs.Add("ProtoGeometry.dll"); liveRunner.ResetVMAndImportLibrary(libs); string type = "Point"; long hostInstancePtr = 0; string functionName = "ByCoordinates"; List <IntPtr> userDefinedArgs = null; List <string> primitiveArgs = new List <string>(); primitiveArgs.Add("0"); primitiveArgs.Add("2"); primitiveArgs.Add("1"); string formatString = "ddd"; string symbolName = ""; string code = ""; AssociativeNode assign1 = ASTCompilerUtils.BuildAST(type, hostInstancePtr, functionName, userDefinedArgs, primitiveArgs, formatString, liveRunner.Core, ref symbolName, ref code); liveRunner.UpdateGraph(assign1); primitiveArgs.Clear(); primitiveArgs.Add("10"); primitiveArgs.Add("0"); primitiveArgs.Add("0"); functionName = "Translate"; AssociativeNode assign2 = ASTCompilerUtils.BuildAST(symbolName, hostInstancePtr, functionName, userDefinedArgs, primitiveArgs, formatString, liveRunner.Core, ref symbolName, ref code); liveRunner.UpdateGraph(assign2); //============================================== // Build a binary expression to retirieve the x property // xval = p.X; //============================================== List <ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List <ProtoCore.AST.AssociativeAST.AssociativeNode>(); ProtoCore.AST.AssociativeAST.IdentifierListNode identListNode = new ProtoCore.AST.AssociativeAST.IdentifierListNode(); identListNode.LeftNode = new ProtoCore.AST.AssociativeAST.IdentifierNode(symbolName); identListNode.Optr = ProtoCore.DSASM.Operator.dot; identListNode.RightNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("X"); ProtoCore.AST.AssociativeAST.BinaryExpressionNode stmt2 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("xval"), identListNode, ProtoCore.DSASM.Operator.assign); astList.Add(stmt2); //============================================== // // import("ProtoGeometry.dll"); // p = Point.Bycoordinates(0.0, 20.0, 1.0); // q = p.Translate(10.0, 0.0, 0.0); // xval = p.X; // //============================================== // update graph CodeBlockNode cNode = new CodeBlockNode(); cNode.Body = astList; liveRunner.UpdateGraph(cNode); ProtoCore.Mirror.RuntimeMirror mirror = liveRunner.InspectNodeValue("xval"); Assert.IsTrue((double)mirror.GetData().Data == 10.0); }
public void GraphILTest_FFIClassUsage_02_astInput() { ProtoScript.Runners.ILiveRunner liveRunner = new ProtoScript.Runners.LiveRunner(); List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); //============================================== // Build the import Nodes //============================================== List<string> libs = new List<string>(); libs.Add("ProtoGeometry.dll"); List<LibraryMirror> libMirrors = liveRunner.ResetVMAndImportLibrary(libs); //============================================== // Build the constructor call nodes // Point.ByCoordinates(10,10,10) //============================================== ProtoCore.AST.AssociativeAST.FunctionCallNode constructorCall = new ProtoCore.AST.AssociativeAST.FunctionCallNode(); constructorCall.Function = new ProtoCore.AST.AssociativeAST.IdentifierNode("ByCoordinates"); List<ProtoCore.AST.AssociativeAST.AssociativeNode> listArgs = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); listArgs.Add(new ProtoCore.AST.AssociativeAST.DoubleNode(10.0)); listArgs.Add(new ProtoCore.AST.AssociativeAST.DoubleNode(10.0)); listArgs.Add(new ProtoCore.AST.AssociativeAST.DoubleNode(10.0)); constructorCall.FormalArguments = listArgs; string className = "Point"; ProtoCore.AST.AssociativeAST.IdentifierNode inode = new ProtoCore.AST.AssociativeAST.IdentifierNode(className); ProtoCore.AST.AssociativeAST.FunctionDotCallNode dotCall = ProtoCore.Utils.CoreUtils.GenerateCallDotNode(inode, constructorCall, liveRunner.Core); //============================================== // Build the binary expression // p = Point.ByCoordinates(10,10,10) //============================================== ProtoCore.AST.AssociativeAST.BinaryExpressionNode stmt1 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("p"), dotCall, ProtoCore.DSASM.Operator.assign); astList.Add(stmt1); //============================================== // Translate the point // newPoint = p.Translate(1,2,3); //============================================== ProtoCore.AST.AssociativeAST.FunctionCallNode functionCallTranslate = new ProtoCore.AST.AssociativeAST.FunctionCallNode(); functionCallTranslate.Function = new ProtoCore.AST.AssociativeAST.IdentifierNode("Translate"); listArgs = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); listArgs.Add(new ProtoCore.AST.AssociativeAST.DoubleNode(1.0)); listArgs.Add(new ProtoCore.AST.AssociativeAST.DoubleNode(2.0)); listArgs.Add(new ProtoCore.AST.AssociativeAST.DoubleNode(3.0)); functionCallTranslate.FormalArguments = listArgs; //ProtoCore.AST.AssociativeAST.FunctionDotCallNode dotCallTranslate = new ProtoCore.AST.AssociativeAST.FunctionDotCallNode("p", functionCallTranslate); className = "p"; inode = new ProtoCore.AST.AssociativeAST.IdentifierNode(className); ProtoCore.AST.AssociativeAST.FunctionDotCallNode dotCallTranslate = ProtoCore.Utils.CoreUtils.GenerateCallDotNode(inode, functionCallTranslate, liveRunner.Core); //============================================== // Build the binary expression //============================================== ProtoCore.AST.AssociativeAST.BinaryExpressionNode stmt2 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("newPoint"), dotCallTranslate, ProtoCore.DSASM.Operator.assign); astList.Add(stmt2); //============================================== // Build a binary expression to retirieve the x property // xval = newPoint.X //============================================== ProtoCore.AST.AssociativeAST.IdentifierListNode identListNode = new ProtoCore.AST.AssociativeAST.IdentifierListNode(); identListNode.LeftNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("newPoint"); identListNode.Optr = ProtoCore.DSASM.Operator.dot; identListNode.RightNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("X"); ProtoCore.AST.AssociativeAST.BinaryExpressionNode stmt3 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("xval"), identListNode, ProtoCore.DSASM.Operator.assign); astList.Add(stmt3); //============================================== // // import ("ProtoGeometry.dll"); // p = Point.Bycoordinates(10.0, 10.0, 10.0); // newPoint = p.Translate(1.0,2.0,3.0); // xval = newPoint.X; // //============================================== // update graph CodeBlockNode cNode = new CodeBlockNode(); cNode.Body = astList; liveRunner.UpdateGraph(cNode); ProtoCore.Mirror.RuntimeMirror mirror = liveRunner.InspectNodeValue("xval"); Assert.IsTrue((double)mirror.GetData().Data == 11.0); }
void TypedIdentifierList(out ProtoCore.AST.AssociativeAST.AssociativeNode node) { Expect(1); node = new IdentifierNode(t.val); while (la.kind == 6) { Get(); ProtoCore.AST.AssociativeAST.AssociativeNode rnode = null; Expect(1); rnode = new IdentifierNode(t.val); ProtoCore.AST.AssociativeAST.IdentifierListNode bnode = new ProtoCore.AST.AssociativeAST.IdentifierListNode(); bnode.LeftNode = node; bnode.Optr = Operator.dot; bnode.RightNode = rnode; node = bnode; NodeUtils.SetNodeLocation(bnode, bnode.LeftNode, bnode.RightNode); } }
internal override IEnumerable<AssociativeNode> BuildAst(List<AssociativeNode> inputAstNodes) { string function = Definition.Name; AssociativeNode rhs; switch (Definition.Type) { case FunctionType.Constructor: case FunctionType.StaticMethod: if (IsPartiallyApplied) { var functionNode = new IdentifierListNode { LeftNode = new IdentifierNode(Definition.ClassName), RightNode = new IdentifierNode(Definition.Name) }; rhs = CreateFunctionObject(functionNode, inputAstNodes); } else { AppendReplicationGuides(inputAstNodes); rhs = AstFactory.BuildFunctionCall( Definition.ClassName, Definition.Name, inputAstNodes); } break; case FunctionType.StaticProperty: var staticProp = new IdentifierListNode { LeftNode = new IdentifierNode(Definition.ClassName), RightNode = new IdentifierNode(Definition.Name) }; rhs = staticProp; break; case FunctionType.InstanceProperty: // Only handle getter here. Setter could be handled in CBN. if (IsPartiallyApplied) { var functionNode = new IdentifierListNode { LeftNode = new IdentifierNode(Definition.ClassName), RightNode = new IdentifierNode(Definition.Name) }; rhs = CreateFunctionObject(functionNode, inputAstNodes); } else { rhs = new NullNode(); if (inputAstNodes != null && inputAstNodes.Count >= 1) { var thisNode = inputAstNodes[0]; if (thisNode != null && !(thisNode is NullNode)) { var insProp = new IdentifierListNode { LeftNode = inputAstNodes[0], RightNode = new IdentifierNode(Definition.Name) }; rhs = insProp; } } } break; case FunctionType.InstanceMethod: if (IsPartiallyApplied) { var functionNode = new IdentifierListNode { LeftNode = new IdentifierNode(Definition.ClassName), RightNode = new IdentifierNode(Definition.Name) }; rhs = CreateFunctionObject(functionNode, inputAstNodes); } else { rhs = new NullNode(); AppendReplicationGuides(inputAstNodes); if (inputAstNodes != null && inputAstNodes.Count >= 1) { var thisNode = inputAstNodes[0]; inputAstNodes.RemoveAt(0); // remove this pointer if (thisNode != null && !(thisNode is NullNode)) { var memberFunc = new IdentifierListNode { LeftNode = thisNode, RightNode = AstFactory.BuildFunctionCall(function, inputAstNodes) }; rhs = memberFunc; } } } break; default: if (IsPartiallyApplied) { var functionNode = new IdentifierNode(function); rhs = CreateFunctionObject(functionNode, inputAstNodes); } else { AppendReplicationGuides(inputAstNodes); rhs = AstFactory.BuildFunctionCall(function, inputAstNodes); } break; } var resultAst = new List<AssociativeNode> { AstFactory.BuildAssignment(AstIdentifierForPreview, rhs) }; if (OutPortData.Count == 1) { var outputIdentiferNode = GetAstIdentifierForOutputIndex(0); string outputIdentifier = outputIdentiferNode.ToString(); string thisIdentifier = AstIdentifierForPreview.ToString(); if (!string.Equals(outputIdentifier, thisIdentifier)) { resultAst.Add(AstFactory.BuildAssignment(outputIdentiferNode, AstIdentifierForPreview)); } } else { var undefinedOutputs = Definition.ReturnKeys == null || !Definition.ReturnKeys.Any(); resultAst.AddRange( Enumerable.Range(0, OutPortData.Count) .Select( outputIdx => undefinedOutputs ? AstIdentifierForPreview : new IdentifierNode(AstIdentifierForPreview) { ArrayDimensions = new ArrayNode { Expr = new StringNode { value = Definition.ReturnKeys.ElementAt(outputIdx) } } })); } return resultAst; }
internal override IEnumerable<AssociativeNode> BuildAst(List<AssociativeNode> inputAstNodes) { var resultAst = new List<AssociativeNode>(); string function = Definition.Name; AssociativeNode rhs; // All inputs are provided, then we should pack all inputs that // belong to variable input parameter into a single array. if (!HasUnconnectedInput()) { var paramCount = Definition.Parameters.Count(); var packId = "__var_arg_pack_" + GUID; resultAst.Add( AstFactory.BuildAssignment( AstFactory.BuildIdentifier(packId), AstFactory.BuildExprList(inputAstNodes.Skip(paramCount - 1).ToList()))); inputAstNodes = inputAstNodes.Take(paramCount - 1) .Concat(new[] { AstFactory.BuildIdentifier(packId) }) .ToList(); } switch (Definition.Type) { case FunctionType.Constructor: case FunctionType.StaticMethod: if (HasUnconnectedInput()) { var functionNode = new IdentifierListNode { LeftNode = new IdentifierNode(Definition.ClassName), RightNode = new IdentifierNode(Definition.Name) }; rhs = CreateFunctionObject(functionNode, inputAstNodes); } else { rhs = AstFactory.BuildFunctionCall(Definition.ClassName, Definition.Name, inputAstNodes); } break; case FunctionType.StaticProperty: var staticProp = new IdentifierListNode { LeftNode = new IdentifierNode(Definition.ClassName), RightNode = new IdentifierNode(Definition.Name) }; rhs = staticProp; break; case FunctionType.InstanceProperty: // Only handle getter here. Setter could be handled in CBN. rhs = new NullNode(); if (inputAstNodes != null && inputAstNodes.Count >= 1) { var thisNode = inputAstNodes[0]; if (thisNode != null && !(thisNode is NullNode)) { var insProp = new IdentifierListNode { LeftNode = inputAstNodes[0], RightNode = new IdentifierNode(Definition.Name) }; rhs = insProp; } } break; case FunctionType.InstanceMethod: rhs = new NullNode(); if (inputAstNodes != null && inputAstNodes.Count >= 1) { var thisNode = inputAstNodes[0]; inputAstNodes.RemoveAt(0); // remove this pointer if (thisNode != null && !(thisNode is NullNode)) { var memberFunc = new IdentifierListNode { LeftNode = thisNode, RightNode = AstFactory.BuildFunctionCall(function, inputAstNodes) }; rhs = memberFunc; } } break; default: if (HasUnconnectedInput()) { var functionNode = new IdentifierNode(function); rhs = CreateFunctionObject(functionNode, inputAstNodes); } else { rhs = AstFactory.BuildFunctionCall(function, inputAstNodes); } break; } resultAst.Add(AstFactory.BuildAssignment(AstIdentifierForPreview, rhs)); if (OutPortData.Count == 1) { var outputIdentiferNode = GetAstIdentifierForOutputIndex(0); string outputIdentifier = outputIdentiferNode.ToString(); string thisIdentifier = AstIdentifierForPreview.ToString(); if (!string.Equals(outputIdentifier, thisIdentifier)) { resultAst.Add( AstFactory.BuildAssignment(outputIdentiferNode, AstIdentifierForPreview)); } } else { var undefinedOutputs = Definition.ReturnKeys == null || !Definition.ReturnKeys.Any(); resultAst.AddRange( Enumerable.Range(0, OutPortData.Count) .Select( outputIdx => undefinedOutputs ? AstIdentifierForPreview : new IdentifierNode(AstIdentifierForPreview) { ArrayDimensions = new ArrayNode { Expr = new StringNode { value = Definition.ReturnKeys.ElementAt( outputIdx) } } })); } return resultAst; }
public IdentifierListNode(IdentifierListNode rhs) : base(rhs) { Optr = rhs.Optr; LeftNode = NodeUtils.Clone(rhs.LeftNode); RightNode = NodeUtils.Clone(rhs.RightNode); isLastSSAIdentListFactor = rhs.isLastSSAIdentListFactor; }
/// <summary> /// Retrieves the string format of the identifier list from left to right, leaving out any symbols after the last identifier. /// Given: A.B() /// Return: "A" /// Given: A.B.C()[0] /// Return: "A.B" /// Given: A.B().C /// Return: "A" /// Given: A.B[0].C /// Return: "A.B[0].C" /// Given: A().B (global function) /// Return: empty string /// Given: A.B[0].C() /// Return: "A.B[0]" /// </summary> /// <param name="identList"></param> /// <returns></returns> public static string GetIdentifierExceptMethodName(IdentifierListNode identList) { Validity.Assert(null != identList); var leftNode = identList.LeftNode; var rightNode = identList.RightNode; var intermediateNodes = new List<AssociativeNode>(); if (!(rightNode is FunctionCallNode)) { intermediateNodes.Insert(0, rightNode); } while (leftNode is IdentifierListNode) { rightNode = ((IdentifierListNode) leftNode).RightNode; if (rightNode is FunctionCallNode) { intermediateNodes.Clear(); } else { intermediateNodes.Insert(0, rightNode); } leftNode = ((IdentifierListNode)leftNode).LeftNode; } if (leftNode is FunctionCallNode) { intermediateNodes.Clear(); return ""; } intermediateNodes.Insert(0, leftNode); return CreateNodeByCombiningIdentifiers(intermediateNodes).ToString(); }
public IdentifierListNode GetIdentList() { IdentifierListNode inode = new IdentifierListNode(); inode.LeftNode = DotCall.FormalArguments[0]; inode.Optr = DSASM.Operator.dot; inode.RightNode = FunctionCall.Function; return inode; }
public static AssociativeNode CreateNodeByCombiningIdentifiers(IList<AssociativeNode> nodeList) { int count = nodeList.Count; if(count == 0) return null; if (count == 1) { return nodeList[0]; } var newIdentList = new IdentifierListNode { LeftNode = nodeList[0], RightNode = nodeList[1], Optr = Operator.dot }; for (var n = 2; n < count; ++n) { var subIdentList = new IdentifierListNode { LeftNode = newIdentList, RightNode = nodeList[n], Optr = Operator.dot }; newIdentList = subIdentList; } return newIdentList; }
// // proc SSAIdentList(node, ssastack, ast) // { // if node is ident // t = SSATemp() // tmpIdent = new Ident(t) // binexpr = new BinaryExpr(tmpIdent, node) // ast.push(binexpr) // ssastack.push(tmpIdent) // else if node is identlist // SSAIdentList(node.left, ssastack, ast) // rhs = new identlist(new Ident(ssastack.pop), node.right) // t = SSATemp() // tmpIdent = new Ident(t) // binexpr = new BinaryExpr(tmpIdent, rhs) // ast.push(binexpr) // ssastack.push(tmpIdent) // end // } // private void SSAIdentList(AssociativeNode node, ref Stack<AssociativeNode> ssaStack, ref List<AssociativeNode> astlist) { if (node is IdentifierNode) { IdentifierNode ident = node as IdentifierNode; if (null == ident.ArrayDimensions) { // Build the temp pointer BinaryExpressionNode bnode = new BinaryExpressionNode(); bnode.Optr = ProtoCore.DSASM.Operator.assign; bnode.isSSAAssignment = true; bnode.isSSAPointerAssignment = true; // Left node var identNode = nodeBuilder.BuildIdentfier(ProtoCore.Utils.CoreUtils.BuildSSATemp(core)); (identNode as IdentifierNode).ReplicationGuides = GetReplicationGuides(ident); bnode.LeftNode = identNode; // Right node bnode.RightNode = ident; astlist.Add(bnode); ssaStack.Push(bnode); } else { EmitSSAArrayIndex(ident, ssaStack, ref astlist, true); } } else if (node is ExprListNode) { //ExprListNode exprList = node as ExprListNode; DFSEmitSSA_AST(node, ssaStack, ref astlist); } else if (node is FunctionCallNode) { FunctionCallNode fcall = node as FunctionCallNode; if (null == fcall.ArrayDimensions) { // Build the temp pointer BinaryExpressionNode bnode = new BinaryExpressionNode(); bnode.Optr = ProtoCore.DSASM.Operator.assign; bnode.isSSAAssignment = true; bnode.isSSAPointerAssignment = true; // Left node var identNode = nodeBuilder.BuildIdentfier(ProtoCore.Utils.CoreUtils.BuildSSATemp(core)); (identNode as IdentifierNode).ReplicationGuides = fcall.ReplicationGuides; bnode.LeftNode = identNode; // Right node bnode.RightNode = fcall; astlist.Add(bnode); ssaStack.Push(bnode); } else { EmitSSAArrayIndex(fcall, ssaStack, ref astlist, true); } } else if (node is IdentifierListNode) { IdentifierListNode identList = node as IdentifierListNode; //Check if the LeftNode for given IdentifierList represents a class. string[] classNames = this.core.ClassTable.GetAllMatchingClasses(identList.LeftNode.ToString()); if (classNames.Length > 1) { string message = string.Format(WarningMessage.kMultipleSymbolFound, identList.LeftNode.ToString(), classNames[0]); for(int i = 1; i < classNames.Length; ++i) message += ", " + classNames[i]; this.core.BuildStatus.LogWarning(WarningID.kMultipleSymbolFound, message); } if(classNames.Length == 1) { var leftNode = nodeBuilder.BuildIdentfier(classNames[0]); SSAIdentList(leftNode, ref ssaStack, ref astlist); } else { // Recursively traversse the left of the ident list SSAIdentList(identList.LeftNode, ref ssaStack, ref astlist); } // Build the rhs identifier list containing the temp pointer IdentifierListNode rhsIdentList = new IdentifierListNode(); rhsIdentList.Optr = Operator.dot; AssociativeNode lhsNode = ssaStack.Pop(); if (lhsNode is BinaryExpressionNode) { rhsIdentList.LeftNode = (lhsNode as BinaryExpressionNode).LeftNode; } else { rhsIdentList.LeftNode = lhsNode; } ArrayNode arrayDimension = null; AssociativeNode rnode = null; if (identList.RightNode is IdentifierNode) { IdentifierNode identNode = identList.RightNode as IdentifierNode; arrayDimension = identNode.ArrayDimensions; rnode = identNode; } else if (identList.RightNode is FunctionCallNode) { FunctionCallNode fcNode = new FunctionCallNode(identList.RightNode as FunctionCallNode); arrayDimension = fcNode.ArrayDimensions; List<AssociativeNode> astlistArgs = new List<AssociativeNode>(); for (int idx = 0; idx < fcNode.FormalArguments.Count; idx++) { AssociativeNode arg = fcNode.FormalArguments[idx]; var replicationGuides = GetReplicationGuides(arg); if (replicationGuides == null) { replicationGuides = new List<AssociativeNode> { }; } else { RemoveReplicationGuides(arg); } DFSEmitSSA_AST(arg, ssaStack, ref astlistArgs); var argNode = ssaStack.Pop(); var argBinaryExpr = argNode as BinaryExpressionNode; if (argBinaryExpr != null) { var newArgNode = NodeUtils.Clone(argBinaryExpr.LeftNode); (newArgNode as IdentifierNode).ReplicationGuides = replicationGuides; fcNode.FormalArguments[idx] = newArgNode; } else { fcNode.FormalArguments[idx] = argNode; } astlist.AddRange(astlistArgs); astlistArgs.Clear(); } astlist.AddRange(astlistArgs); rnode = fcNode; } else { Validity.Assert(false); } Validity.Assert(null != rnode); rhsIdentList.RightNode = rnode; if (null == arrayDimension) { // New SSA expr for the current dot call string ssatemp = ProtoCore.Utils.CoreUtils.BuildSSATemp(core); var tmpIdent = nodeBuilder.BuildIdentfier(ssatemp); BinaryExpressionNode bnode = new BinaryExpressionNode(tmpIdent, rhsIdentList, Operator.assign); bnode.isSSAPointerAssignment = true; astlist.Add(bnode); //ssaStack.Push(tmpIdent); ssaStack.Push(bnode); } else { EmitSSAArrayIndex(rhsIdentList, ssaStack, ref astlist, true); } } }
protected override AssociativeNode GetFunctionApplication(NodeModel model, List<AssociativeNode> inputAstNodes) { AssociativeNode rhs; string function = Definition.Name; switch (Definition.Type) { case FunctionType.Constructor: case FunctionType.StaticMethod: if (model.IsPartiallyApplied) { var functionNode = new IdentifierListNode { LeftNode = new IdentifierNode(Definition.ClassName), RightNode = new IdentifierNode(Definition.Name) }; rhs = CreateFunctionObject(model, functionNode, inputAstNodes); } else { model.AppendReplicationGuides(inputAstNodes); rhs = AstFactory.BuildFunctionCall( Definition.ClassName, Definition.Name, inputAstNodes); } break; case FunctionType.StaticProperty: var staticProp = new IdentifierListNode { LeftNode = new IdentifierNode(Definition.ClassName), RightNode = new IdentifierNode(Definition.Name) }; rhs = staticProp; break; case FunctionType.InstanceProperty: // Only handle getter here. Setter could be handled in CBN. if (model.IsPartiallyApplied) { var functionNode = new IdentifierListNode { LeftNode = new IdentifierNode(Definition.ClassName), RightNode = new IdentifierNode(Definition.Name) }; rhs = CreateFunctionObject(model, functionNode, inputAstNodes); } else { rhs = new NullNode(); if (inputAstNodes != null && inputAstNodes.Count >= 1) { var thisNode = inputAstNodes[0]; if (thisNode != null && !(thisNode is NullNode)) { var insProp = new IdentifierListNode { LeftNode = inputAstNodes[0], RightNode = new IdentifierNode(Definition.Name) }; rhs = insProp; } } } break; case FunctionType.InstanceMethod: if (model.IsPartiallyApplied) { var functionNode = new IdentifierListNode { LeftNode = new IdentifierNode(Definition.ClassName), RightNode = new IdentifierNode(Definition.Name) }; rhs = CreateFunctionObject(model, functionNode, inputAstNodes); } else { rhs = new NullNode(); model.AppendReplicationGuides(inputAstNodes); if (inputAstNodes != null && inputAstNodes.Count >= 1) { var thisNode = inputAstNodes[0]; inputAstNodes.RemoveAt(0); // remove this pointer if (thisNode != null && !(thisNode is NullNode)) { var memberFunc = new IdentifierListNode { LeftNode = thisNode, RightNode = AstFactory.BuildFunctionCall(function, inputAstNodes) }; rhs = memberFunc; } } } break; default: if (model.IsPartiallyApplied) { var functionNode = new IdentifierNode(function); rhs = CreateFunctionObject(model, functionNode, inputAstNodes); } else { model.AppendReplicationGuides(inputAstNodes); rhs = AstFactory.BuildFunctionCall(function, inputAstNodes); } break; } return rhs; }
public void TestBuildAST_01() { //============================================== // // import("ProtoGeometry.dll"); // p = Point.Bycoordinates(0.0, 2.0, 1.0); // xval = p.X; // //============================================== //============================================== // Build the import Nodes //============================================== ProtoScript.Runners.ILiveRunner liveRunner = new ProtoScript.Runners.LiveRunner(); List<string> libs = new List<string>(); libs.Add("ProtoGeometry.dll"); liveRunner.ResetVMAndImportLibrary(libs); string type = "Point"; long hostInstancePtr = 0; string functionName = "ByCoordinates"; List<IntPtr> userDefinedArgs = null; List<string> primitiveArgs = new List<string>(); primitiveArgs.Add("0"); primitiveArgs.Add("2"); primitiveArgs.Add("1"); string formatString = "ddd"; string symbolName = ""; string code = ""; AssociativeNode assign1 = ASTCompilerUtils.BuildAST(type, hostInstancePtr, functionName, userDefinedArgs, primitiveArgs, formatString, liveRunner.Core, ref symbolName, ref code); liveRunner.UpdateGraph(assign1); primitiveArgs.Clear(); primitiveArgs.Add("10"); primitiveArgs.Add("0"); primitiveArgs.Add("0"); functionName = "Translate"; AssociativeNode assign2 = ASTCompilerUtils.BuildAST(symbolName, hostInstancePtr, functionName, userDefinedArgs, primitiveArgs, formatString, liveRunner.Core, ref symbolName, ref code); liveRunner.UpdateGraph(assign2); //============================================== // Build a binary expression to retirieve the x property // xval = p.X; //============================================== List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); ProtoCore.AST.AssociativeAST.IdentifierListNode identListNode = new ProtoCore.AST.AssociativeAST.IdentifierListNode(); identListNode.LeftNode = new ProtoCore.AST.AssociativeAST.IdentifierNode(symbolName); identListNode.Optr = ProtoCore.DSASM.Operator.dot; identListNode.RightNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("X"); ProtoCore.AST.AssociativeAST.BinaryExpressionNode stmt2 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("xval"), identListNode, ProtoCore.DSASM.Operator.assign); astList.Add(stmt2); //============================================== // // import("ProtoGeometry.dll"); // p = Point.Bycoordinates(0.0, 20.0, 1.0); // q = p.Translate(10.0, 0.0, 0.0); // xval = p.X; // //============================================== // update graph CodeBlockNode cNode = new CodeBlockNode(); cNode.Body = astList; liveRunner.UpdateGraph(cNode); ProtoCore.Mirror.RuntimeMirror mirror = liveRunner.InspectNodeValue("xval"); Assert.IsTrue((double)mirror.GetData().Data == 10.0); }
// // proc SSAIdentList(node, ssastack, ast) // { // if node is ident // t = SSATemp() // tmpIdent = new Ident(t) // binexpr = new BinaryExpr(tmpIdent, node) // ast.push(binexpr) // ssastack.push(tmpIdent) // else if node is identlist // SSAIdentList(node.left, ssastack, ast) // rhs = new identlist(new Ident(ssastack.pop), node.right) // t = SSATemp() // tmpIdent = new Ident(t) // binexpr = new BinaryExpr(tmpIdent, rhs) // ast.push(binexpr) // ssastack.push(tmpIdent) // end // } // private void SSAIdentList(AssociativeNode node, ref Stack<AssociativeNode> ssaStack, ref List<AssociativeNode> astlist) { if (node is IdentifierNode) { IdentifierNode ident = node as IdentifierNode; if (null == ident.ArrayDimensions) { // Build the temp pointer BinaryExpressionNode bnode = new BinaryExpressionNode(); bnode.Optr = ProtoCore.DSASM.Operator.assign; bnode.isSSAAssignment = true; bnode.isSSAPointerAssignment = true; // Left node var identNode = nodeBuilder.BuildIdentfier(ProtoCore.Utils.CoreUtils.GetSSATemp(compileStateTracker)); (identNode as IdentifierNode).ReplicationGuides = GetReplicationGuidesFromASTNode(ident); bnode.LeftNode = identNode; // Right node bnode.RightNode = ident; astlist.Add(bnode); ssaStack.Push(bnode); } else { EmitSSAArrayIndex(ident, ssaStack, ref astlist, true); } } else if (node is FunctionCallNode) { FunctionCallNode fcall = node as FunctionCallNode; if (null == fcall.ArrayDimensions) { // Build the temp pointer BinaryExpressionNode bnode = new BinaryExpressionNode(); bnode.Optr = ProtoCore.DSASM.Operator.assign; bnode.isSSAAssignment = true; bnode.isSSAPointerAssignment = true; // Left node var identNode = nodeBuilder.BuildIdentfier(ProtoCore.Utils.CoreUtils.GetSSATemp(compileStateTracker)); (identNode as IdentifierNode).ReplicationGuides = fcall.ReplicationGuides; bnode.LeftNode = identNode; // Right node bnode.RightNode = fcall; astlist.Add(bnode); ssaStack.Push(bnode); } else { EmitSSAArrayIndex(fcall, ssaStack, ref astlist, true); } } else if (node is IdentifierListNode) { IdentifierListNode identList = node as IdentifierListNode; // Recursively traversse the left of the ident list SSAIdentList(identList.LeftNode, ref ssaStack, ref astlist); // Build the rhs identifier list containing the temp pointer IdentifierListNode rhsIdentList = new IdentifierListNode(); rhsIdentList.Optr = Operator.dot; AssociativeNode lhsNode = ssaStack.Pop(); if (lhsNode is BinaryExpressionNode) { rhsIdentList.LeftNode = (lhsNode as BinaryExpressionNode).LeftNode; } else { rhsIdentList.LeftNode = lhsNode; } ArrayNode arrayDimension = null; AssociativeNode rnode = null; if (identList.RightNode is IdentifierNode) { IdentifierNode identNode = identList.RightNode as IdentifierNode; arrayDimension = identNode.ArrayDimensions; rnode = identNode; } else if (identList.RightNode is FunctionCallNode) { FunctionCallNode fCallNode = identList.RightNode as FunctionCallNode; arrayDimension = fCallNode.ArrayDimensions; rnode = fCallNode; } else { Validity.Assert(false); } Validity.Assert(null != rnode); rhsIdentList.RightNode = rnode; if (null == arrayDimension) { // New SSA expr for the current dot call string ssatemp = ProtoCore.Utils.CoreUtils.GetSSATemp(compileStateTracker); var tmpIdent = nodeBuilder.BuildIdentfier(ssatemp); BinaryExpressionNode bnode = new BinaryExpressionNode(tmpIdent, rhsIdentList, Operator.assign); bnode.isSSAPointerAssignment = true; astlist.Add(bnode); //ssaStack.Push(tmpIdent); ssaStack.Push(bnode); } else { EmitSSAArrayIndex(rhsIdentList, ssaStack, ref astlist, true); } } }
void Associative_IdentifierList(out ProtoCore.AST.AssociativeAST.AssociativeNode node) { node = null; if (isInClass && IsIdentList()) { disableKwCheck = true; } Associative_NameReference(out node); disableKwCheck = false; ProtoCore.AST.AssociativeAST.AssociativeNode inode = node; while (la.kind == 6) { Get(); ProtoCore.AST.AssociativeAST.AssociativeNode rnode = null; Associative_NameReference(out rnode); if ((inode is ProtoCore.AST.AssociativeAST.IdentifierNode) && (inode as ProtoCore.AST.AssociativeAST.IdentifierNode).Name == ProtoCore.DSDefinitions.Keyword.This && (rnode is ProtoCore.AST.AssociativeAST.FunctionCallNode)) { node = rnode; return; } ProtoCore.AST.AssociativeAST.IdentifierListNode bnode = new ProtoCore.AST.AssociativeAST.IdentifierListNode(); bnode.LeftNode = node; bnode.Optr = Operator.dot; bnode.RightNode = rnode; node = bnode; NodeUtils.SetNodeLocation(bnode, bnode.LeftNode, bnode.RightNode); } }
private void SSAIdentList(AssociativeNode node, ref Stack<AssociativeNode> ssaStack, ref List<AssociativeNode> astlist) { if (node is IdentifierNode) { IdentifierNode ident = node as IdentifierNode; if (null == ident.ArrayDimensions) { // Build the temp pointer BinaryExpressionNode bnode = new BinaryExpressionNode(); bnode.Optr = ProtoCore.DSASM.Operator.assign; bnode.isSSAAssignment = true; bnode.isSSAPointerAssignment = true; bnode.IsFirstIdentListNode = true; // Left node var identNode =AstFactory.BuildIdentifier(ProtoCore.Utils.CoreUtils.BuildSSATemp(core)); identNode.ReplicationGuides = GetReplicationGuides(ident); identNode.AtLevel = ident.AtLevel; bnode.LeftNode = identNode; // Right node bnode.RightNode = ident; astlist.Add(bnode); ssaStack.Push(bnode); } else { EmitSSAArrayIndex(ident, ssaStack, ref astlist, true); (astlist[0] as BinaryExpressionNode).IsFirstIdentListNode = true; } } else if (node is ExprListNode) { //ExprListNode exprList = node as ExprListNode; DFSEmitSSA_AST(node, ssaStack, ref astlist); } else if (node is FunctionCallNode) { FunctionCallNode fcall = node as FunctionCallNode; if (null == fcall.ArrayDimensions) { // Build the temp pointer BinaryExpressionNode bnode = new BinaryExpressionNode(); bnode.Optr = ProtoCore.DSASM.Operator.assign; bnode.isSSAAssignment = true; bnode.isSSAPointerAssignment = true; bnode.IsFirstIdentListNode = true; // Left node var identNode =AstFactory.BuildIdentifier(ProtoCore.Utils.CoreUtils.BuildSSATemp(core)); identNode.ReplicationGuides = fcall.ReplicationGuides; identNode.AtLevel = fcall.AtLevel; bnode.LeftNode = identNode; // Right node bnode.RightNode = fcall; astlist.Add(bnode); ssaStack.Push(bnode); } else { EmitSSAArrayIndex(fcall, ssaStack, ref astlist, true); (astlist[0] as BinaryExpressionNode).IsFirstIdentListNode = true; } } else if (node is IdentifierListNode) { IdentifierListNode identList = node as IdentifierListNode; // Build the rhs identifier list containing the temp pointer IdentifierListNode rhsIdentList = new IdentifierListNode(); rhsIdentList.Optr = Operator.dot; bool isLeftNodeExprList = false; // Check if identlist matches any namesapce bool resolvedCall = false; string[] classNames = ProtoCore.Utils.CoreUtils.GetResolvedClassName(core.ClassTable, identList); if (classNames.Length == 1) { // // The identlist is a class name and should not be SSA'd // such as: // p = Obj.Create() // var leftNode =AstFactory.BuildIdentifier(classNames[0]); rhsIdentList.LeftNode = leftNode; ProtoCore.Utils.CoreUtils.CopyDebugData(leftNode, node); resolvedCall = true; } else if (classNames.Length > 0) { // There is a resolution conflict // identList resolved to multiple classes // TODO Jun: Move this warning handler to after the SSA transform // http://adsk-oss.myjetbrains.com/youtrack/issue/MAGN-5221 buildStatus.LogSymbolConflictWarning(identList.LeftNode.ToString(), classNames); rhsIdentList = identList; resolvedCall = true; } else { // identList unresolved // Continue to transform this identlist into SSA isLeftNodeExprList = identList.LeftNode is ExprListNode; // Recursively traversse the left of the ident list SSAIdentList(identList.LeftNode, ref ssaStack, ref astlist); AssociativeNode lhsNode = ssaStack.Pop(); if (lhsNode is BinaryExpressionNode) { rhsIdentList.LeftNode = (lhsNode as BinaryExpressionNode).LeftNode; } else { rhsIdentList.LeftNode = lhsNode; } } ArrayNode arrayDimension = null; AssociativeNode rnode = null; if (identList.RightNode is IdentifierNode) { IdentifierNode identNode = identList.RightNode as IdentifierNode; arrayDimension = identNode.ArrayDimensions; rnode = identNode; } else if (identList.RightNode is FunctionCallNode) { FunctionCallNode fcNode = new FunctionCallNode(identList.RightNode as FunctionCallNode); arrayDimension = fcNode.ArrayDimensions; List<AssociativeNode> astlistArgs = new List<AssociativeNode>(); for (int idx = 0; idx < fcNode.FormalArguments.Count; idx++) { AssociativeNode arg = fcNode.FormalArguments[idx]; var replicationGuides = GetReplicationGuides(arg); if (replicationGuides == null) { replicationGuides = new List<AssociativeNode> { }; } else { RemoveReplicationGuides(arg); } var atLevel = GetAtLevel(arg); if (atLevel != null) { RemoveAtLevel(arg); } DFSEmitSSA_AST(arg, ssaStack, ref astlistArgs); var argNode = ssaStack.Pop(); var argBinaryExpr = argNode as BinaryExpressionNode; if (argBinaryExpr != null) { var newArgNode = NodeUtils.Clone(argBinaryExpr.LeftNode) as IdentifierNode; newArgNode.ReplicationGuides = replicationGuides; newArgNode.AtLevel = atLevel; fcNode.FormalArguments[idx] = newArgNode; } else { fcNode.FormalArguments[idx] = argNode; } astlist.AddRange(astlistArgs); astlistArgs.Clear(); } astlist.AddRange(astlistArgs); rnode = fcNode; } else { Validity.Assert(false); } Validity.Assert(null != rnode); rhsIdentList.RightNode = rnode; if (null == arrayDimension) { // New SSA expr for the current dot call string ssatemp = ProtoCore.Utils.CoreUtils.BuildSSATemp(core); var tmpIdent =AstFactory.BuildIdentifier(ssatemp); BinaryExpressionNode bnode = new BinaryExpressionNode(tmpIdent, rhsIdentList, Operator.assign); bnode.isSSAPointerAssignment = true; if (resolvedCall || isLeftNodeExprList ) { bnode.IsFirstIdentListNode = true; } astlist.Add(bnode); ssaStack.Push(bnode); } else { List<AssociativeNode> ssaAstList = new List<AssociativeNode>(); EmitSSAArrayIndex(rhsIdentList, ssaStack, ref ssaAstList, true); if (resolvedCall || isLeftNodeExprList) { (ssaAstList[0] as BinaryExpressionNode).IsFirstIdentListNode = true; } astlist.AddRange(ssaAstList); } } }
public override AssociativeNode VisitIdentifierListNode(IdentifierListNode node) { node.LeftNode = node.LeftNode.Accept(this); var rightNode = node.RightNode; while (rightNode != null) { if (rightNode is FunctionCallNode) { var funcCall = rightNode as FunctionCallNode; funcCall.FormalArguments = VisitNodeList(funcCall.FormalArguments); if (funcCall.ArrayDimensions != null) { funcCall.ArrayDimensions = funcCall.ArrayDimensions.Accept(this) as ArrayNode; } break; } else if (rightNode is IdentifierListNode) { rightNode = (rightNode as IdentifierListNode).RightNode; } else { break; } } return node; }
private void TraverseAndAppendThisPtrArg(AssociativeNode node, ref AssociativeNode newIdentList) { if (node is BinaryExpressionNode) { TraverseAndAppendThisPtrArg((node as BinaryExpressionNode).LeftNode, ref newIdentList); TraverseAndAppendThisPtrArg((node as BinaryExpressionNode).RightNode, ref newIdentList); } else if (node is IdentifierListNode) { // Travere until the left most identifier IdentifierListNode binaryNode = node as IdentifierListNode; while ((binaryNode as IdentifierListNode).LeftNode is IdentifierListNode) { binaryNode = (binaryNode as IdentifierListNode).LeftNode as IdentifierListNode; } IdentifierNode identNode = null; if ((binaryNode as IdentifierListNode).LeftNode is IdentifierNode) { identNode = (binaryNode as IdentifierListNode).LeftNode as IdentifierNode; } Validity.Assert(identNode is IdentifierNode); string leftMostSymbolName = identNode.Name; // Is it a this pointer? if (leftMostSymbolName.Equals(ProtoCore.DSDefinitions.Keyword.This)) { // Then just replace it identNode.Name = identNode.Value = ProtoCore.DSASM.Constants.kThisPointerArgName; newIdentList = node; return; } // Is the left most symbol a member? SymbolNode symbolnode; bool isAccessible = false; bool isAllocated = VerifyAllocation(leftMostSymbolName, globalClassIndex, ProtoCore.DSASM.Constants.kGlobalScope, out symbolnode, out isAccessible); if (!isAllocated) { newIdentList = node; return; } IdentifierListNode identList = new IdentifierListNode(); IdentifierNode lhsThisArg = new IdentifierNode(); lhsThisArg.Name = ProtoCore.DSASM.Constants.kThisPointerArgName; lhsThisArg.Value = ProtoCore.DSASM.Constants.kThisPointerArgName; identList.LeftNode = lhsThisArg; identList.Optr = Operator.dot; identList.RightNode = identNode; // Update the last binary dot node with the new dot node binaryNode.LeftNode = identList; newIdentList = binaryNode; } else if (node is FunctionCallNode) { FunctionCallNode fCall = node as FunctionCallNode; for(int n = 0; n < fCall.FormalArguments.Count; ++n) { AssociativeNode argNode = fCall.FormalArguments[n]; TraverseAndAppendThisPtrArg(argNode, ref argNode); fCall.FormalArguments[n] = argNode; } newIdentList = fCall; } else if (node is FunctionDotCallNode) { FunctionDotCallNode dotCall = node as FunctionDotCallNode; string name = dotCall.DotCall.FormalArguments[0].Name; // TODO Jun: If its a constructor, leave it as it is. // After the first version of global instance functioni s implemented, 2nd phase would be to remove dotarg methods completely bool isConstructorCall = false; if (null != name) { isConstructorCall = ProtoCore.DSASM.Constants.kInvalidIndex != core.ClassTable.IndexOf(name); } FunctionCallNode fCall = (node as FunctionDotCallNode).FunctionCall; if (isConstructorCall) { newIdentList = node; } else { for (int n = 0; n < fCall.FormalArguments.Count; ++n) { AssociativeNode argNode = fCall.FormalArguments[n]; TraverseAndAppendThisPtrArg(argNode, ref argNode); fCall.FormalArguments[n] = argNode; } newIdentList = node; } } else if (node is IdentifierNode) { string identName = (node as IdentifierNode).Name; if (identName.Equals(ProtoCore.DSDefinitions.Keyword.Return)) { newIdentList = node; return; } if (ProtoCore.DSASM.Constants.kInvalidIndex == globalClassIndex) { newIdentList = node; return; } // Temp are not member vars if (ProtoCore.Utils.CoreUtils.IsAutoGeneratedVar(identName)) { newIdentList = node; return; } // Is it a member SymbolNode symbolnode; bool isAccessible = false; bool isAllocated = VerifyAllocation(identName, globalClassIndex, ProtoCore.DSASM.Constants.kGlobalScope, out symbolnode, out isAccessible); if (!isAllocated) { newIdentList = node; return; } IdentifierListNode identList = new IdentifierListNode(); IdentifierNode lhsThisArg = new IdentifierNode(); lhsThisArg.Name = ProtoCore.DSASM.Constants.kThisPointerArgName; lhsThisArg.Value = ProtoCore.DSASM.Constants.kThisPointerArgName; identList.LeftNode = lhsThisArg; identList.Optr = Operator.dot; identList.RightNode = node; newIdentList = identList; } else { newIdentList = node; } }
public void DFSTraverse(ref AST.AssociativeAST.AssociativeNode node) { if (node is AST.AssociativeAST.IdentifierNode) { EmitIdentifierNode(ref node); } else if (node is ProtoCore.AST.AssociativeAST.IdentifierListNode) { AST.AssociativeAST.IdentifierListNode identList = node as ProtoCore.AST.AssociativeAST.IdentifierListNode; EmitIdentifierListNode(ref identList); } else if (node is ProtoCore.AST.AssociativeAST.IntNode) { AST.AssociativeAST.IntNode intNode = node as ProtoCore.AST.AssociativeAST.IntNode; EmitIntNode(ref intNode); } else if (node is ProtoCore.AST.AssociativeAST.DoubleNode) { AST.AssociativeAST.DoubleNode doubleNode = node as ProtoCore.AST.AssociativeAST.DoubleNode; EmitDoubleNode(ref doubleNode); } else if (node is ProtoCore.AST.AssociativeAST.FunctionCallNode) { AST.AssociativeAST.FunctionCallNode funcCallNode = node as ProtoCore.AST.AssociativeAST.FunctionCallNode; EmitFunctionCallNode(ref funcCallNode); } else if (node is ProtoCore.AST.AssociativeAST.FunctionDotCallNode) { 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 != DSASM.Operator.assign) { ; } //EmitCode("("); EmitBinaryNode(ref binaryExpr); if (binaryExpr.Optr == DSASM.Operator.assign) { //EmitCode(ProtoCore.DSASM.Constants.termline); } if (binaryExpr.Optr != DSASM.Operator.assign) { ; } //EmitCode(")"); } else if (node is ProtoCore.AST.AssociativeAST.FunctionDefinitionNode) { AST.AssociativeAST.FunctionDefinitionNode funcDefNode = node as ProtoCore.AST.AssociativeAST.FunctionDefinitionNode; EmitFunctionDefNode(ref funcDefNode); } else if (node is ProtoCore.AST.AssociativeAST.ClassDeclNode) { AST.AssociativeAST.ClassDeclNode classDeclNode = node as ProtoCore.AST.AssociativeAST.ClassDeclNode; EmitClassDeclNode(ref classDeclNode); } else if (node is ProtoCore.AST.AssociativeAST.NullNode) { AST.AssociativeAST.NullNode nullNode = node as ProtoCore.AST.AssociativeAST.NullNode; EmitNullNode(ref nullNode); } else if (node is ProtoCore.AST.AssociativeAST.ArrayIndexerNode) { AST.AssociativeAST.ArrayIndexerNode arrIdxNode = node as ProtoCore.AST.AssociativeAST.ArrayIndexerNode; EmitArrayIndexerNode(ref arrIdxNode); } else if (node is ProtoCore.AST.AssociativeAST.ExprListNode) { AST.AssociativeAST.ExprListNode exprListNode = node as ProtoCore.AST.AssociativeAST.ExprListNode; EmitExprListNode(ref exprListNode); } }