Page 2 of 4

Re: Tiny Pascal Compiler - V1 NOW AVAILABLE!

Posted: Fri Nov 24, 2023 8:42 pm
by yock1960
Well, interesting and perhaps even more interesting is perusing the old Byte issues, pre-dating my entry into this 'universe'!

I'm not much of a Pascal guy, although I have dabbled in it over the years. In digging into this just a bit, I see it really doesn't like comments and I've found one syntax error; line 553, looks like something didn't resolve/convert correctly from BASLOAD, which I haven't looked at, at all.

I actually found the code or version of the code from Byte, over on Sourceforge and tried to compile the Pascal version of the interpreter, which is how I found the syntax error. After removing all of the comments (and correcting the syntax error, with Sourceforge help), it goes on a bit, but eventually gives an illegal factor error, which I can't figure out.

I'll keep an eye on this, if nothing else, I like to dabble!

Re: Tiny Pascal Compiler - V1 NOW AVAILABLE!

Posted: Sat Nov 25, 2023 8:32 pm
by Martin Schmalenbach
yock1960 wrote: Fri Nov 24, 2023 8:42 pm Well, interesting and perhaps even more interesting is perusing the old Byte issues, pre-dating my entry into this 'universe'!

I'm not much of a Pascal guy, although I have dabbled in it over the years. In digging into this just a bit, I see it really doesn't like comments and I've found one syntax error; line 553, looks like something didn't resolve/convert correctly from BASLOAD, which I haven't looked at, at all.

I actually found the code or version of the code from Byte, over on Sourceforge and tried to compile the Pascal version of the interpreter, which is how I found the syntax error. After removing all of the comments (and correcting the syntax error, with Sourceforge help), it goes on a bit, but eventually gives an illegal factor error, which I can't figure out.

I'll keep an eye on this, if nothing else, I like to dabble!
Thanks for the commentary and also for the bug. I found that myself probably around the same time you did, as I was trying to get extended type declarations working. Not a BASLOAD problem but pilot error - me being the pilot! It's fixed and that will come through in the next rev.

I'm having to rewrite the entirety of the variable declaration code to get additional variable types (and function types) implemented. Because the original assumes a single data type, INTEGER, the original authors could simplify their code and the logic... I have to replace all that - I tried to stick with as much of the original code as possible, but it just started getting too convoluted - usually a sign that the path I'm on is a crap one, either functionally or from a maintenance perspective!

I anticipate having the new code up and running by the end of this weekend as far as the compiler is concerned. The pCode interpreter will need updating to handle the additional types, which shouldn't be too difficult now... it's actually a little clearer what is going on BECAUSE I'm not able to make assumptions etc.

I'm planning to add BYTE, CHAR, STRING, LONG (ints) and probably a bit later on, FLOAT.

For now arrays will remain as single dimension, but I do plan to have them handle multiple dimension arrays fairly soon, just not in the next rev.

Continue to dabble away!

Cheers

Martin

Re: Tiny Pascal Compiler - V1 NOW AVAILABLE!

Posted: Sun Nov 26, 2023 2:35 am
by yock1960
Martin Schmalenbach wrote: Sat Nov 25, 2023 8:32 pm

Thanks for the commentary and also for the bug. I found that myself probably around the same time you did, as I was trying to get extended type declarations working. Not a BASLOAD problem but pilot error - me being the pilot! It's fixed and that will come through in the next rev.

I'm having to rewrite the entirety of the variable declaration code to get additional variable types (and function types) implemented. Because the original assumes a single data type, INTEGER, the original authors could simplify their code and the logic... I have to replace all that - I tried to stick with as much of the original code as possible, but it just started getting too convoluted - usually a sign that the path I'm on is a crap one, either functionally or from a maintenance perspective!

I anticipate having the new code up and running by the end of this weekend as far as the compiler is concerned. The pCode interpreter will need updating to handle the additional types, which shouldn't be too difficult now... it's actually a little clearer what is going on BECAUSE I'm not able to make assumptions etc.

I'm planning to add BYTE, CHAR, STRING, LONG (ints) and probably a bit later on, FLOAT.

For now arrays will remain as single dimension, but I do plan to have them handle multiple dimension arrays fairly soon, just not in the next rev.

Continue to dabble away!

Cheers

Martin
I admire your fortitude! No one will ever call my code, neat, tidy or self explanatory...but that code is a whole heaping bowl of spaghetti! :shock:

I did run across a commented version...which is helpful, but it's still a jumbled mess. It would drive me nuts trying to follow it.

I remember being unimpressed with p-code compilers back in the day, but now this has gotten me interested, I guess from an educational perspective.

Good Luck!

