From 85d17e951938f90557bcf98f2f321e9cd5263b1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20Reuh=20Fildadut?= Date: Sun, 7 Jan 2024 18:25:13 +0100 Subject: [PATCH] Add stdlib frFR translation --- anselme/state/State.lua | 26 ++++++++++- anselme/stdlib/environment.lua | 43 +++++++++++++++++ anselme/stdlib/function.lua | 16 +++---- anselme/stdlib/init.lua | 3 +- anselme/stdlib/language/frFR.lua | 80 ++++++++++++++++++++++++++++++++ anselme/stdlib/type check.lua | 4 +- 6 files changed, 159 insertions(+), 13 deletions(-) create mode 100644 anselme/stdlib/environment.lua create mode 100644 anselme/stdlib/language/frFR.lua diff --git a/anselme/state/State.lua b/anselme/state/State.lua index 5c4af20..aaf5618 100644 --- a/anselme/state/State.lua +++ b/anselme/state/State.lua @@ -11,6 +11,7 @@ local uuid = require("anselme.common").uuid local parser = require("anselme.parser") local binser = require("anselme.lib.binser") local assert0 = require("anselme.common").assert0 +local operator_priority = require("anselme.common").operator_priority local anselme local Identifier, Return, Node @@ -40,8 +41,29 @@ State = class { --- Load standard library. -- You will probably want to call this on every State right after creation. - load_stdlib = function(self) - require("anselme.stdlib")(self) + load_stdlib = function(self, language) + local stdlib = require("anselme.stdlib") + if language then + self.scope:push_export() + self.scope:push() + stdlib(self) + self.scope:pop() + local exported = self.scope:capture() + self.scope:pop() + + for name, var in exported.variables:iter(self) do + if operator_priority[name.name] then + self.scope:define(var:get_symbol(), var:get(self)) + end + end + + self.scope:push_partial(Identifier:new("stdlib")) + self.scope:define(Identifier:new("stdlib"):to_symbol(), exported) + parser(require("anselme.stdlib.language."..language), "stdlib/language/"..language..".ans"):eval(self) + self.scope:pop() + else + stdlib(self) + end end, ---## Branching and merging diff --git a/anselme/stdlib/environment.lua b/anselme/stdlib/environment.lua new file mode 100644 index 0000000..50bc060 --- /dev/null +++ b/anselme/stdlib/environment.lua @@ -0,0 +1,43 @@ +local ast = require("anselme.ast") +local Nil, Boolean, Definition = ast.Nil, ast.Boolean, ast.Definition +local assert0 = require("anselme.common").assert0 + +return { + { + "defined", "(c::is environment, s::is string, search parent::is boolean=false)", + function(state, env, s, l) + if l:truthy() then + return Boolean:new(env:defined(state, s:to_identifier())) + else + return Boolean:new(env:defined_in_current(state, s:to_identifier())) + end + end + }, + + { + "_._", "(c::is environment, s::is string)", + function(state, env, s) + local identifier = s:to_identifier() + assert0(env:defined(state, identifier), ("no variable %q defined in environment"):format(s.string)) + return env:get(state, identifier) + end + }, + { + "_._", "(c::is environment, s::is string) = v", + function(state, env, s, v) + local identifier = s:to_identifier() + assert0(env:defined(state, identifier), ("no variable %q defined in environment"):format(s.string)) + env:set(state, identifier, v) + return Nil:new() + end + }, + { + "_._", "(c::is environment, s::is symbol) = v", + function(state, env, s, v) + state.scope:push(env) + local r = Definition:new(s, v):eval(state) + state.scope:pop() + return r + end + }, +} diff --git a/anselme/stdlib/function.lua b/anselme/stdlib/function.lua index d35c8f6..570134d 100644 --- a/anselme/stdlib/function.lua +++ b/anselme/stdlib/function.lua @@ -4,15 +4,13 @@ local assert0 = require("anselme.common").assert0 return { { - "defined", "(c::is function, s::is string)", - function(state, c, s) - return Boolean:new(c.scope:defined_in_current(state, s:to_identifier())) - end - }, - { - "has upvalue", "(c::is function, s::is string)", - function(state, c, s) - return Boolean:new(c.scope:defined(state, s:to_identifier())) + "defined", "(c::is function, s::is string, search parent::is boolean=false)", + function(state, c, s, l) + if l:truthy() then + return Boolean:new(c.scope:defined(state, s:to_identifier())) + else + return Boolean:new(c.scope:defined_in_current(state, s:to_symbol())) + end end }, diff --git a/anselme/stdlib/init.lua b/anselme/stdlib/init.lua index e4efea6..e4c238e 100644 --- a/anselme/stdlib/init.lua +++ b/anselme/stdlib/init.lua @@ -2,7 +2,7 @@ local parser = require("anselme.parser") local function define_lua(state, list) for _, fn in ipairs(list) do - state.scope:define_lua(fn[1], fn[2], fn[3], true) + state.scope:define_lua("@"..fn[1], fn[2], fn[3], true) end end local function load(state, l) @@ -32,6 +32,7 @@ return function(main_state) "structures", "wrap", "attached block", + "environment", "function", "resume", "persist", diff --git a/anselme/stdlib/language/frFR.lua b/anselme/stdlib/language/frFR.lua new file mode 100644 index 0000000..241e9b2 --- /dev/null +++ b/anselme/stdlib/language/frFR.lua @@ -0,0 +1,80 @@ +return [[ +:@bloc attaché = stdlib.attached block + +:@afficher = stdlib.print +:@hash = stdlib.hash +:@erreur = stdlib.error + +:@type = stdlib.type + +:@vrai = stdlib.true +:@faux = stdlib.false + +:@défini = stdlib.defined +:@surcharge = stdlib.overload + +:@si = stdlib.if +:@sinon = stdlib.else +:@sinon si = stdlib.else if +:@tant que = stdlib.while +:@pour = stdlib.for +:@itérer = stdlib.iter +:@intervalle = stdlib.range + +:@casser = stdlib.break +:@continuer = stdlib.continue +:@renvoyer = stdlib.return + +:@longueur = stdlib.len +:@contient = stdlib.has +:@trouver = stdlib.find +:@insérer = stdlib.insert +:@retirer = stdlib.remove + +:@vers struct = stdlib.to struct +:@vers tuple = stdlib.to tuple +:@vers châine = stdlib.to string + +:@depuis = stdlib.from +:@cible de reprise = stdlib.resume target +:@en reprise = stdlib.resuming + +:@script = stdlib.script +:@suivant = stdlib.next +:@cycle = stdlib.cycle +:@aléatoire = stdlib.random + +:@nom = stdlib.name +:@valeur = stdlib.value + +:@pi = stdlib.pi +:@supérieur = stdlib.ceil +:@inférieur = stdlib.floor +:@arrondi = stdlib.round +:@aléatoire = stdlib.rand + +:@égal = stdlib.equal +:@est = stdlib.is +:@est une ancre = stdlib.is anchor +:@est un booléen = stdlib.is boolean +:@est appelable = stdlib.is callable +:@est une fonction = stdlib.is function +:@est une liste = stdlib.is list +:@est un dictionnaire = stdlib.is map +:@est nul = stdlib.is nil +:@est un nombre = stdlib.is number +:@est une surcharge = stdlib.is overload +:@est une paire = stdlib.is pair +:@est un intervalle = stdlib.is range +:@est une séquence = stdlib.is sequence +:@est une chaîne = stdlib.is string +:@est une struct = stdlib.is struct +:@est un symbole = stdlib.is symbol +:@est une table = stdlib.is table +:@est un texte = stdlib.is text +:@est un tuple = stdlib.is tuple +:@est un environnement = stdlib.is environment + +:@fusionner branche = stdlib.merge branch +:@persister = stdlib.persist +]] diff --git a/anselme/stdlib/type check.lua b/anselme/stdlib/type check.lua index e0da8a0..0f48ec4 100644 --- a/anselme/stdlib/type check.lua +++ b/anselme/stdlib/type check.lua @@ -20,7 +20,9 @@ return [[ :@is struct = is("struct") :@is table = is("table") +:@is environment = is("environment") + :@is function = is("function") :@is overload = is("overload") -:@is callable = $(x) x!type == "overload" | x!type == "function" | x!type == "lua function" | x!type == "quote" +:@is callable = $(x) x!type == "overload" | x!type == "function" | x!type == "quote" ]] \ No newline at end of file