Skip to content

Commit 8a6d3da

Browse files
authored
Merge pull request #30 from PetrPytelka/empty_value
Support for empty value / uninitialized variable
2 parents 7a9facf + 6786844 commit 8a6d3da

File tree

12 files changed

+68
-7
lines changed

12 files changed

+68
-7
lines changed

src/main/java/com/scriptbasic/executors/operators/AbstractCompareOperator.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ protected RightValue evaluateOn(final RightValue leftOperand,
6868
}
6969
}
7070
}
71+
if (leftOperand == BasicEmptyValue.EMPTY_VALUE && rightOperand == BasicEmptyValue.EMPTY_VALUE) {
72+
return new BasicBooleanValue(compareTo(BasicEmptyValue.EMPTY_VALUE.getNumericValue(),
73+
BasicEmptyValue.EMPTY_VALUE.getNumericValue()));
74+
}
7175
throw new BasicRuntimeException("Type mismatch, left operand: " + leftOperand +
7276
", right operand: " + rightOperand);
7377
}

src/main/java/com/scriptbasic/executors/rightvalues/BasicBooleanValue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ private static Boolean convertNumeric(
2727
public static Boolean asBoolean(final RightValue originalValue) throws BasicRuntimeException {
2828
final Boolean convertedValue;
2929

30-
if (originalValue == null) {
30+
if (originalValue == null || originalValue == BasicEmptyValue.EMPTY_VALUE) {
3131
convertedValue = Boolean.FALSE;
3232
} else if (originalValue instanceof AbstractNumericRightValue) {
3333
convertedValue = convertNumeric((AbstractNumericRightValue) originalValue);

src/main/java/com/scriptbasic/executors/rightvalues/BasicDoubleValue.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public static Double asDouble(final RightValue rv)
5252
// TODO elaborate the conversion with other object classes, like
5353
// Long, String...
5454
}
55+
if (rv == BasicEmptyValue.EMPTY_VALUE) {
56+
return 0.0;
57+
}
5558
throw new BasicRuntimeException("Can not convert value to double");
5659
}
5760

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.scriptbasic.executors.rightvalues;
2+
3+
public class BasicEmptyValue extends AbstractNumericRightValue<Long, EmptyValue> {
4+
5+
public static final BasicEmptyValue EMPTY_VALUE = new BasicEmptyValue();
6+
7+
private BasicEmptyValue() {
8+
setValue(EmptyValue.EMPTY_VALUE);
9+
}
10+
11+
@Override
12+
public Long getNumericValue() {
13+
return 0L;
14+
}
15+
16+
}

src/main/java/com/scriptbasic/executors/rightvalues/BasicLongValue.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ public static Long asLong(final RightValue rv)
4848
// TODO elaborate the conversion with other object classes, like
4949
// Double, String...
5050
}
51+
if (rv == BasicEmptyValue.EMPTY_VALUE) {
52+
return 0L;
53+
}
5154
throw new BasicRuntimeException("Can not convert value to long");
5255
}
5356

src/main/java/com/scriptbasic/executors/rightvalues/BasicStringValue.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ public BasicStringValue(final String s) {
1212
public static String asString(final RightValue rv) throws BasicRuntimeException {
1313
try {
1414
final String resultString;
15+
if (rv == BasicEmptyValue.EMPTY_VALUE) {
16+
return "";
17+
}
1518
if (rv == null
1619
|| ((AbstractPrimitiveRightValue<Object>) rv).getValue() == null) {
1720
resultString = "undef";
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.scriptbasic.executors.rightvalues;
2+
3+
public class EmptyValue {
4+
5+
public static EmptyValue EMPTY_VALUE = new EmptyValue();
6+
7+
private EmptyValue() {
8+
}
9+
10+
@Override
11+
public String toString() {
12+
return "";
13+
}
14+
}

src/main/java/com/scriptbasic/executors/rightvalues/VariableAccess.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ public RightValue evaluate(final Interpreter interpreter)
1313
throws ScriptBasicException {
1414
final VariableMap variableMap = interpreter.getVariables();
1515
RightValue value = variableMap.getVariableValue(getVariableName());
16+
if (value == null) {
17+
value = BasicEmptyValue.EMPTY_VALUE;
18+
}
1619
value = interpreter.getHook().variableRead(getVariableName(), value);
1720
return value;
1821
}

src/main/java/com/scriptbasic/utility/RightValueUtility.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ public static RightValue createRightValue(final Object value) {
8080
new char[]{(Character) value}));
8181
} else if (value instanceof Boolean) {
8282
rightValue = new BasicBooleanValue((Boolean) value);
83+
} else if (value == EmptyValue.EMPTY_VALUE) {
84+
rightValue = BasicEmptyValue.EMPTY_VALUE;
8385
} else {
8486
rightValue = new BasicJavaObjectValue(value);
8587
}

src/test/java/com/scriptbasic/TestEngine.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import static org.junit.jupiter.api.Assertions.assertAll;
2323
import static org.junit.jupiter.api.Assertions.assertEquals;
24+
import static org.junit.jupiter.api.Assertions.assertNotNull;
2425
import static org.junit.jupiter.api.Assertions.assertNull;
2526
import static org.junit.jupiter.api.Assertions.assertThrows;
2627
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -514,7 +515,7 @@ public void testSubroutineCallWArgumentsWRetval2() throws Exception {
514515
final var engine = ScriptBasic.engine();
515516
engine.eval("sub applePie(b,c)\nglobal a\na = c\nreturn 6\nEndSub");
516517
engine.subroutine(Long.class, "applePie").call("hello world");
517-
assertNull(engine.variable(String.class, "a"));
518+
assertNotNull(engine.variable(Object.class, "a"));
518519
}
519520

520521
@Test
@@ -578,7 +579,7 @@ public void testSubroutineCallWArgumentsWRetval2007() throws Exception {
578579
"EndSub");
579580
final var sub = engine.subroutine(null, "applePie");
580581
sub.call("hello world");
581-
assertGlobalVariableIsNotDefined(engine, "a");
582+
assertNotNull(engine.variable(Object.class, "a"));
582583
}
583584

584585
@Test
@@ -599,7 +600,7 @@ public void testSubroutineCallWArgumentsWRetval007() throws Exception {
599600
}
600601

601602
private void assertGlobalVariableIsNotDefined(final ScriptBasic engine, final String name) throws ScriptBasicException {
602-
assertNull(engine.variable(Object.class, "a"));
603+
assertNull(engine.variable(Object.class, name));
603604
}
604605

605606
@Test

0 commit comments

Comments
 (0)