From 094b44c66ebe771f45b3068cbbb8c234b018f956 Mon Sep 17 00:00:00 2001 From: Mark Long Date: Mon, 24 Oct 2016 23:30:23 -0500 Subject: [PATCH] Made first pass at retokenizing negative numbers. May still have some issues. --- src/applesoftfile/applesoftfile.cxx | 42 +++++++++++++++++++++++- src/applesoftfile/applesoftformatter.cxx | 8 +++-- src/applesoftfile/applesofttoken.h | 4 ++- 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src/applesoftfile/applesoftfile.cxx b/src/applesoftfile/applesoftfile.cxx index ad26dd8..a80753f 100644 --- a/src/applesoftfile/applesoftfile.cxx +++ b/src/applesoftfile/applesoftfile.cxx @@ -428,7 +428,47 @@ QVector Retokenizer::retokenizeNegativeNumbers(QVector tmptokens = QList::fromVector(datatokens); + ApplesoftToken token; + + QMutableListIteratorit(tmptokens); + + bool lastWasInt = false; + + while (it.hasNext()) + { + token = it.next(); + if (token.getTokenId() == ApplesoftToken::IntegerTokenVal) lastWasInt = true; + else if (token.getTokenId() == ApplesoftToken::FloatTokenVal) lastWasInt = true; + else if (token.getTokenId() == ApplesoftToken::IntVarTokenVal) lastWasInt = true; + else if (token.getTokenId() == ApplesoftToken::FloatVarTokenVal) lastWasInt = true; + else if (token.getTokenId() == ')') lastWasInt = true; +else + if (token.getTokenId() == ApplesoftToken::ASMINUS) + { + if (!lastWasInt && it.hasNext() && it.peekNext().getTokenId() == ApplesoftToken::IntegerTokenVal) + { + it.remove(); + token = it.next(); + it.remove(); + int val = token.getUnsignedIntegerValue() * -1; + token.setValue(val); + it.insert(token); + lastWasInt = true; + } + else + { + lastWasInt = false; + } + } + else + { + lastWasInt = false; + } - return datatokens; + + } + + return tmptokens.toVector(); } diff --git a/src/applesoftfile/applesoftformatter.cxx b/src/applesoftfile/applesoftformatter.cxx index da58564..571c341 100644 --- a/src/applesoftfile/applesoftformatter.cxx +++ b/src/applesoftfile/applesoftformatter.cxx @@ -81,13 +81,15 @@ QString ApplesoftFormatter::formatText() if (okToConvert) { - quint32 ui32val = token.getIntegerValue(); - if (ui32val < 256) + quint32 ui32val = token.getUnsignedIntegerValue(); + qint32 i32val = token.getIntegerValue(); +qDebug() << "UI32: " << ui32val << " I32: " << i32val; + if ((i32val < 128 && i32val >= -128) || ui32val < 256) { quint8 ui8 = ui32val; tokenstr = "0x"+uint8ToHex(ui8); } - else if (ui32val < 65536) + else if ((i32val < 32768 && i32val >= -32768) || ui32val < 65536) { quint16 ui16 = ui32val; tokenstr = "0x"+uint16ToHex(ui16); diff --git a/src/applesoftfile/applesofttoken.h b/src/applesoftfile/applesofttoken.h index c6e5190..9fff4b5 100644 --- a/src/applesoftfile/applesofttoken.h +++ b/src/applesoftfile/applesofttoken.h @@ -137,7 +137,9 @@ public: QByteArray getByteStringValue() const { return m_payload.toByteArray(); } QString getStringValue() const { return m_payload.toString(); } - quint32 getIntegerValue() const { return (quint32) (m_payload.toUInt() & 0xFFFFFFFF); } + quint32 getUnsignedIntegerValue() const { return (quint32) (m_payload.toUInt() & 0xFFFFFFFF); } + qint32 getIntegerValue() const { return (qint32) (m_payload.toInt() & 0xFFFFFFFF); } + quint16 getWordValue() const { return (quint16) (m_payload.toUInt() & 0xFFFF); } quint8 getByteValue() const { return (quint8) (m_payload.toUInt() & 0xFF); }