Add PETSCII string literals

This commit is contained in:
drmortalwombat 2021-12-06 20:54:11 +01:00
parent a8acde291b
commit 75ea2ee439
4 changed files with 68 additions and 8 deletions

View File

@ -88,6 +88,10 @@ The translation mode is selected in conio with the variable "giocharmap" and the
Will switch to the lowercase PETSCII font and translate the strings while printing. Will switch to the lowercase PETSCII font and translate the strings while printing.
PETSCII string literals can also be generated using a "p" or "P" prefix such as:
printf(p"Hello World\n");
Input from the console will also be translated accordingly. Input from the console will also be translated accordingly.
## Embedding binary data ## Embedding binary data

View File

@ -2820,6 +2820,11 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration*
mErrors->Error(exp->mLocation, EERR_INCOMPATIBLE_OPERATOR, "Cannot cast void object to non void object"); mErrors->Error(exp->mLocation, EERR_INCOMPATIBLE_OPERATOR, "Cannot cast void object to non void object");
return ExValue(exp->mLeft->mDecType, vr.mTemp, vr.mReference); return ExValue(exp->mLeft->mDecType, vr.mTemp, vr.mReference);
} }
else if (exp->mLeft->mDecType->IsIntegerType() && vr.mType->IsIntegerType())
{
vr = Dereference(proc, block, vr);
return CoerceType(proc, block, vr, exp->mLeft->mDecType);
}
else else
{ {
vr = Dereference(proc, block, vr); vr = Dereference(proc, block, vr);

View File

@ -458,7 +458,7 @@ void Scanner::NextToken(void)
else if (mToken == TK_LESS_THAN) else if (mToken == TK_LESS_THAN)
{ {
mOffset--; mOffset--;
StringToken('>'); StringToken('>', 'a');
if (!mPreprocessor->OpenSource("Including", mTokenString, false)) if (!mPreprocessor->OpenSource("Including", mTokenString, false))
mErrors->Error(mLocation, EERR_FILE_NOT_FOUND, "Could not open source file", mTokenString); mErrors->Error(mLocation, EERR_FILE_NOT_FOUND, "Could not open source file", mTokenString);
} }
@ -621,7 +621,7 @@ void Scanner::NextToken(void)
else if (mToken == TK_LESS_THAN) else if (mToken == TK_LESS_THAN)
{ {
mOffset--; mOffset--;
StringToken('>'); StringToken('>', 'a');
if (!mPreprocessor->EmbedData("Embedding", mTokenString, false, skip, limit)) if (!mPreprocessor->EmbedData("Embedding", mTokenString, false, skip, limit))
mErrors->Error(mLocation, EERR_FILE_NOT_FOUND, "Could not open source file", mTokenString); mErrors->Error(mLocation, EERR_FILE_NOT_FOUND, "Could not open source file", mTokenString);
} }
@ -1021,10 +1021,10 @@ void Scanner::NextRawToken(void)
break; break;
case '\'': case '\'':
CharToken(); CharToken('a');
break; break;
case '"': case '"':
StringToken(mTokenChar); StringToken(mTokenChar, 'a');
break; break;
case '#': case '#':
@ -1136,6 +1136,19 @@ void Scanner::NextRawToken(void)
else else
break; break;
} }
if (n == 1)
{
if (mTokenChar == '"')
{
StringToken(mTokenChar, tkident[0]);
break;
}
else if (mTokenChar == '\'')
{
CharToken(tkident[0]);
break;
}
}
tkident[n] = 0; tkident[n] = 0;
if (n == 256) if (n == 256)
Error("Identifier exceeds max character limit"); Error("Identifier exceeds max character limit");
@ -1238,7 +1251,7 @@ void Scanner::Error(const char* error)
mErrors->Error(mLocation, EERR_SYNTAX, error); mErrors->Error(mLocation, EERR_SYNTAX, error);
} }
void Scanner::StringToken(char terminator) void Scanner::StringToken(char terminator, char mode)
{ {
int n = 0; int n = 0;
@ -1295,6 +1308,28 @@ void Scanner::StringToken(char terminator)
if (mLine[mOffset] && mLine[mOffset] == terminator) if (mLine[mOffset] && mLine[mOffset] == terminator)
{ {
switch (mode)
{
case 'a':
break;
case 'p':
for (int i = 0; i < n; i++)
{
if (mTokenString[i] >= 'A' && mTokenString[i] <= 'Z' || mTokenString[i] >= 'a' && mTokenString[i] <= 'z')
mTokenString[i] = mTokenString[i] ^ 0x20;
}
break;
case 'P':
for (int i = 0; i < n; i++)
{
if (mTokenString[i] >= 'A' && mTokenString[i] <= 'Z' || mTokenString[i] >= 'a' && mTokenString[i] <= 'z')
mTokenString[i] = (mTokenString[i] ^ 0x20) & 0xdf;
}
break;
default:
Error("Invalid string literal mode");
}
mToken = TK_STRING; mToken = TK_STRING;
mOffset++; mOffset++;
NextChar(); NextChar();
@ -1308,7 +1343,7 @@ void Scanner::StringToken(char terminator)
} }
void Scanner::CharToken(void) void Scanner::CharToken(char mode)
{ {
int n = 0; int n = 0;
@ -1356,6 +1391,22 @@ void Scanner::CharToken(void)
} }
} }
switch (mode)
{
case 'a':
break;
case 'p':
if (mTokenChar >= 'A' && mTokenChar <= 'Z' || mTokenChar >= 'a' && mTokenChar <= 'z')
mTokenChar = mTokenChar ^ 0x20;
break;
case 'P':
if (mTokenChar >= 'A' && mTokenChar <= 'Z' || mTokenChar >= 'a' && mTokenChar <= 'z')
mTokenChar = (mTokenChar ^ 0x20) & 0xdf;
break;
default:
Error("Invalid string literal mode");
}
mTokenInteger = mTokenChar; mTokenInteger = mTokenChar;
if (mLine[mOffset] && mLine[mOffset] == '\'') if (mLine[mOffset] && mLine[mOffset] == '\'')

View File

@ -226,8 +226,8 @@ protected:
MacroDict* mDefines, * mDefineArguments; MacroDict* mDefines, * mDefineArguments;
void StringToken(char terminator); void StringToken(char terminator, char mode);
void CharToken(void); void CharToken(char mode);
bool NextChar(void); bool NextChar(void);
void ParseNumberToken(void); void ParseNumberToken(void);