Skip to content

Commit

Permalink
Fix: BASIC renumber now also renumbers line numbers directly after THEN
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgRottensteiner committed May 27, 2019
1 parent 5cb5ccd commit 9d7452d
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 17 deletions.
65 changes: 58 additions & 7 deletions C64Models/Parser/BasicFileParser.cs
Expand Up @@ -354,15 +354,17 @@ static BasicFileParser()



public BasicFileParser()
public BasicFileParser( ParserSettings Settings )
{
LabelMode = false;
this.Settings = Settings;
}



public BasicFileParser( string Filename )
public BasicFileParser( ParserSettings Settings, string Filename )
{
this.Settings = Settings;
LabelMode = false;
m_Filename = Filename;
}
Expand Down Expand Up @@ -1881,6 +1883,8 @@ public string EncodeToLabels()
sb.AppendLine();
sb.Append( lineNumberReference[lineInfo.Value.LineNumber] + "\r\n" );
}
bool hadREM = false;

for ( int i = 0; i < lineInfo.Value.Tokens.Count; ++i )
{
Token token = lineInfo.Value.Tokens[i];
Expand All @@ -1900,6 +1904,11 @@ public string EncodeToLabels()

if ( token.TokenType == Token.Type.BASIC_TOKEN )
{
if ( token.ByteValue == m_Opcodes["REM"].ByteValue )
{
hadREM = true;
}

if ( ( token.ByteValue == m_Opcodes["RUN"].ByteValue )
|| ( token.ByteValue == m_Opcodes["THEN"].ByteValue ) )
{
Expand Down Expand Up @@ -1927,7 +1936,8 @@ public string EncodeToLabels()
{
// ON x GOTO/GOSUB can have more than one line number
// insert label instead of line number
sb.Append( token.Content + " " );
//sb.Append( token.Content + " " );
sb.Append( token.Content );

int nextIndex = i + 1;
bool mustBeComma = false;
Expand All @@ -1947,6 +1957,16 @@ public string EncodeToLabels()
}
sb.Append( lineNumberReference[refNo].ToString() );
}
else if ( ( nextToken.TokenType == Token.Type.DIRECT_TOKEN )
&& ( nextToken.Content == " " ) )
{
if ( !Settings.StripSpaces )
{
sb.Append( nextToken.Content );
}
++nextIndex;
continue;
}
else
{
// error or end
Expand Down Expand Up @@ -1978,7 +1998,8 @@ public string EncodeToLabels()
|| ( token.TokenType == Token.Type.NUMERIC_LITERAL )
|| ( token.TokenType == Token.Type.EX_BASIC_TOKEN ) )
{
if ( token.ByteValue != m_Opcodes["REM"].ByteValue )
if ( ( token.ByteValue != m_Opcodes["REM"].ByteValue )
&& ( hadREM ) )
{
sb.Append( " " );
}
Expand All @@ -1991,6 +2012,7 @@ public string EncodeToLabels()



// finds next non-space token or returns -1
private int FindNextToken( List<Token> Tokens, int StartIndex )
{
int curIndex = StartIndex + 1;
Expand Down Expand Up @@ -2030,7 +2052,7 @@ public string DecodeFromLabels()
string labelToReplace = "LABEL" + lineInfo.Value.Tokens[1].Content;

labelToNumber[labelToReplace] = lineNumber;
Debug.Log( "Replace label " + labelToReplace + " with line " + lineNumber );
//Debug.Log( "Replace label " + labelToReplace + " with line " + lineNumber );
}
else
{
Expand All @@ -2043,6 +2065,8 @@ public string DecodeFromLabels()
lineNumber = 10;
foreach ( KeyValuePair<int, LineInfo> lineInfo in m_LineInfos )
{
bool hadREM = false;

// is this a label definition?
int nextTokenIndex = FindNextToken( lineInfo.Value.Tokens, -1 );
int nextTokenIndex2 = FindNextToken( lineInfo.Value.Tokens, nextTokenIndex );
Expand All @@ -2067,11 +2091,21 @@ public string DecodeFromLabels()
Token token = lineInfo.Value.Tokens[tokenIndex];

if ( ( token.TokenType == Token.Type.DIRECT_TOKEN )
&& ( Settings.StripSpaces )
&& ( !hadREM )
&& ( token.Content.Trim().Length == 0 ) )
{
continue;
}
bool tokenIsInserted = false;

if ( ( token.TokenType == Token.Type.BASIC_TOKEN )
&& ( token.ByteValue == m_Opcodes["REM"].ByteValue ) )
{
hadREM = true;
}


if ( ( token.TokenType == Token.Type.BASIC_TOKEN )
&& ( ( token.ByteValue == m_Opcodes["GOTO"].ByteValue )
|| ( token.ByteValue == m_Opcodes["GOSUB"].ByteValue )
Expand All @@ -2081,6 +2115,7 @@ public string DecodeFromLabels()
bool isGotoOrGosub = ( token.ByteValue == m_Opcodes["GOTO"].ByteValue ) | ( token.ByteValue == m_Opcodes["GOSUB"].ByteValue );
nextTokenIndex = FindNextToken( lineInfo.Value.Tokens, tokenIndex );
nextTokenIndex2 = FindNextToken( lineInfo.Value.Tokens, nextTokenIndex );

while ( ( nextTokenIndex != -1 )
&& ( nextTokenIndex2 != -1 ) )
{
Expand All @@ -2100,6 +2135,13 @@ public string DecodeFromLabels()
{
tokenIsInserted = true;
sb.Append( token.Content );

while ( nextTokenIndex - 1 > tokenIndex )
{
// there were blanks in between
sb.Append( lineInfo.Value.Tokens[tokenIndex + 1].Content );
++tokenIndex;
}
}
sb.Append( labelToNumber[label].ToString() );
}
Expand Down Expand Up @@ -2397,9 +2439,18 @@ public string Renumber( int LineStart, int LineStep )
if ( i + 1 < lineInfo.Tokens.Count )
{
int refNo = -1;
if ( int.TryParse( lineInfo.Tokens[i + 1].Content, out refNo ) )
int nextTokenIndex = FindNextToken( lineInfo.Tokens, i );
if ( ( nextTokenIndex != -1 )
&& ( int.TryParse( lineInfo.Tokens[nextTokenIndex].Content, out refNo ) ) )
{
sb.Append( token.Content + lineNumberReference[refNo].ToString() );
sb.Append( token.Content );

while ( i + 1 < nextTokenIndex )
{
sb.Append( lineInfo.Tokens[i + 1].Content );
++i;
}
sb.Append( lineNumberReference[refNo] );
++i;
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion C64Studio/Compiling.cs
Expand Up @@ -21,7 +21,7 @@ public class Compiling


public Parser.ASMFileParser ParserASM = new C64Studio.Parser.ASMFileParser();
public Parser.BasicFileParser ParserBasic = new C64Studio.Parser.BasicFileParser();
public Parser.BasicFileParser ParserBasic = new C64Studio.Parser.BasicFileParser( new Parser.BasicFileParser.ParserSettings() );



Expand Down
2 changes: 1 addition & 1 deletion C64Studio/CustomRenderer/BASICSyntaxHighlighter.cs
Expand Up @@ -8,7 +8,7 @@ namespace C64Studio.CustomRenderer
{
class BASICSyntaxHighlighter : FastColoredTextBoxNS.SyntaxHighlighter
{
static Parser.BasicFileParser _Parser = new Parser.BasicFileParser();
static Parser.BasicFileParser _Parser = new Parser.BasicFileParser( new Parser.BasicFileParser.ParserSettings() );



Expand Down
2 changes: 1 addition & 1 deletion C64Studio/Dialogs/Settings.cs
Expand Up @@ -1213,7 +1213,7 @@ private void editKeyMapBinding_PreviewKeyDown( object sender, PreviewKeyDownEven

private void checkBASICStripSpaces_CheckedChanged( object sender, EventArgs e )
{
Core.Settings.BASICStripSpaces = checkBASICStripSpaces.Checked;
Core.Settings.BASICStripSpaces = checkBASICStripSpaces.Checked;
Core.Compiling.ParserBasic.Settings.StripSpaces = Core.Settings.BASICStripSpaces;
}

Expand Down
6 changes: 5 additions & 1 deletion C64Studio/Documents/SourceBasicEx.cs
Expand Up @@ -1353,7 +1353,11 @@ private bool ToggleLabelMode()
bool labelMode = !m_LabelMode;

Core.MainForm.m_CompileResult.ClearMessages();
Parser.BasicFileParser parser = new C64Studio.Parser.BasicFileParser( DocumentInfo.FullPath );

var settings = new Parser.BasicFileParser.ParserSettings();
settings.StripSpaces = Core.Settings.BASICStripSpaces;

Parser.BasicFileParser parser = new C64Studio.Parser.BasicFileParser( settings, DocumentInfo.FullPath );
parser.LabelMode = m_LabelMode;

var compilerConfig = new C64Studio.Parser.CompileConfig() { Assembler = C64Studio.Types.AssemblerType.AUTO };
Expand Down
3 changes: 3 additions & 0 deletions C64Studio/TODO.txt
@@ -1,5 +1,8 @@
C64Studio
o Output bei Custom Build kann Info von anderer Datei anzeigen?
o BASIC: Schalte ich Labelmode um und wieder zurück werden auch alle Spaces entfernt (sogar in REM Kommentaren?!).
o BASIC: Undo bei Label-Mode-Wechsel sollte Label-Mode mit wechseln!

o Die Sortierreihenfolge vom "autocomplete" führt bei mir häufig zu Eingabefehlern, weil die Vorschläge alphabetisch sortiert und nicht nach Relevanz dargestellt werden (siehe Bild).
Ausserdem wäre es gut, wenn ein vollständig ausgeschriebener Begriff in der Liste (ganz oben) erthalten bleiben würde (siehe Bild), so könnte man wie gewohnt mit ENTER in die näschte Zeile wechseln.
o Could a future version get some keybindings for the editors, for example the sprite editor would benefit from bindings for:
Expand Down
Binary file modified Sample Projects/MacroTest/MacroTest.c64
Binary file not shown.
2 changes: 1 addition & 1 deletion Sample Projects/MacroTest/test.bas
@@ -1 +1 @@
YYyyyyYYYyyYYYYYYYZZZYYYYZZ
10 GOTO 10:REM Y Y YYYYYYYYYYYYYYYYZZZYYYYZZ
Binary file modified Sample Projects/MacroTest/test.prg
Binary file not shown.
90 changes: 85 additions & 5 deletions TestProject/UnitTestBASICParser.cs
Expand Up @@ -6,9 +6,16 @@ namespace TestProject
[TestClass]
public class UnitTestBASICParser
{
private C64Studio.Parser.BasicFileParser CreateParser()
{
return new C64Studio.Parser.BasicFileParser( new C64Studio.Parser.BasicFileParser.ParserSettings() );
}



private GR.Memory.ByteBuffer TestCompile( string Source )
{
C64Studio.Parser.BasicFileParser parser = new C64Studio.Parser.BasicFileParser();
var parser = CreateParser();

C64Studio.Parser.CompileConfig config = new C64Studio.Parser.CompileConfig();
config.OutputFile = "test.prg";
Expand All @@ -28,7 +35,7 @@ public void TestRenumberWithSpaces()
{
string source = @"20 ifa=1then 20";

C64Studio.Parser.BasicFileParser parser = new C64Studio.Parser.BasicFileParser();
var parser = CreateParser();

C64Studio.Parser.CompileConfig config = new C64Studio.Parser.CompileConfig();
config.OutputFile = "test.prg";
Expand All @@ -43,13 +50,34 @@ public void TestRenumberWithSpaces()



[TestMethod]
public void TestRenumberWithSpacesNoStripSpaces()
{
string source = @"20 ifa=1then 20";

var parser = CreateParser();
parser.Settings.StripSpaces = false;

C64Studio.Parser.CompileConfig config = new C64Studio.Parser.CompileConfig();
config.OutputFile = "test.prg";
config.TargetType = C64Studio.Types.CompileTargetType.PRG;
config.Assembler = C64Studio.Types.AssemblerType.C64_STUDIO;
Assert.IsTrue( parser.Parse( source, null, config ) );

string result = parser.Renumber( 10, 3 );

Assert.AreEqual( "10IFA=1THEN 10", result );
}



[TestMethod]
public void TestRenumberOverLines()
{
string source = @"10 goto 300
300 goto 10";

C64Studio.Parser.BasicFileParser parser = new C64Studio.Parser.BasicFileParser();
var parser = CreateParser();

C64Studio.Parser.CompileConfig config = new C64Studio.Parser.CompileConfig();
config.OutputFile = "test.prg";
Expand All @@ -73,7 +101,7 @@ public void TestRenumberOnGosub()
400 printb
700 printc";

C64Studio.Parser.BasicFileParser parser = new C64Studio.Parser.BasicFileParser();
var parser = CreateParser();

C64Studio.Parser.CompileConfig config = new C64Studio.Parser.CompileConfig();
config.OutputFile = "test.prg";
Expand All @@ -100,7 +128,7 @@ 400 printb
700 printc
2000 printd";

C64Studio.Parser.BasicFileParser parser = new C64Studio.Parser.BasicFileParser();
var parser = CreateParser();

C64Studio.Parser.CompileConfig config = new C64Studio.Parser.CompileConfig();
config.OutputFile = "test.prg";
Expand All @@ -117,5 +145,57 @@ 700 printc
22PRINTD", result );
}



[TestMethod]
public void TestEncodeToLabels()
{
string source = @"10 print ""Hallo""
20 goto 10";

var parser = CreateParser();

C64Studio.Parser.CompileConfig config = new C64Studio.Parser.CompileConfig();
config.OutputFile = "test.prg";
config.TargetType = C64Studio.Types.CompileTargetType.PRG;
config.Assembler = C64Studio.Types.AssemblerType.C64_STUDIO;

Assert.IsTrue( parser.Parse( source, null, config ) );

string encoded = parser.EncodeToLabels();
Assert.AreEqual( @"
LABEL10
PRINT""HALLO""
GOTOLABEL10
", encoded );
}



[TestMethod]
public void TestEncodeToLabelsNoStripSpaces()
{
string source = @"10 print ""Hallo""
20 goto 10";

var parser = CreateParser();

parser.Settings.StripSpaces = false;

C64Studio.Parser.CompileConfig config = new C64Studio.Parser.CompileConfig();
config.OutputFile = "test.prg";
config.TargetType = C64Studio.Types.CompileTargetType.PRG;
config.Assembler = C64Studio.Types.AssemblerType.C64_STUDIO;

Assert.IsTrue( parser.Parse( source, null, config ) );

string encoded = parser.EncodeToLabels();
Assert.AreEqual( @"
LABEL10
PRINT ""HALLO""
GOTO LABEL10
", encoded );
}

}
}

0 comments on commit 9d7452d

Please sign in to comment.