summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbd <bdunahu@operationnull.com>2025-03-16 19:32:12 -0400
committerbd <bdunahu@operationnull.com>2025-03-16 19:32:12 -0400
commit19d13c8339ee990fba358417a54aa6f1c94c7bca (patch)
tree9e5d2e3e937fa7a2f76c995cb44b90623424208c
parentaca2504eec49f140c76b668b6949ba767746a847 (diff)
Add clingon command-line arg parser, finish test harness setup
-rw-r--r--README.md1
-rw-r--r--rva.asd19
-rw-r--r--src/main.lisp68
-rw-r--r--src/package.lisp4
-rw-r--r--src/util.lisp5
-rw-r--r--t/main.lisp5
-rw-r--r--t/package.lisp4
-rw-r--r--t/util.lisp16
8 files changed, 88 insertions, 34 deletions
diff --git a/README.md b/README.md
index bc9852f..d797781 100644
--- a/README.md
+++ b/README.md
@@ -5,6 +5,7 @@
- SBCL
- ASDF
- fiveam
+- clingon
## To compile
diff --git a/rva.asd b/rva.asd
index 8d51f55..65a4262 100644
--- a/rva.asd
+++ b/rva.asd
@@ -8,11 +8,13 @@
:homepage "https://github.com/bdunahu/rva"
:description "Assembler for the RISC-V[ECTOR] mini-ISA."
:source-control (:git "git@github.com:bdunahu/rva.git")
- :depends-on ("uiop")
+ :depends-on (:uiop
+ :clingon)
:components ((:module "src"
- :serial t
- :components ((:file "package")
- (:file "main"))))
+ :serial t
+ :components ((:file "package")
+ (:file "util")
+ (:file "main"))))
:long-description
#.(uiop:read-file-string
(uiop:subpathname *load-pathname* "README.md"))
@@ -26,11 +28,12 @@
;; :license ""
:description "rva's test suite"
:depends-on (:rva
- :fiveam)
+ :fiveam)
:components ((:module "t"
- :serial t
- :components ((:file "package")
- (:file "main"))))
+ :serial t
+ :components ((:file "package")
+ (:file "main")
+ (:file "util"))))
:perform (test-op (o s) (uiop:symbol-call :rva-tests :test-rva)))
(defmethod asdf:perform ((o asdf:image-op) (c asdf:system))
diff --git a/src/main.lisp b/src/main.lisp
index ddb8b8d..c85e392 100644
--- a/src/main.lisp
+++ b/src/main.lisp
@@ -1,29 +1,59 @@
(in-package #:rva)
(defparameter *banner*
- " _/_/ _/_/
- _/ _/
- _/ _/ _/_/ _/ _/ _/_/_/ _/
- _/ _/_/ _/ _/ _/ _/ _/
- _/ _/ _/ _/ _/ _/ _/
- _/ _/ _/ _/_/_/ _/
+ " _/_/ _/_/
+ _/ _/
+ _/ _/ _/_/ _/ _/ _/_/_/ _/
+ _/ _/_/ _/ _/ _/ _/ _/
+ _/ _/ _/ _/ _/ _/ _/
+ _/ _/ _/ _/_/_/ _/
_/_/ _/_/ "
"Stylized ASCII logo.")
-(defun print-version-number ()
+(defun print-splash ()
"Prints a pretty splash-screen."
- (let ((system (asdf:find-system "rva" nil)))
- (format t "~av~a~%" *banner* (asdf:component-version system))))
+ (format t "~a~%" *banner*))
-(defun error-cli (message)
- "Prints MESSAGE and usage information to stderr
-and exits with error code 1."
- (format *error-output*
- "~a~%Usage:
- rva file~%"
- message)
- (sb-ext:exit :code 1))
+(defun cli/options ()
+ "Returns options for the `rva' assembler."
+ (list
+ (clingon:make-option
+ :flag
+ :description "run the lexer, but stop before parsing"
+ :long-name "lex"
+ :short-name #\l
+ :required nil
+ :key :lex)
+ (clingon:make-option
+ :flag
+ :description "run the parser, but stop before emission"
+ :long-name "parse"
+ :short-name #\p
+ :required nil
+ :key :parse)))
-(defun main ()
- (print-version-number)
+(defun driver (cmd)
+ "Reads in a file and directs lexing, parsing, and binary emission."
+ (print-splash)
+ (let ((args (clingon:command-arguments cmd))
+ (parse? (not (clingon:getopt cmd :lex)))
+ (emit? (not (clingon:getopt cmd :parse))))
+ (cond
+ ;; complain about num arguments
+ ((/= (length args) 1) (error "Wrong number of arguments."))
+ ((not (util:asm-extension? (car args))) (error "The file is not an asm source code file."))))
(error-cli "Nitimur in Vetitum"))
+
+(defun cli/command ()
+ "Returns a clingon command."
+ (clingon:make-command
+ :name "rva"
+ :description "generates a binary compatible with the RISC V[ECTOR] simulator"
+ :usage "[options] file"
+ :version(asdf:component-version
+ (asdf:find-system "rva" nil))
+ :options (cli/options)
+ :handler #'driver))
+
+(defun main ()
+ (clingon:run (cli/command)))
diff --git a/src/package.lisp b/src/package.lisp
index 78eb546..9d21293 100644
--- a/src/package.lisp
+++ b/src/package.lisp
@@ -1,3 +1,7 @@
(defpackage #:rva
(:use #:cl)
(:export #:main))
+
+(defpackage #:util
+ (:use #:cl)
+ (:export #:asm-extension?))
diff --git a/src/util.lisp b/src/util.lisp
new file mode 100644
index 0000000..87e4df9
--- /dev/null
+++ b/src/util.lisp
@@ -0,0 +1,5 @@
+(in-package #:util)
+
+(defun asm-extension? (file)
+ "Returns t if FILE is extended with .asm, nil otherwise."
+ (string= (pathname-type file) "asm"))
diff --git a/t/main.lisp b/t/main.lisp
index 11646b3..0bb62db 100644
--- a/t/main.lisp
+++ b/t/main.lisp
@@ -7,8 +7,3 @@
(defun test-rva ()
(run! 'all-tests))
-
-(test dummy-tests
- "Just a placeholder."
- (is (listp (list 1 2)))
- (is (= 5 (+ 2 3))))
diff --git a/t/package.lisp b/t/package.lisp
index 3581978..28a24c2 100644
--- a/t/package.lisp
+++ b/t/package.lisp
@@ -1,5 +1,5 @@
(defpackage #:rva-tests
(:use #:cl #:fiveam)
(:export #:run!
- #:test-rva
- #:all-tests))
+ #:test-rva
+ #:all-tests))
diff --git a/t/util.lisp b/t/util.lisp
new file mode 100644
index 0000000..ef59fbb
--- /dev/null
+++ b/t/util.lisp
@@ -0,0 +1,16 @@
+(in-package #:rva-tests)
+
+(def-suite util-tests
+ :description "Test functions designated as miscellaneous."
+ :in all-tests)
+
+(in-suite util-tests)
+
+(test asm-extension?-returns-false-obvious-case
+ (is (not (util:asm-extension? "quux.c"))))
+
+(test asm-extension?-returns-false-pattern-match-case
+ (is (not (util:asm-extension? "quux.asm.c"))))
+
+(test asm-extension?-returns-true-obvious-case
+ (is (util:asm-extension? "quux.asm")))