-
Notifications
You must be signed in to change notification settings - Fork 0
[interpreter] Custom descriptor syntax #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -41,7 +41,15 @@ and str_type = | |||||||||||||||||||||||||
| DefArrayT of array_type | ||||||||||||||||||||||||||
| DefFuncT of func_type | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
and sub_type = SubT of final * heap_type list * str_type | ||||||||||||||||||||||||||
and described_type = | ||||||||||||||||||||||||||
| DescriptorT of heap_type * str_type | ||||||||||||||||||||||||||
| NoDescriptorT of str_type | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
and describing_type = | ||||||||||||||||||||||||||
| DescribesT of heap_type * described_type | ||||||||||||||||||||||||||
| NoDescribesT of described_type | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
and sub_type = SubT of final * heap_type list * describing_type | ||||||||||||||||||||||||||
Comment on lines
+44
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest a more direct syntax, which simplifies pattern matching a lot:
Suggested change
(We could turn the tuple into a record to avoid confusing the two clauses, but I suspect that will be tedious to use.) |
||||||||||||||||||||||||||
and rec_type = RecT of sub_type list | ||||||||||||||||||||||||||
and def_type = DefT of rec_type * int32 | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
|
@@ -223,9 +231,19 @@ let subst_str_type s = function | |||||||||||||||||||||||||
| DefArrayT at -> DefArrayT (subst_array_type s at) | ||||||||||||||||||||||||||
| DefFuncT ft -> DefFuncT (subst_func_type s ft) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
let subst_described_type s = function | ||||||||||||||||||||||||||
| DescriptorT (ht, st) -> | ||||||||||||||||||||||||||
DescriptorT (subst_heap_type s ht, subst_str_type s st) | ||||||||||||||||||||||||||
| NoDescriptorT st -> NoDescriptorT (subst_str_type s st) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
let subst_describing_type s = function | ||||||||||||||||||||||||||
| DescribesT (ht, dt) -> | ||||||||||||||||||||||||||
DescribesT (subst_heap_type s ht, subst_described_type s dt) | ||||||||||||||||||||||||||
| NoDescribesT dt -> NoDescribesT (subst_described_type s dt) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
let subst_sub_type s = function | ||||||||||||||||||||||||||
| SubT (fin, hts, st) -> | ||||||||||||||||||||||||||
SubT (fin, List.map (subst_heap_type s) hts, subst_str_type s st) | ||||||||||||||||||||||||||
| SubT (fin, hts, dt) -> | ||||||||||||||||||||||||||
SubT (fin, List.map (subst_heap_type s) hts, subst_describing_type s dt) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
let subst_rec_type s = function | ||||||||||||||||||||||||||
| RecT sts -> RecT (List.map (subst_sub_type s) sts) | ||||||||||||||||||||||||||
|
@@ -298,9 +316,13 @@ let unroll_def_type (dt : def_type) : sub_type = | |||||||||||||||||||||||||
Lib.List32.nth sts i | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
let expand_def_type (dt : def_type) : str_type = | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would expect that this has to return desc_type now, since the clauses affect a type's structure and become relevant in some operations. With the flat type representation suggested above, they can just be pattern-matched away where not needed. (FWIW, I should rename str_type to comp_type here, to sync with the spec.) |
||||||||||||||||||||||||||
let SubT (_, _, st) = unroll_def_type dt in | ||||||||||||||||||||||||||
st | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
let SubT (_, _, dt) = unroll_def_type dt in | ||||||||||||||||||||||||||
let dt = match dt with | ||||||||||||||||||||||||||
| DescribesT (_, dt) -> dt | ||||||||||||||||||||||||||
| NoDescribesT dt -> dt in | ||||||||||||||||||||||||||
match dt with | ||||||||||||||||||||||||||
| DescriptorT (_, st) -> st | ||||||||||||||||||||||||||
| NoDescriptorT st -> st | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
(* String conversion *) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
|
@@ -403,12 +425,22 @@ and string_of_str_type = function | |||||||||||||||||||||||||
| DefArrayT at -> "array " ^ string_of_array_type at | ||||||||||||||||||||||||||
| DefFuncT ft -> "func " ^ string_of_func_type ft | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
and string_of_described_type = function | ||||||||||||||||||||||||||
| DescriptorT (ht, st) -> | ||||||||||||||||||||||||||
"(descriptor " ^ string_of_heap_type ht ^ " " ^ string_of_str_type st ^ ")" | ||||||||||||||||||||||||||
| NoDescriptorT st -> string_of_str_type st | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
and string_of_describing_type = function | ||||||||||||||||||||||||||
| DescribesT (ht, dt) -> | ||||||||||||||||||||||||||
"(describes " ^ string_of_heap_type ht ^ " " ^ string_of_described_type dt ^ ")" | ||||||||||||||||||||||||||
| NoDescribesT dt -> string_of_described_type dt | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
and string_of_sub_type = function | ||||||||||||||||||||||||||
| SubT (Final, [], st) -> string_of_str_type st | ||||||||||||||||||||||||||
| SubT (fin, hts, st) -> | ||||||||||||||||||||||||||
| SubT (Final, [], dt) -> string_of_describing_type dt | ||||||||||||||||||||||||||
| SubT (fin, hts, dt) -> | ||||||||||||||||||||||||||
String.concat " " | ||||||||||||||||||||||||||
(("sub" ^ string_of_final fin) :: List.map string_of_heap_type hts) ^ | ||||||||||||||||||||||||||
" (" ^ string_of_str_type st ^ ")" | ||||||||||||||||||||||||||
" (" ^ string_of_describing_type dt ^ ")" | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
and string_of_rec_type = function | ||||||||||||||||||||||||||
| RecT [st] -> string_of_sub_type st | ||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -238,7 +238,7 @@ let anon_fields (c : context) x n loc = | |||||
|
||||||
|
||||||
let inline_func_type (c : context) ft loc = | ||||||
let st = SubT (Final, [], DefFuncT ft) in | ||||||
let st = SubT (Final, [], NoDescribesT (NoDescriptorT (DefFuncT ft))) in | ||||||
match | ||||||
Lib.List.index_where (function | ||||||
| DefT (RecT [st'], 0l) -> st = st' | ||||||
|
@@ -299,7 +299,7 @@ let parse_annots (m : module_) : Custom.section list = | |||||
%token ANYREF NULLREF EQREF I31REF STRUCTREF ARRAYREF | ||||||
%token FUNCREF NULLFUNCREF EXNREF NULLEXNREF EXTERNREF NULLEXTERNREF | ||||||
%token ANY NONE EQ I31 REF NOFUNC EXN NOEXN EXTERN NOEXTERN NULL | ||||||
%token MUT FIELD STRUCT ARRAY SUB FINAL REC | ||||||
%token MUT FIELD STRUCT ARRAY DESCRIPTOR DESCRIBES SUB FINAL REC | ||||||
%token UNREACHABLE NOP DROP SELECT | ||||||
%token BLOCK END IF THEN ELSE LOOP | ||||||
%token BR BR_IF BR_TABLE | ||||||
|
@@ -466,12 +466,22 @@ str_type : | |||||
| LPAR ARRAY array_type RPAR { fun c x -> DefArrayT ($3 c) } | ||||||
| LPAR FUNC func_type RPAR { fun c x -> DefFuncT ($3 c) } | ||||||
|
||||||
described_type : | ||||||
| str_type { fun c x -> NoDescriptorT ($1 c x) } | ||||||
| LPAR DESCRIPTOR var str_type RPAR | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would have expected this, i.e., treating the clauses more like extra fields:
Suggested change
|
||||||
{ fun c x -> DescriptorT ((fun y -> VarHT (StatX y.it)) ($3 c type_), ($4 c x)) } | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some of these lines are over 80 char long, but I'm not familiar with the formatting conventions. Is there a good automatic formatter I can run? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, formatting of the parser has become a real mess. I don't think there is a tool for auto-formatting mly files. Just follow some of the surrounding code and don't worry about it too much. I'll clean it up eventually. :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
describing_type : | ||||||
| described_type { fun c x -> NoDescribesT ($1 c x) } | ||||||
| LPAR DESCRIBES var described_type RPAR | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
{ fun c x -> DescribesT ((fun y -> VarHT (StatX y.it)) ($3 c type_), ($4 c x)) } | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
sub_type : | ||||||
| str_type { fun c x -> SubT (Final, [], $1 c x) } | ||||||
| LPAR SUB var_list str_type RPAR | ||||||
| describing_type { fun c x -> SubT (Final, [], $1 c x) } | ||||||
| LPAR SUB var_list describing_type RPAR | ||||||
{ fun c x -> SubT (NoFinal, | ||||||
List.map (fun y -> VarHT (StatX y.it)) ($3 c type_), $4 c x) } | ||||||
| LPAR SUB FINAL var_list str_type RPAR | ||||||
| LPAR SUB FINAL var_list describing_type RPAR | ||||||
{ fun c x -> SubT (Final, | ||||||
List.map (fun y -> VarHT (StatX y.it)) ($4 c type_), $5 c x) } | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
;; Test custom descriptors | ||
|
||
(module | ||
(rec | ||
(type (descriptor 1 (struct))) | ||
(type (describes 0 (struct))) | ||
) | ||
) | ||
|
||
(module | ||
(rec | ||
(type $super (sub (descriptor $super-desc (struct)))) | ||
(type $super-desc (sub (describes $super (struct)))) | ||
) | ||
(rec | ||
(type $sub (sub $super (descriptor $sub-desc (struct)))) | ||
(type $sub-desc (sub $super-desc (describes $sub (struct)))) | ||
) | ||
) | ||
|
||
(module | ||
(type $super (sub (struct))) | ||
(rec | ||
(type $other (sub (descriptor $super-desc (struct)))) | ||
(type $super-desc (sub (describes $other (struct)))) | ||
) | ||
(rec | ||
(type $sub (sub $super (descriptor $sub-desc (struct)))) | ||
(type $sub-desc (sub $super-desc (describes $sub (struct)))) | ||
) | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This pattern comes up a lot. Do you have a recommendation for a name and location for a helper function for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See suggestion for flattening the type.