110 Commits

Author SHA1 Message Date
Yukihiro "Matz" Matsumoto 613b03ac18 mruby-compiler: add no_return_value context flag for script optimization
when running scripts via mruby -e or file, return values are unused.
this adds a no_return_value flag to skip generating unnecessary code.

for parallel assignment like a,b = 1,2:
- before: 18 bytes, 5 registers, creates temporary array
- after: 5 bytes, 3 registers, direct register assignment, no RETURN

the flag is set only for the main program, not for libraries loaded
with -r option. eval() and mirb continue returning values correctly.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-21 17:26:49 +09:00
Yukihiro "Matz" Matsumoto e8096bf745 mruby-compiler: add brace-less hash pattern support
Add support for brace-less hash patterns at top level of case/in.
`in a: x, b: y` is now equivalent to `in {a: x, b: y}`.
`in a:, b:` shorthand now works with newlines (CRuby compatible).

Changes:
- Add EXPR_VALUE to IS_LABEL_POSSIBLE() to recognize labels after `in`
- Add brace-less hash pattern rules to p_expr
- Change p_hash_elem to use p_as instead of p_expr to avoid recursion
- Add in_kwarg flag to parser state for pattern matching context
- Set in_kwarg in lexer when keyword_in is returned
- Use EXPR_ARG after tLABEL_TAG when in_kwarg is set (makes newlines significant)

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-18 16:27:26 +09:00
Yukihiro "Matz" Matsumoto 76745161c5 mruby-compiler: remove var_nodes_enabled and use_variable_nodes flags
Eliminates gradual rollout feature flags that controlled variable-sized AST
nodes. Variable-sized nodes are now the default and only behavior, completing
the AST unification and simplification process.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-03 19:46:20 +09:00
Yukihiro "Matz" Matsumoto d15c0271d6 mruby-compiler: remove unused variable node recycling mechanism
Remove var_free_lists, var_alloc_counts, and var_total_allocated fields
from parser_state struct as they were never used since all nodes go
directly to codegen. Replace parser_alloc_var() wrapper with direct
parser_palloc() calls throughout the codebase, reducing parser memory
footprint by 88 bytes.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-03 19:46:20 +09:00
Yukihiro "Matz" Matsumoto ac02635ba5 mruby-compiler: add infrastructure for variable-sized ast nodes
This commit introduces the core infrastructure for variable-sized AST
nodes, designed to improve memory efficiency. The previous fixed-size
nodes are replaced by nodes that can store data inline, such as
strings and integers, reducing pointer indirection and memory overhead.

Key changes include:
- A generic variable-sized node header (`mrb_ast_var_header`).
- A size-class-based memory allocation system for these nodes.
- Implementation of variable-sized nodes for core types: symbols,
  strings, integers, and variables (lvar, gvar, ivar, cvar).
- Integration into the parser and code generator, controlled by a
  feature flag.
- Centralized and improved type-casting macros for AST nodes.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-03 19:46:03 +09:00
Yukihiro "Matz" Matsumoto 2cbb99c16d mruby-compiler: make mrb_ast_node an opaque struct in compile.h
Move the definition of struct mrb_ast_node to a private header to
hide implementation details from the public API.

Co-authored-by: Gemini <gemini@google.com>
2025-08-21 07:23:14 +09:00
Yukihiro "Matz" Matsumoto ae7e125388 mruby-compiler: encapsulate string and heredoc types
Move STR_FUNC_* macros, enum mrb_string_type, and struct
mrb_parser_heredoc_info from include/mruby/compile.h to
mrbgems/mruby-compiler/core/node.h.

These types are internal to the mruby compiler gem and are used by
both parse.y and codegen.c. Moving them to node.h encapsulates them
within the compiler gem, cleaning up the public mruby/compile.h header.

Co-authored-by: Gemini <gemini@google.com>
2025-08-21 07:23:13 +09:00
Yukihiro "Matz" Matsumoto 9fc58852e3 mruby-compiler: removed mrb_state dependency from two functions
- mrb_ccontext_partial_hook
- mrb_ccontext_cleanup_local_variables
2025-05-19 08:00:36 +09:00
Yukihiro "Matz" Matsumoto 9ef2f551b7 mruby-compiler: use new mempool API 2025-05-13 08:13:48 +09:00
Yukihiro "Matz" Matsumoto c2d5a402ba compile.h: change struct mrb_mempool to mrb_mempool
So that compatibility layer can be used without macro definition.
2025-05-12 12:30:15 +09:00
Yukihiro "Matz" Matsumoto 49525fa207 mempool.c: renamed from pool.c
To avoid confusion with pools in irep, we renamed region-based memory
manager from pool to mempool.

- rename pool.c to mempool.c
- separate mempool.h
- rename all mrb_pool to mrb_mempool

