Neolithic Tiny Basic

Programming the 6502 microprocessor and its relatives in assembly and other languages.
Post Reply
User avatar
BigEd
Posts: 11463
Joined: 11 Dec 2008
Location: England
Contact:

Re: Neolithic Tiny Basic

Post by BigEd »

This is a personal view, but I'd be wary of a syntax design which involves scanning ahead. (ELSE is not so bad in BBC Basic as it only scans to the end of the line.) But the idea of finding a "matching" element seems to me a problematic one: you might need a rule that such an element has to be in the first statement on a line, or not guarded by an IF, for example.

I very often write code where a FOR might have two or three NEXTs, or similarly for UNTILs. Perhaps I'm just a very naughty boy. Or perhaps such code should be illegal in a particular dialect which does try to match loop start with loop end.
User avatar
barrym95838
Posts: 2056
Joined: 30 Jun 2013
Location: Sacramento, CA, USA

Re: Neolithic Tiny Basic

Post by barrym95838 »

I'm familiar with only the Micro$oft, Woz and TRS-80 Level I variations of BASIC, and for me a FOR/NEXT loop that may execute zero times is unexpected, due to the extra effort in scanning forward (through treacherous but legal spaghetti) ... that's more of a C thing, isn't it?

[Edit: I see that Ed and I think along similar "naughty" lines. I believe that DEC even offered an UNLESS keyword, which adds another (unnecessary IMO) kink ...]
Got a kilobyte lying fallow in your 65xx's memory map? Sprinkle some VTL02C on it and see how it grows on you!

Mike B. (about me) (learning how to github)
barnacle
Posts: 1831
Joined: 19 Jan 2004
Location: Potsdam, DE
Contact:

Re: Neolithic Tiny Basic

Post by barnacle »

And I've just discovered that my for/next isn't behaving as I expected it to. I think a problem with the comparison... need to think about that.

But first, input...

Neil
User avatar
drogon
Posts: 1671
Joined: 14 Feb 2018
Location: Scotland
Contact:

Re: Neolithic Tiny Basic

Post by drogon »

barrym95838 wrote:
I'm familiar with only the Micro$oft, Woz and TRS-80 Level I variations of BASIC, and for me a FOR/NEXT loop that may execute zero times is unexpected, due to the extra effort in scanning forward (through treacherous but legal spaghetti) ... that's more of a C thing, isn't it?
Scanning forward in an interpreter is/can be tricky. I do it in my 'big' Basic but it's limited to one statement per line which helps this and some other things too.

C treats a FOR loop like a WHILE construct... So:

Code: Select all

for (i = 0 ; i < 10 ; ++i) { do somestuff} 
is effectively like:

Code: Select all

  i = 0 ;
  while (i < 10) {
    do somestuff ;
    ++i ;
  }
So it can be executed zero times - but then the compiler has to forward scan that anyway, so knows that the {} isn't closed and can whinge about it. Forward scanning in an interpreter is as tricky in that I have to count all inside loops and hope that someone like Ed didn't use a naughty GOTO to jump out of the loop ;-)

Quote:
[Edit: I see that Ed and I think along similar "naughty" lines. I believe that DEC even offered an UNLESS keyword, which adds another (unnecessary IMO) kink ...]
BCPL also has UNLESS which just negates the test.

-Gordon
--
Gordon Henderson.
See my Ruby 6502 and 65816 SBC projects here: https://projects.drogon.net/ruby/
barnacle
Posts: 1831
Joined: 19 Jan 2004
Location: Potsdam, DE
Contact:

Re: Neolithic Tiny Basic

Post by barnacle »

BigEd wrote:
I very often write code where a FOR might have two or three NEXTs, or similarly for UNTILs. Perhaps I'm just a very naughty boy. Or perhaps such code should be illegal in a particular dialect which does try to match loop start with loop end.
<shudders> Ye gods and little fishies, I'd forgotten that such shenanigans were even legal, let alone expected... that sort of code's going to break my basic, I think. So, er, in my basic it's not legal, OK? :mrgreen:

Neil

p.s. yup, it breaks it; it mostly works, but this code with two nexts from a single for:
Screenshot from 2025-02-27 20-37-00.png
executes both the inner and outer loops the expected number of times, but it's clearly not matching the nexts correctly; line 80 is executed only once and its next is being treated as an end (reasonable: an endif, next, or return all do the same thing: nothing; they're considered markers rather than executable keywords).
Screenshot from 2025-02-27 20-38-01.png
Neil
User avatar
BigEd
Posts: 11463
Joined: 11 Dec 2008
Location: England
Contact:

Re: Neolithic Tiny Basic

Post by BigEd »

(FWIW I believe there's a Basic on the Spectrum Next which likewise tries to match loop beginnings and ends. And I think I checked that ANSI Basic also does. So, it's certainly a design choice.)
User avatar
drogon
Posts: 1671
Joined: 14 Feb 2018
Location: Scotland
Contact:

Re: Neolithic Tiny Basic

Post by drogon »

It's not 'legal' in my TinyBasic to omit the loop variable - and it is checked. Could I remove the need for it and check? Yes, but I think I like it. One thing I can't do is:

Code: Select all

NEXT J,I
which you can do in some Basics..

-Gordon
--
Gordon Henderson.
See my Ruby 6502 and 65816 SBC projects here: https://projects.drogon.net/ruby/
barnacle
Posts: 1831
Joined: 19 Jan 2004
Location: Potsdam, DE
Contact:

Re: Neolithic Tiny Basic

Post by barnacle »

My feeling is that whatever is being attempted with multiple nexts from the same for can probably be achieved with more structured, er, structure.

I'm having some difficulty even contemplating how you might track nexts if the language not only permits that you can have more than one next per for but also that any given next might apply to more than one for... truly 'goto considered harmful'

I suppose it comes down to whether one wishes to write a structured program, or treat basic like spaghetti machine code?

Strictly, of course, the only construct you really need is if and goto... not even endif.

Neil
User avatar
BigEd
Posts: 11463
Joined: 11 Dec 2008
Location: England
Contact:

Re: Neolithic Tiny Basic

Post by BigEd »

A particular thing which I find quite difficult to write efficiently as a properly structured program is a prime tester using trial division - because there are two conditions for leaving a loop, broadly speaking.
User avatar
drogon
Posts: 1671
Joined: 14 Feb 2018
Location: Scotland
Contact:

Re: Neolithic Tiny Basic

Post by drogon »

BigEd wrote:
A particular thing which I find quite difficult to write efficiently as a properly structured program is a prime tester using trial division - because there are two conditions for leaving a loop, broadly speaking.
A-Ha! I was about to say "oh no you don't" ... Then I checked. The 'core' of my B Prime checker:

Code: Select all

800REM Is it Prime?
810REM Input N, output P - true or false (1 or 0)
811REM
820IF N < 1 P = 0 : RETURN : REM 0 and 1 are not Prime
830IF N < 4 P = 1 : RETURN : REM 2 and 3 are Prime
834IF N%2 = 0 P = 0 : RETURN : REM Trivial case for even numbers
840S = N : GOSUB 1000 : U = Q : REM Test up to the sqrt of S
845T = 3
850DO
860  IF N % T = 0 THEN P = 0 : DPOP: RETURN
870  T = T + 2
880UNTIL T > Q
890P = 1
899RETURN
See that DPOP there? Well - proves you right :-)

-Gordon
--
Gordon Henderson.
See my Ruby 6502 and 65816 SBC projects here: https://projects.drogon.net/ruby/
barnacle
Posts: 1831
Joined: 19 Jan 2004
Location: Potsdam, DE
Contact:

Re: Neolithic Tiny Basic

Post by barnacle »

Well, at the moment I seem to have completed the last module: input of either an integer or a string. So it's _nominally_ finished, though I need to look again at that for/next issue; perhaps something wrong in the compare routine. I also need to rationalise variable names and jump target names.

Then I'll stick it up and let you all have a laugh...

But I'm under 4k (just - $fcb = 4043 bytes :) )

Neil
User avatar
BigEd
Posts: 11463
Joined: 11 Dec 2008
Location: England
Contact:

Re: Neolithic Tiny Basic

Post by BigEd »

well done! Under 4k is impressive!
barnacle
Posts: 1831
Joined: 19 Jan 2004
Location: Potsdam, DE
Contact:

Re: Neolithic Tiny Basic

Post by barnacle »

The parent C version - x64 linux - is 29.7k, so it's improved somewhat.

Neil
User avatar
BigEd
Posts: 11463
Joined: 11 Dec 2008
Location: England
Contact:

Re: Neolithic Tiny Basic

Post by BigEd »

Wow!
barnacle
Posts: 1831
Joined: 19 Jan 2004
Location: Potsdam, DE
Contact:

Re: Neolithic Tiny Basic

Post by barnacle »

Well as a first cut, here it is. A couple of issues that I know about:
  • There are some combinations of values for which for/next never stops. Still investigating; I may rewrite the whole chunk.
  • Code to list single lines or group of lines still to be done
  • As far as I know, execution stops on an error, with a message, but does not yet list the line number
  • There is no 'oops' (e.g. ctrl-C) or warm restart

Code: Select all

; assemble with as65, http://www.kingswood-consulting.co.uk/assemblers/
; use: as65 tiny.asm -h0 -ltiny.lst -s2 -x -m
; (or remove -s2 for binary output)
;
; THIS BUILD FOR SYMON EMULATOR - check ACIA register locations
It builds to just under 4k, starting $e000.

Enjoy, please let me know if you find (or fix!) any bugs. It's explicit in the code, but, FreeBSD licence to avoid future worries.

Neil
Attachments
tiny.asm
(73.75 KiB) Downloaded 202 times
Post Reply