From 198c06e2e8c1e33954baf313eea8224cc5a2ad5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20Reuh=20Fildadut?= Date: Sat, 5 Jun 2021 00:06:00 +0200 Subject: [PATCH] Rename format to {} --- README.md | 4 ++-- interpreter/common.lua | 2 +- notes.txt | 3 ++- parser/common.lua | 6 ++++-- stdlib/bootscript.lua | 4 ---- stdlib/functions.lua | 7 +++++++ test/tests/custom text formatting.ans | 2 +- 7 files changed, 17 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 05e5077..dd1a42b 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/interpreter/common.lua b/interpreter/common.lua index d83ea01..2544ab3 100644 --- a/interpreter/common.lua +++ b/interpreter/common.lua @@ -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) diff --git a/notes.txt b/notes.txt index 943e4e4..2455d40 100644 --- a/notes.txt +++ b/notes.txt @@ -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: `^+-==", "<=", "<", ">", "+", "-", "*", "//", "/", "%", + "::", ":", "!", - "^", "::", ":", "()" + "^", + "()", "{}" }, -- 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) diff --git a/stdlib/bootscript.lua b/stdlib/bootscript.lua index 094ed4c..89ca8c8 100644 --- a/stdlib/bootscript.lua +++ b/stdlib/bootscript.lua @@ -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 ]] diff --git a/stdlib/functions.lua b/stdlib/functions.lua index 8949b60..7846b74 100644 --- a/stdlib/functions.lua +++ b/stdlib/functions.lua @@ -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", diff --git a/test/tests/custom text formatting.ans b/test/tests/custom text formatting.ans index 4c2fdd8..06ff053 100644 --- a/test/tests/custom text formatting.ans +++ b/test/tests/custom text formatting.ans @@ -7,5 +7,5 @@ $ Person(name, age) {abject} -$ format(p::person) +$ {}(p::person) @"Name: {p("name")}\nAge: {p("age")}"