So if someone is using pool.c functions (I suppose no one does though),
they need to rename all `mrb_pool` to `mrb_mempool` and include
`mruby/mempool.h` header at the top.
2024-10-31 14:06:10 +09:00
dearblue a0526418ce Update documentation for mrb_top_run()
Also, add explanations for the `mrb_load_irep()` and `mrb_load_string()` families, which are indirect calls to `mrb_top_run()`.
2024-04-26 22:05:45 +09:00
Yukihiro "Matz" Matsumoto c5e3cbe4bf compile.h: stop using mrbc_ prefix for compiler context
Since it is confusing that the term `mrbc` stands for both "mruby
compiler" and "mruby compiler context" in the source code. Instead,
we use `mrb_ccontext` (stands for compiler context) prefix hereafter.

We choose `mrb_ccontext` because we have already used `mrb_context` (for
VM execusion context).
2023-12-06 15:22:08 +09:00
John Bampton 06a765a1c2 Change MRuby to mruby 2023-05-03 22:12:59 +10:00
Yukihiro "Matz" Matsumoto 561dffcccc parse.y: fix nested here-document bug; fix #5607
- remove `lex_strterm_before_heredoc` that does not nest
- remove `all_heredocs` that cannot distinguish nested and followed
  here-doc
- replace `lex_strterm` represented by cons list by C struct
- push/pop `lex_strterm` before/after interpolation
2022-06-13 22:25:37 +09:00
Yukihiro "Matz" Matsumoto 4d70d17af7 compile.h: reorder parser_heredoc_info members to reduce alignment gap. 2022-06-12 18:48:34 +09:00
dearblue 6b8582c95d Add bin/mrbc --no-ext-ops switch
Print an error if `OP_EXT[123]` is needed when generating mruby binary.
This may be useful for mruby/c.

Inspired by #5590.
2021-12-17 23:02:04 +09:00
Yukihiro "Matz" Matsumoto 7c1878669a parse.y: should allow newline after .... 2021-11-12 15:02:04 +09:00
Yukihiro "Matz" Matsumoto 2fb06aabe6 parse.y: refactor mrb_parser_parse().
- remove `mrb_jmpbuf` from `truct mrb_parser_state`
- unify exception handling of `mrb_state` and `mrb_parser_state`.
2021-09-07 07:31:21 +09:00
Seeker 1099050377 Treat tabs as 8 spaces in squiggly heredocs 2020-12-31 19:09:38 -08:00
Seeker 49b01f23a4 Fix mixed indentation and escaped tabs in squiggly heredocs 2020-12-28 18:22:13 -08:00
Seeker 54bfcaf1ff Add support for squiggly heredocs 2020-12-26 23:48:20 -08:00
John Bampton 4fa3359d44 refactor: remove trailing whitespace from C, Header, Ruby and YAML files
Lint
2020-12-15 19:44:02 +10:00
KOBAYASHI Shuji 3d056d084a Rename MRB_{ENABLE,DISABLE}_ to MRB_{USE,NO}_; close #5163
|        Previous Name         |        New Name         |
|------------------------------|-------------------------|
| MRB_ENABLE_ALL_SYMBOLS       | MRB_USE_ALL_SYMBOLS     |
| MRB_ENABLE_SYMBOLL_ALL       | MRB_USE_ALL_SYMBOLS     |
| MRB_ENABLE_CXX_ABI           | MRB_USE_CXX_ABI         |
| MRB_ENABLE_CXX_EXCEPTION     | MRB_USE_CXX_EXCEPTION   |
| MRB_ENABLE_DEBUG_HOOK        | MRB_USE_DEBUG_HOOK      |
| MRB_DISABLE_DIRECT_THREADING | MRB_NO_DIRECT_THREADING |
| MRB_DISABLE_STDIO            | MRB_NO_STDIO            |
| ENABLE_LINENOISE             | MRB_USE_LINENOISE       |
| ENABLE_READLINE              | MRB_USE_READLINE        |
| DISABLE_MIRB_UNDERSCORE      | MRB_NO_MIRB_UNDERSCORE  |
| DISABLE_GEMS                 | MRB_NO_GEMS             |

* `MRB_ENABLE_SYMBOLL_ALL` seems to be a typo, so it is fixed.
* `MRB_` prefix is added to those without.
* The previous names can also be used for compatibility.
2020-11-21 21:14:40 +09:00
dearblue a045b6b8d9 Allow to mixed and specify *.rb and *.mrb in bin/mruby
It is not decides by the extension.
In order to be recognized as a `.mrb` file, the following three points must be satisfied:
- File starts with "RITE"
- At least `sizeof(struct rite_binary_header)` bytes can be read
- `NUL` is included in the first 64 bytes of the file
If these are not met, it is judged as a text file and it is processed as a Ruby script.

The `bin/mruby -b` switch is still available which treats the given file as a `.mrb` file.

New `MRB_API` function:
- `include/mruby/compile.h` and `mrbgems/mruby-compiler/core/parse.y`
  - `mrb_load_detect_file_cxt()` (remove with `MRB_DISABLE_STDIO`)

NOTE:
- Even script files now always open in binary mode for `bin/mruby`.
  The `\r\n` is handled by the `nextc()` function already, so there is no problem even on Windows.
- The `nextc0()` function in `mrbgems/mruby-compiler/core/parse.y` can now specify a string buffer and a file pointer at the same time.
  In this case, get it from the string buffer first.

