Recent

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

Wysardry

  • Jr. Member
  • **
  • Posts: 69
Can FreePascal be used to create an internal or embedded DSL?
« on: August 11, 2022, 08:51:15 am »
I'm looking to create a domain specific language within standard FreePascal code (rather than modifying the FreePascal compiler).

I'm not 100% certain of the terminology, so I don't know what to search for in the documentation.

Is it possible to replace keywords such as "writeLn" with "writeLine" or "say" or whatever?

How about operators such as ":=" being replaced with "becomes" or "isNow"?

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Can FreePascal be used to create an internal or embedded DSL?
« Reply #1 on: August 11, 2022, 09:00:00 am »
Is it possible to replace keywords such as "writeLn" with "writeLine" or "say" or whatever?

WriteLn is not a keyword, it's an intrinsic, a compiler provided function. That means, no, you can't replace it with something else without modifying the compiler (*).

How about operators such as ":=" being replaced with "becomes" or "isNow"?

Again, no, this is not possible without modifying the compiler (*).

* Well, technically you could define macros (e.g. {$define WriteLine := WriteLn} and {$define becomes := :=}), but this might lead to other problems down the road (e.g. if you need to use a preexisting function or type that contains that identifier) and there might be situations where using a macro might not be enough.


Wysardry

  • Jr. Member
  • **
  • Posts: 69
Re: Can FreePascal be used to create an internal or embedded DSL?
« Reply #2 on: August 11, 2022, 09:24:39 am »
Would creating a procedure or function called "writeLine" that contained "writeLn" work?

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Can FreePascal be used to create an internal or embedded DSL?
« Reply #3 on: August 11, 2022, 09:58:32 am »
I'm looking to create a domain specific language within standard FreePascal code (rather than modifying the FreePascal compiler).

I'm not 100% certain of the terminology, so I don't know what to search for in the documentation.

Is it possible to replace keywords such as "writeLn" with "writeLine" or "say" or whatever?

How about operators such as ":=" being replaced with "becomes" or "isNow"?

Don't follow that approach. Define the DSL syntax and behaviour you want, then learn how to parse and compile/interpret it.

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

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Can FreePascal be used to create an internal or embedded DSL?
« Reply #4 on: August 11, 2022, 12:44:44 pm »
Quote
WriteLn is not a keyword, it's an intrinsic, a compiler provided function. That means, no, you can't replace it with something else without modifying the compiler (*).
Yes that is perfectly possible to some extend and just like Sarah wrote it does have its limitations. Just for fun:
Code: Pascal  [Select][+][-]
  1. {$macro on}{$define say :=writeln}
  2. program writewhatever;
  3. begin
  4. say('Whatever');
  5. end.
  6.  
We had a lot of fun with such macro's a couple of years ago.. I did a Dutch Pascal, someone else did a German Pascal etc.
See here : https://forum.lazarus.freepascal.org/index.php/topic,36887.msg246289.html#msg246289
« Last Edit: August 11, 2022, 12:50:10 pm by Thaddy »
Specialize a type, not a var.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Can FreePascal be used to create an internal or embedded DSL?
« Reply #5 on: August 11, 2022, 01:41:15 pm »
Would creating a procedure or function called "writeLine" that contained "writeLn" work?

You can't replicate the functionality of WriteLn one to one. You can replicate single aspects, but not the whole.

Don't follow that approach. Define the DSL syntax and behaviour you want, then learn how to parse and compile/interpret it.

That would be my suggestion as well.

simone

  • Hero Member
  • *****
  • Posts: 573
Re: Can FreePascal be used to create an internal or embedded DSL?
« Reply #6 on: August 11, 2022, 01:44:24 pm »
I don't know exactly what you want to do. However I have implemented a sort of DSL building a transpiler which translates to freepascal. The transpiler itself has been written in freepascal. In this context, the fact that FPC supports a lot of targets is a great advantage in terms of portability.
« Last Edit: August 11, 2022, 02:31:55 pm by simone »
Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

Wysardry

  • Jr. Member
  • **
  • Posts: 69
Re: Can FreePascal be used to create an internal or embedded DSL?
« Reply #7 on: August 11, 2022, 07:30:34 pm »
My main goal is to create a simple and easy to use language where the code can be written using Windows, Linux, Raspberry Pi etc. and then compiled for a wider range of platforms including 8-bit retro machines and WebAssembly.

The created programs would be text only, so the graphics capabilities of the target machines would not be an issue.