Re: Tiny Pascal Compiler - V1 NOW AVAILABLE!

Posted: Sun Nov 26, 2023 7:55 pm
by Martin Schmalenbach
yock1960 wrote: Sun Nov 26, 2023 2:35 am I admire your fortitude! No one will ever call my code, neat, tidy or self explanatory...but that code is a whole heaping bowl of spaghetti! :shock:

I did run across a commented version...which is helpful, but it's still a jumbled mess. It would drive me nuts trying to follow it.

I remember being unimpressed with p-code compilers back in the day, but now this has gotten me interested, I guess from an educational perspective.

Good Luck!
My interest is also somewhat educational. In a sense any CPU that uses microcode is running a pCode system... just at a very low level!

I do believe some enterprising soul developed a CPU that would run the Pascal p-System pCode directly. The pCode for the Tiny Pascal compiler is extremely simple. I came across a pCode system for a larger machine that had I think 164 different instructions/opCodes, including floating point handling, string manipulation and a whole bunch of other eye candy. Interesting, in a macabre sort of way!

The code for this compiler IS a whole heap of spaghetti - I'm able to follow it reasonably well & quickly because I've written a number of Pascal-like compilers before, including one targeted at the PIC24/dsPIC33 series of microcontrollers from Microchip Technology Inc. That was fun! Code generation was not bad, and many of the more ubiquitous optimizations were part of that compiler - THAT was actually the more interesting aspect... getting it to generate a correct Abstract Syntax Tree (easy - once you know how!!) and then using that to do the optimizations and then finally generate code. Mind you, it helps to have a target machine like the PIC24/dsPIC33, which looks like a 16-bit version of the early MIPS/ARM CPUs. Quite a bit different from something like the 6502 which from a compiler standpoint is about as horrible a target as you can imagine!!

Compilers, especially of the 'recursive descent' variety like the ones I'm familiar with and like Tiny Pascal, tend to share many similar design philosophies, flows and constraints.

At some point, I do plan on refactoring the code in the interests of longer-term sanity and code maintenance/development.

For now, it's back to getting these extra data types implemented!

Re: Tiny Pascal Compiler - V1 NOW AVAILABLE!

Posted: Mon Dec 18, 2023 9:51 pm
by pzembrod
Hi Martin,

I'll join the ranks of those who admire your courage and perseverence to work your way into this code base - and I'll say I also admire the original authors' effort to construct and bootstrap this thing in BASIC.

Interestingly there's a somewhat similar effort going on in the "Commodore 64/128 Programming" group on Facebook where a guy called El Dendo is bootstrapping a PL/0 compiler with pCode VM in BASIC, and unless I'm much mistaken, Nikolaus Wirth's PL/0 and Chung/Yuen's Tiny Pascal should be very similar things.

Your work is also getting me interested in pCode again. My own small C compiler cc64, which shares many features with Tiny Pascal, is designed to directly generate 6502 code. And while the code runs decently fast, or so I think, a big disadvantage is that he generated code is rather large. So the amount of functionality you could fit into 64k is quite limited. So in this respect, pCode becomes really attractive.

Cheers
/Philip

Re: Tiny Pascal Compiler - V1 NOW AVAILABLE!

Posted: Mon Dec 18, 2023 10:25 pm
by Martin Schmalenbach
Thanks Philip!

It's a fun project, and as I work to add additional data types I'm coming to appreciate that the 2 original authors coded this very much with the limitations of their environment in mind... including simplifying many aspects and making a number of assumptions to reduce code size and complexity simply because it only handles one data type! The expression evaluator is a little inefficient in some areas, such as how it parses and handles negative numeric constants, but it makes for easier and less complex code. And there is no attempt to optimize anything anywhere.

The recursive descent parser is written slightly differently to how I have written RD parsers elsewhere so I do trip myself up from time to time!

It is my plan, once I have the additional data types up and running, to have the compiler generate either pCode or 6502 assembly code. Then I plan to do some of the more routine and easier to implement optimizations, such as dead code removal, common sub expression optimization, variable liveness, constant folding etc - the usual. Should be fun given the 6502 really isn't designed for this. I wrote a pascal compiler for the 16 bit Microchip microcontrollers,the PIC24 and dsPIC30/33 series about a lifetime ago - easy code generation there when you have so many registers to play with, and versatile enough to properly support activation records and frame pointers etc! I might cheat a bit and make use of the zero page 16 bit 'registers' that have been defined for the CX16. All of this means adding to the compiler's complexity to generate abstract syntax trees, control glow graphs etc, and making use of single static assignment. I suspect the compiler will end up being a very multi pass affair.

I'm sure you had similar fun crafting cc64!

