Lazarus

Announcements => Third party => Topic started by: vtereshkov on March 27, 2020, 11:18:05 pm

Title: Go-flavored Pascal: now with Raylib bindings
Post by: vtereshkov on March 27, 2020, 11:18:05 pm
By replacing the heavyweight Delphi-style OOP with a much simpler method/interface model inspired by Go, I have written an extremely compact (~10000 lines) Pascal compiler (https://github.com/vtereshkov/xdpw) for Windows. Initially built with Free Pascal, it is now self-hosting. It can be viewed as an implementation of Russ Cox's thought:

"If I could export one feature of Go into other languages, it would be interfaces."

The compiler directly emits native x86 code and doesn't require any external assembler or linker. It can be easily embedded into larger software systems and used for educational purposes, e.g., as a playground for language design amateurs.

Integration (https://github.com/vtereshkov/raylib-xdpw) with the Raylib (https://www.raylib.com/) 2D/3D game development library has become the first sign of maturity for my compiler.
Title: Re: Go-flavored Pascal: now with Raylib bindings
Post by: marcov on March 27, 2020, 11:32:28 pm
"If I could export one feature of Go into other languages, it would be interfaces."

https://wiki.freepascal.org/How_To_Use_Interfaces
Title: Re: Go-flavored Pascal: now with Raylib bindings
Post by: vtereshkov on March 27, 2020, 11:44:55 pm
In Delphi and Free Pascal (which both use the C++-style OOP model), an interface is a secondary notion that depends on the fundamental notion of a class.

Go does not have classes, objects, or inheritance. All these concepts are replaced by an interface. This is much simpler but provides the same polymorphic behavior as C++-style classes.

That's how it is done in Go (https://research.swtch.com/interfaces) and in my compiler (https://medium.com/@vtereshkov/how-i-implemented-go-style-interfaces-in-my-own-pascal-compiler-a0f8d37cd297).

 
Title: Re: Go-flavored Pascal: now with Raylib bindings
Post by: 440bx on March 28, 2020, 12:06:34 am
I have written an extremely compact (~10000 lines) Pascal compiler (https://github.com/vtereshkov/xdpw) for Windows. Initially built with Free Pascal, it is now self-hosting.
I've looked at your compiler in the past.  I see you've been busy :)

Nicely done.
Title: Re: Go-flavored Pascal: now with Raylib bindings
Post by: 440bx on March 28, 2020, 02:38:45 am
Just in case you'd like to know.  When compiling this program:
Code: Pascal  [Select][+][-]
  1.   program Test;
  2.  
  3.   const
  4.     ACONSTANT = not true;
  5.  
  6.   begin
  7.   end.
  8.  
I get a stack overflow
Quote
XD Pascal for Windows 0.12
Copyright (c) 2009-2010, 2019-2020, Vasiliy Tereshkov
Compiling system.pas
Compiling BooleanConstantExpression.lpr
An unhandled exception occurred at $00416D6A:
EStackOverflow: Stack overflow
  $00416D6A
  $00416FA7
  $00416FA7
  $00416FA7
  $00416FA7
  $00416FA7
  $00416FA7
  $00416FA7
  $00416FA7
  $00416FA7
  $00416FA7
  $00416FA7
  $00416FA7
  $00416FA7
  $00416FA7
  $00416FA7
  $00416FA7
Title: Re: Go-flavored Pascal: now with Raylib bindings
Post by: vtereshkov on March 28, 2020, 10:41:51 am
Thank you. I've fixed it :)
Title: Re: Go-flavored Pascal: now with Raylib bindings
Post by: circular on March 28, 2020, 11:20:55 am
Interesting.

By the way, while you're at it, why not remove the ";" ?
Title: Re: Go-flavored Pascal: now with Raylib bindings
Post by: marcov on March 28, 2020, 11:21:48 am
Well, the license is a main pro of course, because that makes it embeddable, and I'll certainly have a look. That appeals more to me than the GO stuff.

You could try to develop that idea further with a compile to memory option or so and a good demonstration (integrate in old dev pascal ide or so?).

If you are going for embeddability it might be good to rename some of the compiler units as their names are (literally) awfully COMMON :)

For bits and pieces like the bug that 440bx found you could try to harvest the FPC testsuite (and maybe pas2js has tests somewhere too for the fcl-passrc parser)
Title: Re: Go-flavored Pascal: now with Raylib bindings
Post by: vtereshkov on March 28, 2020, 11:45:17 am
By the way, while you're at it, why not remove the ";" ?
Is the semicolon so annoying for you? Even more than begin...end instead of (now common) {...} ? :)
I'll think of it, but the semicolon cannot be completely removed - it can be made optional. The compiler source will have them anyway, since I need to keep it compatible with Delphi/FPC.
Title: Re: Go-flavored Pascal: now with Raylib bindings
Post by: vtereshkov on March 28, 2020, 12:05:55 pm
You could try to develop that idea further with a compile to memory option or so and a good demonstration (integrate in old dev pascal ide or so?).
I'll definitely do it if I see any signs of practical interest from the community. Now many people like my project as a curious museum piece, but not as a tool for their daily tasks.
Quote
For bits and pieces like the bug that 440bx found you could try to harvest the FPC testsuite (and maybe pas2js has tests somewhere too for the fcl-passrc parser)
The only test suite I regularly run is the ISO Pascal Acceptance Test by S. A. Moore (with some Borland-specific changes). Once I tried to run a huge GNU Pascal test suite, but it mostly contains tests for some rare features (like "modules" from ISO Extended Pascal). So my compiler has successfully compiled just about 600 tests of 5000 (Delphi compiled about 900). I have to look at the FPC test suite.
Title: Re: Go-flavored Pascal: now with Raylib bindings
Post by: marcov on March 28, 2020, 12:26:16 pm
By the way, while you're at it, why not remove the ";" ?
Is the semicolon so annoying for you? Even more than begin...end instead of (now common) {...} ? :)
I'll think of it, but the semicolon cannot be completely removed - it can be made optional. The compiler source will have them anyway, since I need to keep it compatible with Delphi/FPC.

