// Construct a FunctionInfo object. parent should be the info object // for the enclosing function, or null if we are the root program. // nameInParent should be the function name, or null for functions that // are defined in an expression (even if the function is named in the // expression, since such names don't create an entry in the parent's // scope). nameInParent should also be null for the root function. // // If parent is not null, we add ourselves to its children list. public FunctionInfo(ProgramInfo program, FunctionInfo parent, string nameInParent) { program_ = program; parent_ = parent; firstChild_ = null; lastChild_ = null; nextSib_ = null; childScan_ = null; nameInParent_ = nameInParent; callsEval_ = false; usesArgs_ = false; params_ = new StringCollection(); locals_ = new StringCollection(); // Register ourselves in our parent scope. if (parent != null) { if (parent.firstChild_ == null) { Trace.Assert(parent.lastChild_ == null); parent.firstChild_ = this; parent.childScan_ = this; parent.lastChild_ = this; } else { Trace.Assert(parent.lastChild_ != null); parent.lastChild_.nextSib_ = this; parent.lastChild_ = this; } } } // FunctionInfo constructor
} // CaptureTokens // Read JavaScript source text and output a C# file. progClassName // is the name of the C# class we generate. inputFileLabel denotes // the source of the input text; it is inserted in a comment in the // C# source. public static void CompileToCSharp( TextReader input, TextWriter output, string progClassName, string inputFileLabel, bool forEvalCode ) { Tokenizer tokenizer = new Tokenizer(input); TokenListNode tokenList = CaptureTokens(tokenizer); Phase1Parser parser = new Phase1Parser(new Retokenizer(tokenList)); ProgramInfo programInfo = new ProgramInfo(); FunctionInfo rootFunc = new FunctionInfo(programInfo, null, null); parser.ParseProgram(rootFunc); PrettyPrinter pp = new PrettyPrinter(output); pp.Line("// JANET compiler output for source file " + inputFileLabel); pp.Line("// "); CSharpGenerator gen = new CSharpGenerator( rootFunc, pp, progClassName, forEvalCode ); Phase2Parser parser2 = new Phase2Parser( new Retokenizer(tokenList), gen ); parser2.ParseProgram(rootFunc); } // CompileToCSharp