0

This is my code:

OperatorTable addOperator("xor", 11)

OperatorTable println

true xor := method(bool, if(bool, false, true))
false xor := method(bool, if(bool, true, false))

true xor(false)
true xor false

When I type it into relp, it works correctly. But, when I try to run it in file, true xor false works strangely.

Jian
  • 13
  • 2

1 Answers1

0

That happens because the operator table code is parsed like the rest of your code, once. Meaning you'll want to have your operator table code in a separate file if you want to use it in the file you defined it in first. Then have a call like doFile("...") in the same file as your operator table stuff.

One thing to understand about Io, its parser does not have multiple stages it goes through beyond "rewriting operators" -- meaning, if the operators are in the table at the time the file is being parsed, it'll use those precedence levels to add parenthesis wherever needed according to those rules. However, if you're defining the precedence rules in the file you want them to be used in, it will not work because Io doesn't do a second parsing phase after you manipulate the operator table.

When we were building this feature, we talked about it, but opted to keep it simple because multiple phases required additional complexity.

The REPL works the way it does because each time you hit enter, it's like a new file -- it's a new string buffer with code in it that the VM will interpret within the running context, but parse it separately.

I hope this helps. For context, I was a developer of Io for years.

jer
  • 20,094
  • 5
  • 45
  • 69
  • Thank you! Are there any active iolang comunities? – Jian Jul 16 '21 at 15:44
  • I am not sure anymore. I know there was a channel on the IRC network freenode called #io that was reasonably active for the whole time I was working with and on the language, but not sure what's active anymore. – jer Jul 16 '21 at 19:51
  • ok. I still have a question: how can I write this in file. Should I write the declarations of operator in one file, and then 'import' it in another file? If so, how to 'import' in iolang? – Jian Jul 17 '21 at 14:49
  • As I mentioned, you'll just want a file like main.io that contains your operator table declarations, and at the end, a `doFile("blah.io")` assuming the file with your code is in `blah.io` in the same directory. – jer Jul 17 '21 at 19:40