Skip to content

[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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tlively
Copy link
Member

@tlively tlively commented Apr 16, 2025

Add descriptor and describes clauses to the syntax of types. Update the
various operations on types accordingly, including text and binary
parsing and writing.

Add descriptor and describes clauses to the syntax of types. Update the
various operations on types accordingly, including text and binary
parsing and writing.
@tlively tlively requested a review from rossberg April 16, 2025 02:48
Comment on lines +406 to +412
let dt = match dt with
| DescribesT (_, dt) -> dt
| NoDescribesT dt -> dt in
let ct = match dt with
| DescriptorT (_, ct) -> ct
| NoDescriptorT ct -> ct in
ct) subtypes in
Copy link
Member Author

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?

Copy link
Member

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.

described_type :
| str_type { fun c x -> NoDescriptorT ($1 c x) }
| LPAR DESCRIPTOR var str_type RPAR
{ fun c x -> DescriptorT ((fun y -> VarHT (StatX y.it)) ($3 c type_), ($4 c x)) }
Copy link
Member Author

Choose a reason for hiding this comment

The 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?

Copy link
Member

Choose a reason for hiding this comment

The 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. :)

Comment on lines +44 to +52
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
Copy link
Member

Choose a reason for hiding this comment

The 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
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
and desc_type = DescT of heap_type option * heap_type option * str_type
and sub_type = SubT of final * heap_type list * desc_type

(We could turn the tuple into a record to avoid confusing the two clauses, but I suspect that will be tedious to use.)

@@ -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 =
Copy link
Member

Choose a reason for hiding this comment

The 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.)

described_type :
| str_type { fun c x -> NoDescriptorT ($1 c x) }
| LPAR DESCRIPTOR var str_type RPAR
{ fun c x -> DescriptorT ((fun y -> VarHT (StatX y.it)) ($3 c type_), ($4 c x)) }
Copy link
Member

Choose a reason for hiding this comment

The 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. :)

described_type :
| str_type { fun c x -> NoDescriptorT ($1 c x) }
| LPAR DESCRIPTOR var str_type RPAR
{ fun c x -> DescriptorT ((fun y -> VarHT (StatX y.it)) ($3 c type_), ($4 c x)) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{ fun c x -> DescriptorT ((fun y -> VarHT (StatX y.it)) ($3 c type_), ($4 c x)) }
{ fun c x -> DescriptorT (VarHT (StatX ($3 c type_).it), $4 c x) }

describing_type :
| described_type { fun c x -> NoDescribesT ($1 c x) }
| LPAR DESCRIBES var described_type RPAR
{ fun c x -> DescribesT ((fun y -> VarHT (StatX y.it)) ($3 c type_), ($4 c x)) }
Copy link
Member

Choose a reason for hiding this comment

The 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)) }
{ fun c x -> DescribesT (VarHT (StatX ($3 c type_).it), $4 c x) }

Comment on lines +406 to +412
let dt = match dt with
| DescribesT (_, dt) -> dt
| NoDescribesT dt -> dt in
let ct = match dt with
| DescriptorT (_, ct) -> ct
| NoDescriptorT ct -> ct in
ct) subtypes in
Copy link
Member

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.

@@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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
| LPAR DESCRIPTOR var str_type RPAR
| LPAR DESCRIPTOR var RPAR str_type


describing_type :
| described_type { fun c x -> NoDescribesT ($1 c x) }
| LPAR DESCRIBES var described_type RPAR
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| LPAR DESCRIBES var described_type RPAR
| LPAR DESCRIBES var RPAR described_type

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants