Exemplo n.º 1
0
	void Associative_Number(out ProtoCore.AST.AssociativeAST.AssociativeNode node) {
		node = null; 
		int sign = 1;
		int line = ProtoCore.DSASM.Constants.kInvalidIndex; 
		int col = ProtoCore.DSASM.Constants.kInvalidIndex; 
		
		if (la.kind == 15) {
			Get();
			sign = -1; 
			line = t.line; 
			col = t.col; 
			
		}
		if (la.kind == 2) {
			Get();
			Int64 value;
			if (Int64.TryParse(t.val, System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, out value))
			{
			   node = new ProtoCore.AST.AssociativeAST.IntNode(value * sign);
			}
			else
			{
			   node = new ProtoCore.AST.AssociativeAST.NullNode();
			}
			
			if (ProtoCore.DSASM.Constants.kInvalidIndex == line
			   &&  ProtoCore.DSASM.Constants.kInvalidIndex == col)
			{
			   NodeUtils.SetNodeLocation(node, t);
			}
			else
			{
			   node.line = line; node.col = col;
			node.endLine = t.line; node.endCol = t.col;
			}
			
		} else if (la.kind == 3) {
			Get();
			double value;
			if (Double.TryParse(t.val, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out value))
			{
			   node = new ProtoCore.AST.AssociativeAST.DoubleNode(value * sign);
			}
			else
			{
			   node = new ProtoCore.AST.AssociativeAST.NullNode();
			}
			
			if (ProtoCore.DSASM.Constants.kInvalidIndex == line
			   &&  ProtoCore.DSASM.Constants.kInvalidIndex == col)
			{
			   NodeUtils.SetNodeLocation(node, t);
			}
			else
			{
			   node.line = line; node.col = col;
			}
			
		} else SynErr(98);
	}
Exemplo n.º 2
0
        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;
        }
        private void EmitFunctionNode(Func node, out AssociativeNode outnode)
        {
            Validity.Assert(node != null);

            AssociativeNode fNode = new NullNode();
            string funcQualifier = node.Name;
            if (node.isRange)
            {
                EmitRangeExpNode(node, out fNode);
            }
            else if (!funcQualifier.Contains("."))
            {
                if (node.isProperty)
                {
                    Dictionary<int, Node> nodes = node.GetChildrenWithIndices();
                    Validity.Assert(nodes.Count == 1);
                    for (int i = 0; i < nodes.Count; ++i)
                    {
                        AssociativeNode instanceNode = null;
                        DFSTraverse(nodes[i], out instanceNode);
                        
                        EmitFunctionCallNode(node, out fNode);
                        ((fNode as FunctionCallNode).Function as IdentifierNode).Value = ProtoCore.DSASM.Constants.kGetterPrefix + ((fNode as FunctionCallNode).Function as IdentifierNode).Value;
                        //string className = (node.Name.Split('.'))[0];
                        //IdentifierNode inode = new IdentifierNode(className);

                        fNode = ProtoCore.Utils.CoreUtils.GenerateCallDotNode(instanceNode, fNode as FunctionCallNode);
                    }
                }
                else if (node.isMemberFunction)
                {
                    Dictionary<int, Node> nodes = node.GetChildrenWithIndices();                    
                    
                    AssociativeNode instanceNode = null;
                    DFSTraverse(nodes[0], out instanceNode);

                    EmitFunctionCallNode(node, out fNode);
                    //string className = (node.Name.Split('.'))[0];
                    //IdentifierNode inode = new IdentifierNode(className);

                    fNode = ProtoCore.Utils.CoreUtils.GenerateCallDotNode(instanceNode, fNode as FunctionCallNode);                    
                }
                else
                {
                    // Create AssociativeAST.FunctionCallNode for global and built-in functions
                    EmitFunctionCallNode(node, out fNode);
                }
                
            }
            else
            {
                // Create FunctionDotCallNode for ctors, static and instance methods
                EmitFunctionDotCallNode(node, out fNode);
            }

            BinaryExpressionNode assignmentNode = new BinaryExpressionNode();
            assignmentNode.LeftNode = new IdentifierNode(node.tempName);
            assignmentNode.Optr = ProtoCore.DSASM.Operator.assign;
            assignmentNode.RightNode = fNode;
            assignmentNode.Guid = node.Guid;

            Validity.Assert(gc != null);
            gc.HandleNewNode(assignmentNode);

            outnode = assignmentNode;
        }
