Skip to content

Commit

Permalink
Rename methods from InstructionParser
Browse files Browse the repository at this point in the history
  • Loading branch information
benvenutti committed Sep 15, 2024
1 parent 93a6ab8 commit c42a511
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 67 deletions.
6 changes: 3 additions & 3 deletions hasm/include/hasm/InstructionParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Hasm::InstructionParser
{

bool isComputationCommand( const std::string& cmd );
bool isLabelCommand( const std::string& cmd );
bool isLoadCommand( const std::string& cmd );
bool isComputation( const std::string& instruction );
bool isLabel( const std::string& instruction );
bool isLoad( const std::string& instruction );

} // namespace Hasm::InstructionParser
12 changes: 6 additions & 6 deletions hasm/src/hasm/InstructionParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@
namespace Hasm
{

bool InstructionParser::isComputationCommand( const std::string& cmd )
bool InstructionParser::isComputation( const std::string& instruction )
{
static const std::regex regex{ Grammar::computation_command };

return std::regex_match( cmd, regex );
return std::regex_match( instruction, regex );
}

bool InstructionParser::isLabelCommand( const std::string& cmd )
bool InstructionParser::isLabel( const std::string& instruction )
{
static const std::regex regex{ Grammar::label };

return std::regex_match( cmd, regex );
return std::regex_match( instruction, regex );
}

bool InstructionParser::isLoadCommand( const std::string& cmd )
bool InstructionParser::isLoad( const std::string& instruction )
{
static const std::regex regex{ Grammar::load };

return std::regex_match( cmd, regex );
return std::regex_match( instruction, regex );
}

} // namespace Hasm
6 changes: 3 additions & 3 deletions hasm/src/hasm/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ void removeComment( std::string& str )

