beebasm

changeset 40:7699ee2b4db7

Fixed bug which allows mismatched braces in macro definitions.
author RichTW <richtw1@gmail.com>
date Mon Apr 25 17:39:31 2011 +0200
parents 0e5eb702df24
children b6d9dd6812d5
files beebasm.exe demo.ssd src/sourcecode.cpp src/sourcecode.h
diffstat 4 files changed, 20 insertions(+), 7 deletions(-) [+]
line diff
     1.1 Binary file beebasm.exe has changed
     2.1 Binary file demo.ssd has changed
     3.1 --- a/src/sourcecode.cpp	Mon Apr 25 04:37:49 2011 +0200
     3.2 +++ b/src/sourcecode.cpp	Mon Apr 25 17:39:31 2011 +0200
     3.3 @@ -51,7 +51,9 @@
     3.4  /*************************************************************************************************/
     3.5  SourceCode::SourceCode( const string& filename, int lineNumber )
     3.6  	:	m_forStackPtr( 0 ),
     3.7 +		m_initialForStackPtr( 0 ),
     3.8  		m_ifStackPtr( 0 ),
     3.9 +		m_initialIfStackPtr( 0 ),
    3.10  		m_currentMacro( NULL ),
    3.11  		m_filename( filename ),
    3.12  		m_lineNumber( lineNumber ),
    3.13 @@ -87,8 +89,8 @@
    3.14  {
    3.15  	// Remember the FOR and IF stack initial pointer values
    3.16  
    3.17 -	int initialForStackPtr = m_forStackPtr;
    3.18 -	int initialIfStackPtr = m_ifStackPtr;
    3.19 +	m_initialForStackPtr = m_forStackPtr;
    3.20 +	m_initialIfStackPtr = m_ifStackPtr;
    3.21  
    3.22  	// Iterate through the file line-by-line
    3.23  
    3.24 @@ -133,7 +135,7 @@
    3.25  
    3.26  	// Check that we have no FOR / braces mismatch
    3.27  
    3.28 -	if ( m_forStackPtr > initialForStackPtr )
    3.29 +	if ( m_forStackPtr != m_initialForStackPtr )
    3.30  	{
    3.31  		For& mismatchedFor = m_forStack[ m_forStackPtr - 1 ];
    3.32  
    3.33 @@ -155,7 +157,7 @@
    3.34  
    3.35  	// Check that we have no IF / MACRO mismatch
    3.36  
    3.37 -	if ( m_ifStackPtr > initialIfStackPtr )
    3.38 +	if ( m_ifStackPtr != m_initialIfStackPtr )
    3.39  	{
    3.40  		If& mismatchedIf = m_ifStack[ m_ifStackPtr - 1 ];
    3.41  
    3.42 @@ -301,7 +303,15 @@
    3.43  /*************************************************************************************************/
    3.44  void SourceCode::CloseBrace( const string& line, int column )
    3.45  {
    3.46 -	if ( m_forStackPtr == 0 )
    3.47 +	// Instead of comparing against 0, I compare with the initial value of the stack ptr when
    3.48 +	// SourceCode::Process() was called.
    3.49 +	// This is because macros start wih a copy of the parent FOR stack frame, with an extra set of
    3.50 +	// braces pushed so they are in their own scope.  Without this amendment, it'd be possible to
    3.51 +	// close the 'hidden' braces started by the macro instantiation - with hilarious* consequences!
    3.52 +	//
    3.53 +	// * for unfunny values of hilarious
    3.54 +
    3.55 +	if ( m_forStackPtr == m_initialForStackPtr )
    3.56  	{
    3.57  		throw AsmException_SyntaxError_MismatchedBraces( line, column );
    3.58  	}
     4.1 --- a/src/sourcecode.h	Mon Apr 25 04:37:49 2011 +0200
     4.2 +++ b/src/sourcecode.h	Mon Apr 25 17:39:31 2011 +0200
     4.3 @@ -55,9 +55,10 @@
     4.4  
     4.5  
     4.6  	// For loop / if related stuff
     4.7 +	// Should use a std::vector here, but I can't really be bothered to change it now
     4.8  
     4.9 -	#define MAX_FOR_LEVELS	32
    4.10 -	#define MAX_IF_LEVELS	32
    4.11 +	#define MAX_FOR_LEVELS	64
    4.12 +	#define MAX_IF_LEVELS	64
    4.13  
    4.14  protected:
    4.15  
    4.16 @@ -77,6 +78,7 @@
    4.17  
    4.18  	For						m_forStack[ MAX_FOR_LEVELS ];
    4.19  	int						m_forStackPtr;
    4.20 +	int						m_initialForStackPtr;
    4.21  
    4.22  	struct If
    4.23  	{
    4.24 @@ -90,6 +92,7 @@
    4.25  	};
    4.26  
    4.27  	int						m_ifStackPtr;
    4.28 +	int						m_initialIfStackPtr;
    4.29  	If						m_ifStack[ MAX_IF_LEVELS ];
    4.30  
    4.31  	Macro*					m_currentMacro;