Skip to content

Allow uppercase hex chars in expressions #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions esp32_ulp/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,23 @@ def validate_expression(param):
for token in split_tokens(param):
state = 0
for c in token:
if c not in ' \t+-*/%()<>&|~x0123456789abcdef':
if c not in ' \t+-*/%()<>&|~xX0123456789abcdefABCDEF':
return False

# the following allows hex digits a-f after 0x but not otherwise
if state == 0:
if c in 'abcdef':
if c in 'abcdefABCDEF':
return False
if c == '0':
state = 1
continue

if state == 1:
state = 2 if c == 'x' else 0
state = 2 if c in 'xX' else 0
continue

if state == 2:
if c not in '0123456789abcdef':
if c not in '0123456789abcdefABCDEF':
state = 0
return True

Expand Down
8 changes: 8 additions & 0 deletions tests/compat/expr.S
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,11 @@ exit:
move r3, 0x1234 & ~2
move r3, 42|4&0xf # 46 (4&0xf is evaluated first)
move r3, (42|4)&0xf # 14 (42|4 is evaluated first)

# ---
# test that expressions accept hex characters in either upper or lower case
move r3, 0xaa - 1
move r3, 0xBB - 1
move r3, 0xCc - 1
move r3, 0Xdd - 1
move r3, 0XEF - 1
5 changes: 5 additions & 0 deletions tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ def test_validate_expression():
assert validate_expression('0x100 & ~2') is True
assert validate_expression('0xabcdef') is True
assert validate_expression('0x123def') is True
assert validate_expression('0xABC') is True
assert validate_expression('0xaBc') is True
assert validate_expression('0Xabc') is True
assert validate_expression('0X123ABC') is True
assert validate_expression('2*3+4/5&6|7') is True
assert validate_expression('(((((1+1) * 2') is True # valid characters, even if expression is not valid

Expand All @@ -55,6 +59,7 @@ def test_validate_expression():
assert validate_expression('123 ^ 4') is False # operator not supported for now
assert validate_expression('evil()') is False
assert validate_expression('def cafe()') is False # valid hex digits, but potentially dangerous code
assert validate_expression('def CAFE()') is False


@test
Expand Down