X-Git-Url: https://git.xandkar.net/?a=blobdiff_plain;f=tiger%2Fsrc%2Flib%2Ftiger%2Ftiger_parser.mly;h=43ec10f1955186574dcbd7e592bc0686f40228ae;hb=a87678f05df6d8d1dfd1c5ab38666c6f1fe31322;hp=bbf89f7e3da54775f7b6ec4f6f5a6ca07ad24680;hpb=ffff6f3575662f5ccca3c37d288774f69df86f89;p=tiger.ml.git diff --git a/tiger/src/lib/tiger/tiger_parser.mly b/tiger/src/lib/tiger/tiger_parser.mly index bbf89f7..43ec10f 100644 --- a/tiger/src/lib/tiger/tiger_parser.mly +++ b/tiger/src/lib/tiger/tiger_parser.mly @@ -109,9 +109,16 @@ exp: { sprintf "string[%S]" $1 } - | fun_call + | ID LPAREN RPAREN { - $1 + let id = $1 in + sprintf "fun_call[%s, []]" id + } + | ID LPAREN fun_args RPAREN + { + let id = $1 in + let fun_args = $3 in + sprintf "fun_call[%s, %s]" id fun_args } | exp op exp { @@ -144,27 +151,28 @@ exp: { "break[]" } - | LPAREN seq RPAREN + | LPAREN exps RPAREN { - sprintf "seq[%s]" $2 + sprintf "exps[%s]" $2 } - | LET decs IN seq END + | LET decs IN exps END { let decs = $2 in - let seq = $4 in - sprintf "let[decs[%s], in[seq[%s]]]" decs seq + let exps = $4 in + sprintf "let[decs[%s], in[exps[%s]]]" decs exps } - | unit + | LPAREN RPAREN { - $1 + (* Perhaps "void"? *) + "unit[]" } -seq: +exps: | exp { sprintf "%s" $1 } - | exp SEMICOLON seq + | exp SEMICOLON exps { sprintf "%s; %s" $1 $3 } @@ -180,36 +188,32 @@ decs: } dec: - | tydec {$1} - | vardec {$1} - | fundec {$1} - -fundec: - | FUNCTION ID unit EQ exp + /* Tydec */ + | TYPE ID EQ ID { - let id = $2 in - let exp = $5 in - sprintf "fundec[%s, exp[%s]]" id exp + let type_id_new = $2 in + let type_id_orig = $4 in + sprintf "tydec_alias[from[%s], to[%s]]" type_id_new type_id_orig } - | FUNCTION ID LPAREN tyfields RPAREN EQ exp + | TYPE ID EQ LBRACE RBRACE { - let id = $2 in - let tyfields = $4 in - let exp = $7 in - sprintf "fundec[%s, tyfields[%s], exp[%s]]" id tyfields exp + let type_id = $2 in + sprintf "tydec_empty_record[%s]" type_id } - | FUNCTION ID LPAREN tyfields RPAREN COLON ID EQ exp + | TYPE ID EQ LBRACE tyfields RBRACE { - 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 + let type_id = $2 in + let tyfields = $5 in + sprintf "tydec_record[%s, fields[%s]]" type_id tyfields + } + | TYPE ID EQ ARRAY OF ID + { + let type_id = $2 in + let element_type_id = $6 in + sprintf "tydec_array[%s, elements_of_type[%s]]" type_id element_type_id } -vardec: + /* Vardec */ | VAR ID ASSIGN exp { let id = $2 in @@ -224,59 +228,45 @@ vardec: sprintf "vardec[%s, type_id[%s], exp[%s]]" id type_id exp } -tydec: - | TYPE ID EQ ty - { - let type_id = $2 in - let ty = $4 in - sprintf "tydec[%s, %s]" type_id ty - } - -ty: - | ID + /* Fundec */ + | FUNCTION ID LPAREN RPAREN EQ exp { - let type_id = $1 in - sprintf "type[type_id[%S]]" type_id - } - | LBRACE RBRACE - { - "record[]" + let id = $2 in + let exp = $6 in + sprintf "fundec[%s, arguments[], exp[%s]]" id exp } - | LBRACE tyfields RBRACE + | FUNCTION ID LPAREN tyfields RPAREN EQ exp { - let tyfields = $2 in - sprintf "record[%s]" tyfields + let id = $2 in + let tyfields = $4 in + let exp = $7 in + sprintf "fundec[%s, arguments[%s], exp[%s]]" id tyfields exp } - | ARRAY OF ID + | FUNCTION ID LPAREN tyfields RPAREN COLON ID EQ exp { - let type_id = $3 in - sprintf "array_of_type[%s]" type_id + 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 } tyfields: -/*| epsilon */ - | tyfield - {$1} - | tyfield COMMA tyfields - { - let tyfield = $1 in - let tyfields = $3 in - sprintf "%s, %s" tyfield tyfields - } - -tyfield: | ID COLON ID { - let id = $1 in - let type_id = $3 in - sprintf "tyfield[%s, %s]" id type_id + let id_1 = $1 in + let id_2 = $3 in + sprintf "%s : %s" id_1 id_2 } - -/* Perhaps "void"? */ -unit: - | LPAREN RPAREN + | ID COLON ID COMMA tyfields { - "unit[]" + 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 } rec_field_assignments: @@ -294,19 +284,6 @@ rec_field_assignments: sprintf "%S = %s, %s" id exp rec_field_assignments } -fun_call: - | ID unit - { - let id = $1 in - sprintf "fun_call[%s, []]" id - } - | ID LPAREN fun_args RPAREN - { - let id = $1 in - let fun_args = $3 in - sprintf "fun_call[%s, %s]" id fun_args - } - fun_args: | exp {