compilation/spf.lark
2025-03-19 16:29:54 +01:00

117 lines
2.3 KiB
Plaintext

start: (instruction)*
instruction: declaration ";"
| assignation ";"
| SHOW_KW expression ("," expression)* ";" -> afficher
| ADD_KW expression "dans" VARIABLE ";"
| controls
expression: expressionleft // TODO: priorité des operator certainement fausse
| operator
expressionleft: literal
| list
| range
| VARIABLE -> variable
| "(" expression ")"
//any -> bool
operator: expressionleft SAME_OP expression
| expressionleft DIFF_OP expression
//bool -> bool
| expressionleft AND_OP expression
| expressionleft OR_OP expression
| NOT_OP expression
//int -> bool
| expressionleft LT_OP expression
| expressionleft LE_OP expression
| expressionleft GT_OP expression
| expressionleft GE_OP expression
//int -> int
| expressionleft PLUS_OP expression
| expressionleft MINUS_OP expression
| expressionleft TIMES_OP expression
| expressionleft DIVIDE_OP expression
| NEG_OP expression
// string/list -> string/list
| expressionleft CONC_OP expression
| SIZE_OP expression
?type: BOOL_TYPE
| INT_TYPE
| STR_TYPE
| LIST_TYPE
declaration: type VARIABLE (EQUAL_SIGN expression)?
assignation: VARIABLE EQUAL_SIGN expression
loop: /tant que/ expression "faire" "{" (instruction)* "}"
| /pour chaque/ type VARIABLE "dans" expression "faire" "{" (instruction)* "}"
?literal: ENTIER -> entier
| booleen
| ESCAPED_STRING -> string
list: "[" expression? ("," expression)* "]"
range: "[" expression? ":" expression? "]"
controls: test
| loop
test: "si" expression "alors" "{" instruction* "}" ("sinon" "{" instruction* "}")?
TERMINAL: ";"
VAR_CHARS: /[a-zA-Zçâêîôûéàèìòùëïü_]/
VARIABLE: VAR_CHARS (VAR_CHARS | /[0-9]/)*
EQUAL_SIGN: "="
ENTIER: /0|[1-9][0-9]*/
?booleen: TRUE_KW -> true
| FALSE_KW -> false
BOOL_TYPE: "booléen"
INT_TYPE: "entier"
STR_TYPE: "texte"
LIST_TYPE: "liste"
SAME_OP: "==" | "vaut"
DIFF_OP: "!=" | /ne vaut pas/
AND_OP: "et"
OR_OP: "ou"
NOT_OP: "not"
NEG_OP: "-"
PLUS_OP: "+"
MINUS_OP: "-"
TIMES_OP: "*"
DIVIDE_OP: "/"
LT_OP: "<"
LE_OP: "<="
GT_OP: ">"
GE_OP: ">="
CONC_OP: "+"
SIZE_OP: "taille"
ADD_KW: "ajouter"
SHOW_KW: "afficher"
TRUE_KW: "vrai"
FALSE_KW: "faux"
%import common.ESCAPED_STRING
COMMENT: /#[^\n]*/
%ignore COMMENT
%import common.WS
%ignore WS