I don't know of any Pascal compilers that can target multiple retro machines, but I have found a couple of C compilers that should work.

As I personally dislike C syntax and it isn't the easiest language for beginners (who would hopefully eventually use this language) I would need some sort of conversion option.

Pascal is already quite close to what I'm aiming for. It would just need a few minor tweaks to become more accessible to beginners, those using screen readers or anyone that is used to BASIC.

To create an entirely new language from scratch seems like it would be a lot of wasted effort when other established languages are available.

A slightly easier option would be to modify an existing language and compiler. That would also require modifying/creating a transpiler targetting C though.

Even easier would be to create an internal or embedded language, which is an option I have only recently discovered is possible.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Can FreePascal be used to create an internal or embedded DSL?
« Reply #8 on: August 11, 2022, 08:09:30 pm »
I don't know of any Pascal compilers that can target multiple retro machines,

https://wiki.freepascal.org/Category:Operating_Systems_and_Platforms

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 #9 on: August 11, 2022, 08:31:48 pm »
Unless I'm missing something, there are only two retro machines supported: ZX Spectrum and MSX.

z88dk can compile C code for over a hundred Z80 machines.

I'm not personally interested in compiling for all those platforms, but the ones I am interested in are in that list.

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Can FreePascal be used to create an internal or embedded DSL?
« Reply #10 on: August 11, 2022, 08:54:16 pm »
There are many more supported, including Wii which is great fun.
Specialize a type, not a var.

Wysardry

  • Jr. Member
  • **
  • Posts: 69
Re: Can FreePascal be used to create an internal or embedded DSL?
« Reply #11 on: August 11, 2022, 09:23:34 pm »
In the platform list there are just two entries in the classic home computers column.

By "retro machines" I meant 8-bit home computers, such as the Amstrad CPC series, Sinclair Spectrum, Enterprise, Sharp MZ series, MSX etc.

Most of the ones I'm especially interested in had a Z80 processor, but I would like to include other 8-bit machines (such as the Dragon 32 or BBC B) if possible.

Edson

  • Hero Member
  • *****
  • Posts: 1301
Re: Can FreePascal be used to create an internal or embedded DSL?
« Reply #12 on: August 11, 2022, 09:29:52 pm »
I've developed a compiler/IDE for the 6502 CPU: https://github.com/t-edson/P65Pas. Still in Beta by now.

Support for Commodore 64 and Working in C128. I'll be including support for more 8-bits platforms later.
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Can FreePascal be used to create an internal or embedded DSL?
« Reply #13 on: August 12, 2022, 04:46:36 am »
How about TurboRascal?

Quote
... one of whom put together a new compiler which targets retro platforms – and it goes by the name Turbo Rascal.

The list of supported platforms is extensive, with Turbo Rascal able to compile highly-optimized binaries for the C64, Amiga 500, BBC Micro, IBM PC, Atari ST, Game Boy, Amstrad, NES, ZX Spectrum, and more.
Source: https://hackaday.com/2021/11/30/turbo-rascal-is-the-retro-pascal-compiler-we-always-wanted/

Quote
(Turbo Rascal SE, TRSE) is a complete suite (IDE, compiler, programming language, image sprite level resource editor) intended for developing games/demos for 8 / 16-bit line of computers, with a focus on the MOS 6502, the Motorola 68000, the (GB)Z80 and the X86. TRSE currently supports application development for the C64, C128, VIC-20, PLUS4, NES, Gameboy, PET, ZX Spectrum, TIKI 100, Amstrad CPC 464, Atari 2600, 8086AT, Amiga 500, Atari 800, BBC Micro, Mega65, MSX, Apple II and the Atari ST 520.
Source: https://retrogamecoders.com/introduction-to-trse-programming/
« Last Edit: August 12, 2022, 05:21:50 am by Handoko »

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Can FreePascal be used to create an internal or embedded DSL?
« Reply #14 on: August 12, 2022, 03:41:36 pm »
A slightly easier option would be to modify an existing language and compiler. That would also require modifying/creating a transpiler targetting C though.

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.

Unless I'm missing something, there are only two retro machines supported: ZX Spectrum and MSX.

z88dk can compile C code for over a hundred Z80 machines.

I'm not personally interested in compiling for all those platforms, but the ones I am interested in are in that list.

You could just as well port FPC for those platforms. After all I've done the same for MSX...

 

TinyPortal © 2005-2018