コード例 #1
0
        public void GenerateCode(InvokeNode node, ICIL_CodeGenerator codeGenerator)
        {
            node.Holder = codeGenerator.DefineVariable();
            codeGenerator.AddLocalVariable(
                new CIL_LocalVariable((Variable)node.Holder));

            #region GenerateCode SelfNode

            var self = codeGenerator.DefineVariable();
            codeGenerator.AddLocalVariable(
                new CIL_LocalVariable(self));

            var instance = node.OwnerType.Self.Holder;

            codeGenerator.AddInstruction(
                new Assign(self, instance));

            #endregion

            if (node.Parameters != null)
            {
                GenerateCode(node.Parameters, codeGenerator);
            }

            codeGenerator.AddInstruction(
                new Param(self));

            codeGenerator.AddInstruction(
                new Call((Variable)node.Holder, $"{node.Method.ParentScope}_{node.FuncName}"));
        }
コード例 #2
0
        public void CheckSemantic(InvokeNode node, IScope scope = null)
        {
            var copyScope = scope;

            while (!(copyScope is IType))
            {
                copyScope = copyScope?.ParentScope;
            }
            var type = (IType)copyScope;

            #region Ready to use for Code Generation

            node.OwnerType = type;

            #endregion

            if (!type.IsDefinedMethod(node.FuncName, out var method))
            {
                Logger.LogError(node.Line, node.CharPositionInLine,
                                $"The name '{node.FuncName}' does not exist in the {type}'s context");
                return;
            }
            node.Method = method;

            //chek arguments
            if (node.Parameters != null)
            {
                if (node.Parameters.Children.Count == method.Arguments.Length)
                {
                    var i = 0;
                    foreach (var parameter in node.Parameters.Expressions)
                    {
                        CheckSemantic(parameter, scope);

                        if (!((CoolType)method.Arguments[i].Type <= (CoolType)parameter.ComputedType))
                        {
                            Logger.LogError(parameter.Line, parameter.CharPositionInLine,
                                            $"Argument '{i}': cannot convert from '{parameter.ComputedType}' to {method.Arguments[i].Type}");
                        }
                        i++;
                    }
                }
                else
                {
                    Logger.LogError(node.Line, node.CharPositionInLine,
                                    $"No overload for method '{method.Name}' takes '{node.Parameters.Children.Count}' arguments");
                }
            }
            else if (method.Arguments.Length > 0)
            {
                Logger.LogError(node.Line, node.CharPositionInLine,
                                $"No overload for method '{method.Name}' takes '0' arguments");
            }

            node.ComputedType = method.ReturnType;
        }
コード例 #3
0
 public virtual Value evaluate(Context cx, InvokeNode node)
 {
     output("<InvokeNode position=\"" + node.pos() + "\">");
     indent_Renamed_Field++;
     if (node.args != null)
     {
         node.args.evaluate(cx, this);
     }
     indent_Renamed_Field--;
     output("</InvokeNode>");
     return(null);
 }
コード例 #4
0
    public void Parse_InvokeAccessExpression_1Arg()
    {
        ASTNode root = ExpressionParser.Parse("someProperty(1)");

        Assert.IsInstanceOf <MemberAccessExpressionNode>(root);
        MemberAccessExpressionNode node = (MemberAccessExpressionNode)root;

        Assert.AreEqual(1, node.parts.Count);
        Assert.IsInstanceOf <InvokeNode>(node.parts[0]);
        InvokeNode invokeNode = (InvokeNode)node.parts[0];

        Assert.AreEqual(1, invokeNode.parameters.Count);
        Assert.IsInstanceOf <LiteralNode>(invokeNode.parameters[0]);
    }
コード例 #5
0
ファイル: InvokeNodeTests.cs プロジェクト: ajlopez/ScalaSharp
        public void CreateInvokeNode()
        {
            IList <INode> arguments = new List <INode>()
            {
                new ConstantNode(42)
            };
            InvokeNode node = new InvokeNode(new NameNode("append"), arguments);

            node.RegisterInContext(null);
            Assert.AreSame(arguments, node.Arguments);
            Assert.IsNotNull(node.Target);
            Assert.IsInstanceOfType(node.Target, typeof(NameNode));
            Assert.AreEqual("append", ((NameNode)node.Target).Name);
        }
