Compare commits
	
		
			2 Commits
		
	
	
		
			76e2707156
			...
			1a181095c6
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 1a181095c6 | |||
| 948b335cb5 | 
							
								
								
									
										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 | ||||
|  | ||||
							
								
								
									
										27
									
								
								spf.py
									
									
									
									
									
								
							
							
						
						
									
										27
									
								
								spf.py
									
									
									
									
									
								
							| @ -15,13 +15,19 @@ class SPFInterpreter(lark.visitors.Interpreter): | ||||
|         self.variables = Variables(trace) | ||||
|  | ||||
|     def while_loop(self, el): | ||||
|         print("TODO: while") | ||||
|         cond = el.children[0] | ||||
|         instr = el.children[1:] | ||||
|         print(cond.pretty()) | ||||
|         while self.visit_children(el.children[0])[0]: | ||||
|             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 = "" | ||||
| @ -103,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] | ||||
| @ -113,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]) | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	