private void CreateConstructor(Signature signature, int initializerSymbol) { Compiler methodCompiler = new Compiler(_parser, this, false); methodCompiler.Emit(_enclosingClass.IsForeign ? Instruction.FOREIGN_CONSTRUCT : Instruction.CONSTRUCT); methodCompiler.EmitShortArg(Instruction.CALL_0 + signature.Arity, initializerSymbol); methodCompiler.Emit(Instruction.RETURN); methodCompiler.EndCompiler(""); }
private static void string_(Compiler c, bool allowAssignment) { int constant = c.StringConstant(); // Compile the code to load the constant. c.EmitShortArg(Instruction.CONSTANT, constant); }
// A map literal. private static void Map(Compiler c, bool allowAssignment) { // Load the Map class. int mapClassSymbol = c.parser.module.Variables.FindIndex(v => v.Name == "Map"); c.EmitShortArg(Instruction.LOAD_MODULE_VAR, mapClassSymbol); // Instantiate a new map. c.CallMethod(0, "<instantiate>"); // Compile the map elements. Each one is compiled to just invoke the // subscript setter on the map. if (c.Peek() != TokenType.TOKEN_RIGHT_BRACE) { do { c.IgnoreNewlines(); // Push a copy of the map since the subscript call will consume it. c.Emit(Instruction.DUP); // The key. c.ParsePrecedence(false, Precedence.PREC_PRIMARY); c.Consume(TokenType.TOKEN_COLON, "Expect ':' after map key."); // The value. c.Expression(); c.CallMethod(2, "[_]=(_)"); // Discard the result of the setter call. c.Emit(Instruction.POP); } while (c.Match(TokenType.TOKEN_COMMA)); } // Allow newlines before the closing '}'. c.IgnoreNewlines(); c.Consume(TokenType.TOKEN_RIGHT_BRACE, "Expect '}' after map entries."); }
private static void Number(Compiler c, bool allowAssignment) { int constant = c.AddConstant(new Obj(c._parser.Number)); // Compile the code to load the constant. c.EmitShortArg(Instruction.CONSTANT, constant); }
// A list literal. private static void List(Compiler c, bool allowAssignment) { // Load the List class. int listClassSymbol = c.parser.module.Variables.FindIndex(v => v.Name == "List"); //ASSERT(listClassSymbol != -1, "Should have already defined 'List' variable."); c.EmitShortArg(Instruction.LOAD_MODULE_VAR, listClassSymbol); // Instantiate a new list. c.CallMethod(0, "<instantiate>"); // Compile the list elements. Each one compiles to a ".add()" call. if (c.Peek() != TokenType.TOKEN_RIGHT_BRACKET) { do { c.IgnoreNewlines(); // Push a copy of the list since the add() call will consume it. c.Emit(Instruction.DUP); // The element. c.Expression(); c.CallMethod(1, "add(_)"); // Discard the result of the add() call. c.Emit(Instruction.POP); } while (c.Match(TokenType.TOKEN_COMMA)); } // Allow newlines before the closing ']'. c.IgnoreNewlines(); c.Consume(TokenType.TOKEN_RIGHT_BRACKET, "Expect ']' after list elements."); }