Anyway, seasons greetings, and onwards & upwards - or downwards - whichever works!

Cheers

Martin

Re: Tiny Pascal Compiler - V1 NOW AVAILABLE!

Posted: Thu Dec 21, 2023 9:05 pm
by pzembrod
Martin Schmalenbach wrote: Mon Dec 18, 2023 10:25 pm The recursive descent parser is written slightly differently to how I have written RD parsers elsewhere so I do trip myself up from time to time!
I'll have to try to dig into Tiny Pascal's code at some point again and see how its RD parser compares to the one I constructed for cc64. I wasn't aware yet how different RD parser styles look.

Incidentally, I recently read about Dijkstra's shunting yard angorithm, and for the first time understood the mechanism of at least one non-RD parser.

And for something completely different again: Were you aware that G-Pascal has heritage to Tiny Pascal?
I was surprised when I found https://www.gammon.com.au/G-Pascal/history.htm
Martin Schmalenbach wrote: Mon Dec 18, 2023 10:25 pm It is my plan, once I have the additional data types up and running, to have the compiler generate either pCode or 6502 assembly code.
I'm thinking the same. Ideally interchangeably, so you can have speed where you need it, without having to pay the code size price for the enire program.
Martin Schmalenbach wrote: Mon Dec 18, 2023 10:25 pm I'm sure you had similar fun crafting cc64!
Yes, I think I did. :-)

Seasons' greetings to you, too.

Cheers
/Philip

Re: Tiny Pascal Compiler - V1 NOW AVAILABLE!

Posted: Sat Dec 30, 2023 11:25 am
by yock1960
I guess now is as good a time as any to reveal that Martin's project has piqued my interest so much and the BASIC code was so daunting, that I decided to try my hand at translating some other 'Pascal' source code into Prog8, which, if successfull, would run faster. I needed a project, being in between coding projects and I had no other tasks that interested me at the time.

I poked around and found a fair number of sources, written in Pascal, not my favorite language but I'm not too picky. I finally determined to try to translate Pascal-S. It's apparently a 'student' version of Pascal, written by N. Wirth himself. Fortunately, there are sources written in Turbo Pascal, since they are a bit more approachable than the original Pascal source code. I've been working on this since the end of November, finally getting the compiler portion to compile that same example that Martin included in his post. A few more days work and I had the interpreter up and running.

Lots of hair pulling to this point as the original source makes heavy use of recursion, which is not supported 'out of the box' by Prog8. I've been testing additional Pascal code for the last few days, progress is getting more difficult. Repeat and while loops are kind of working. For loops seem to not have made the 'final cut' of this version, not a big deal, but puzzling. The last few days have had me struggling to get array variables working. They compile, but the code generated is not correct. I have this compiler running on the PC side and have made changes to assist my translation. I believe that I have pinpointed the problem, but so far, not been able to fix it. I've been stuck several times already and am not ready to throw in the towel just yet. It's a new day today, perhaps I'll get over this latest hump today!

I'm not sure that if I can ever get the supported language constructs to work properly, that this will ever be more than a 'toy'. Perhaps that's not the best term. Like some other programs that I have written for the X16, I've never done anything like this before, but retirement leaves one plenty of time on one's hands and while I always wanted to be able to 'program stuff', back in the day, I never did very much.

If I ever do get it fully working, I would like to add the capability to support the X16 kernal. I'm not too sure exactly how that can be done, but where there's a will, there's a way! 😄

Re: Tiny Pascal Compiler - V1 NOW AVAILABLE!

Posted: Tue Mar 12, 2024 7:40 am
by oodler
Hi! I am trying this out because I want to find things to do with the XVI editor (vi port), and this seems like the perfect thing to try in addition to writing BASIC.

I typed out the program using XVI on the x16, loaded the compiler and after fixing some errors in my code I was able to get the compiler to do stuff. But after a while, it ended with:
DISK ERROR 64
00, OK,00,00

BREAK IN 119
(with a black background, though)

I am using the TPCOMPV1.2.PRG that I found in page 1 of this thread. Please advise! If I can get this down, I will add it to my small but growing list of "things to do with XVI". I would also like to bundle your programs (with due credit) along in my ".zip" distributing the editor. I currently just have a "BASLOAD" that can be used for BASIC programs typed into the editor and saved as text file.

Cheers,
oodler

Re: Tiny Pascal Compiler - V1 NOW AVAILABLE!

Posted: Tue Mar 12, 2024 7:48 am
by Martin Schmalenbach
Hi

Thanks for having a play with the TP compiler!

Can you share the pascal program you used with the TP compiler so I can try to recreate the error situation you experienced and try to fix it from there?

Cheers

Martin