JSAbrahams / mamba

🐍 The Mamba programming language, because we care about safety
MIT License
85 stars 3 forks source link

Type check contents of f-Strings #182

Closed JSAbrahams closed 4 years ago

JSAbrahams commented 4 years ago

Relevant issues

Fixes #178

Summary

Add f-strings, which can have expressions contained within { and }. I.e.:

def my_name <- "Joël"
print "My name is {my_name}."

The expressions contained within the curly brackets are type-checked just as any other expression. In addition to this, we verify that they are indeed expressions (and not statements) and that the resulting value has the __str__ method. This means that we are slightly more strict than Python, which would in the case of getting the string value of a value that does not have the __str__ instead print its memory address:

class A:
     my_field = 20

a = A()
/* prints <__main__.A object at 0x012OHD70> */
print(a)

In addition to this, strings which do not contain any expressions are treated and desugared as regular strings. So: "my_string {my_value}" becomes f"my_string {my_value}" "Hello World" becomes/stays "Hello World"

Added Tests