Nand To Tetris (Jack): Simple if conditional with equality testing giving this error - “Expected - or ~ or ( in term”

纵饮孤独 提交于 2019-12-12 04:25:02

问题


class Main {
  function void main() {
    var String foo;
    let foo = "bar";

    if (foo == "bar") {
      do Output.printString("true");
    }
    else {
      do Output.printString("false");
    }

    return;
  }
}

I get the error: Expected - or ~ or ( in term.

Full output:

code/nand2tetris » tools/JackCompiler.sh projects/09/Test
Compiling /Users/adamzerner/code/nand2tetris/projects/09/Test
In Main.jack (line 6): In subroutine main: Expected - or ~ or ( in term
code/nand2tetris »

What does the error mean?


回答1:


The issue is that I used == instead of =. In Jack, equality is tested for using a single equals rather than a double or triple (double/triple equals is conventional in other languages).

See bullet point 7 in slide 22 of the Chapter 9 PDF for documentation saying that equality comparison is done with a single equals.

See SquareGame.jack line 40 in the course software for an example.

The following code compiles without error. It doesn't give the expected output, but the reason for that is a separate topic.

class Main {
  function void main() {
    var String foo;
    let foo = "bar";

    if (foo = "bar") {
      do Output.printString("true");
    }
    else {
      do Output.printString("false");
    }

    return;
  }
}


来源:https://stackoverflow.com/questions/41433103/nand-to-tetris-jack-simple-if-conditional-with-equality-testing-giving-this-e

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!