Skip to content

Commit

Permalink
Replace istringstream::getline with SourceCode::GetLine
Browse files Browse the repository at this point in the history
  • Loading branch information
mungre committed Jun 11, 2024
1 parent 8ba405c commit 8fba958
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 164 deletions.
62 changes: 1 addition & 61 deletions src/macro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
*/
/*************************************************************************************************/

#include <cmath>
#include <iostream>

#include "macro.h"

Expand Down Expand Up @@ -60,8 +58,7 @@ Macro::Macro( const string& filename, int lineNumber )
*/
/*************************************************************************************************/
MacroInstance::MacroInstance( const Macro* macro, const SourceCode* sourceCode )
: SourceCode( macro->GetFilename(), macro->GetLineNumber(), sourceCode ),
m_stream( macro->GetBody() )
: SourceCode( macro->GetFilename(), macro->GetLineNumber(), macro->GetBody(), sourceCode )
//,m_macro( macro )
{
// cout << "Instance macro: " << m_macro->GetName() << " (" << m_filename << ":" << m_lineNumber << ")" << endl;
Expand All @@ -73,63 +70,6 @@ MacroInstance::MacroInstance( const Macro* macro, const SourceCode* sourceCode )



/*************************************************************************************************/
/**
MacroInstance::GetLine()
Reads a line from the source code and returns it into lineFromFile
*/
/*************************************************************************************************/
istream& MacroInstance::GetLine( string& lineFromFile )
{
return getline( m_stream, lineFromFile );
}



/*************************************************************************************************/
/**
MacroInstance::GetFilePointer()
Returns the current file pointer
*/
/*************************************************************************************************/
int MacroInstance::GetFilePointer()
{
return static_cast< int >( m_stream.tellg() );
}



/*************************************************************************************************/
/**
MacroInstance::SetFilePointer()
Sets the current file pointer
*/
/*************************************************************************************************/
void MacroInstance::SetFilePointer( int i )
{
m_lineStartPointer = i;
m_stream.seekg( i );
}



/*************************************************************************************************/
/**
MacroInstance::IsAtEnd()
Returns whether the current stream is in an end-of-file state
*/
/*************************************************************************************************/
bool MacroInstance::IsAtEnd()
{
return m_stream.eof();
}



/*************************************************************************************************/
/**
MacroTable::Create()
Expand Down
14 changes: 0 additions & 14 deletions src/macro.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include <cstdlib>
#include <map>
#include <string>
#include <sstream>
#include <vector>
#include "sourcecode.h"

Expand Down Expand Up @@ -103,19 +102,6 @@ class MacroInstance : public SourceCode

MacroInstance( const Macro* macro, const SourceCode* parent );
virtual ~MacroInstance() {}

// Accessors

virtual int GetFilePointer();
virtual void SetFilePointer( int i );
virtual std::istream& GetLine( std::string& lineFromFile );
virtual bool IsAtEnd();


private:

std::istringstream m_stream;
//const Macro* m_macro;
};


Expand Down
3 changes: 2 additions & 1 deletion src/scopedsymbolname.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#ifndef SCOPEDSYMBOLNAME_H_
#define SCOPEDSYMBOLNAME_H_

#include <functional>
#include <string>


Expand Down Expand Up @@ -100,7 +101,7 @@ struct std::hash<ScopedSymbolName>
{
std::size_t operator()(const ScopedSymbolName& s) const
{
std::hash<string> hasher;
std::hash<std::string> hasher;
std::size_t h1 = hasher(s.m_name);
std::size_t h2 = s.m_id;
std::size_t h3 = s.m_count;
Expand Down
62 changes: 56 additions & 6 deletions src/sourcecode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
*/
/*************************************************************************************************/

#include <iostream>
#include <iomanip>
#include <sstream>

#include "sourcecode.h"
#include "asmexception.h"
#include "stringutils.h"
Expand All @@ -51,7 +47,7 @@ using namespace std;
The supplied file will be opened. If there is a problem, an AsmException will be thrown.
*/
/*************************************************************************************************/
SourceCode::SourceCode( const string& filename, int lineNumber, const SourceCode* parent )
SourceCode::SourceCode( const string& filename, int lineNumber, const std::string& text, const SourceCode* parent )
: m_forStackPtr( 0 ),
m_initialForStackPtr( 0 ),
m_ifStackPtr( 0 ),
Expand All @@ -60,8 +56,16 @@ SourceCode::SourceCode( const string& filename, int lineNumber, const SourceCode
m_filename( filename ),
m_lineNumber( lineNumber ),
m_parent( parent ),
m_lineStartPointer( 0 )
m_lineStartPointer( 0 ),
m_text( text ),
m_textPointer( 0 )
{
// Double-check the supplied text came with a '\n' sentinel
if (m_text.empty() || m_text.back() != '\n')
{
assert(false);
m_text.push_back('\n');
}
}


Expand Down Expand Up @@ -622,3 +626,49 @@ bool SourceCode::ShouldOutputAsm()

return false;
}



/*************************************************************************************************/
/**
SourceCode::GetLine()
Reads a line from the source code and returns it into lineFromFile
*/
/*************************************************************************************************/
bool SourceCode::GetLine( string& lineFromFile )
{
if (IsAtEnd())
{
return false;
}
// Check there is always a trailing '\n' (the constructor should ensure this)
assert(m_text.back() == '\n');
int begin = m_textPointer;
while (m_text[m_textPointer++] != '\n')
{
}
// Adding the line in one go rather than character by character is very much faster
lineFromFile.assign(m_text.cbegin() + begin, m_text.cbegin() + m_textPointer - 1);
return true;
}



/*************************************************************************************************/
/**
SourceCode::SetFilePointer()
Sets the current file pointer
*/
/*************************************************************************************************/
void SourceCode::SetFilePointer( int i )
{
if (i > static_cast<int>(m_text.length()))
{
assert(false);
i = m_text.length();
}
m_lineStartPointer = i;
m_textPointer = i;
}
14 changes: 7 additions & 7 deletions src/sourcecode.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
#ifndef SOURCECODE_H_
#define SOURCECODE_H_

#include <fstream>
#include <string>
#include <vector>

#include "scopedsymbolname.h"
#include "value.h"
Expand All @@ -38,7 +36,7 @@ class SourceCode

// Constructor/destructor

SourceCode( const std::string& filename, int lineNumber, const SourceCode* parent );
SourceCode( const std::string& filename, int lineNumber, const std::string& text, const SourceCode* parent );
~SourceCode();

// Process the file
Expand All @@ -52,10 +50,10 @@ class SourceCode
inline const SourceCode*GetParent() const { return m_parent; }
inline int GetLineStartPointer() const { return m_lineStartPointer; }

virtual std::istream& GetLine( std::string& lineFromFile ) = 0;
virtual int GetFilePointer() = 0;
virtual void SetFilePointer( int i ) = 0;
virtual bool IsAtEnd() = 0;
virtual bool GetLine( std::string& lineFromFile );
virtual int GetFilePointer() { return m_textPointer; }
virtual void SetFilePointer( int i );
virtual bool IsAtEnd() { return m_textPointer == static_cast<int>(m_text.length()); }


// For loop / if related stuff
Expand Down Expand Up @@ -147,6 +145,8 @@ class SourceCode
int m_lineNumber;
const SourceCode* m_parent;
int m_lineStartPointer;
std::string m_text;
int m_textPointer;
};


