Metamorphing Machine I rather be this walking metamorphosis
than having that old formed opinion about everything!

From comic book to compiler

I am a Marvel fan. I've been a fan since 1979 when I first read a Captain America's comic book. It happens to be my favorite Marvel's character for quite some time now.
At that time - and through the 2010s - my young brother and I would buy comic books paying 50/50 each.
(By the way, we would not read Marvel only. We read DC, Vertigo, Image, and pretty much everything we could put our hands on.)
Over time our collection grew. I would like to have it accounted somehow, but it would not be until the 2000s that I had my first try at building an Access database.
That's when I was introduced to VBA, a subset of Visual Basic 6 language. I taught myself VBA and SQL, just so I could have a database of my beloved comic books.

While coding my butt out to have a half-baked application, I relied heavily on VBA's help. I've used some functions that it stated were not recommended because they might be removed in the next versions. I was like "Nah... I need them." And then came a new Access version. And my application would not work. And I would need to discover how to replace them in a hurry. Not funny. I learned a valuable - although painful - lesson there: Keep your code up to date.

Up to then, I was a hobbyist programmer. I vividly remember buying a magazine that came with a free CD full of sample programs. Among them, there was Visual Studio 6.
I installed it, opened it, and dragged a button to a form. "Behind" the button, I've written a message box command saying something like "Hello world!"
Hit F5, my program started and I clicked that button. Magic happened! It was love at first sight (or maybe second, first being VBA?)

Then, by the end of 2001, I've lost my job. I didn't see it coming. I was also an unqualified worker. I didn't have college instruction by then.
Fortunately, the future-husband-of-my-brother's-friend was starting a software company and got a good impression of me in a previous meeting. He came to my home to install what he thought would be games while I asked him to install Visual Studio instead.

His company had an ERP software built using VB6, SQL Server, and Crystal reports. Pretty standard choices for that time. I started working to them (the company had three owners.)
About that time I started to read about that brand new Microsoft thing: .NET. Having been bitten by the soon-to-be-deprecated thing before, I started to warn my employers that they needed to start using it.

But none of us was prepared for the SNAFU that followed.

To explain it thoroughly, it would need a series of posts by its own, so I will only hint some heavy-optionated highlights:
So we, VB6 programmers, complained. And waited. And nothing happened. It's been around 20 years now, and the situation is the same.
Microsoft had to extend its support to VB6 from 2008 to... well, basically forever because of that: Massive use and no viable way to update to .NET.

I perused every. single. VB6. alternative. I. could. find. Nothing was good enough. For a language that was released in 1998, it set a pretty high bar, even for today's standards.

Back to my life, I went to college in 2005 and graduated in 2006. But even there I have never had any heavy subjects, like compiler theory or big O notation. I am essentially a LOB programmer. But I started thinking of building my compiler, and it has to have something to do with VB. I just love the language, warts and all.

I tried to build it using VB6 itself.
Then I tried C. Then C++. Then Javascript. Then Go. Then VB.NET.
I even tried some things like HLA (High-Level Assembly) and GOLD Parser Engine. I hit stumbling blocks on all of these attempts. I also learned a lot.

For instance, I had to acknowledge I didn't have a fraking clue about how to parse expressions (things like "5 + a * 3 ^ b".)
There are a plethora of ways to approach it (parser combinators, shunting-yard algorithm, etc.), but if I can't understand it well enough to bend it to my needs, then it's not going to happen.

Also, simply converting a float value to string is a much more complex problem than I ever dreamed.

But I digress. My half-thought plan to build a compiler is: I never passed the second bullet point.

I said compiler, but I should have said transpiler. I intend to translate VB6 to code to... something else. The only flexible enough language I can think of is C: It has go-to (I'm looking at you, Go), you can pass parameters by reference (and looking at you, Javascript), and have plenty of opportunities to shoot one in the foot. But in C you have to do everything by hand. Including classes and Dictionary, and VB6 does have both. It's hard to create them from zero.

Also, I would be happy enough to compile a simple console application.
GUI programming is another can of worms altogether. It is not included in the plan delineated above.

Now, for the hard parts:
VB6 has 22 binary operators 1. I'd like to add at least six more 2. And need to handle three syntax elements 3 as they were binary operators. We are talking about 31 operators.
VB6 has also 16 data types 4. I'd like to add two more 5.

Now, if we combine all those data types with all those binary operators, we have to deal with (31 × 16 × 31) 15,376 situations in expressions. Because a Long divided by a Currency yields a Double. And a Null concatenated to a Boolean yields a String. How in the world one manage to write and test over 15,000 functions? What if I want to introduce non-signed integers??? This is a problem I am not able to solve.

Another issue is the "standard library" - not that it is called like that in classical VB. I think it does not even have a name...
Anyway, it requires around 210 functions.

I would like my compiler to be able to compile programs for any operating system (transpiled to C, remember?) But some of these functions are Windows-specific, and it's hard to port it to other OSes. But this is not the main problem. I suppose we could simply omit those functions that cannot be ported.

The problem arises when trying to implement the language in the language itself - as I tried. Sometimes I use a function in the implementation of another function, but the later also uses the former, causing a stack overflow when the unhappy path is followed.

How one can avoid it? No amount of care survives implementing more than 200 functions.
So I'm stuck at it too.

I almost forgot to say: The standard library is being written in VB6. Parser and stuff in VB.NET. The intention is to rewrite it in the language itself when it is "good enough."

Those are the bigger questions. But I have slightly smaller ones:

I implemented the SendKeys function. My .NET version works flawlessly.
My VB6's version, don't. I've used PostMessage, keybd_event, and SendInput. I've downloaded supposedly VB6 working projects. It doesn't matter. It does not work.

Also, my CallByName implementation crashes when passing parameters. No clue why and I've already run out of ideas.

Due to the problems mentioned above, I've stalled for the last months.
Still trying to figure out how to solve them.

Andrej Biasic
2020-02-12
1 And, Eqv, Imp, Is, Like, Mod, Or, TypeOf, Xor, +, -, *, /, \, ^, <, <=, =, >=, >, <>, &
2 AndAlso, OrElse, IsNot, <<, >>, >>>
3 :=, ., !
4 Boolean, Byte, Integer, Error, Long, Currency, Decimal, Single, Double, Date, String, Object, Variant, Empty, Nothing, Null
5 LongLong, LongPtr