예제 #1
0
        public static TokenizerOutput Tokenize(IErrorPrinter errorPrinter, IEnumerable <string> program)
        {
            var parser = new BasicNParser();
            IEnumerable <Line> lines = parser.ParseLines(program, false);

            bool ret = true;

            foreach (Line line in lines)
            {
                if (line.Report != ParseReport.Ok)
                {
                    ret = false;
                    PrintErrorReport(errorPrinter, line);
                }
            }

            if (!ret)
            {
                return(null);
            }

            IList <TLine> tokenizedLines = Normalize(lines);

            return(Tokenize(errorPrinter, tokenizedLines));
        }
예제 #2
0
        public static bool Compile(IErrorPrinter errorPrinter, IEnumerable <string> program, string outputType, string fileName)
        {
            TokenizerOutput to;

            try {
                to = NBTokenizer.Tokenize(errorPrinter, program);
            }
            catch (TokenizerException te) {
                Console.WriteLine("Compilation error!");
                Console.WriteLine(te.Line.OriginalLine.OriginalLine);
                Console.WriteLine(te.Message);
                to = null;
            }

            if (to == null)
            {
                return(false);
            }

            CompilerContext context = CreateCompilerContext(outputType, fileName);

            CreateProgramType(context, to);
            CreateMainTypeAndSave(context);

            return(true);
        }
예제 #3
0
        private static void PrintErrorReport(IErrorPrinter errorPrinter, Line line)
        {
            errorPrinter.PrintError("Error: " + line.Report);
            errorPrinter.PrintError(line.ErrorMessage);

            if (!line.ErrorColumn.HasValue || !line.OriginalLinePosition.HasValue)
            {
                return;
            }

            errorPrinter.PrintError(String.Format("Position: {0}:{1}", line.OriginalLinePosition, line.ErrorColumn));
            errorPrinter.PrintError(line.OriginalLine);
            string s = new string( ' ', line.ErrorColumn.Value - 1 ) + "^";

            errorPrinter.PrintError(s);
            errorPrinter.PrintError("");
        }
예제 #4
0
 public static TokenizerOutput TokenizeFile(IErrorPrinter errorPrinter, string fileName)
 {
     return(Tokenize(errorPrinter, GetLines(fileName)));
 }
예제 #5
0
        private static TokenizerOutput Tokenize(IErrorPrinter errorPrinter, IEnumerable <TLine> program)
        {
            var ret = new TokenizerOutput {
                Program = new List <TLine>()
            };

            var positions = new Dictionary <int, int>();
            var loopStack = new Stack <LoopData>();

            try {
                foreach (var line in program)
                {
                    int ln = line.OriginalLine.LineNum.Value;
                    if (!positions.ContainsKey(ln))
                    {
                        positions.Add(ln, ret.Program.Count);
                    }

                    TokenizeStatement(ret, line, loopStack);
                }

                ret.Program.Add(new TLine(new Line(), new KwEnd()));
            }
            catch (TokenizerException e) {
                Console.WriteLine("ERROR!");
                Console.WriteLine("Line " + e.Line.OriginalLine.OriginalLinePosition + " : " + e.Line.OriginalLine.OriginalLine);
                Console.WriteLine(e.Message);
                ret = null;
            }

            if (ret == null)
            {
                return(null);
            }

            // check jumps
            for (int i = 0; i < ret.Program.Count; ++i)
            {
                TLine cmd = ret.Program[i];
                var   gt  = cmd.Statement as KwrJump;
                if (gt != null)
                {
                    if (!gt.Normalized)
                    {
                        if (!positions.ContainsKey(gt.JumpPos))
                        {
                            throw new TokenizerException(new TLine(cmd.OriginalLine, cmd.OriginalLine.Statements[0]), "Invalid jump");
                        }

                        gt.JumpPos    = positions[gt.JumpPos];
                        gt.Normalized = true;
                    }

                    if (gt is KwrGosub)
                    {
                        ((KwrGosub)gt).ReturnAddress = i + 1;
                    }
                }
            }

            return(ret);
        }
예제 #6
0
 public static bool Compile(IErrorPrinter errorPrinter, string sourcePath, string outputType, string fileName)
 {
     return(Compile(errorPrinter, NBTokenizer.GetLines(sourcePath), outputType, fileName));
 }