From d204b3216d3416ecd883aed5a75f409fcf359b03 Mon Sep 17 00:00:00 2001 From: Theo Souchon Date: Mon, 5 Dec 2022 17:36:21 +0900 Subject: [PATCH] fake-static-classes fixed --- ccmeta.leg | 27 ++++++++- parsimonyLibrary/fake-static-classes.mc | 76 ++++++++----------------- tests-parsimony/objectExample.c | 2 +- tests-parsimony/testFile.c | 40 ------------- tests-parsimony/testFile2.c | 57 ------------------- 5 files changed, 52 insertions(+), 150 deletions(-) delete mode 100644 tests-parsimony/testFile.c delete mode 100644 tests-parsimony/testFile2.c diff --git a/ccmeta.leg b/ccmeta.leg index c689511..a925522 100644 --- a/ccmeta.leg +++ b/ccmeta.leg @@ -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; i0; 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; } diff --git a/tests-parsimony/objectExample.c b/tests-parsimony/objectExample.c index 8c19b49..fe9d008 100644 --- a/tests-parsimony/objectExample.c +++ b/tests-parsimony/objectExample.c @@ -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; diff --git a/tests-parsimony/testFile.c b/tests-parsimony/testFile.c deleted file mode 100644 index 3d095ea..0000000 --- a/tests-parsimony/testFile.c +++ /dev/null @@ -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; -} \ No newline at end of file diff --git a/tests-parsimony/testFile2.c b/tests-parsimony/testFile2.c deleted file mode 100644 index e2b2aac..0000000 --- a/tests-parsimony/testFile2.c +++ /dev/null @@ -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; -} \ No newline at end of file