From fdcea38e5e9cd3d8ea0da5b0c4442c05e7d54f6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20Reuh=20Fildadut?= Date: Fri, 14 Jan 2022 23:43:20 +0100 Subject: [PATCH] Update documentation --- LANGUAGE.md | 21 ++++++++ anselme.lua | 4 +- test/tests/implicit multiplication.ans | 11 ++++ test/tests/implicit multiplication.lua | 71 ++++++++++++++++++++++++++ 4 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 test/tests/implicit multiplication.ans create mode 100644 test/tests/implicit multiplication.lua diff --git a/LANGUAGE.md b/LANGUAGE.md index ac0dcc7..fcb5607 100644 --- a/LANGUAGE.md +++ b/LANGUAGE.md @@ -624,6 +624,25 @@ These can be used to represent some caracters in string and other text elements Only `0` and `nil` are false. Everything else is considered true. +#### Refering to an identifier + +Any defined identifier can be accessed from an expression by using its name; the identifier will be first searched in the current namespace, then go up until it finds it as described in [identifiers](#identifiers). + +What will happen then depends on what the identifier refer to: see [function calls](#function-calls) for functions and [checkpoint calls](#checkpoint-calls) for checkpoints. + +For variables, the identifier will returns the value of the variable when evaluated. + +When the identifier is preceeded by another expression directly (without any operator between the two), Anselme consider this to be an implicit multiplication. This behave in the same way as if there was a `*` operator between the expression and identifier, but has a priority just higher than explicit multiplication. + +``` +:x = 3 + +{2x} = {2*x} = 6 + +(Priority is made slighly higher to avoid parentheses in this kind of operation:) +{1/2x} = {1/(2*x)} = 1/6 +``` + #### Function calls The simplest way to call a function is simply to use its name. If the function has no arguments, parantheses are optional, or can be replaced with a `!`: @@ -959,6 +978,8 @@ This only works on strings: Variables for default types (each is associated to a string of the internal variable type name): `nil`, `number`, `string`, `list`, `pair`, `function reference`, `variable reference`. +The π constant is also defined in `pi`. + #### Built-in language scripts Anselme provides some scripts that define translated aliases for built-in variables and functions. Currently `enUS` (English) and `frFR` (French) are provided. diff --git a/anselme.lua b/anselme.lua index ffdeae6..8ea279e 100644 --- a/anselme.lua +++ b/anselme.lua @@ -488,7 +488,7 @@ local vm_mt = { -- expr: expression to evaluate (string or parsed expression), or a block to run -- will merge state after successful execution -- namespace(default=""): namespace to evaluate the expression in - -- tags(default={}): defaults tag when evaluating the expression + -- tags(default={}): defaults tags when evaluating the expression (Lua value) -- return interpreter in case of success -- returns nil, err in case of error run = function(self, expr, namespace, tags) @@ -555,7 +555,7 @@ local vm_mt = { -- merge state after sucessful execution automatically like :run -- expr: expression to evaluate (string or parsed expression), or a block to evaluate -- namespace(default=""): namespace to evaluate the expression in - -- tags(default={}): defaults tag when evaluating the expression + -- tags(default={}): defaults tags when evaluating the expression (Lua value) -- return value in case of success (nil if nothing returned) -- returns nil, err in case of error eval = function(self, expr, namespace, tags) diff --git a/test/tests/implicit multiplication.ans b/test/tests/implicit multiplication.ans new file mode 100644 index 0000000..3400bdc --- /dev/null +++ b/test/tests/implicit multiplication.ans @@ -0,0 +1,11 @@ +:x = 4 + +{2x} = 8 + +{(2+1)x} = 12 + +{2pi} = 2pi + +{1/2x} = 0.125 + +{(1/2)x} = 2 diff --git a/test/tests/implicit multiplication.lua b/test/tests/implicit multiplication.lua new file mode 100644 index 0000000..5fb882f --- /dev/null +++ b/test/tests/implicit multiplication.lua @@ -0,0 +1,71 @@ +local _={} +_[31]={} +_[30]={} +_[29]={} +_[28]={} +_[27]={} +_[26]={} +_[25]={} +_[24]={} +_[23]={} +_[22]={} +_[21]={text=" = 2",tags=_[31]} +_[20]={text="2.0",tags=_[30]} +_[19]={text=" = 0.125",tags=_[29]} +_[18]={text="0.125",tags=_[28]} +_[17]={text=" = 2pi",tags=_[27]} +_[16]={text="6.2831853071796",tags=_[26]} +_[15]={text=" = 12",tags=_[25]} +_[14]={text="12",tags=_[24]} +_[13]={text=" = 8",tags=_[23]} +_[12]={text="8",tags=_[22]} +_[11]={_[20],_[21]} +_[10]={_[18],_[19]} +_[9]={_[16],_[17]} +_[8]={_[14],_[15]} +_[7]={_[12],_[13]} +_[6]={"return"} +_[5]={"text",_[11]} +_[4]={"text",_[10]} +_[3]={"text",_[9]} +_[2]={"text",_[8]} +_[1]={"text",_[7]} +return {_[1],_[2],_[3],_[4],_[5],_[6]} +--[[ +{ "text", { { + tags = {}, + text = "8" + }, { + tags = {}, + text = " = 8" + } } } +{ "text", { { + tags = {}, + text = "12" + }, { + tags = {}, + text = " = 12" + } } } +{ "text", { { + tags = {}, + text = "6.2831853071796" + }, { + tags = {}, + text = " = 2pi" + } } } +{ "text", { { + tags = {}, + text = "0.125" + }, { + tags = {}, + text = " = 0.125" + } } } +{ "text", { { + tags = {}, + text = "2.0" + }, { + tags = {}, + text = " = 2" + } } } +{ "return" } +]]-- \ No newline at end of file