From a22bc69491fe060bedfb048d72aec891c10ccc6d Mon Sep 17 00:00:00 2001 From: Anthony Debucquoy Date: Thu, 20 Mar 2025 21:37:08 +0100 Subject: [PATCH] Adding range --- examples/lists.spf | 9 +++++++++ spf.lark | 2 +- spf.py | 4 ++++ 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 examples/lists.spf diff --git a/examples/lists.spf b/examples/lists.spf new file mode 100644 index 0000000..9dfb9ef --- /dev/null +++ b/examples/lists.spf @@ -0,0 +1,9 @@ +liste premier = [1, 2, 3]; + +ajouter 4 dans premier; + +liste deuxieme = [1:4]; + +liste troisième = [deuxieme[0]: 10]; + +liste quatrième = [1: troisième[6]]; diff --git a/spf.lark b/spf.lark index 55f6bd1..e41353a 100644 --- a/spf.lark +++ b/spf.lark @@ -55,7 +55,7 @@ loop: "tant" "que" expression "faire" "{" instruction_seq "}" -> while_loop list: "[" expression? ("," expression)* "]" -range: "[" expression? ":" expression? "]" +range: "[" expression ":" expression "]" controls: test | loop diff --git a/spf.py b/spf.py index 8c8b907..2a3d98c 100755 --- a/spf.py +++ b/spf.py @@ -136,6 +136,10 @@ class SPFInterpreter(lark.visitors.Interpreter): true = lambda self, _: True false = lambda self, _: False + def range(self, el): + (left, right) = self.visit_children(el) + return list(range(left, right+1)) + def dump(self): self.variables.dump()