ZX81 BASIC

Anything QL Software or Programming Related.
User avatar
NormanDunbar
Forum Moderator
Posts: 2251
Joined: Tue Dec 14, 2010 9:04 am
Location: Leeds, West Yorkshire, UK
Contact:

Re: ZX81 BASIC

Post by NormanDunbar »

In a tokenised QL SuperBASIC program, the functions have different token from the variables. They are also stored in the name table with a different type, so the interpreter knows which is which.

You can see what the internal representation is in this Quanta article by Dilwyn Jones. There's a useful decoder program included.

http://www.dilwyn.me.uk/docs/formats/savfiles.pdf

When tokenising a program, I think the names are entered in the name table when first encountered (and in that letter case) because each line is tokenised when typed. I imagine that later yse of the name in question would identify its type, so a LET X =, or just X =, would identify the type of X to be a floating point variable. X% is an obvious integer variable, X$ a string.

If the expression is LET X = Y, then Y could be a function or another floating point variable, assuming Y is not already defined and typed, a following DEF FuNction Y, would give the game away.

The ZX81 didn't have functions, so any name you see is, effectively, a floating point variable, unless followed by a $.

HTH

Cheers,
Norm.


Why do they put lightning conductors on churches?
Author of Arduino Software Internals
Author of Arduino Interrupts

No longer on Twitter, find me on https://mastodon.scot/@NormanDunbar.
User avatar
bwinkel67
QL Wafer Drive
Posts: 1187
Joined: Thu Oct 03, 2019 2:09 am

Re: ZX81 BASIC

Post by bwinkel67 »

Right, the issue comes up only within an expression such as:

Code: Select all

LET VALUE=5*PI+3
PRINT VALUE
But if I define a variable PI, which one takes precedence?

Code: Select all

PI = 7.1
LET VALUE=5*PI+3
PRINT VALUE
In ZX81 BASIC, PI is typed in as a token and only shown as PI so the ZX81 knows it is different. However, when parsing text, there has to be a precedence determination made. I would put the built-in value PI before the variable PI but that's just one example. Not sure if there are others out there on the ZX81.

I believe ZX81 built-infunctions don't use ( and ) for their arguments. On the QL though you do, right. What about a DIMensioned variable? Can they clashes with a user defined function and if so how is it determined which to choose? They both are used in a similar way, i.e. print a(1) could be a function call or a DIMension'ed variable, right?


User avatar
NormanDunbar
Forum Moderator
Posts: 2251
Joined: Tue Dec 14, 2010 9:04 am
Location: Leeds, West Yorkshire, UK
Contact:

Re: ZX81 BASIC

Post by NormanDunbar »

I would say that the ZX81 is preventing you from using variable names which match keywords/function names by dint of its tokenisation. Your PI variable, and the PI function can never be the same.

Parsers I've seen (and the couple I've written) give precedence to keywords when an identifier is lexed. If it matches a keyword or built in, like PI, it is assumed to be that, and not a variable name.

Of course, in the event that it should have been a variable name, you will get errors further down the line.

I had this very problem on Thursday with a bit of Oracle PL/SQL code I wrote. I used SQL as a variable name, but it's a keyword, and the parser really didn't like it at all. Once changed to sqlStmt, it parsed, and executed.

HTH

Cheers,
Norm.


Why do they put lightning conductors on churches?
Author of Arduino Software Internals
Author of Arduino Interrupts

No longer on Twitter, find me on https://mastodon.scot/@NormanDunbar.
User avatar
bwinkel67
QL Wafer Drive
Posts: 1187
Joined: Thu Oct 03, 2019 2:09 am

Re: ZX81 BASIC

Post by bwinkel67 »

Yes, I agree that keywords should take precedence. That's why I likely had the functions evaluated before variable names in my code. I think it would speed things up a little if I reversed it but then I could overwrite PI which might be interesting. I think that would be the only issue.

I am already not 100% compatible in my ZXSimulator since I had to make an exception with the ZX81's weird identifier scheme allowing: FOR I=A TO 10 TO A TO 10. Since the ZX81 tokenize the TO on coding input via the keyboard, this works perfectly and internally is parsed as: FOR I=ATO10 TO ATO10. I had to put a stop to it when I find the first token TO and so the above would cause and error as it is parsed as: FOR I=A TO 10 TOATO10 and so the trailing stuff (TOATO10) would be the syntax error.

Worse, I can come up with a scenario where it is not even a syntax error: FOR I=A TO B TO A TO B. This parses on mine to FOR I= A TO BTOATOB. Now I can check to see if BTOATOB is defined as a variable and then throw an error (the ZX81 looks for undefined variables) but right now I don't.


User avatar
1024MAK
Super Gold Card
Posts: 592
Joined: Sun Dec 11, 2011 1:16 am
Location: Looking forward to summer in Somerset, UK...

Re: ZX81 BASIC

Post by 1024MAK »

Go here for info on specBAS https://www.worldofspectrum.org/forums/ ... -available

Also there is help listed here http://www.zxspectrum4.net/forum/viewforum.php?f=6

A description of the most used emulator cassette file formats used for the ZX81 is given here http://problemkaputt.de/zxdocs.htm#zx80 ... fileimages

The AT command within a PRINT statement is alive and well in the ZX Spectrum version of BASIC. See https://www.worldofspectrum.org/ZXBasic ... hap15.html

