Commit Graph
814 Commits
Author SHA1 Message Date
Bob Nystrom b712bb1929 feat: add count method to Sequence class with iteration-based element tally
Implement a `count` method on the `Sequence` class that iterates over all elements and returns the total number. The method uses a simple for-loop to increment a counter for each element, providing a generic way to determine sequence length without requiring a separate length property. Includes a test sequence that yields values 1 through 10 to verify the count returns 10.
2015-03-06 15:01:02 +00:00
Marco Lizza ea20c2d086 fix: remove redundant integer division primitive from wren core
The `num_div` primitive performed integer division on two int32 values, but this
functionality is already covered by the existing `num_divide` primitive which
handles floating-point division. Removing this duplicate method eliminates
confusion and reduces code maintenance burden.
2015-03-05 08:48:52 +00:00
Marco Lizza 86ab538c21 style: reformat num_sign primitive to use early returns and consistent brace style 2015-03-04 23:47:54 +00:00
Marco Lizza a4ebc39877 fix: replace int32_t cast with modf for correct truncate behavior
The previous implementation cast the number to int32_t, which incorrectly
truncated values outside the int32_t range and produced wrong results for
large doubles. The new implementation uses the standard C `modf` function
to correctly truncate the fractional part while preserving the full
double precision of the integer portion.
2015-03-04 23:46:45 +00:00
Marco Lizza adca2b3aac feat: rename 'decimal' primitive to 'fraction' and reimplement using modf
Replace the num_decimal primitive with num_fraction, changing the implementation from a simple subtraction of integer part to using the standard C modf function for more robust fractional part extraction. Update the method registration in wrenInitializeCore to bind the new name and function.
2015-03-04 23:45:30 +00:00
Marco Lizza 5ffb2815ae feat: add integer division primitive 'div' to num class in wren_core.c
Implement DEF_PRIMITIVE(num_div) that casts both operands to int32_t
and performs integer division, returning the truncated quotient.
Register the new primitive as "div(_)" on the num class in
wrenInitializeCore, placed alphabetically between "deg" and "floor".
2015-03-04 15:10:37 +00:00
Bob Nystrom 6519dbf70e refactor: replace per-type mark helpers with unified wrenMarkObj in GC marking
Remove the dedicated `setMarkedFlag`, `markString`, `markFn`, and `markClass` helper functions, replacing their calls with the new generic `wrenMarkObj` entry point. This eliminates duplicated marking logic and ensures all object types go through a single marking path that handles cycle detection and memory tracking consistently.
2015-03-04 15:08:48 +00:00
Marco Lizza 610f98957f style: rename local variable num to value in num_decimal and num_sign primitives for consistency 2015-03-04 13:46:24 +00:00
Marco Lizza 2e36078d6e fix: extend num_sign primitive to return 0 for zero-valued numbers 2015-03-04 13:45:51 +00:00
Marco Lizza 1a60874e9c feat: add 'sign' primitive method to Number class returning 1 for non-negative and -1 for negative
Implement the num_sign primitive that checks if the numeric argument is >= 0.0
and returns 1.0 for non-negative values or -1.0 for negative values. Register
the new primitive on the Number class in wrenInitializeCore.
2015-03-04 13:42:50 +00:00
Marco Lizza 74943b558c fix: swap implementations of num_decimal and num_truncate primitives
The num_decimal primitive was incorrectly returning the integer part of a number
instead of the fractional part, while num_truncate was returning the fractional
part instead of truncating to the integer portion. This commit corrects both
functions by exchanging their logic bodies.
2015-03-04 13:42:34 +00:00
Marco Lizza 0a448e08d1 feat: add decimal and truncate number methods to wren core
Implement two new primitive methods for the Number class: `decimal` returns the integer part by casting to uint32_t, and `truncate` returns the fractional part by subtracting the integer portion from the original value. Register both primitives in the core initialization.
2015-03-03 23:29:19 +00:00
Marco Lizza 3c2c84e390 feat: add deg and rad number conversion primitives to Num class
Implement two new primitive methods on the Num class: `deg` converts radians to degrees by multiplying by 180/Ï€ (57.29578) and flooring, while `rad` converts degrees to radians by dividing by the same factor and flooring. Both are registered in `wrenInitializeCore` alongside existing trigonometric primitives.
2015-03-03 23:28:44 +00:00
Bob Nystrom 4781e77809 feat: add Sequence.any method and update docs, tests, and core source
Implement the `any` method on the Sequence class that returns true if any element passes the given predicate function. Update the core.wren library source, the C embedded source string in wren_core.c, and add comprehensive test files for normal usage, non-bool return values, and non-function argument error handling. Also fix a typo in the QA documentation and correct the initial account balance in the example.
2015-03-03 15:23:47 +00:00
Bob Nystrom f809e4a1f1 refactor: extract shared makeNumber helper and fix OBJ_VAL wrapping in num_fromString
Extract the duplicated number parsing logic from readHexNumber and readNumber into a single makeNumber function that accepts an isHex flag. Also fix a missing OBJ_VAL wrapper in the num_fromString primitive's ERANGE error path and normalize trailing newlines in three test files.
2015-03-03 15:17:56 +00:00
Gavin Schulz 3dcffddbd5 feat: add ERANGE error handling for oversized number literals in lexer and Num.fromString
Replace TODO stubs with actual errno checks in readHexNumber, readNumber, and num_fromString to emit a "Number literal is too large." error when strtol/strtod overflow. Add three test files (literal_too_large.wren, hex_too_large.wren, from_string_too_large.wren) verifying compile-time and runtime error behavior for out-of-range numeric inputs.
2015-03-03 08:06:34 +00:00
Bob Nystrom 2eff9633ae fix: correct comment in DEALLOCATE macro to use "allocator" instead of "allocate"
The comment for the DEALLOCATE macro in wren_common.h previously read "Use the VM's allocate to free"
which was grammatically incorrect and misleading. Changed "allocate" to "allocator" to accurately
describe that the macro uses the VM's memory allocator function to free memory at the given pointer.
2015-03-02 15:32:09 +00:00
Bob Nystrom 09a7a126b0 feat: add DEALLOCATE macro and replace direct wrenReallocate calls for freeing memory
Introduce a DEALLOCATE macro that wraps wrenReallocate(vm, pointer, 0, 0) to provide a consistent, readable interface for freeing allocated memory. Replace all existing direct wrenReallocate calls used for deallocation across wren_core.c, wren_utils.c, wren_value.c, and wren_vm.c with the new macro. Also remove two stale TODO comments in wren_core.c related to testing and unimplemented map methods.
2015-03-02 15:31:46 +00:00
Bob Nystrom 8897c6a0a5 refactor: rename NATIVE/DEF_NATIVE macros to PRIMITIVE/DEF_PRIMITIVE across wren_core
Rename the NATIVE macro to PRIMITIVE and DEF_NATIVE to DEF_PRIMITIVE, updating all
corresponding function prefixes from native_ to prim_ to maintain consistency with
the existing METHOD_PRIMITIVE type and terminology used throughout the codebase.
2015-03-02 15:24:04 +00:00
Bob Nystrom aa5113ba77 fix: remove dead commented code and add null check for sourcePath in stack trace 2015-02-28 21:51:19 +00:00
Bob Nystrom ff420c816b feat: add wrenGetMethod, wrenCall, and wrenReleaseMethod for invoking Wren methods from C code 2015-02-28 21:31:15 +00:00
Thorbjørn Lindeijer 8f54dc5f76 feat: add Sequence.any method as complement to Sequence.all
Adds a new `any` method to the Sequence class that tests whether any element
in the sequence passes the given predicate function, returning true on the
first match or false if none match. Includes documentation, core library
source update, and three new test files covering basic functionality,
non-boolean return values, and non-function argument error handling.
2015-02-28 20:45:39 +00:00
Bob Nystrom 091860b89d fix: add dedicated markRange function to correctly count ObjRange memory in GC 2015-02-28 17:09:57 +00:00
Peter Reijnders 0ddef2c17f chore: enable -Wextra compiler flag and suppress unused-parameter warnings
Enable the -Wextra compiler flag in the wren.mk build configuration, which was previously commented out with a TODO. To handle the new unused parameter warnings triggered by -Wextra (particularly for WrenVM* vm parameters in several functions), add -Wno-unused-parameter to suppress them. Also fix a declaration ordering issue in wren_vm.c where the 'static' keyword was placed after the return type in loadIntoCore, moving it to the correct position before the type specifier.
2015-02-28 12:52:48 +00:00
Peter Reijnders 21e387efec fix: add null and already-freed guard to wrenFreeVM to prevent double-free crash
The guard checks if the VM pointer is NULL or if its methodNames count is zero,
which serves as a heuristic to detect an already-freed VM instance. This prevents
a segmentation fault or memory corruption when wrenFreeVM is called multiple times
on the same VM object.
2015-02-28 12:29:17 +00:00
Bob Nystrom cf9d568469 fix: guard GC trigger to only fire on allocation, not deallocation 2015-02-28 05:41:59 +00:00
Bob Nystrom 3a8bc85674 refactor: extract validateSuperclass helper and move subclassing tests to inheritance directory 2015-02-27 16:08:27 +00:00
Bob Nystrom d7d067b960 feat: prevent subclassing of built-in types with runtime error
Add a check in the interpreter's class creation path that rejects attempts to subclass any of the seven core built-in types (Class, Fiber, Fn, List, Map, Range, String). When such a subclass is declared, a runtime error is raised with a message like "Class 'SubName' may not subclass a built-in". Also add corresponding test files for each forbidden subclass scenario.
2015-02-27 15:41:25 +00:00
Bob Nystrom 08f40299f0 refactor: extract signature stringification into dedicated helper and update callers
The `signatureSymbol` function previously built a method name string as a side effect before hashing it. This change separates the stringification logic into a new `signatureToString` function that writes the full signature name into a provided buffer and updates its length. All callers now use the new helper, making the code clearer and allowing the stringified signature to be reused independently of symbol lookup.
2015-02-27 15:10:44 +00:00
Bob Nystrom 469f64fa9a refactor: remove endToken parameter and error handling from finishParameterList
The finishParameterList function no longer needs to consume the closing delimiter
or produce error messages, as that responsibility has been moved to the callers.
The parameterList wrapper function has been inlined into its single call site,
simplifying the parameter parsing flow.
2015-02-27 14:51:37 +00:00
Bob Nystrom 5382fa05d7 feat: allow empty argument list methods in calls, definitions, and docs
- Compile empty `()` as a valid method signature distinct from no parentheses
- Update `call()`, `clear()`, `run()`, `try()`, and `yield()` to use empty argument lists
- Revise documentation across multiple files to reflect new signature semantics
2015-02-27 07:08:36 +00:00
Bob Nystrom 94f34367f9 feat: switch method signatures to parenthesized parameter notation and update error messages
The method signature format has been changed from space-counted arity (e.g., "== ") to explicit parenthesized parameter lists (e.g., "==(_)"). This required updating the MAX_METHOD_SIGNATURE calculation to account for parentheses and parameter separators, replacing the old copyName helper with a new Signature struct and SignatureType enum, and rewriting the methodNotFound error message to display the full signature string instead of reconstructing arity from trailing spaces. All core method registrations in wren_core.c, wren_io.c, and corresponding test expectations have been updated to use the new signature format.
2015-02-27 05:56:15 +00:00
Bob Nystrom a24f51d4af feat: replace separate methodName/arity params with unified signature string in foreign method API
The wrenDefineMethod and wrenDefineStaticMethod functions now accept a single `signature` string parameter instead of separate `methodName` and `arity` arguments. This simplifies the API by letting callers specify the full method signature (including arity spaces) directly, removing the internal arity-to-spaces conversion logic in defineMethod. The change updates the header declaration, the internal defineMethod helper, and all call sites in wren_io.c to pass pre-formatted signatures.
2015-02-26 15:44:45 +00:00
Bob Nystrom a12b1695a7 docs: document NULL return behavior for wrenCompile on syntax errors 2015-02-26 15:37:10 +00:00
Peter Reijnders 3987db35f7 feat: prevent subclassing of built-in types with runtime error in class creation
Add a check in the interpreter's class creation path that detects when a user-defined class attempts to inherit from any of the seven built-in types (Class, Fiber, Fn, List, Map, Range, String). When such an inheritance is detected, a runtime error is raised with a descriptive message indicating the class name and that it may not subclass a built-in. This implements the restriction discussed in issue #70.

Also adds test files for each built-in type to verify the runtime error is correctly triggered, and includes a commented-out test for closure subclassing that currently causes a segfault.
2015-02-25 19:13:37 +00:00
Bob Nystrom 8fb17b57f1 feat: split monolithic PREC_BITWISE into separate precedence levels for |, ^, &, and << >> in wren_compiler.c
Add new Precedence enum values (PREC_BITWISE_OR, PREC_BITWISE_XOR, PREC_BITWISE_AND, PREC_BITWISE_SHIFT) and a PREC_TERNARY level between assignment and logical operators. Update and_(), or_(), and conditional() to use the refined precedence constants. Include a test file verifying correct associativity and precedence ordering among bitwise operators (|, &, ^, <<) in expressions.
2015-02-25 15:14:31 +00:00
Bob Nystrom 3aa82e7960 fix: reject trailing non-number characters in Num.fromString parsing
Previously, `Num.fromString` accepted strings like `"1.2prefix"` and returned the
leading numeric portion (`1.2`). This change makes parsing stricter by requiring
that the entire string be consumed after `strtod`, skipping only trailing
whitespace. If any non-whitespace characters remain after the number, the
function now returns `null` instead of a partial result.

The fix also adds an explicit empty-string check and updates the test suite to
reflect the new behavior, replacing the old `"1.2prefix"` success test with a
`"1.2suffix"` null-return test.
2015-02-25 15:07:54 +00:00
Marco Lizza e09d8c9b9e fix: raise '&&' precedence above '||' in wren compiler's operator precedence table
Split the former PREC_LOGIC into PREC_LOGICAL_OR and PREC_LOGICAL_AND, inserting PREC_TERNARY between them, and updated all parsePrecedence calls and grammar rule entries to use the new precedence levels.
2015-02-25 15:04:02 +00:00
Marco Lizza 5ff3e291da fix: adjust bitwise operator precedence between comparison and range in compiler enum 2015-02-25 15:02:29 +00:00
Bob Nystrom f0a73b6ce2 feat: add Num.fromString static method to parse decimal strings into numbers
Implement a new `Num.fromString(value)` method that attempts to parse a string as a decimal literal and returns the corresponding `Num` instance, or `null` if parsing fails. The implementation uses `strtod` for conversion, validates the argument is a string at runtime, and includes tests for valid numbers, edge cases like leading/trailing whitespace and partial suffix parsing, non-number inputs returning null, and a runtime error when a non-string argument is passed. Also fixes a missing semicolon in the `toString` native registration and removes the hex literal TODO from the number literals test.
2015-02-25 14:53:12 +00:00
Bob Nystrom c9f0dcde66 fix: convert signed integer types to unsigned in core and value modules to eliminate -Wsign-compare warnings
- Change `int` to `uint32_t` for iterator indices in `map_iterate` and `string_iterate` to match capacity/length types
- Update `String.length` field type from `int` to `uint32_t` in `wren_utils.h`
- Modify `wrenStringCodePointAt` parameter type from `int` to `uint32_t` and remove redundant negative index assertion
- Convert loop variable in `markMap` from `int` to `uint32_t` to match `map->capacity` type
- Add `-Wsign-compare`, `-Wtype-limits`, and `-Wuninitialized` compiler flags to catch future mismatches
2015-02-25 05:56:33 +00:00
Bob Nystrom fca963cb48 fix: ensure UINT32_MAX is available in gcc C++ builds by defining __STDC_LIMIT_MACROS before including stdint.h in wren_common.h 2015-02-25 05:09:04 +00:00
Bob Nystrom 29e8708812 fix: add missing stdint.h include for UINT32_MAX in wren_core.c
The UINT32_MAX macro is defined in <stdint.h> but was not included, causing
compilation errors on platforms where it is not implicitly pulled in by other
headers. This commit adds the missing include to ensure the constant is
available for use in the source file.
2015-02-25 04:26:39 +00:00
Bob Nystrom 6596537e87 refactor: replace platform-specific getcwd with empty root directory for relative imports
Remove conditional getcwd logic for MSVC and POSIX, replacing it with a simple
empty root directory string. This simplifies the import path resolution by
always using relative paths from the current working directory instead of
attempting to resolve an absolute root path at startup.
2015-02-25 03:47:17 +00:00
Bob Nystrom 9d700df1cd fix: replace dynamic allocation with static buffer for rootDirectory in main.c
- Add platform-specific getcwd header includes for MSVC and POSIX
- Replace malloc-based rootDirectory assignment with a static 2048-byte buffer
- Initialize rootDirectory to point to the static buffer instead of an empty string
- Update runFile to use const char* for lastSlash and copy path directly into buffer
- Implement TODO in runRepl by calling getcwd to set rootDirectory to current working directory
2015-02-25 03:43:12 +00:00
Marco Lizza 4f45d18150 fix: split monolithic PREC_BITWISE into separate precedence levels for | ^ & << >> operators 2015-02-23 16:28:43 +00:00
Bob Nystrom 8022c2b3c2 feat: allow multiple wrenInterpret calls to share module state for REPL
Initialize vm->modules to NULL in wrenNewVM and create a module registry map to track loaded modules. In loadModule, check if a module has already been loaded via wrenMapFind; if found, reuse the existing module object instead of creating a new one, enabling successive wrenInterpret calls to execute in the same module context and preserve variable bindings across REPL invocations.
2015-02-23 15:02:27 +00:00
Marco Lizza 4b37134bfa feat: set REPL module root to current working directory via getcwd
Replace the TODO placeholder for rootDirectory initialization in the REPL
with a call to getcwd (with _getcwd fallback for MSVC), enabling module
imports to resolve relative to the user's working directory at startup.
2015-02-23 12:57:56 +00:00
Marco Lizza 3b28ac8435 fix: replace malloc with stack buffer for rootDirectory to fix memory leak and GCC warnings 2015-02-23 12:57:20 +00:00
Gavin Schulz 87cc5fdbae feat: add Num.fromString static method to parse decimal string literals
Implements a new `fromString` static method on the `Num` class that attempts to parse a string as a decimal number literal using `strtod`. Returns the parsed `Num` value on success, or `null` if the string does not represent a valid number. Includes runtime validation that the argument is a string, and adds test coverage for positive, negative, zero, and decimal inputs as well as non-numeric strings.
2015-02-23 04:06:17 +00:00