Beginnings of a debugger

This commit is contained in:
Dane Johnson 2025-01-12 21:01:02 -06:00
parent 422c675981
commit 0c029118f0
2 changed files with 62 additions and 24 deletions

View File

@ -1,36 +1,69 @@
#! /bin/sh #! /bin/sh
exec guile -L . -e main -s "$0" "$@" exec guile -L . -e main -s "$0" "$@"
!# !#
(use-modules (ice-9 getopt-long) (use-modules (rnrs bytevectors)
(ice-9 binary-ports)
(srfi srfi-26)
(scmvm vm)) (scmvm vm))
(define *options-spec* (define the-vm #f)
'((output (single-char #\o) (define the-program #f)
(value #t))
(help (single-char #\h)
(value #f))
(stack-size (value #t))
(memory-size (value #t))))
(define parse-options (define (load-binary port)
(lambda (options) (set! the-vm (make-vm))
(getopt-long options *options-spec*))) (set! the-program port))
(define (usage) (eval-when (expand load eval)
(format #t "Usage: scmvm.scm [-o outfile] mode infile (use-modules (oop goops)
Compile or run Scheme programs (g-golf))
(g-irepository-require "Gtk" #:version "4.0")
(for-each (lambda (name)
(gi-import-by-name "Gio" name))
'("SimpleAction"
"SimpleActionGroup"))
(for-each (lambda (name) (gi-import-by-name "Gtk" name))
'("Application"
"ApplicationWindow"
"CallbackAction"
"FileDialog")))
Commands: (define (open-file win cb)
\tcompile\t Compile the source into an object file (define dialog (make <gtk-file-dialog>))
\trun\t Run the object file (define (cb-with-port dialog result data)
(call-with-input-file (get-path (open-finish dialog result)) cb))
(open dialog win #f cb-with-port #f))
Yes the VM runs on Scheme, no that doesn't make any sense\n") (define (install-action action-group name callback)
(exit)) (define action (make <g-simple-action> #:name name))
(add-action action-group action)
(connect action 'activate (lambda (a v) (callback))))
(define (activate app)
(define win (make <gtk-application-window>
#:application app
#:title "Scheme Compiler"))
;; All this to make a menu...
(let ([app-action-group (make <g-simple-action-group>)]
[menu (make <g-menu>)]
[file-menu (make <g-menu>)])
(append file-menu "Open..." "app.open-file")
(append-submenu menu "File" file-menu)
(set-menubar app menu)
(set-show-menubar win #t)
(insert-action-group win "app" app-action-group)
(install-action
app-action-group
"open-file"
(lambda ()
(open-file win load-binary))))
(present win))
(define* (main #:optional args) (define* (main #:optional args)
(define options (parse-options args)) (define app (make <gtk-application>))
(when (option-ref options 'help #f) (connect app 'activate activate)
(usage))) (let ([status (run app args)])
(exit status)))
;; Local Variables: ;; Local Variables:
;; mode: scheme ;; mode: scheme

View File

@ -4,7 +4,7 @@
#:use-module (rnrs bytevectors) #:use-module (rnrs bytevectors)
#:use-module (srfi srfi-1) #:use-module (srfi srfi-1)
#:use-module (srfi srfi-26) #:use-module (srfi srfi-26)
#:export (make-vm run-vm vm-memory-ref vm-memory-set! #:export (make-vm run-vm vm-memory-ref vm-memory-set! vm-memory
*instruction-set* instruction-type instruction-code)) *instruction-set* instruction-type instruction-code))
;;; Data Structures ;;; Data Structures
@ -65,7 +65,7 @@
((stack 'ref) k)) ((stack 'ref) k))
(define* (make-ram #:optional (memory-size *memory-size*)) (define* (make-ram #:optional (memory-size *memory-size*))
(make-vector memory-size)) (make-vector memory-size #x00))
(define (ram-ref ram k) (define (ram-ref ram k)
(vector-ref ram k)) (vector-ref ram k))
@ -189,6 +189,7 @@
(lambda (x) (lambda (x)
(case x (case x
[(run) fetch-and-execute] [(run) fetch-and-execute]
[(vm-memory) (lambda () ram)]
[(vm-memory-ref) (cute ram-ref ram <>)] [(vm-memory-ref) (cute ram-ref ram <>)]
[(vm-memory-set!) (lambda (k v) (ram-set! ram k v))]))) [(vm-memory-set!) (lambda (k v) (ram-set! ram k v))])))
@ -200,6 +201,10 @@
"Externally set VM memory at k to v" "Externally set VM memory at k to v"
((vm 'vm-memory-set!) k v)) ((vm 'vm-memory-set!) k v))
(define (vm-memory vm)
"Just get the memory vector"
((vm 'vm-memory)))
(define (run-vm vm port) (define (run-vm vm port)
"Read and execute instructions read from port on VM" "Read and execute instructions read from port on VM"
(with-input-from-port port (vm 'run))) (with-input-from-port port (vm 'run)))