Exemplo n.º 4
0
	void Associative_Factor(out ProtoCore.AST.AssociativeAST.AssociativeNode node) {
		node = null; 
		if (IsNumber()) {
			Associative_Number(out node);
		} else if (la.kind == 41) {
			Get();
			node = new ProtoCore.AST.AssociativeAST.BooleanNode(true);
			NodeUtils.SetNodeLocation(node, t);
			
		} else if (la.kind == 42) {
			Get();
			node = new ProtoCore.AST.AssociativeAST.BooleanNode(false);
			NodeUtils.SetNodeLocation(node, t);
			
		} else if (la.kind == 43) {
			Get();
			node = new ProtoCore.AST.AssociativeAST.NullNode();
			NodeUtils.SetNodeLocation(node, t);
			
		} else if (la.kind == 5) {
			Associative_Char(out node);
		} else if (la.kind == 4) {
			Associative_String(out node);
		} else if (la.kind == 1 || la.kind == 12 || la.kind == 45) {
			Associative_IdentifierList(out node);
		} else if (StartOf(20)) {
			Associative_UnaryExpression(out node);
		} else SynErr(95);
	}
        private void EmitBinaryExpNode(Operator node, out AssociativeNode outnode)
        {
            Validity.Assert(node != null);

            Dictionary<int, Node> nodes = node.GetChildrenWithIndices();
            Validity.Assert(nodes.Count <= 2);

            BinaryExpressionNode expressionNode = new BinaryExpressionNode();

            // Create operator from input node
            switch (node.Name)
            {
                case "=":
                    expressionNode.Optr = ProtoCore.DSASM.Operator.assign;
                    break;
                case "+":
                    expressionNode.Optr = ProtoCore.DSASM.Operator.add;
                    break;
                case "-":
                    expressionNode.Optr = ProtoCore.DSASM.Operator.sub;
                    break;
                case "*":
                    expressionNode.Optr = ProtoCore.DSASM.Operator.mul;
                    break;
                case "/":
                    expressionNode.Optr = ProtoCore.DSASM.Operator.div;
                    break;
                case "%":
                    expressionNode.Optr = ProtoCore.DSASM.Operator.mod;
                    break;
                case "==":
                    expressionNode.Optr = ProtoCore.DSASM.Operator.eq;
                    break;
                case "!=":
                    expressionNode.Optr = ProtoCore.DSASM.Operator.nq;
                    break;
                case ">=":
                    expressionNode.Optr = ProtoCore.DSASM.Operator.ge;
                    break;
                case ">":
                    expressionNode.Optr = ProtoCore.DSASM.Operator.gt;
                    break;
                case "<=":
                    expressionNode.Optr = ProtoCore.DSASM.Operator.le;
                    break;
                case "<":
                    expressionNode.Optr = ProtoCore.DSASM.Operator.lt;
                    break;
                case "&&":
                    expressionNode.Optr = ProtoCore.DSASM.Operator.and;
                    break;
                case "||":
                    expressionNode.Optr = ProtoCore.DSASM.Operator.or;
                    break;
                case "&":
                    expressionNode.Optr = ProtoCore.DSASM.Operator.bitwiseand;
                    break;
                case "|":
                    expressionNode.Optr = ProtoCore.DSASM.Operator.bitwiseor;
                    break;
                case "^":
                    expressionNode.Optr = ProtoCore.DSASM.Operator.bitwisexor;
                    break;
                default: break;
            }

            AssociativeNode identNode1 = new NullNode();
            AssociativeNode identNode2 = new NullNode();

            if (nodes.Count == 2)
            {
                // Create BinaryExpressionNode from identNode1, identNode2 and operator                
                DFSTraverse(nodes[0], out identNode1);
                DFSTraverse(nodes[1], out identNode2);
                
            }
            else if (nodes.Count == 1)
            {
                // Create BinaryExpressionNode from identNode1, null 
                DFSTraverse(nodes[0], out identNode1);
            }
            expressionNode.LeftNode = identNode1;
            expressionNode.RightNode = identNode2;

            //(AstRootNode as CodeBlockNode).Body.Add(expressionNode);
            BinaryExpressionNode assignmentNode = new BinaryExpressionNode();
            assignmentNode.LeftNode = new IdentifierNode(node.tempName);
            assignmentNode.Optr = ProtoCore.DSASM.Operator.assign;
            assignmentNode.RightNode = expressionNode;
            assignmentNode.Guid = node.Guid;

            Validity.Assert(gc != null);
            gc.HandleNewNode(assignmentNode);

            outnode = assignmentNode;
        }
