From 69d2bbaaf3b9dea4e4ebc224cc32ca26b409bb25 Mon Sep 17 00:00:00 2001 From: Anthony Debucquoy Date: Tue, 29 Apr 2025 21:38:31 +0200 Subject: [PATCH] Fixing loop variables --- examples/boucles_var.spf | 15 +++++++++++++++ spf.lark | 8 ++++---- spf.py | 4 +++- 3 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 examples/boucles_var.spf diff --git a/examples/boucles_var.spf b/examples/boucles_var.spf new file mode 100644 index 0000000..3984004 --- /dev/null +++ b/examples/boucles_var.spf @@ -0,0 +1,15 @@ +entier x = 0; +tant que x < 10 faire { + entier y = x; + x = x + 1; + afficher x, y; +} + +afficher "yess"; + +pour chaque entier i dans [1:5] faire { + entier y = i; + y = y + i; + x = x + i; + afficher x, y; +} diff --git a/spf.lark b/spf.lark index a87728a..346696d 100644 --- a/spf.lark +++ b/spf.lark @@ -1,10 +1,10 @@ start: (instruction)* instruction: declaration ";" - | assignation ";" - | SHOW_KW expression ("," expression)* ";" -> afficher - | ADD_KW expression "dans" VARIABLE ";" -> append - | controls + | assignation ";" + | SHOW_KW expression ("," expression)* ";" -> afficher + | ADD_KW expression "dans" VARIABLE ";" -> append + | controls // rule finishing by u are "UnambigiousED" expression: logical diff --git a/spf.py b/spf.py index 7601c2e..4b19207 100755 --- a/spf.py +++ b/spf.py @@ -19,7 +19,7 @@ class SPFInterpreter(lark.visitors.Interpreter): old = self.variables.variables.copy() while self.visit_children(el.children[0])[0]: self.visit_children(el.children[1]) - self.variables.variables = old.copy() + self.variables.variables = old.copy() def for_loop(self, el): type = el.children[0].value @@ -30,6 +30,7 @@ class SPFInterpreter(lark.visitors.Interpreter): except SPFException as e: e.errorline = el.meta.line raise e + old_inloop = self.variables.variables.copy() target = self.visit_children(el.children[2])[0] for i in target: @@ -39,6 +40,7 @@ class SPFInterpreter(lark.visitors.Interpreter): e.errorline = el.meta.line raise e self.visit_children(el.children[3]) + self.variables.variables = old_inloop.copy() self.variables.variables = old.copy() def afficher(self, el):