Переглянути джерело

First working prototype for class creation

develop-theo
Theo Souchon 2 роки тому
джерело
коміт
df3cf0bb80
4 змінених файлів з 127 додано та 1 видалено
  1. +3
    -1
      ccmeta.leg
  2. +35
    -0
      class.mc
  3. +42
    -0
      tests-parsimony/testFile.c
  4. +47
    -0
      tests-parsimony/testFile2.c

+ 3
- 1
ccmeta.leg Переглянути файл

@ -2413,7 +2413,7 @@ MO_STRING = 'string' ![(a-zA-Z0-9_] --
#--------------------------------------------- Meta rules ----------------------------------------------#
META_NIL = 'nil' ![a-zA-Z0-9_] --
META_NIL = 'nil' ![a-zA-Z0-9_] --
#META_FUN = 'fun' ![a-zA-Z0-9_] --
#META_SYNTAX = 'syntax' ![a-zA-Z0-9_] --
META_VAR = 'var' ![a-zA-Z0-9_] --
@ -3640,6 +3640,8 @@ void outputNode(oop node)
case Symbol:
outputText(get(node, Symbol, name));
return;
case Function:
return;
default:
fprintf(stderr, "\noutputNode: unknown node type %i\n", getType(node));
abort();

+ 35
- 0
class.mc Переглянути файл

@ -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;
}

+ 42
- 0
tests-parsimony/testFile.c Переглянути файл

@ -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;
}

+ 47
- 0
tests-parsimony/testFile2.c Переглянути файл

@ -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;
}

Завантаження…
Відмінити
Зберегти