Recent

Author Topic: Can FreePascal be used to create an internal or embedded DSL?  (Read 8782 times)

Wysardry

  • Jr. Member
  • **
  • Posts: 69
Re: Can FreePascal be used to create an internal or embedded DSL?
« Reply #15 on: August 12, 2022, 06:26:37 pm »
How about TurboRascal?
I bookmarked that some time ago, but it isn't yet complete enough for my needs.

Unless the support sheet is out of date, it doesn't yet support the platforms I need and probably won't until most of those on that list are 100% complete.

The source is available, but it's mostly C and C++ unfortunately.

Then FPC wouldn't be a help for you anyway, cause FPC does not transpile to C. And changing what language FPC supports wouldn't change what targets it supports.
None of the third party Pascal to C translators will work with FreePascal?

I haven't looked at any of them closely, as I wasn't sure FreePascal would be suitable for this project.

Quote
You could just as well port FPC for those platforms. After all I've done the same for MSX...
Do you mean create a cross-compiler that runs on modern machines and compiles for the retro machines, or create a native FPC compiler for each retro machine?

Either way, it sounds like it would be complicated.

I did notice that there's a Z80 cross-compiler already in the dev branch, but again I haven't looked into that option as I'm not sure FreePascal meets my other needs.

HiSoft Pascal was available for some of the machines I'm interested in, but getting hold of it legally for them would likely be difficult.

The only other option I have found is to use a language and compiler that already produces C code that can be compiled with SDCC or z88dk.

So far, I've seen Nim and two versions of BASIC that might work. I've heard the former has been used with 8-bit micro boards and 512 bytes of RAM, so that seems the most promising.

There's also The ZX BASIC compiler written in Python, which targets a wide range of 8-bit platforms, but no modern ones as far as I can tell.

It should be possible to modify the existing BASIC language used (to make it a DSL), but that could mean having to modify a whole bunch of compilation scripts too.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6647
Re: Can FreePascal be used to create an internal or embedded DSL?
« Reply #16 on: August 12, 2022, 06:55:57 pm »
(a) Don't write off an FPC port when- by your own admission- you have no understanding of how it's structured (b) you'll find that converting Pascal to C will result in comparatively inefficient and potentially unreliable ** code and (c) I've already told you that your best approach is probably learning how to write a DSL yourself... with care it might be usable with both FPC and TurboRascal (which I admit I'd forgotten about and haven't looked at).

** On account of C's tendency to use the heap heavily, which is unreasonable on a small system.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Wysardry

  • Jr. Member
  • **
  • Posts: 69
Re: Can FreePascal be used to create an internal or embedded DSL?
« Reply #17 on: August 12, 2022, 07:39:58 pm »
I'm not ruling anything out completely at this point, as I'm not sure what my options are as a relative beginner. That is why I asked for clarification on porting FPC. For all I know, there could be existing code that just needs tweaking.

C might be an inefficient option, but AFAIK the only other simple option is to use whatever version of BASIC is available on each target platform. I would imagine that a C compiler aimed at small devices is going to be reasonably efficient in comparison.

Writing a DSL myself is one of the options I'm looking at, but I'm not sure what existing language to use to write it in, given the target machines I have.

I do know that I want to avoid anything with a C-style syntax, as all those curly braces would sap my will to live before I was halfway done. I'd almost rather work with assembly language.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6647
Re: Can FreePascal be used to create an internal or embedded DSL?
« Reply #18 on: August 12, 2022, 08:09:29 pm »
C might be an inefficient option, but AFAIK the only other simple option is to use whatever version of BASIC is available on each target platform. I would imagine that a C compiler aimed at small devices is going to be reasonably efficient in comparison.

Why, and do you mean speed-efficient or space-efficient?

Quote
I do know that I want to avoid anything with a C-style syntax, as all those curly braces would sap my will to live before I was halfway done. I'd almost rather work with assembly language.

ALGOL-derived languages are all much the same: from an implementation POV "begin", "end" and the braces are just tokens (sometimes recognised by a distinct lexer) which are parsed much the same.

