Adding list accessor and for loop
This commit is contained in:
parent
948b335cb5
commit
1a181095c6
6
examples/test.spf
Normal file
6
examples/test.spf
Normal file
@ -0,0 +1,6 @@
|
||||
texte name = "Anthony";
|
||||
si name ne vaut pas "Anthony" alors {
|
||||
afficher "C'est pas moi";
|
||||
} sinon {
|
||||
afficher "C'est moi";
|
||||
}
|
8
spf.lark
8
spf.lark
@ -35,6 +35,7 @@ operator: expressionleft SAME_OP expression -> equal
|
||||
| NEG_OP expression -> neg
|
||||
// string/list -> string/list
|
||||
| SIZE_OP expression -> sizeof
|
||||
| expressionleft "[" expression "]" -> list_get
|
||||
|
||||
?type: BOOL_TYPE
|
||||
| INT_TYPE
|
||||
@ -45,8 +46,8 @@ declaration: type VARIABLE (EQUAL_SIGN expression)?
|
||||
|
||||
assignation: VARIABLE EQUAL_SIGN expression
|
||||
|
||||
loop: "tant" "que" expression "faire" "{" (instruction)* "}" -> while_loop
|
||||
| "pour" "chaque" type VARIABLE "dans" expression "faire" "{" (instruction)* "}" -> for_loop
|
||||
loop: "tant" "que" expression "faire" "{" instruction_seq "}" -> while_loop
|
||||
| "pour" "chaque" type VARIABLE "dans" expression "faire" "{" instruction_seq "}" -> for_loop
|
||||
|
||||
?literal: ENTIER -> entier
|
||||
| booleen
|
||||
@ -59,7 +60,8 @@ range: "[" expression? ":" expression? "]"
|
||||
controls: test
|
||||
| loop
|
||||
|
||||
test: "si" expression "alors" "{" instruction* "}" ("sinon" "{" instruction* "}")?
|
||||
test: "si" expression "alors" "{" instruction_seq "}" ("sinon" "{" instruction_seq "}")?
|
||||
instruction_seq: (instruction*)
|
||||
|
||||
?booleen: TRUE_KW -> true
|
||||
| FALSE_KW -> false
|
||||
|
23
spf.py
23
spf.py
@ -16,10 +16,18 @@ class SPFInterpreter(lark.visitors.Interpreter):
|
||||
|
||||
def while_loop(self, el):
|
||||
while self.visit_children(el.children[0])[0]:
|
||||
[self.visit_children(i) for i in el.children[1:]]
|
||||
self.visit_children(el.children[1])
|
||||
|
||||
def for_loop(self, el):
|
||||
print("TODO: for")
|
||||
type = el.children[0].value
|
||||
name = el.children[1].value
|
||||
self.variables.declare(type, name)
|
||||
|
||||
target = self.visit_children(el.children[2])[0]
|
||||
for i in target:
|
||||
self.variables.assign(name, i)
|
||||
self.visit_children(el.children[3])
|
||||
# TODO: delete the variable out of scope
|
||||
|
||||
def afficher(self, el):
|
||||
ligne = ""
|
||||
@ -101,6 +109,10 @@ class SPFInterpreter(lark.visitors.Interpreter):
|
||||
|
||||
sizeof = lambda self, el:len(self.visit_children(el)[1])
|
||||
|
||||
def list_get(self, el):
|
||||
(left, right) = self.visit_children(el)
|
||||
return left[right]
|
||||
|
||||
|
||||
def expression(self, el):
|
||||
return self.visit_children(el)[0]
|
||||
@ -111,6 +123,13 @@ class SPFInterpreter(lark.visitors.Interpreter):
|
||||
def variable(self, el):
|
||||
return self.variables.get(el.children[0].value)
|
||||
|
||||
def test(self,el):
|
||||
if self.visit_children(el.children[0])[0]:
|
||||
self.visit_children(el.children[1])
|
||||
elif len(el.children) >= 3:
|
||||
self.visit_children(el.children[2])
|
||||
|
||||
|
||||
# Literals
|
||||
string = lambda self, el: el.children[0][1:-1]
|
||||
entier = lambda self, el: int(el.children[0])
|
||||
|
Loading…
x
Reference in New Issue
Block a user