diff options
| author | Siddarth Suresh <155843085+SiddarthSuresh98@users.noreply.github.com> | 2025-04-12 13:06:51 -0400 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-04-12 13:06:51 -0400 | 
| commit | fc20e7e7276b712f1e8db773b9215f900e877169 (patch) | |
| tree | caecdd1499d2e391cd5bd2dcde3aebfade002a09 /src/main.lisp | |
| parent | 5dbf0b63988b42c112ca0087cbbbb090566df5c1 (diff) | |
| parent | 639098b1ea82be82bd18a4af415458fcbaf5e20b (diff) | |
Merge pull request #8 from bdunahu/bdunahu
Add write raw bytes stage
Diffstat (limited to 'src/main.lisp')
| -rw-r--r-- | src/main.lisp | 44 | 
1 files changed, 34 insertions, 10 deletions
| diff --git a/src/main.lisp b/src/main.lisp index f20b022..f1f3021 100644 --- a/src/main.lisp +++ b/src/main.lisp @@ -23,27 +23,51 @@ _/_/                                  _/_/  "      :long-name "parse"      :short-name #\p      :required nil -    :key :parse))) +    :key :parse) +   (clingon:make-option +    :flag +    :description "run the emitter, but stop before writing" +    :long-name "emit" +    :short-name #\e +    :required nil +    :key :emit))) + +(defun postprocess (out file) +  "Given OUT, a list of words, writes the raw bytes to FILE." +  (let ((file (util:generate-file-name file)) +        (bytes (alexandria:flatten +                (mapcar (lambda (x) +                          (util:word-to-bytes (parse-integer x :radix 2))) +                        out)))) +    (with-open-file (stream file +                            :direction :output +                            :if-exists :supersede +                            :if-does-not-exist :create +                            :element-type '(unsigned-byte 8)) +      (loop for byte in bytes do (write-byte byte stream))) +    (format t "File written! Check ~a~%" file)))  (defun driver (cmd)    "Reads in a file and directs lexing, parsing, and binary emission."    (print-splash)    (let* ((args (clingon:command-arguments cmd))           (file (car args)) -         (emit? (not (clingon:getopt cmd :parse)))) +         (emit? (not (clingon:getopt cmd :parse))) +         (write? (not (clingon:getopt cmd :emit))))      (cond -      ;; complain about num arguments        ((/= (length args) 1) (error "Wrong number of arguments.~%"))        ((not (util:asm-extension? file))         (error "The file is not an asm source code file.~%"))        (t (let ((str (uiop:read-file-string file)))             (if str -	       (let ((ast (esrap:parse 'parse::str->ast (string-upcase str)))) -		 (when emit? -		   (format t "~a~%" (emit::emit ast)))) -               (error "The file does not exist, or it could not be opened.~%")) -           (format t "Nitimur in Vetitum~%")))))) - +               (let ((ast (esrap:parse 'parse::str->ast (string-upcase str)))) +                 (if emit? +                     (let ((words (emit:emit ast))) +                       (if write? +                           (postprocess words file) +                           (format t "Emission successful, got: ~%~a~%" words))) +                     (format t "Parse successful, got:~%~a~%" (emit:ast->str ast)))) +               (error "The file does not exist, or it could not be opened.~%")))))))  (defun cli/command ()    "Returns a clingon command." @@ -51,7 +75,7 @@ _/_/                                  _/_/  "     :name "rva"     :description "generates a binary compatible with the RISC V[ECTOR] simulator"     :usage "[options] file" -   :version(asdf:component-version +   :version (asdf:component-version              (asdf:find-system "rva" nil))     :options (cli/options)     :handler #'driver)) | 
