Skip to content

Commit

Permalink
Add coalton implicit progn (#1347)
Browse files Browse the repository at this point in the history
* add implicit progn for coalton expressions

* changed location of implicit progn

* changed if to cond in read-expressions
  • Loading branch information
YarinHeffes authored Jan 13, 2025
1 parent 6c6df06 commit 99f2560
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/parser/expression.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
#:node-do-nodes ; ACCESSOR
#:node-do-last-node ; ACCESSOR
#:parse-expression ; FUNCTION
#:parse-expressions ; FUNCTION
#:parse-body ; FUNCTION
#:parse-variable ; FUNCTION
))
Expand Down Expand Up @@ -1040,6 +1041,19 @@ Rebound to NIL parsing an anonymous FN.")
:collect (parse-expression rand source))
:location (form-location source form)))))

(defun parse-expressions (forms source)
(declare (type list forms)
(values node &optional))
(let ((nodes (mapcar (lambda (form) (parse-expression form source)) forms)))
(make-node-progn
:body (make-node-body
:nodes nodes
:last-node (first (last nodes)))
:location (source:make-location
source
(cons (source:span-start (cst:source (first forms)))
(source:span-end (cst:source (first (last forms)))))))))

(defun parse-variable (form source)
(declare (type cst:cst form)
(values node-variable &optional))
Expand Down
30 changes: 30 additions & 0 deletions src/parser/toplevel.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
#:parse-toplevel-form ; FUNCTION
#:read-program ; FUNCTION
#:read-expression ; FUNCTION
#:read-expressions ; FUNCTION
))

(in-package #:coalton-impl/parser/toplevel)
Expand Down Expand Up @@ -555,6 +556,35 @@ If MODE is :macro, a package form is forbidden, and an explicit check is made fo

(parse-expression form source))))

(defun read-expressions (stream source)
(let* (;; Setup eclector readtable
(eclector.readtable:*readtable*
(eclector.readtable:copy-readtable eclector.readtable:*readtable*)))

;; Read the coalton form
(multiple-value-bind (form presentp)
(maybe-read-form stream source *coalton-eclector-client*)

(unless presentp
(parse-error "Malformed coalton expression"
(note source (cons (- (file-position stream) 2)
(- (file-position stream) 1))
"missing expression")))

(let ((additional-forms nil))
;; Read multiple forms if present.
(block collect-additional-forms
(loop (multiple-value-bind (next-form presentp)
(maybe-read-form stream source *coalton-eclector-client*)
(if presentp
(push next-form additional-forms)
(return-from collect-additional-forms)))))
(cond
((consp additional-forms)
(parse-expressions (cons form (nreverse additional-forms)) source))
(t
(parse-expression form source)))))))

;;; Packages

(defun parse-import-statement (package cursor)
Expand Down
2 changes: 1 addition & 1 deletion src/reader.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ SOURCE provides metadata for the stream argument, for error messages."
nil)

(coalton:coalton
(entry:expression-entry-point (parser:read-expression stream source)))
(entry:expression-entry-point (parser:read-expressions stream source)))

;; Fall back to reading the list manually
(t
Expand Down

0 comments on commit 99f2560

Please sign in to comment.