2023-10-25 17:23:57 +03:00
|
|
|
#include "prism/prism.h"
|
2024-02-27 17:20:24 +03:00
|
|
|
#include "ruby/encoding.h"
|
2023-10-25 17:23:57 +03:00
|
|
|
|
2024-01-15 23:59:11 +03:00
|
|
|
/**
|
|
|
|
* the getlocal and setlocal instructions require two parameters. level is how
|
|
|
|
* many hops up the iseq stack one needs to go before finding the correct local
|
|
|
|
* table. The index is the index in that table where our variable is.
|
|
|
|
*
|
|
|
|
* Because these are always calculated and used together, we'll bind them
|
|
|
|
* together as a tuple.
|
|
|
|
*/
|
|
|
|
typedef struct pm_local_index_struct {
|
|
|
|
int index, level;
|
|
|
|
} pm_local_index_t;
|
|
|
|
|
2024-03-28 21:15:26 +03:00
|
|
|
// A declaration for the struct that lives in compile.c.
|
|
|
|
struct iseq_link_anchor;
|
|
|
|
|
2023-10-25 17:23:57 +03:00
|
|
|
// ScopeNodes are helper nodes, and will never be part of the AST. We manually
|
|
|
|
// declare them here to avoid generating them.
|
|
|
|
typedef struct pm_scope_node {
|
|
|
|
pm_node_t base;
|
|
|
|
struct pm_scope_node *previous;
|
|
|
|
pm_node_t *ast_node;
|
2023-12-06 18:05:14 +03:00
|
|
|
pm_node_t *parameters;
|
2023-10-25 17:23:57 +03:00
|
|
|
pm_node_t *body;
|
|
|
|
pm_constant_id_list_t locals;
|
2024-02-27 17:20:24 +03:00
|
|
|
|
2024-02-08 18:14:27 +03:00
|
|
|
const pm_parser_t *parser;
|
2024-02-27 17:20:24 +03:00
|
|
|
rb_encoding *encoding;
|
2023-10-25 17:23:57 +03:00
|
|
|
|
2024-08-29 21:26:08 +03:00
|
|
|
/**
|
|
|
|
* This is a pointer to the list of script lines for the ISEQs that will be
|
|
|
|
* associated with this scope node. It is only set if
|
|
|
|
* RubyVM.keep_script_lines is true. If it is set, it will be set to a
|
|
|
|
* pointer to an array that is always stack allocated (so no GC marking is
|
|
|
|
* needed by this struct). If it is not set, it will be NULL. It is
|
|
|
|
* inherited by all child scopes.
|
|
|
|
*/
|
|
|
|
VALUE *script_lines;
|
|
|
|
|
2024-03-27 22:04:27 +03:00
|
|
|
/**
|
|
|
|
* This is the encoding of the actual filepath object that will be used when
|
|
|
|
* a __FILE__ node is compiled or when the path has to be set on a syntax
|
|
|
|
* error.
|
|
|
|
*/
|
|
|
|
rb_encoding *filepath_encoding;
|
|
|
|
|
2024-05-20 18:29:50 +03:00
|
|
|
// The size of the local table on the iseq which includes locals and hidden
|
|
|
|
// variables.
|
2023-12-06 18:05:14 +03:00
|
|
|
int local_table_for_iseq_size;
|
2023-12-01 00:18:14 +03:00
|
|
|
|
2023-10-25 23:37:37 +03:00
|
|
|
ID *constants;
|
|
|
|
st_table *index_lookup_table;
|
2024-03-28 21:15:26 +03:00
|
|
|
|
2024-05-20 18:29:50 +03:00
|
|
|
// The current coverage setting, passed down through the various scopes.
|
|
|
|
int coverage_enabled;
|
|
|
|
|
2024-03-28 21:15:26 +03:00
|
|
|
/**
|
|
|
|
* This will only be set on the top-level scope node. It will contain all of
|
|
|
|
* the instructions pertaining to BEGIN{} nodes.
|
|
|
|
*/
|
|
|
|
struct iseq_link_anchor *pre_execution_anchor;
|
2023-10-25 17:23:57 +03:00
|
|
|
} pm_scope_node_t;
|
|
|
|
|
2024-02-27 17:20:24 +03:00
|
|
|
void pm_scope_node_init(const pm_node_t *node, pm_scope_node_t *scope, pm_scope_node_t *previous);
|
2024-01-18 19:55:31 +03:00
|
|
|
void pm_scope_node_destroy(pm_scope_node_t *scope_node);
|
2024-01-31 20:17:31 +03:00
|
|
|
|
|
|
|
typedef struct {
|
2024-03-27 22:04:27 +03:00
|
|
|
/** The parser that will do the actual parsing. */
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_parser_t parser;
|
2024-03-27 22:04:27 +03:00
|
|
|
|
|
|
|
/** The options that will be passed to the parser. */
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_options_t options;
|
2024-03-27 22:04:27 +03:00
|
|
|
|
|
|
|
/** The input that represents the source to be parsed. */
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_string_t input;
|
2024-03-27 22:04:27 +03:00
|
|
|
|
|
|
|
/** The resulting scope node that will hold the generated AST. */
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_scope_node_t node;
|
2024-03-27 22:04:27 +03:00
|
|
|
|
|
|
|
/** Whether or not this parse result has performed its parsing yet. */
|
2024-01-31 20:17:31 +03:00
|
|
|
bool parsed;
|
|
|
|
} pm_parse_result_t;
|
|
|
|
|
2024-04-25 20:32:08 +03:00
|
|
|
VALUE pm_load_file(pm_parse_result_t *result, VALUE filepath, bool load_error);
|
2024-08-29 21:26:08 +03:00
|
|
|
VALUE pm_parse_file(pm_parse_result_t *result, VALUE filepath, VALUE *script_lines);
|
|
|
|
VALUE pm_load_parse_file(pm_parse_result_t *result, VALUE filepath, VALUE *script_lines);
|
|
|
|
VALUE pm_parse_string(pm_parse_result_t *result, VALUE source, VALUE filepath, VALUE *script_lines);
|
2024-03-11 18:05:31 +03:00
|
|
|
VALUE pm_parse_stdin(pm_parse_result_t *result);
|
2024-01-31 20:17:31 +03:00
|
|
|
void pm_parse_result_free(pm_parse_result_t *result);
|
|
|
|
|
2024-11-08 22:33:48 +03:00
|
|
|
rb_iseq_t *pm_iseq_new(pm_scope_node_t *node, VALUE name, VALUE path, VALUE realpath, const rb_iseq_t *parent, enum rb_iseq_type, int *error_state);
|
|
|
|
rb_iseq_t *pm_iseq_new_top(pm_scope_node_t *node, VALUE name, VALUE path, VALUE realpath, const rb_iseq_t *parent, int *error_state);
|
|
|
|
rb_iseq_t *pm_iseq_new_main(pm_scope_node_t *node, VALUE path, VALUE realpath, const rb_iseq_t *parent, int opt, int *error_state);
|
|
|
|
rb_iseq_t *pm_iseq_new_eval(pm_scope_node_t *node, VALUE name, VALUE path, VALUE realpath, int first_lineno, const rb_iseq_t *parent, int isolated_depth, int *error_state);
|
|
|
|
rb_iseq_t *pm_iseq_new_with_opt(pm_scope_node_t *node, VALUE name, VALUE path, VALUE realpath, int first_lineno, const rb_iseq_t *parent, int isolated_depth, enum rb_iseq_type, const rb_compile_option_t *option, int *error_state);
|
2024-01-31 20:17:31 +03:00
|
|
|
|
|
|
|
VALUE pm_iseq_compile_node(rb_iseq_t *iseq, pm_scope_node_t *node);
|