Well, I’m trying to write a C Compiler without a few features like structs, and for the most part I’m failing, but I only started a few weeks ago, so I’m still in the somewhat early stages.
Basically the idea is that I have something of a tree. At the top level I have a class called namespaces. At the beginning the program finds how many namespaces there are, and calls the constructor call for namespaces (for each namespace). The constructor’s arguement is a string, and the constructor basically takes apart that string and looks for while loops and for loops. It calls the constructor for the Loop object, and for any statements, etc. that are in the namespace. The constructor of the loop objects calls the constructor of the conditional class, the constructor of the conditional class calls the constructors of the statements classes, etc.
Then there is a class which breaks apart that object, in a repetitive, iterative, and sometimes recursive set of operations, and converts it into binary.
I am having a few problems though:
basically the code works by converting
int x = 3
int y = x + 2;
int* z = &y;
to
;given n is defined beforehand as a memory location
mov dx, n
mov [dx], 3
mov eax, [dx]
mov [dx + 1], eax
add [dx + 1], 2
mov eax, dx
add eax, 1
mov [dx + 2], eax
But my code doesn’t work. WHY!!!
WHAT DO YOU MEAN INVALID OPERAND SIZE?!?
C
Clearly, if this C compiler is ever going to defeat Visual Studio, I’m going to need to sharpen my assembly skills. But that’s why I’m writing a compiler.
Write a compiler-> learn assembly -> Write a compiler
Checkmate!