And I suggest giving some thought to Alan Kay's comment on Meta-2.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Wysardry

  • Jr. Member
  • **
  • Posts: 69
Re: Can FreePascal be used to create an internal or embedded DSL?
« Reply #19 on: August 12, 2022, 08:46:08 pm »
Why, and do you mean speed-efficient or space-efficient?
It has supposedly been written with small devices in mind, so should be optimised for such.

As I mentioned previously, Nim has been used to create programs for 8-bit micro boards with 512 bytes of RAM via a C compiler (I don't know which one, but likely SDCC or z88dk).

It should be more efficient in terms of memory and speed, as BASIC was usually interpreted and memory resident on 8-bit machines.
Quote
And I suggest giving some thought to Alan Kay's comment on Meta-2.
I'm afraid you've lost me there.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6647
Re: Can FreePascal be used to create an internal or embedded DSL?
« Reply #20 on: August 12, 2022, 10:57:30 pm »
It should be more efficient in terms of memory and speed, as BASIC was usually interpreted and memory resident on 8-bit machines.

Not a safe assumption. Python, Perl and so on are to a large extent interpreted.

Quote
Quote
And I suggest giving some thought to Alan Kay's comment on Meta-2.
I'm afraid you've lost me there.

You can use Google can't you?

Quote
META II is the first documented version of a metacompiler, as it compiles to machine code for one of the earliest instances of a virtual machine.

The paper itself is a wonderful gem which includes a number of excellent examples, including the bootstrapping of Meta II in itself (all this was done on an 8K (six bit byte) 1401!)."—Alan Kay

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

sketch

  • New Member
  • *
  • Posts: 32
Re: Can FreePascal be used to create an internal or embedded DSL?
« Reply #21 on: August 13, 2022, 12:35:47 am »
I do know that I want to avoid anything with a C-style syntax, as all those curly braces would sap my will to live before I was halfway done.
Use Racket https://www.racket-lang.org/, it avoids the C-style syntax and you don't need to worry about all those curly braces  ;D
« Last Edit: August 13, 2022, 12:46:39 am by sketch »

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: Can FreePascal be used to create an internal or embedded DSL?
« Reply #22 on: August 13, 2022, 07:11:01 pm »
Then FPC wouldn't be a help for you anyway, cause FPC does not transpile to C. And changing what language FPC supports wouldn't change what targets it supports.
None of the third party Pascal to C translators will work with FreePascal?

They might work with normal Pascal code, but if you adjust the language to your needs do you really think you wouldn't need to adjust these either?

Quote
You could just as well port FPC for those platforms. After all I've done the same for MSX...
Do you mean create a cross-compiler that runs on modern machines and compiles for the retro machines, or create a native FPC compiler for each retro machine?

Either way, it sounds like it would be complicated.

I did notice that there's a Z80 cross-compiler already in the dev branch, but again I haven't looked into that option as I'm not sure FreePascal meets my other needs.

I mean as a cross-compiler. These targets are too restrained to support FPC itself.

Porting FPC is not even that hard as long as the hardware platform is already supported (in this case Z80 which you noticed is supported in the development version), but you need to know your target system or learn it. The main part is implementing the necessary platform specific RTL functions. For MSX-DOS it essentially boiled down to these commits:

  • add support for the target in the compiler (commit)
  • add support for the target in fpcmake (commit)
  • add support for the target in fpmake (commit)
  • implement the RTL (commit)

Wysardry

  • Jr. Member
  • **
  • Posts: 69
Re: Can FreePascal be used to create an internal or embedded DSL?
« Reply #23 on: August 14, 2022, 07:04:32 am »
I haven't learned anything about assembly language, but wouldn't object to learning enough about each Z80 system to create new targets based on the existing ones.

Some of the targets I would like to support are quite obscure, but I should be able to find the necessary info.

I could possibly create something similar to what I'm aiming for by building a domain specific library or framework, if I could add enough new Z80 targets.

Would PascalScript be an option if I wanted to be able to test programs without compiling?

MarkMLl

  • Hero Member
  • *****
  • Posts: 6647
Re: Can FreePascal be used to create an internal or embedded DSL?
« Reply #24 on: August 14, 2022, 10:28:46 am »
I haven't learned anything about assembly language, but wouldn't object to learning enough about each Z80 system to create new targets based on the existing ones.

Some of the targets I would like to support are quite obscure, but I should be able to find the necessary info.

If you can't find the info you're screwed in every case. In FPC terms, the compiler generated Z80 code and you might need to adapt the RTL slightly for different targets. However I suggest you examine what early versions of Turbo Pascal did to customise themselves, even if in general they only targeted different variants of CP/M.
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: Can FreePascal be used to create an internal or embedded DSL?
« Reply #25 on: August 14, 2022, 01:39:52 pm »
Some of the targets I would like to support are quite obscure, but I should be able to find the necessary info.

It's not as if FPC doesn't already support obscure targets. ;)

