Skip to content

Commit

Permalink
Remove "internal/compile.h"
Browse files Browse the repository at this point in the history
  • Loading branch information
yui-knk committed Apr 14, 2023
1 parent 496b637 commit 79291cd
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 32 deletions.
12 changes: 12 additions & 0 deletions external/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

// TODO: Expand
#include "ruby/internal/value.h"
#include "ruby/st.h"

typedef struct rb_parser_config_struct {
/* Memory */
Expand Down Expand Up @@ -75,6 +76,17 @@ typedef struct rb_parser_config_struct {
/* Ractor */
VALUE (*ractor_make_shareable)(VALUE obj);

/* Compile */
int (*vm_keep_script_lines)(void);
// int rb_local_defined(ID id, const rb_iseq_t *iseq);
int (*local_defined)(ID, const void*);
// int rb_dvar_defined(ID id, const rb_iseq_t *iseq);
int (*dvar_defined)(ID, const void*);

/* Compile (parse.y) */
int (*literal_cmp)(VALUE val, VALUE lit);
st_index_t (*literal_hash)(VALUE a);

/* Error */
const char *(*builtin_class_name)(VALUE x);
// VALUE rb_syntax_error_append(VALUE, VALUE, int, int, rb_encoding*, const char*, va_list);
Expand Down
45 changes: 13 additions & 32 deletions parse.y
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ struct lex_context;
#include "ruby/internal/config.h"

#include "internal.h"
#include "internal/compile.h"
#include "internal/encoding.h"
#include "internal/imemo.h"
#include "internal/symbol.h"
Expand All @@ -52,6 +51,7 @@ struct lex_context;
#include "symbol.h"

#ifdef RIPPER
#include "internal/compile.h"
#include "internal/compilers.h"
#include "internal/complex.h"
#include "internal/error.h"
Expand Down Expand Up @@ -155,6 +155,13 @@ RBIMPL_WARNING_POP()

#define rb_ractor_make_shareable p->config.ractor_make_shareable

#define ruby_vm_keep_script_lines p->config.vm_keep_script_lines
#define rb_local_defined p->config.local_defined
#define rb_dvar_defined p->config.dvar_defined

#define literal_cmp p->config.literal_cmp
#define literal_hash p->config.literal_hash

#define rb_builtin_class_name p->config.builtin_class_name
#define rb_syntax_error_append p->config.syntax_error_append

Expand Down Expand Up @@ -12878,40 +12885,14 @@ append_literal_keys(st_data_t k, st_data_t v, st_data_t h)
return ST_CONTINUE;
}

static bool
hash_literal_key_p(VALUE k)
{
switch (OBJ_BUILTIN_TYPE(k)) {
case T_NODE:
return false;
default:
return true;
}
}

static int
literal_cmp(VALUE val, VALUE lit)
{
if (val == lit) return 0;
if (!hash_literal_key_p(val) || !hash_literal_key_p(lit)) return -1;
return rb_iseq_cdhash_cmp(val, lit);
}

static st_index_t
literal_hash(VALUE a)
{
if (!hash_literal_key_p(a)) return (st_index_t)a;
return rb_iseq_cdhash_hash(a);
}

static const struct st_hash_type literal_type = {
literal_cmp,
literal_hash,
};

static NODE *
remove_duplicate_keys(struct parser_params *p, NODE *hash)
{
struct st_hash_type literal_type = {
literal_cmp,
literal_hash,
};

st_table *literal_keys = st_init_table_with_size(&literal_type, hash->nd_alen / 2);
NODE *result = 0;
NODE *last_expr = 0;
Expand Down
53 changes: 53 additions & 0 deletions ruby_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "internal.h"
#include "internal/array.h"
#include "internal/bignum.h"
#include "internal/compile.h"
#include "internal/complex.h"
#include "internal/error.h"
#include "internal/hash.h"
Expand All @@ -18,6 +19,7 @@
#include "node.h"
#include "internal.h"
#include "ruby/ractor.h"
#include "vm_core.h"

struct ruby_parser {
rb_parser_t *parser_params;
Expand Down Expand Up @@ -110,6 +112,50 @@ syntax_error_append(VALUE exc, VALUE file, int line, int column,
return rb_syntax_error_append(exc, file, line, column, (rb_encoding *)enc, fmt, args);
}

static int
vm_keep_script_lines(void)
{
return ruby_vm_keep_script_lines;
}

static int
local_defined(ID id, const void *p)
{
return rb_local_defined(id, (const rb_iseq_t *)p);
}

static int
dvar_defined(ID id, const void *p)
{
return rb_dvar_defined(id, (const rb_iseq_t *)p);
}

static bool
hash_literal_key_p(VALUE k)
{
switch (OBJ_BUILTIN_TYPE(k)) {
case T_NODE:
return false;
default:
return true;
}
}

static int
literal_cmp(VALUE val, VALUE lit)
{
if (val == lit) return 0;
if (!hash_literal_key_p(val) || !hash_literal_key_p(lit)) return -1;
return rb_iseq_cdhash_cmp(val, lit);
}

static st_index_t
literal_hash(VALUE a)
{
if (!hash_literal_key_p(a)) return (st_index_t)a;
return rb_iseq_cdhash_hash(a);
}

void
rb_parser_config_initialize(rb_parser_config_t *config)
{
Expand Down Expand Up @@ -170,6 +216,13 @@ rb_parser_config_initialize(rb_parser_config_t *config)

config->ractor_make_shareable = rb_ractor_make_shareable;

config->vm_keep_script_lines = vm_keep_script_lines;
config->local_defined = local_defined;
config->dvar_defined = dvar_defined;

config->literal_cmp = literal_cmp;
config->literal_hash = literal_hash;

config->builtin_class_name = rb_builtin_class_name;
config->syntax_error_append = syntax_error_append;

Expand Down

0 comments on commit 79291cd

Please sign in to comment.