# Part II

## End of December 2019:

After been busy in the fall on other things I finally had time to get back to part 2 of the interpreter book. The book itself is now up to 70 pages and aim to submit for publication at around 150 pages or so.

## As of today (1/3/20):

The interpreter can now compile recursive code such as:

``// Recursive version for computing the Fibonacci Sequence``
``// Fn = Fn-1 + Fn-2, where Fo = 0 and F1 = 0``
``function fib (x)``
``   if ((x == 0) or (x == 1)) then``
``      return x;``
``   end;``
``   return fib (x-1) + fib (x-2)``
``end;``

``f = fib (31);``
``println (f);``

or user functions with local variables:

``function printme (x, y)``
``   a = x;``
``   b = y;   ``
``   println ("Print me: ");``
``   return a+b``
``end;``

``a = printme(2, 4) + 10;``
``println ("Answer = ", a)``

It also has support for while, for, repeat, and conditionals:

``println ("Testing while loop");``
``a = 6;``
``while a >= 1 do``
``  print ("while: ");``
``  println (a);  ``
``  a = a - 1;``
``end;``

``println ("Testing repeat loop");``
``a = 6;``
``repeat ``
``  print ("repeat: ");``
``  println (a);  ``
``  a = a - 1;``
``until a < 1;``

``println ("If statements");``
``a = 6;``
``if a > 4 then``
``   print ("If statement: ");``
``   println (a);``
``end;``

``println ("For loops:");``
``for i = 1 to 10 do``
``    print (i, " ")``
``end``

## As of today (1/8/20):

The interpreter now supports lists and can execute code such as:

``// Compute the Hilbert Matrix``

``n = 5; // Size of matrix``
``row = n*{0};``
``m = n*{row};``

``for i = 0 to n - 1 do``
``    for j = 0 to n - 1 do``
``          m[i,j] = 1/((i+1)+(j+1)-1);``
``    end;``
``end;``

Write a function to compute the Hilbert matrix:

``function hillbert (n)``
``   row = n*{0};``
``   m = n*{row};``

``   for i = 0 to n - 1 do``
``       for j = 0 to n - 1 do``
``           m[i,j] = 1/((i+1)+(j+1)-1);``
``       end;``
``   end;``
``   return m;``
``end;``

``println (hillbert (3));``

## As of today (1/14/2020):

The interpreter now supports break statements in looks and can execute code such as:

``i = 1;``
``while i < 10 do``
``    i = i + 1;``
``    println ("In loop: ", i);``
``    if i == 5 then``
``       break;``
``    end;``
``end;``
``println ("i = ", i);``

or

``for i = 3 to 10 do``
``    for j = 2 to 12 do``
``        println ("i = ", i, " j = ", j);``
``        if j == 5 then``
``           break;``
``        end;``
``    end;``
``    if i == 8 then``
``       break;``
``    end;``
``end;``

That competes the feature list for Part II of the series. I am now upto 145 regression tests. These have cought many edge cases that I would have missed otherwise. I will continue to add more tests to increase the coverage and then finish writing up the text itself.

## As of today (2/1/2020):

The garbage collection system appears to be finally stable. There may be the odd edge case that might still cause trouble but it passes all the current tests cases. Cases such as these will cause garbage, that is alocated memory that is no longer referenced.

``a = {1,2,3}``
``a = 1``
``a = "Beware" + "the" + "Ides" + "of" + "March" ``

The number of regression tests is now up to 198.

In my pursuit of efficiency I forgot to make functions first class citizens, that is objects that can be passed to other functions. I might have to fix that.

Lists have been expanded a little in the first version. The operators addition and multiplicaton now apply to lists. This means one can write:

For Multiplication:

``a = {0}``
``println (a*10)``
``{0,0,0,0,0,0,0,0,0,0}``

``a = {{1,2}}*5``
``println (a)``
``{{1,2},{1,2},{1,2},{1,2},{1,2}}``

``a = {0}``
``println (a + 1)  // Same as: a + {1}, this matches python sematics for lists. ``
``{0,1}``

``a = {1} + {True} + {"string"} + {3.14}``
``println (a)``
``{1,True,string,3.14}``

``a = {1} + {{2,3,4,5}}``
``println (a)``
``{1,{2,3,4,5}}``

## As of today (2/10/2020):

I redesigned the code for handling break statements. The original code was too complex, the new code is much simpler.

Continued to add text to the book, now at 92 pages.

More testing

The number of tests scripts is now at 208. Identified and fixed some issues with indexing variables inside user functions.

## As of today (2/15/2020)

Refactored the top level code to make ready for library support in part 3 of the book.

I did one last major (wasn't that major) change to the code which was to add support for builtins. I realized I needed at least int (x) in order to convert floats to integers. There won't be that many builtins because most functionality will be accessible via builtin modules.

Number of regression tests now at 209. Books pages now at 104.

What's left it to finish the book. It won't be much bigger than what it currently is as I've covered most of the topics. What's leves is editing and anything I' discover I left out.

## As of today (2/17/2020)

The page count for the book is now 125 pages and that is probably it for Part II. The current chapter titles are:

1. Interpreters

2. The Virtual Machine

3. A Simple Assembler

4. Testing and Performance

5. Emitting Code

6. User Functions

7. Supporting Strings

8. Supporting Lists

9. Built-in Functions

10. A Glimpse at Part III

The book will now go through a series of editing phases and adding anything that was left out.

## As of today (2/24/2020)

The page count for the book is now 149 pages and I think that really is it for Part II. I added a new appendix on the console, expanded some sections on testing and more material on lists. I added a basic index, and preface. I'd like to have it published by 7th March because after that I am traveling on and off for a few weeks. I added a google discussion forum on the main page and split the site into four pages.

## As of today (2/26/2020)

I forgot I hadn't included global functinality in user fucntions. This is the same as how global is used in Python. Now added to source code. Added another three tests.

## As of today (3/7/2020)

Editing the book continues and the page count is now at 155 pages. I've made one addiitonal change to the grammar. It annoyed me that in some places one had to put semicolons and in other places you didn't. In the latest version, I've made semicolons entirely optional.

I'm still on schedule for an end of March release.

# List test, create nested list {{6,7},4,9}# pushi 1 pushi 4 pushi 9 createList 3 store 0 pushi 6 pushi 7 createList 2 pushi 0 load 0 svecIdx load 0 print
# Test simple builtin call, 3 is the sqrt function, repeat 10 times# pushd 10.0 store 0L1: load 0 builtin 3 print load 0 pushd 1.0 sub store 0 load 0 print load 0 pushd 0.0 isGt jmpIfTrue L1
# Call with arguments# pushi 4 call 1
# Repeat/until loop# i = 1# repeat# print i# i = i + 1# until i > 5# print "Done"# pushi 1 store 0l1: load 0 print pushi 1 load 0 add store 0 load 0 pushi 5 isGt jmpIfFalse l1 pushs "Done"l2: print