std::optional< Hack::InstructionType > instructionType( const std::string& instruction )
{
if ( Hasm::InstructionParser::isLoadCommand( instruction ) )
if ( Hasm::InstructionParser::isLoad( instruction ) )
{
return Hack::InstructionType::addressing;
}

if ( Hasm::InstructionParser::isLabelCommand( instruction ) )
if ( Hasm::InstructionParser::isLabel( instruction ) )
{
return Hack::InstructionType::label;
}

if ( Hasm::InstructionParser::isComputationCommand( instruction ) )
if ( Hasm::InstructionParser::isComputation( instruction ) )
{
return Hack::InstructionType::computation;
}
Expand Down
46 changes: 23 additions & 23 deletions hasm/tests/src/InstructionParser.computation.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,45 +54,45 @@ SCENARIO( "parse valid computations", "[InstructionParser]" )
{
for ( const auto& computation : allComps )
{
REQUIRE( Hasm::InstructionParser::isComputationCommand( computation ) );
REQUIRE( Hasm::InstructionParser::isComputation( computation ) );
}
}

SCENARIO( "parse invalid computations", "[InstructionParser]" )
{
// invalid computations
REQUIRE_FALSE( Hasm::InstructionParser::isComputationCommand( "D+X" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isComputation( "D+X" ) );

// invalid destinations
REQUIRE_FALSE( Hasm::InstructionParser::isComputationCommand( "X=D+A" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isComputationCommand( "1=D+A" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isComputationCommand( "=D+A" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isComputationCommand( "D=X" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isComputationCommand( "D=2" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isComputationCommand( "D=" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isComputation( "X=D+A" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isComputation( "1=D+A" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isComputation( "=D+A" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isComputation( "D=X" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isComputation( "D=2" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isComputation( "D=" ) );

// invalid computation and valid jump
REQUIRE_FALSE( Hasm::InstructionParser::isComputationCommand( "X;JMP" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isComputationCommand( "2;JGT" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isComputationCommand( ";JGE" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isComputation( "X;JMP" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isComputation( "2;JGT" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isComputation( ";JGE" ) );

// valid computation and invalid jump
REQUIRE_FALSE( Hasm::InstructionParser::isComputationCommand( "D;JM" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isComputationCommand( "D;JG" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isComputationCommand( "D;" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isComputation( "D;JM" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isComputation( "D;JG" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isComputation( "D;" ) );

// invalid destination, valid computation and jump
REQUIRE_FALSE( Hasm::InstructionParser::isComputationCommand( "d=D+1;JMP" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isComputationCommand( "0=D+1;JMP" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isComputationCommand( "=D+1;JMP" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isComputation( "d=D+1;JMP" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isComputation( "0=D+1;JMP" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isComputation( "=D+1;JMP" ) );

// valid destination, invalid computation and valid jump
REQUIRE_FALSE( Hasm::InstructionParser::isComputationCommand( "D=d+1;JMP" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isComputationCommand( "D=D+;JMP" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isComputationCommand( "D=;JMP" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isComputation( "D=d+1;JMP" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isComputation( "D=D+;JMP" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isComputation( "D=;JMP" ) );

// valid destination and computation, invalid jump
REQUIRE_FALSE( Hasm::InstructionParser::isComputationCommand( "D=A+1;jmp" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isComputationCommand( "D=D+1;JM" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isComputationCommand( "D=M+1;" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isComputation( "D=A+1;jmp" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isComputation( "D=D+1;JM" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isComputation( "D=M+1;" ) );
}
38 changes: 19 additions & 19 deletions hasm/tests/src/InstructionParser.label.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@

SCENARIO( "parse valid labels", "[InstructionParser]" )
{
REQUIRE( Hasm::InstructionParser::isLabelCommand( "(A)" ) );
REQUIRE( Hasm::InstructionParser::isLabelCommand( "(AA)" ) );
REQUIRE( Hasm::InstructionParser::isLabelCommand( "(_)" ) );
REQUIRE( Hasm::InstructionParser::isLabelCommand( "(_A)" ) );
REQUIRE( Hasm::InstructionParser::isLabelCommand( "(_AA)" ) );
REQUIRE( Hasm::InstructionParser::isLabelCommand( "(.)" ) );
REQUIRE( Hasm::InstructionParser::isLabelCommand( "(.A)" ) );
REQUIRE( Hasm::InstructionParser::isLabelCommand( "(.AA)" ) );
REQUIRE( Hasm::InstructionParser::isLabelCommand( "($)" ) );
REQUIRE( Hasm::InstructionParser::isLabelCommand( "($A)" ) );
REQUIRE( Hasm::InstructionParser::isLabelCommand( "($AA)" ) );
REQUIRE( Hasm::InstructionParser::isLabelCommand( "(:)" ) );
REQUIRE( Hasm::InstructionParser::isLabelCommand( "(:A)" ) );
REQUIRE( Hasm::InstructionParser::isLabelCommand( "(:AA)" ) );
REQUIRE( Hasm::InstructionParser::isLabel( "(A)" ) );
REQUIRE( Hasm::InstructionParser::isLabel( "(AA)" ) );
REQUIRE( Hasm::InstructionParser::isLabel( "(_)" ) );
REQUIRE( Hasm::InstructionParser::isLabel( "(_A)" ) );
REQUIRE( Hasm::InstructionParser::isLabel( "(_AA)" ) );
REQUIRE( Hasm::InstructionParser::isLabel( "(.)" ) );
REQUIRE( Hasm::InstructionParser::isLabel( "(.A)" ) );
REQUIRE( Hasm::InstructionParser::isLabel( "(.AA)" ) );
REQUIRE( Hasm::InstructionParser::isLabel( "($)" ) );
REQUIRE( Hasm::InstructionParser::isLabel( "($A)" ) );
REQUIRE( Hasm::InstructionParser::isLabel( "($AA)" ) );
REQUIRE( Hasm::InstructionParser::isLabel( "(:)" ) );
REQUIRE( Hasm::InstructionParser::isLabel( "(:A)" ) );
REQUIRE( Hasm::InstructionParser::isLabel( "(:AA)" ) );
}

SCENARIO( "parse invalid labels", "[InstructionParser]" )
{
REQUIRE_FALSE( Hasm::InstructionParser::isLabelCommand( "()" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isLabelCommand( "(A" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isLabelCommand( "(AA" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isLabelCommand( "A)" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isLabelCommand( "AA)" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isLabel( "()" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isLabel( "(A" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isLabel( "(AA" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isLabel( "A)" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isLabel( "AA)" ) );
}
26 changes: 13 additions & 13 deletions hasm/tests/src/InstructionParser.load.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@

SCENARIO( "parse valid loads", "[InstructionParser]" )
{
REQUIRE( Hasm::InstructionParser::isLoadCommand( "@10" ) );
REQUIRE( Hasm::InstructionParser::isLoadCommand( "@1010" ) );
REQUIRE( Hasm::InstructionParser::isLoadCommand( "@symbol" ) );
REQUIRE( Hasm::InstructionParser::isLoadCommand( "@A" ) );
REQUIRE( Hasm::InstructionParser::isLoad( "@10" ) );
REQUIRE( Hasm::InstructionParser::isLoad( "@1010" ) );
REQUIRE( Hasm::InstructionParser::isLoad( "@symbol" ) );
REQUIRE( Hasm::InstructionParser::isLoad( "@A" ) );
}

SCENARIO( "parse invalid loads", "[InstructionParser]" )
{
REQUIRE_FALSE( Hasm::InstructionParser::isLoadCommand( "@symbol+" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isLoadCommand( "@+symbol" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isLoadCommand( "@symbol-" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isLoadCommand( "@-symbol" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isLoadCommand( "@+1" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isLoadCommand( "@-1" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isLoadCommand( "10" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isLoadCommand( "id" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isLoadCommand( "@" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isLoad( "@symbol+" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isLoad( "@+symbol" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isLoad( "@symbol-" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isLoad( "@-symbol" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isLoad( "@+1" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isLoad( "@-1" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isLoad( "10" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isLoad( "id" ) );
REQUIRE_FALSE( Hasm::InstructionParser::isLoad( "@" ) );
}

0 comments on commit c42a511

Please sign in to comment.