Commit 876e19ba authored by Leon Arnott's avatar Leon Arnott
Browse files

Fixed variables in comparisons to the right of 'to' being compiled to VarRefs.

This caused various expressions like (set: $a to $a < $y) to break. Fixes #68. Fixes #69.
parent 4ff8ef000066
......@@ -702,6 +702,15 @@ define(['utils'], ({toJSLiteral, impossible}) => {
compile those next to it.
*/
if (i > -1) {
/*
If two variables are being compared (such as $a < $y or $b contains $c)
and the comparison operation is in potential VarRef position
(such as (set: $x to $a < $y)) then don't compile the variables into VarRefs
(because comparison operations can't, and shouldn't, process VarRefs instead of values.)
*/
if (operation) {
isVarRef = false;
}
/*
Any of the comparisons above could have provided specific
values for left and right, but usually they will just be
......
......@@ -133,6 +133,17 @@ describe("twinescript operators", function () {
expect("(print: -10 is not >= 1)").markupToPrint("true");
expect("(print: 1 is not >= 1)").markupToPrint("false");
});
it("has correct order of operations with 'to' and 'into'", function (){
expect("(set: $a to 1 < 2)(print:$a)").markupToPrint("true");
expect("(put: 1 < 2 into $a)(print:$a)").markupToPrint("true");
expect("(set: $a to 4 < 3)(print:$a)").markupToPrint("false");
expect("(put: 4 < 3 into $a)(print:$a)").markupToPrint("false");
});
it("can compare variables as the subject of 'to' and 'into'", function (){
runPassage("(set:$a to 1)");
expect("(set: $b to $a < $a)(print:$b)").markupToPrint("false");
expect("(put: $a < $a into $b)(print:$b)").markupToPrint("false");
});
});
describe("the > operator", function () {
it("performs 'less than' on two numbers", function (){
......@@ -158,6 +169,17 @@ describe("twinescript operators", function () {
expect("(print: -10 is not <= 1)").markupToPrint("false");
expect("(print: 1 is not <= 1)").markupToPrint("false");
});
it("has correct order of operations with 'to' and 'into'", function (){
expect("(set: $a to 1 > 2)(print:$a)").markupToPrint("false");
expect("(put: 1 > 2 into $a)(print:$a)").markupToPrint("false");
expect("(set: $a to 4 > 3)(print:$a)").markupToPrint("true");
expect("(put: 4 > 3 into $a)(print:$a)").markupToPrint("true");
});
it("can compare variables as the subject of 'to' and 'into'", function (){
runPassage("(set:$a to 1)");
expect("(set: $b to $a > $a)(print:$b)").markupToPrint("false");
expect("(put: $a > $a into $b)(print:$b)").markupToPrint("false");
});
});
describe("the <= operator", function () {
it("performs 'less than' on two numbers", function (){
......@@ -183,6 +205,17 @@ describe("twinescript operators", function () {
expect("(print: -10 is not > 1)").markupToPrint("true");
expect("(print: 1 is not > 1)").markupToPrint("true");
});
it("has correct order of operations with 'to' and 'into'", function (){
expect("(set: $a to 1 <= 2)(print:$a)").markupToPrint("true");
expect("(put: 1 <= 2 into $a)(print:$a)").markupToPrint("true");
expect("(set: $a to 4 <= 3)(print:$a)").markupToPrint("false");
expect("(put: 4 <= 3 into $a)(print:$a)").markupToPrint("false");
});
it("can compare variables as the subject of 'to' and 'into'", function (){
runPassage("(set:$a to 1)");
expect("(set: $b to $a <= $a)(print:$b)").markupToPrint("true");
expect("(put: $a <= $a into $b)(print:$b)").markupToPrint("true");
});
});
describe("the >= operator", function () {
it("performs 'less than' on two numbers", function (){
......@@ -208,6 +241,17 @@ describe("twinescript operators", function () {
expect("(print: -10 is not < 1)").markupToPrint("false");
expect("(print: 1 is not < 1)").markupToPrint("true");
});
it("has correct order of operations with 'to' and 'into'", function (){
expect("(set: $a to 1 >= 2)(print:$a)").markupToPrint("false");
expect("(put: 1 >= 2 into $a)(print:$a)").markupToPrint("false");
expect("(set: $a to 4 >= 3)(print:$a)").markupToPrint("true");
expect("(put: 4 >= 3 into $a)(print:$a)").markupToPrint("true");
});
it("can compare variables as the subject of 'to' and 'into'", function (){
runPassage("(set:$a to 1)");
expect("(set: $b to $a >= $a)(print:$b)").markupToPrint("true");
expect("(put: $a >= $a into $b)(print:$b)").markupToPrint("true");
});
});
describe("the 'and' operator", function () {
it("ANDs booleans", function (){
......
......@@ -20,6 +20,7 @@ describe("game state macros", function() {
it("passage datamaps have tags", function (){
createPassage("","The Kitchen", ["area"]);
expect("(print: (passage: 'The Kitchen')'s tags contains 'area')").markupToPrint("true");
expect("(set: $x to (passage: 'The Kitchen')'s tags contains 'area')(print:$x)").markupToPrint("true");
});
it("doesn't pass data by reference", function (){
createPassage("","The Kitchen");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment