2018-06-07 17:40:39 +03:00
|
|
|
/* indent-tabs-mode: nil */
|
2018-01-16 02:43:17 +03:00
|
|
|
#include "ruby.h"
|
|
|
|
#include "ruby/encoding.h"
|
2018-06-07 17:46:25 +03:00
|
|
|
#include "ruby/util.h"
|
2018-01-16 02:43:17 +03:00
|
|
|
#include "internal.h"
|
|
|
|
#include "node.h"
|
|
|
|
#include "vm_core.h"
|
2018-11-05 05:13:45 +03:00
|
|
|
#include "iseq.h"
|
2019-11-07 12:29:20 +03:00
|
|
|
#include "builtin.h"
|
2018-01-16 02:43:17 +03:00
|
|
|
|
|
|
|
static VALUE rb_mAST;
|
|
|
|
static VALUE rb_cNode;
|
|
|
|
|
|
|
|
struct ASTNodeData {
|
|
|
|
rb_ast_t *ast;
|
|
|
|
NODE *node;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
node_gc_mark(void *ptr)
|
|
|
|
{
|
|
|
|
struct ASTNodeData *data = (struct ASTNodeData *)ptr;
|
|
|
|
rb_gc_mark((VALUE)data->ast);
|
|
|
|
}
|
|
|
|
|
2019-07-23 10:18:40 +03:00
|
|
|
static size_t
|
|
|
|
node_memsize(const void *ptr)
|
|
|
|
{
|
|
|
|
struct ASTNodeData *data = (struct ASTNodeData *)ptr;
|
|
|
|
return rb_ast_memsize(data->ast);
|
|
|
|
}
|
|
|
|
|
2018-01-16 02:43:17 +03:00
|
|
|
static const rb_data_type_t rb_node_type = {
|
|
|
|
"AST/node",
|
2019-07-23 10:18:40 +03:00
|
|
|
{node_gc_mark, RUBY_TYPED_DEFAULT_FREE, node_memsize,},
|
2018-01-16 02:43:17 +03:00
|
|
|
0, 0,
|
|
|
|
RUBY_TYPED_FREE_IMMEDIATELY,
|
|
|
|
};
|
|
|
|
|
|
|
|
static VALUE rb_ast_node_alloc(VALUE klass);
|
|
|
|
|
|
|
|
static void
|
|
|
|
setup_node(VALUE obj, rb_ast_t *ast, NODE *node)
|
|
|
|
{
|
|
|
|
struct ASTNodeData *data;
|
|
|
|
|
|
|
|
TypedData_Get_Struct(obj, struct ASTNodeData, &rb_node_type, data);
|
|
|
|
data->ast = ast;
|
|
|
|
data->node = node;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
ast_new_internal(rb_ast_t *ast, NODE *node)
|
|
|
|
{
|
|
|
|
VALUE obj;
|
|
|
|
|
|
|
|
obj = rb_ast_node_alloc(rb_cNode);
|
|
|
|
setup_node(obj, ast, node);
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2018-11-11 03:55:19 +03:00
|
|
|
static VALUE rb_ast_parse_str(VALUE str);
|
|
|
|
static VALUE rb_ast_parse_file(VALUE path);
|
|
|
|
static VALUE rb_ast_parse_array(VALUE array);
|
2018-11-09 16:39:35 +03:00
|
|
|
|
2018-11-10 14:16:36 +03:00
|
|
|
static VALUE
|
|
|
|
ast_parse_new(void)
|
|
|
|
{
|
|
|
|
return rb_parser_set_context(rb_parser_new(), NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
ast_parse_done(rb_ast_t *ast)
|
|
|
|
{
|
|
|
|
if (!ast->body.root) {
|
|
|
|
rb_ast_dispose(ast);
|
|
|
|
rb_exc_raise(GET_EC()->errinfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ast_new_internal(ast, (NODE *)ast->body.root);
|
|
|
|
}
|
|
|
|
|
2018-01-16 02:43:17 +03:00
|
|
|
static VALUE
|
2019-11-07 12:29:20 +03:00
|
|
|
ast_s_parse(rb_execution_context_t *ec, VALUE module, VALUE str)
|
2018-11-09 16:39:35 +03:00
|
|
|
{
|
|
|
|
return rb_ast_parse_str(str);
|
|
|
|
}
|
|
|
|
|
2018-11-11 03:55:19 +03:00
|
|
|
static VALUE
|
2018-11-09 16:39:35 +03:00
|
|
|
rb_ast_parse_str(VALUE str)
|
2018-01-16 02:43:17 +03:00
|
|
|
{
|
|
|
|
rb_ast_t *ast = 0;
|
|
|
|
|
2019-01-06 08:07:10 +03:00
|
|
|
StringValue(str);
|
2018-11-10 14:16:36 +03:00
|
|
|
ast = rb_parser_compile_string_path(ast_parse_new(), Qnil, str, 1);
|
|
|
|
return ast_parse_done(ast);
|
2018-01-16 02:43:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2019-11-07 12:29:20 +03:00
|
|
|
ast_s_parse_file(rb_execution_context_t *ec, VALUE module, VALUE path)
|
2018-11-09 16:39:35 +03:00
|
|
|
{
|
|
|
|
return rb_ast_parse_file(path);
|
|
|
|
}
|
|
|
|
|
2018-11-11 03:55:19 +03:00
|
|
|
static VALUE
|
2018-11-09 16:39:35 +03:00
|
|
|
rb_ast_parse_file(VALUE path)
|
2018-01-16 02:43:17 +03:00
|
|
|
{
|
2018-11-10 14:16:36 +03:00
|
|
|
VALUE f;
|
2018-01-16 02:43:17 +03:00
|
|
|
rb_ast_t *ast = 0;
|
|
|
|
rb_encoding *enc = rb_utf8_encoding();
|
|
|
|
|
|
|
|
FilePathValue(path);
|
|
|
|
f = rb_file_open_str(path, "r");
|
|
|
|
rb_funcall(f, rb_intern("set_encoding"), 2, rb_enc_from_encoding(enc), rb_str_new_cstr("-"));
|
2018-11-10 14:16:36 +03:00
|
|
|
ast = rb_parser_compile_file_path(ast_parse_new(), Qnil, f, 1);
|
2018-01-16 02:43:17 +03:00
|
|
|
rb_io_close(f);
|
2018-11-10 14:16:36 +03:00
|
|
|
return ast_parse_done(ast);
|
2018-01-16 02:43:17 +03:00
|
|
|
}
|
|
|
|
|
2018-11-11 03:55:19 +03:00
|
|
|
static VALUE
|
2018-11-10 14:40:33 +03:00
|
|
|
lex_array(VALUE array, int index)
|
|
|
|
{
|
|
|
|
VALUE str = rb_ary_entry(array, index);
|
|
|
|
if (!NIL_P(str)) {
|
|
|
|
StringValue(str);
|
|
|
|
if (!rb_enc_asciicompat(rb_enc_get(str))) {
|
|
|
|
rb_raise(rb_eArgError, "invalid source encoding");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2018-11-11 03:55:19 +03:00
|
|
|
static VALUE
|
2018-11-10 14:40:33 +03:00
|
|
|
rb_ast_parse_array(VALUE array)
|
|
|
|
{
|
|
|
|
rb_ast_t *ast = 0;
|
|
|
|
|
|
|
|
array = rb_check_array_type(array);
|
|
|
|
ast = rb_parser_compile_generic(ast_parse_new(), lex_array, Qnil, array, 1);
|
|
|
|
return ast_parse_done(ast);
|
|
|
|
}
|
|
|
|
|
2018-11-05 05:13:45 +03:00
|
|
|
static VALUE node_children(rb_ast_t*, NODE*);
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
node_find(VALUE self, const int node_id)
|
|
|
|
{
|
|
|
|
VALUE ary;
|
|
|
|
long i;
|
|
|
|
struct ASTNodeData *data;
|
|
|
|
TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
|
|
|
|
|
|
|
|
if (nd_node_id(data->node) == node_id) return self;
|
|
|
|
|
|
|
|
ary = node_children(data->ast, data->node);
|
|
|
|
|
|
|
|
for (i = 0; i < RARRAY_LEN(ary); i++) {
|
|
|
|
VALUE child = RARRAY_AREF(ary, i);
|
|
|
|
|
|
|
|
if (CLASS_OF(child) == rb_cNode) {
|
|
|
|
VALUE result = node_find(child, node_id);
|
|
|
|
if (RTEST(result)) return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2018-11-10 14:43:02 +03:00
|
|
|
extern VALUE rb_e_script;
|
|
|
|
|
2018-11-10 13:39:58 +03:00
|
|
|
static VALUE
|
|
|
|
script_lines(VALUE path)
|
|
|
|
{
|
|
|
|
VALUE hash, lines;
|
|
|
|
ID script_lines;
|
|
|
|
CONST_ID(script_lines, "SCRIPT_LINES__");
|
|
|
|
if (!rb_const_defined_at(rb_cObject, script_lines)) return Qnil;
|
|
|
|
hash = rb_const_get_at(rb_cObject, script_lines);
|
|
|
|
if (!RB_TYPE_P(hash, T_HASH)) return Qnil;
|
|
|
|
lines = rb_hash_lookup(hash, path);
|
|
|
|
if (!RB_TYPE_P(lines, T_ARRAY)) return Qnil;
|
|
|
|
return lines;
|
|
|
|
}
|
|
|
|
|
2018-11-05 05:13:45 +03:00
|
|
|
static VALUE
|
2019-11-07 12:29:20 +03:00
|
|
|
ast_s_of(rb_execution_context_t *ec, VALUE module, VALUE body)
|
2018-11-05 05:13:45 +03:00
|
|
|
{
|
2018-11-10 13:39:58 +03:00
|
|
|
VALUE path, node, lines;
|
2018-11-05 05:13:45 +03:00
|
|
|
int node_id;
|
|
|
|
const rb_iseq_t *iseq = NULL;
|
|
|
|
|
|
|
|
if (rb_obj_is_proc(body)) {
|
|
|
|
iseq = vm_proc_iseq(body);
|
|
|
|
|
|
|
|
if (!rb_obj_is_iseq((VALUE)iseq)) {
|
|
|
|
iseq = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
iseq = rb_method_iseq(body);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!iseq) return Qnil;
|
|
|
|
|
|
|
|
path = rb_iseq_path(iseq);
|
|
|
|
node_id = iseq->body->location.node_id;
|
2018-11-10 13:39:58 +03:00
|
|
|
if (!NIL_P(lines = script_lines(path))) {
|
2018-11-10 14:40:33 +03:00
|
|
|
node = rb_ast_parse_array(lines);
|
2018-11-10 13:39:58 +03:00
|
|
|
}
|
2018-11-10 14:43:02 +03:00
|
|
|
else if (RSTRING_LEN(path) == 2 && memcmp(RSTRING_PTR(path), "-e", 2) == 0) {
|
|
|
|
node = rb_ast_parse_str(rb_e_script);
|
|
|
|
}
|
2018-11-10 13:39:58 +03:00
|
|
|
else {
|
|
|
|
node = rb_ast_parse_file(path);
|
|
|
|
}
|
2018-11-05 05:13:45 +03:00
|
|
|
|
|
|
|
return node_find(node, node_id);
|
|
|
|
}
|
|
|
|
|
2018-01-16 02:43:17 +03:00
|
|
|
static VALUE
|
|
|
|
rb_ast_node_alloc(VALUE klass)
|
|
|
|
{
|
|
|
|
struct ASTNodeData *data;
|
|
|
|
VALUE obj = TypedData_Make_Struct(klass, struct ASTNodeData, &rb_node_type, data);
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char*
|
2018-12-03 04:06:34 +03:00
|
|
|
node_type_to_str(const NODE *node)
|
2018-01-16 02:43:17 +03:00
|
|
|
{
|
2018-12-03 04:06:34 +03:00
|
|
|
return (ruby_node_name(nd_type(node)) + rb_strlen_lit("NODE_"));
|
2018-01-16 02:43:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2019-11-07 12:29:20 +03:00
|
|
|
ast_node_type(rb_execution_context_t *ec, VALUE self)
|
2018-01-16 02:43:17 +03:00
|
|
|
{
|
|
|
|
struct ASTNodeData *data;
|
|
|
|
TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
|
|
|
|
|
2018-12-03 04:06:34 +03:00
|
|
|
return rb_sym_intern_ascii_cstr(node_type_to_str(data->node));
|
2018-01-16 02:43:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#define NEW_CHILD(ast, node) node ? ast_new_internal(ast, node) : Qnil
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_ary_new_from_node_args(rb_ast_t *ast, long n, ...)
|
|
|
|
{
|
|
|
|
va_list ar;
|
|
|
|
VALUE ary;
|
|
|
|
long i;
|
|
|
|
|
|
|
|
ary = rb_ary_new2(n);
|
|
|
|
|
|
|
|
va_start(ar, n);
|
|
|
|
for (i=0; i<n; i++) {
|
2018-06-06 09:14:21 +03:00
|
|
|
NODE *node;
|
|
|
|
node = va_arg(ar, NODE *);
|
|
|
|
rb_ary_push(ary, NEW_CHILD(ast, node));
|
2018-01-16 02:43:17 +03:00
|
|
|
}
|
|
|
|
va_end(ar);
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
dump_block(rb_ast_t *ast, NODE *node)
|
|
|
|
{
|
|
|
|
VALUE ary = rb_ary_new();
|
|
|
|
do {
|
2018-06-06 09:14:21 +03:00
|
|
|
rb_ary_push(ary, NEW_CHILD(ast, node->nd_head));
|
2018-01-16 02:43:17 +03:00
|
|
|
} while (node->nd_next &&
|
2018-06-06 09:14:21 +03:00
|
|
|
nd_type(node->nd_next) == NODE_BLOCK &&
|
|
|
|
(node = node->nd_next, 1));
|
2018-01-16 02:43:17 +03:00
|
|
|
if (node->nd_next) {
|
2018-06-06 09:14:21 +03:00
|
|
|
rb_ary_push(ary, NEW_CHILD(ast, node->nd_next));
|
2018-01-16 02:43:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
dump_array(rb_ast_t *ast, NODE *node)
|
|
|
|
{
|
|
|
|
VALUE ary = rb_ary_new();
|
|
|
|
rb_ary_push(ary, NEW_CHILD(ast, node->nd_head));
|
|
|
|
|
2019-09-07 04:42:00 +03:00
|
|
|
while (node->nd_next && nd_type(node->nd_next) == NODE_LIST) {
|
2018-06-06 09:14:21 +03:00
|
|
|
node = node->nd_next;
|
|
|
|
rb_ary_push(ary, NEW_CHILD(ast, node->nd_head));
|
2018-01-16 02:43:17 +03:00
|
|
|
}
|
|
|
|
rb_ary_push(ary, NEW_CHILD(ast, node->nd_next));
|
|
|
|
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
2018-06-07 17:46:25 +03:00
|
|
|
static VALUE
|
|
|
|
var_name(ID id)
|
|
|
|
{
|
|
|
|
if (!id) return Qnil;
|
|
|
|
if (!rb_id2str(id)) return Qnil;
|
|
|
|
return ID2SYM(id);
|
|
|
|
}
|
|
|
|
|
2018-01-16 02:43:17 +03:00
|
|
|
static VALUE
|
|
|
|
node_children(rb_ast_t *ast, NODE *node)
|
|
|
|
{
|
2018-06-07 17:46:25 +03:00
|
|
|
char name[DECIMAL_SIZE_OF_BITS(sizeof(long) * CHAR_BIT) + 2]; /* including '$' */
|
|
|
|
|
2018-05-01 09:55:43 +03:00
|
|
|
enum node_type type = nd_type(node);
|
|
|
|
switch (type) {
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_BLOCK:
|
2018-06-06 09:14:21 +03:00
|
|
|
return dump_block(ast, node);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_IF:
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 3, node->nd_cond, node->nd_body, node->nd_else);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_UNLESS:
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 3, node->nd_cond, node->nd_body, node->nd_else);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_CASE:
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_CASE2:
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
|
2019-04-17 09:48:03 +03:00
|
|
|
case NODE_CASE3:
|
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_WHEN:
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 3, node->nd_head, node->nd_body, node->nd_next);
|
2019-04-17 09:48:03 +03:00
|
|
|
case NODE_IN:
|
|
|
|
return rb_ary_new_from_node_args(ast, 3, node->nd_head, node->nd_body, node->nd_next);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_WHILE:
|
2018-06-06 09:14:21 +03:00
|
|
|
goto loop;
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_UNTIL:
|
|
|
|
loop:
|
2019-05-18 03:35:40 +03:00
|
|
|
return rb_ary_push(rb_ary_new_from_node_args(ast, 2, node->nd_cond, node->nd_body),
|
|
|
|
(node->nd_state ? Qtrue : Qfalse));
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_ITER:
|
|
|
|
case NODE_FOR:
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_iter, node->nd_body);
|
2018-01-16 10:24:53 +03:00
|
|
|
case NODE_FOR_MASGN:
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 1, node->nd_var);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_BREAK:
|
2018-06-06 09:14:21 +03:00
|
|
|
goto jump;
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_NEXT:
|
2018-06-06 09:14:21 +03:00
|
|
|
goto jump;
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_RETURN:
|
|
|
|
jump:
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 1, node->nd_stts);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_REDO:
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 0);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_RETRY:
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 0);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_BEGIN:
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 1, node->nd_body);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_RESCUE:
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 3, node->nd_head, node->nd_resq, node->nd_else);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_RESBODY:
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 3, node->nd_args, node->nd_body, node->nd_head);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_ENSURE:
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_ensr);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_AND:
|
2018-06-06 09:14:21 +03:00
|
|
|
goto andor;
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_OR:
|
|
|
|
andor:
|
2018-06-06 09:14:21 +03:00
|
|
|
{
|
|
|
|
VALUE ary = rb_ary_new();
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
rb_ary_push(ary, NEW_CHILD(ast, node->nd_1st));
|
|
|
|
if (!node->nd_2nd || nd_type(node->nd_2nd) != (int)type)
|
|
|
|
break;
|
|
|
|
node = node->nd_2nd;
|
|
|
|
}
|
|
|
|
rb_ary_push(ary, NEW_CHILD(ast, node->nd_2nd));
|
|
|
|
return ary;
|
|
|
|
}
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_MASGN:
|
2018-03-20 16:30:57 +03:00
|
|
|
if (NODE_NAMED_REST_P(node->nd_args)) {
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 3, node->nd_value, node->nd_head, node->nd_args);
|
2018-01-16 02:43:17 +03:00
|
|
|
}
|
2019-04-20 06:25:36 +03:00
|
|
|
else {
|
|
|
|
return rb_ary_new_from_args(3, NEW_CHILD(ast, node->nd_value),
|
|
|
|
NEW_CHILD(ast, node->nd_head),
|
|
|
|
ID2SYM(rb_intern("NODE_SPECIAL_NO_NAME_REST")));
|
|
|
|
}
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_LASGN:
|
2018-06-06 09:14:21 +03:00
|
|
|
goto asgn;
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_DASGN:
|
2018-06-06 09:14:21 +03:00
|
|
|
goto asgn;
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_DASGN_CURR:
|
2018-06-06 09:14:21 +03:00
|
|
|
goto asgn;
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_IASGN:
|
2018-06-06 09:14:21 +03:00
|
|
|
goto asgn;
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_CVASGN:
|
|
|
|
asgn:
|
2018-03-20 16:30:57 +03:00
|
|
|
if (NODE_REQUIRED_KEYWORD_P(node)) {
|
2019-04-20 06:25:36 +03:00
|
|
|
return rb_ary_new_from_args(2, var_name(node->nd_vid), ID2SYM(rb_intern("NODE_SPECIAL_REQUIRED_KEYWORD")));
|
2018-01-16 02:43:17 +03:00
|
|
|
}
|
2018-06-07 17:46:25 +03:00
|
|
|
return rb_ary_new_from_args(2, var_name(node->nd_vid), NEW_CHILD(ast, node->nd_value));
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_GASGN:
|
2018-06-07 17:46:25 +03:00
|
|
|
goto asgn;
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_CDECL:
|
|
|
|
if (node->nd_vid) {
|
2018-06-07 17:46:25 +03:00
|
|
|
return rb_ary_new_from_args(2, ID2SYM(node->nd_vid), NEW_CHILD(ast, node->nd_value));
|
2018-01-16 02:43:17 +03:00
|
|
|
}
|
2018-06-07 17:46:25 +03:00
|
|
|
return rb_ary_new_from_args(3, NEW_CHILD(ast, node->nd_else), ID2SYM(node->nd_else->nd_mid), NEW_CHILD(ast, node->nd_value));
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_OP_ASGN1:
|
2018-06-07 17:46:25 +03:00
|
|
|
return rb_ary_new_from_args(4, NEW_CHILD(ast, node->nd_recv),
|
|
|
|
ID2SYM(node->nd_mid),
|
|
|
|
NEW_CHILD(ast, node->nd_args->nd_head),
|
|
|
|
NEW_CHILD(ast, node->nd_args->nd_body));
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_OP_ASGN2:
|
2018-06-07 17:46:25 +03:00
|
|
|
return rb_ary_new_from_args(4, NEW_CHILD(ast, node->nd_recv),
|
|
|
|
node->nd_next->nd_aid ? Qtrue : Qfalse,
|
|
|
|
ID2SYM(node->nd_next->nd_vid),
|
|
|
|
NEW_CHILD(ast, node->nd_value));
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_OP_ASGN_AND:
|
2018-06-07 17:46:25 +03:00
|
|
|
return rb_ary_new_from_args(3, NEW_CHILD(ast, node->nd_head), ID2SYM(idANDOP),
|
|
|
|
NEW_CHILD(ast, node->nd_value));
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_OP_ASGN_OR:
|
2018-06-07 17:46:25 +03:00
|
|
|
return rb_ary_new_from_args(3, NEW_CHILD(ast, node->nd_head), ID2SYM(idOROP),
|
|
|
|
NEW_CHILD(ast, node->nd_value));
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_OP_CDECL:
|
2018-06-07 17:46:25 +03:00
|
|
|
return rb_ary_new_from_args(3, NEW_CHILD(ast, node->nd_head),
|
|
|
|
ID2SYM(node->nd_aid),
|
|
|
|
NEW_CHILD(ast, node->nd_value));
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_CALL:
|
|
|
|
case NODE_OPCALL:
|
2018-06-07 17:46:25 +03:00
|
|
|
case NODE_QCALL:
|
|
|
|
return rb_ary_new_from_args(3, NEW_CHILD(ast, node->nd_recv),
|
2018-06-08 14:03:39 +03:00
|
|
|
ID2SYM(node->nd_mid),
|
2018-06-07 17:46:25 +03:00
|
|
|
NEW_CHILD(ast, node->nd_args));
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_FCALL:
|
2018-06-08 14:03:39 +03:00
|
|
|
return rb_ary_new_from_args(2, ID2SYM(node->nd_mid),
|
2018-06-07 17:46:25 +03:00
|
|
|
NEW_CHILD(ast, node->nd_args));
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_VCALL:
|
2018-06-08 14:03:39 +03:00
|
|
|
return rb_ary_new_from_args(1, ID2SYM(node->nd_mid));
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_SUPER:
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 1, node->nd_args);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_ZSUPER:
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 0);
|
2019-09-07 04:42:00 +03:00
|
|
|
case NODE_LIST:
|
2018-06-06 09:14:21 +03:00
|
|
|
goto ary;
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_VALUES:
|
|
|
|
ary:
|
2018-06-06 09:14:21 +03:00
|
|
|
return dump_array(ast, node);
|
2019-09-07 04:42:00 +03:00
|
|
|
case NODE_ZLIST:
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 0);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_HASH:
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 1, node->nd_head);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_YIELD:
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 1, node->nd_head);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_LVAR:
|
|
|
|
case NODE_DVAR:
|
2018-06-07 17:46:25 +03:00
|
|
|
return rb_ary_new_from_args(1, var_name(node->nd_vid));
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_IVAR:
|
|
|
|
case NODE_CONST:
|
|
|
|
case NODE_CVAR:
|
|
|
|
case NODE_GVAR:
|
2018-06-07 17:46:25 +03:00
|
|
|
return rb_ary_new_from_args(1, ID2SYM(node->nd_vid));
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_NTH_REF:
|
2018-06-07 17:46:25 +03:00
|
|
|
snprintf(name, sizeof(name), "$%ld", node->nd_nth);
|
|
|
|
return rb_ary_new_from_args(1, ID2SYM(rb_intern(name)));
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_BACK_REF:
|
2018-06-07 17:46:25 +03:00
|
|
|
name[0] = '$';
|
|
|
|
name[1] = (char)node->nd_nth;
|
|
|
|
name[2] = '\0';
|
|
|
|
return rb_ary_new_from_args(1, ID2SYM(rb_intern(name)));
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_MATCH:
|
2018-06-07 17:46:25 +03:00
|
|
|
goto lit;
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_MATCH2:
|
2018-06-06 09:14:21 +03:00
|
|
|
if (node->nd_args) {
|
|
|
|
return rb_ary_new_from_node_args(ast, 3, node->nd_recv, node->nd_value, node->nd_args);
|
|
|
|
}
|
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_recv, node->nd_value);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_MATCH3:
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_recv, node->nd_value);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_LIT:
|
2018-06-06 09:14:21 +03:00
|
|
|
goto lit;
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_STR:
|
2018-06-06 09:14:21 +03:00
|
|
|
goto lit;
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_XSTR:
|
|
|
|
lit:
|
2018-06-07 17:46:25 +03:00
|
|
|
return rb_ary_new_from_args(1, node->nd_lit);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_ONCE:
|
|
|
|
return rb_ary_new_from_node_args(ast, 1, node->nd_body);
|
|
|
|
case NODE_DSTR:
|
2018-06-06 09:14:21 +03:00
|
|
|
goto dlit;
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_DXSTR:
|
2018-06-06 09:14:21 +03:00
|
|
|
goto dlit;
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_DREGX:
|
2018-06-06 09:14:21 +03:00
|
|
|
goto dlit;
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_DSYM:
|
|
|
|
dlit:
|
2019-01-14 13:16:54 +03:00
|
|
|
return rb_ary_new_from_args(3, node->nd_lit,
|
|
|
|
NEW_CHILD(ast, node->nd_next->nd_head),
|
|
|
|
NEW_CHILD(ast, node->nd_next->nd_next));
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_EVSTR:
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 1, node->nd_body);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_ARGSCAT:
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_ARGSPUSH:
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_SPLAT:
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 1, node->nd_head);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_BLOCK_PASS:
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_DEFN:
|
2018-06-28 17:33:28 +03:00
|
|
|
return rb_ary_new_from_args(2, ID2SYM(node->nd_mid), NEW_CHILD(ast, node->nd_defn));
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_DEFS:
|
2018-06-28 17:33:28 +03:00
|
|
|
return rb_ary_new_from_args(3, NEW_CHILD(ast, node->nd_recv), ID2SYM(node->nd_mid), NEW_CHILD(ast, node->nd_defn));
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_ALIAS:
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_1st, node->nd_2nd);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_VALIAS:
|
2018-06-07 17:46:25 +03:00
|
|
|
return rb_ary_new_from_args(2, ID2SYM(node->nd_alias), ID2SYM(node->nd_orig));
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_UNDEF:
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 1, node->nd_undef);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_CLASS:
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 3, node->nd_cpath, node->nd_super, node->nd_body);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_MODULE:
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_cpath, node->nd_body);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_SCLASS:
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_recv, node->nd_body);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_COLON2:
|
2018-06-07 17:46:25 +03:00
|
|
|
return rb_ary_new_from_args(2, NEW_CHILD(ast, node->nd_head), ID2SYM(node->nd_mid));
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_COLON3:
|
2018-06-07 17:46:25 +03:00
|
|
|
return rb_ary_new_from_args(1, ID2SYM(node->nd_mid));
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_DOT2:
|
2018-06-06 09:14:21 +03:00
|
|
|
goto dot;
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_DOT3:
|
2018-06-06 09:14:21 +03:00
|
|
|
goto dot;
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_FLIP2:
|
2018-06-06 09:14:21 +03:00
|
|
|
goto dot;
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_FLIP3:
|
|
|
|
dot:
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_beg, node->nd_end);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_SELF:
|
2018-06-07 17:40:39 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 0);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_NIL:
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 0);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_TRUE:
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 0);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_FALSE:
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 0);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_ERRINFO:
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 0);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_DEFINED:
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 1, node->nd_head);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_POSTEXE:
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 1, node->nd_body);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_ATTRASGN:
|
2018-06-07 17:46:25 +03:00
|
|
|
return rb_ary_new_from_args(3, NEW_CHILD(ast, node->nd_recv), ID2SYM(node->nd_mid), NEW_CHILD(ast, node->nd_args));
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_LAMBDA:
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 1, node->nd_body);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_OPT_ARG:
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_body, node->nd_next);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_KW_ARG:
|
2018-06-06 09:14:21 +03:00
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_body, node->nd_next);
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_POSTARG:
|
2018-06-06 09:14:21 +03:00
|
|
|
if (NODE_NAMED_REST_P(node->nd_1st)) {
|
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_1st, node->nd_2nd);
|
|
|
|
}
|
2019-04-20 06:25:36 +03:00
|
|
|
return rb_ary_new_from_args(2, ID2SYM(rb_intern("NODE_SPECIAL_NO_NAME_REST")),
|
|
|
|
NEW_CHILD(ast, node->nd_2nd));
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_ARGS:
|
2018-06-07 17:46:25 +03:00
|
|
|
{
|
|
|
|
struct rb_args_info *ainfo = node->nd_ainfo;
|
|
|
|
return rb_ary_new_from_args(10,
|
|
|
|
INT2NUM(ainfo->pre_args_num),
|
|
|
|
NEW_CHILD(ast, ainfo->pre_init),
|
|
|
|
NEW_CHILD(ast, ainfo->opt_args),
|
|
|
|
var_name(ainfo->first_post_arg),
|
|
|
|
INT2NUM(ainfo->post_args_num),
|
|
|
|
NEW_CHILD(ast, ainfo->post_init),
|
|
|
|
var_name(ainfo->rest_arg),
|
2019-04-24 21:40:02 +03:00
|
|
|
(ainfo->no_kwarg ? Qfalse : NEW_CHILD(ast, ainfo->kw_args)),
|
|
|
|
(ainfo->no_kwarg ? Qfalse : NEW_CHILD(ast, ainfo->kw_rest_arg)),
|
2018-06-07 17:46:25 +03:00
|
|
|
var_name(ainfo->block_arg));
|
|
|
|
}
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_SCOPE:
|
2018-06-07 17:46:25 +03:00
|
|
|
{
|
|
|
|
ID *tbl = node->nd_tbl;
|
|
|
|
int i, size = tbl ? (int)*tbl++ : 0;
|
|
|
|
VALUE locals = rb_ary_new_capa(size);
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
rb_ary_push(locals, var_name(tbl[i]));
|
|
|
|
}
|
|
|
|
return rb_ary_new_from_args(3, locals, NEW_CHILD(ast, node->nd_args), NEW_CHILD(ast, node->nd_body));
|
|
|
|
}
|
2019-04-17 09:48:03 +03:00
|
|
|
case NODE_ARYPTN:
|
|
|
|
{
|
|
|
|
struct rb_ary_pattern_info *apinfo = node->nd_apinfo;
|
2019-04-20 06:37:22 +03:00
|
|
|
VALUE rest = NODE_NAMED_REST_P(apinfo->rest_arg) ? NEW_CHILD(ast, apinfo->rest_arg) :
|
|
|
|
ID2SYM(rb_intern("NODE_SPECIAL_NO_NAME_REST"));
|
2019-04-17 09:48:03 +03:00
|
|
|
return rb_ary_new_from_args(4,
|
|
|
|
NEW_CHILD(ast, node->nd_pconst),
|
|
|
|
NEW_CHILD(ast, apinfo->pre_args),
|
2019-04-20 06:37:22 +03:00
|
|
|
rest,
|
2019-04-17 09:48:03 +03:00
|
|
|
NEW_CHILD(ast, apinfo->post_args));
|
|
|
|
}
|
|
|
|
case NODE_HSHPTN:
|
|
|
|
{
|
2019-09-01 10:39:34 +03:00
|
|
|
VALUE kwrest = node->nd_pkwrestarg == NODE_SPECIAL_NO_REST_KEYWORD ? ID2SYM(rb_intern("NODE_SPECIAL_NO_REST_KEYWORD")) :
|
|
|
|
NEW_CHILD(ast, node->nd_pkwrestarg);
|
|
|
|
|
2019-04-17 09:48:03 +03:00
|
|
|
return rb_ary_new_from_args(3,
|
|
|
|
NEW_CHILD(ast, node->nd_pconst),
|
|
|
|
NEW_CHILD(ast, node->nd_pkwargs),
|
2019-09-01 10:39:34 +03:00
|
|
|
kwrest);
|
2019-04-17 09:48:03 +03:00
|
|
|
}
|
2018-01-16 02:43:17 +03:00
|
|
|
case NODE_ARGS_AUX:
|
|
|
|
case NODE_LAST:
|
2018-06-06 09:14:21 +03:00
|
|
|
break;
|
2018-01-16 02:43:17 +03:00
|
|
|
}
|
|
|
|
|
2018-05-01 09:55:43 +03:00
|
|
|
rb_bug("node_children: unknown node: %s", ruby_node_name(type));
|
2018-01-16 02:43:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2019-11-07 12:29:20 +03:00
|
|
|
ast_node_children(rb_execution_context_t *ec, VALUE self)
|
2018-01-16 02:43:17 +03:00
|
|
|
{
|
|
|
|
struct ASTNodeData *data;
|
|
|
|
TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
|
|
|
|
|
|
|
|
return node_children(data->ast, data->node);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2019-11-07 12:29:20 +03:00
|
|
|
ast_node_first_lineno(rb_execution_context_t *ec, VALUE self)
|
2018-01-16 02:43:17 +03:00
|
|
|
{
|
|
|
|
struct ASTNodeData *data;
|
|
|
|
TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
|
|
|
|
|
|
|
|
return INT2NUM(nd_first_lineno(data->node));
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2019-11-07 12:29:20 +03:00
|
|
|
ast_node_first_column(rb_execution_context_t *ec, VALUE self)
|
2018-01-16 02:43:17 +03:00
|
|
|
{
|
|
|
|
struct ASTNodeData *data;
|
|
|
|
TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
|
|
|
|
|
|
|
|
return INT2NUM(nd_first_column(data->node));
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2019-11-07 12:29:20 +03:00
|
|
|
ast_node_last_lineno(rb_execution_context_t *ec, VALUE self)
|
2018-01-16 02:43:17 +03:00
|
|
|
{
|
|
|
|
struct ASTNodeData *data;
|
|
|
|
TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
|
|
|
|
|
|
|
|
return INT2NUM(nd_last_lineno(data->node));
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2019-11-07 12:29:20 +03:00
|
|
|
ast_node_last_column(rb_execution_context_t *ec, VALUE self)
|
2018-01-16 02:43:17 +03:00
|
|
|
{
|
|
|
|
struct ASTNodeData *data;
|
|
|
|
TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
|
|
|
|
|
|
|
|
return INT2NUM(nd_last_column(data->node));
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2019-11-07 12:29:20 +03:00
|
|
|
ast_node_inspect(rb_execution_context_t *ec, VALUE self)
|
2018-01-16 02:43:17 +03:00
|
|
|
{
|
|
|
|
VALUE str;
|
|
|
|
VALUE cname;
|
|
|
|
struct ASTNodeData *data;
|
|
|
|
TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
|
|
|
|
|
|
|
|
cname = rb_class_path(rb_obj_class(self));
|
|
|
|
str = rb_str_new2("#<");
|
|
|
|
|
|
|
|
rb_str_append(str, cname);
|
2018-12-24 04:56:21 +03:00
|
|
|
rb_str_catf(str, ":%s@%d:%d-%d:%d>",
|
|
|
|
node_type_to_str(data->node),
|
|
|
|
nd_first_lineno(data->node), nd_first_column(data->node),
|
|
|
|
nd_last_lineno(data->node), nd_last_column(data->node));
|
2018-01-16 02:43:17 +03:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2019-11-08 10:24:24 +03:00
|
|
|
#include "ast.rbinc"
|
2019-11-07 12:29:20 +03:00
|
|
|
|
2018-01-16 02:43:17 +03:00
|
|
|
void
|
|
|
|
Init_ast(void)
|
|
|
|
{
|
2018-11-09 04:37:41 +03:00
|
|
|
rb_mAST = rb_define_module_under(rb_cRubyVM, "AbstractSyntaxTree");
|
2018-01-16 02:43:17 +03:00
|
|
|
rb_cNode = rb_define_class_under(rb_mAST, "Node", rb_cObject);
|
2018-08-03 09:53:14 +03:00
|
|
|
rb_undef_alloc_func(rb_cNode);
|
2019-11-07 12:29:20 +03:00
|
|
|
|
|
|
|
load_ast();
|
2018-01-16 02:43:17 +03:00
|
|
|
}
|