Implementing a Java-like language

One of the exciting topic in the course of computer science is mathematical theory of parsing. After learning the basics of the theory, it is a very good Exercise to design a small language and write a compiler or interpreter for the language. If you do not like to write a compiler for real CPU, the stackmachine sm1 will be a good target machine. For example, the language may accept the input

  12345678910111213*(256+2)
and the interpreter or the compiler generate the following code for sm1
  (12345678910111213)..
  (256)..
  (2).. add
   mul message
One can easily write an arbitrary precision calculator by using sm1 and also try algorithms in the number theory by one's own language.

Exercise 1: parse a set of linear equations like 2x+3y+z = 2; y-z =4; , output the equation in the matrix form and find solutions.
Exercise 2: Modify the calculator hoc so that it can use sm1 as the backend engine. The calculator hoc is discussed in the book: Kerningham and Pike, Unix programming environment.

The stackmachine sm1 provides a very strong virtual machine for object oriented system by the dictionary tree. We can easily implement a language, on which Java-like object oriented programming mechanism is installed, by using sm1. Here is a sample program of kan/k0, which is an object oriented language and works on sm1. I taught a course on writing mathematical softwares in a graduate school with k0.

class Complex extends Object {
  local re, /* real part */
        im; /* imaginary part*/
  def new2(a,b) {
    this = new(super.new0());
    re = a;
    im = b;
    return(this);
  }
  def real() { return(re); }
  def imaginary() { return(im); }
  def operator add(b) {
    return( new2(re+b.real(), im+b.imaginary()) );
  }
  def operator sub(b) {
    return( new2(re-b.real(), im-b.imaginary()) );
  }
  def operator mul(b) {
    return(new2( re*b.real()-im*b.imaginary(), re*b.imaginary()+im*b.real()));
  }
  def operator div(b) {
    local den,num1,num2;
    den = (b.real())^2 + (b.imaginary())^2 ;
    num1 = re*b.real() + im*b.imaginary();
    num2 = -re*b.imaginary()+im*b.real();
    return(new2(num1/den, num2/den));
  }

  def void show() {
    Print(re); Print(" +I["); Print(im); Print("]");
  }
  def void showln() {
    this.show(); Ln();
  }
}
a = Complex.new2(1,3);
a:
1 +I[3]
a*a:
-8 +I[6]

Nobuki Takayama 2020-11-24