Procházet zdrojové kódy

adds functions: map, guardmap, select, reject

develop-theo
Sebeck10 před 2 roky
rodič
revize
945cf11555
2 změnil soubory, kde provedl 75 přidání a 60 odebrání
  1. +57
    -60
      ccmeta.leg
  2. +18
    -0
      tests-parsimony/map.c

+ 57
- 60
ccmeta.leg Zobrazit soubor

@ -1308,9 +1308,10 @@ language printLang = C;
oop outputProgram= 0;
/* TO DELETE IN FUTURE
oop searchType(char *type)
{
ssize_t pos = map_intern_search(symbol_table, type );
ssize_t pos = map_intern_search(symbol_table, type);
if (pos >= 0) return get(symbol_table, Map, elements)[pos].key;
return null;
}
@ -1332,72 +1333,66 @@ oop findMap(oop map)
}
return null;
}
*/
oop selectFunction(oop type, oop map)
oop selectFunction(oop filter, oop map)
{
oop enumList;
oop filter = searchType(type->String.value);
if (filter == null)
return null;
oop ret = findMap(map);
oop name_Type = map_get(map_get(ret, __proto___symbol), __name___symbol);
int number_type = get(name_Type, Symbol, prototype);
switch (number_type) {
case t_C_enumSpec :
enumList = map_get(ret, enumList_symbol);
size_t sizeType = map_size(enumList);
for (int i=0; i<sizeType; i++) {
oop singleton = map_get(enumList, makeInteger(i));
oop singletonType = map_get(map_get(singleton, __proto___symbol), __name___symbol);
if (filter == singletonType)
println(singletonType);
}
break;
default:
printf("not find for number : %i\n", number_type);
return null;
oop ret = makeMap();
size_t keysNumber = map_size(eval(globals, map));
for (int i=0; i<keysNumber; i++) {
oop singleValue = makeMap();
oop singleton = newGetMap(GetIndex_proto, map, makeInteger(i));
map_append(singleValue, singleton);
if (eval(globals, newCall(filter, singleValue)) != makeInteger(0))
map_append(ret, eval(globals, singleton));
}
return null;
return ret;
}
oop rejectFunction(oop type, oop map)
oop rejectFunction(oop filter, oop map)
{
oop enumList;
oop filter = searchType(type->String.value);
if (filter == null)
return null;
oop ret = findMap(map);
oop name_Type = map_get(map_get(ret, __proto___symbol), __name___symbol);
int number_type = get(name_Type, Symbol, prototype);
switch (number_type) {
case t_C_enumSpec :
enumList = map_get(ret, enumList_symbol);
size_t sizeType = map_size(enumList);
for (int i=0; i<sizeType; i++) {
oop singleton = map_get(enumList, makeInteger(i));
oop singletonType = map_get(map_get(singleton, __proto___symbol), __name___symbol);
if (filter != singletonType)
println(singletonType);
}
break;
default:
printf("not find for number : %i\n", number_type);
return null;
oop ret = makeMap();
size_t keysNumber = map_size(eval(globals, map));
for (int i=0; i<keysNumber; i++) {
oop singleValue = makeMap();
oop singleton = newGetMap(GetIndex_proto, map, makeInteger(i));
map_append(singleValue, singleton);
if (eval(globals, newCall(filter, singleValue)) == makeInteger(0))
map_append(ret, eval(globals, singleton));
}
return null;
return ret;
}
oop mapFunction(oop func, oop args)
{
int size = map_size(args);
for(int i = 0; i < size; i++) {
oop ret = makeMap();
oop keys = map_allKeys(args);
size_t keysNumber = map_size(keys);
for(int i = 0; i < keysNumber; i++) {
oop singleValue = makeMap();
oop singleton = newGetMap(GetIndex_proto, args, makeInteger(i));
map_append(singleValue, singleton);
map_set(ret, makeInteger(i), eval(globals, newCall(func, singleValue)));
}
return ret;
}
oop guardMapFunction(oop func, oop args, oop guard)
{
oop ret = makeMap();
size_t keysNumber = map_size(args);
for(int i = 0; i < keysNumber; i++) {
oop singleValue = makeMap();
map_append(singleValue, map_get(args, makeInteger(i)));
map_set(args, makeInteger(i), eval(globals, newCall(func, singleValue)));
oop singleton = newGetMap(GetIndex_proto, args, makeInteger(i));
map_append(singleValue, singleton);
if ( eval(globals, newCall(guard, singleValue)) != makeInteger(0)) {
map_set(ret, makeInteger(i), eval(globals, newCall(func, singleValue)));
} else {
map_set(ret, makeInteger(i), map_get(args, makeInteger(i)));
}
}
return args;
return ret;
}
@ -2316,13 +2311,16 @@ meta_exp = META_VAR l:meta_ident META_ASSIGN e:meta_exp
| l:meta_syntax2 a:meta_argumentList s:meta_block { $$ = (map_append(a, s), apply(globals, globals, l, a, a)) }
| c:meta_cond { $$ = c }
meta_funcSelect = META_LPAREN t:tmpSelectString META_COMMA i:idOpt META_RPAREN { $$ = selectFunction(t, i) }
meta_funcReject = META_LPAREN t:tmpSelectString META_COMMA i:idOpt META_RPAREN { $$ = rejectFunction(t, i) }
meta_funcMap = META_LPAREN i:meta_value META_COMMA e:meta_exp ( META_RPAREN { $$ = mapFunction(i, eval(globals, e)) }
| META_COMMA f:meta_value META_RPAREN { $$ = guardMapFunction(i, eval(globals, e), f) }
)
meta_funcSelect = META_LPAREN t:meta_value META_COMMA i:meta_exp META_RPAREN { $$ = selectFunction(t, i) }
meta_funcReject = META_LPAREN t:meta_value META_COMMA i:meta_exp META_RPAREN { $$ = rejectFunction(t, i) }
tmpSelectString = < [a-zA-Z_]* > { $$ = makeString(unescape(yytext))}
meta_ident = l:META_IDENT { $$ = l }
# | META_AT n:meta_prefix { $$ = newUnary(Unquote_proto, n) }
# | META_AT n:meta_prefix { $$ = newUnary(Unquote_proto, n) }
meta_syntax2 = < [a-zA-Z_][a-zA-Z0-9_]* >
&{ null != getSyntaxId(2, intern(yytext)) } -- { $$ = getSyntaxId(2, intern(yytext)) }
@ -2419,12 +2417,11 @@ meta_postfix = i:meta_value
( META_DOT s:META_IDENT a:meta_argumentList { i = newInvoke(i, s, a) }
| META_DOT s:META_IDENT !meta_assignOp { i = newGetMap(GetMember_proto, i, s) }
| META_LBRAC p:meta_exp META_RBRAC !meta_assignOp { i = newGetMap(GetIndex_proto, i, p) }
| a:meta_argumentList { i = (null != getSyntax(1, i)) ? apply(globals, globals, getSyntax(1, i), a, i) : newCall(i, a); }
| a:meta_argumentList { i = (null != getSyntax(1, i)) ? apply(globals, globals, getSyntax(1, i), a, i) : newCall(i, a) }
| META_PLUSPLUS { i = newPostIncrement(i) }
| META_MINUSMINUS { i = newPostDecrement(i) }
) * { $$ = i }
meta_funcMap = META_LPAREN i:meta_value META_COMMA e:meta_exp META_RPAREN { $$ = mapFunction(i, eval(globals, e)) }
meta_paramList = META_LPAREN m:meta_makeMap
( i:META_IDENT { map_append(m, i) }
@ -2509,7 +2506,7 @@ MO_OPERATION = META_BACKTICK ( MO_INITIALIZER i:initializer { $$ = newU
| MO_STRING i:stringLiteral { $$ = newUnary(Quote_proto ,i) }
)
META_OPERATORS = MO_INITIALIZER | MO_CONSTANT | MO_STATEMENT | MO_INTEGER | MO_DECLARATION | MO_STRING
# META_OPERATORS = MO_INITIALIZER | MO_CONSTANT | MO_STATEMENT | MO_INTEGER | MO_DECLARATION | MO_STRING
MO_INITIALIZER = 'initializer' ![(a-zA-Z0-9_] --
MO_CONSTANT = 'constant' ![(a-zA-Z0-9_] --

+ 18
- 0
tests-parsimony/map.c Zobrazit soubor

@ -6,9 +6,27 @@
foo * foo;
}
c = ["earth", "sky", "water"];
isSky(x) { x == "sky" }
c = reject(isSky, c);
}
@{print(map(p2, [100, 2, 3]));}
@{print(map(toChar, c));}
enum foo { A, B, C };
enum oof { A, B, C };
@{
isEnum(x) { x.__proto__ == C_enum }
to_C_string(x) {{ __proto__: C_string, value: "\"" + string(x.name.identifier) + "\"" }}
// to_C_string(properties.enums.foo.enumList[?].name.identifier);
a = map(to_C_string, select(isEnum, properties.enums.foo.enumList));
b = map(to_C_string, properties.enums.foo.enumList, isEnum);
}
char *a = @(a);
char *b[] = { @(b) };

Načítá se…
Zrušit
Uložit