Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ protected RightValue evaluateOn(final RightValue leftOperand,
}
}
}
if (leftOperand == BasicEmptyValue.EMPTY_VALUE && rightOperand == BasicEmptyValue.EMPTY_VALUE) {
return new BasicBooleanValue(compareTo(BasicEmptyValue.EMPTY_VALUE.getNumericValue(),
BasicEmptyValue.EMPTY_VALUE.getNumericValue()));
}
throw new BasicRuntimeException("Type mismatch, left operand: " + leftOperand +
", right operand: " + rightOperand);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private static Boolean convertNumeric(
public static Boolean asBoolean(final RightValue originalValue) throws BasicRuntimeException {
final Boolean convertedValue;

if (originalValue == null) {
if (originalValue == null || originalValue == BasicEmptyValue.EMPTY_VALUE) {
convertedValue = Boolean.FALSE;
} else if (originalValue instanceof AbstractNumericRightValue) {
convertedValue = convertNumeric((AbstractNumericRightValue) originalValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public static Double asDouble(final RightValue rv)
// TODO elaborate the conversion with other object classes, like
// Long, String...
}
if (rv == BasicEmptyValue.EMPTY_VALUE) {
return 0.0;
}
throw new BasicRuntimeException("Can not convert value to double");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.scriptbasic.executors.rightvalues;

public class BasicEmptyValue extends AbstractNumericRightValue<Long, EmptyValue> {

public static final BasicEmptyValue EMPTY_VALUE = new BasicEmptyValue();

private BasicEmptyValue() {
setValue(EmptyValue.EMPTY_VALUE);
}

@Override
public Long getNumericValue() {
return 0L;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public static Long asLong(final RightValue rv)
// TODO elaborate the conversion with other object classes, like
// Double, String...
}
if (rv == BasicEmptyValue.EMPTY_VALUE) {
return 0L;
}
throw new BasicRuntimeException("Can not convert value to long");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public BasicStringValue(final String s) {
public static String asString(final RightValue rv) throws BasicRuntimeException {
try {
final String resultString;
if (rv == BasicEmptyValue.EMPTY_VALUE) {
return "";
}
if (rv == null
|| ((AbstractPrimitiveRightValue<Object>) rv).getValue() == null) {
resultString = "undef";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.scriptbasic.executors.rightvalues;

public class EmptyValue {

public static EmptyValue EMPTY_VALUE = new EmptyValue();

private EmptyValue() {
}

@Override
public String toString() {
return "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public RightValue evaluate(final Interpreter interpreter)
throws ScriptBasicException {
final VariableMap variableMap = interpreter.getVariables();
RightValue value = variableMap.getVariableValue(getVariableName());
if (value == null) {
value = BasicEmptyValue.EMPTY_VALUE;
}
value = interpreter.getHook().variableRead(getVariableName(), value);
return value;
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/scriptbasic/utility/RightValueUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public static RightValue createRightValue(final Object value) {
new char[]{(Character) value}));
} else if (value instanceof Boolean) {
rightValue = new BasicBooleanValue((Boolean) value);
} else if (value == EmptyValue.EMPTY_VALUE) {
rightValue = BasicEmptyValue.EMPTY_VALUE;
} else {
rightValue = new BasicJavaObjectValue(value);
}
Expand Down
7 changes: 4 additions & 3 deletions src/test/java/com/scriptbasic/TestEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -514,7 +515,7 @@ public void testSubroutineCallWArgumentsWRetval2() throws Exception {
final var engine = ScriptBasic.engine();
engine.eval("sub applePie(b,c)\nglobal a\na = c\nreturn 6\nEndSub");
engine.subroutine(Long.class, "applePie").call("hello world");
assertNull(engine.variable(String.class, "a"));
assertNotNull(engine.variable(Object.class, "a"));
}

@Test
Expand Down Expand Up @@ -578,7 +579,7 @@ public void testSubroutineCallWArgumentsWRetval2007() throws Exception {
"EndSub");
final var sub = engine.subroutine(null, "applePie");
sub.call("hello world");
assertGlobalVariableIsNotDefined(engine, "a");
assertNotNull(engine.variable(Object.class, "a"));
}

@Test
Expand All @@ -599,7 +600,7 @@ public void testSubroutineCallWArgumentsWRetval007() throws Exception {
}

private void assertGlobalVariableIsNotDefined(final ScriptBasic engine, final String name) throws ScriptBasicException {
assertNull(engine.variable(Object.class, "a"));
assertNull(engine.variable(Object.class, name));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,18 @@ public void testOperators() throws AnalysisException,
a("a= 3.0 <> \"a\"", true);
a("a= true = \"a\"", false);

// Comparison with uninitialized/empty variable
a("a= b = \"\"", true);
a("a= b = 0", true);
a("a= b = \"x\"", false);
a("a= b = 1", false);
a("a= b = c", true);
a("a= b <> c", false);
a("a= b < c", false);
a("a= b > c", false);
a("a= b <= c", true);
a("a= b >= c", true);

a("a= \"x\" <> \"x\"", false);
a("a= \"x\" <> \"y\"", true);
a("a= \"x\" < \"y\"", true);
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/com/scriptbasic/testprograms/TestPrograms.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void testPrograms() throws Exception {
}
codeTest("TestSub1.bas", "6");
codeTest("TestSub2.bas", "21");
codeTest("TestSub3.bas", "21undef");
codeTest("TestSub3.bas", "21");
codeTest("TestSub4.bas", "123\n123\n123\n123\n");
codeTest("TestSub5.bas", "1111");
codeTest("TestWhile1.bas", "89");
Expand Down Expand Up @@ -126,7 +126,7 @@ public void testPrograms() throws Exception {
codeTest("TestForLoop9.bas", "111213212223313233");
codeTest("TestForLoop10.bas", "12323");
codeTest("TestRuntimeFunction.bas", "1.01.5707963267948966");
codeTest("TestNullFunction.bas", "undefundef");
codeTest("TestNullFunction.bas", "undef");
codeTest("TestMethodCall.bas",
"" + Math.sin(1.0) + "\n" + Math.sin(1.0));
map = new HashMap<>();
Expand All @@ -135,7 +135,7 @@ public void testPrograms() throws Exception {

testRuntimeFail("TestBadArray1.bas");
codeTest("TestBadArray2.bas", "");
codeTest("TestBadArray3.bas", "");
testRuntimeFail("TestBadArray3.bas");
codeTest("BubbleSort.bas", "-1\n0\n2\n3\n7\n58\n99\n");
codeTest("TestAbs.bas", "131355.377.7");
codeTest("TestChomp.bas", "ttt");
Expand Down