Adding list accessor and for loop
This commit is contained in:
		
							
								
								
									
										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 |   | NEG_OP expression -> neg | ||||||
| // string/list -> string/list | // string/list -> string/list | ||||||
|   | SIZE_OP expression -> sizeof |   | SIZE_OP expression -> sizeof | ||||||
|  |   | expressionleft "[" expression "]" -> list_get | ||||||
|  |  | ||||||
| ?type: BOOL_TYPE | ?type: BOOL_TYPE | ||||||
|      | INT_TYPE  |      | INT_TYPE  | ||||||
| @ -45,8 +46,8 @@ declaration: type VARIABLE (EQUAL_SIGN expression)? | |||||||
|  |  | ||||||
| assignation: VARIABLE EQUAL_SIGN expression | assignation: VARIABLE EQUAL_SIGN expression | ||||||
|  |  | ||||||
| loop: "tant" "que" expression "faire" "{" (instruction)* "}" -> while_loop | loop: "tant" "que" expression "faire" "{" instruction_seq "}" -> while_loop | ||||||
|     | "pour" "chaque" type VARIABLE "dans" expression "faire" "{" (instruction)* "}" -> for_loop |     | "pour" "chaque" type VARIABLE "dans" expression "faire" "{" instruction_seq "}" -> for_loop | ||||||
|  |  | ||||||
| ?literal: ENTIER -> entier | ?literal: ENTIER -> entier | ||||||
| 	   | booleen  | 	   | booleen  | ||||||
| @ -59,7 +60,8 @@ range: "[" expression? ":" expression? "]" | |||||||
| controls: test | controls: test | ||||||
| 		| loop | 		| loop | ||||||
|  |  | ||||||
| test: "si" expression "alors" "{" instruction* "}" ("sinon" "{" instruction* "}")? | test: "si" expression "alors" "{" instruction_seq "}" ("sinon" "{" instruction_seq "}")? | ||||||
|  | instruction_seq: (instruction*) | ||||||
|  |  | ||||||
| ?booleen: TRUE_KW  -> true | ?booleen: TRUE_KW  -> true | ||||||
| 	   | FALSE_KW -> false | 	   | FALSE_KW -> false | ||||||
|  | |||||||
							
								
								
									
										23
									
								
								spf.py
									
									
									
									
									
								
							
							
						
						
									
										23
									
								
								spf.py
									
									
									
									
									
								
							| @ -16,10 +16,18 @@ class SPFInterpreter(lark.visitors.Interpreter): | |||||||
|  |  | ||||||
|     def while_loop(self, el): |     def while_loop(self, el): | ||||||
|         while self.visit_children(el.children[0])[0]: |         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): |     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): |     def afficher(self, el): | ||||||
|         ligne = "" |         ligne = "" | ||||||
| @ -101,6 +109,10 @@ class SPFInterpreter(lark.visitors.Interpreter): | |||||||
|  |  | ||||||
|     sizeof = lambda self, el:len(self.visit_children(el)[1]) |     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): |     def expression(self, el): | ||||||
|         return self.visit_children(el)[0] |         return self.visit_children(el)[0] | ||||||
| @ -111,6 +123,13 @@ class SPFInterpreter(lark.visitors.Interpreter): | |||||||
|     def variable(self, el): |     def variable(self, el): | ||||||
|         return self.variables.get(el.children[0].value) |         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 |     # Literals | ||||||
|     string = lambda self, el: el.children[0][1:-1] |     string = lambda self, el: el.children[0][1:-1] | ||||||
|     entier = lambda self, el: int(el.children[0]) |     entier = lambda self, el: int(el.children[0]) | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user