diff --git a/requirements.txt b/requirements.txt index b0ef8cc..30bf384 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ +interegular==0.3.3 lark==1.2.2 diff --git a/spf.lark b/spf.lark index 3aa6a56..e086cea 100644 --- a/spf.lark +++ b/spf.lark @@ -1,16 +1,22 @@ -start: (instruction)* TERMINAL +start: (instruction)* -instruction: KEYWORD TERMINAL - | declaration TERMINAL +instruction: keyword expression TERMINAL + | type declaration TERMINAL + | assignation TERMINAL + | test + | loop -declaration: TYPE VARIABLE - | TYPE VARIABLE "=" expression +declaration: VARIABLE (EQUAL_SIGN expression)? + +assignation: VARIABLE EQUAL_SIGN expression expression: literal | list | range - | expression operator expression - | operator expression + //| expression operator expression + //| operator expression + +type: "booléen" | "entier" | "texte" | "liste" literal: ENTIER | BOOLEEN @@ -25,35 +31,49 @@ range: "[" ENTIER? ":" ENTIER? "]" controls: test | loop -operator: PLUS - | MINUS - | TIMES - | DIVIDE // TODO: not complete +operator: PLUS_SIGN + | MINUS_SIGN + | TIMES_SIGN + | DIVIDE_SIGN // TODO: not complete -PLUS: "+" -MINUS: "-" -TIMES: "*" -DIVIDE: "/" +PLUS_SIGN: "+" +MINUS_SIGN: "-" +TIMES_SIGN: "*" +DIVIDE_SIGN: "/" + +EQUAL_SIGN: "=" test: "si" expression "alors" "{" instruction* "}" + | "si" expression "alors" "{" instruction* "}" "sinon" "{" instruction* "}" -loop: "si" expression "alors" "{" (instruction)* "}" - -COMMENT: "#" /[^\n]/ -%ignore COMMENT +loop: WHILES_KW expression WHILEE_KW "{" (instruction)* "}" TERMINAL: ";" -VARIABLE: /([a-z]|[A-Z]|_)+([a-z]|[A-Z]|[0-9]|_)+/ // TODO: ajouter accents - -TYPE: "booléen" | "entier" | "texte" | "liste" - -KEYWORD: "ajouter" | "afficher" - -ENTIER: /[1-9][0-9]*/ - -BOOLEEN: "vrai" | "faux" - -STRING: /\w+/ +VAR_CHARS: /[a-zA-Zçâêîôûéàèìòùëïü_]/ +VARIABLE: VAR_CHARS (VAR_CHARS | /[0-9]/)* +ADD_KW: "ajouter" +SHOW_KW: "afficher" +WHILES_KW: "tant que" +WHILEE_KW: "faire" + +keyword: ADD_KW + | SHOW_KW + +%import common.INT -> ENTIER + +TRUE_KW: "vrai" +FALSE_KW: "faux" + +BOOLEEN: TRUE_KW + | FALSE_KW + +%import common.ESCAPED_STRING -> STRING + +COMMENT: /#[^\n]*/ +%ignore COMMENT + +%import common.WS +%ignore WS diff --git a/spf.py b/spf.py index 0951032..8acd6d1 100755 --- a/spf.py +++ b/spf.py @@ -29,11 +29,13 @@ def main(): print("Trace activated (TODO)", file=sys.stderr) with open("spf.lark") as grammar: - spf_parser = lark.Lark(grammar, parser="lalr", transformer=SPFTransformer) + spf_parser = lark.Lark(grammar, parser="lalr", strict=True, debug=True) with open(args.spf_file) as spf_input: program = spf_input.read() - spf_parser.parse(program) + parsed = spf_parser.parse(program) + + print(parsed.pretty()) if __name__ == "__main__":