johnynek / bosatsu

A python-ish pure and total functional programming language
Apache License 2.0
226 stars 11 forks source link

Pass in MatchString if a string match must be total (which we already knew) #1266

Closed johnynek closed 5 days ago

johnynek commented 5 days ago

This also improves the python code generation somewhat after looking more at the generated code.

johnynek commented 5 days ago

with this we get

def string_to_Char(s: String) -> Option[Char]:
  match s:
    case "$.{c}": Some(c)
    case _: None

compiling to:

def string_to_Char(s):
    if s.__len__() == 1:
        return (1, s[0])
    else:
        return (0,)

and

def last_String(s: String) -> Option[Char]:
  match s:
    case "": None
    case "${_}$.{l}": Some(l)

compiling to:

def last_String(s):
    if s == "":
        return (0,)
    else:
        return (1, s[-1])

Even though C backend is the priority, by improving python code-gen we learn about how to make codegen in general better.