コード例 #6
0
    public void Parse_InvokeAccessExpression_2Arg_Chained()
    {
        ASTNode root = ExpressionParser.Parse("someProperty(1, something)()");

        Assert.IsInstanceOf <MemberAccessExpressionNode>(root);
        MemberAccessExpressionNode node = (MemberAccessExpressionNode)root;

        Assert.AreEqual(2, node.parts.Count);
        Assert.IsInstanceOf <InvokeNode>(node.parts[0]);
        Assert.IsInstanceOf <InvokeNode>(node.parts[1]);
        InvokeNode invokeNode = (InvokeNode)node.parts[0];

        Assert.AreEqual(2, invokeNode.parameters.Count);
        Assert.IsInstanceOf <LiteralNode>(invokeNode.parameters[0]);
        Assert.IsInstanceOf <IdentifierNode>(invokeNode.parameters[1]);
    }
コード例 #7
0
ファイル: InvokeNodeTests.cs プロジェクト: ajlopez/ScalaSharp
        public void CheckType()
        {
            Context context = new Context();
            DefNode defnode = new DefNode("append", null, TypeInfo.Int, null);

            defnode.RegisterInContext(context);
            IList <INode> arguments = new List <INode>()
            {
                new ConstantNode(42)
            };
            InvokeNode node = new InvokeNode(new NameNode("append"), arguments);

            Assert.IsNull(node.TypeInfo);
            node.CheckType(context);
            Assert.AreEqual(TypeInfo.Int, node.TypeInfo);
        }
コード例 #8
0
        public AstNode VisitInvoke(InvokeNode n)
        {
            Visit(n.Instance);
            Append("(");
            if (!n.Arguments.IsNullOrEmpty())
            {
                Visit(n.Arguments[0]);
                for (var i = 1; i < n.Arguments.Count; i++)
                {
                    Append(", ");
                    Visit(n.Arguments[i]);
                }
            }

            Append(")");
            return(n);
        }
コード例 #9
0
		public virtual Value evaluate(Context cx, InvokeNode node)
		{
			output("<InvokeNode position=\"" + node.pos() + "\">");
			indent_Renamed_Field++;
			if (node.args != null)
			{
				node.args.evaluate(cx, this);
			}
			indent_Renamed_Field--;
			output("</InvokeNode>");
			return null;
		}
コード例 #10
0
 public virtual AstNode VisitInvoke(InvokeNode n)
 {
     Visit(n.Instance);
     Visit(n.Arguments);
     return(n);
 }
コード例 #11
0
        private INode ParseSimpleNode()
        {
            var token = this.NextToken();

            if (token == null)
            {
                return(null);
            }

            if (token.Type == TokenType.Name && token.Value == "class")
            {
                return(this.ParseClassNode());
            }

            if (token.Type == TokenType.Name && token.Value == "object")
            {
                return(this.ParseObjectNode());
            }

            if (token.Type == TokenType.Name && token.Value == "val")
            {
                return(this.ParseValNode());
            }

            if (token.Type == TokenType.Name && token.Value == "var")
            {
                return(this.ParseVarNode());
            }

            if (token.Type == TokenType.Name && token.Value == "def")
            {
                return(this.ParseDefNode());
            }

            this.PushToken(token);

            INode node = this.ParseExpressionNode();

            if (node == null)
            {
                return(null);
            }

            while (true)
            {
                INode newnode = node;

                while (this.TryParseToken(TokenType.Delimiter, "."))
                {
                    newnode = new DotNameNode(newnode, this.ParseName());
                }

                while (this.TryParseToken(TokenType.Delimiter, "("))
                {
                    IList <INode> arguments = new List <INode>();

                    while (!this.TryParseToken(TokenType.Delimiter, ")"))
                    {
                        if (arguments.Count > 0)
                        {
                            this.ParseToken(TokenType.Delimiter, ",");
                        }

                        arguments.Add(this.ParseExpressionNode());
                    }

                    if (newnode is DotNameNode)
                    {
                        newnode = new InvokeMethodNode(((DotNameNode)newnode).Target, ((DotNameNode)newnode).Name, arguments);
                    }
                    else
                    {
                        newnode = new InvokeNode(newnode, arguments);
                    }
                }

                if (node == newnode)
                {
                    break;
                }

                node = newnode;
            }

            return(node);
        }