If not for compatibility, you'd be better off implementing a Modula2 than a Pascal anyway. Even if you stick with Wirthian. So keeping compatibility is paramount IMHO
Title: Re: Go-flavored Pascal: now with Raylib bindings
Post by: marcov on March 28, 2020, 12:37:09 pm
You could try to develop that idea further with a compile to memory option or so and a good demonstration (integrate in old dev pascal ide or so?).
I'll definitely do it if I see any signs of practical interest from the community. Now many people like my project as a curious museum piece, but not as a tool for their daily tasks.

I understand.  I also mostly envision usage for an old project (that might become live again if a customer comes back again). Mostly to replace a kind of  expression evaluator with generated code, that is then compiled + reloaded/unloaded.

So I'm also not immediately starting unfortunately and only storing the link for future reference.

As for the testsuite, it might get you more borland oriented source pieces. But probably it is a mish mash of the various dialects support for FPC (turbo/delphi/iso/objective pascal). so might require some sorting out. It certainly won't be ready to run.
Title: Re: Go-flavored Pascal: now with Raylib bindings
Post by: vtereshkov on March 28, 2020, 12:38:56 pm
If not for compatibility, you'd be better off implementing a Modula2 than a Pascal anyway. Even if you stick with Wirthian. So keeping compatibility is paramount IMHO
If I dare abandon compatibility, I'll probably make a completely different language. Modula-2 and Oberon still require that declarations be separated from statements, so that I cannot declare a variable where I really need it. They still don't have type inference, returning multiple values from a function, the "+=" operator, the generalized for statement, etc.
Title: Re: Go-flavored Pascal: now with Raylib bindings
Post by: marcov on March 28, 2020, 01:00:43 pm

If I dare abandon compatibility, I'll probably make a completely different language. Modula-2 and Oberon still require that declarations be separated from statements, so that I cannot declare a variable where I really need it. They still don't have type inference, returning multiple values from a function, the "+=" operator, the generalized for statement, etc.

If you care about those. 

Except maybe generalized for, but that depends on what you mean with it. I kind of like the situation in Object Pascal, where the normal FOR is a true for (as in that the number of iterations is known up front), but for..in allows escape from that using duck-typing iterators.

Horrible as language design from a purist view, but it simply works nice in practice. But too many skip out the true for in favour of only the generalized for, and that is IMHO a shame.