PLOT on both the ZX81 and ZX Spectrum “prints” the smallest point available on that system that BASIC can handle. Hence on a ZX81 PLOT prints a character as the display under BASIC is a character mapped system. Whereas on the ZX Spectrum, it’s a pixel mapped system (except for the attributes like colour, flashing). See https://www.worldofspectrum.org/ZXBasic ... hap17.html

And http://www.worldofspectrum.org/ZX81Basi ... hap18.html

The editor on a ZX81 will let you type “FOR I=A TO 10 TO A TO 10” but will not accept the line when you press NEW LINE (enter), as the syntax checker rejects it.

Of course. TO being a key word is entered using the “single key entry” system (SHIFT + 4).

In most systems, key words, command names, function names etc. are treated as reserved words and cannot be used for variable names or function names. Because of the input system on the ZX81 and ZX Spectrum, this limitation does not apply. I can’t remember how it was handled for the ZX Spectrum 128 (and later machines) where the “single key entry” system was dropped in the full screen editor (this is a normal letter by letter editor).

Mark


:!: Standby alert :!:
“There are four lights!”
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb :!:
Looking forward to summer in Somerset later in the year :)

QL, Falcon, Atari 520STFM, Atari 1040STE, more PC's than I care to count and an assortment of 8 bit micros (Sinclair and Acorn)(nearly forgot the Psion's)
User avatar
bwinkel67
QL Wafer Drive
Posts: 1187
Joined: Thu Oct 03, 2019 2:09 am

Re: ZX81 BASIC

Post by bwinkel67 »

1024MAK wrote: The editor on a ZX81 will let you type “FOR I=A TO 10 TO A TO 10” but will not accept the line when you press NEW LINE (enter), as the syntax checker rejects it.
I just tried it again on the EightyOne emulator and it will accept it as proper syntax. If you don't define the variable at first you get a runtime error saying it's not defined but if you define it (like in the example below) it runs the loop once and prints out the value of the variable,
EightyOne.png
Thank you for all the valuable info in your reply. I looked at the Speccy keyboard a second time just now and finally found the AT keyword on the I key :-/


User avatar
bwinkel67
QL Wafer Drive
Posts: 1187
Joined: Thu Oct 03, 2019 2:09 am

Re: ZX81 BASIC

Post by bwinkel67 »

Wow, so ZX81 BASIC is just plain weird.

This on the QL works as advertised resulting in either 1 when true or 0 when false. However, on ZX81 we get this:

PRINT 2 AND 17 -> results in 2
PRINT 17 AND 2 -> results in 17
PRINT 0 AND 17 -> results in 0
PRINT 17 AND 0 -> results in 0
PRINT 0 AND 0 -> results in 0

So if it's false it prints 0 but if it's true it takes the first argument.


With OR, the ZX81 is even less consistent (works as it should on the QL):

PRINT 2 OR 17 -> results in 1
PRINT 17 OR 2 -> results in 1
PRINT 0 OR 17 -> results in 1
PRINT 17 OR 0 -> results in 17
PRINT 0 OR 0 -> results in 0

So, OR results in 1 for all cases of true unless the second argument is false, then instead of 1 it results in the first argument???

Mind numbingly insane.


User avatar
tofro
Font of All Knowledge
Posts: 2685
Joined: Sun Feb 13, 2011 10:53 pm
Location: SW Germany

Re: ZX81 BASIC

Post by tofro »

bwinkel67 wrote: Mind numbingly insane.
Not necessarily - The specific value returned for a logical expression is of no interest whatsoever. What you want to know is "Is it true or false" - And with the convention of "0 == false" and "<>0 = true", that's perfectly fine with presumably better performance.

Tobias


ʎɐqǝ ɯoɹɟ ǝq oʇ ƃuᴉoƃ ʇou sᴉ pɹɐoqʎǝʞ ʇxǝu ʎɯ 'ɹɐǝp ɥO
User avatar
bwinkel67
QL Wafer Drive
Posts: 1187
Joined: Thu Oct 03, 2019 2:09 am

Re: ZX81 BASIC

Post by bwinkel67 »

tofro wrote:
bwinkel67 wrote: Mind numbingly insane.
Not necessarily - The specific value returned for a logical expression is of no interest whatsoever. What you want to know is "Is it true or false" - And with the convention of "0 == false" and "<>0 = true", that's perfectly fine with presumably better performance.

Tobias
But can causes inaccurate results, forcing you to use ( and ). The ZXSimulator does it the way the QL and C/C++/Java/etc... does it and caused a program I loaded into it to fail :-/


User avatar
tofro
Font of All Knowledge
Posts: 2685
Joined: Sun Feb 13, 2011 10:53 pm
Location: SW Germany

Re: ZX81 BASIC

Post by tofro »

bwinkel67 wrote:
tofro wrote:
bwinkel67 wrote: Mind numbingly insane.
Not necessarily - The specific value returned for a logical expression is of no interest whatsoever. What you want to know is "Is it true or false" - And with the convention of "0 == false" and "<>0 = true", that's perfectly fine with presumably better performance.

Tobias
But can causes inaccurate results, forcing you to use ( and ). The ZXSimulator does it the way the QL and C/C++/Java/etc... does it and caused a program I loaded into it to fail :-/
That one I don't get. I can't think of a case atm where parenthesis could save an improperly written program that relies on the specific value of a boolean expression. Can you gve an example?


ʎɐqǝ ɯoɹɟ ǝq oʇ ƃuᴉoƃ ʇou sᴉ pɹɐoqʎǝʞ ʇxǝu ʎɯ 'ɹɐǝp ɥO
Post Reply