-
Notifications
You must be signed in to change notification settings - Fork 70
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
Add define-type-alias
#1294
Open
YarinHeffes
wants to merge
21
commits into
coalton-lang:main
Choose a base branch
from
YarinHeffes:type-alias
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+882
−67
Open
Add define-type-alias
#1294
Changes from 20 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
758cf3d
added basic define-alias toplevel form
YarinHeffes a125e4a
fixed alias substitution for constructors and struct-fields
YarinHeffes 074488b
added alias tests
YarinHeffes 688322c
improved alias error messages
YarinHeffes 287f089
removed with-aliases-from
YarinHeffes 77a2443
Merge branch 'coalton-lang:main' into type-alias
YarinHeffes e05bd8a
alias coalton-release fix
YarinHeffes db74f65
added parametrized type aliases
YarinHeffes ffbef6e
added tests for parametrized type aliases
YarinHeffes f252499
added describe-alias
YarinHeffes 9dda5c2
added define-alias.txt tests
YarinHeffes 314ff0b
add tutorial documentation for type aliases
YarinHeffes fde68b5
added type alias documentation guide
YarinHeffes c9ba647
Merge branch 'main' into type-alias
YarinHeffes 5851da6
removed empty line
YarinHeffes ff0e7f7
changed :nil to nil
YarinHeffes 9b3beda
added documentation to *pprint-aliases*
YarinHeffes 57366b7
changed define-alias -> define-type-alias
YarinHeffes 1aba695
Improved error message for unused type alias arguments
YarinHeffes e6784b8
typo fix
YarinHeffes 8619ceb
minor fixes
YarinHeffes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,13 @@ | |
#:toplevel-define-type-repr ; ACCESSOR | ||
#:toplevel-define-type-head-location ; ACCESSOR | ||
#:toplevel-define-type-list ; TYPE | ||
#:toplevel-define-type-alias ; STRUCT | ||
#:make-toplevel-define-type-alias ; CONSTRUCTOR | ||
#:toplevel-define-type-alias-name ; ACCESSOR | ||
#:toplevel-define-type-alias-vars ; ACCESSOR | ||
#:toplevel-define-type-alias-type ; ACCESSOR | ||
#:toplevel-define-type-alias-head-location ; ACCESSOR | ||
#:toplevel-define-type-alias-list ; TYPE | ||
#:struct-field ; STRUCT | ||
#:make-struct-field ; CONSTRUCTOR | ||
#:struct-field-name ; ACCESSOR | ||
|
@@ -112,6 +119,7 @@ | |
#:program-package ; ACCESSOR | ||
#:program-lisp-forms ; ACCESSOR | ||
#:program-types ; ACCESSOR | ||
#:program-type-aliases ; ACCESSOR | ||
#:program-structs ; ACCESSOR | ||
#:program-declares ; ACCESSOR | ||
#:program-defines ; ACCESSOR | ||
|
@@ -169,6 +177,9 @@ | |
;;;; toplevel-define-type := "(" "define-type" identifier docstring? constructor* ")" | ||
;;;; | "(" "define-type" "(" identifier keyword+ ")" docstring? constructor* ")" | ||
;;;; | ||
;;;; toplevel-define-type-alias := "(" "define-type-alias" identifier ty docstring? ")" | ||
;;;; | "(" "define-type-alias" "(" identifier keyword+ ")" ty docstring? ")" | ||
;;;; | ||
;;;; struct-field := "(" identifier docstring? type ")" | ||
;;;; | ||
;;;; toplevel-define-struct := "(" "define-struct" identifier docstring? struct-field* ")" | ||
|
@@ -265,6 +276,22 @@ | |
(deftype toplevel-define-type-list () | ||
'(satisfies toplevel-define-type-list-p)) | ||
|
||
(defstruct (toplevel-define-type-alias | ||
(:include toplevel-definition) | ||
(:copier nil)) | ||
(name (util:required 'name) :type identifier-src :read-only t) | ||
(vars (util:required 'vars) :type keyword-src-list :read-only t) | ||
(type (util:required 'type) :type ty :read-only t) | ||
(head-location (util:required 'head-location) :type source:location :read-only t)) | ||
|
||
(eval-when (:load-toplevel :compile-toplevel :execute) | ||
(defun toplevel-define-type-alias-list-p (x) | ||
(and (alexandria:proper-list-p x) | ||
(every #'toplevel-define-type-alias-p x)))) | ||
|
||
(deftype toplevel-define-type-alias-list () | ||
'(satisfies toplevel-define-type-alias-list-p)) | ||
|
||
(defstruct (struct-field | ||
(:include toplevel-definition) | ||
(:copier nil)) | ||
|
@@ -464,15 +491,16 @@ | |
(export nil :type list)) | ||
|
||
(defstruct program | ||
(package nil :type (or null toplevel-package) :read-only t) | ||
(types nil :type toplevel-define-type-list :read-only nil) | ||
(structs nil :type toplevel-define-struct-list :read-only nil) | ||
(declares nil :type toplevel-declare-list :read-only nil) | ||
(defines nil :type toplevel-define-list :read-only nil) | ||
(classes nil :type toplevel-define-class-list :read-only nil) | ||
(instances nil :type toplevel-define-instance-list :read-only nil) | ||
(lisp-forms nil :type toplevel-lisp-form-list :read-only nil) | ||
(specializations nil :type toplevel-specialize-list :read-only nil)) | ||
(package nil :type (or null toplevel-package) :read-only t) | ||
(types nil :type toplevel-define-type-list :read-only nil) | ||
(type-aliases nil :type toplevel-define-type-alias-list :read-only nil) | ||
(structs nil :type toplevel-define-struct-list :read-only nil) | ||
(declares nil :type toplevel-declare-list :read-only nil) | ||
(defines nil :type toplevel-define-list :read-only nil) | ||
(classes nil :type toplevel-define-class-list :read-only nil) | ||
(instances nil :type toplevel-define-instance-list :read-only nil) | ||
(lisp-forms nil :type toplevel-lisp-form-list :read-only nil) | ||
(specializations nil :type toplevel-specialize-list :read-only nil)) | ||
|
||
(defun read-program (stream source &optional mode) | ||
"Read a PROGRAM from SOURCE (an instance of source-error:source). | ||
|
@@ -520,6 +548,7 @@ If MODE is :macro, a package form is forbidden, and an explicit check is made fo | |
"attribute must be attached to another form"))) | ||
|
||
(setf (program-types program) (nreverse (program-types program))) | ||
(setf (program-type-aliases program) (nreverse (program-type-aliases program))) | ||
(setf (program-structs program) (nreverse (program-structs program))) | ||
(setf (program-declares program) (nreverse (program-declares program))) | ||
(setf (program-defines program) (nreverse (program-defines program))) | ||
|
@@ -858,6 +887,12 @@ If the parsed form is an attribute (e.g., repr or monomorphize), add it to to AT | |
(push type (program-types program)) | ||
t)) | ||
|
||
((coalton:define-type-alias) | ||
(forbid-attributes attributes form source) | ||
(let ((alias (parse-define-type-alias form source))) | ||
(push alias (program-type-aliases program)) | ||
t)) | ||
|
||
((coalton:define-struct) | ||
(let* ((struct (parse-define-struct form source)) | ||
(repr (consume-repr attributes struct "when parsing define-struct"))) | ||
|
@@ -1107,6 +1142,97 @@ consume all attributes"))) | |
:location (form-location source form) | ||
:head-location (form-location source (cst:second form))))) | ||
|
||
(defun parse-define-type-alias (form source) | ||
(declare (type cst:cst form) | ||
(values toplevel-define-type-alias)) | ||
|
||
(assert (cst:consp form)) | ||
|
||
(let (docstring | ||
name | ||
variables) | ||
|
||
;; (define-type-alias) | ||
(unless (cst:consp (cst:rest form)) | ||
(parse-error "Malformed type alias definition" | ||
(note source form "expected body"))) | ||
|
||
(cond | ||
;; (define-type-alias _ ...) | ||
((cst:atom (cst:second form)) | ||
;; (define-type-alias 0.5 ...) | ||
(unless (identifierp (cst:raw (cst:second form))) | ||
(parse-error "Malformed type alias definition" | ||
(note source (cst:second form) "expected symbol"))) | ||
|
||
;; (define-type-alias name ...) | ||
(setf name (make-identifier-src :name (cst:raw (cst:second form)) | ||
:location (form-location source form)))) | ||
|
||
;; (define-type-alias (_ ...) ...) | ||
(t | ||
;; (define-type-alias((name) ...) ...) | ||
(unless (cst:atom (cst:first (cst:second form))) | ||
(parse-error "Malformed type alias definition" | ||
(note source (cst:first (cst:second form)) | ||
"expected symbol") | ||
(help source (cst:second form) | ||
(lambda (existing) | ||
(subseq existing 1 (1- (length existing)))) | ||
"remove parentheses"))) | ||
|
||
;; (define-type-alias (0.5 ...) ...) | ||
(unless (identifierp (cst:raw (cst:first (cst:second form)))) | ||
(parse-error "Malformed type alias definition" | ||
(note source (cst:first (cst:second form)) | ||
"expected symbol"))) | ||
|
||
;; (define-type-alias (name ...) ...) | ||
(setf name (make-identifier-src :name (cst:raw (cst:first (cst:second form))) | ||
:location (form-location source | ||
(cst:first (cst:second form))))) | ||
|
||
;; (define-type-alias (name) ...) | ||
(when (cst:atom (cst:rest (cst:second form))) | ||
(parse-error "Malformed type alias definition" | ||
(note source (cst:second form) | ||
"nullary type aliases should not have parentheses") | ||
(help source (cst:second form) | ||
(lambda (existing) | ||
(subseq existing 1 (1- (length existing)))) | ||
"remove unnecessary parentheses"))) | ||
|
||
;; (define-type-alias (name type-variables+) ...) | ||
(loop :for vars := (cst:rest (cst:second form)) :then (cst:rest vars) | ||
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. check that the tyvars are unique 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. This takes place during the type checking phase, similar to |
||
:while (cst:consp vars) | ||
:do (push (parse-type-variable (cst:first vars) source) variables)))) | ||
|
||
;; (define-type-alias name) | ||
(unless (cst:consp (cst:rest (cst:rest form))) | ||
(parse-error "Malformed type alias definition" | ||
(note source form "expected type"))) | ||
|
||
;; (define-type-alias name type docstring) | ||
(when (and (cst:consp (cst:nthrest 3 form)) | ||
(cst:atom (cst:fourth form)) | ||
(stringp (cst:raw (cst:fourth form)))) | ||
(setf docstring (cst:raw (cst:fourth form)))) | ||
|
||
;; (define-type-alias name type docstring ...) | ||
(when (and docstring | ||
(cst:consp (cst:nthrest 4 form))) | ||
(parse-error "Malformed type alias definition" | ||
(note source (cst:fifth form) | ||
"unexpected trailing form"))) | ||
|
||
(make-toplevel-define-type-alias | ||
:name name | ||
:vars (reverse variables) | ||
:type (parse-type (cst:third form) source) | ||
:docstring docstring | ||
:location (form-location source form) | ||
:head-location (form-location source (cst:second form))))) | ||
|
||
(defun parse-define-struct (form source) | ||
(declare (type cst:cst form)) | ||
|
||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think these functions and their output may need some workshopping.
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.
Do you have any feedback as far as what is good/bad about them?