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

F-strings and format #15

Open
gustavwilliam opened this issue Dec 26, 2020 · 13 comments
Open

F-strings and format #15

gustavwilliam opened this issue Dec 26, 2020 · 13 comments

Comments

@gustavwilliam
Copy link
Member

F-strings are one of the most convenient features in python. We should be able to implement something similar in Gurklang. I'm proposing the following syntax:

>>> "Gustav" :name var
>>> "Hello {name}!" f-string println
"Hello Gustav!"

This would parse the string and create a new string with the values of the variables with the names inside {}, in the string. Just like in python. An issue about string formatting with arguments will be opened shortly, but this would be the syntax for f-strings.

What do you think that the name should be? We don't have to call it f-str or f-string if we don't want to.

@decorator-factory
Copy link
Collaborator

decorator-factory commented Dec 27, 2020

We don't have to call it f-str or f-string if we don't want to.

Yeah, would be strange to call it that, since there's no f :)

@gustavwilliam
Copy link
Member Author

Hahah. I don't think we need the f to call it an f-string.

Any name options? And do you think this is a good way to implement something f-string like in Gurklang?

@decorator-factory
Copy link
Collaborator

decorator-factory commented Dec 27, 2020

My idea: make a function that turns a string into a parent-scoped function:

>>> "Hello, {name}!" % 
>>> inspect.dis
Disassembling function 1 (flags: PARENT_SCOPE, closure: none):
(Put :{% sentinel})
(Put "!")
(CallByName "name")
(Put "Hello, ")
(Put `--%-concat`)
(CallByValue)
>>> 
>>> "Gustav" :name var
>>> "Hello, {name}!" % ! println
"Hello, Gustav!"
>>>

This is similar to how template engines first compile a template to a function. The advantage is that this function could be implemented in Gurklang.

@decorator-factory
Copy link
Collaborator

decorator-factory commented Dec 27, 2020

Crude imperative implementaiton, using imaginary functions. Should work, but assumes the input is correct.

{ {(CallByValue) {:Put --%-concat},}] box :compiled var
  "" box :part var
  { { ("{") { compiled {:Put part ->},        <-cons-   part "" <- }
      ("}") { compiled {:CallByName part ->}, <-cons-   part "" <- }
      ( c ) { part { c concat } <= }
    } case
  } string foreach-codepoint
    
  compiled (CallByValue) <-append-  
  compiled -> invert-linked-list "%-string" inspect.make-function
  compiled <X- part <X-
} :% jar

@gustavwilliam
Copy link
Member Author

That looks quite interesting. Compiling it into a function also allows it to be passed around easily, in some sense. What would be the advantages of requiring ! instead of executing it straight away?

@decorator-factory
Copy link
Collaborator

@gustavwilliam Well, it takes quite some time to compile the function. So the compiled function can be stored and given a name.

@gustavwilliam
Copy link
Member Author

True. And it could help if the same f-string is reused multiple times.

The name % is already used in math though. Maybe %f could work as the name of the jar. Any other ideas?

@decorator-factory
Copy link
Collaborator

Ah yes, % is modulo. %f could be a float modulo (if it's not, it's still confusing).

%%? %string? %str? format (although what about the other format)?

@gustavwilliam
Copy link
Member Author

Float modulo doesn’t sound like it should really exist. I’m not a huge fan of calling it %% or %str either. It just seems a bit off imo.

Honestly, maybe f-str or f-string is best. Python users will immediately recognize it and it’s distinctive.

@decorator-factory
Copy link
Collaborator

decorator-factory commented Dec 27, 2020

What about interpolate? After all, we're doing string interpolation
Or an abbreviation, like inter, but that sounds weird.

@gustavwilliam
Copy link
Member Author

True. We could just call it interpolate. Maybe int, but that doesn't seem quite right.

Would you like to implement this, considering that you seem to have a semi-functional python version of this already?

@decorator-factory
Copy link
Collaborator

I'd like to work on the docs instead. Maybe I can assign you to implement string-related stuff?

@gustavwilliam
Copy link
Member Author

I could implement a lot of the other string commands — maybe this one as well. I’ll just have to figure out the best way to do string interpolation.

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

No branches or pull requests

2 participants