// A map literal. private static void Map(Compiler c, bool allowAssignment) { // Load the Map class. c.LoadCoreVariable("Map"); // Instantiate a new map. c.CallMethod(0, "new()"); // Compile the map elements. Each one is compiled to just invoke the // subscript setter on the map. if (c.Peek() != TokenType.RightBrace) { do { c.IgnoreNewlines(); if (c.Peek() == TokenType.RightBrace) break; // Push a copy of the map since the subscript call will consume it. c.Emit(Instruction.DUP); // The key. c.ParsePrecedence(false, Precedence.Primary); c.Consume(TokenType.Colon, "Expect ':' after map key."); c.IgnoreNewlines(); // The value. c.Expression(); c.CallMethod(2, "[_]=(_)"); // Discard the result of the setter call. c.Emit(Instruction.POP); } while (c.Match(TokenType.Comma)); } // Allow newlines before the closing '}'. c.IgnoreNewlines(); c.Consume(TokenType.RightBrace, "Expect '}' after map entries."); }
// 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."); }
// A list literal. private static void List(Compiler c, bool allowAssignment) { // Load the List class. c.LoadCoreVariable("List"); // Instantiate a new list. c.CallMethod(0, "new()"); // Compile the list elements. Each one compiles to a ".add()" call. if (c.Peek() != TokenType.RightBracket) { do { c.IgnoreNewlines(); if (c.Peek() == TokenType.RightBracket) break; // 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.Comma)); } // Allow newlines before the closing ']'. c.IgnoreNewlines(); c.Consume(TokenType.RightBracket, "Expect ']' after list elements."); }
// 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."); }