コード例 #1
0
ファイル: Compiler.cs プロジェクト: robotii/sophielang
        private static void super_(Compiler c, bool allowAssignment)
        {
            ClassCompiler enclosingClass = c.GetEnclosingClass();

            if (enclosingClass == null)
            {
                c.Error("Cannot use 'super' outside of a method.");
            }
            else if (enclosingClass.IsStaticMethod)
            {
                c.Error("Cannot use 'super' in a static method.");
            }

            c.LoadThis();

            // TODO: Super operator calls.

            // See if it's a named super call, or an unnamed one.
            if (c.Match(TokenType.Dot))
            {
                // Compile the superclass call.
                c.Consume(TokenType.Name, "Expect method name after 'super.'.");
                c.NamedCall(allowAssignment, Instruction.Super0);
            }
            else if (enclosingClass != null)
            {
                // No explicit name, so use the name of the enclosing method. Make sure we
                // check that enclosingClass isn't null first. We've already reported the
                // error, but we don't want to crash here.
                c.MethodCall(Instruction.Super0, enclosingClass.MethodName, enclosingClass.MethodLength);
            }
        }
コード例 #2
0
ファイル: Compiler.cs プロジェクト: robotii/sophielang
        private static void new_(Compiler c, bool allowAssignment)
        {
            // Allow a dotted name after 'new'.
            c.Consume(TokenType.Name, "Expect name after 'new'.");
            Name(c, false);
            while (c.Match(TokenType.Dot))
            {
                Call(c, false);
            }

            // The angle brackets in the name are to ensure users can't call it directly.
            c.CallMethod(0, "<instantiate>");

            // Invoke the constructor on the new instance.
            c.MethodCall(Instruction.Call0, "new", 3);
        }