From ef5e1a577fe8034b0a9630ff2c5f4f47ccc4f166 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20Reuh=20Fildadut?= Date: Fri, 16 Sep 2022 14:19:48 +0900 Subject: [PATCH] Re-add alternative : operator for pairs --- LANGUAGE.md | 6 ++++-- notes.txt | 13 ++----------- parser/common.lua | 6 ++++-- parser/expression.lua | 2 +- stdlib/functions.lua | 9 +++++++++ test/tests/pair operator.ans | 2 ++ test/tests/pair operator.lua | 20 ++++++++++++++------ 7 files changed, 36 insertions(+), 22 deletions(-) diff --git a/LANGUAGE.md b/LANGUAGE.md index f28d1ee..a259471 100644 --- a/LANGUAGE.md +++ b/LANGUAGE.md @@ -621,7 +621,7 @@ Default types are: * `string`: a string. Can be defined between double quotes `"string"`. Support [text interpolation](#text-interpolation). Support [escape codes](#escape-codes). -* `pair`: a couple of values. Types can be mixed. Can be defined using equal sign `"key"=5`. Pairs named by a string that is also a valid identifier can be created using the `key=5` shorthand syntax; `key` will not be interpreted as the variable `key` but the string `"key"` (if `key` is a variable and you want to force the use of its value as a key instead of the string `"key"`, you can wrap it in parentheses). +* `pair`: a couple of values. Types can be mixed. Can be defined using equal sign `"key"=5` or a colon `"key":5`. Pairs named by a string that is also a valid identifier can be created using the `key=5` shorthand syntax; `key` will not be interpreted as the variable `key` but the string `"key"` (if `key` is a variable and you want to force the use of its value as a key instead of the string `"key"`, you can either wrap it in parentheses or use the colon syntax). * `annotated`: a couple of values. Types can be mixed. Can be defined using colon `expr::type`. The second value is used in type constraints, this is intended to be use to give a custom type to a value. @@ -1023,7 +1023,9 @@ This only works on strings: `a;`: evaluate a, discard its result, returns nil. -`a = b`: evaluate a and b, returns a new pair with a as key and b as value. If a is an identifier, will interpret it as a string (and not a variable; you can wrap a in parentheses if you want to use the value associated with variable a instead). +`a = b`: evaluate a and b, returns a new pair with a as key and b as value. If a is an identifier, will interpret it as a string (and not a variable; you can wrap a in parentheses if you want to use the value associated with variable a instead, or just use the alternative pair operator `a:b` which does not have this behavior). + +`a : b`: evaluate a and b, returns a new pair with a as key and b as value. Unlike the `a=b` operator, will evaluate a as and will get the value associated with the identifier if a is a variable identifier. Can be used for example to benefit from variable aliases and have translatable keys. `a :: b`: evaluate a and b, returns a new annotated value with a as value and b as the annotation. This annotation will be checked in type constraints. diff --git a/notes.txt b/notes.txt index f15fb29..a23ad37 100644 --- a/notes.txt +++ b/notes.txt @@ -19,11 +19,9 @@ In the end, we decided to reserve all of those except '. Using other unicode symbols may be also alright, but there also should be a way to only use these symbols. -TODO: add alias to ยง - Reserved symbols that are still not used in expressions: `\_?@$ -Reserved symbols that are still not used as a line type: `^+-==_", "_<=_", "_<_", "_>_", "_+_", "_-_", "_*_", "_//_", "_/_", "_%_", - "_::_", "_=_", + "_::_", "_^_", - "_._", "_!_", + "_!_", + "_._", -- suffix unop "_;", "_!", diff --git a/parser/expression.lua b/parser/expression.lua index 0fb52dd..d80f42d 100644 --- a/parser/expression.lua +++ b/parser/expression.lua @@ -6,7 +6,7 @@ local binops_prio = { [2] = { ":=", "+=", "-=", "//=", "/=", "*=", "%=", "^=" }, [3] = { "," }, [4] = { "~?", "~", "#" }, - [5] = { "=" }, + [5] = { "=", ":" }, [6] = { "|", "&" }, [7] = { "!=", "==", ">=", "<=", "<", ">" }, [8] = { "+", "-" }, diff --git a/stdlib/functions.lua b/stdlib/functions.lua index 517a43b..02b15bf 100644 --- a/stdlib/functions.lua +++ b/stdlib/functions.lua @@ -64,6 +64,15 @@ lua_functions = { } end }, + ["_:_(a, b)"] = { + mode = "raw", + value = function(a, b) + return { + type = "pair", + value = { a, b } + } + end + }, -- annotate ["_::_(a, b)"] = { mode = "raw", diff --git a/test/tests/pair operator.ans b/test/tests/pair operator.ans index e428ac5..68cc034 100644 --- a/test/tests/pair operator.ans +++ b/test/tests/pair operator.ans @@ -1,3 +1,5 @@ :name = "test" {[name=1, 3="p", (name)="foo", "ho"="ah", name=name]} + +{["name":1, 3:"p", name:"foo", "ho":"ah", "name":name]} diff --git a/test/tests/pair operator.lua b/test/tests/pair operator.lua index 43b90fd..4b2af01 100644 --- a/test/tests/pair operator.lua +++ b/test/tests/pair operator.lua @@ -1,11 +1,19 @@ local _={} -_[5]={} -_[4]={tags=_[5],text="[name=1, 3=p, test=foo, ho=ah, name=test]"} -_[3]={_[4]} -_[2]={"return"} -_[1]={"text",_[3]} -return {_[1],_[2]} +_[9]={} +_[8]={} +_[7]={tags=_[9],text="[name=1, 3=p, test=foo, ho=ah, name=test]"} +_[6]={tags=_[8],text="[name=1, 3=p, test=foo, ho=ah, name=test]"} +_[5]={_[7]} +_[4]={_[6]} +_[3]={"return"} +_[2]={"text",_[5]} +_[1]={"text",_[4]} +return {_[1],_[2],_[3]} --[[ +{ "text", { { + tags = {}, + text = "[name=1, 3=p, test=foo, ho=ah, name=test]" + } } } { "text", { { tags = {}, text = "[name=1, 3=p, test=foo, ho=ah, name=test]"