// 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 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."); }