private NamedCall ( bool allowAssignment, Instruction instruction ) : void | ||
allowAssignment | bool | |
instruction | Instruction | |
return | void |
private static void super_(Compiler c, bool allowAssignment) { ClassCompiler enclosingClass = c.GetEnclosingClass(); if (enclosingClass == null) { c.Error("Cannot use 'super' outside of a 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.SUPER_0); } 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.SUPER_0, enclosingClass.Signature); } }
private static void Call(Compiler c, bool allowAssignment) { c.IgnoreNewlines(); c.Consume(TokenType.Name, "Expect method name after '.'."); c.NamedCall(allowAssignment, Instruction.CALL_0); }
// Compiles a variable name or method call with an implicit receiver. private static void Name(Compiler c, bool allowAssignment) { // Look for the name in the scope chain up to the nearest enclosing method. Token token = c._parser.Previous; Instruction loadInstruction; string varName = c._parser.Source.Substring(token.Start, token.Length); int index = c.ResolveNonmodule(varName, token.Length, out loadInstruction); if (index != -1) { c.Variable(allowAssignment, index, loadInstruction); return; } // TODO: The fact that we return above here if the variable is known and parse // an optional argument list below if not means that the grammar is not // context-free. A line of code in a method like "someName(foo)" is a parse // error if "someName" is a defined variable in the surrounding scope and not // if it isn't. Fix this. One option is to have "someName(foo)" always // resolve to a self-call if there is an argument list, but that makes // getters a little confusing. // If we're inside a method and the name is lowercase, treat it as a method // on this. if (IsLocalName(varName) && c.GetEnclosingClass() != null) { c.LoadThis(); c.NamedCall(allowAssignment, Instruction.CALL_0); return; } // Otherwise, look for a module-level variable with the name. int module = c._parser.Module.Variables.FindIndex(v => v.Name == varName); if (module == -1) { if (IsLocalName(varName)) { c.Error("Undefined variable."); return; } // If it's a nonlocal name, implicitly define a module-level variable in // the hopes that we get a real definition later. module = c._parser.vm.DeclareVariable(c._parser.Module, varName); if (module == -2) { c.Error("Too many module variables defined."); } } c.Variable(allowAssignment, module, Instruction.LOAD_MODULE_VAR); }