Title: Re: Go-flavored Pascal: now with Raylib bindings
Post by: vtereshkov on March 28, 2020, 10:40:09 pm
As for the testsuite, it might get you more borland oriented source pieces. But probably it is a mish mash of the various dialects support for FPC (turbo/delphi/iso/objective pascal). so might require some sorting out. It certainly won't be ready to run.
Thank you. It is not well-suited to my compiler, but anyway it has already helped me find and fix one more bug :)
Title: Re: Go-flavored Pascal: now with Raylib bindings
Post by: circular on April 01, 2020, 11:43:04 pm
By the way, while you're at it, why not remove the ";" ?
Is the semicolon so annoying for you? Even more than begin...end instead of (now common) {...} ? :)
I'll think of it, but the semicolon cannot be completely removed - it can be made optional. The compiler source will have them anyway, since I need to keep it compatible with Delphi/FPC.
That's a suggestion as I was thinking of writing a parser to make Pascal easier. Removing the semicolon was part of it.

If find the semicolon not really annoying but useless. I would almost never use it. And recently a semicolon lost after a "then" was an error difficult to spot.

There are so few cases where you could need it. So I really wanted to remove it, not making it optional. What are the use cases anyway?
Code: Delphi  [Select][+][-]
  1. if sometest then ;
Code: Delphi  [Select][+][-]
  1. for i := 1 to 10 do ;
What's the point of that?

Maybe
Code: Delphi  [Select][+][-]
  1. if sometest then if sometest2 then DoA; else DoB
But then you could anyway do it with begin/end making it much more readable.

Or I am missing something?
Title: Re: Go-flavored Pascal: now with Raylib bindings
Post by: marcov on April 01, 2020, 11:57:19 pm

Or I am missing something?

- Dangling else?
- multiline statements?
- error handling?
- compatibility ? And if you change this, why try being compatible or even pascal at all?
Title: Re: Go-flavored Pascal: now with Raylib bindings
Post by: circular on April 02, 2020, 12:14:54 am
- Dangling else?
Give me an example as I have done, please. That's too easy otherwise.

Quote
- multiline statements?
You don't need a semicolon for that :
Code: Delphi  [Select][+][-]
  1. a := 2   b := 3
would compile just fine. Though one could allow a semi-colon if there is some text afterwards, to make it more readable.

Quote
- error handling?
Please expand.

Quote
- compatibility ? And if you change this, why try being compatible or even pascal at all?
That's just polarizing the debate and you are aware of it. I like Pascal, there are just some things that can be improved.
Title: Re: Go-flavored Pascal: now with Raylib bindings
Post by: vtereshkov on April 02, 2020, 12:41:01 am

Or I am missing something?

- Dangling else?
- multiline statements?
- error handling?
- compatibility ? And if you change this, why try being compatible or even pascal at all?

I think the most dangerous form of 'dangling else' is when it's inside the case statement. The output of the example below depends on whether there is a semicolon before else or not.
Code: Pascal  [Select][+][-]
  1. var
  2.   a: integer = 1;
  3.  
  4. begin
  5. case a of
  6.   1, 2: if a = 2 then
  7.           writeln(2);
  8.         else
  9.           writeln(1);
  10. end;            
  11. end.
Title: Re: Go-flavored Pascal: now with Raylib bindings
Post by: winni on April 02, 2020, 02:35:32 am
Hi!

I repeat myself but:

The else in the case statement is and was a bad idea.
It was introduced by Turbo Pascal.

The Pascal syntax is otherwise.

Just abolish the else in case.

Winni

Title: Re: Go-flavored Pascal: now with Raylib bindings
Post by: eljo on April 02, 2020, 04:10:24 am
Hi!

I repeat myself but:

The else in the case statement is and was a bad idea.
It was introduced by Turbo Pascal.

The Pascal syntax is otherwise.

Just abolish the else in case.

Winni
the pascal syntax did not support classes either they where introduced with delphi 1 it did not support objects, advenced records and a list of other features too. That's a week excuse of an argument don't you think?
Title: Re: Go-flavored Pascal: now with Raylib bindings
Post by: 440bx on April 02, 2020, 04:10:30 am
- error handling?
Please expand.
Getting rid of the semicolon in Pascal is not a good idea.  The reason is, a semicolon tells the compiler a statement has ended.  Without the semicolon, the compiler has to figure out a statement has ended using context, that alone is likely not possible in all cases and, even if possible, it would complicate the compiler's code. 


vtereshkov provided a good example of that in his last post.  In the code:
Code: Pascal  [Select][+][-]
  1. var
  2.   a: integer = 1;
  3.  
  4. begin
  5. case a of
  6.   1, 2: if a = 2 then
  7.           writeln(2);
  8.         else
  9.           writeln(1);
  10. end;            
  11. end.
if there were no semicolons, how could the compiler decide if the "else" associates with the "if" or the "case" ?

