X-Git-Url: https://git.xandkar.net/?p=tiger.ml.git;a=blobdiff_plain;f=tiger%2Fsrc%2Flib%2Ftiger%2Ftiger_parser.mly;h=625632f0df22ba3655a06b5a864fa322c9bade72;hp=2cd530db084d75f4ebd43aa69de44653bef67a0a;hb=b0047ce7ac589181a63394b523fe6c054cb9c9ff;hpb=0e2c715d15688b48aba534d8ab9c3a2aec918752 diff --git a/tiger/src/lib/tiger/tiger_parser.mly b/tiger/src/lib/tiger/tiger_parser.mly index 2cd530d..625632f 100644 --- a/tiger/src/lib/tiger/tiger_parser.mly +++ b/tiger/src/lib/tiger/tiger_parser.mly @@ -49,6 +49,9 @@ %token WHILE /* from lowest precedence */ +%left ELSE +%nonassoc ASSIGN +%left OF DO %left OR %left AND %nonassoc EQ NEQ GT LT GE LE @@ -68,6 +71,7 @@ program: { sprintf "program[%s]" $1 } + ; exp: | NIL @@ -85,9 +89,11 @@ exp: | ID LBRACK exp RBRACK OF exp { let type_id = $1 in - let exp_1 = $3 in - let exp_2 = $6 in - sprintf "array[type[%s], size[%s], val[%s]]" type_id exp_1 exp_2 + let number_of_elements = $3 in + let initial_value = $6 in + sprintf + "array[type[%s], size[%s], val[%s]]" + type_id number_of_elements initial_value } | ID LBRACE rec_field_assignments RBRACE { @@ -166,16 +172,37 @@ exp: (* Perhaps "void"? *) "unit[]" } + ; + +rec_field_assignments: + | ID EQ exp + { + let id = $1 in + let exp = $3 in + sprintf "%S = %s" id exp + } + | ID EQ exp COMMA rec_field_assignments + { + let id = $1 in + let exp = $3 in + let rec_field_assignments = $5 in + sprintf "%S = %s, %s" id exp rec_field_assignments + } + ; exps: | exp { - sprintf "%s" $1 + let exp = $1 in + sprintf "%s" exp } | exp SEMICOLON exps { - sprintf "%s; %s" $1 $3 + let exp = $1 in + let exps = $3 in + sprintf "%s; %s" exp exps } + ; decs: | dec @@ -186,53 +213,10 @@ decs: { sprintf "%s %s" $1 $2 } + ; dec: - | tydec {$1} - | vardec {$1} - | fundec {$1} - -fundec: - | FUNCTION ID LPAREN RPAREN EQ exp - { - let id = $2 in - let exp = $6 in - sprintf "fundec[%s, arguments[], exp[%s]]" id exp - } - | FUNCTION ID LPAREN tyfields RPAREN EQ exp - { - let id = $2 in - let tyfields = $4 in - let exp = $7 in - sprintf "fundec[%s, arguments[%s], exp[%s]]" id tyfields exp - } - | FUNCTION ID LPAREN tyfields RPAREN COLON ID EQ exp - { - let id = $2 in - let tyfields = $4 in - let type_id = $7 in - let exp = $9 in - sprintf - "fundec[%s, tyfields[%s], type_id[%s], exp[%s]]" - id tyfields type_id exp - } - -vardec: - | VAR ID ASSIGN exp - { - let id = $2 in - let exp = $4 in - sprintf "vardec[%s, exp[%s]]" id exp - } - | VAR ID COLON ID ASSIGN exp - { - let id = $2 in - let type_id = $4 in - let exp = $6 in - sprintf "vardec[%s, type_id[%s], exp[%s]]" id type_id exp - } - -tydec: + /* Tydec */ | TYPE ID EQ ID { let type_id_new = $2 in @@ -257,39 +241,63 @@ tydec: sprintf "tydec_array[%s, elements_of_type[%s]]" type_id element_type_id } -tyfields: -/*| epsilon */ - | tyfield - {$1} - | tyfield COMMA tyfields + /* Vardec */ + | VAR ID ASSIGN exp { - let tyfield = $1 in - let tyfields = $3 in - sprintf "%s, %s" tyfield tyfields + let id = $2 in + let exp = $4 in + sprintf "vardec[%s, exp[%s]]" id exp + } + | VAR ID COLON ID ASSIGN exp + { + let id = $2 in + let type_id = $4 in + let exp = $6 in + sprintf "vardec[%s, type_id[%s], exp[%s]]" id type_id exp } -tyfield: - | ID COLON ID + /* Fundec */ + | FUNCTION ID LPAREN RPAREN EQ exp { - let id = $1 in - let type_id = $3 in - sprintf "tyfield[%s, %s]" id type_id + let id = $2 in + let exp = $6 in + sprintf "fundec[%s, arguments[], exp[%s]]" id exp + } + | FUNCTION ID LPAREN tyfields RPAREN EQ exp + { + let id = $2 in + let tyfields = $4 in + let exp = $7 in + sprintf "fundec[%s, arguments[%s], exp[%s]]" id tyfields exp } + | FUNCTION ID LPAREN tyfields RPAREN COLON ID EQ exp + { + let id = $2 in + let tyfields = $4 in + let type_id = $7 in + let exp = $9 in + sprintf + "fundec[%s, tyfields[%s], type_id[%s], exp[%s]]" + id tyfields type_id exp + } + ; -rec_field_assignments: - | ID EQ exp +tyfields: + | ID COLON ID { - let id = $1 in - let exp = $3 in - sprintf "%S = %s" id exp + let id_1 = $1 in + let id_2 = $3 in + sprintf "%s : %s" id_1 id_2 } - | ID EQ exp COMMA rec_field_assignments + | ID COLON ID COMMA tyfields { - let id = $1 in - let exp = $3 in - let rec_field_assignments = $5 in - sprintf "%S = %s, %s" id exp rec_field_assignments + let id_1 = $1 in + let id_2 = $3 in + let tyfield = sprintf "%s : %s" id_1 id_2 in + let tyfields = $5 in + sprintf "%s, %s" tyfield tyfields } + ; fun_args: | exp @@ -300,6 +308,7 @@ fun_args: { sprintf "%s, %s" $1 $3 } + ; op: | PLUS {"+"} @@ -314,24 +323,37 @@ op: | LE {"<="} | AND {"&"} | OR {"|"} + ; lvalue: - | ID + | ID lvalue_part { let id = $1 in - sprintf "lvalue[%s]" id + let part = $2 in + sprintf "lvalue[%s, part[%s]]" id part } - | lvalue DOT ID + ; + +lvalue_part: + | {"epsilon[]"} + | lvalue_subscript {$1} + | lvalue_field_access {$1} + ; + +lvalue_subscript: + | LBRACK exp RBRACK { - let record = $1 in - let field = $3 in - sprintf "get_record_field[%s, %s]" record field + let exp = $2 in + sprintf "subscript[%s]" exp } - | lvalue LBRACK exp RBRACK + ; + +lvalue_field_access: + | DOT ID { - let array = $1 in - let subscript = $3 in - sprintf "get_array_subscript[%s, %s]" array subscript + let field = $2 in + sprintf "field_access[%s]" field } + ; %%