mirror of
https://github.com/Reuh/anselme.git
synced 2025-10-28 17:19:31 +00:00
Rename format to {}
This commit is contained in:
parent
64bc85741a
commit
198c06e2e8
7 changed files with 17 additions and 11 deletions
|
|
@ -403,7 +403,7 @@ Text and choice lines allow for arbitrary text. Expression can be evaluated and
|
||||||
Value of a: {a}
|
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
|
### Event buffer
|
||||||
|
|
||||||
|
|
@ -847,7 +847,7 @@ This only works on strings:
|
||||||
|
|
||||||
##### Various
|
##### 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.
|
`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.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -59,7 +59,7 @@ common = {
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
-- format a anselme value to something printable
|
-- 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
|
-- str: if success
|
||||||
-- * nil, err: if error
|
-- * nil, err: if error
|
||||||
format = function(val)
|
format = function(val)
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ TODO: add alias to §
|
||||||
|
|
||||||
Reserved symbols that are still not used in expressions: ~`\_?@$#
|
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
|
# 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
|
* can be used for better static variant selection; if everything is type annotated, selection could be restricted to a single function
|
||||||
Disadvantages:
|
Disadvantages:
|
||||||
* idk if it's worth the trouble
|
* 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
|
TODO: allow to define aliases after the initial definition
|
||||||
~ alias(number, "nombre")
|
~ alias(number, "nombre")
|
||||||
|
|
|
||||||
|
|
@ -34,8 +34,10 @@ common = {
|
||||||
"!=", "==", ">=", "<=", "<", ">",
|
"!=", "==", ">=", "<=", "<", ">",
|
||||||
"+", "-",
|
"+", "-",
|
||||||
"*", "//", "/", "%",
|
"*", "//", "/", "%",
|
||||||
|
"::", ":",
|
||||||
"!",
|
"!",
|
||||||
"^", "::", ":", "()"
|
"^",
|
||||||
|
"()", "{}"
|
||||||
},
|
},
|
||||||
-- escapement code and their value in strings
|
-- 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
|
-- 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 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
|
if not rem:match("^%s*}") then return nil, ("expected closing } at end of expression before %q"):format(rem) end
|
||||||
-- wrap in format() call
|
-- 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
|
if not variant then return variant, err end
|
||||||
-- add to text
|
-- add to text
|
||||||
table.insert(l, variant)
|
table.insert(l, variant)
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,4 @@ return [[
|
||||||
:string="string"
|
:string="string"
|
||||||
:list="list"
|
:list="list"
|
||||||
:pair="pair"
|
:pair="pair"
|
||||||
|
|
||||||
(Default formatter: doesn't do anything and let built-in formatters do the job)
|
|
||||||
$ format(v)
|
|
||||||
@v
|
|
||||||
]]
|
]]
|
||||||
|
|
|
||||||
|
|
@ -117,6 +117,13 @@ functions = {
|
||||||
return v
|
return v
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
|
-- format
|
||||||
|
["{}(v)"] = {
|
||||||
|
mode = "raw",
|
||||||
|
value = function(v)
|
||||||
|
return v
|
||||||
|
end
|
||||||
|
},
|
||||||
-- pair methods
|
-- pair methods
|
||||||
["name(p::pair)"] = {
|
["name(p::pair)"] = {
|
||||||
mode = "untyped raw",
|
mode = "untyped raw",
|
||||||
|
|
|
||||||
|
|
@ -7,5 +7,5 @@ $ Person(name, age)
|
||||||
|
|
||||||
{abject}
|
{abject}
|
||||||
|
|
||||||
$ format(p::person)
|
$ {}(p::person)
|
||||||
@"Name: {p("name")}\nAge: {p("age")}"
|
@"Name: {p("name")}\nAge: {p("age")}"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue