Browse Source

added a way to print all the tree

master
Nathan R 3 years ago
parent
commit
c0c4334e85
1 changed files with 58 additions and 24 deletions
  1. +58
    -24
      ccmeta.leg

+ 58
- 24
ccmeta.leg View File

@ -2103,6 +2103,19 @@ __INLINE = '__inline' !IDREST &{gnu} { $$= newToken("__inlin
_FLOAT128 = '_Float128' !IDREST &{gnu} { $$= newToken("_Float128" ) } - _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 ----------------------------------------------# #--------------------------------------------- Meta grammar ----------------------------------------------#
# the semicolon has to be explicit with no space eaten afterwards to prevent the # 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)) { switch (getType(node)) {
case Undefined: case Undefined:
return; return;
@ -3865,21 +3890,6 @@ void outputValue(oop node) {
fprintf(stderr, "\noutputNode: unknown node type %i\n", getType(node)); fprintf(stderr, "\noutputNode: unknown node type %i\n", getType(node));
abort(); 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)); assert(is(Map, node));
oop proto= map_get(node, __proto___symbol); oop proto= map_get(node, __proto___symbol);
if (null == proto) { // assume this is just a list of nodes 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_number is the enum version of the proto symbol
proto_t proto_number= get(map_get(proto, __name___symbol), Symbol, prototype); proto_t proto_number= get(map_get(proto, __name___symbol), Symbol, prototype);
printSpace(depth);
switch (proto_number) { switch (proto_number) {
#define CASE(NAME) case t_##NAME:printf("%s:\n", #NAME); #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) CASE(Comment)
OUT(Comment); OUT(Comment);
break; break;
@ -3908,7 +3917,7 @@ void outputTree(oop node, int depth)
/** C terminal nodes */ /** C terminal nodes */
#define CASE(NAME) case t_C_##NAME:printf("%s:\n", #NAME); #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) CASE(int)
OUT(text); OUT(text);
break; break;
@ -4422,8 +4431,34 @@ void outputTree(oop node, int depth)
CASE(PostDecIndex) CASE(PostDecIndex)
OUT(rhs); OUT(rhs);
break; 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 /** TODO
* CASE(Quasiquote) * CASE(Quasiquote)
* PRINT(Quasiquote); * PRINT(Quasiquote);
@ -4449,7 +4484,7 @@ void printTree(oop element, language id) {
if (id == C) printf("-- C program --\n"); if (id == C) printf("-- C program --\n");
else if (id == META) printf("-- Meta program --\n"); else if (id == META) printf("-- Meta program --\n");
else fprintf(stderr, "Wrong language in printTree()"); else fprintf(stderr, "Wrong language in printTree()");
outputTree(element, 3);
outputTree(element, 0);
printf("\n"); printf("\n");
} }
@ -4530,7 +4565,6 @@ int main(int argc, char **argv)
} }
} }
if (!repled) { if (!repled) {
readEvalPrint(globals, NULL); readEvalPrint(globals, NULL);
} }

Loading…
Cancel
Save