This patch includes modifies by comment of https://github.com/mruby/mruby/pull/5157.
2020-11-21 19:05:46 +09:00
Yukihiro "Matz" Matsumoto 52507b1083 Generate C struct from irep instead of binary dump. 2020-10-12 16:21:10 +09:00
Yukihiro "Matz" Matsumoto c27e451931 Merge pull request #4933 from dearblue/variables
Fix take over file scope variables with `mruby` and `mirb` command
2020-09-10 18:12:38 +09:00
dearblue f85906b679 Remove patch_irep() in mruby-eval
- It can now deal with operands in the range of `OP_EXT*`.
- It can now call the same method as the variable name without arguments.

  ```ruby
  def a
    "Safe!"
  end

  a = "Auto!"

  eval "a()" # call method `a`
  ```
2020-06-02 14:49:27 +09:00
Rory OConnell 69bc477b43 Adding warnings for mrb_load functions leaking RProc objects 2020-05-19 01:07:49 -07:00
dearblue 893cc2780c Add mrbc_cleanup_local_variables() with mrbc_context; ref #4931
Clean up defined local variables.
2020-01-19 21:42:09 +09:00
Ukrainskiy Sergey 72d57ad094 Implement numbered parameters 2019-12-09 20:50:41 +09:00
David Siaw b5299b1c58 fix up documentation for values 2019-08-18 20:12:44 +09:00
KOBAYASHI Shuji acad9567c1 Unify type of line number to uint16_t 2019-07-15 23:03:41 +09:00
Yukihiro "Matz" Matsumoto 304b52fabe Set maximum string (and symbol) size to 65534 (UINT16_MAX-1).
The previous value (`UINT16_MAX`) was too long for symbols, so it raises
an exception after the length check.
2019-05-16 11:44:51 +09:00
Yukihiro "Matz" Matsumoto 2871d0cdc5 Avoid keeping pointers from mrb_sym2name_len(); fix #4342
The addresses for packed inline symbols reference `mrb->symbuf` that
could be overridden by the later call of `mrb_sym2name_len`. Since
file names in call stack information are kept as symbols, keeping the
address in the C structures could cause problems like #4342.

This changes small incompatible changes in function prototypes:
* `mrb_parser_get_filename`: return value changed to `mrb_sym`.
* `mrb_debug_get_filename`: add `mrb_state*` as a first argument.
* `mrb_debug_get_line`: ditto.

I believe above functions are almost internal, and no third-party
mrbgem use them.
2019-04-01 14:13:06 +09:00
Yukihiro "Matz" Matsumoto ec19c34a20 Fixed a bug in mirb heredoc handling; fix #3989 2018-11-20 10:39:34 +09:00
Yukihiro "Matz" Matsumoto 7e3e8d8ed5 Shrink file name table size to uint16_t; ref #4138 2018-11-15 23:34:32 +09:00
Yukihiro "Matz" Matsumoto 891839b976 New bytecode implementation of mruby VM. 2018-07-30 22:57:54 +09:00
Yukihiro "Matz" Matsumoto 37f6e42293 Removed redundant function prototype. 2017-11-04 17:59:00 +09:00
YAMAMOTO Masaya acdc2d1f24 Add MRB_WITHOUT_FLOAT 2017-10-11 17:58:11 +09:00
Yukihiro "Matz" Matsumoto f1fa5bf1c6 Remove integer type mismatch warnings from parse.y. 2017-08-28 22:31:02 +09:00
Christopher Aue 2fadddcd20 Replaced tabs with spaces 2017-08-09 21:56:27 +02:00
Yukihiro "Matz" Matsumoto aee1476ebd Provide better way to check compile errors.
Now you can just call `mrb_load_*` functions then check
`context->parser_nerr > 0` if the return value is `undef`,
instead of calling `mrb_parse_*` functions independently.
ref #3248 #3331
2017-06-28 16:04:40 +09:00
Yukihiro "Matz" Matsumoto 15945e14b9 Revert "Make mrb_load_exec a static function."
This reverts commit 7944d9a6d4.
Because it voids #3248 and #3331. But we should add better way
to check whether compile errors occur without duplicated callings.
2017-06-28 16:00:59 +09:00
Yukihiro "Matz" Matsumoto 7944d9a6d4 Make mrb_load_exec a static function. 2017-06-23 17:56:12 +09:00
Yukihiro "Matz" Matsumoto b84e005fc3 Merge pull request #3335 from mattn/fix-vs2013
fix build on vs2013-vs2015
2016-12-08 16:48:08 +09:00
Yasuhiro Matsumoto eda6c1dc05 fix build on vs2013-vs2015 2016-12-08 15:38:19 +09:00
Tomasz Dąbrowski 0360a744b5 Promote load_exec to mruby API as mrb_load_exec (fixes #3248) 2016-12-07 21:21:21 +01:00
Yukihiro "Matz" Matsumoto d4d807b774 relax string length limitation to 64KB; fix #2725 2016-07-13 03:22:15 +09:00
Yukihiro "Matz" Matsumoto f7afe1d82a change mrb_run related API names; compatibility macros provided 2016-01-07 22:36:29 +09:00