by MarsIronPI · 1y ago · 0 comments
Still a really cool article and an impressive project, though. I especially like the StringPool technique; I'll have to keep it in mind if I ever write a compiler!
I miss how fast it was, compared to modern computers. VS Code is much laggier in comparison.
Now I write Python for fun.
And what kinds of programs could you write with it?
HP's calculators are pretty capable, with built-in commands for image processing, so RPL programs could rival the capabilities of assembly language programs on TI-8x calculators.
Historically, at least, it's pretty verbose to define a data type in Python compared to languages that are more designed for writing compilers. Consider these definitions from my prototype Bicicleta interpreter, which is written in ML, specifically OCaml:
type methods = NoDefs
(* name, body, is_positional ... *)
| Definition of string * bicexpr * bool * methods
and bicexpr = Name of string
| Call of bicexpr * string
| Literal of string option * methods
| Derivation of bicexpr * string option * methods
| StringConstant of string
| Integer of int
| Float of float
| NativeMethod of (lookup -> bicobj)
Those ten lines of code would be ten classes in Python with an average of 1.6 attributes each. Using dataclasses or attrs, that would be 36 lines of code, and then (if you're doing it the OO way) every function that I defined on one of these OCaml types becomes a method implemented in each class implementing a particular protocol, with a copy of its argument signature in every class. (If you used namedtuple instead, it's no less code, but you write it on less lines.) So, for example, this function on bicexprs let rec freevars = function
Name n -> stringset [n]
| Integer _ | StringConstant _ | Float _ -> stringset ["prog"]
| NativeMethod _ -> stringset []
| Literal (Some selfname, methods) ->
StringSet.diff (freevars_methods methods) (stringset [selfname])
| Literal (None, methods) -> freevars_methods methods
| Derivation(object_, self, methods) ->
StringSet.union (freevars object_) (freevars (Literal(self, methods)))
| Call(object_, _) -> freevars object_
becomes six to eight method definitions in the different classes. (You can cut it down to six if you define an abstract base class for the constant classes.) And Literal.freevars needs an if-then-else. So that's another 20 lines of code.Python does support pattern-matching now, so functions like this might not be any more verbose than the ML version if you program them the same way instead of in the OO fashion. I haven't tried using Python pattern-matching, so I don't really know.
In general, though, Python is more verbose than ML-family languages for this kind of thing by a factor of about 2–4, and that's before you count the test code you need in Python to get the kind of confidence in correctness that ML's type-checking gives you with no extra code. To my knowledge, Mypy doesn't do the kinds of pattern-matching-exhaustiveness checks that ML compilers do.
I've sometimes "cheated" by trying to write code like this in Python using regular tuples rather than named tuples. You can definitely make it work, but it's a real pain to debug.
Quoting Andy Chu from https://andychu.net/projects/:
> Python is not the right language for [implementing] languages. I will use OCaml for subsequent projects like this.
Python does have GC and dynamic dispatch, though, and those count for a lot.
In my half-finished Ruby compiler prototype, even before I added type tagging, and so allocated every integer on the heap, I just didn't add a GC for a long time because it was fine to just leak, because the compiler isn't generally long running.
This is Common Lisp, which I am not an expert in:
;;; Stupid CLOS example.
(defgeneric x (point)) ; make X a method
(defgeneric y (point)) ; make Y a method
(defclass rect-point ()
((x :accessor x :initarg :x)
(y :accessor y :initarg :y)))
(defclass polar-point ()
((radius :accessor radius :initarg :radius)
(angle :accessor angle :initarg :angle)))
(defmethod x ((p polar-point))
(* (radius p) (cos (angle p))))
(defmethod y ((p polar-point))
(* (radius p) (sin (angle p))))
(defgeneric move-by (point Δx Δy))
(defmethod move-by ((p rect-point) Δx Δy)
(incf (x p) Δx)
(incf (y p) Δy))
(defmethod move-by ((p polar-point) Δx Δy)
(let ((x (+ (x p) Δx)) (y (+ (y p) Δy)))
(setf (radius p) (sqrt (+ (* x x) (* y y)))
(angle p) (atan y x))))
(defmethod print-object ((p polar-point) stream) ; standard method print-object
(format stream "@~a<~a" (radius p) (angle p)))
(defvar p1 (make-instance 'rect-point :x 3 :y 4))
(defvar p2 (make-instance 'polar-point :radius 1 :angle 1.047))
;; prints (3, 4) → (4, 5)
(format t "(~a, ~a) → " (x p1) (y p1))
(move-by p1 1 1)
(format t "(~a, ~a)~%" (x p1) (y p1))
;; prints @1<1.047 (0.500171, 0.8659266) → @1.9318848<0.7853087 (1.366171, 1.3659266)
(format t "~a (~a, ~a) → " p2 (x p2) (y p2))
(move-by p2 .866 .5)
(format t "~a (~a, ~a)~%" p2 (x p2) (y p2))
Here's a similar program in OCaml, which I am also not an expert in, using pattern-matching functions instead of methods, and avoiding mutation: (* Stupid OCaml example. *)
type point = Rect of float * float | Polar of float * float
let x = function
| Rect(x, y) -> x
| Polar(r, theta) -> r *. Float.cos theta
let y = function
| Rect(x, y) -> y
| Polar(r, theta) -> r *. Float.sin theta
let moved_by = fun dx dy ->
function
| Rect(x, y) -> Rect(x +. dx, y +. dy)
| p ->
let x = dx +. x p and y = dy +. y p in
Polar(Float.sqrt(x *. x +. y *. y),
Float.atan2 y x)
let string_of_point = function
| Rect(x, y) -> Printf.sprintf "Rect(%f, %f)" x y
| Polar(r, theta) -> Printf.sprintf "Polar(%f, %f)" r theta
;;
print_endline(string_of_point(moved_by 1. 2. (Rect(3., 4.)))) ;
print_endline(string_of_point(moved_by 0.866 0.5 (Polar(1., 1.047))))I think this more function-focused approach is more elegant, but also more extensible and possibly less verbose when dealing with multiple classes that share the same interface.
> the same as in Python except that you also have to define the generic function itself before you can define the methods. As a side note, though it's not terribly important, the "defgeneric" can be omitted if you don't care to specify docstring or any special behavior.
And B evolved from BCPL:
https://en.m.wikipedia.org/wiki/BCPL
I had read the book about BCPL by Martin Richards, creator of the language. it was quite interesting. IIRC, after describing the language, he gave some examples of small system software routines or utilities, written in it.
https://en.m.wikipedia.org/wiki/Martin_Richards_(computer_sc...