Expand Down
63 changes: 2 additions & 61 deletions src/sourcefile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
*/
/*************************************************************************************************/

#include <fstream>
#include <iostream>
#include <iomanip>
#include <sstream>

#include "sourcefile.h"
#include "asmexception.h"
Expand Down Expand Up @@ -117,8 +116,7 @@ static string ReadFile( string filename )
*/
/*************************************************************************************************/
SourceFile::SourceFile( const string& filename, const SourceCode* parent )
: SourceCode( filename, 1, parent ),
m_file( ReadFile( filename ) )
: SourceCode( filename, 1, ReadFile( filename ), parent )
{
}

Expand Down Expand Up @@ -156,60 +154,3 @@ void SourceFile::Process()
cerr << "Processed file '" << m_filename << "' ok" << endl;
}
}



/*************************************************************************************************/
/**
SourceFile::GetLine()
Reads a line from the source code and returns it into lineFromFile
*/
/*************************************************************************************************/
istream& SourceFile::GetLine( string& lineFromFile )
{
return getline( m_file, lineFromFile );
}



/*************************************************************************************************/
/**
SourceFile::GetFilePointer()
Returns the current file pointer
*/
/*************************************************************************************************/
int SourceFile::GetFilePointer()
{
return static_cast< int >( m_file.tellg() );
}



/*************************************************************************************************/
/**
SourceFile::SetFilePointer()
Sets the current file pointer
*/
/*************************************************************************************************/
void SourceFile::SetFilePointer( int i )
{
m_lineStartPointer = i;
m_file.seekg( i );
}



/*************************************************************************************************/
/**
SourceFile::IsAtEnd()
Returns whether the current stream is in an end-of-file state
*/
/*************************************************************************************************/
bool SourceFile::IsAtEnd()
{
return m_file.eof();
}
14 changes: 0 additions & 14 deletions src/sourcefile.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#ifndef SOURCEFILE_H_
#define SOURCEFILE_H_

#include <fstream>
#include <string>
#include <vector>
#include "sourcecode.h"
Expand All @@ -40,19 +39,6 @@ class SourceFile : public SourceCode
virtual ~SourceFile();

virtual void Process();


// Accessors

virtual int GetFilePointer();
virtual void SetFilePointer( int i );
virtual std::istream& GetLine( std::string& lineFromFile );
virtual bool IsAtEnd();


private:

std::istringstream m_file;
};


Expand Down

0 comments on commit 8fba958

Please sign in to comment.