TodePond / DreamBerd

perfect programming language
https://dreamberd.computer
Other
11.33k stars 349 forks source link

Pre-processor #try / #catch extensions #634

Open c3d opened 4 months ago

c3d commented 4 months ago

Obviously, you can use the C preprocessor in DreamBeard, since it's an independent tool. Unfortunately, this means that you cannot easily add indispensable features. One such indispensable feature, which was never implemented for reasons that totally elude me, is #try #catch #endtry.

For example, #ifdef in C has always been broken in that it cannot tell if a variable is defined (something that DreamBerd should repair as well, but that's another issue). So the following does not work as intended:

int b = 0; // Introduced in version 2.5 of the product
#ifdef b
int a = b + 1;
#else
int a = 1;
#endif

Clearly, the above code makes a lot of sense, and #try makes it quite easy to write:

int b = 0; // Introduced in version 2.5 of the product
#try
int a = b + 1;
#catch
int a = 1;
#endtry

What this does is quite obvious: it tries to compile the first version, and if that fails, falls back to compiling the second version. Easy peachy, simple to understand, even simpler to implement. It's really surprising it was never done before.

The only open question about this proposal is whether we need #retry in case the compiler goofed-up the first time. This can be useful if for example the compiler runs out of memory, but compiling again and again might succeed once the compiler has OOM-killed all other processes in the system.

c3d commented 4 months ago

We probably need to be a bit precise about what happens if #catch or #endtry is misspelled (since that too would be a compile error).

For example:

#try
int a = 0 / 0;
#cacht // Warning: misspelled
int a = 1;
#endtry

I believe that any reasonable compiler in this case would deduce that a=1 in all cases, and therefore be able to ignore the misspelling of #catch, but that needs to be specified. Probably emit a warning, something like:

test.dreamberd line 3: Warning, #catch is misspelled as #cacht, the problem is ignored because both branches have the same effect of setting a to 1.
TodePond commented 4 months ago

interesting! there is probably some fun to be had with this kind of thing in general!