Skip to content

Commit

Permalink
Added better error handling for macros, and corrected problem that re…
Browse files Browse the repository at this point in the history
…ported incorrect line number for any error inside a macro definition.
  • Loading branch information
richtw1 committed Apr 25, 2011
1 parent b1d7f79 commit ea0cf26
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 16 deletions.
11 changes: 9 additions & 2 deletions about.txt
Expand Up @@ -152,6 +152,13 @@ This specifies the name of the source file for BeebAsm to process. In the
absence of any switches specifying disc image filenames, SAVE commands in the
source code will write out object files directly to the current directory.

-o <filename>

If this is specified, then the SAVE command can be used without supplying a
filename, and this one will be used instead. This allows BeebAsm to be used
like a conventional assembler, specifying both input and output filenames
from the command line.

-do <filename>

This specifies the name of a new disc image to be created. All object code
Expand Down Expand Up @@ -548,8 +555,8 @@ ENDMACRO
ADDI8 bonus, 10 ; add 10 to the memory location 'bonus'
ADDI8 pills, pill_add ; pills += pill_add

Macros can also be nested, as demonstrated by this somewhat contrived
example:
Macros can also be called from other macros, as demonstrated by this somewhat
contrived example:

MACRO ADDI16 addr, val
IF val=0
Expand Down
Binary file modified beebasm.exe
Binary file not shown.
4 changes: 2 additions & 2 deletions src/Makefile
Expand Up @@ -51,8 +51,8 @@ LDLIBS := -lstdc++ -lm

# Parameters to the executable

#PARAMS := -i ../demo.6502 -do ../demo.ssd -boot Code -v
PARAMS := -i ../test.6502 -v
PARAMS := -i ../demo.6502 -do ../demo.ssd -boot Code -v
#PARAMS := -i ../test.6502 -v



Expand Down
1 change: 1 addition & 0 deletions src/asmexception.h
Expand Up @@ -204,6 +204,7 @@ DEFINE_SYNTAX_EXCEPTION( SecondPassProblem, "Fatal error: the second assembler p
DEFINE_SYNTAX_EXCEPTION( InvalidMacroName, "Invalid macro name; must start with a letter and contain only letters, numbers and underscore." );
DEFINE_SYNTAX_EXCEPTION( NoNestedMacros, "Cannot define one macro inside another." );
DEFINE_SYNTAX_EXCEPTION( EndMacroUnexpected, "ENDMACRO encountered without a matching MACRO directive." );
DEFINE_SYNTAX_EXCEPTION( NoEndMacro, "Unterminated macro (ENDMACRO not found)." );
DEFINE_SYNTAX_EXCEPTION( DuplicateMacroName, "Macro name already defined." );

// meta-language parsing exceptions
Expand Down
12 changes: 12 additions & 0 deletions src/commands.cpp
Expand Up @@ -1731,6 +1731,18 @@ void LineParser::HandleMacro()
throw AsmException_SyntaxError_UnexpectedComma( m_line, m_column - 1 );
}

// If there is nothing else on the line following the MACRO command, put a newline at the
// beginning of the macro definition, so any errors are reported on the correct line

if ( m_column == m_line.length() &&
GlobalData::Instance().IsFirstPass() )
{
m_sourceCode->GetCurrentMacro()->AddLine("\n");
}

// Set the IF condition to false - this is a cheaty way of ensuring that the macro body
// is not assembled as it is parsed

m_sourceCode->SetCurrentIfCondition(false);
}

Expand Down
3 changes: 2 additions & 1 deletion src/globaldata.cpp
Expand Up @@ -71,7 +71,8 @@ GlobalData::GlobalData()
m_bVerbose( false ),
m_bUseDiscImage( false ),
m_pDiscImage( NULL ),
m_bSaved( false )
m_bSaved( false ),
m_pOutputFile( NULL )
{
}

Expand Down
1 change: 1 addition & 0 deletions src/main.cpp
Expand Up @@ -146,6 +146,7 @@ int main( int argc, char* argv[] )
case WAITING_FOR_OUTPUT_FILENAME:

