Commit Graph
690 Commits
Author SHA1 Message Date
Bob Nystrom d731e554e6 refactor: rename debug print functions to dump and move value printing to wren_debug.c 2015-03-15 17:09:43 +00:00
Bob Nystrom cf3db93a06 refactor: remove failIf helper and return NULL on missing file in readFile 2015-03-15 17:09:22 +00:00
Thorbjørn Lindeijer c717b8d3fd feat: add Num.atan(x) method mapping to C atan2 for quadrant-aware arc tangent
Adds a new `atan(_)` primitive method to the Num class that wraps the C
`atan2` function, allowing callers to compute the arc tangent of the
number divided by a given argument `x` while using the signs of both
numbers to determine the correct quadrant of the result. The
implementation registers the primitive `num_atan2` in the core VM
initialization and documents the new method in the core library
reference.
2015-03-15 15:43:02 +00:00
Thorbjørn Lindeijer b22040577e feat: move contains method from List to Sequence and add tests for List and Range
Add a generic `contains` implementation on the `Sequence` base class, removing the duplicate method from `List`. This allows all sequence types (including Range) to use the method without per-class redefinition. Include new test files for `List.contains` and `Range.contains` covering ordered, backwards, and exclusive ranges.
2015-03-15 14:43:10 +00:00
Bob Nystrom 18cf57d5f5 refactor: split source tree into vm/ and cli/ directories with updated build system
Separate Wren VM library sources from CLI tool sources by moving them into
src/vm/ and src/cli/ subdirectories respectively. Update the Makefile to use
separate wildcard patterns for each directory, generate distinct object file
paths under build/vm/ and build/cli/, and adjust include paths to src/include.
Also update the Xcode project file to reference the new file locations.
2015-03-14 22:00:50 +00:00
Bob Nystrom 8436ead62e feat: add Class.supertype primitive and documentation for retrieving superclass
Implement a new `supertype` property on Class objects that returns the superclass of a given class. The primitive `class_supertype` checks if the class has a superclass (returning null for Object) and returns the superclass object. Includes documentation in the class reference and a test file verifying behavior for explicit superclasses, implicit Object inheritance, and Object's null supertype.
2015-03-14 16:48:45 +00:00
Bob Nystrom 3c44a3a001 refactor: replace manual zero-initialization with memset in wrenNewVM
Remove explicit NULL and zero assignments for vm fields (methodHandles, foreignCallSlot, foreignCallNumArgs, bytesAllocated, compiler, fiber, first, numTempRoots, modules) and consolidate them into a single memset(vm, 0, sizeof(WrenVM)) call, simplifying the initialization logic.
2015-03-14 16:36:27 +00:00
Bob Nystrom f2693f57ad refactor: extract DEBUG_TRACE_INSTRUCTIONS macro and fix RUNTIME_ERROR brace style in runInterpreter
Moves the debug trace instruction printing logic from inline DISPATCH() macro into a dedicated DEBUG_TRACE_INSTRUCTIONS macro with proper WREN_DEBUG_TRACE_INSTRUCTIONS guard, and adjusts RUNTIME_ERROR macro to use Allman brace style for consistency with surrounding code.
2015-03-14 16:29:17 +00:00
Thorbjørn Lindeijer ac414a24fd feat: add acos, asin, atan trigonometric methods to Num class
Implement arc cosine, arc sine, and arc tangent primitives in the VM core
and document them in the Num API reference.
2015-03-14 13:52:16 +00:00
Thorbjørn Lindeijer 2e6cb23b56 feat: add Num.tan method to core library and documentation
Add the missing `tan` method to the Num class, implementing the tangent
trigonometric function. This fills the gap between the existing `sin` and
`cos` methods, providing a complete set of basic trigonometric operations.

- Register `num_tan` primitive in the VM core initialization
- Implement `DEF_PRIMITIVE(num_tan)` using C's `tan()` math function
- Add documentation entry for `tan` in the core library reference
2015-03-14 13:40:23 +00:00
Thorbjørn Lindeijer 9dc939380c feat: add Sequence.count(predicate) method and update documentation
Implement a new `count(f)` method on the Sequence class that accepts a predicate function and returns the number of elements for which the predicate evaluates to true. The implementation iterates over the sequence, calling `f.call(element)` on each element and incrementing a counter when the result is truthy.

