about summary refs log tree commit diff stats
path: root/cbasic.lisp
blob: 2ddc5b4005a6fbcec79b40868b72c710c59eb9d9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
;;; I'm trying to write an interpreter for a BBC BASIC subset
;;; initially following the design of
;;; https://caml.inria.fr/pub/docs/oreilly-book/html/book-ora058.html
;;; then later I can optimise following
;;; https://github.com/Henry/BuddKaminInterpreters and *maybe*
;;; https://oleksandrmanzyuk.wordpress.com/2014/06/18/from-object-algebras-to-finally-tagless-interpreters-2/
;;;
;;; It might be worth reading the example in "Beautiful Racket" for
;;; the extensions suggested on the OCaml page,
;;; or more likely switch to COMAL.
;;;
;;; A BASIC interpreter already exists at
;;; https://gitlab.com/com-informatimago/com-informatimago/-/tree/master/small-cl-pgms/basic
;;; but it is idiomatic CL,
;;; whereas I'm experimenting with a subset like ISLisp.

;; Because this is the main package,
;; 1) ql:quickload QuickLisp dependencies and
(ql:quickload "closer-mop")
;; 2) load local dependencies here.
(load "cutil.lisp")
(load "cabs-syn.lisp")
(load "clex.lisp")
(load "cparse.lisp")
;;
;; Use require & defpackage in each package from then on.
;; NB: must have no circular dependencies, and topologically sort the loads.
;;
;; TODO: is it easier to just get started with ASDF?
;;       ( https://lisp-lang.org/learn/writing-libraries )
;;       Although this is beyond OpenLisp.

(require "cparse")
(defpackage #:cbasic
  (:use #:common-lisp)
  (:export
   #:main))
(in-package #:cbasic)

;;; Not sure if it's a good idea,
;;; but try to keep the number of top-level functions the same as in OCaml.

(defun one-command (st)
  (format *standard-output* "> ")
  (with-handler #'error-handler
    (let ((l (parse (read-line))))
      (case (car l)
        ((line) (insert (cadr c)))
        ((p-end) (throw 'end nil)))))) ; throw and conditions are orthogonal

(defclass <state> () ((program :accessor program)
                      (env :accessor env)))
(defmethod initialize-object :after ((self <state>) initargs)
  (setf (program self) nil)
  (setf (env self) nil))

(defun main ()
  (catch 'end (lambda ()
                (format *standard-output* "BASIC version 0.1~%~%")
                (for ((st (create (class <state>))))
                     (catch 'error (one-command st)))))
  (format *standard-output* "See you later...~%"))
(provide "cbasic")