Browse Source

fake-static-classes fixed

develop-theo
Theo Souchon 2 years ago
parent
commit
d204b3216d
5 changed files with 52 additions and 150 deletions
  1. +26
    -1
      ccmeta.leg
  2. +25
    -51
      parsimonyLibrary/fake-static-classes.mc
  3. +1
    -1
      tests-parsimony/objectExample.c
  4. +0
    -40
      tests-parsimony/testFile.c
  5. +0
    -57
      tests-parsimony/testFile2.c

+ 26
- 1
ccmeta.leg View File

@ -2203,7 +2203,7 @@ metaCatch = META_AT ( META_IMPORT s:META_STRING ";" { inputStack
)
mvalue = ( i:META_IDENT { i= newGetVariable(i) } ( a:meta_argumentList { i = (null != getSyntax(1, i)) ? apply(globals, globals, getSyntax(1, i), a, i) : newCall(i, a) }
| { i = is(Function, eval(globals, i)) == 1 ? apply(globals, globals, eval(globals, i), makeMap(), i) : i }
| { i = is(Function, eval(globals, i)) == 1 ? apply(globals, globals, eval(globals, i), makeMap(), i) : i }
)
| META_LPAREN ( i:meta_exp ) META_RPAREN
) { $$= eval(globals, i) }
@ -3150,6 +3150,31 @@ oop eval(oop scope, oop ast)
exit(1);
}
oop args = map_get(ast, args_symbol);
oop paramFunc = get(func, Function, param);
if ( paramFunc != null ) {
oop nameArgs = makeMap();
oop newArgs = makeMap();
int count = 0;
for (int i = 0; i<map_size(args); i++ ) {
oop varName = map_get(map_get(args, makeInteger(i)), intern("lhs"));
oop val = map_get(args, makeInteger(i));
if (varName != null ) {
map_set(nameArgs, varName , val);
} else {
map_set(newArgs , makeInteger(count++), val);
}
}
for (int k = 0; k<map_size(nameArgs); k++) {
oop currentKey = map_get(map_keys(nameArgs), makeInteger(k));
for (int i = 0; i<map_size(paramFunc); i++) {
oop oopI = makeInteger(i);
if (map_get(paramFunc, oopI) == currentKey) {
map_set(newArgs, oopI, map_get(nameArgs, currentKey));
}
}
}
args = newArgs;
}
if (isFalse(get(func, Function, fixed))) {
args = evalArgs(scope, args);
}

+ 25
- 51
parsimonyLibrary/fake-static-classes.mc View File

