Skip to content

Commit

Permalink
Add EXTERN, numerical expression support, printing
Browse files Browse the repository at this point in the history
  • Loading branch information
macrologist committed Apr 16, 2024
1 parent a9952a5 commit fba864e
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 93 deletions.
86 changes: 53 additions & 33 deletions src/ast.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,13 @@ names. Meaning if, for example, a DEFCIRCUIT is defined with name
marked as STUB, that circuit will be totally ignored by
compilation passes."))

(defclass extern ()
((name :reader extern-name
:initarg :name
:documentation "The name of the function."))
(:documentation "A function that operates on classical memory and values, declared to
be available in the underlying system."))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Definitions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Gate Definitions
Expand Down Expand Up @@ -1568,15 +1575,17 @@ For example,
(cond
((eql 'mref (first expr))
(format stream "~A[~A]" (second expr) (third expr)))
((= (length expr) 3)
((and (= (length expr) 3)
(lisp-symbol->quil-infix-operator (first expr)))
(format stream "(~A~A~A)"
(print-delayed-expression (second expr) nil)
(lisp-symbol->quil-infix-operator (first expr))
(print-delayed-expression (third expr) nil)))
((= (length expr) 2)
(format stream "~A(~A)"
(t
(format stream "~A(~{~A~^,~})"
(lisp-symbol->quil-function-or-prefix-operator (first expr))
(print-delayed-expression (second expr) nil)))))
(loop :for ex :in (rest expr)
:collect (print-delayed-expression ex nil))))))
(number
(format stream "(~/cl-quil:complex-fmt/)" expr))
(symbol
Expand Down Expand Up @@ -1660,6 +1669,9 @@ For example,
(:method ((instr stub) (stream stream))
(format stream "STUB ~A" (stub-name instr)))

(:method ((instr extern) (stream stream))
(format stream "EXTERN ~A" (extern-name instr)))

(:method ((instr application) (stream stream))
(print-operator-description (application-operator instr) stream)
(format stream "~@[(~{~/cl-quil:instruction-fmt/~^, ~})~]~{ ~/cl-quil:instruction-fmt/~}"
Expand Down Expand Up @@ -1766,33 +1778,46 @@ For example,
;;; simply a list of AST objects.

(defclass parsed-program (transformable)
((gate-definitions :initarg :gate-definitions
:accessor parsed-program-gate-definitions
:type list
:documentation "The gate definitions introduced by DEFGATE.")
(circuit-definitions :initarg :circuit-definitions
:accessor parsed-program-circuit-definitions
:type list
:documentation "The circuit definitions introduced by DEFCIRCUIT.")
(memory-definitions :initarg :memory-definitions
:accessor parsed-program-memory-definitions
:type list
:documentation "The memory definitions introduced by DECLARE.")
(executable-program :initarg :executable-code
:accessor parsed-program-executable-code
:type (vector instruction)
:documentation "A vector of executable Quil instructions.")
(stub-operations :initarg :stub-operations
:accessor parsed-program-stub-operations
:type hash-table
:documentation "A hash table mapping string NAMEs to generalized booleans, indicating that an operation so named is a stub."))
((gate-definitions
:initarg :gate-definitions
:accessor parsed-program-gate-definitions
:type list
:documentation "The gate definitions introduced by DEFGATE.")
(circuit-definitions
:initarg :circuit-definitions
:accessor parsed-program-circuit-definitions
:type list
:documentation "The circuit definitions introduced by DEFCIRCUIT.")
(memory-definitions
:initarg :memory-definitions
:accessor parsed-program-memory-definitions
:type list
:documentation "The memory definitions introduced by DECLARE.")
(executable-program
:initarg :executable-code
:accessor parsed-program-executable-code
:type (vector instruction)
:documentation "A vector of executable Quil instructions.")
(extern-declarations
:initarg :extern-declarations
:accessor parsed-program-extern-declarations
:type hash-table
:documentation "A hash table mapping string to booleans.")
(stub-operations
:initarg :stub-operations
:accessor parsed-program-stub-operations
:type hash-table
:documentation "A hash table mapping string names to booleans."))
(:default-initargs
:gate-definitions '()
:circuit-definitions '()
:memory-definitions '()
:executable-code #()
:extern-declarations (make-hash-table :test #'equal)
:stub-operations (make-hash-table :test #'equal))
(:documentation "A representation of a parsed Quil program, in which instructions have been duly sorted into their various categories (e.g. definitions vs executable code), and internal references have been resolved."))
(:documentation "A representation of a parsed Quil program, in which instructions have
been duly sorted into their various categories (e.g. definitions vs
executable code), and internal references have been resolved."))

(defmethod copy-instance ((parsed-program parsed-program))
(let ((pp (make-instance 'parsed-program)))
Expand All @@ -1809,14 +1834,9 @@ For example,
(map 'vector #'copy-instance
(parsed-program-executable-code parsed-program)))
(setf (parsed-program-stub-operations pp)
(let ((new-table
(make-hash-table :test #'equal))
(old-table
(parsed-program-stub-operations parsed-program)))
(loop :for key :being :the :hash-key :of old-table
:using (:hash-value value)
:do (setf (gethash key new-table) value))
new-table))
(a:copy-hash-table (parsed-program-stub-operations parsed-program)))
(setf (parsed-program-extern-declarations pp)
(a:copy-hash-table (parsed-program-extern-declarations parsed-program)))
pp))

(defvar *print-parsed-program-text* nil
Expand Down
7 changes: 6 additions & 1 deletion src/cl-quil.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ This also signals ambiguous definitions, which may be handled as needed."
(memory-defs '())
(exec-code '())
(stubs (make-hash-table :test #'equal))
(externs (make-hash-table :test #'equal))
;; The following maps definition signatures to a list of (filename . defn) pairs
(all-seen-defns (make-hash-table :test 'equalp)))
(flet ((bin (instr)
Expand All @@ -114,13 +115,16 @@ This also signals ambiguous definitions, which may be handled as needed."
(memory-descriptor (push instr memory-defs))
(stub
(setf (gethash (stub-name instr) stubs) t))
(extern
(setf (gethash (extern-name instr) externs) t))
(t (push instr exec-code)))))
(mapc #'bin code)
(make-instance 'parsed-program
:gate-definitions (nreverse gate-defs)
:circuit-definitions (nreverse circ-defs)
:memory-definitions (nreverse memory-defs)
:stub-operations stubs
:extern-declarations externs
:executable-code (coerce (nreverse exec-code)
'simple-vector)))))

Expand All @@ -143,7 +147,8 @@ This also signals ambiguous definitions, which may be handled as needed."
ambiguous-definition-handler)))
(let ((*current-file* originating-file)
(*parser-extensions* parser-extensions)
(*lexer-extensions* lexer-extensions))
(*lexer-extensions* lexer-extensions)
(*names-declared-extern* +builtin-externs+))
(let* ((raw-quil (parse-quil-into-raw-program string))
(pp (resolve-objects
(funcall build-parsed-program
Expand Down
Loading

0 comments on commit fba864e

Please sign in to comment.