For a school project, the goal is to build a compiler and although I am done all other requirements, I just cannot seem to get the comparison operator working for strings. It works fine for Integers, Floats, Characters, and Booleans but falters when dealing with strings. Without posting my entire code, I will tell you that the arguments of the variable are outputting the correct values.
For ex. this is within my own compiler:
var test := "hello" == "hi";
The arguments that are within my function are Tokens that have the values of hello and hi respectively.
if(type == type.STRING) {
if(operator == Punctuator.Equal) { // the operator checks which operator is used which in this case is "==", that is what Punctuator.Equal is equal to.
code.add(JumpTrue);
code.add(Jump);
}
// operator for when '!='
}
So what this code is doing is looking if the arguments are Strings and the operator is equal to '=='. Then it is using the compiler code to convert to machine language which I will post a snippet below.
Jump, // takes a string operand, branches to statement with that label.
JumpFalse, // takes a string operand. Pops top value (integer) from stack, does Jump if value=0
JumpTrue, // takes a string operand. Pops the top (integer) and Jumps if it is not 0.
JumpNeg, // takes a string operand. Pops the top (integer) and Jumps if it is negative.
JumpPos, // takes a string operand. Pops the top (integer) and Jumps if it is positive.
JumpFNeg, // takes a string operand. Pops the top (floating) and Jumps if it is negative.
JumpFPos, // takes a string operand. Pops the top (floating) and Jumps if it is positive.
JumpFZero, // takes a string operand. Pops the top (floating) and Jumps if it is zero.
Call, // takes a string operand. Jumps to that location, and pushes return instruction location.
JumpV, // [... addr] -> [...] Branches to addr.
CallV, // [... addr] -> [...] Jumps to addr, and pushes return instruction location.
If anyone can see where my mistake may lay, please give me some hints so that I can patch it up.
I believe that the "code.add(JumpTrue)" may not work considering that I am dealing with Strings but I do not know what else to use.
Edit: I am NOT using the Java compiler to compile my code. This is being compiled by my own compiler which allows the "==" comparison by Strings.