Also update the documentation for both Sequence and List classes: add full documentation for the new `count(predicate)` method on Sequence, and change the terminology in List docs from "items" to "elements" for consistency. Add three test files covering the basic predicate behavior, non-boolean return values from the predicate, and the error case when a non-function argument is passed.
2015-03-14 13:17:21 +00:00
Bob Nystrom e52573c3f6 fix: change infinity string representation from "inf" to "infinity" in num_toString
Update the num_toString primitive in wren_core.c to return "infinity" and "-infinity"
instead of the platform-dependent "inf" and "-inf" for positive and negative infinity
values. This ensures consistent string output across different C compilers and
platforms that may format infinity differently. Update the corresponding test
expectations in test/number/divide.wren to match the new output format.
2015-03-13 14:35:10 +00:00
Bob Nystrom 9c56d90e1c fix: cast size_t arguments to unsigned long for consistent printf format across architectures 2015-03-13 14:27:11 +00:00
Bob Nystrom e2555a0b96 chore: remove boilerplate comment block and fix include guard in wren_debug.h
Remove the Xcode-generated file header comment with copyright notice
and author attribution from wren_debug.h. Also correct the include guard
macro from `wren_wren_debug_h` to `wren_debug_h` to match the actual
file path and naming convention used by other headers in the project.
2015-03-13 14:26:42 +00:00
Bob Nystrom eea7803aae fix: correct comment and fix typo in wren compiler and vm files
- Fix misleading comment in `new_` function in `wren_compiler.c` to accurately describe angle brackets instead of leading space
- Fix typo "Uknown" to "Unknown" in assertion message in `wrenCall` function in `wren_vm.c`
- Remove trailing whitespace in `wren_vm.c`
2015-03-12 14:26:29 +00:00
Bob Nystrom 101c94802b fix: pass char* buffer instead of ObjString to error() in wrenCompile
In the variable undefined check within wrenCompile, the error() call was
receiving an ObjString struct instead of its underlying char* buffer,
causing a type mismatch. Changed the argument to access the .buffer field
of the variable name string.
2015-03-11 14:31:18 +00:00
Gavin Schulz a0e7e1fc67 fix: correct comment about name mangling in new_ function
The comment previously stated that a leading space prevents direct calls,
but the actual mechanism uses angle brackets in the method name "<instantiate>"
to make it inaccessible from user code.
2015-03-09 01:22:17 +00:00
Gavin Schulz 6c36816245 fix: correct spelling of "Unknown" in ASSERT message for argument type validation 2015-03-09 00:44:24 +00:00
Peter Reijnders 875c74b211 fix: align ObjList capacity and count types with ObjMap's uint32_t
Change the `capacity` and `count` fields in `ObjList` from `int` to `uint32_t` to match the
types used in `ObjMap`, ensuring consistency between the two structures. Update all loop
variables and index calculations in `wren_core.c` and `wren_value.c` that interact with
these fields to use `uint32_t` instead of `int`, and remove the now-unnecessary negative
bounds check in `list_iterate` since unsigned types cannot be negative.
2015-03-08 11:33:29 +00:00
Bob Nystrom 146995d54d chore: remove outdated TODO comments across compiler, core, value, and vm headers
- Delete TODO about nullary constructor handling in constructorSignature
- Remove TODO and commented-out code for making fibers iterable in wrenInitializeCore
- Eliminate TODO questioning elimination of OBJ_CLASS enum in favor of classObj pointers
- Drop TODO about moving WrenVM struct definition and using array for class fields
2015-03-07 22:10:34 +00:00
Bob Nystrom 250a855d82 fix: return null from IO.read on EOF instead of empty string
Previously, IO.read would return an empty string when stdin reached EOF because it always called wrenReturnString with the buffer, even when fgets returned NULL. Now it returns null by only calling wrenReturnString when fgets succeeds, and adds a test case verifying that IO.read returns null on EOF.
2015-03-07 21:47:07 +00:00
Bob Nystrom 3979a7dfff refactor: replace wrenMapFind with wrenMapGet returning Value instead of index
Refactor the internal Map API to replace the low-level `wrenMapFind` function
(which returned a raw entry index or UINT32_MAX) with a higher-level
`wrenMapGet` that directly returns the stored Value or UNDEFINED_VAL.

This change simplifies all call sites by eliminating manual index-based entry
lookups and direct access to the internal `entries` array. The old
`wrenMapFind` is made static and renamed to `findEntry`, returning a pointer
to the MapEntry struct instead of an index, which is used internally by
`wrenMapGet` and `wrenMapRemoveKey`.

Additionally, fix a bug in `loadModule` where a new module was always created
before checking if the module already existed; now the early return for
already-loaded modules correctly skips module creation and variable copying.
2015-03-07 20:41:18 +00:00
Bob Nystrom f1fd125385 feat: allow yielding main fiber and expose current fiber reference
- Change Fiber.yield() to exit interpreter when no parent fiber exists instead of raising runtime error
- Add Fiber.current primitive to return the currently executing fiber object
- Update interpreter loop to stop execution when PRIM_RUN_FIBER receives null value
- Modify test expectations to reflect new yielding behavior from main fiber
2015-03-07 20:32:11 +00:00
Michel Martens bdd0708a99 fix: return null from IO.read on EOF instead of empty string
Previously, IO.read would return an empty string when reading from stdin reached EOF. This changes the behavior to return null when fgets returns NULL, indicating end-of-file. The fix removes the unconditional wrenReturnString call and only returns the buffer when data was actually read. A new test case verifies that IO.read returns null on EOF.
2015-03-07 09:10:18 +00:00
Bob Nystrom 7dcd39f512 fix: remove deprecated num_deg and num_rad primitives, simplify num_fraction implementation 2015-03-07 06:52:42 +00:00
Bob Nystrom 92d72be158 feat: add deg, rad, fraction, sign, truncate primitives to num class
Add five new numeric methods to the Wren core library: deg (convert radians to degrees), rad (convert degrees to radians), fraction (extract fractional part), sign (return -1, 0, or 1), and truncate (discard fractional part). Register all new primitives in wrenInitializeCore and include corresponding test files for fraction, sign, and truncate operations.
2015-03-07 06:43:02 +00:00
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