Up to Main Index                               Up to Journal for May, 2023

                     JOURNAL FOR WEDNESDAY 31ST MAY, 2023
______________________________________________________________________________

SUBJECT: Getting on with some programming
   DATE: Wed 31 May 21:37:41 BST 2023

The end of another month looms nigh. After two weeks of messing around setting
up and tweaking my new PC I’ve settled down to some proper programming again.
At the moment that involves writing more tests for Mere. I think I have about
fifteen operators left to update or write tests for. Then I’ll update Mere ICE
with all the changes for adding the unsigned integer type. Changes will also
include a new bit-wise not operator ‘¬’. For example:


    >cat not.mr
    x = 0b00010101u
    printf "%08b\n" x
    printf "%08b\n" ¬x & 0b11111111u

    >mere not.mr
    00010101
    11101010
    >


The “& 0b11111111u” above masks off the first 8 bits of x to print it nicely.
Else “1111111111111111111111111111111111111111111111111111111111101010” would
be printed in full for x.

The choice of using the traditional bit-wise not operator ‘¬’ may be a little
controversial[1]. I can change it later or provide an alternative — but I’m
running out of symbols. The caret ‘^’ is already used for bit-wise XOR and the
tilde ‘~’ is tentatively reserved for built-in regular expression support…

What next?

After that I’ll be tackling functions. Without functions, and the scoping of
variables they provide, larger Mere code is a mess of globals. I already know
how I want to implement functions, but I’m going to have to think about how to
handle variable scoping.

Scoping is relevant here as variables should be local to the function in which
they are defined. Function scope is probably the easiest one to deal with.
When calling a function: create a new stack frame, populate it with any passed
parameters, replace current stack frame with the new one, on return populate
the previous stack frame with any returned values, throw away the current
stack frame and revert to the previous one. There is more to it than that, but
that’s the basic idea.

This simple idea should work for functions calling other functions as well as
for recursive functions that call themselves.

Another area I’ll need to address is the symbol table and variable storage. As
mentioned previously all variables, and labels, are currently global. Not too
much of an issue as globals will only ever be placed in the first stack frame.

What is a problem is that variable storage is currently tagged at compile
time. That is, a storage slot is tagged with a variable’s symbol table ID. A
problem if a function called twice tries using the same slots.

I’ve already played a bit with an alternative scheme. It seems to work quite
well, but a few tests break due to the way I am counting variables to check
for memory leaks. Not a huge issue, but something I need to consider and fix.

--
Diddymus

  [1] I type ‘¬’ either using Ctrl-k ‘N’ ‘O’ in VIM or AltGr-‘,’ elsewhere.


  Up to Main Index                               Up to Journal for May, 2023