Jian
Jian

Reputation: 23

Iolang code works differently between in file and in relp

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.

Upvotes: 1

Views: 90

Answers (1)

jer
jer

Reputation: 20236

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.

Upvotes: 1

Related Questions