1
0
Fork 0
mirror of https://github.com/Reuh/anselme.git synced 2025-10-27 16:49:31 +00:00

Rename format to {}

This commit is contained in:
Étienne Fildadut 2021-06-05 00:06:00 +02:00
parent 64bc85741a
commit 198c06e2e8
7 changed files with 17 additions and 11 deletions

View file

@ -403,7 +403,7 @@ Text and choice lines allow for arbitrary text. Expression can be evaluated and
Value of a: {a}
```
The expression is automatically wrapped in a call to `format(expr)`. You can overload `format` to change its behaviour for custom types.
The expression is automatically wrapped in a call to `{}(expr)`. You can overload `{}` to change its behaviour for custom types.
### Event buffer
@ -847,7 +847,7 @@ This only works on strings:
##### Various
`format(v)`: function called when formatting a value in a text interpolation
`{}(v)`: function called when formatting a value in a text interpolation for printing
`rand([m[, n]])`: when called whitout arguments, returns a random float in [0,1). Otherwise, returns a random number in [m,n]; m=1 if not given.

View file

@ -59,7 +59,7 @@ common = {
end
end,
-- format a anselme value to something printable
-- does not call custom format() functions, only built-in ones, so it should not be able to fail
-- does not call custom {}() functions, only built-in ones, so it should not be able to fail
-- str: if success
-- * nil, err: if error
format = function(val)

View file

@ -23,7 +23,7 @@ TODO: add alias to §
Reserved symbols that are still not used in expressions: ~`\_?@$#
Reserved symbols that are still not used as a line type: `^+-=</[]*{}|\_!?.,;()"&%
Reserved symbols that are still not used as a line type: `^+-=</[]*{}|\_!?.,;)"&%
# Code Q&A
@ -52,6 +52,7 @@ Advantages:
* can be used for better static variant selection; if everything is type annotated, selection could be restricted to a single function
Disadvantages:
* idk if it's worth the trouble
* could do something like `$ ()(l::list(?), i::number)::?`, but then can't return nil on not found...
TODO: allow to define aliases after the initial definition
~ alias(number, "nombre")

View file

@ -34,8 +34,10 @@ common = {
"!=", "==", ">=", "<=", "<", ">",
"+", "-",
"*", "//", "/", "%",
"::", ":",
"!",
"^", "::", ":", "()"
"^",
"()", "{}"
},
-- escapement code and their value in strings
-- I don't think there's a point in supporting form feed, carriage return, and other printer and terminal related codes
@ -136,7 +138,7 @@ common = {
if not exp then return nil, rem end
if not rem:match("^%s*}") then return nil, ("expected closing } at end of expression before %q"):format(rem) end
-- wrap in format() call
local variant, err = common.find_function_variant(state, namespace, "format", exp, true)
local variant, err = common.find_function_variant(state, namespace, "{}", exp, true)
if not variant then return variant, err end
-- add to text
table.insert(l, variant)

View file

@ -5,8 +5,4 @@ return [[
:string="string"
:list="list"
:pair="pair"
(Default formatter: doesn't do anything and let built-in formatters do the job)
$ format(v)
@v
]]

View file

@ -117,6 +117,13 @@ functions = {
return v
end
},
-- format
["{}(v)"] = {
mode = "raw",
value = function(v)
return v
end
},
-- pair methods
["name(p::pair)"] = {
mode = "untyped raw",

View file

@ -7,5 +7,5 @@ $ Person(name, age)
{abject}
$ format(p::person)
$ {}(p::person)
@"Name: {p("name")}\nAge: {p("age")}"