Skip to content
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

More improvements to MatchString codegen on Python #1269

Merged
merged 3 commits into from
Nov 21, 2024

Conversation

johnynek
Copy link
Owner

@johnynek johnynek commented Nov 21, 2024

I am mostly doing this to just practice improving code generation on an easier target than C. Since I can run the tests under python I have good confidence the changes make sense. Also, the generated code is fairly easy to read.

With this PR, these examples:

def starts_with_foo(s): s matches "foo${_}"
def ends_with_foo(s): s matches "${_}foo"
def contains_foo(s): s matches "${_}foo${_}"
def contains_foo_bar(s): s matches "${_}foo${_}bar${_}"

compile to:

def starts_with_foo(s):
    ___t14 = 0
    return s.startswith("foo", 0)

def ends_with_foo(s):
    ___t15 = 0
    return s.endswith("foo")

def contains_foo(s):
    ___t16 = 0
    return "foo" in s

def contains_foo_bar(s):
    ___t17 = 0
    ___t18 = 0
    ___t19 = False
    ___t20 = s.find("foo", ___t18)
    if ___t20 > -1:
        ___t21 = ___t20 + 3
        if s.find("bar", ___t21) > -1:
            ___t19 = True
            ___t18 = -1
        else:
            ___t18 = ___t20 + 1
    else:
        ___t18 = -1
    return ___t19

@johnynek johnynek merged commit 6fd5607 into main Nov 21, 2024
8 checks passed
@johnynek johnynek deleted the oscar/20241121_improve_python_matchstring branch November 21, 2024 23:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant