-
Notifications
You must be signed in to change notification settings - Fork 25
Preprocessor for ULP/RTC macros #43
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
Changes from 1 commit
79db90f
84d734d
ec81ecc
c184924
25d34b0
76a81ac
56f4530
9907b10
27ab850
54b117e
feb42dc
87507c9
99352a3
d76fd26
4dded94
219f939
5c3eeb8
3e8c0d5
ac1de99
8d88fd1
46f1442
2f6ee78
69ae946
4f90f76
d44384f
254adf9
c3bd101
2a0a39a
47d5e8a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
from uctypes import struct, addressof, LITTLE_ENDIAN, UINT32, BFUINT32, BF_POS, BF_LEN | ||
|
||
from .soc import * | ||
from .util import split_tokens, validate_expression | ||
|
||
# XXX dirty hack: use a global for the symbol table | ||
symbols = None | ||
|
@@ -267,6 +268,20 @@ def make_ins(layout): | |
ARG = namedtuple('ARG', ('type', 'value', 'raw')) | ||
|
||
|
||
def eval_arg(arg): | ||
parts = [] | ||
for token in split_tokens(arg): | ||
if symbols.has_sym(token): | ||
_, _, sym_value = symbols.get_sym(token) | ||
parts.append(str(sym_value)) | ||
else: | ||
parts.append(token) | ||
parts = "".join(parts) | ||
if not validate_expression(parts): | ||
raise ValueError('Unsupported expression: %s' % parts) | ||
return eval(parts) | ||
|
||
|
||
def arg_qualify(arg): | ||
""" | ||
look at arg and qualify its type: | ||
|
@@ -289,8 +304,12 @@ def arg_qualify(arg): | |
return ARG(IMM, int(arg), arg) | ||
except ValueError: | ||
pass | ||
entry = symbols.get_sym(arg) | ||
return ARG(SYM, entry, arg) | ||
try: | ||
entry = symbols.get_sym(arg) | ||
return ARG(SYM, entry, arg) | ||
except KeyError: | ||
pass | ||
return ARG(IMM, int(eval_arg(arg)), arg) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seen my previous comment? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed now. |
||
|
||
|
||
def get_reg(arg): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from esp32_ulp.util import split_tokens, validate_expression | ||
|
||
tests = [] | ||
|
||
|
||
def test(param): | ||
""" | ||
the @test decorator | ||
""" | ||
tests.append(param) | ||
|
||
|
||
@test | ||
def test_split_tokens(): | ||
assert split_tokens("") == [] | ||
assert split_tokens("t") == ['t'] | ||
assert split_tokens("test") == ['test'] | ||
assert split_tokens("t t") == ['t', ' ', 't'] | ||
assert split_tokens("t,t") == ['t', ',', 't'] | ||
assert split_tokens("test(arg)") == ['test', '(', 'arg', ')'] | ||
assert split_tokens("test(arg,arg2)") == ['test', '(', 'arg', ',', 'arg2', ')'] | ||
assert split_tokens("test(arg,arg2)") == ['test', '(', 'arg', ',', 'arg2', ')'] | ||
assert split_tokens(" test( arg, arg2)") == [' ', 'test', '(', ' ', 'arg', ',', ' ', 'arg2', ')'] | ||
assert split_tokens(" test( arg ) ") == [' ', 'test', '(', ' ', 'arg', ' ', ')', ' '] | ||
assert split_tokens("\t test \t ") == ['\t ', 'test', " \t "] | ||
assert split_tokens("test\nrow2") == ['test', "\n", "row2"] | ||
|
||
# split_token does not support comments. should generally only be used after comments are already stripped | ||
assert split_tokens("test(arg /*comment*/)") == ['test', '(', 'arg', ' ', '/', '*', 'comment', '*', '/', ')'] | ||
assert split_tokens("#test") == ['#', 'test'] | ||
|
||
|
||
@test | ||
def test_validate_expression(): | ||
assert validate_expression('') is True | ||
assert validate_expression('1') is True | ||
assert validate_expression('1+1') is True | ||
assert validate_expression('(1+1)') is True | ||
assert validate_expression('(1+1)*2') is True | ||
assert validate_expression('(1 + 1)') is True | ||
assert validate_expression('10 % 2') is True | ||
assert validate_expression('0x100 << 2') is True | ||
assert validate_expression('0x100 & ~2') is True | ||
assert validate_expression('0xabcdef') is True | ||
assert validate_expression('0x123def') 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 | ||
|
||
assert validate_expression(':') is False | ||
assert validate_expression('_') is False | ||
assert validate_expression('=') is False | ||
assert validate_expression('.') is False | ||
assert validate_expression('!') is False | ||
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 | ||
|
||
ThomasWaldmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if __name__ == '__main__': | ||
# run all methods marked with @test | ||
for t in tests: | ||
t() |
Uh oh!
There was an error while loading. Please reload this page.