tinyPascal in BASIC

Talk about your programs in progress. Discuss how to implement features, etc.
Forum rules
This section is for testing Commander X16 programs and programs related to the CX16 for other platforms (compilers, data conversion tools, etc.)

Feel free to post works in progress, test builds, prototypes, and tech demos.

Finished works go in the Downloads category. Don't forget to add a hashtag (#) and the version number your program was meant to run on. (ie: #R41).
Martin Schmalenbach
Posts: 125
Joined: Tue Jul 21, 2020 10:08 pm

Re: tinyPascal in BASIC

Post by Martin Schmalenbach »

In the interests of fun & exploration, I'm posting here the current working versions of the Tiny Pascal compiler and pCode interpreter.

NONE of the X16 functions are implemented, and there is no opportunity for the moment to manipulate memory directly.

I'll also post the original example test program that was part of the original 3 part series of articles in BYTE magazine starting September 1978.

For the compiler, when it asks for the RAM Bank - for now leave it at the default, so just press RETURN.
Ditto when it asks for the pCode starting address.

This means the pCode will be written to memory starting at address $A000 in RAM Bank 10 on the X16 and emulator.

When asked if you wish the pCodes displayed, answering Y means that as the compiler runs and compiles, it will print out the compiled pCode for each line it processes, and print out that line from the source.

Source code is expected to be a simple text file. Give it a file name - ideally locate it in the same folder as the compiler.

And then just sit back!

When compiled, the compiler will write the entirety of the RAM bank 10 from $A000 to $BFFF to disk using BSAVE.

It will have the same filename as the source code file BUT with ".OBJ" added on the end.

When you run the pCode interpreter, you will be asked for the pCode start address. For now, enter in 40960, and for the RAM bank stick with the default of 10.

Then give the full filename of the object pCode to run - including the ".OBJ" on the end.

It will then load that in to memory and examine and display the first pCode instruction.

You will be presented with the "INTERP>" prompt... pressing "?" and ENTER will display a list of all accepted commands. They are 1 character commands followed by the RETURN key. Any other info it needs it will ask for at the time.

Enjoy!!
TPINTV1.PRG
Tiny Pascal Interpreter
(7.65 KiB) Downloaded 1643 times
TPX16COMPV1.PRG
Tiny Pascal Compiler
(11.68 KiB) Downloaded 1573 times
Martin Schmalenbach
Posts: 125
Joined: Tue Jul 21, 2020 10:08 pm

Re: tinyPascal in BASIC

Post by Martin Schmalenbach »

Here's the test program I mentioned - for some reason I couldn't get it to upload here as a file.

Code: Select all

CONST CR=13; LF=10;
VAR A,B,C,D:INTEGER;
FUNC MAX4(X1,X2,X3,X4);
   FUNC MAX2(X1,X2);
      BEGIN
         IF X1>X2 THEN MAX2:=X1
         ELSE MAX2:=X2
         END;
   BEGIN
      MAX4:=MAX2(MAX2(X1,X2),MAX2(X3,X4))
      END;