Would PascalScript be an option if I wanted to be able to test programs without compiling?

How do you believe this would help you? If you just want to test programs you can use FPC as well. And for other targets you'd need to port PascalScript first which is rather heavy weight.

I haven't learned anything about assembly language, but wouldn't object to learning enough about each Z80 system to create new targets based on the existing ones.

Some of the targets I would like to support are quite obscure, but I should be able to find the necessary info.

If you can't find the info you're screwed in every case. In FPC terms, the compiler generated Z80 code and you might need to adapt the RTL slightly for different targets. However I suggest you examine what early versions of Turbo Pascal did to customise themselves, even if in general they only targeted different variants of CP/M.

FPC's RTL is already well abstracted so that platform specific code can be easily inserted. It might be a bit more heavy weight than what TP did in a more direct way (after all we want to keep this maintainable), but so far it worked great. :)

MarkMLl

  • Hero Member
  • *****
  • Posts: 6647
Re: Can FreePascal be used to create an internal or embedded DSL?
« Reply #26 on: August 14, 2022, 02:18:20 pm »
FPC's RTL is already well abstracted so that platform specific code can be easily inserted. It might be a bit more heavy weight than what TP did in a more direct way (after all we want to keep this maintainable), but so far it worked great. :)

Yes, but my intended point was that with a minimum of research OP would discover how many variants of early TP versions there were (hint: more than a handful, fewer than a dozen), and the extent to which each variant could be customised for a particular target during installation. And from that he might possibly have the wit to work out /why/ the variants differed, even if he has so far been resistant to looking at the FPC porting documentation and to following our suggestion of learning a bit about compiler writing.

I used stuff to which I have alluded in this thread- and I have no intention of repeating myself- to compile an ALGOL-derivative (which could just as easily have been a Pascal-derivative) to assembler source for various 8- and 16-bit processors (I admit to having been helped by having assemblers available which took a common directive syntax). This was poor on type checking etc., and poor on optimisation... but there is at least one promising derivative which can improve the latter.

But this isn't my project or problem, and while- like any member of the community- I'd do my best to help if he were porting FPC or the RTL I've seen (and admittedly sold or supported) too many dubious development solutions to have much enthusiasm for half-arsed discussion of transpilers via C etc.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Wysardry

  • Jr. Member
  • **
  • Posts: 69
Re: Can FreePascal be used to create an internal or embedded DSL?
« Reply #27 on: August 14, 2022, 07:43:22 pm »
Okay, a lot of assumptions are being made here, probably because I didn't think it would be helpful to go into a lot of detail about what I have researched so far.

My end goal is to create a system that runs on modern hardware that can produce text-based programs that run on modern and retro hardware.

I want to find the simplest, easiest and least time-consuming route to do that, as I want to live long enough to be able to use that system myself and make it available for others to use.

On and off I have spent the last two years looking into ways this could be done and I'm now suffering from information overload and analysis paralysis. Many others have worked on different parts of the process, but I haven't found a complete system.

The main target audience for this system would be those who own retro 8-bit home computers or use emulators for them (possibly because they no longer own such machines).

Interpreters were common for these devices and compilers were much less so. Including one with the front end of the system seems natural, especially if the process for translating and compiling the code has several stages.

BASIC would have been the most commonly used language on these retro machines, as it was inluded with most of them. Pascal and C compilers were available, but much less widely used.

