Beispiel #1
0
        /// Compile an array of source lines.
        void CompileString(string filename, string[] lines)
        {
            try {
                _parsingIf = false;
                _blockDepth = 0;
                _state = BlockState.NONE;

                // Create the top-level program node.
                if (_programDef == null) {
                    string moduleName = Path.GetFileNameWithoutExtension(_opts.OutputFile);
                    if (string.IsNullOrEmpty(moduleName)) {
                        moduleName = "Class";
                    }
                    _programDef = new ProgramDefinition();
                    _programDef.Name = moduleName;
                    _programDef.Globals = _globalSymbols;
                    _programDef.IsExecutable = true;
                    _programDef.Root = _ptree;
                }

                CompileUnit(filename, lines);

                // Dump file?
                if (_opts.Dump) {
                    XmlDocument xmlTree = ParseTreeXML.Tree(_programDef);
                    string outputFilename = Path.GetFileName(_opts.OutputFile);
                    outputFilename = Path.ChangeExtension(outputFilename, ".xml");
                    xmlTree.Save(outputFilename);
                }
            } catch (Exception e) {
                if (_opts.DevMode) {
                    throw;
                }
                _messages.Error(MessageCode.COMPILERFAILURE, String.Format("Compiler error: {0}", e.Message));
            }
        }
Beispiel #2
0
        /// <summary>
        /// Generate the code for the entire parse tree as specified by the
        /// given program definition.
        /// </summary>
        /// <param name="programDef">A program definition object</param>
        public void GenerateCode(ProgramDefinition programDef)
        {
            if (programDef == null) {
                throw new ArgumentNullException("programDef");
            }
            try {
                _prog = new Program(_opts, programDef.Name, programDef.IsExecutable);

                GenerateSymbols(programDef.Globals);

                foreach (ParseNode node in programDef.Root.Nodes) {
                    node.Generate(this);
                }
            } catch (Exception e) {
                if (_opts.DevMode) {
                    throw;
                }
                Error(String.Format("Compiler error: {0}", e.Message));
            }
        }