BEGIN
   REPEAT
      READ(A#,B#,C#,D#);
      WRITE('THE LARGEST IS ',MAX4(A,B,C,D)#,CR,LF)
      UNTIL A<0
   END.

Martin Schmalenbach
Posts: 125
Joined: Tue Jul 21, 2020 10:08 pm

Re: tinyPascal in BASIC

Post by Martin Schmalenbach »

I couldn't post the screencast here, so I've uploaded it and all other relevant files for your enjoyment on my google drive.

https://drive.google.com/drive/folders/ ... sp=sharing

Cheers

Martin
Martin Schmalenbach
Posts: 125
Joined: Tue Jul 21, 2020 10:08 pm

Re: tinyPascal in BASIC

Post by Martin Schmalenbach »

KNOWN ISSUES...
New as at 15:39 HRS MST 19NOV2023:
  • For now the source pascal file needs to have at least 1 line of empty or any text after the "END." statement of the Tiny Pascal program.
+++ END
voidstar
Posts: 359
Joined: Thu Apr 15, 2021 8:05 am

Re: tinyPascal in BASIC

Post by voidstar »

This is incredible! :shock:

You've compiled an actually-useful Pascal program, all while being "on system."

Features: calling a user defined function, including sub-functions, passing arguments, typed variable declarations, and those functions can return values.
(one question: the function arguments "carry the passed type" or for now are integer by default?)

And it is dusting off an approach from 1978 of using BASIC to implement this high-level language!

This is a great starting point that captures the original BYTE article. In 1978, microcomputers were still "mysterious" on what they could even be used for - you had the soaring mainframes, meanwhile pocket calculators crawled up and became desktop "appliance computers." So "historical projects" like this capture that moment of progress.

Turbo Pascal 1.0 for the PC came out in 1983.
There was an Apple Pascal in 1979, but it also needed its own OS.

"bootstrapping" a higher level language out of BASIC didn't really catch on, maybe because it remained difficult to construct programs larger than BASIC itself? You have to read an input file, and the buffer of that has to go somewhere. Then your generated "object code" (be it a tokenized intermediate, assembler, or actual opcodes) has to go somewhere, while your compiler code also has to go somewhere. Balancing all that on systems with under 32KB system RAM, and *maybe* a 160KB single sided disk drive, chewing on bytes at barely 1MHz :) Or maybe it didn't catch on since BASIC line numbers just has a complexity limit where you just get entangled, and now BASLOAD helps solve that.

But separate from those technical constraints, this project I think is a good early-look at how to start a high level language and the concepts involved, going through the full gamut involved for making executable software.

Thanks for taking this project up!! Would love to see a LONG or BYTE type, RECORD, ARRAY, maybe UNITS support, and even a form of inline assembly? FLOAT is nice, but to me a lower priority (just since I try to scale or solve things in a way that don't really need floating point).
Attachments
IMG_2510.JPG
IMG_2510.JPG (1.41 MiB) Viewed 256751 times
IMG_2507.JPG
IMG_2507.JPG (1.3 MiB) Viewed 256751 times
Last edited by voidstar on Tue Nov 21, 2023 4:46 am, edited 1 time in total.
Martin Schmalenbach
Posts: 125
Joined: Tue Jul 21, 2020 10:08 pm

Re: tinyPascal in BASIC

Post by Martin Schmalenbach »

voidstar wrote: Tue Nov 21, 2023 3:05 am This is incredible! :shock:
Thanks - all I've done is take the work from the original authors Kin-Man Chung and Herbert Yuen from all those years ago and get it to run on the X16!

My interest in languages and compilers goes back a long way, and got more involved when I came across Jack Cranshaw's excellent "Let's Build A Compiler!" series from 1988 to 1995 or so. A Pascal-like language built from the ground up... in Pascal as it happens, and generating 68K assembly language. I took it from there to compiling on a PIC24/dsPIC33 microcontroller and added basic optimizations.
voidstar wrote: Tue Nov 21, 2023 3:05 amYou've compiled an actually-useful Pascal program, all while being "on system."

Features: calling a user defined function, including sub-functions, passing arguments, typed variable declarations, and those functions can return values.
(one question: the function arguments "carry the passed type" or for now are integer by default?)

And it is dusting off an approach from 1978 of using BASIC to implement this high-level language!

This is a great starting point that captures the original BYTE article. In 1978, microcomputers were still "mysterious" on what they could even be used for - you had the soaring mainframes, meanwhile pocket calculators crawled up and became desktop "appliance computers."

Turbo Pascal 1.0 for the PC came out in 1983.
There was an Apple Pascal in 1979, but it also needed its own OS.

"bootstrapping" a higher level language out of BASIC didn't really catch on, maybe because it remained difficult to construct programs larger than BASIC itself? You have to read an input file, and the buffer of that has to go somewhere. Then your generated "object code" (be it a tokenized intermediate, assembler, or actual opcodes) has to go somewhere, while your compiler code also has to go somewhere. Balancing all that on systems with under 32KB system RAM, and *maybe* a 160KB single sided disk drive, chewing on bytes at barely 1MHz :) Or maybe it didn't catch on since BASIC line numbers just has a complexity limit where you just get entangled, and now BASLOAD helps solve that.

But separate from those technical constraints, this project I think is a good early-look at how to start a high level language and the concepts involved, going through the full gamut involved for making executable software.
Yep - lots of good stuff in it and those articles. I found it a tad tricky to start off with, trying to follow the compiler's logic in an unstructured language... it helped that I'd had previous exposure to compilers etc.

It's actually put together quite well, all things considered.

But there are limits to complexity - well, I suspect mostly to patience. Taking it over to the BASLOAD environment helps hugely.
voidstar wrote: Tue Nov 21, 2023 3:05 amThanks for taking this project up!! Would love to see a LONG or BYTE type, RECORD, ARRAY, maybe UNITS support, and even a form of inline assembly? FLOAT is nice, but to me a lower priority (just since I try to scale or solve things in a way that don't really need floating point).
I'm planning LONG & BYTE. At the moment it only does INTEGERS, signed 16 bit. And single dimension arrays of the same. Characters are done via integers too! BYTE will be easy as it fits nicely in to the pCode instruction's 32 bits - each pCode is 4 bytes, with the first byte indicating the main opcode, the second byte indicating either a subset of the main opcode, or an index/hierarchy level to handle sub functions/sub procedures and accessing a parent/grandparent etc's local variables or parameters. It's this which makes having localized sub functions and sub procedures very easy to implement - just like many other 'serious' compiled languages do & their implementations do.

A high priority for me at the moment is adding some form of PEEK/POKE and SYS/USR capability so that the entirety of the machine is opened up, albeit it in a crude way for now. I'll likely do DEEK/DOKE and QUEEK/QUOKE for double (16 bit) and Lon (32 bit) values to set/read.

LONG will likely need to make use of a double pCode - 2 pCode instructions, or 64 bits... which at first glance makes for a complex stack-handling process, though previous experience with other compilers suggests this may not be as awkward/complex as it first seems.

I'll want to add pointers too, to help with string handling as well as improved array handling and so on. User defined SCALAR types in Pascal terms, like ENUMERATED and SUBRANGE should be quite straightforward to do, not sure sure though about the very useful 'IN' operator.

And I want to provide good access to the full 2 or 3.5Mb of address space of the X16, especially if we're to have RECORDs, which I'd love to implement too. And sets...

You asked about the INTEGERs in the parameter definitions - it's as you infer - because INTEGER is the only type. Bringing additional types on board is a little more challenging than it first looks due to questions of type strength and type casting. Especially in parameter passing and function value returns.

At the moment they are all 'pass by value' parameters - I'm toying with the idea of 'pass-by-reference' capability as well - one more reason for having a good pointer capability within the guts of it all!!

I am very open to the very useful UNIT capability, but I've not given that sufficient thought just yet. I do know though that I'll be making heavy use of the banked RAM, and wherever possible, the BASIC & KERNAL ROMs wherever it makes sense to!
Last edited by Martin Schmalenbach on Tue Nov 21, 2023 3:44 am, edited 1 time in total.
Martin Schmalenbach
Posts: 125
Joined: Tue Jul 21, 2020 10:08 pm

Re: tinyPascal in BASIC

Post by Martin Schmalenbach »

BTW - those screen shots - are they of the Tiny Pascal compiler/interpreter running on actual hardware, or the emulator?
Martin Schmalenbach
Posts: 125
Joined: Tue Jul 21, 2020 10:08 pm

Re: tinyPascal in BASIC

Post by Martin Schmalenbach »

Looking further in to the UNITS feature... that should be fairly straight forward to do... and perhaps make the task of dead-code removal as a compilation optimization somewhat easy to implement 'well enough'...

Ditto with file handling... once I've got strings and/or arrays of chars implemented...
voidstar
Posts: 359
Joined: Thu Apr 15, 2021 8:05 am

Re: tinyPascal in BASIC

Post by voidstar »

Yes, the images are from running on hardware!
Martin Schmalenbach
Posts: 125
Joined: Tue Jul 21, 2020 10:08 pm

Re: tinyPascal in BASIC

Post by Martin Schmalenbach »

voidstar wrote: Tue Nov 21, 2023 4:22 am Yes, the images are from running on hardware!
Very excellent!
Post Reply