@spheris
Thanks for your work on YAB! Unfortunately I started this thread to discuss a transpiler. In other words: a program that will convert YAB programs into C++.
@moderators
If a moderator would be willing to split up this topic after the first post and give the the later posts a title like āFrench Locale for YABā, Iād be most grateful.
Thanks! BASIC has always been a tricky one for compilers because it lets you freely use new variables without having to allocate them first like C makes you do. Also, some branching commands like āon n gosubā arenāt fully structured. Iāve worked around both of these problems now. I just need to get it done.
Today I discovered a longstanding bug in the transpiler. In two call-sites to the same function, the second was being totally ignored and the parameter pass was always being sent to the first caller. Now fixed, it took me all day to get it done. This didnāt solve the bug I was looking for though.
With the āduck typingā of Python 3, this has been in the pipeline for a long time. Does it need a garbage collector for the Python and Java codes to convert into C++?
Out of sheer curiosity:
Why to C++ and not C? Maybe because C++ is Haikuās standard language?
Or does Yabasic have OOP? If I remember correctly, it hasnāt.
I use the string type from the standard template library instead of reinventing the wheel to do length-terminated strings in C. Null terminated strings really donāt work very quickly and BASIC strings are length terminated in almost every implementation.
Heavens no! Itās just an experimental code generator at this stage. Once I get the bugs worked out of it, I can use Yabās existing parser and runtime for the rest.
I did keep it close to C syntax so it could be used for other BASIC versions when Yab is done. I was going to try to write a transpiler for AMOS BASIC on the Amiga but I doubt Iāll get to it now.
Yesterday I got the āon n subā code working correctly and today I generated my first successful array with the transpiler. The function parameter passing and local variables are about the only remaining untested code pieces. I might be able to start integrating with the Yab parser and runtimes soon.
I might start looking into Flex++ and Bison++ or just newer versions that can generate C++ code directly. That would eliminate some glue written in C and maybe I could integrate the interpreter and the transpiler. Just food for thought.
Due to the state machine construct, setting a state and breaking out of the switch statement is a branch equivalent. I should have just made it a macro.
I agree with@memsom. Back in school my computer teacher was very strict about structured programming and avoiding spaghetti code. That helped me for example to write more readable assembler code many years later.
I donāt see the benefit of a macro here but I donāt understand what you wrote about āstateā. I guess your transpiler is a finite state automaton (FSA, like most compilers and interpreters are afaik).
BASIC is non-structured code for the most part. Iām bypassing the C++ constructs completely as a result. There are only two types of branch constructs in my runtime engine: Conditional and unconditional. In the end the code generated by the transpiler ends up looking like the following:
state=4;
while (state>=4)
{
switch (state)
{
case 4: /* <- this is a label */
{state=6; break;} /* <- this is a jump statement */
case 5:
/* this is the body of a while loop */
case 6: /* <- this is the entry point of a while loop */
if(v1<k2){state=5;break;} /* <- this is the conditional branch of a while loop */
{state=0;break;} /* <- this jumps to the end of the program */
default:
{state=1;break;} /* error condition for undefined state */
}
}
/* shutdown code is here */
switch (state)
{
case 0;
return 0; /* <- normal program ending */
case 1:
cerr << "Undefined state encountered" << endl;
return 1;
}
I agree itās ugly but the code that generates it is much more structured than the resultant code generated. When using C++ as a backend to a non-structured language, sometimes you just have to make new rules.