瀏覽代碼

Add boot file with usefull functions

develop-theo
Sebeck10 2 年之前
父節點
當前提交
d09512529b
共有 4 個檔案被更改,包括 105 行新增105 行删除
  1. +4
    -102
      ccmeta.leg
  2. +43
    -0
      tests-parsimony/boot.mc
  3. +48
    -3
      tests-parsimony/map.c
  4. +10
    -0
      tests-parsimony/projExample.c

+ 4
- 102
ccmeta.leg 查看文件

@ -1308,94 +1308,6 @@ language printLang = C;
oop outputProgram= 0;
/* TO DELETE IN FUTURE
oop searchType(char *type)
{
ssize_t pos = map_intern_search(symbol_table, type);
if (pos >= 0) return get(symbol_table, Map, elements)[pos].key;
return null;
}
oop findMap(oop map)
{
oop symbol_name = map_get(map, identifier_symbol);
size_t size = map_size(properties);
oop retType, ret;
for (int i = 0; i < size; i++) {
retType = map_get(map_keys(properties), makeInteger(i));
retType = map_get(properties, retType);
size_t sizeType = map_size(retType);
for (int k = 0; k < sizeType; k++) {
ret = map_get(map_keys(retType), makeInteger(k));
if (ret == symbol_name)
return map_get(retType, ret);
}
}
return null;
}
*/
oop selectFunction(oop filter, oop map)
{
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 ret;
}
oop rejectFunction(oop filter, oop map)
{
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 ret;
}
oop mapFunction(oop func, oop args)
{
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();
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 ret;
}
%}
@ -1404,7 +1316,7 @@ oop guardMapFunction(oop func, oop args, oop guard)
# yylval == null => "pseudo" op, e.g., change language -- ignored by REPL
# yylval == 0 => end of input file while in META mode only
start = m:metaCatch { yylval = null }
start = m:metaCatch { yylval = m }
| s:externalDeclaration { yylval= s }
error = EOL* < (!EOL .)* EOL* (!EOL .)* > &{ error(yytext), 1 }
@ -2252,9 +2164,9 @@ _FLOAT128 = '_Float128' !IDREST &{gnu} { $$= newToken("_Float1
# the semicolon has to be explicit with no space eaten afterwards to prevent the
# input buffer from moving past it before redirecting input from the imported file
metaCatch = META_AT ( m:mvalue { map_append(outputProgram, m) }
| META_LCB m:mstmts { map_append(outputProgram, m) }
| META_IMPORT s:META_STRING ";" { inputStackPush(get(s, String, value)) }
metaCatch = META_AT ( META_IMPORT s:META_STRING ";" { inputStackPush(get(s, String, value)) ; $$ = 0 }
| m:mvalue { map_append(outputProgram, m) ; $$ = null }
| META_LCB m:mstmts { map_append(outputProgram, m) ; $$ = null }
)
@ -2298,9 +2210,6 @@ meta_block = META_LCB m:meta_makeMap
META_RCB { $$ = newBlock(m) }
meta_exp = META_VAR l:meta_ident META_ASSIGN e:meta_exp { $$ = newDeclaration(l, e) }
| META_FUNCMAP m:meta_funcMap { $$ = m}
| META_FUNCSELECT s:meta_funcSelect { $$ = s }
| META_FUNCREJECT s:meta_funcReject { $$ = s }
# | META_SYNTAX l:META_IDENT p:meta_paramList q:META_IDENT e:meta_block { $$ = (map_append(p, q), newFunc(l, p, e, makeInteger(2))) }
# | 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)) }
@ -2311,13 +2220,6 @@ 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_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) }
meta_ident = l:META_IDENT { $$ = l }
# | META_AT n:meta_prefix { $$ = newUnary(Unquote_proto, n) }

+ 43
- 0
tests-parsimony/boot.mc 查看文件

@ -0,0 +1,43 @@
@{
append(s, e) {
s[length(s)] = e;
}
map(f, seq, g) {
out = {};
if (g) {
for (i in seq) {
e = seq[i];
if (g(e)) { e = f(e); }
append(out, e);
}
} else {
for (i in seq) {
append(out, f(seq[i]));
}
}
out;
}
select(f, s) {
out = {};
for (i in s) {
e = s[i];
if (f(e)) { append(out, e); }
}
out;
}
reject(f, s) {
out = {};
for (i in s) {
e = s[i];
if (!f(e)) { append(out, e); }
}
out;
}
last(s) {
s[length(s)-1];
}
}

+ 48
- 3
tests-parsimony/map.c 查看文件

@ -1,4 +1,42 @@
@{
append(s, e) {
s[length(s)] = e;
}
map(f, seq, g) {
out = {};
if (g) {
for (i in seq) {
e = seq[i];
if (g(e)) { e = f(e); }
append(out, e);
}
} else {
for (i in seq) {
append(out, f(seq[i]));
}
}
out;
}
select(f, s) {
out = {};
for (i in s) {
e = s[i];
if (f(e)) { append(out, e); }
}
out;
}
reject(f, s) {
out = {};
for (i in s) {
e = s[i];
if (!f(e)) { append(out, e); }
}
out;
}
toChar(foo) {
"\"type :"+foo+"\"";
}
@ -8,6 +46,7 @@
c = ["earth", "sky", "water"];
isSky(x) { x == "sky" }
c = reject(isSky, c);
}
@{print(map(p2, [100, 2, 3]));}
@ -18,15 +57,21 @@ enum foo { A, B, C };
enum oof { A, B, C };
@{
isEnum(x) { x.__proto__ == C_enum }
isEnum(x) { x.__proto__ == C_enum }
notToken(x) { x.__proto__ != Token }
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);
b = map(to_C_string, properties.enums.foo.enumList, notToken);
c = map(to_C_string, properties.enums.foo.enumList, isEnum);
}
char *a = @(a);
char *a[] = { @(a) };
char *b[] = { @(b) };
char *c[] = { @(c) };

+ 10
- 0
tests-parsimony/projExample.c 查看文件

@ -0,0 +1,10 @@
@{import("tests-parsimony/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 }
}
static char stateNames[] = { @(map(idToString, properties.enums.foo.enumList, notToken)) };

Loading…
取消
儲存