Exemplo n.º 6
0
        void Associative_Factor(out ProtoCore.AST.AssociativeAST.AssociativeNode node)
        {
            node = null;
            if (IsNumber()) {
            Associative_Number(out node);
            } else if (la.kind == 40) {
            Get();
            node = new ProtoCore.AST.AssociativeAST.BooleanNode() { value = ProtoCore.DSASM.Literal.True };
            NodeUtils.SetNodeLocation(node, t);

            } else if (la.kind == 41) {
            Get();
            node = new ProtoCore.AST.AssociativeAST.BooleanNode() { value = ProtoCore.DSASM.Literal.False };
            NodeUtils.SetNodeLocation(node, t);

            } else if (la.kind == 42) {
            Get();
            node = new ProtoCore.AST.AssociativeAST.NullNode();
            NodeUtils.SetNodeLocation(node, t);

            } else if (la.kind == 5) {
            Associative_Char(out node);
            } else if (la.kind == 4) {
            Associative_String(out node);
            } else if (la.kind == 1 || la.kind == 9 || la.kind == 44) {
            Associative_IdentifierList(out node);
            } else if (StartOf(21)) {
            Associative_UnaryExpression(out node);
            } else SynErr(97);
        }
Exemplo n.º 7
0
        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;
        }
Exemplo n.º 8
0
        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;
        }
Exemplo n.º 9
0
	void Associative_Number(out ProtoCore.AST.AssociativeAST.AssociativeNode node) {
		node = null; 
		int sign = 1;
		int line = ProtoCore.DSASM.Constants.kInvalidIndex; 
		int col = ProtoCore.DSASM.Constants.kInvalidIndex; 
		
		if (la.kind == 13) {
			Get();
			sign = -1; 
			line = t.line; 
			col = t.col; 
			
		}
		if (la.kind == 2) {
			Get();
			Int64 value;
			if (Int64.TryParse(t.val, out value))
			{
			   node = new ProtoCore.AST.AssociativeAST.IntNode(value * sign);
			}
			else
			{
			   node = new ProtoCore.AST.AssociativeAST.NullNode();
			}
			
			if (ProtoCore.DSASM.Constants.kInvalidIndex == line
			   &&  ProtoCore.DSASM.Constants.kInvalidIndex == col)
			{
			   NodeUtils.SetNodeLocation(node, t);
			}
			else
			{
			   node.line = line; node.col = col;
			}
			
		} else if (la.kind == 3) {
			Get();
			double value;
			if (Double.TryParse(t.val, out value))
			{
			   node = new ProtoCore.AST.AssociativeAST.DoubleNode(value * sign);
			}
			else
			{
			   node = new ProtoCore.AST.AssociativeAST.NullNode();
			}
			
			if (ProtoCore.DSASM.Constants.kInvalidIndex == line
			   &&  ProtoCore.DSASM.Constants.kInvalidIndex == col)
			{
			   NodeUtils.SetNodeLocation(node, t);
			}
			else
			{
			   node.line = line; node.col = col;
			}
			
		} else SynErr(104);
	}