// Compiles an (optional) argument list and then calls it. private void MethodCall(Instruction instruction, string name, int length) { Signature signature = new Signature { Type = SignatureType.Getter, Arity = 0, Name = name, Length = length }; // Parse the argument list, if any. if (Match(TokenType.LeftParen)) { signature.Type = SignatureType.Method; // Allow empty an argument list. if (Peek() != TokenType.RightParen) { FinishArgumentList(signature); } Consume(TokenType.RightParen, "Expect ')' after arguments."); } // Parse the block argument, if any. if (Match(TokenType.LeftBrace)) { // Include the block argument in the arity. signature.Type = SignatureType.Method; signature.Arity++; Compiler fnCompiler = new Compiler(_parser, this, true); // Make a dummy signature to track the arity. Signature fnSignature = new Signature { Arity = 0 }; // Parse the parameter list, if any. if (Match(TokenType.Pipe)) { fnCompiler.FinishParameterList(fnSignature); Consume(TokenType.Pipe, "Expect '|' after function parameters."); } fnCompiler._numParams = fnSignature.Arity; fnCompiler.FinishBody(false); // TODO: Use the name of the method the block is being provided to. fnCompiler.EndCompiler(); } // TODO: Allow Grace-style mixfix methods? CallSignature(instruction, signature); }
// Compiles a method definition inside a class body. Returns the symbol in the // method table for the new method. private int Method(ClassCompiler classCompiler, bool isConstructor, SignatureFn signatureFn) { // Build the method signature. Signature signature = new Signature(); SignatureFromToken(signature); classCompiler.MethodName = signature.Name; classCompiler.MethodLength = signature.Length; Compiler methodCompiler = new Compiler(_parser, this, false); // Compile the method signature. signatureFn(methodCompiler, signature); // Include the full signature in debug messages in stack traces. Consume(TokenType.LeftBrace, "Expect '{' to begin method body."); methodCompiler.FinishBody(isConstructor); methodCompiler.EndCompiler(); return SignatureSymbol(signature); }