Another example that comes to mind which I personally use all the time is this:
Code: Pascal  [Select][+][-]
  1. if somecondition then
  2. {$IFDEF FORCE_CONDITION} ; {$ENDIF}
  3. begin
  4.   <statements to handle the condition>
  5. end;
  6.  
that way I can ensure the code can handle the condition without having to create a test case for it, which in some cases can be very difficult and/or time consuming, to ensure the code works.  By optionally inserting that semicolon after the "then" I can activate the code.

Spaces and newlines make for lousy separators.

Maybe that also gives an idea to those who put the "then" and "begin" keywords on the same line, why doing that is not a good idea either, nor good "style" for that matter.
Title: Re: Go-flavored Pascal: now with Raylib bindings
Post by: bytebites on April 02, 2020, 08:12:16 am
Code: Pascal  [Select][+][-]
  1. var
  2.   a: integer = 1;
  3.  
  4. begin
  5. case a of
  6.   1, 2: if a = 2 then
  7.           writeln(2);
  8.         otherwise
  9.           writeln(1);
  10. end;            
  11. end.

Use otherwise with case to make meaning clear.

Title: Re: Go-flavored Pascal: now with Raylib bindings
Post by: 440bx on April 02, 2020, 08:39:40 am
Use otherwise with case to make meaning clear.
I agree.  I used that example to show that getting rid of semicolons in Pascal is not as simple as it first seems to be.  Removing the "else" option from the "case" statement would definitely break backwards compatibility which is obviously not desirable.

Additional, there would no longer be a simple option to cause compound statements in an "if" to be conditionally executed (shown in the second example)
Title: Re: Go-flavored Pascal: now with Raylib bindings
Post by: marcov on April 02, 2020, 09:39:58 am
Quote
- compatibility ? And if you change this, why try being compatible or even pascal at all?
That's just polarizing the debate and you are aware of it. I like Pascal, there are just some things that can be improved.

This whole subthread is polarizing, making minor syntax details seem to be the center of the world.

The point is and remains if you "improve" it (and that is a very subjective word), it is no longer Pascal, and Modula2 is not that far from Pascal, but then with better blockstructure. See e.g. Oxygene with its changing procedure to method for objects etc.

You are either compatible, or not. Even if you label "not compatible" as "improved" to make it sound nicer.

But I'd leave this the same for compatibility's sake, and if it is that important for you, and it is that easy/automatic, enhance an editor to do it for you.

Note that the nested IF dangling else only matters to keep the scanner part more context free https://en.wikipedia.org/wiki/Dangling_else

But maybe that is less important if you do away the ;, then you have to reevaluate the grammar entirely anyway.
Title: Re: Go-flavored Pascal: now with Raylib bindings
Post by: circular on April 02, 2020, 10:33:54 am
I think the most dangerous form of 'dangling else' is when it's inside the case statement. The output of the example below depends on whether there is a semicolon before else or not.
Code: Pascal  [Select][+][-]
  1. var
  2.   a: integer = 1;
  3.  
  4. begin
  5. case a of
  6.   1, 2: if a = 2 then
  7.           writeln(2);
  8.         else
  9.           writeln(1);
  10. end;            
  11. end.
Indeed. In fact, that would be the only compelling argument for keeping the semicolon.

The else in the case statement is and was a bad idea.
It was introduced by Turbo Pascal.

The Pascal syntax is otherwise.

Just abolish the else in case.
Oh I did not know I could use otherwise here. That feels safer indeed.  :)

Another example that comes to mind which I personally use all the time is this:
Code: Pascal  [Select][+][-]
  1. if somecondition then
  2. {$IFDEF FORCE_CONDITION} ; {$ENDIF}
  3. begin
  4.   <statements to handle the condition>
  5. end;
  6.  
that way I can ensure the code can handle the condition without having to create a test case for it, which in some cases can be very difficult and/or time consuming, to ensure the code works.  By optionally inserting that semicolon after the "then" I can activate the code.
Interesting trick. Though you can also do:
Code: Delphi  [Select][+][-]
  1. {$IFNDEF FORCE_CONDITION}
  2. if somecondition then
  3. {$ENDIF}
  4. begin
  5.   <statements to handle the condition>
  6. end;
  7.  

This whole subthread is polarizing, making minor syntax details seem to be the center of the world.
You're trying the mirror effect now.

Nope I did not intend to make a whole subthread or make it the center of the world. It just seems that people care and have interesting things to say about it.

