Added new retokenizing to make strings and REM arguments into tokens, themselves.

This commit is contained in:
Mark Long 2016-10-10 20:34:33 -05:00
parent 37f450fd95
commit 9da780252f
6 changed files with 118 additions and 71 deletions

View File

@ -52,7 +52,11 @@ void ApplesoftFile::parse(quint16 start_address)
line.tokens.append(token); line.tokens.append(token);
} while (val != 0x00); } while (val != 0x00);
Retokenizer ret;
ret.retokenize(line);
current_address = line.next_address; current_address = line.next_address;
m_lines.append(line); m_lines.append(line);
} }
@ -93,78 +97,90 @@ QByteArray ApplesoftFile::extraData()
void Retokenizer::retokenize(ApplesoftLine &line) void Retokenizer::retokenize(ApplesoftLine &line)
{ {
Q_UNUSED(line); Q_UNUSED(line);
// QList <QByteArray> string_list;
// QList <QByteArray> number_list;
// QByteArray newTokens;
//// Set Strings aside for a bit to simplify parsing
// QByteArray tmpTokens = line.raw_tokens;
// QByteArray tmpstr;
// bool inString = false;
// for (int idx = 0; idx < line.raw_tokens.length(); idx++)
// {
// quint8 byte = tmpTokens.at(idx);
// if (byte == '"') {
// if (!inString) {
// inString = true;
// tmpstr.append(byte);
// } else {
// inString = false;
// string_list.append(tmpstr);
// tmpstr = "";
// newTokens.append(0xff);
// }
// } else if (!inString) {
// newTokens.append(byte);
// }
// }
// if (inString) {
// string_list.append(tmpstr);
// newTokens.append(0xff);
// }
QVector<ApplesoftToken> replacements;
QVector<ApplesoftToken> tmptokens = line.tokens;
ApplesoftToken token;
QByteArray buffer;
////// Set Numbers aside for a bit too bool inRem = false;
//// tmpTokens = newTokens;
//// newTokens = "";
//// bool inNumber = false;
//// QByteArray tmpnum;
//// for (int idx = 0; idx < tmpTokens.length(); idx++)
//// {
//// quint8 byte = tmpTokens.at(idx);
//// if ((byte >= '0' && byte <= '9') || byte == '.') { // Handle REMs
//// if (!inNumber) {
//// inNumber = true;
//// tmpnum.append(byte);
//// }
//// } else if (inNumber) {
//// inNumber = false;
//// number_list.append(tmpnum);
//// tmpnum = "";
//// newTokens.append(0xfe);
//// newTokens.append(byte);
//// } else {
//// newTokens.append(byte);
//// }
//// }
//// if (inNumber) {
//// number_list.append(tmpnum);
//// newTokens.append(0xfe);
//// }
while (!tmptokens.isEmpty())
{
token = tmptokens.takeFirst();
// line.advanced_tokens = newTokens; if (!inRem) {
replacements.append(token);
if (token.getByteValue() == ApplesoftToken::ASRem)
{
inRem = true;
}
}
else
{
buffer.append(token.getByteValue());
}
}
if (inRem) {
ApplesoftToken remstrtoken(ApplesoftToken::RemStringTokenVal, buffer);
replacements.append(remstrtoken);
buffer.clear();
inRem = false;
}
// // QList<QByteArray> tmpParts = tmpTokens.split(':'); line.tokens = replacements;
replacements.clear();
// Handle DATAs
// Handle Strings
tmptokens = line.tokens;
bool inString = false;
while (!tmptokens.isEmpty())
{
token = tmptokens.takeFirst();
if (token.getTokenId() >= 0x80)
{
replacements.append(token);
continue;
}
if (token.getByteValue() == '"')
{
if (!inString)
{
inString = true;
buffer.append(token.getByteValue());
continue;
}
else
{
buffer.append(token.getByteValue());
ApplesoftToken strtoken(ApplesoftToken::StringTokenVal, buffer);
replacements.append(strtoken);
buffer.clear();
inString = false;
continue;
}
}
if (inString)
{
buffer.append(token.getByteValue());
continue;
}
replacements.append(token);
}
line.tokens = replacements;
//// foreach (QByteArray part, tmpParts) {
//// QByteArray retokenizedPart = retokenzePart(part);
//// }
} }
QByteArray Retokenizer::retokenizePart(QByteArray part) { QByteArray Retokenizer::retokenizePart(QByteArray part) {

View File

@ -1,4 +1,5 @@
#include "applesoftformatter.h" #include "applesoftformatter.h"
#include "util.h"
ApplesoftFormatter::ApplesoftFormatter(QObject *parent) : ApplesoftFormatter::ApplesoftFormatter(QObject *parent) :
QObject(parent) QObject(parent)
@ -19,23 +20,44 @@ QString ApplesoftFormatter::formatText()
} }
QString retval; QString retval;
foreach (ApplesoftLine line, m_file->getLines()) { foreach (ApplesoftLine line, m_file->getLines()) {
QString linestring = QString("%1 ").arg(line.linenum,5,10,QChar(' ')); QString linestring = QString("%1 ").arg(line.linenum,5,10,QChar(' '));
retval.append(linestring); retval.append(linestring);
foreach (ApplesoftToken token, line.tokens) { foreach (ApplesoftToken token, line.tokens)
{
QString tokenstr = token.getRawPrintableString(); QString tokenstr = token.getRawPrintableString();
if (m_format_options.testFlag(BreakAfterReturn)) {
if (token.getByteValue() == ApplesoftToken::ASReturn)
{
tokenstr += "\n";
}
}
#define DEBUGTOKENS
#ifdef DEBUGTOKENS
if (token.getTokenId() >= 0x80)
{
tokenstr = QString(" {%1} ").arg(uint16ToHex(token.getTokenId()));
// tokenstr = " __ ";
}
else
{
tokenstr = QString("%1").arg(QChar(token.getByteValue()));
}
#endif
if (m_format_options.testFlag(ShowCtrlChars)) { if (m_format_options.testFlag(ShowCtrlChars)) {
tokenstr.replace(QChar(0x7f),QChar(0x2401)); tokenstr.replace(QChar(0x7f),QChar(0x2401));
for (int idx = 1; idx <= 0x1f; idx++) { for (int idx = 1; idx <= 0x1f; idx++) {
if (idx == '\n') continue;
if (idx == '\t') continue;
tokenstr.replace(QChar(idx),QChar(idx+0x2400)); tokenstr.replace(QChar(idx),QChar(idx+0x2400));
} }
} }
if (m_format_options.testFlag(BreakAfterReturn)) {
tokenstr.replace("RETURN","RETURN\n");
}
retval.append(tokenstr); retval.append(tokenstr);
} }

View File

@ -97,8 +97,11 @@ QString ApplesoftToken::getRawPrintableString()
return QString(QChar(m_token_id)); return QString(QChar(m_token_id));
} else if (m_token_id <= 0xff) { } else if (m_token_id <= 0xff) {
return m_tokens[m_token_id]; return m_tokens[m_token_id];
} else } else if (m_token_id == StringTokenVal) {
return getByteStringValue();
} else {
return "[temp undefined]"; return "[temp undefined]";
}
} }
void ApplesoftToken::initializeTokenTable() void ApplesoftToken::initializeTokenTable()

