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

Re-add alternative : operator for pairs

This commit is contained in:
Étienne Fildadut 2022-09-16 14:19:48 +09:00
parent 4284b06f10
commit ef5e1a577f
7 changed files with 36 additions and 22 deletions

View file

@ -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.

View file

@ -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: `^+-=</[]*{}|\_!?.,;)"&%
Reserved symbols that are still not used as a line type: `^+-=</[]*{}|\_!?.,;)"&%$
# Code Q&A
@ -49,13 +47,6 @@ TODO: support parametric types in contraints: list(number)
(push a constraint context when checking compatibility)
or more generic constraints? allow custom functions to check constraints, etc (performance?)
TODO: a way to alias key names in pairs/maps? the alias should not end up in the save file... we could use variable for that, like
:foo:bar = "key"
:map = {bar=42}
i.e. just remove the special case of interpreting identifiers as strings in pairs. But then might need to distinguish from the named argument syntax in function calls.
TODO: type system is not super nice to use.
UPDATE: tried to implement a static type system, ain't worth it. Would require major refactoring to go full static with good type inference. The language is supposed to allow to not have to worry about low level stuff, so no deal if the type inference isn't good. Thank you multiple dispatch for making everything complicated (i still love you though)... Well, if I ever reimplement Anselme, let's thank the Julia devs for doing the hard work: https://docs.julialang.org/en/v1/devdocs/inference/
@ -68,7 +59,7 @@ TODO: fn/checkpoint/tag: maybe consider them a regular func call that takes chil
would allow more flexibility esp. for tags...
a func def would be:
:a
:a = $
stuff
but then args?

View file

@ -39,12 +39,14 @@ common = {
"&_",
-- binop
"_;_",
"_=_", "_:_",
"_!=_", "_==_", "_>=_", "_<=_", "_<_", "_>_",
"_+_", "_-_",
"_*_", "_//_", "_/_", "_%_",
"_::_", "_=_",
"_::_",
"_^_",
"_._", "_!_",
"_!_",
"_._",
-- suffix unop
"_;",
"_!",

View file

@ -6,7 +6,7 @@ local binops_prio = {
[2] = { ":=", "+=", "-=", "//=", "/=", "*=", "%=", "^=" },
[3] = { "," },
[4] = { "~?", "~", "#" },
[5] = { "=" },
[5] = { "=", ":" },
[6] = { "|", "&" },
[7] = { "!=", "==", ">=", "<=", "<", ">" },
[8] = { "+", "-" },

View file

@ -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",

View file

@ -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]}

View file

@ -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]"