uBasic is insane..!

24 views
Skip to first unread message

The Beez

unread,
Jun 12, 2021, 6:02:41 AM6/12/21
to 4tH-compiler
Hi 4tH-ers!

Well, the only connection between 4tH and uBasic/4tH is that the latter is written in 4tH. And you know how I went about. Use what you have and respect the original architecture. That mentality has kept 4tH maintainable for close to 30 years.

For uBasic I've thrown in a bit of C and Forth - which has resulted in a completely insane structure. Take a look at this Recaman sequence, which I wrote for Rosetta code.


a = 0
Print "First 15 numbers:"

For i = 1 Step 1
  If i<16 Then Print a,
  b = Iif ((a-i<1) + (Func(_Peek(Max(0, a-i))) = 1), a+i, a-i)
  If Func(_Peek(b)) = 1) Then Print "\nFirst repetition: ";b : Break
Proc _Set(Set(a, b))
Next
End 

_Set  Param(1) : Let @(a@/32) = Func(_Poke(a@/32, a@%32)) : Return
_Poke Param(2) : Return (Or(@(a@), Shl(1, b@)))
_Peek Param(1) : Return (And(@(a@/32), Shl(1, a@%32))>0)

For the Recaman sequence you have to keep track which numbers you've already visited. I did that by turning uBasic's only array into a bitmap - which is serviced by the last three functions and subroutines. Oneliners, of course - are we Forthers or not?

The FOR statement is the first indication that this is not your mummies Basic, since it doesn't have a limit. It loops forever, but STEP makes sure it still counts up.

The assignment of variable "b" is another indication that something strange if going on. It is done by an IIF() statement, which evaluates if it renders a negative number -or- if it resides in the bitmap. 

Now that might very well end up before the beginning of the bitmap (since it might render a negative value), so the Forth-like MAX() function clips it at ZERO. 

At the end of the loop variable "a" is SET() to "b" and the value of "b" is passed to user-defined function _SET() to set the appropriate bit in the bitmap.

Although it doesn't fulfill the optional part of the Rosetta assignment, it's pretty short compared to other implementations. Even with comments it's less than 1K sourcecode.

I love it ;-)

Hans Bezemer

The Beez

unread,
Jun 14, 2021, 5:01:01 AM6/14/21
to 4tH-compiler
Hi 4tH-ers!

The VERY first program uBasic/4tH ran was "99 bottles of beer". I still remember that one very well, since it blew up in my face - of course - and at that time I wondered whether I could get this 13K program ever get to run properly.
Of course I did and the rest is history. I was proud enough to post it on Rosetta code. Now, looking back more than ten years later, it's fun to see how much it developed. Thew first incarnation was a Classic Basic, but nowadays you can also work completely structured.


Hans Bezemer

Reply all
Reply to author
Forward
0 new messages