Просмотр исходного кода

add object generator

develop-theo
Theo Souchon 2 лет назад
Родитель
Сommit
1a6c7636b7
14 измененных файлов: 1488 добавлений и 68 удалений
  1. +77
    -25
      ccmeta.leg
  2. +0
    -33
      fake-static-classes.mc
  3. +25
    -0
      parsimonyLibrary/boot.mc
  4. +167
    -0
      parsimonyLibrary/fake-static-classes.mc
  5. +1182
    -0
      test.txt
  6. +28
    -0
      tests-parsimony/objectExample.c
  7. +1
    -1
      tests-parsimony/parsimony-lang/003.c
  8. +1
    -1
      tests-parsimony/parsimony-lang/005.c
  9. +1
    -1
      tests-parsimony/parsimony-lang/006.c
  10. +1
    -1
      tests-parsimony/parsimony-lang/007.c
  11. +1
    -1
      tests-parsimony/parsimony-lang/map.c
  12. +2
    -3
      tests-parsimony/projExample.c
  13. +1
    -1
      tests-parsimony/testFile.c
  14. +1
    -1
      tests-parsimony/testFile2.c

+ 77
- 25
ccmeta.leg Просмотреть файл

@ -104,8 +104,22 @@ jb_record *jbs= NULL;
// this is the global scope
oop globals= 0;
oop program= 0;
oop prop_enums= 0;
oop prog_last= 0;
oop prog_objects= 0;
oop objects_elements= 0;
oop objects_last= 0;
oop prog_enums= 0;
oop prog_structs= 0;
oop prog_unions= 0;
oop enum_last= 0;
oop struct_last= 0;
oop union_last= 0;
#define DO_SYMBOLS() \
DO_PROTOS() _DO(__proto__) _DO(__name__) _DO(__default__) _DO(__arguments__) \
@ -844,6 +858,14 @@ oop new_C_structSpec(oop structTok, oop attributeSpecifier1, oop id, oop leftCur
map_set(object, declarationL_symbol,declarationList);
map_set(object, rightCurly_symbol, rightCurly);
map_set(object, attribute2_symbol, attributeSpecifier2);
if (strcmp(get(map_get(structTok, text_symbol), String, value), "union") == 0) {
map_set(prog_unions, map_get(id, identifier_symbol), object);
map_set(prog_unions, intern("last" ), object);
} else {
map_set(prog_structs, map_get(id, identifier_symbol), object);
map_set(prog_structs, intern("last" ), object);
}
map_set(program, intern("last"), object);
return object;
}
@ -863,8 +885,10 @@ oop new_C_enumSpec(oop enumTok, oop id, oop leftCurly, oop enumeratorList, oop r
map_set(object, enumList_symbol, enumeratorList);
map_set(object, rightCurly_symbol, rightCurly);
if (id != null) {
map_set(prop_enums, map_get(id, identifier_symbol), object);
map_set(prog_enums, map_get(id, identifier_symbol), object);
}
map_set(prog_enums, intern("last"), object);
map_set(program, intern("last"), object);
return object;
}
@ -1606,7 +1630,7 @@ constantExpression = conditionalExpression
# 6.7
declaration = @{ C_declarationBegin() }
declaration = @{ C_declarationBegin() }
( s:declarationSpecifiers
d:initDeclaratorListOpt
t:SEMI { $$= new_C_declaration(s, d, t) }
@ -1616,7 +1640,7 @@ declaration = @{ C_declarationBegin() }
declarationSpecifiers = @{ int specified= 0 } { listBegin() }
( s:storageClassSpecifier { listAppend(s) }
| s:typeSpecifier @{ specified++ } { listAppend(s) }
| s:typeSpecifier @{ specified++ } { listAppend(s) }
# Any list of specifiers and qualifiers at the start
# of a declaration may contain attribute specifiers
| s:attributeSpecifier &{gnu} { listAppend(s) }
@ -1638,7 +1662,7 @@ initDeclarator = d:declarator
# 6.7.1
storageClassSpecifier = TYPEDEF @{ declarationTypedef() }
storageClassSpecifier = TYPEDEF @{ declarationTypedef() }
| AUTO
| parameterStorageClassSpecifier
| functionStorageClassSpecifier
@ -1930,9 +1954,9 @@ jumpStatement = g:GOTO i:id t:SEMI { $$= new_C_goto(g,
## translationUnit = externalDeclaration+
externalDeclaration = <Space+> { $$= newComment(yytext); }
| ( SEMI &{gnu}
| ( SEMI &{gnu}
| declaration
| functionDefinition
| functionDefinition
| &. &{ errmsg= "declaration expected" } error
)
@ -2165,21 +2189,17 @@ _FLOAT128 = '_Float128' !IDREST &{gnu} { $$= newToken("_Float128"
# input buffer from moving past it before redirecting input from the imported file
metaCatch = META_AT ( META_IMPORT s:META_STRING ";" { inputStackPush(get(s, String, value)) ; $$= 0 }
| m:mvalue { $$= m }
| m:mvalue { map_append(outputProgram, m); $$= null }
| META_LCB s:mstmts { map_append(outputProgram, s); $$= null }
)
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) } )?
{ $$= eval(globals, i)}
| META_LPAREN ( i:MO_OPERATION
| i:meta_exp
) META_RPAREN { $$= eval(globals, i) }
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) } )?
| META_LPAREN ( i:meta_exp ) META_RPAREN
) { $$= eval(globals, i) }
mstmts = ( s:eval_stmt )* ( META_NIL META_SEMICOLON META_RCB { $$= null }
| META_RCB { $$= s }
)
mstmts = ( s:eval_stmt )* META_RCB { $$= s }
eval_stmt = s:meta_stmt { $$= eval(globals, s) }
@ -2213,6 +2233,7 @@ meta_exp = META_VAR l:meta_ident META_ASSIGN e:meta_exp
# | META_SYNTAX p:meta_paramList q:META_IDENT e:meta_block { $$= (map_append(p, q), newFunc(null, p, e, makeInteger(2))) }
# | META_SYNTAX l:META_IDENT p:meta_paramList e:meta_block { $$= newFunc(l, p, e, makeInteger(1)) }
# | META_SYNTAX p:meta_paramList e:meta_block { $$= newFunc(null, p, e, makeInteger(1)) }
| m:MO_OPERATION { $$= m }
| l:META_IDENT o:meta_assignOp e:meta_exp { $$= newAssign(Assign_proto, l, o, e) }
| l:meta_postfix META_DOT i:META_IDENT o:meta_assignOp e:meta_exp { $$= newSetMap(SetMember_proto, l, i, o, e) }
| l:meta_postfix META_LBRAC i:meta_exp META_RBRAC o:meta_assignOp e:meta_exp { $$= newSetMap(SetIndex_proto, l, i, o, e) }
@ -2308,7 +2329,6 @@ meta_prefix = META_PLUS n:meta_prefix { $$= n }
| META_PLING n:meta_prefix { $$= newUnary(Not_proto, n) }
| META_PLUSPLUS n:meta_prefix { $$= newPreIncrement(n) }
| META_MINUSMINUS n:meta_prefix { $$= newPreDecrement(n) }
| m:MO_OPERATION { $$= m }
# | META_BACKTICK n:meta_prefix { $$= newUnary(Quasiquote_proto, n) }
# | META_AT n:meta_prefix { $$= newUnary(Unquote_proto, n) }
| n:meta_postfix { $$= n }
@ -2404,6 +2424,8 @@ MO_OPERATION = META_BACKTICK ( MO_INITIALIZER i:initializer { $$= newUn
| MO_INTEGER i:integerConstant { $$= newUnary(Quote_proto ,i) }
| MO_DECLARATION i:declaration { $$= newUnary(Quote_proto ,i) }
| MO_STRING i:stringLiteral { $$= newUnary(Quote_proto ,i) }
| MO_FUN i:functionDefinition { $$= newUnary(Quote_proto ,i) }
| MO_ED i:externalDeclaration{ $$= newUnary(Quote_proto ,i) }
)
MO_INITIALIZER = 'initializer' ![(a-zA-Z0-9_] --
@ -2412,6 +2434,8 @@ MO_STATEMENT = 'statement' ![(a-zA-Z0-9_] --
MO_INTEGER = 'integer' ![(a-zA-Z0-9_] --
MO_DECLARATION = 'declaration' ![(a-zA-Z0-9_] --
MO_STRING = 'string' ![(a-zA-Z0-9_] --
MO_FUN = 'fun' ![(a-zA-Z0-9_] --
MO_ED = 'ed' ![(a-zA-Z0-9_] --
#--------------------------------------------- Meta rules ----------------------------------------------#
@ -2429,7 +2453,7 @@ META_IN = 'in' ![a-zA-Z0-9_] --
META_WHILE = 'while' ![a-zA-Z0-9_] --
META_IF = 'if' ![a-zA-Z0-9_] --
META_ELSE = 'else' ![a-zA-Z0-9_] --
META_NULL = 'null' ![a-zA-Z0-9_] --
META_NULL = ('null'|'nil') ![a-zA-Z0-9_] --
META_RETURN = 'return' ![a-zA-Z0-9_] --
META_BREAK = 'break' ![a-zA-Z0-9_] --
META_CONTINUE = 'continue' ![a-zA-Z0-9_] --
@ -3453,6 +3477,15 @@ oop prim_keys(oop scope, oop params)
return null;
}
oop prim_all_keys(oop scope, oop params)
{
if (map_hasIntegerKey(params, 0)) {
oop arg= get(params, Map, elements)[0].value;
if (is(Map, arg)) return map_allKeys(arg);
}
return null;
}
oop prim_values(oop scope, oop params)
{
if (map_hasIntegerKey(params, 0)) {
@ -3504,7 +3537,7 @@ oop prim_print(oop scope, oop params)
}
return params;
}
//TRY
oop evalArgs(oop scope, oop args)
{
int i = 0;
@ -4634,6 +4667,7 @@ int main(int argc, char **argv)
map_set(globals, intern("exit" ), makeFunction(prim_exit, intern("exit" ), null, null, globals, null));
map_set(globals, intern("keys" ), makeFunction(prim_keys, intern("keys" ), null, null, globals, null));
map_set(globals, intern("allKeys" ), makeFunction(prim_all_keys, intern("allKeys" ), null, null, globals, null));
map_set(globals, intern("values" ), makeFunction(prim_values, intern("values" ), null, null, globals, null));
map_set(globals, intern("length" ), makeFunction(prim_length, intern("length" ), null, null, globals, null));
map_set(globals, intern("print" ), makeFunction(prim_print, intern("print" ), null, null, globals, null));
@ -4644,7 +4678,7 @@ int main(int argc, char **argv)
map_set(globals, intern("microseconds"), makeFunction(prim_microseconds, intern("microseconds"), null, null, globals, null));
map_set(globals, intern("string" ), makeFunction(prim_String , intern("string" ), null, null, globals, null));
map_set(globals, intern("scope"), makeFunction(prim_scope, intern("scope"), null, null, globals, null));
map_set(globals, intern("scope" ), makeFunction(prim_scope, intern("scope" ), null, null, globals, null));
#define _DO(NAME) NAME##_symbol=intern(#NAME);
DO_SYMBOLS()
@ -4668,10 +4702,28 @@ int main(int argc, char **argv)
DO_PROTOS()
#undef _DO
program = makeMap();
prop_enums = makeMap();
map_set(program, intern("enums"), prop_enums);
map_set(globals, intern("program"), program);
program = makeMap();
prog_enums = makeMap();
prog_structs = makeMap();
prog_unions = makeMap();
prog_last = makeMap();
enum_last = makeMap();
struct_last = makeMap();
union_last = makeMap();
prog_objects = makeMap();
objects_elements = makeMap();
objects_last = makeMap();
map_set(prog_enums , intern("last" ), enum_last );
map_set(prog_structs , intern("last" ), struct_last );
map_set(prog_unions , intern("last" ), union_last );
map_set(prog_objects , intern("elements" ), objects_elements );
map_set(prog_objects , intern("last" ), objects_last );
map_set(program , intern("structs" ), prog_structs );
map_set(program , intern("unions" ), prog_unions );
map_set(program , intern("enums" ), prog_enums );
map_set(program , intern("objects" ), prog_objects );
map_set(globals , intern("program" ), program );
fixScope(globals);

+ 0
- 33
fake-static-classes.mc Просмотреть файл

@ -1,33 +0,0 @@
@(import("boot.mc"))
@{
initClass(class) {
out = {};
if (class == null) {
out.values = {};
out.functions = {};
} else {
out = {values: clone(class.values), functions: clone(class.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;
}

boot.mc → parsimonyLibrary/boot.mc Просмотреть файл

@ -1,4 +1,27 @@
@{
println(x) { print(x, "\n"); }
treeCopy(x) {
l = {};
k = allKeys(x);
if (k == null) {
return clone(x);
} else {
for (i in k) {
l[k[i]] = clone(treeCopy(x[k[i]]));
}
return clone(l);
}
}
getId(x) { { identifier: x, __proto__: C_id }; }
getEnum(x) { { attributeL: null, expression: null, name: getId(x), __proto__: C_enum }; }
getComma() { { text: ",", comment: { text : " ", __proto__: Comment }, __proto__: Token }; }
getComment(x) { { text : x, __proto__: Comment }; }
append(s, e) {
s[length(s)] = e;
}
@ -19,6 +42,8 @@
out;
}
notToken(x) { x.__proto__ != Token }
select(f, s) {
out = {};
for (i in s) {

+ 167
- 0
parsimonyLibrary/fake-static-classes.mc Просмотреть файл

@ -0,0 +1,167 @@
@(import("parsimonyLibrary/boot.mc"))
@{
checkIdorDecl(x) {
(x.identifier == null) ? x.declarators.identifier : x.identifier;
}
checkIdorText(x) {
(x.identifier == null) ? x.text : x.identifier;
}
object(x) {
x == null ? x = "oop" : x;
program.objects.elements[length(program.objects.elements)] = x;
program.objects.last = x;
l = {};
enum = `declaration typedef enum {_} type_t;;
enum.semicolon.comment = getComment("\n");
append(l, enum);
obj = `declaration union object;;
obj.semicolon.comment = getComment("\n");
append(l, treeCopy(obj));
type_obj = `declaration typedef union object *oop;;
type_obj.declarators[0].declarators.identifier = clone(x);
type_obj.semicolon.comment = getComment("\n");
append(l, treeCopy(type_obj));
l;
}
addObject() {
enum = 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;
} else {
append(program.enums.last.enumList, getComma());
append(program.enums.last.enumList, enum);
}
for (i=length(program.last.declarationL); i>0; i--) {
program.last.declarationL[i] = program.last.declarationL[i-1];
};
newType = `declaration type_t type;;
newType.semicolon.comment = getComment("\n ");
program.last.declarationL[0] = treeCopy(newType);
nil;
}
getObjectMacro() {
macro = `ed #define get(PTR, TYPE, FIELD) (_checkType(PTR, TYPE, __FILE__, __LINE__)->TYPE.FIELD)
;
macro;
}
setObjectMacro() {
macro = `ed #define set(PTR, TYPE, FIELD, VALUE) (_checkType(PTR, TYPE, __FILE__, __LINE__)->TYPE.FIELD = VALUE)
;
macro;
}
getGeneralObject(x) {
func = `fun int getfoo(int obj) {
if (!isFoo(obj)) {
fprintf(stderr, "\ngetFoo call on non-foo\n");
exit(1);
}
return get(obj, foo, foo);
}
;
id = program.structs[x].declarationL[1].specifiers[0].identifier;
star = program.structs[x].declarationL[1].declarators[0].declarators.star;
if (id != null) {
program.structs[x].declarationL[1].specifiers[0].text = program.structs[x].declarationL[1].specifiers[0].identifier
}
if (star != null) {
func.specifiers[0].text = string(checkIdorText(program.structs[x].declarationL[1].specifiers[0])) + " *";
func.specifiers[0].comment = "";
} else {
func.specifiers[0].text = string(checkIdorText(program.structs[x].declarationL[1].specifiers[0]));
}
func.declarators.declarators.identifier = "get" + string(x);
func.compoundS.expression[0].condition.expression.declarators.identifier = "is" + string(x);
func.compoundS.expression[0].consequent.expression[0].expression.paramTypeL[2].text[0].value = "\"\\nget" + string(x) + " call on non-" + string(x) + "\\n\"";
func.compoundS.expression[1].expression.paramTypeL[2].identifier = string(x);
func.compoundS.expression[1].expression.paramTypeL[4].identifier = checkIdorDecl(program.structs[x].declarationL[length(program.structs[x].declarationL)-1].declarators[0].declarators);
func.declarators.paramTypeL[0].specifiers[0].text = program.objects.last;
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.semicolon.comment = getComment("\n");
obj = select(notToken, program.enums.last.enumList);
elt = treeCopy(`declaration struct nil nil;);
for (i in obj) {
len = length(objectUnion.specifiers[0].declarationL);
elt.specifiers[0].name.identifier = elt.declarators[0].identifier = obj[i].name.identifier;
objectUnion.specifiers[0].declarationL[len] = treeCopy(elt);
objectUnion.specifiers[0].declarationL[len].semicolon.comment = getComment(" ");
}
append(l, objectUnion);
isDef = `declaration int is(type_t type, int obj);;
isDef.declarators[0].paramTypeL[2].specifiers[0].text = program.objects.last;
isDef.semicolon.comment = getComment("\n");
append(l, isDef);
func = `fun int isFoo(int obj) { return is(foo, obj); };
func.declarators.paramTypeL[0].specifiers[0].text = program.objects.last;
func.compoundS.rightCurly.comment = getComment("\n");
for (i in obj) {
func.declarators.declarators.identifier = "is" + string(obj[i].name.identifier);
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.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.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()));
for (i in program.unions.last.declarationL) {
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);
}
l;
}
nil;
}

+ 1182
- 0
test.txt
Разница между файлами не показана из-за своего большого размера
Просмотреть файл


+ 28
- 0
tests-parsimony/objectExample.c Просмотреть файл

@ -0,0 +1,28 @@
@import("parsimonyLibrary/fake-static-classes.mc")
typedef long long int_t;
typedef long double flt_t;
@object()
struct Integer {
int_t i;
};
@addObject()
// struct Float {
// flt_t _value;
// };
// @addObject()
struct String {
char *value;
};
@addObject()
// struct Symbol {
// char *name;
// };
// @addObject()
@endObject()

+ 1
- 1
tests-parsimony/parsimony-lang/003.c Просмотреть файл

@ -1,4 +1,4 @@
@{import("boot.mc");}
@{import("parsimonyLibrary/boot.mc");}
@{
nTimes = `constant 10;

+ 1
- 1
tests-parsimony/parsimony-lang/005.c Просмотреть файл

@ -1,4 +1,4 @@
@{import("boot.mc");}
@{import("parsimonyLibrary/boot.mc");}
@{
l = {};

+ 1
- 1
tests-parsimony/parsimony-lang/006.c Просмотреть файл

@ -1,4 +1,4 @@
@{import("boot.mc");}
@{import("parsimonyLibrary/boot.mc");}
@{
l = {};

+ 1
- 1
tests-parsimony/parsimony-lang/007.c Просмотреть файл

@ -1,4 +1,4 @@
@{import("boot.mc");}
@{import("parsimonyLibrary/boot.mc");}
@{
l = {};

+ 1
- 1
tests-parsimony/parsimony-lang/map.c Просмотреть файл

@ -1,4 +1,4 @@
@{import("boot.mc");}
@{import("parsimonyLibrary/boot.mc");}
enum foo { A, B, C };
enum oof { A, B, C };

+ 2
- 3
tests-parsimony/projExample.c Просмотреть файл

@ -1,11 +1,10 @@
@{import("boot.mc");}
@{import("parsimonyLibrary/boot.mc");}
enum foo { Closed = 0, ReadOnly = 1, WriteOnly = 2 };
@{
idToString(x) {{ __proto__: C_string, value: "\"" + string(x.name.identifier) + "\"" }}
notToken(x) { x.__proto__ != Token }
nil;
}
static char *stateNames[] = { @(map(idToString, program.enums.foo.enumList, notToken)) };
static char *stateNames[] = { @(map(idToString, program.last.enumList, notToken)) };

+ 1
- 1
tests-parsimony/testFile.c Просмотреть файл

@ -1,4 +1,4 @@
@(import("fake-static-classes.mc"))
@(import("parsimonyLibrary/fake-static-classes.mc"))
@{
//creation of the class

+ 1
- 1
tests-parsimony/testFile2.c Просмотреть файл

@ -1,4 +1,4 @@
@(import("fake-static-classes.mc"))
@(import("parsimonyLibrary/fake-static-classes.mc"))
@{
//creation of the object class

Загрузка…
Отмена
Сохранить