diff --git a/language.grammar b/language.grammar index a3163db..09c2b62 100644 --- a/language.grammar +++ b/language.grammar @@ -4,12 +4,22 @@ token lower_identifier = /[a-z_][a-z_A-Z0-9]*/; token kw_data = /data/; token kw_type = /type/; token kw_defn = /defn/; +token kw_let = /let/; +token kw_letrec = /letrec/; +token kw_in = /in/; +token kw_case = /case/; +token kw_of = /of/; token tkn_ocurly = /{/; token tkn_ccurly = /}/; token tkn_oparen = /\(/; token tkn_cparen = /\)/; token tkn_eq = /=/; token tkn_comma = /,/; +token tkn_arrow = /->/; +token op_add = /\+/; +token op_sub = /-/; +token op_mul = /\*/; +token op_div = /\//; rule S = program; @@ -55,7 +65,67 @@ rule defn_body ; rule expr + = expr_add + | expr_let + | expr_letrec + | expr_case + ; + +rule expr_add + = expr_add op_add expr_mul + | expr_add op_sub expr_mul + | expr_mul + ; + +rule expr_mul + = expr_mul op_mul expr_app + | expr_mul op_div expr_app + | expr_app + ; + +rule expr_app + = expr_app? expr_app_bottom + ; + +rule expr_app_bottom = lower_identifier + | upper_identifier + | tkn_oparen expr tkn_cparen + ; + +rule expr_curly + = tkn_ocurly expr tkn_ccurly + ; + +rule expr_let + = kw_let lower_identifier tkn_eq expr_curly kw_in expr_curly + ; + +rule expr_letrec + = kw_letrec lower_identifier tkn_eq expr_curly kw_in expr_curly + ; + +rule expr_case + = kw_case expr kw_of tkn_ocurly expr_case_branches tkn_ccurly + ; + +rule expr_case_branches + = expr_case_branches? expr_case_branch + ; + +rule expr_case_branch + = pattern tkn_arrow expr_curly + ; + +rule pattern + = lower_identifier + | upper_identifier + | upper_identifier tkn_oparen patterns tkn_cparen + ; + +rule patterns + = pattern + | patterns tkn_comma pattern ; rule type