Project

General

Profile

Feature #2455 ยป dump-parsetree.patch

mame (Yusuke Endoh), 12/08/2009 03:46 AM

View differences:

common.mk
io.$(OBJEXT) \
marshal.$(OBJEXT) \
math.$(OBJEXT) \
node.$(OBJEXT) \
numeric.$(OBJEXT) \
object.$(OBJEXT) \
pack.$(OBJEXT) \
......
marshal.$(OBJEXT): {$(VPATH)}marshal.c $(RUBY_H_INCLUDES) {$(VPATH)}io.h \
$(ENCODING_H_INCLUDES) {$(VPATH)}util.h
math.$(OBJEXT): {$(VPATH)}math.c $(RUBY_H_INCLUDES)
node.$(OBJEXT): {$(VPATH)}node.c $(RUBY_H_INCLUDES) \
$(VM_CORE_H_INCLUDES)
numeric.$(OBJEXT): {$(VPATH)}numeric.c $(RUBY_H_INCLUDES) \
{$(VPATH)}util.h $(ENCODING_H_INCLUDES)
object.$(OBJEXT): {$(VPATH)}object.c $(RUBY_H_INCLUDES) {$(VPATH)}util.h
node.c
/**********************************************************************
compile.c - ruby node tree -> dump string
$Author: mame $
created at: 09/12/06 21:23:44 JST
Copyright (C) 2009 Yusuke Endoh
**********************************************************************/
#include "ruby/ruby.h"
#include "vm_core.h"
#define A(str) rb_str_cat2(buf, (str))
#define AR(str) rb_str_concat(buf, (str))
#define A_INDENT add_indent(buf, indent)
#define A_ID(id) add_id(buf, id)
#define A_INT(val) rb_str_catf(buf, "%d", (val));
#define A_LIT(lit) AR(rb_inspect(lit))
#define A_NODE_HEADER(node) \
rb_str_catf(buf, "@ %s (line: %d)", ruby_node_name(nd_type(node)), nd_line(node))
#define A_FIELD_HEADER(name) \
rb_str_catf(buf, "+- %s:", name)
#define D_NULL_NODE A_INDENT; A("(null node)"); A("\n");
#define D_NODE_HEADER(node) A_INDENT; A_NODE_HEADER(node); A("\n");
#define COMPOUND_FIELD(name, name2, block) \
do { \
A_INDENT; A_FIELD_HEADER(comment ? name2 : name); A("\n"); \
rb_str_cat2(indent, next_indent); \
block; \
rb_str_resize(indent, RSTRING_LEN(indent) - 4); \
} while (0)
#define SIMPLE_FIELD(name, name2, block) \
do { \
A_INDENT; A_FIELD_HEADER(comment ? name2 : name); A(" "); block; A("\n"); \
} while (0)
#define F_CUSTOM1(name, ann, block) SIMPLE_FIELD(#name, #name " (" ann ")", block)
#define F_ID(name, ann) SIMPLE_FIELD(#name, #name " (" ann ")", A_ID(node->name))
#define F_GENTRY(name, ann) SIMPLE_FIELD(#name, #name " (" ann ")", A_ID((node->name)->id))
#define F_INT(name, ann) SIMPLE_FIELD(#name, #name " (" ann ")", A_INT(node->name))
#define F_LIT(name, ann) SIMPLE_FIELD(#name, #name " (" ann ")", A_LIT(node->name))
#define F_MSG(name, ann, desc) SIMPLE_FIELD(#name, #name " (" ann ")", A(desc))
#define F_CUSTOM2(name, ann, block) \
COMPOUND_FIELD(#name, #name " (" ann ")", block)
#define F_NODE(name, ann) \
COMPOUND_FIELD(#name, #name " (" ann ")", dump_node(buf, indent, comment, node->name))
#define ANN(ann) \
if (comment) { \
A_INDENT; A("| # "); A(ann); A("\n"); \
}
#define LAST_NODE (next_indent = " ")
static void
add_indent(VALUE buf, VALUE indent)
{
AR(indent);
}
static void
add_id(VALUE buf, ID id)
{
if (id == 0) {
A("(null)");
}
else {
VALUE str = rb_id2str(id);
if (str) {
A(":"); AR(rb_id2str(id));
}
else {
A("(internal variable)");
}
}
}
static void
dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
{
const char *next_indent = "| ";
if (!node) {
D_NULL_NODE;
return;
}
D_NODE_HEADER(node);
switch (nd_type(node)) {
case NODE_BLOCK:
ANN("[nd_head]; [nd_next]");
F_NODE(nd_head, "body");
LAST_NODE;
F_NODE(nd_next, "next block");
break;
case NODE_IF:
ANN("if [nd_cond] then [nd_body] else [nd_else] end");
F_NODE(nd_cond, "condition");
F_NODE(nd_body, "then clause");
LAST_NODE;
F_NODE(nd_else, "else clause");
break;
case NODE_CASE:
ANN("case [nd_head]; [nd_body]; end");
F_NODE(nd_head, "case value");
LAST_NODE;
F_NODE(nd_body, "when clauses");
break;
case NODE_WHEN:
ANN("when [nd_head]; [nd_body]; (when or else) [nd_next]");
F_NODE(nd_head, "when value");
F_NODE(nd_body, "when clause");
LAST_NODE;
F_NODE(nd_next, "next when clause");
break;
case NODE_OPT_N:
ANN("ruby -ne ''"); goto loop;
case NODE_WHILE:
ANN("while [nd_cond]; [nd_body]; end"); goto loop;
case NODE_UNTIL:
ANN("until [nd_cond]; [nd_body]; end");
loop:
F_CUSTOM1(nd_state, "begin-end-while?", {
A_INT(node->nd_state);
A((node->nd_state == 1) ? " (while-end)" : " (begin-end-while)");
});
F_NODE(nd_cond, "condition");
LAST_NODE;
F_NODE(nd_body, "body");
break;
case NODE_ITER:
ANN("[nd_iter] { [nd_body] }"); goto iter;
case NODE_FOR:
ANN("for * in [nd_iter] do [nd_body] end");
iter:
F_NODE(nd_iter, "iteration receiver");
LAST_NODE;
F_NODE(nd_body, "body");
break;
case NODE_BREAK:
ANN("break [nd_stts]"); goto jump;
case NODE_NEXT:
ANN("next [nd_stts]"); goto jump;
case NODE_RETURN:
ANN("return [nd_stts]");
jump:
LAST_NODE;
F_NODE(nd_stts, "value");
break;
case NODE_REDO:
ANN("redo");
break;
case NODE_RETRY:
ANN("retry");
break;
case NODE_BEGIN:
ANN("begin; [nd_body]; end");
LAST_NODE;
F_NODE(nd_body, "body");
break;
case NODE_RESCUE:
ANN("begin; [nd_body]; (rescue) [nd_resq]; else [nd_else]; end");
F_NODE(nd_head, "body");
F_NODE(nd_resq, "rescue clause list");
LAST_NODE;
F_NODE(nd_else, "rescue else clause");
break;
case NODE_RESBODY:
ANN("rescue [nd_args]; [nd_body]; (rescue) [nd_head]");
F_NODE(nd_args, "rescue exceptions");
F_NODE(nd_body, "rescue clause");
LAST_NODE;
F_NODE(nd_head, "next rescue clause");
break;
case NODE_ENSURE:
ANN("begin; [nd_head]; ensure; [nd_ensr]; end");
F_NODE(nd_head, "body");
LAST_NODE;
F_NODE(nd_ensr, "ensure clause");
break;
case NODE_AND:
ANN("[nd_1st] && [nd_2nd]"); goto andor;
case NODE_OR:
ANN("[nd_1st] || [nd_2nd]");
andor:
F_NODE(nd_1st, "left expr");
LAST_NODE;
F_NODE(nd_2nd, "right expr");
break;
case NODE_MASGN:
ANN("[nd_head], [nd_args] = [nd_value]");
F_NODE(nd_value, "rhsn");
F_NODE(nd_head, "lhsn");
if ((VALUE)node->nd_args != (VALUE)-1) {
LAST_NODE;
F_NODE(nd_args, "splatn");
}
else {
F_MSG(nd_args, "splatn", "-1 (rest argument without name)");
}
break;
case NODE_LASGN:
ANN("[nd_vid](lvar) = [nd_value]"); goto asgn;
case NODE_DASGN:
ANN("[nd_vid](dvar) = [nd_value]"); goto asgn;
case NODE_DASGN_CURR:
ANN("[nd_vid](dvar curr) = [nd_value]"); goto asgn;
case NODE_IASGN:
ANN("[nd_vid](ivar) = [nd_value]"); goto asgn;
case NODE_CVASGN:
ANN("[nd_vid](cvar) = [nd_value]");
asgn:
F_ID(nd_vid, "variable");
LAST_NODE;
F_NODE(nd_value, "rvalue");
break;
case NODE_GASGN:
ANN("[nd_entry](gvar) = [nd_value]");
F_GENTRY(nd_entry, "global variable");
LAST_NODE;
F_NODE(nd_value, "rvalue");
break;
case NODE_CDECL:
ANN("[nd_else]::[nd_vid](constant) = [nd_value]");
if (node->nd_vid) {
F_ID(nd_vid, "variable");
F_MSG(nd_else, "extension", "not used");
}
else {
F_MSG(nd_vid, "variable", "0 (see extension field)");
F_NODE(nd_else, "extension");
}
LAST_NODE;
F_NODE(nd_value, "rvalue");
break;
case NODE_OP_ASGN1:
ANN("[nd_value] [ [nd_args->nd_body] ] [nd_vid]= [nd_args->nd_head]");
F_NODE(nd_recv, "receiver");
F_ID(nd_vid, "operator");
F_NODE(nd_args->nd_body, "index");
LAST_NODE;
F_NODE(nd_args->nd_head, "rvalue");
break;
case NODE_OP_ASGN2:
ANN("[nd_value].[attr] [nd_next->nd_mid]= [nd_value]");
ANN(" where [attr]reader: [nd_next->nd_vid], [attr]writer: [nd_next->nd_aid]");
F_NODE(nd_recv, "receiver");
F_ID(nd_next->nd_vid, "reader");
F_ID(nd_next->nd_aid, "writer");
F_CUSTOM1(nd_next->nd_mid, "operator", {
switch (node->nd_next->nd_mid) {
case 0: A("0 (||)"); break;
case 1: A("1 (&&)"); break;
default: A_ID(node->nd_next->nd_mid);
}
});
LAST_NODE;
F_NODE(nd_value, "rvalue");
break;
case NODE_OP_ASGN_AND:
ANN("[nd_head] &&= [nd_value]"); goto asgn_andor;
case NODE_OP_ASGN_OR:
ANN("[nd_head] ||= [nd_value]");
asgn_andor:
F_NODE(nd_head, "variable");
LAST_NODE;
F_NODE(nd_value, "rvalue");
break;
case NODE_CALL:
ANN("[nd_mid]([nd_args])");
F_ID(nd_mid, "method id");
F_NODE(nd_recv, "receiver");
LAST_NODE;
F_NODE(nd_args, "arguments");
break;
case NODE_FCALL:
ANN("[nd_recv].[nd_mid]([nd_args])");
F_ID(nd_mid, "method id");
LAST_NODE;
F_NODE(nd_args, "arguments");
break;
case NODE_VCALL:
ANN("[nd_mid]");
F_ID(nd_mid, "method id");
break;
case NODE_SUPER:
ANN("super [nd_args]");
LAST_NODE;
F_NODE(nd_args, "arguments");
break;
case NODE_ZSUPER:
ANN("super");
break;
case NODE_ARRAY:
case NODE_VALUES:
ANN("[ [nd_head], [nd_next].. ] (length: [nd_alen])");
F_INT(nd_alen, "length");
F_NODE(nd_head, "element");
LAST_NODE;
F_NODE(nd_next, "next element");
break;
case NODE_ZARRAY:
ANN("[]");
break;
case NODE_HASH:
ANN("{ [nd_head] }");
LAST_NODE;
F_NODE(nd_head, "contents");
break;
case NODE_YIELD:
ANN("yield [nd_head]");
LAST_NODE;
F_NODE(nd_head, "arguments");
break;
case NODE_LVAR:
ANN("[nd_vid](lvar)"); goto var;
case NODE_DVAR:
ANN("[nd_vid](dvar)"); goto var;
case NODE_IVAR:
ANN("[nd_vid](ivar)"); goto var;
case NODE_CONST:
ANN("[nd_vid](constant)"); goto var;
case NODE_CVAR:
ANN("[nd_vid](cvar)");
var:
F_ID(nd_vid, "local variable");
break;
case NODE_GVAR:
ANN("[nd_entry](gvar)");
F_GENTRY(nd_entry, "global variable");
break;
case NODE_NTH_REF:
ANN("$[nd_nth](0, 1, 2, ..)");
F_CUSTOM1(nd_nth, "variable", { A("$"); A_INT(node->nd_nth); });
break;
case NODE_BACK_REF:
ANN("$[nd_nth](&, `, ', +)");
F_CUSTOM1(nd_nth, "variable", {
char name[3];
name[0] = '$';
name[1] = node->nd_nth;
name[2] = '\0';
A(name);
});
break;
case NODE_MATCH:
ANN("/[nd_lit]/ (in condition)");
F_LIT(nd_lit, "regexp");
break;
case NODE_MATCH2:
ANN("/[nd_recv]/ =~ \"[nd_value]\"");
F_NODE(nd_recv, "regexp (receiver)");
LAST_NODE;
F_NODE(nd_value, "string (argument)");
break;
case NODE_MATCH3:
ANN("\"[nd_recv]\" =~ /[nd_value]/");
F_NODE(nd_recv, "string (receiver)");
LAST_NODE;
F_NODE(nd_value, "regexp (argument)");
break;
case NODE_LIT:
ANN("[nd_lit](literal)"); goto lit;
case NODE_STR:
ANN("\"[nd_lit]\"(string)"); goto lit;
case NODE_XSTR:
ANN("`[nd_lit]`(xstring)");
lit:
F_LIT(nd_lit, "literal");
break;
case NODE_DSTR:
ANN("\"[nd_lit]\" (with interpolation)"); goto dlit;
case NODE_DXSTR:
ANN("`[nd_lit]` (with interpolation)"); goto dlit;
case NODE_DREGX:
ANN("/[nd_lit]/ (with interpolation)"); goto dlit;
case NODE_DREGX_ONCE:
ANN("/[nd_lit]/o (with interpolation)"); goto dlit;
case NODE_DSYM:
ANN(":[nd_lit] (with interpolation)");
dlit:
F_LIT(nd_lit, "literal");
F_NODE(nd_next->nd_head, "preceding string");
LAST_NODE;
F_NODE(nd_next->nd_next, "interpolation");
break;
case NODE_EVSTR:
ANN("interpolation");
LAST_NODE;
F_NODE(nd_body, "body");
break;
case NODE_ARGSCAT:
ANN("method_name(*[nd_head], [nd_body..])");
F_NODE(nd_head, "preceding array");
LAST_NODE;
F_NODE(nd_body, "following array");
break;
case NODE_ARGSPUSH:
ANN("method_name(*[nd_head], [nd_body])");
F_NODE(nd_head, "preceding array");
LAST_NODE;
F_NODE(nd_body, "following element");
break;
case NODE_SPLAT:
ANN("method_name(*[nd_head])");
LAST_NODE;
F_NODE(nd_head, "splat'ed array");
break;
case NODE_BLOCK_PASS:
ANN("method_name([nd_head], &[nd_body])");
F_NODE(nd_head, "other arguments");
LAST_NODE;
F_NODE(nd_body, "block pass");
break;
case NODE_DEFN:
ANN("def [nd_mid] [nd_defn]; end");
F_ID(nd_mid, "method name");
LAST_NODE;
F_NODE(nd_defn, "method definition");
break;
case NODE_DEFS:
ANN("def [nd_recv].[nd_mid] [nd_defn]; end");
F_NODE(nd_recv, "receiver");
F_ID(nd_mid, "method name");
LAST_NODE;
F_NODE(nd_defn, "method definition");
break;
case NODE_ALIAS:
ANN("alias [u1.node] [u2.node]");
F_NODE(u1.node, "new name");
LAST_NODE;
F_NODE(u2.node, "old name");
break;
case NODE_VALIAS:
ANN("alias [u1.id](gvar) [u2.id](gvar)");
F_ID(u1.id, "new name");
F_ID(u2.id, "old name");
break;
case NODE_UNDEF:
ANN("undef [u2.id](gvar)");
LAST_NODE;
F_NODE(u2.node, "old name");
break;
case NODE_CLASS:
ANN("class [nd_cpath] < [nd_super]; [nd_body]; end");
F_NODE(nd_cpath, "class path");
F_NODE(nd_super, "superclass");
LAST_NODE;
F_NODE(nd_body, "class definition");
break;
case NODE_MODULE:
ANN("module [nd_cpath]; [nd_body]; end");
F_NODE(nd_cpath, "module path");
LAST_NODE;
F_NODE(nd_body, "module definition");
break;
case NODE_SCLASS:
ANN("class << [nd_recv]; [nd_body]; end");
F_NODE(nd_recv, "receiver");
LAST_NODE;
F_NODE(nd_body, "singleton class definition");
break;
case NODE_COLON2:
ANN("[nd_head]::[nd_mid]");
F_ID(nd_mid, "constant name");
LAST_NODE;
F_NODE(nd_head, "receiver");
break;
case NODE_COLON3:
ANN("::[nd_mid]");
F_ID(nd_mid, "constant name");
break;
case NODE_DOT2:
ANN("[nd_beg]..[nd_end]"); goto dot;
case NODE_DOT3:
ANN("[nd_beg]...[nd_end]"); goto dot;
case NODE_FLIP2:
ANN("[nd_beg]..[nd_end] (in condition)"); goto dot;
case NODE_FLIP3:
ANN("[nd_beg]...[nd_end] (in condition)");
dot:
F_NODE(nd_beg, "begin");
LAST_NODE;
F_NODE(nd_end, "end");
break;
case NODE_SELF:
ANN("self");
break;
case NODE_NIL:
ANN("nil");
break;
case NODE_TRUE:
ANN("true");
break;
case NODE_FALSE:
ANN("false");
break;
case NODE_ERRINFO:
ANN("rescue => id");
break;
case NODE_DEFINED:
ANN("defined?([nd_head])");
F_NODE(nd_head, "expr");
break;
case NODE_POSTEXE:
ANN("END { [nd_body] }");
LAST_NODE;
F_NODE(nd_body, "END clause");
break;
case NODE_ATTRASGN:
ANN("[nd_recv].[nd_mid] = [nd_args]");
if (node->nd_recv == (NODE *) 1) {
F_MSG(nd_recv, "receiver", "1 (self)");
}
else {
F_NODE(nd_recv, "receiver");
}
F_ID(nd_mid, "method name");
LAST_NODE;
F_NODE(nd_args, "arguments");
break;
case NODE_PRELUDE:
ANN("BEGIN { [nd_head] }; [nd_body]");
F_NODE(nd_head, "prelude");
LAST_NODE;
F_NODE(nd_body, "body");
break;
case NODE_LAMBDA:
ANN("-> [nd_body]");
LAST_NODE;
F_NODE(nd_body, "lambda clause");
break;
case NODE_OPT_ARG:
ANN("def method_name([nd_body=some], [nd_next..])");
F_NODE(nd_body, "body");
LAST_NODE;
F_NODE(nd_next, "next");
break;
case NODE_POSTARG:
ANN("*[nd_1st], [nd_2nd..] = ..");
if ((VALUE)node->nd_1st != (VALUE)-1) {
F_NODE(nd_1st, "rest argument");
}
else {
F_MSG(nd_1st, "rest argument", "-1 (rest argument without name)");
}
LAST_NODE;
F_NODE(nd_2nd, "post arguments");
break;
case NODE_ARGS_AUX:
F_CUSTOM1(nd_rest, "rest argument", {
if (node->nd_rest == 1) A("nil (with last comma)");
else A_ID(node->nd_rest);
});
F_CUSTOM1(nd_body, "block argument", { A_ID((ID)node->nd_body); });
LAST_NODE;
F_CUSTOM2(nd_next, "aux info 2", {
node = node->nd_next;
next_indent = "| ";
if (!node) {
D_NULL_NODE;
}
else {
D_NODE_HEADER(node);
F_ID(nd_pid, "first post argument");
F_INT(nd_plen, "post argument length");
LAST_NODE;
F_NODE(nd_next, "aux info 3");
}
});
break;
case NODE_ARGS:
ANN("def method_name(.., [nd_opt=some], *[nd_rest], [nd_pid], .., &[nd_body])");
F_INT(nd_frml, "argc");
F_NODE(nd_next, "aux info 1");
LAST_NODE;
F_NODE(nd_opt, "optional arguments");
break;
case NODE_SCOPE:
ANN("[nd_tbl]: local table, [nd_args]: arguments, [nd_body]: body");
F_CUSTOM1(nd_tbl, "local table", {
ID *tbl = node->nd_tbl;
int i;
int size = tbl ? (int)*tbl++ : 0;
if (size == 0) A("(empty)");
for (i = 0; i < size; i++) {
A_ID(tbl[i]); if (i < size - 1) A(",");
}
});
F_NODE(nd_args, "arguments");
LAST_NODE;
F_NODE(nd_body, "body");
break;
default:
rb_bug("dump_node: unknown node: %s", ruby_node_name(nd_type(node)));
}
}
VALUE
rb_parser_dump_tree(NODE *node, int comment)
{
VALUE buf = rb_str_new_cstr(
"###############################################\n"
"## Do NOT use this node dump for any purpose ##\n"
"## other than debug and research. ##\n"
"###############################################\n\n"
);
dump_node(buf, rb_str_new_cstr("# "), comment, node);
return buf;
}
ruby.c
dump_usage,
dump_yydebug,
dump_syntax,
dump_parsetree,
dump_parsetree_with_comment,
dump_insns,
dump_flag_count
};
......
SET_WHEN_DUMP(usage);
SET_WHEN_DUMP(yydebug);
SET_WHEN_DUMP(syntax);
SET_WHEN_DUMP(parsetree);
SET_WHEN_DUMP(parsetree_with_comment);
SET_WHEN_DUMP(insns);
rb_warn("don't know how to dump `%.*s',", len, str);
rb_warn("but only [version, copyright, usage, yydebug, syntax, insns].");
rb_warn("but only [version, copyright, usage, yydebug, syntax, parsetree, parsetree_with_comment, insns].");
}
static void
......
rb_define_global_function("chomp", rb_f_chomp, -1);
}
if (opt->dump & DUMP_BIT(parsetree) || opt->dump & DUMP_BIT(parsetree_with_comment)) {
rb_io_write(rb_stdout, rb_parser_dump_tree(tree, opt->dump & DUMP_BIT(parsetree_with_comment)));
rb_io_flush(rb_stdout);
return Qtrue;
}
PREPARE_PARSE_MAIN({
iseq = rb_iseq_new_main(tree, opt->script_name);
});
    (1-1/1)