YAB compiler

After my day job went away, I got a few hours to work on my YAB to C++ Transpiler. The most obvious bug just got fixed:

if(v2<k31)state=33;
break;

is not the same as

if(v2<k31){state=33; break;}

and now that I fixed it it seems like a major facepalm that I missed something so obvious when I wrote it.

Maybe I can make some more headway these next few weeks.

9 Likes

6 posts were split to a new topic: Localization for yab

@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.

Done, i have split the discussion now

2 Likes

One of my first thoughts when yab came out was to have this possibility. Convert and compile a yab source code as c or c++ code.

Thank you for your work so far

4 Likes

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.

5 Likes

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.

4 Likes

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++?

Edit

I forgot to post the link to my project on GitHub.

no idea, but ut can be trained

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.

Greetings
Peter

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.

Is the Transpiler already ready for testing?

Greetings
Peter

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.

7 Likes

You could use BString and avoid STL probably.

I would honestly suggest:

if(v2<k31)
{
  state=33;
}
break;

Or

if(v2<k31)
{
  state=33;
  break;
} 

It is more long winded, but you will never make a mistake like that again if you use the braces to force scope.

2 Likes

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).

Greetings
Peter

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.