@ -12,40 +12,44 @@
program.objects = null;
beginObject(x) {
beginObject(objectName, typeName) {
program.objects = {};
program.objects.elements = {};
program.objects.last = null;
x == null ? x = "oop" : x;
program.objects.elements[length(program.objects.elements)] = x;
program.objects.last = x;
objectName == null ? objectName = "oop" : objectName;
typeName == null ? typeName = "type_t" : typeName;
program.objects.elements[length(program.objects.elements)] = objectName;
program.objects.last = objectName;
l = {};
enum = `declaration typedef enum {_} type_t;;
enum.declarators[0].identifier = typeName;
enum.semicolon.comment = getComment(" ");
append(l, enum);
program.objects.enum = treeCopy(enum);
append(l, program.objects.enum);
obj = `declaration union object;;
obj.semicolon.comment = getComment(" ");
append(l, treeCopy(obj));
type_obj = `declaration typedef union object *oop;;
type_obj.declarators[0].declarators.identifier = x;
type_obj.declarators[0].declarators.identifier = objectName;
type_obj.semicolon.comment = getComment(" ");
append(l, treeCopy(type_obj));
l;
}
addObject() {
enum = treeCopy(getEnum(program.last.name.identifier));
structName = treeCopy(getEnum(program.last.name.identifier));
l = length(program.enums.last.enumList);
if (string(program.enums.last.enumList[0].name.identifier) == "_" && l == 1) {
program.enums.last.enumList[0] = enum;
if (string(program.objects.enum.specifiers[1].enumList[0].name.identifier) == "_" && l == 1) {
program.objects.enum.specifiers[1].enumList[0] = structName;
} else {
append(program.enums.last.enumList, getComma());
append(program.enums.last.enumList, enum);
append(program.objects.enum.specifiers[1].enumList, getComma());
append(program.objects.enum.specifiers[1].enumList, structName);
}
for (i=length(program.last.declarationL); i>0; i--) {
program.last.declarationL[i] = program.last.declarationL[i-1];
};
newType = `declaration type_t type;;
newType = `declaration int type;;
newType.specifiers[0].text = program.objects.enum.declarators[0].identifier;
newType.semicolon.comment = getComment("\n ");
program.last.declarationL[0] = treeCopy(newType);
nil;
@ -98,39 +102,12 @@
treeCopy(func);
}
// makeGeneralObject(x) {
// func = `fun int makeString(int value)
// {
// oop newString = malloc(sizeof(struct String));
// newString->type = String;
// }
// ;
// func.declarators.declarators.identifier = "make" + string(x);
// func.specifiers[0].text = program.objects.last;
// star = program.structs[x].declarationL[1].declarators[0].declarators.star;
// if (star != null) {
// func.declarators.paramTypeL[0].specifiers[0].text = string(checkIdorText(program.structs[x].declarationL[1].specifiers[0])) + " *";
// func.declarators.paramTypeL[0].specifiers[0].comment = "";
// } else {
// func.declarators.paramTypeL[0].specifiers[0].text = string(checkIdorText(program.structs[x].declarationL[1].specifiers[0]));
// }
// func.compoundS.expression[0].declarators[0].lhs.identifier = "new" + string(x);
// func.compoundS.expression[0].declarators[0].rhs.paramTypeL[0].typeName.specifiers[0].name.identifier = string(x);
// func.compoundS.expression[1].expression.rhs.identifier = string(x);
// func.compoundS.expression[1].expression.lhs.lhs.identifier = "new" + string(x);
// for (i in program.structs[x].declarationL) {
// if (i == 0) { continue; }
// println(program.structs[x].declarationL[i]);
// }
// // println(func.compoundS.expression[1]);
// treeCopy(func);
// }
endObject() {
l = {};
objectUnion = `declaration union object { type_t type; };;
objectUnion = `declaration union object { int type; };;
objectUnion.specifiers[0].declarationL[0].specifiers[0].text = program.objects.enum.declarators[0].identifier;
objectUnion.semicolon.comment = getComment("\n");
obj = select(notToken, program.enums.last.enumList);
obj = select(notToken, program.objects.enum.specifiers[1].enumList);
elt = treeCopy(`declaration struct nil nil;);
for (i in obj) {
len = length(objectUnion.specifiers[0].declarationL);
@ -139,7 +116,8 @@
objectUnion.specifiers[0].declarationL[len].semicolon.comment = getComment(" ");
}
append(l, objectUnion);
isDef = `declaration int is(type_t type, int obj);;
isDef = `declaration int is(int type, int obj);;
isDef.declarators[0].paramTypeL[0].specifiers[0].text = program.objects.enum.declarators[0].identifier;
isDef.declarators[0].paramTypeL[2].specifiers[0].text = program.objects.last;
isDef.semicolon.comment = getComment("\n");
append(l, isDef);
@ -151,22 +129,18 @@
func.compoundS.expression[0].expression.paramTypeL[0].identifier = obj[i].name.identifier;
append(l, treeCopy(func));
}
func = `fun type_t getType(int ptr) { assert(ptr); return ptr->type; };
func = `fun int getType(int ptr) { assert(ptr); return ptr->type; };
func.specifiers[0].text = program.objects.enum.declarators[0].identifier;
func.declarators.paramTypeL[0].specifiers[0].text = program.objects.last;
func.compoundS.rightCurly.comment = getComment("\n");
append(l, treeCopy(func));
func = `fun int is(type_t type, int obj) { return type == getType(obj); };
func = `fun int is(int type, int obj) { return type == getType(obj); };
func.declarators.paramTypeL[0].specifiers[0].text = program.objects.enum.declarators[0].identifier;
func.declarators.paramTypeL[2].specifiers[0].text = program.objects.last;
func.compoundS.rightCurly.comment = getComment("\n");
append(l, treeCopy(func));
append(l, treeCopy(getObjectMacro()));
append(l, treeCopy(setObjectMacro()));
// if (i == 0) { continue; }
// func = getGeneralObject(program.unions.last.declarationL[i].specifiers[0].name.identifier);
// append(l, func);
// // func = makeGeneralObject(program.unions.last.declarationL[i].specifiers[0].name.identifier);
// // append(l, func);
// }
program.objects = null;
l;
}

+ 1
- 1
tests-parsimony/objectExample.c View File

@ -4,7 +4,7 @@ typedef long long int_t;
typedef long double flt_t;
@beginObject
@beginObject(typeName = "typeName", objectName = "objectName")
struct Integer {
int_t i;

+ 0
- 40
tests-parsimony/testFile.c View File

@ -1,40 +0,0 @@
@(import("parsimonyLibrary/fake-static-classes.mc"))
@{
//creation of the class
P = initClass();
P.values.x = "null";
P.values.y = "null";
printf(p) { // Can we create a self variable or somthing like that ?
print("P(", p.values.x, ", ", p.values.y, ")\n");
}
P.functions.printf = printf;
// To know if the parameter is known
(P.values.x != null) ? print("\nyes") : print("\nno");
(P.values.w != null) ? print("\nyes\n\n") : print("\nno\n\n");
// Use of the P class
// x = new(P, { x: 2, y: 4, z: 6 });
// or
newP(a, b) { new(P, { x: a, y: b }) }
x = newP(2, 4);
x.functions.printf(x);
// Create another class from P
Q = initClass(P);
Q.values.z = "null";
printf(q) {
print("Q(", q.values.x, ", ", q.values.y, ", ", q.values.z, ")\n");
}
Q.functions.printf = printf;
// Use of the Q class
// y = new(Q, { x: 1, y: 2, z: 3 });
// or
newQ(a, b, c) { new(Q, { x: a, y: b, z: c }) }
y = newQ(1, 2, 3);
y.functions.printf(y);
nil;
}

+ 0
- 57
tests-parsimony/testFile2.c View File

@ -1,57 +0,0 @@
@(import("parsimonyLibrary/fake-static-classes.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_Nil: {
print("Nil");
return;
}
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
Nil = initClass(Object);
Nil.type = type_t.type_Nil;
// Int
Int = initClass(Object);
Int.values.value = "null";
Int.type = type_t.type_Int;
// Pair
Pair = initClass(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);
print("\n");
s = newInt(1);
s.functions.printf(s);
nil;
}

Loading…
Cancel
Save