@ -0,0 +1,35 @@ | |||||
@(import("boot.mc")) | |||||
@{ | |||||
classCopy(class) { | |||||
{values: clone(class.values), functions: clone(class.functions)}; | |||||
} | |||||
initClass(class) { | |||||
out = {}; | |||||
if (class == null) { | |||||
out.values = {}; | |||||
out.functions = {}; | |||||
} | |||||
out; | |||||
} | |||||
new(class, param) { | |||||
k = 0; | |||||
out = initClass(); | |||||
for (i in class.values) { | |||||
out.values[i] = clone(param[i]); | |||||
k++; | |||||
} | |||||
for (i in class) { | |||||
if (i != keys(class)[0] ) { | |||||
out[i] = clone(class[i]); | |||||
} | |||||
} | |||||
return out; | |||||
} | |||||
nil; | |||||
} |
@ -0,0 +1,42 @@ | |||||
@(import("class.mc")) | |||||
@{ | |||||
//creation of the class | |||||
P = initClass(); | |||||
P.values.x = "null"; | |||||
P.values.y = "null"; | |||||
P.values.z = "null"; | |||||
P.functions.printf = "null"; | |||||
printf(p) { // Can we create a self variable or somthing like that ? | |||||
print("Object P : "); | |||||
print(p); | |||||
print("\n"); | |||||
} | |||||
P.functions.printf = printf; | |||||
// To know if the parameter is known | |||||
print("\n"); | |||||
(P.values.x != null) ? print("yes\n") : print("no\n"); | |||||
(P.values.w != null) ? print("yes\n") : print("no\n"); | |||||
print("\n"); | |||||
// Use of the P class | |||||
x = new(P, {x: 2, y: 4, z: 6 }); | |||||
x.functions.printf(x); | |||||
// Create another class from P | |||||
Q = classCopy(P); | |||||
Q.values.q = "null"; | |||||
printf(q) { | |||||
print("Object Q : "); | |||||
print(q); | |||||
print("\n"); | |||||
} | |||||
Q.functions.printf = printf; | |||||
// Use of the Q class | |||||
y = new(Q, {x: 1, y: 2, z: 3, q: 42 }); | |||||
P.functions.printf(P); | |||||
nil; | |||||
} |
@ -0,0 +1,47 @@ | |||||
@(import("class.mc")) | |||||
@{ | |||||
//creation of the object class | |||||
Object = initClass(); | |||||
Object.type = "object"; | |||||
type_t = { type_Nil: 0, type_Int: 1, type_Pair: 2}; | |||||
printf(x) { | |||||
switch (x.type) { | |||||
case type_t.type_Int: { | |||||
print(x.values.value); | |||||
return; | |||||
} | |||||
case type_t.type_Pair: { | |||||
print("("); | |||||
printf(x.values.a); | |||||
print(", "); | |||||
printf(x.values.d); | |||||
print(")"); | |||||
return; | |||||
} | |||||
} | |||||
} | |||||
Object.functions.printf = printf; | |||||
// from object | |||||
Nil = classCopy(Object); | |||||
Nil.type = type_t.type_Nil; | |||||
Int = classCopy(Object); | |||||
Int.values.value = "null"; | |||||
Int.type = type_t.type_Int; | |||||
Pair = classCopy(Object); | |||||
Pair.values.a = "null"; | |||||
Pair.values.d = "null"; | |||||
Pair.type = type_t.type_Pair; | |||||
// function to simplify the writting of example | |||||
newNil(x) { new(Nil, {}); } | |||||
newInt(x) { new(Int, {value: x}); } | |||||
newPair(x, y) { new(Pair, { a: x, d: y }); } | |||||
// example | |||||
l = newPair(newInt(1), newPair(newInt(2), newPair(newInt(3), newNil()))) | |||||
l.functions.printf(l); | |||||
nil; | |||||
} |