1

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
QQCompi
  • 225
  • 4
  • 14
  • where are you comparing the strings? You have to use .equals for string comparison and not the '==' operator – pelumi Oct 06 '15 at 04:49
  • Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Rahul Tripathi Oct 06 '15 at 04:52
  • Just to be clear (for everybody else), I'm not convinced that this is a pure `String == String` problem, as they seem to be implementing their own machine language/compiler, so the input is `var test := "hello" == "hi";` and then the code is suppose to "compile" the machine language instructions – MadProgrammer Oct 06 '15 at 04:55
  • @MadProgrammer You are right, sorry for the confusion. – QQCompi Oct 06 '15 at 04:57
  • @MadProgrammer i think what is happening is that the input is being 'compiled' to an euivalent Java statment where they are also using the '==' operator instead of the .equals. There is a possibility that's why he suggests it works for intergers. The question then becomes where is the implementation that 'interprets' the '==' operator? – pelumi Oct 06 '15 at 04:57
  • @pelumi That's possibly true, then the question should be closed for not providing a runnable example, not because it "might" use `==` to compare `String` internally – MadProgrammer Oct 06 '15 at 04:58
  • As of my understanding the input is `var test := "hello" == "hi";`, but they are using `Java` to convert that input in some sort of machine language. So it seems to be a `String == String` problem to me. – Ashraf Purno Oct 06 '15 at 04:59
  • @pelumi the java compiler does not see the string == string, that is handed off to my own compiler – QQCompi Oct 06 '15 at 04:59
  • @AshrafPurno the var test := "hello" == "hi" is within a file that my own compiler detects and runs. All java is doing is detecting that there are two strings and a '==' operator, it is doing nothing with them. The implementation is handed off to my compiler. – QQCompi Oct 06 '15 at 05:01
  • So how do you implement the comparison in your compiler which is built in Java? – pelumi Oct 06 '15 at 05:02
  • @QQCompi `if(operator == Punctuator.Equal)`, what are the types of `operator` and `Punctuator.Equal`? – Ashraf Purno Oct 06 '15 at 05:03
  • @pelumi that is what the code.add(value) is to do. If you look at the second code block, it shows which each variable does at a machine level. Ill edit to clear things up a bit. – QQCompi Oct 06 '15 at 05:03
  • Here is an example of the "==" operator with floats: `if(type == type.Float) { if(operator == Punctuator.Equal) { code.add(JumpTrue); code.add(Jump); } }` Sorry for the formatting but it is essentially the same as String comparison so I feel that the compiler is having issues with this. – QQCompi Oct 06 '15 at 05:08
  • Well one possibility can be to convert both the string to `Character Arrays` and then compare each entries of those arrays as you are comparing `Characters` (as you said it works for Character). Make sure to check if the strings are of same length first – Ashraf Purno Oct 06 '15 at 05:13
  • 5
    Sorry, but I can't see that there is nearly enough information here for us to provide any help. – ajb Oct 06 '15 at 05:17
  • @AshrafPurno I believe that can be an option but would require me to change a lot of the foundation of the code. I believe in the issue mainly lies within the "code.append" line, I feel I am using an argument that wont work properly with Strings. I am pushing the top off but Strings are a collection of pointers so I feel there would be a conflict. – QQCompi Oct 06 '15 at 05:19
  • Ohh, thanks for the explanation, i now have a better understanding of the problem. Where is the `code.append` line? In addition, how do you detect the `type` – pelumi Oct 06 '15 at 05:21
  • sorry I meant to say `code.add()`, I believe `Type` is detected within the parser file but I can assure you that is properly detecting the values. "hello" and "hey" are strings and "==" is an operator – QQCompi Oct 06 '15 at 05:24
  • Can you explain the meaning of the comment next to `JumpTrue`? ` `JumpTrue, // takes a string operand. Pops the top (integer) and Jumps if it is not 0.` – pelumi Oct 06 '15 at 05:37
  • A better explanation would be in the code chuck above, it explains which each value does in machine code. – QQCompi Oct 06 '15 at 05:41
  • Your 'machine language' commands seem to imply that your intepreter deals only with integers. If that's the case then your string comparison operator will need to be converted to much more complicated 'machine language' which compares each character in turn. – sprinter Oct 06 '15 at 06:07

0 Answers0