119 lines
2.3 KiB
Plaintext
119 lines
2.3 KiB
Plaintext
|
|
print("=== Type System Demo ===");
|
||
|
|
print("");
|
||
|
|
|
||
|
|
print("--- Dynamic Typing ---");
|
||
|
|
x = 42;
|
||
|
|
print("x =", x, "type:", typeof(x));
|
||
|
|
|
||
|
|
x = "hello";
|
||
|
|
print("x =", x, "type:", typeof(x));
|
||
|
|
|
||
|
|
x = {1, 2, 3};
|
||
|
|
print("x = array, type:", typeof(x));
|
||
|
|
|
||
|
|
x = null;
|
||
|
|
print("x =", x, "type:", typeof(x));
|
||
|
|
|
||
|
|
print("");
|
||
|
|
print("--- Null Defaults ---");
|
||
|
|
undeclared = null;
|
||
|
|
print("null variable:", undeclared);
|
||
|
|
|
||
|
|
intDefault = int(null);
|
||
|
|
print("int(null) =", intDefault);
|
||
|
|
|
||
|
|
strDefault = str(null);
|
||
|
|
print("str(null) = \"" + strDefault + "\"");
|
||
|
|
|
||
|
|
boolDefault = bool(null);
|
||
|
|
print("bool(null) =", boolDefault);
|
||
|
|
|
||
|
|
print("");
|
||
|
|
print("--- Type Constructors ---");
|
||
|
|
fromStr = int("123");
|
||
|
|
print("int(\"123\") =", fromStr, "type:", typeof(fromStr));
|
||
|
|
|
||
|
|
fromInt = str(456);
|
||
|
|
print("str(456) =", fromInt, "type:", typeof(fromInt));
|
||
|
|
|
||
|
|
fromFloat = int(3.14);
|
||
|
|
print("int(3.14) =", fromFloat);
|
||
|
|
|
||
|
|
print("");
|
||
|
|
print("--- Type Coercion ---");
|
||
|
|
a = "The answer is: ";
|
||
|
|
b = 42;
|
||
|
|
result = a + str(b);
|
||
|
|
print(result);
|
||
|
|
|
||
|
|
c = "10";
|
||
|
|
d = int(c) + 5;
|
||
|
|
print("\"10\" + 5 =", d);
|
||
|
|
|
||
|
|
print("");
|
||
|
|
print("--- Boolean Context ---");
|
||
|
|
print("Boolean evaluation (0 = false, nonzero = true):");
|
||
|
|
|
||
|
|
values = {0, 1, -1, 42, "", "hello"};
|
||
|
|
names = {"0", "1", "-1", "42", "\"\"", "\"hello\""};
|
||
|
|
|
||
|
|
for (i = 0; i < len(values); i++) {
|
||
|
|
if (values[i]) {
|
||
|
|
print(names[i], "-> truthy");
|
||
|
|
} else {
|
||
|
|
print(names[i], "-> falsy");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
print("");
|
||
|
|
print("--- Integer as Boolean ---");
|
||
|
|
flag = 1;
|
||
|
|
if (flag) {
|
||
|
|
print("flag (1) is truthy");
|
||
|
|
}
|
||
|
|
|
||
|
|
flag = 0;
|
||
|
|
if (flag) {
|
||
|
|
print("This won't print");
|
||
|
|
} else {
|
||
|
|
print("flag (0) is falsy");
|
||
|
|
}
|
||
|
|
|
||
|
|
print("");
|
||
|
|
print("--- Type Checking with typeof ---");
|
||
|
|
checkTypes = {42, "text", null};
|
||
|
|
typeNames = {"integer", "string", "null"};
|
||
|
|
for (i = 0; i < len(checkTypes); i++) {
|
||
|
|
print(typeNames[i], "->", typeof(checkTypes[i]));
|
||
|
|
}
|
||
|
|
|
||
|
|
class TestClass {
|
||
|
|
value = 0;
|
||
|
|
TestClass(this) {
|
||
|
|
}
|
||
|
|
}
|
||
|
|
obj = new TestClass();
|
||
|
|
print("object -> type:", typeof(obj));
|
||
|
|
|
||
|
|
print("");
|
||
|
|
print("--- Automatic Initialization ---");
|
||
|
|
class DefaultValues {
|
||
|
|
intVal = 0;
|
||
|
|
strVal = "";
|
||
|
|
boolVal = 0;
|
||
|
|
nullVal = null;
|
||
|
|
|
||
|
|
DefaultValues(this) {
|
||
|
|
}
|
||
|
|
|
||
|
|
showDefaults(this) {
|
||
|
|
print("intVal:", this.intVal);
|
||
|
|
print("strVal: \"" + this.strVal + "\"");
|
||
|
|
print("boolVal:", this.boolVal);
|
||
|
|
print("nullVal:", this.nullVal);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
defaults = new DefaultValues();
|
||
|
|
defaults.showDefaults();
|