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(""); }
public static ObjFn Compile(WrenVM vm, ObjModule module, string sourcePath, string source, bool printErrors) { Parser parser = new Parser { vm = vm, Module = module, SourcePath = sourcePath, Source = source, TokenStart = 0, CurrentChar = 0, CurrentLine = 1, Current = { Type = TokenType.Error, Start = 0, Length = 0, Line = 0 }, SkipNewlines = true, PrintErrors = printErrors, HasError = false, Raw = "" }; Compiler compiler = new Compiler(parser, null, true); // Read the first token. compiler.NextToken(); compiler.IgnoreNewlines(); while (!compiler.Match(TokenType.Eof)) { compiler.Definition(); // If there is no newline, it must be the end of the block on the same line. if (!compiler.MatchLine()) { compiler.Consume(TokenType.Eof, "Expect end of file."); break; } } compiler.Emit(Instruction.NULL); compiler.Emit(Instruction.RETURN); // See if there are any implicitly declared module-level variables that never // got an explicit definition. // TODO: It would be nice if the error was on the line where it was used. for (int i = 0; i < parser.Module.Variables.Count; i++) { ModuleVariable t = parser.Module.Variables[i]; if (t.Container == Obj.Undefined) { compiler.Error(string.Format("Variable '{0}' is used but not defined.", t.Name)); } } return compiler.EndCompiler("(script)"); }
private static void Boolean(Compiler c, bool allowAssignment) { c.Emit(c._parser.Previous.Type == TokenType.False ? Instruction.FALSE : Instruction.TRUE); }
private static void null_(Compiler c, bool allowAssignment) { c.Emit(Instruction.NULL); }
// 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."); }
private static void Boolean(Compiler c, bool allowAssignment) { c.Emit(c.parser.previous.type == TokenType.TOKEN_FALSE ? Instruction.FALSE : Instruction.TRUE); }
// 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. 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."); }