HiSoft sold versions of BASIC, Pascal and C, for example.

If you owned a Z80 machine with a disk drive (rare in the UK), you could use more languages via CP/M. I'm reasonably sure that this was how Turbo Pascal was made available for 8-bit micros.

The upshot of this is, that more users would know BASIC than any other language, so the system should use something similar. Pascal would be close enough for those used to structured BASIC.

Languages with a C-style syntax would be harder to adjust to. I know it caused me issues when I tried learning it.

So, one end of the process should be a BASIC or Pascal style language and at the other should be 8-bit machine code for various systems.

I know it's possible to create a programming language using FreePascal as there are several in use (Decimal BASIC and Nim are examples), but I'm not sure if creating another would be the most efficient way. Maybe modifying an existing one would be better?

I also know it's possible to create a transpiler (or source to source compiler) using FreePascal as there's one available to convert Decimal BASIC to FreePascal and several mentioned in the Wiki. It's also possible that the Nim to C translation program was written in FreePascal before Nim became self-compiling.

I've found several compilers that target the machines I'm most interested in, plus a site on Z80 assembly programming.

I have at least a hundred sites bookmarked that could potentially be pieces in the puzzle I'm trying to solve, but I'm having problems deciding which ones to use and how to fit them together.

I need help in deciding what to actually focus on going forward. I do know that FreePascal should be involved in the process somewhere, even if it is just as a source to source compiler target.

alpine

  • Hero Member
  • *****
  • Posts: 1032
Re: Can FreePascal be used to create an internal or embedded DSL?
« Reply #28 on: August 14, 2022, 08:40:43 pm »
Okay, a lot of assumptions are being made here, probably because I didn't think it would be helpful to go into a lot of detail about what I have researched so far.

My end goal is to create a system that runs on modern hardware that can produce text-based programs that run on modern and retro hardware.

I want to find the simplest, easiest and least time-consuming route to do that, as I want to live long enough to be able to use that system myself and make it available for others to use.
*snip*

And you want it to be BASIC- or Pascal-like one?
IMHO it depends what features you want to include into. Text-mode is one mentioned so far.

For example - in Arduino a subset of C language is described and the script is cleverly included in a pre-made gcc project. As a result the user thinks he wrote an INO script, but actually there is a full-blown gcc toolchain at the back. That approach is solely IDE oriented.

If something simple is required, it can be written from scratch, in Wirth's book a small pascal-like language implementation is described.
 
AFAIK the original sources for the Pascal compiler and P-code interpreter are available in the Internet but I didn't kept the link.

It is not such a big deal to implement (a simple) language since tools for that are freely available, see https://wiki.freepascal.org/Plex_and_Pyacc. Porting FPC to a new target looks to me much more complicated, its getting more and more sophisticated recently (but I can be wrong here).
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Wysardry

  • Jr. Member
  • **
  • Posts: 69
Re: Can FreePascal be used to create an internal or embedded DSL?
« Reply #29 on: August 14, 2022, 09:38:07 pm »
Yes, I'm leaning towards something similar to BASIC or Pascal syntax.

Something along the lines of ANSI Full BASIC without graphics or sound libraries but possibly with some additional Pascal features such as records or enumeration. It depends on the difficulty in including those features on many platforms.

Even ignoring that many retro machine users will be more familiar with BASIC, I personally find it easier to keep track of where code blocks begin and end when most block types have a different keyword associated with them. i.e. "for" loops end with "next [var]" and "while" loops end with "wend".

I'm guessing that what is easier for me to keep track of is easier for a computer to parse. That is just a guess though.

Edit: At this stage, I also don't want to rule out the possibility of translating to any 8-bit BASIC compilers.

I did find an interesting project called The Amsterdam Compiler Kit that could be useful. It doesn't seem that active, but most of the computers I would be targetting won't have changed much in the last few decades anyway.

I did notice Plex and Pyacc in the Wiki, but didn't explore them too deeply as I'm still undecided on what path to follow.
« Last Edit: August 14, 2022, 09:53:51 pm by Wysardry »

 

TinyPortal © 2005-2018