Fix parsing of integer literals with base prefix #106
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
MicroPython 1.25.0 introduced a breaking change, aligning the behaviour of the
int()
function closer to the behaviour of CPython (something along the lines of: strings are assumed to represent a decimal number, unless a base is specified. if a base of 0 is specified, is the base is inferred from the string)This broke our parsing logic, which relied on the previous behaviour of the
int()
function to automatically determine the base of the string literal, based on a base prefix present in the string. Specifying base 0 was not a solution, as this resulted in parsing behaviour different from GNU as.Additionally, we never actually parsed octal in the format
0100
correctly - even before this PR; that number would have been interpreted as 100 rather than 64.So, to fix this, and to ensure our parsing matches the GNU assembler, this PR implements a custom
parse_int()
function, using the base prefix in a string to determine the correct base to pass toint()
. The following are supported:The
parse_int
method also supports the negative prefix operator for all of the above cases.This change also ensures
.int
,.long
,.word
directives correctly handle the above mentioned formats. This fixes the issue described in #104.Note: GNU as does not actually accept the octal prefix
0o...
, but we accept it as a convenience, as this is accepted in Python code. This means however, that our assembler accepts code which GNU as does not accept. But the other way around, we still accept all code that GNU as accepts, which was one of our goals.