Quote
The point is and remains if you "improve" it (and that is a very subjective word), it is no longer Pascal, and Modula2 is not that far from Pascal, but then with better blockstructure. See e.g. Oxygene with its changing procedure to method for objects etc.
I am not forcing the idea. We could vote on it and that could be a compiler switch.

Quote
You are either compatible, or not. Even if you label "not compatible" as "improved" to make it sound nicer.
There you go again polarizing.

Quote
But I'd leave this the same for compatibility's sake, and if it is that important for you, and it is that easy/automatic, enhance an editor to do it for you.
I have been thinking about it.

Quote
Note that the nested IF dangling else only matters to keep the scanner part more context free https://en.wikipedia.org/wiki/Dangling_else
In fact I don't see how it changes anything because the ; is not allowed anyway before else.

I created a new topic:
https://forum.lazarus.freepascal.org/index.php/topic,49153.0.html
Title: Re: Go-flavored Pascal: now with Raylib bindings
Post by: 440bx on April 02, 2020, 10:54:14 am
Interesting trick. Though you can also do:
Code: Delphi  [Select][+][-]
  1. {$IFNDEF FORCE_CONDITION}
  2. if somecondition then
  3. {$ENDIF}
  4. begin
  5.   <statements to handle the condition>
  6. end;
  7.  
Yes but, that takes two lines.  With the one I use, I can have the directive in one line in the editor's buffer and insert very quickly in all the "if" statements whose statements I want to test and, removing it if necessary only requires deleting one line.  Faster, more efficient.   Long live the semicolon. :)
Title: Re: Go-flavored Pascal: now with Raylib bindings
Post by: circular on April 02, 2020, 10:56:53 am
Yes but, that takes two lines.  With the one I use, I can have the directive in one line in the editor's buffer and insert very quickly in all the "if" statements whose statements I want to test and, removing it if necessary only requires deleting one line.  Faster, more efficient.   Long live the semicolon. :)
:D I can see why you would not want to enable the compiler directive semicolon-free.  :)
Title: Re: Go-flavored Pascal: now with Raylib bindings
Post by: marcov on April 02, 2020, 12:01:50 pm
This whole subthread is polarizing, making minor syntax details seem to be the center of the world.
You're trying the mirror effect now.

Nope. Simply two standards. Everybody but you has hidden agenda, your actions are indisputable. I'm wondering what "effect" you'll think up for this.

Quote
Nope I did not intend to make a whole subthread or make it the center of the world. It just seems that people care and have interesting things to say about it.

If you accept the basis premise that it is a serious problem. The thread is terribly light on this.

Quote
I am not forcing the idea. We could vote on it and that could be a compiler switch.

Ok, I didn't know mr. vtereshkov accepted such features by vote.

Quote
Quote
You are either compatible, or not. Even if you label "not compatible" as "improved" to make it sound nicer.
There you go again polarizing.

I'm not polarizing. It is a fact. Unless you want to accuse other pascal compilers not accepting the dialect anymore as polarizing. Naughty, naughty parsers.

Quote
Quote
But I'd leave this the same for compatibility's sake, and if it is that important for you, and it is that easy/automatic, enhance an editor to do it for you.
I have been thinking about it.

I can see the use for it if you run it on e.g. selected blocks to enhance quotes. E.g. to fix them after other substitutions etc.

Quote
Quote
Note that the nested IF dangling else only matters to keep the scanner part more context free https://en.wikipedia.org/wiki/Dangling_else
In fact I don't see how it changes anything because the ; is not allowed anyway before else.

That is solved in a different (semantic) layer. Read the article.

Quote
I created a new topic:
https://forum.lazarus.freepascal.org/index.php/topic,49153.0.html

I had the errorhandling point to elaborate, I did it in that thread.
Title: Re: Go-flavored Pascal: now with Raylib bindings
Post by: circular on April 02, 2020, 09:42:43 pm
Nope. Simply two standards. Everybody but you has hidden agenda, your actions are indisputable. I'm wondering what "effect" you'll think up for this.
I have no idea what you're talking about.

Quote
If you accept the basis premise that it is a serious problem. The thread is terribly light on this.
I don't consider it to be a serious problem. I am just talking freely.

Quote
I'm not polarizing. It is a fact. Unless you want to accuse other pascal compilers not accepting the dialect anymore as polarizing. Naughty, naughty parsers.
Judgements are not the way to go my friend.
TinyPortal © 2005-2018