pOutputFile = argv[i];
GlobalData::Instance().SetOutputFile( pOutputFile );
state = READY;
break;

Expand Down
47 changes: 36 additions & 11 deletions src/sourcecode.cpp
Expand Up @@ -154,16 +154,26 @@ void SourceCode::Process()
}
}

// Check that we have no IF mismatch
// Check that we have no IF / MACRO mismatch

if ( m_ifStackPtr > initialIfStackPtr )
{
If& mismatchedIf = m_ifStack[ m_ifStackPtr - 1 ];

AsmException_SyntaxError_IfWithoutEndif e( mismatchedIf.m_line, mismatchedIf.m_column );
e.SetFilename( m_filename );
e.SetLineNumber( mismatchedIf.m_lineNumber );
throw e;
if ( mismatchedIf.m_isMacroDefinition )
{
AsmException_SyntaxError_NoEndMacro e( mismatchedIf.m_line, mismatchedIf.m_column );
e.SetFilename( m_filename );
e.SetLineNumber( mismatchedIf.m_lineNumber );
throw e;
}
else
{
AsmException_SyntaxError_IfWithoutEndif e( mismatchedIf.m_line, mismatchedIf.m_column );
e.SetFilename( m_filename );
e.SetLineNumber( mismatchedIf.m_lineNumber );
throw e;
}
}
}

Expand Down Expand Up @@ -385,17 +395,31 @@ void SourceCode::AddIfLevel( const string& line, int column )
throw AsmException_SyntaxError_TooManyIFs( line, column );
}

m_ifStack[ m_ifStackPtr ].m_condition = true;
m_ifStack[ m_ifStackPtr ].m_passed = false;
m_ifStack[ m_ifStackPtr ].m_hadElse = false;
m_ifStack[ m_ifStackPtr ].m_line = line;
m_ifStack[ m_ifStackPtr ].m_column = column;
m_ifStack[ m_ifStackPtr ].m_lineNumber = m_lineNumber;
m_ifStack[ m_ifStackPtr ].m_condition = true;
m_ifStack[ m_ifStackPtr ].m_passed = false;
m_ifStack[ m_ifStackPtr ].m_hadElse = false;
m_ifStack[ m_ifStackPtr ].m_isMacroDefinition = false;
m_ifStack[ m_ifStackPtr ].m_line = line;
m_ifStack[ m_ifStackPtr ].m_column = column;
m_ifStack[ m_ifStackPtr ].m_lineNumber = m_lineNumber;
m_ifStackPtr++;
}



/*************************************************************************************************/
/**
SourceCode::SetCurrentIfAsMacroDefinition()
*/
/*************************************************************************************************/
void SourceCode::SetCurrentIfAsMacroDefinition()
{
assert( m_ifStackPtr > 0 );
m_ifStack[ m_ifStackPtr - 1 ].m_isMacroDefinition = true;
}



/*************************************************************************************************/
/**
SourceCode::SetCurrentIfCondition()
Expand Down Expand Up @@ -486,6 +510,7 @@ void SourceCode::StartMacro( const string& line, int column )
}

AddIfLevel( line, column );
SetCurrentIfAsMacroDefinition();
}


Expand Down
2 changes: 2 additions & 0 deletions src/sourcecode.h
Expand Up @@ -83,6 +83,7 @@ class SourceCode
bool m_condition;
bool m_hadElse;
bool m_passed;
bool m_isMacroDefinition;
std::string m_line;
int m_column;
int m_lineNumber;
Expand Down Expand Up @@ -118,6 +119,7 @@ class SourceCode

bool IsIfConditionTrue() const;
void AddIfLevel( const std::string& line, int column );
void SetCurrentIfAsMacroDefinition();
void SetCurrentIfCondition( bool b );
void StartElse( const std::string& line, int column );
void StartElif( const std::string& line, int column );
Expand Down

0 comments on commit ea0cf26

Please sign in to comment.