Exemple #1
0
        public void TestFormattedLines()
        {
            var tokeniser = new Tokeniser(Grammar.All);

            foreach (var line in FormattedLines)
            {
                var tokenisedLine   = tokeniser.Tokenise(line);
                var detokenisedLine = tokeniser.DetokeniseLine(tokenisedLine).Text;

                var retokenizedLine   = tokeniser.Tokenise(detokenisedLine);
                var redetokenisedLine = tokeniser.DetokeniseLine(retokenizedLine).Text;

                Assert.IsTrue(detokenisedLine == redetokenisedLine, $"Either tokenisation or detokenisation of '{line}' has some serious issues");
            }
        }
Exemple #2
0
        public string[] GetLines(int startLine, int endLine)
        {
            if (_protected)
            {
                throw new ReplRuntimeException(ReplExceptionCode.IllegalFunctionCall);
            }

            // 65529 is max insertable line number for GW-BASIC 3.23.
            // however, 65530-65535 are executed if present in tokenised form.
            // in GW-BASIC, 65530 appears in LIST, 65531 and above are hidden

            // sort by positions, not line numbers!
            var linesByPostion = _lineNumberMap.Where(x => x.Key >= startLine && x.Key <= endLine)
                                 .Select(x => x.Value).OrderBy(x => x);

            var lines   = new List <string>();
            var current = Bytecode.Position;

            foreach (var position in linesByPostion)
            {
                Bytecode.Seek(position + 1, SeekOrigin.Begin);
                lines.Add(Tokeniser.DetokeniseLine(Bytecode).Text);
            }

            Bytecode.Seek(current, SeekOrigin.Begin);
            return(lines.ToArray());
        }
Exemple #3
0
        /// <summary>
        /// Save the program to stream stream in defined mode.
        /// </summary>
        /// <param name="stream">Stream to which to save</param>
        /// <param name="mode">Mode in which to save program</param>
        public void SaveTo(Stream stream, ProgramMode mode)
        {
            if (mode != ProgramMode.Protected && _protected)
            {
                throw new ReplRuntimeException(ReplExceptionCode.IllegalFunctionCall);
            }

            var current = Bytecode.Position;

            // skip first \x00 in bytecode
            Bytecode.Seek(1, SeekOrigin.Begin);
            switch (mode)
            {
            case ProgramMode.Binary:
                Bytecode.CopyTo(stream);
                break;

            case ProgramMode.Protected:
                ProtectedProgramEncoder.Encode(Bytecode).CopyTo(stream);
                break;

            case ProgramMode.Ascii:
                while (true)
                {
                    var output = Tokeniser.DetokeniseLine(Bytecode);
                    if (output.LineNumber == -1 || output.LineNumber > MaxLineNumber)
                    {
                        break;
                    }

                    stream.Write(output.Text);
                }
                break;
            }

            Bytecode.Seek(current, SeekOrigin.Begin);
        }