View File

@ -12,12 +12,16 @@ public:
static const quint16 StringTokenVal = 0x100; static const quint16 StringTokenVal = 0x100;
static const quint16 RemStringTokenVal = 0x101; static const quint16 RemStringTokenVal = 0x101;
static const quint16 DataStringTokenVal = 0x102; static const quint16 DataStringTokenVal = 0x102;
static const quint16 IntegerTokenVal = 0x103; static const quint16 IntegerTokenVal = 0x103;
static const quint16 FloatTokenVal = 0x104; static const quint16 FloatTokenVal = 0x104;
static const quint16 IntVarTokenVal = 0x105; static const quint16 IntVarTokenVal = 0x105;
static const quint16 IntAryVarTokenVal = 0x106; static const quint16 IntAryVarTokenVal = 0x106;
static const quint16 FloatVarTokenVal = 0x107; static const quint16 FloatVarTokenVal = 0x107;
static const quint16 FloatAryVarTokenVal = 0x108; static const quint16 FloatAryVarTokenVal = 0x108;
static const quint16 StringVarTokenVal = 0x109; static const quint16 StringVarTokenVal = 0x109;
static const quint16 StringAryVarTokenVal = 0x10A; static const quint16 StringAryVarTokenVal = 0x10A;
@ -127,8 +131,8 @@ public:
quint16 getTokenId() const { return m_token_id; } quint16 getTokenId() const { return m_token_id; }
void setValue(QVariant value); void setValue(QVariant value);
QVariant getValue() const { return m_payload; } QVariant getValue() const { return m_payload; }
QByteArray getByteStringValue() const { return m_payload.toByteArray(); } QByteArray getByteStringValue() const { return m_payload.toByteArray(); }
quint16 getWordValue() const { return (quint16) (m_payload.toUInt() & 0xFFFF); } quint16 getWordValue() const { return (quint16) (m_payload.toUInt() & 0xFFFF); }
quint8 getByteValue() const { return (quint8) (m_payload.toUInt() & 0xFF); } quint8 getByteValue() const { return (quint8) (m_payload.toUInt() & 0xFF); }

View File

@ -82,9 +82,11 @@ void MainWindow::unloadDiskFile()
void MainWindow::showLoadDialog() void MainWindow::showLoadDialog()
{ {
QSettings settings;
QString last = settings.value("lastOpened",".").toString();
QString filename = QFileDialog::getOpenFileName(this, QString filename = QFileDialog::getOpenFileName(this,
tr("Open Disk Image"), tr("Open Disk Image"),
"/home/mlong/Desktop", last,
"Disk Images (*.do *.dsk)"); "Disk Images (*.do *.dsk)");
if (!filename.isEmpty()) { if (!filename.isEmpty()) {
loadDiskFile(filename); loadDiskFile(filename);

View File

@ -12,7 +12,7 @@ ApplesoftFileViewer::ApplesoftFileViewer(QWidget *parent) :
setWindowTitle(title); setWindowTitle(title);
m_formatter = new ApplesoftFormatter(this); m_formatter = new ApplesoftFormatter(this);
m_formatter->setFlags(ApplesoftFormatter::PrettyFlags); m_formatter->setFlags(ApplesoftFormatter::PrettyFlags | ApplesoftFormatter::BreakAfterReturn);
connect(ui->findButton,SIGNAL(clicked(bool)), SLOT(findText())); connect(ui->findButton,SIGNAL(clicked(bool)), SLOT(findText()));
m_isFirstFind = true; m_isFirstFind = true;