瀏覽代碼

added a way to print all the tree

master
Nathan R 3 年之前
父節點
當前提交
c0c4334e85
共有 1 個文件被更改,包括 58 次插入24 次删除
  1. +58
    -24
      ccmeta.leg

+ 58
- 24
ccmeta.leg 查看文件

@ -2103,6 +2103,19 @@ __INLINE = '__inline' !IDREST &{gnu} { $$= newToken("__inlin
_FLOAT128 = '_Float128' !IDREST &{gnu} { $$= newToken("_Float128" ) } -
#--------------------------------------------- Common rules ----------------------------------------------#
- = (blank | comment)*
blank = space | eol
space = [ \t]
eol = ( "\n""\r"*
| "\r""\n"*
) { inputStack->lineNumber++ }
comment = "//" ( ![\n\r] . )*
| "/*" ( !"*/" (eol | .) )* "*/"
#--------------------------------------------- Meta grammar ----------------------------------------------#
# the semicolon has to be explicit with no space eaten afterwards to prevent the
@ -3842,8 +3855,20 @@ void outputNode(oop node)
}
void outputValue(oop node) {
if (!node) return;
void printSpace(int depth) {
for (int i = 0 ; i < depth ; i++) {
printf(" ");
}
}
void outputTree(oop node, int depth)
{
if(node == null) {
printSpace(depth);
printf("<null>\n");
return;
}
printSpace(depth);
switch (getType(node)) {
case Undefined:
return;
@ -3865,21 +3890,6 @@ void outputValue(oop node) {
fprintf(stderr, "\noutputNode: unknown node type %i\n", getType(node));
abort();
}
}
void printSpace(int depth) {
for (int i = 0 ; i < depth ; i++) {
printf(" ");
}
}
void outputTree(oop node, int depth)
{
if(node == null) {
printSpace(depth);
printf("<null>\n");
return;
}
assert(is(Map, node));
oop proto= map_get(node, __proto___symbol);
if (null == proto) { // assume this is just a list of nodes
@ -3892,10 +3902,9 @@ void outputTree(oop node, int depth)
}
// proto_number is the enum version of the proto symbol
proto_t proto_number= get(map_get(proto, __name___symbol), Symbol, prototype);
printSpace(depth);
switch (proto_number) {
#define CASE(NAME) case t_##NAME:printf("%s:\n", #NAME);
#define OUT(NAME) printSpace(depth+DELTA); outputValue(map_get(node, text_symbol));
#define OUT(NAME) outputTree(map_get(node, text_symbol), depth+DELTA);
CASE(Comment)
OUT(Comment);
break;
@ -3908,7 +3917,7 @@ void outputTree(oop node, int depth)
/** C terminal nodes */
#define CASE(NAME) case t_C_##NAME:printf("%s:\n", #NAME);
#define OUT(NAME) printSpace(depth+DELTA) ; outputValue(map_get(node, NAME##_symbol));
#define OUT(NAME) outputTree(map_get(node, NAME##_symbol), depth+DELTA);
CASE(int)
OUT(text);
break;
@ -4422,8 +4431,34 @@ void outputTree(oop node, int depth)
CASE(PostDecIndex)
OUT(rhs);
break;
CASE(Assign)
OUT(lhs);
OUT(operator);
OUT(rhs);
break;
CASE(SetMember)
OUT(map);
OUT(key);
OUT(operator);
OUT(value);
break;
CASE(SetIndex)
OUT(map);
OUT(key);
OUT(operator);
OUT(value);
break;
CASE(GetMember)
OUT(map);
OUT(key);
break;
CASE(GetIndex)
OUT(map);
OUT(key);
break;
CASE(GetVariable)
OUT(key)
break;
/** TODO
* CASE(Quasiquote)
* PRINT(Quasiquote);
@ -4449,7 +4484,7 @@ void printTree(oop element, language id) {
if (id == C) printf("-- C program --\n");
else if (id == META) printf("-- Meta program --\n");
else fprintf(stderr, "Wrong language in printTree()");
outputTree(element, 3);
outputTree(element, 0);
printf("\n");
}
@ -4530,7 +4565,6 @@ int main(int argc, char **argv)
}
}
if (!repled) {
readEvalPrint(globals, NULL);
}

Loading…
取消
儲存