ZX Spectrum BASIC Compiler just released - Sinclair
This is a discussion on ZX Spectrum BASIC Compiler just released - Sinclair ; Well, just to recall: I've posted some questions about FP-CALC and
others for a BASIC compiler I've been working on.
Yesterday I released the "Release Candidate 3", which is (IMHO) very
estable and completely useful. If anybody is interested, please ...
-
ZX Spectrum BASIC Compiler just released
Well, just to recall: I've posted some questions about FP-CALC and
others for a BASIC compiler I've been working on.
Yesterday I released the "Release Candidate 3", which is (IMHO) very
estable and completely useful. If anybody is interested, please check
the compiler page at http://www.boriel.com/2008/05/05/zx-...asic-compiler/
Like Z88DK, its a cross-platform development kit, but uses BASIC
instead of C. It covers many of the ZX Spectrum BASIC construct (Line
numbers, GOTO/GOSUB, etc..) but can be also used in a modern BASIC
fashion (similar to Visual Basic Script or Free Basic). That is: You
can define functions and subroutines. Free Basic site contains an
extensive Language reference at http://www.freebasic.net/wiki which I
also tried to reach, but with some exceptions, since it is a Spectrum/
Z80/8bits oriented compiler. 
The SDK also has an assembler (zxbasm), which can be used standalone,
and a macro-preprocessor (zxbpp).
This tools are written in python, so you will need either to install
the python interpreter in your system (Linux / Windows / Mac) or use
the .MSI (windows only) version which does not requires the python
environment installed.
-
Re: ZX Spectrum BASIC Compiler just released
Boriel ha scritto:
> Well, just to recall: I've posted some questions about FP-CALC and
> others for a BASIC compiler I've been working on.
>
> Yesterday I released the "Release Candidate 3", which is (IMHO) very
> estable and completely useful. If anybody is interested, please check
> the compiler page at http://www.boriel.com/2008/05/05/zx-...asic-compiler/
(first post in this NG by me)
Hm.... I'm wrong or there is only a rc4 zipfile ?
Best regards from Italy,
Dott. Piergiorgio M. d' Errico.
-
Re: ZX Spectrum BASIC Compiler just released
Some issues for you! Under Winxp I did a couple of quick tests.
1 - How do you implement the pause command? Comes up with an error
when you you use 'pause 0' or 'pause 1'
2 this program wont work - halts after the first line is drawn - works
ok in Basic though.
1 CLS
2 FOR c=0 TO 5
3 FOR b=0 TO 255
4 PLOT b,c
5 NEXT b
6 NEXT c
3 a few sample programs and a readme on use would come in handy too.
I like the way you can produce a .asm file too - its nice to have
alook at whats happening (though I havent looked into the above code!)
Dicky
-
Re: ZX Spectrum BASIC Compiler just released
Boriel wrote:
> Yesterday I released the "Release Candidate 3", which is (IMHO) very
> estable and completely useful. If anybody is interested, please check
> the compiler page at
> http://www.boriel.com/2008/05/05/zx-...asic-compiler/
Very nice idea. I look forward to checking it out.
-
Re: ZX Spectrum BASIC Compiler just released
On 9 jun, 13:49, dickydodds wrote:
> Some issues for you! Under Winxp I did a couple of quick tests.
>
> 1 - How do you implement the pause command? Comes up with an error
> when you you use 'pause 0' or 'pause 1'
>
> 2 this program wont work - halts after the first line is drawn - works
> ok in Basic though.
>
> 1 CLS
> 2 FOR c=0 TO 5
> 3 FOR b=0 TO 255
> 4 PLOT b,c
> 5 NEXT b
> 6 NEXT c
>
> 3 a few sample programs and a readme on use would come in handy too.
>
> I like the way you can produce a .asm file too - its nice to have
> alook at whats happening (though I havent looked into the above code!)
>
> Dicky
There are examples in the examples/ directory (download the .zip
file).
There's also a Wiki in progress. Check at: http://www.boriel.com/wiki/en/index.php/ZXBasic
The pause command is not implemented as a command, but as a function
(I decided it for a very long-to-explain reason).
Just use:
#include once
....
And then, in your program:
pause(xxxx)
Note: By now, you must write it in lowercase (I will allow it case-
insensitive in future releases). Also put the parenthesis.
-
Re: ZX Spectrum BASIC Compiler just released
On 9 jun, 13:49, dickydodds wrote:
> Some issues for you! Under Winxp I did a couple of quick tests.
> 2 this program wont work - halts after the first line is drawn - works
> ok in Basic though.
>
> 1 CLS
> 2 FOR c=0 TO 5
> 3 FOR b=0 TO 255
> 4 PLOT b,c
> 5 NEXT b
> 6 NEXT c
> Dicky
This example was also discuses in speccy.org. The problem here is that
this is *compiled* basic. Default type is not floating point, but is
chosen in a smart way. Since the compiler see you uses b in the range
[0..255] it uses a byte. But a byte is not enough for a 0 to 255 loop!
(It will stop when b > 255, but this will never happen because it will
restart to 0). To fix it, DECLARE the b variable (most of compilers
requires this).
1 CLS: DIM b as Uinteger
Uintegers are 16 bit unsigned integers. Types are:
Byte, UByte, Integer, Uinteger, Long, ULong, Fixed and Float.
-
Re: ZX Spectrum BASIC Compiler just released
Boriel wrote:
> There are examples in the examples/ directory (download the .zip
> file).
> There's also a Wiki in progress. Check at: http://www.boriel.com/wiki/en/index.php/ZXBasic
>
> The pause command is not implemented as a command, but as a function
> (I decided it for a very long-to-explain reason).
Please explain.
After all, you are attempting a SINCLAIR basic compiler so the first aim
should be to make it as sinclair compatible as possible before implementing
new features.
PAUSE is a command, not a function. In fact, what possible return value
could you get from a PAUSE *function*? Whether or not it completed its time
in pause before a key was pressed?
Note the difference between a command and a function. A command performs a
task (PRINT, PLOT, PAUSE), a function returns a value (SGN, SIN, INKEY$).
--
| spike1@freenet.co.uk | Windows95 (noun): 32 bit extensions and a |
| | graphical shell for a 16 bit patch to an 8 bit |
| Andrew Halliwell BSc | operating system originally coded for a 4 bit |
| in |microprocessor, written by a 2 bit company, that|
| Computer Science | can't stand 1 bit of competition. |
-
Re: ZX Spectrum BASIC Compiler just released
Boriel wrote:
> This example was also discuses in speccy.org. The problem here is that
> this is *compiled* basic. Default type is not floating point, but is
> chosen in a smart way. Since the compiler see you uses b in the range
> [0..255] it uses a byte. But a byte is not enough for a 0 to 255 loop!
> (It will stop when b > 255, but this will never happen because it will
> restart to 0). To fix it, DECLARE the b variable (most of compilers
> requires this).
>
> 1 CLS: DIM b as Uinteger
>
> Uintegers are 16 bit unsigned integers. Types are:
> Byte, UByte, Integer, Uinteger, Long, ULong, Fixed and Float.
Might it not be more sensible to assume all numbers are Float unless
declared otherwise, just as sinclair basic itself does?
It'll make compiling basic programs without having to modify them possible.
And there's a whole library of pre-written basic code out there.
--
| |What to do if you find yourself stuck in a crack|
| spike1@freenet.co.uk |in the ground beneath a giant boulder, which you|
| |can't move, with no hope of rescue. |
| Andrew Halliwell BSc |Consider how lucky you are that life has been |
| in |good to you so far... |
| Computer Science | -The BOOK, Hitch-hiker's guide to the galaxy.|
-
Re: ZX Spectrum BASIC Compiler just released
On Jun 12, 9:51*pm, Andrew Halliwell wrote:
> Boriel wrote:
> > There are examples in the examples/ directory (download the .zip
> > file).
> > There's also a Wiki in progress. Check at:http://www.boriel.com/wiki/en/index.php/ZXBasic
>
> > The pause command is not implemented as a command, but as a function
> > (I decided it for a very long-to-explain reason).
>
> Please explain.
> After all, you are attempting a SINCLAIR basic compiler so the first aim
> should be to make it as sinclair compatible as possible before implementing
> new features.
>
> PAUSE is a command, not a function. In fact, what possible return value
> could you get from a PAUSE *function*? Whether or not it completed its time
> in pause before a key was pressed?
Sorry. My mistake. PAUSE is a SUBroutine (read pause.bas file in /
library subdirectory).
The problem with PAUSE is that is too Sinclair Related. And this
compiler aims to be not a strict Sinclair BASIC compiler. This is
almost impossible: Many instructions like READ/DATA can only be
implemented in an interpreter which is what the ROM already does. This
is not an interpreter, but a compiler. In fact, PRINT AT Y, X should
be "LOCATE Y, X". But I finally leave the Sinclair version.
The compiler aims to be a Sinclair first, Z80 later, and generic
finally (.NET, Java, .exe, PS3 or WII) compiler (it can be done).
PRINT, PLOT are more extended (every computer has a screen). Since it
is compiled, some things have been enhanced and it tries to get as
closer as possible to FreeBasic (http://www.freebasic.net/wiki/).
However, I tried to close to Sinclair BASIC as much as possible. But
some thinks are unavoidable. E.g. Every IF must be closed with END IF.
If people are really interested, I could include PAUSE as a sentence
(I was thinking of making the compiler extensible by the user, but
it's hard to make it easy to others, by the way).
> Note the difference between a command and a function. A command performs a
> task (PRINT, PLOT, PAUSE), a function returns a value (SGN, SIN, INKEY$).
> --
> | * spi...@freenet.co.uk * | * Windows95 (noun): 32 bit extensions and a * *|
> | * * * * * * * * * * * * *| graphical shell fora 16 bit patch to an 8 bit |
> | * Andrew Halliwell BSc * | operating system originally *coded for a 4 bit |
> | * * * * * *in * * * * * *|microprocessor, written by a 2 bit company, that|
> | * * Computer Science * * | * * * *can't stand 1 bit of competition. * * * |
-
Re: ZX Spectrum BASIC Compiler just released
On Jun 12, 9:54*pm, Andrew Halliwell wrote:
> Boriel wrote:
> > This example was also discuses in speccy.org. The problem here is that
> > this is *compiled* basic. Default type is not floating point, but is
> > chosen in a smart way. Since the compiler see you uses b in the range
> > [0..255] it uses a byte. But a byte is not enough for a 0 to 255 loop!
> > (It will stop when b > 255, but this will never happen because it will
> > restart to 0). To fix it, DECLARE the b variable (most of compilers
> > requires this).
>
> > 1 CLS: DIM b as Uinteger
>
> > Uintegers are 16 bit unsigned integers. Types are:
> > Byte, UByte, Integer, Uinteger, Long, ULong, Fixed and Float.
>
> Might it not be more sensible to assume all numbers are Float unless
> declared otherwise, just as sinclair basic itself does?
Yes, that was at fist, but resulted too inefficient (faster than
BASIC, of course). Anyway, I will put a command line parameter to
allow this in brief. Something
like:
zxb.py --default-type=float ...
> It'll make compiling basic programs without having to modify them possible..
> And there's a whole library of pre-written basic code out there.
Please, tell me where to get that library. It would be a very good
test bech.
Regards,
J.
-
Re: ZX Spectrum BASIC Compiler just released
> Yes, that was at fist, but resulted too inefficient (faster than
Hmm. My replies are riddled with typos and I'm not a native English
speaker.
Sorry, but forums like usenet do not allow re-editing posts, so I
can't fix them up.
My apologies.
Regards,
J.
-
Re: ZX Spectrum BASIC Compiler just released
Boriel wrote:
> The problem with PAUSE is that is too Sinclair Related. And this
> compiler aims to be not a strict Sinclair BASIC compiler. This is
> almost impossible: Many instructions like READ/DATA can only be
> implemented in an interpreter which is what the ROM already does.
Why is read/data/restore such a problem for a compiler?
Seems quite straightforward.
Data defines an area of memory from which values are read.
Read reads that data
Restore sets a pointer to that data based on the line number it was stored
in.
A simple memory structure should be able to handle that.
Something like "Data block start marker incorporating basic line number"
"Data type indicator" "data value" "data type indicator" "data value" ...
"data block end marker"
Then read would work properly.
The compiler couid have a "current data pointer" (which restore would reset
to the beginning of a given line)
And read/data/restore are three commands that MOST 1980s BASICS had.
This
> is not an interpreter, but a compiler. In fact, PRINT AT Y, X should
> be "LOCATE Y, X". But I finally leave the Sinclair version.
>
> The compiler aims to be a Sinclair first, Z80 later, and generic
> finally (.NET, Java, .exe, PS3 or WII) compiler (it can be done).
> PRINT, PLOT are more extended (every computer has a screen).
Err. Not every computer...

> Since it
> is compiled, some things have been enhanced and it tries to get as
> closer as possible to FreeBasic (http://www.freebasic.net/wiki/).
Old style compilers got around enhancements by using REM statements to hold
some commmands... Might be an idea.
--
| spike1@freenet.co.uk | Windows95 (noun): 32 bit extensions and a |
| | graphical shell for a 16 bit patch to an 8 bit |
| Andrew Halliwell BSc | operating system originally coded for a 4 bit |
| in |microprocessor, written by a 2 bit company, that|
| Computer Science | can't stand 1 bit of competition. |
-
Re: ZX Spectrum BASIC Compiler just released
Boriel wrote:
>> It'll make compiling basic programs without having to modify them possible.
>> And there's a whole library of pre-written basic code out there.
> Please, tell me where to get that library. It would be a very good
> test bech.
Well, there's WOS, quite a few type-ins are on there...
and you could take a look at the CSSCGC (most of the crap games on there
were written in basic. Some have machine code embedded, most don't)
--
| spike1@freenet.co.uk | Windows95 (noun): 32 bit extensions and a |
| | graphical shell for a 16 bit patch to an 8 bit |
| Andrew Halliwell BSc | operating system originally coded for a 4 bit |
| in |microprocessor, written by a 2 bit company, that|
| Computer Science | can't stand 1 bit of competition. |
-
Re: ZX Spectrum BASIC Compiler just released
On Fri, 13 Jun 2008 09:42:16 +0100, Andrew Halliwell
wrote:
>Boriel wrote:
>> The problem with PAUSE is that is too Sinclair Related. And this
>> compiler aims to be not a strict Sinclair BASIC compiler. This is
>> almost impossible: Many instructions like READ/DATA can only be
>> implemented in an interpreter which is what the ROM already does.
>
>Why is read/data/restore such a problem for a compiler?
Indeed. I wrote an Amiga-based compiler years ago that handled
READ/DATA in the same way as Speccy BASIC works.
-
Re: ZX Spectrum BASIC Compiler just released
OK, I have some more issues - sorry! I ask here as I cant post on wos
at the moment.
1 - Can you confirm if the files must be unix format? I seem to be
having issues with this. If it is, then it is not possible to use the
Freebasic compilers - listings produced by fbedit dont compile under
this compiler, nor is it possible to copy and paste in windows. as you
get all sorts of compiler errors.
I can only get code to work if its typed into notepad by hand.
I think this is a real issue - in which case, can the CRLF's in the
file be stripped out or something? I have even tried using ultraedit
in unix mode but that deosnt always work either - its driving me
NUTS!!!
Dicky
-
Re: ZX Spectrum BASIC Compiler just released
dickydodds wrote:
> I think this is a real issue - in which case, can the CRLF's in the
> file be stripped out or something? I have even tried using ultraedit
> in unix mode but that deosnt always work either - its driving me
> NUTS!!!
In unix there're tools to convert files from windows or mac to unix format.
dos2unix is one of them.
I imagine the same programs will have ports to windows too.
--
| spike1@freenet.co.uk | Windows95 (noun): 32 bit extensions and a |
| | graphical shell for a 16 bit patch to an 8 bit |
| Andrew Halliwell BSc | operating system originally coded for a 4 bit |
| in |microprocessor, written by a 2 bit company, that|
| Computer Science | can't stand 1 bit of competition. |
-
Re: ZX Spectrum BASIC Compiler just released
Andrew Halliwell wrote:
> dickydodds wrote:
>> I think this is a real issue - in which case, can the CRLF's in the
>> file be stripped out or something? I have even tried using ultraedit
>> in unix mode but that deosnt always work either - its driving me
>> NUTS!!!
>
> In unix there're tools to convert files from windows or mac to unix format.
> dos2unix is one of them.
> I imagine the same programs will have ports to windows too.
>
I use notepad++ for all my text editing needs in windows (it really is
rather good) and it can convert to unix format from dos format and vice
versa
-
Re: ZX Spectrum BASIC Compiler just released
> I use notepad++ for all my text editing needs in windows (it really is
> rather good) and it can convert to unix format from dos format and vice
> versa
Yes, it is good - now I can see end of line chars. However, I still
have a problem (which I thought was due to the EOL chars)
The clock 2 example .bas has a dual while loop and so deos a bit of
gwbasic code I got off the net - looking to convert to the spectrum -
just as a test of the compiler - but everytime I get an error : -
test.bas:15: Syntax Error. Unexpected token 'END'
Traceback (most recent call last):
File "zxb.py", line 215, in
File "zxb.py", line 174, in main
File "ply\yacc.pyc", line 314, in parse
KeyError: 'expr'
Here is the code - slightly hacked by me before giving up!
10 Dim r, rec, re, m As float
im Left, j,recen, imcen As Integer
60 LEFT = 150 : TOP = 380 : W = 360 : M = .833
70 R = 2.64 : S = 2 * R / W
80 RECEN = 0 : IMCEN = 0
90 CLS
100 FOR Y = 0 TO W
110 FOR X = 0 TO W
120 REC = S * (X - W / 2) + RECEN : IMC = S * (Y - W / 2) + IMCEN
130 RE = REC : IM = IMC
140 RE2 = RE * RE : IM2 = IM * IM : J = 0
150 WHILE (RE2 + IM2 <= 256) AND (J < 15)
160 IM = 2 * RE * IM + IMC
170 RE = RE2 - IM2 + REC
180 RE2 = RE * RE : IM2 = IM * IM : J = J + 1
190 end while
200 IF J < 3 THEN GOTO 270
210 IF J >= 3 AND J < 6 THEN Color 0, 4 : REM YELLOW
230 IF J >= 6 AND J < 9 THEN Color 0, 1 : REM BLUE
240 IF J >= 9 AND J < 12 THEN Color 0,2 : REM GREEN
250 IF J >= 12 AND J < 15 THEN Color 0, 7 : REM WHITE
260 IF J >= 15 THEN ink 2 : REM RED
270 PSET (X + LEFT, (TOP - Y)*M)
270 NEXT X
280 NEXT Y
290 color0,7 : REM WHITE
300 LINE (LEFT, (TOP - W / 2) * M) - (W + LEFT, (TOP - W / 2) * M)
310 LINE (W / 2 + LEFT, (TOP - W) * M) - (W / 2 + LEFT, TOP * M)
The offending line is 190 - it deosnt matter that its lower case. I am
not stupid but it all looks fine to me, so, why deosnt it work? I
started declaring vars in the first line to see if that made a
difference to no avail. Even if I remove the lines 160, 170, 180 to a
print statement and it fails - so is it the actual While Statement? (I
added the brackets to it as another option to compile it to no avail).
I am using RC6 version too.
I love the idea of this compiler, but, I cant get much to work so far
LOL!!
Dicky
-
Re: ZX Spectrum BASIC Compiler just released
> I think this is a real issue - in which case, can the CRLF's in the
> file be stripped out or something? I have even tried using ultraedit
> in unix mode but that deosnt always work either - its driving me
> NUTS!!!
>
> Dicky
It should understand both Unix and Dos formats (In fact I work in both
platforms). If you suspect there's an issue with it, please, tell me
which message error are you getting so I could fix it up in the
compiler.
On the other hand, you can also use Notepad++ which saves in both Dos
and Unix formats (as someone stated here).
-
Re: ZX Spectrum BASIC Compiler just released
On 13 jun, 16:09, dickydodds wrote:
> > I use notepad++ for all my text editing needs in windows (it really is
> > rather good) and it can convert to unix format from dos format and vice
> > versa
>
> Yes, it is good - now I can see end of line chars. However, I still
> have a problem (which I thought was due to the EOL chars)
>
> The clock 2 example .bas has a dual while loop and so deos a bit of
> gwbasic code I got off the net - looking to convert to the spectrum -
> just as a test of the compiler - but everytime I get an error : -
>
> test.bas:15: Syntax Error. Unexpected token 'END'
> Traceback (most recent call last):
> File "zxb.py", line 215, in
> File "zxb.py", line 174, in main
> File "ply\yacc.pyc", line 314, in parse
> KeyError: 'expr'
>
> Here is the code - slightly hacked by me before giving up!
>
> 10 Dim r, rec, re, m As float
im Left, j,recen, imcen As Integer
> 60 LEFT = 150 : TOP = 380 : W = 360 : M = .833
> 70 R = 2.64 : S = 2 * R / W
> 80 RECEN = 0 : IMCEN = 0
> 90 CLS
> 100 FOR Y = 0 TO W
> 110 FOR X = 0 TO W
> 120 REC = S * (X - W / 2) + RECEN : IMC = S * (Y - W / 2) + IMCEN
> 130 RE = REC : IM = IMC
> 140 RE2 = RE * RE : IM2 = IM * IM : J = 0
> 150 WHILE (RE2 + IM2 <= 256) AND (J < 15)
> 160 IM = 2 * RE * IM + IMC
> 170 RE = RE2 - IM2 + REC
> 180 RE2 = RE * RE : IM2 = IM * IM : J = J + 1
> 190 end while
> 200 IF J < 3 THEN GOTO 270
> 210 IF J >= 3 AND J < 6 THEN Color 0, 4 : REM YELLOW
> 230 IF J >= 6 AND J < 9 THEN Color 0, 1 : REM BLUE
> 240 IF J >= 9 AND J < 12 THEN Color 0,2 : REM GREEN
> 250 IF J >= 12 AND J < 15 THEN Color 0, 7 : REM WHITE
> 260 IF J >= 15 THEN ink 2 : REM RED
> 270 PSET (X + LEFT, (TOP - Y)*M)
> 270 NEXT X
> 280 NEXT Y
> 290 color0,7 : REM WHITE
> 300 LINE (LEFT, (TOP - W / 2) * M) - (W + LEFT, (TOP - W / 2) * M)
> 310 LINE (W / 2 + LEFT, (TOP - W) * M) - (W / 2 + LEFT, TOP * M)
>
> The offending line is 190 - it deosnt matter that its lower case. I am
> not stupid but it all looks fine to me, so, why deosnt it work? I
....
As stated above, Every IF must be closed with END IF.
This compiler is NOT line oriented.
On the other hand I see some sentences that are not Sinclair BASIC:
Color x,y -> Use PAPER x: INK y (or whatever)
PSET (X, y): Use PLOT x, y
LINE X, Y: Use DRAW x, y.
Tray this and tell me.
On the other hand, when the compiler reports a syntax error, it's
related to the text file number, no to the basic line numbers.
BASIC line numbers ARE ignored and completely useless here (you can
ommit them). They're only labels useful for GOTO or GOSUB. You can
also define text labels, with a colon:
mylabel:
goto mylabel
See http://boriel.com/wiki/en/index.php/ZxBasic:Labels
Regards,
B.