Add seti and int operators

This commit is contained in:
2019-11-23 12:21:14 +01:00
parent f0e00ff018
commit 99f616e1e4
16 changed files with 153 additions and 66 deletions

View File

@@ -17,6 +17,8 @@ namespace Token
{ "lti", OperandType::LessThanInteger },
{ "gti", OperandType::GreaterThanInteger },
{ "eqi", OperandType::EqualInteger },
{ "seti", OperandType::SetInteger },
{ "int", OperandType::Interrupt }
};
auto const & result = operations.find(op);

View File

@@ -64,49 +64,26 @@ namespace Token
case TokenType::ImmediateInteger:
if (isValid)
{
std::printf("$int=%i", std::get<int>(data));
std::printf("%i", std::get<int>(data));
}
else
{
std::printf("BAD_IMM_INT");
std::printf("BAD_IMM");
}
break;
case TokenType::Operand:
if (isValid)
{
switch(std::get<OperandType>(data))
OperandType const opType = std::get<OperandType>(data);
switch(opType)
{
case OperandType::AddInteger:
std::printf("addi");
break;
case OperandType::MultiplyInteger:
std::printf("muli");
break;
case OperandType::SubtractInteger:
std::printf("subi");
break;
case OperandType::DivideInteger:
std::printf("divi");
break;
case OperandType::ShiftIntegerLeft:
std::printf("shli");
break;
case OperandType::ShiftIntegerRight:
std::printf("shri");
break;
case OperandType::Jump:
std::printf("jump");
case OperandType::Unknown:
std::printf("unknown_op");
break;
default:
std::printf("unknown_op");
std::printf("op%i", static_cast<int>(opType));
break;
}
}
@@ -119,25 +96,14 @@ namespace Token
case TokenType::Register:
if (isValid)
{
switch(std::get<RegisterType>(data))
RegisterType const regType = std::get<RegisterType>(data);
switch(regType)
{
case RegisterType::A:
std::printf("%%A");
break;
case RegisterType::B:
std::printf("%%B");
break;
case RegisterType::C:
std::printf("%%C");
break;
case RegisterType::D:
std::printf("%%D");
break;
default:
std::printf("%%%i", static_cast<int>(regType));
break;
case RegisterType::Unknown:
std::printf("%%unknown_reg");
break;
}
@@ -153,7 +119,7 @@ namespace Token
break;
case TokenType::Label:
std::printf("label=%s", std::get<std::string>(data).c_str());
std::printf("LABEL=%s", std::get<std::string>(data).c_str());
break;
case TokenType::Unknown:

View File

@@ -70,32 +70,32 @@ namespace Token
LookForTokenEnd,
};
TokenizerState state = TokenizerState::LookForNextToken;
unsigned tokenStart = 0;
for(unsigned i = 0u; i < line.size(); ++i)
unsigned columnTokenStart = 0;
for(unsigned column = 0u; column < line.size(); ++column)
{
switch(state)
{
case TokenizerState::LookForNextToken:
if (!IsWhiteSpace(line[i]))
if (!IsWhiteSpace(line[column]))
{
if (line[i] == '#')
if (line[column] == '#')
{
// Ignore comments
return;
}
tokenStart = i;
columnTokenStart = column;
state = TokenizerState::LookForTokenEnd;
}
break;
case TokenizerState::LookForTokenEnd:
if (IsWhiteSpace(line[i]) || line[i] == ';')
if (IsWhiteSpace(line[column]) || line[column] == ';')
{
tokens.push_back(ExtractToken(line.substr(tokenStart, i - tokenStart), lineNumber, tokenStart));
if (line[i] == ';')
tokens.push_back(ExtractToken(line.substr(columnTokenStart, column - columnTokenStart), lineNumber, columnTokenStart));
if (line[column] == ';')
{
tokens.push_back(ExtractToken(line.substr(i, 1), lineNumber, tokenStart));
tokens.push_back(ExtractToken(line.substr(column, 1), lineNumber, column));
}
state = TokenizerState::LookForNextToken;
}
@@ -104,7 +104,7 @@ namespace Token
}
if (state == TokenizerState::LookForTokenEnd)
{
tokens.push_back(ExtractToken(line.substr(tokenStart, line.size()), lineNumber, tokenStart));
tokens.push_back(ExtractToken(line.substr(columnTokenStart, line.size()), lineNumber, columnTokenStart));
}
}
}