Commit Graph
100 Commits
Author SHA1 Message Date
Bob Nystrom 67aea3f676 docs: add flow-control and values documentation pages with updated template and build script 2013-12-04 15:46:41 +00:00
Bob Nystrom 398cede489 feat: include "include" directory in source code metrics glob
Extend the file scanning loop to also cover header and source files under the `include/` directory by using `itertools.chain` to combine both glob patterns, ensuring metrics are computed for all relevant source locations.
2013-12-04 15:46:23 +00:00
Bob Nystrom 5b05c477c6 feat: implement closures with upvalue support and statement/expression separation
Add closure objects that wrap functions with captured upvalues, enabling
first-class closures that close over local variables from enclosing scopes.
Introduce new bytecode instructions (CODE_CLOSURE, CODE_LOAD_UPVALUE,
CODE_STORE_UPVALUE, CODE_CLOSE_UPVALUE, CODE_RETURN) and a CompilerUpvalue
tracking system with a MAX_UPVALUES limit of 256. Refactor the grammar to
strictly separate statements from expressions, fixing a bug where block
expressions incorrectly popped locals while temporaries remained on the
stack. Rename internal functions (wrenCallFunction, wrenDebugDumpInstruction,
wrenDebugDumpStack) and add Upvalue allocation, closure creation, and
debug dump support for the new instructions.
2013-12-04 15:43:50 +00:00
Bob Nystrom 7e7a508096 refactor: replace integer offset fields with direct char* pointers in token and lexer structs
Change Token struct to store `const char* start` pointer and `int length` instead of integer start/end offsets. Update Lexer struct similarly, replacing `tokenStart` and `currentChar` offsets with direct pointers into the source string. Adjust Local struct's `length` field type from `size_t` to `int` for consistency. Simplify error reporting in `endCompiler` to use `printf` with `%.*s` format specifier and the new pointer/length fields, eliminating manual character-by-character printing loops.
2013-12-01 23:22:24 +00:00
Bob Nystrom ed005573ad refactor: unify compiler end-of-chunk code path into single endCompiler function
Extract the common bytecode finalization logic into a dedicated endCompiler function that emits CODE_END and handles parent compiler function loading, replacing duplicated inline code. Update VM method definition opcodes to pop function from stack instead of reading from constants, enabling future closure support. Add TODO comments outlining planned closure capture mechanism and upvalue tracking requirements.
2013-12-01 22:59:56 +00:00
Bob Nystrom eaf69b892f feat: replace direct malloc/free calls with user-provided allocator in VM
The `wrenNewVM` function now uses the provided `reallocateFn` for fiber allocation instead of calling `malloc` directly, and stores the allocator in `vm->reallocate` for later use. The `wrenReallocate` function delegates all memory operations (including freeing) to the stored allocator, removing the fallback to `realloc` and `free`. This ensures consistent memory management through the external allocator, enabling custom allocation strategies like memory pools or tracking.
2013-12-01 18:22:32 +00:00
Bob Nystrom 2cb7522812 feat: add sidebar navigation layout and restructure docs page template
Introduce a `.nav` sidebar with categorized links (Welcome, Language, Objects, Usage) alongside a `.content` area, replacing the old inline header list. Widen the page container from 640px to 840px, rename `.column` to `.page`, and update the header to show a subtitle instead of a small tagline. Adjust the GitHub ribbon z-index to prevent overlap with the new sidebar.
2013-12-01 17:54:51 +00:00
Bob Nystrom 0496b4e79d refactor: rename MAX_METHOD_NAME to MAX_METHOD_SIGNATURE and extract copyName helper
Extract identifier copying logic from subscript and call functions into a dedicated copyName helper function, reducing code duplication. Rename the method signature length constant to MAX_METHOD_SIGNATURE with updated documentation to clarify it includes arity spaces. Update subscript function to use the new constant name and remove stale TODO comment about handling >10 args.
2013-12-01 08:04:46 +00:00
Bob Nystrom d9ed245dd4 feat: enforce MAX_LOCALS limit of 256 in declareVariable and add test cases for boundary conditions
Add a compile-time check in declareVariable() that emits an error when the number of simultaneous local variables reaches MAX_LOCALS (256), replacing the previous placeholder TODO. The limit is derived from the bytecode encoding constraint where CODE_LOAD_LOCAL and CODE_STORE_LOCAL use a single argument byte. Also move the MAX_LOCALS definition from a TODO comment to a proper #define, increasing the value from 255 to 256. Include four new test files: many_locals.wren (exactly 256 simultaneous locals), many_nonsimultaneous_locals.wren (more than 256 locals across nested scopes), too_many_locals.wren (257 simultaneous locals triggering the error), and too_many_locals_nested.wren (exceeding the limit across nested blocks).
2013-12-01 02:51:27 +00:00
Bob Nystrom 4240e46e05 feat: replace Scope stack with Local array to fix variable shadowing in nested blocks
Replace the linked-list Scope structure with a flat Local array indexed by depth, enabling proper shadowing semantics where inner declarations correctly hide outer ones. Remove the PUSH_SCOPE/POP_SCOPE macros and the SymbolTable-based locals tracking, replacing them with a MAX_LOCALS (255) fixed-size array that stores variable name, length, and depth. Add four new test cases covering local/parameter collision, nested block scoping, global shadowing, and local shadowing to validate the new behavior.
2013-12-01 02:28:01 +00:00
Bob Nystrom 2f37b99eef feat: add JavaScript binary tree benchmark and update runner with trial count and graph improvements 2013-11-30 04:25:00 +00:00
Bob Nystrom f0db2cbacd chore: add scratch.wren to gitignore for temporary Wren script testing
The .gitignore file now includes an entry for scratch.wren, a temporary Wren script left at the top level for quick testing purposes. This prevents accidental commits of ad-hoc experimentation files.
2013-11-30 00:20:44 +00:00
Bob Nystrom 50b1a381e0 feat: add binary_trees benchmark suite and fix string escape for tab character
Add Lua, Python, Ruby, and Wren implementations of the binary_trees benchmark from the Computer Language Benchmarks Game. Update the benchmark runner to support multiple benchmarks with output validation, and fix the Wren compiler to handle the '\t' escape sequence in string literals.
2013-11-30 00:19:13 +00:00
Bob Nystrom f0f74c6785 fix: correct compiler pointer in method constructor code emission
The method function was incorrectly using the outer `compiler` pointer instead of the inner `methodCompiler` when emitting `CODE_LOAD_LOCAL` and its operand, causing incorrect bytecode generation for constructor receiver loading.
2013-11-30 00:17:43 +00:00
Bob Nystrom 88f7444f0c feat: extract bytecode dumpInstruction into new wren_debug module
Move the dumpInstruction function and its associated opcode printing logic from wren_vm.c into a dedicated wren_debug.c source file with a corresponding wren_debug.h header. Add the new source file to the Xcode project build configuration.
2013-11-30 00:17:33 +00:00
Bob Nystrom fa92ce86ad test: add constructor overload tests for Foo class with arity dispatch
Add test file for Foo class constructors covering zero, one, and two argument overloads by arity. Verify each overload produces correct output and that new instances return proper type and toString behavior.
2013-11-29 23:21:36 +00:00
Bob Nystrom 230017a616 feat: raise max method parameters from 10 to 16 with proper error handling
Add MAX_PARAMETERS constant set to 16 and extend VM bytecode instructions CODE_CALL_11 through CODE_CALL_16 to support the new limit. Introduce compile-time error messages when parameter or argument count exceeds 16 in function definitions, method declarations, subscript calls, and regular method calls. Add new native fiber call stubs fn_call9 through fn_call16 and register them in the core initialization. Include comprehensive test coverage for 9-16 parameter functions and methods, plus error tests for exceeding the limit in all contexts.
2013-11-29 23:08:27 +00:00
Bob Nystrom 1b3b50374f feat: disallow field references outside class definitions with explicit error
Add a check in the `field()` function to detect when a field is referenced outside of a class context. When `compiler->fields` is NULL (indicating no enclosing class), emit a clear error message "Cannot reference a field outside of a class definition." and assign a dummy field index of 255 to allow continued parsing with minimal cascading errors.

Also includes minor cleanup: remove two stale TODO comments about EOF checking and better error messages in `nextToken()` and `consume()`, and add a new test file `test/field/outside_class.wren` that verifies the error is produced for top-level field assignment.
2013-11-29 18:53:56 +00:00
Bob Nystrom fe7e9041d1 refactor: replace hardcoded pinned array with linked list in WrenVM GC root tracking
Replace the fixed-size `MAX_PINNED` array of `Obj*` pointers with a dynamically allocated linked list of `PinnedObj` nodes, removing the 16-object limit and eliminating the `numPinned` counter. Update `pinObj` to accept a caller-provided `PinnedObj` stack variable and link it at the head of the list, and simplify `unpinObj` to pop the head without requiring the object argument. Adjust `wrenNewVM` to initialize the new `pinned` pointer to NULL, and modify `collectGarbage` to iterate the linked list instead of the array.
2013-11-29 17:14:19 +00:00
Bob Nystrom f9c534aef9 chore: rename source files to wren_ prefix and add module-level documentation headers 2013-11-28 16:11:50 +00:00
Bob Nystrom 4091fe4911 refactor: rename primitives module to wren_core and update all references
Rename the `primitives.h` and `primitives.c` files to `wren_core.h` and `wren_core.c` respectively, reflecting the module's purpose of defining built-in classes and native methods. Update all includes, macro names (PRIMITIVE -> NATIVE, DEF_PRIMITIVE -> DEF_NATIVE, etc.), and function calls (wrenLoadCore -> wrenInitializeCore) across the codebase and Xcode project file to match the new naming convention.
2013-11-28 16:00:55 +00:00
Bob Nystrom f36118a451 refactor: rename internal value creation functions to wren-prefixed public API names
Move `allocate`, `initObj`, `newClass`, and related static helpers from `vm.c` into `value.c` with consistent `wren` prefix, and update all call sites in `compiler.c`, `primitives.c`, and `vm.c` to use the new names (`wrenNewFunction`, `wrenNewString`, `wrenNewClass`, `wrenNewInstance`, `wrenNewList`). Remove the old declarations from `vm.h` and add comprehensive documentation comments to `value.h` explaining the NaN-tagging representation and type system.
2013-11-28 04:00:03 +00:00
Bob Nystrom 12d27a05b4 chore: remove stale TODO comment about capacity shrinking in list_removeAt 2013-11-27 19:34:35 +00:00
Bob Nystrom b17c2667c0 feat: implement list_removeAt primitive with capacity shrink and test suite
Add DEF_PRIMITIVE(list_removeAt) to src/primitives.c that removes an element at a given index from an ObjList, shifts remaining elements up, shrinks capacity when excess is detected via LIST_GROW_FACTOR, and returns the removed Value. Register the primitive as "removeAt " on vm->listClass. Create test/list/remove_at.wren covering removal at positive indices 0,1,2, negative indices -1,-2,-3, out-of-bounds cases returning null, and verification that the removed value is returned.
2013-11-27 18:20:32 +00:00
Bob Nystrom 3c3fa2bfd9 feat: add list clear and insert primitives with capacity growth and index validation
Extract inline list growth logic into `ensureListCapacity` helper and add `validateIndex` function supporting negative indices. Implement `list_clear` primitive that resets count to zero without freeing memory, and `list_insert` primitive that shifts elements right and inserts at validated position. Add comprehensive test suites for both operations covering empty lists, normal/negative indices, and return values.
2013-11-27 07:11:11 +00:00
Bob Nystrom 91c675b28a chore: reorganize test files into domain-specific subdirectories
- Moved unit tests for user authentication into `tests/unit/auth/`
- Relocated integration tests for payment processing to `tests/integration/payments/`
- Grouped API endpoint tests under `tests/api/` with separate files for each resource
- Updated test runner configuration to discover tests in new directory structure
2013-11-27 06:52:00 +00:00
Bob Nystrom cd1bd39ce1 feat: implement list.add() with dynamic growth and add wrenPrintValue helper
Add the `list_add` primitive to support appending elements to lists with automatic capacity management using `LIST_MIN_CAPACITY` and `LIST_GROW_FACTOR` constants. Rename internal `valuesEqual` and `reallocate` functions to `wrenValuesEqual` and `wrenReallocate` for consistent naming, and introduce `wrenPrintValue` to handle formatted output of all value types including lists. Add a test file `test/list_add.wren` verifying basic add operations and return value behavior.
2013-11-27 02:02:10 +00:00
Bob Nystrom 6996f5a48d feat: replace per-type equality primitives with unified object_eqeq and wrenGetClass
Move equality and inequality primitives from Bool and Function to a single Object-level implementation using valuesEqual, and add wrenGetClass to return the runtime class of any value. Refactor newSingleClass to inherit methods from superclass and wire metaclass superclass to classClass. Add type-checking tests for all built-in types and class inheritance.
2013-11-26 15:52:37 +00:00
Bob Nystrom 4f73052934 feat: add JavaScript benchmark runner with fib(30) test and node integration
Add a new JavaScript benchmark file `benchmark/fib.js` that implements a recursive Fibonacci function and measures execution time using `process.hrtime()`. Register the JavaScript language entry in `benchmark/run_all` with the `node` interpreter and `.js` extension, enabling JS benchmarks to be included in the full benchmark suite alongside existing languages.
2013-11-26 15:50:12 +00:00
Bob Nystrom 05eab7f82b feat: add public C API header wren.h and refactor internal types to WrenVM prefix 2013-11-25 15:47:02 +00:00
Bob Nystrom b21c774891 feat: unify memory management into single reallocate function with GC triggers
Replace separate allocation, deallocation, and reallocation logic with a single `reallocate` function that handles all memory operations (alloc, grow, shrink, free) and integrates garbage collection triggers based on total allocated memory thresholds. Add `allocate` and `deallocate` convenience wrappers that delegate to `reallocate`.
2013-11-25 15:14:51 +00:00
Bob Nystrom 4e87680dd1 feat: add optional computed gotos support for ~5% interpreter speedup
Introduce a `COMPUTED_GOTOS` preprocessor flag in `src/common.h` that, when defined, replaces the interpreter's traditional `for(;;) switch(instruction)` dispatch loop with a computed-goto dispatch table using GCC/LLVM labels-as-values extension. The new implementation defines a static `dispatchTable[]` array mapping opcodes to their corresponding `&&code_*` labels, and uses `DISPATCH()` macro to `goto` the next handler directly. When `COMPUTED_GOTOS` is not defined, the fallback retains the original `switch`-based loop. This change yields approximately a 5% performance improvement in benchmarks, though optimal GCC performance may require additional flags like `-fno-gcse`.
2013-11-25 05:48:27 +00:00
Bob Nystrom b098c60f7c feat: implement subscript operator for lists with negative index and bounds checking
Add list_subscript primitive that supports integer indexing with negative indices counting from the end, returns null for out-of-bounds access, non-integer indices, and non-numeric argument types. Register the `[ ]` operator on the List class in loadCore and include comprehensive test coverage in test/list_subscript.wren.
2013-11-25 03:08:46 +00:00
Bob Nystrom cc117912fa feat: implement list literal syntax and List class with count property
Add support for list literals in the compiler with bracket-delimited syntax and comma-separated elements. Introduce the ObjList runtime type with a contiguous elements array, garbage collection marking, and proper memory deallocation. Expose a `List` class in the core library with a `count` method returning the number of elements. Update the grammar table to register `[` as a prefix token for list compilation instead of an infix subscript operator, and reorder opcode enum entries to place CODE_LIST before the load/store group.
2013-11-24 21:38:31 +00:00
Bob Nystrom 2490e6a425 feat: add subscript operator for array indexing and string character access
Implement the `[]` subscript operator as an infix parser at `PREC_CALL` precedence, compiling to a method call on the receiver. Add a `string_subscript` primitive that handles integer indices (including negative for reverse indexing), returns `null` for out-of-bounds or non-integer arguments, and returns a one-character string. Register the `[ ]` method on the string class and include tests for basic indexing, negative indices, out-of-bounds, wrong types, and non-integer indices.
2013-11-24 18:45:07 +00:00
Bob Nystrom cc55fd2a57 feat: implement string escape sequence parsing in lexer and add test coverage
Add escape sequence handling to the string literal lexer in compiler.c, translating
common escape sequences (\", \\, \n) into their literal character equivalents during
tokenization. Introduce a fixed-size buffer (MAX_STRING 1024) in the Parser struct to
accumulate the unescaped string content, along with a helper function addStringChar()
to append characters. The readString() function now loops over input characters,
breaking on an unescaped double quote, and processing backslash-prefixed escapes via
a switch statement. Remove the previous TODO placeholder comment and the elapsed time
computation simplification in benchmark/fib.lua. Add a new test file
test/string_escapes.wren that verifies the three supported escape sequences, and
remove the now-obsolete TODO about escapes from test/string_literals.wren.
2013-11-24 05:00:47 +00:00
Bob Nystrom 56f0f1639b refactor: remove redundant explicit casts to Value in string and primitives functions 2013-11-24 01:35:11 +00:00
Bob Nystrom f50453e24e feat: add instance field support with lexer, compiler, and GC tracing
Add TOKEN_FIELD token type and '_' prefix handling in lexer to distinguish field identifiers from regular names. Extend Compiler struct with fields symbol table pointer for tracking enclosing class fields. Implement field() parse function and CODE_LOAD_FIELD/CODE_STORE_FIELD bytecodes in VM. Modify ObjClass to store numFields count and ObjInstance to use flexible array member for field storage. Update GC markInstance to trace all instance fields. Add newClass signature to accept numFields parameter. Include test cases for field default null, multiple fields, object references with GC, and use-before-set ordering.
2013-11-23 22:55:05 +00:00
Bob Nystrom f4982f7b21 docs: add classes documentation content, usage page stub, and fix page title in template
Add full introductory content to the classes documentation page covering class definition, inheritance, and the Object superclass. Create a new usage documentation page with a TODO placeholder. Update the HTML page title format from "wren {title}" to "Wren - {title}" and add a navigation link to the new usage page.
2013-11-23 22:52:50 +00:00
Bob Nystrom 65615f4aa8 feat: add ASCII art graph of benchmark results with configurable trial count
Extract hardcoded trial count into NUM_TRIALS constant set to 7, collect all timing results into a list, and add graph_results function that prints a horizontal ASCII bar chart scaling each run's times relative to the global maximum across all benchmarks and languages.
2013-11-22 17:17:45 +00:00
Bob Nystrom 95faef89d5 feat: add benchmark runner with fib implementations and OS.clock primitive
Add a benchmark runner script (benchmark/run_all) that executes multiple
fibonacci implementations across languages (Lua, Python, Ruby, Wren) and
computes mean, median, and standard deviation from 7 runs. Update existing
fib benchmarks to loop 5 times at fib(30) with elapsed timing. Introduce
OS.clock primitive in C core and OS class to support timing in Wren.
Adjust number formatting to "%.14g" for consistent output precision.
2013-11-22 16:55:22 +00:00
Bob Nystrom 4a9ea2f0b4 feat: add MIT license file for the Wren programming language project
This commit introduces a LICENSE file containing the full text of the MIT License, attributed to Robert Nystrom (2013), which governs the distribution and use of the Wren software.
2013-11-22 05:41:44 +00:00
Bob Nystrom 67c9435b03 feat: add initial documentation site with markdown pages and build script 2013-11-22 05:38:36 +00:00
Bob Nystrom 456c7e87e0 docs: document receiver-less call ambiguity in private getter vs field access 2013-11-22 05:38:21 +00:00
Bob Nystrom b9264e4c5a feat: add named user-defined constructors with this keyword prefix in class bodies 2013-11-20 15:20:16 +00:00
Bob Nystrom ca400d725e chore: disable DEBUG_GC_STRESS and refactor initCompiler with addConstant helper 2013-11-20 04:54:47 +00:00
Bob Nystrom 36c3f0b704 refactor: extract twoCharToken helper and remove skipWhitespace from lexer
Extract repeated two-character token matching logic into a dedicated
twoCharToken function, reducing code duplication in readRawToken.
Remove the unused skipWhitespace helper that only handled single spaces
and was replaced by more robust whitespace handling elsewhere.
Fix indentation in isKeyword and remove stale TODO comment in readName.
2013-11-20 04:19:02 +00:00
Bob Nystrom 643a7c4f97 feat: add logical OR operator with short-circuit evaluation and token support
Add TOKEN_PIPEPIPE token for '||' syntax, implement or() parsing function with CODE_OR bytecode, and add VM interpreter logic that short-circuits on truthy left operand. Update .gitignore to exclude *.xccheckout files, extend test coverage for falsy behavior in and.wren and if.wren, and add comprehensive or.wren test suite verifying short-circuit semantics and truthiness rules.
2013-11-20 02:24:58 +00:00
Bob Nystrom 06be5808da feat: add IS_FALSE macro for non-NaN-tag boolean false check in value.h
The new IS_FALSE macro checks if a Value's type field equals VAL_FALSE,
complementing the existing IS_NULL and IS_NUM macros for type discrimination
without relying on NaN-boxing tag bits.
2013-11-19 15:36:59 +00:00
Bob Nystrom 9e8eed6c29 feat: implement logical AND operator with short-circuit evaluation and new CODE_AND instruction
Add TOKEN_AMPAMP token to the lexer for parsing '&&' operator, introduce PREC_LOGIC precedence level for proper expression parsing, implement the `and()` compiler function that emits CODE_AND with a patchable jump offset, and add runtime support in the VM with IS_FALSE macro for truthiness checking. Include comprehensive test cases in test/and.wren verifying short-circuit behavior and return values.
2013-11-19 15:35:25 +00:00
Bob Nystrom ed9f4ec9f3 feat: add block scope support with push/pop macros and scope tracking in compiler
Introduce Scope struct and PUSH_SCOPE/POP_SCOPE macros to manage nested variable scopes during compilation. Update declareVariable to distinguish global declarations from local ones based on scope presence rather than parent compiler. Add truncateSymbolTable helper to clean up local symbols when exiting a scope. Include new test files verifying scoped variable isolation in if, while, and block constructs.
2013-11-18 17:19:03 +00:00
Bob Nystrom dc2f053d76 feat: add while loop token, parser support, CODE_LOOP opcode, and patchJump helper
Add TOKEN_WHILE to the token enum and keyword lookup in the compiler, enabling the parser to recognize 'while' statements. Introduce the CODE_LOOP opcode in the VM for backward jumps, and implement the patchJump helper to resolve forward jump offsets during compilation. Include comprehensive test cases for while loops covering single-expression bodies, block bodies, newline handling, and null result semantics.
2013-11-18 06:38:59 +00:00
Bob Nystrom 9c663aa13c feat: implement NaN tagging for Value union and hoist bytecode/IP into locals in interpret loop
Switch Value from a struct with type/num/obj fields to a NaN-tagged uint64_t/double union, enabling faster type checks and unboxed numbers. Refactor all macros and accessors (IS_OBJ, AS_OBJ, AS_NUM, etc.) to work with the new representation. Hoist CallFrame pointer, instruction pointer, and bytecode array into local variables inside the interpret loop, updating them on frame push/pop. Enable NAN_TAGGING define and DEBUG_GC_STRESS in common.h. Update Xcode project to use c99 standard, pedantic warnings, and treat warnings as errors. Add benchmark/fib.txt with timing comparisons showing fib benchmark within 25% of Lua.
2013-11-17 22:20:15 +00:00
Bob Nystrom b3d5f3b4a0 fix: change String#contains return type from numeric to boolean
Replace NUM_VAL and integer literals with TRUE_VAL, FALSE_VAL, and BOOL_VAL
in the string_contains primitive implementation, and update the test
expectations from 1/0 to true/false accordingly.
2013-11-17 18:04:02 +00:00
Bob Nystrom 69744a3acc refactor: replace call method primitives with fiber primitives and remove NO_VAL sentinel
Introduce METHOD_FIBER type and FIBER_PRIMITIVE/DEF_FIBER_PRIMITIVE macros to handle primitives that directly manipulate the fiber's call stack, eliminating the need for the special NO_VAL return value hack. Remove all fn_call0 through fn_call8 primitives and convert them to fiber primitives that call callFunction without returning a value. Update Primitive typedef to remove the Fiber parameter, and adjust primitive_metaclass_new and the interpret switch case accordingly. Delete the NO_VAL macro and its TODO comment from value.h.
2013-11-17 17:02:57 +00:00
Bob Nystrom 4e9b4349d6 feat: replace placeholder conditionals with actual hello world output in example
Remove two dead conditional branches that wrote "bad" and "bad2" when false, replacing them with a single unconditional call to io.write("Hello, world!") to serve as a proper introductory example for the Wren language.
2013-11-17 16:55:04 +00:00
Bob Nystrom a59289a0af refactor: extract value type definitions and helpers from vm.h into new value.h/c files 2013-11-17 01:51:30 +00:00
Bob Nystrom 7c54588c0d chore: replace AS_STRING macro with AS_CSTRING and AS_STRING, refactor GC marking into type-specific functions 2013-11-17 01:41:09 +00:00
Bob Nystrom 6c14050101 fix: remove VAL_NO_VALUE value type and replace with null object sentinel
The VAL_NO_VALUE enum variant and its associated NO_VAL macro were removed from the value type system. The NO_VAL sentinel is now represented as a VAL_OBJ with a NULL object pointer, eliminating the hacky "no value" type. Updated the interpret function's primitive call return check to detect this new sentinel by verifying both the type is VAL_OBJ and the object pointer is NULL, rather than checking for the removed VAL_NO_VALUE type. Removed the TODO(bob) comments referencing the hack.
2013-11-16 19:54:42 +00:00
Bob Nystrom 21d600ebe5 fix: increase default GC heap threshold from 1MB to 10MB in newVM
The default garbage collection trigger threshold in newVM() is raised from 1024*1024 (1MB) to 1024*1024*10 (10MB) to reduce GC frequency for larger workloads. Additionally, the DEBUG_GC_STRESS macro in common.h is commented out to disable stress-test GC collections on every allocation by default.
2013-11-16 19:43:40 +00:00
Bob Nystrom ebbe5bb7cd refactor: unbox numeric values by storing double directly in Value struct
Remove the ObjNum heap-allocated wrapper for numbers, storing the double value inline in the Value union. This eliminates OBJ_NUM enum case, the newNum() constructor, and all ObjNum-related code in the GC, printing, and type dispatch. Replace all (Value)newNum(vm, ...) calls with the NUM_VAL() macro and update AS_NUM() to read from the inline field. Adjust IS_FN/IS_STRING macros to use IS_OBJ() guard and update all Value literal macros to include the new num field initializer.
2013-11-16 19:41:29 +00:00
Bob Nystrom 5930aacb24 feat: replace boxed Obj bool/null with direct VAL_TRUE/FALSE_VAL/NULL_VAL macros 2013-11-16 19:35:59 +00:00
Bob Nystrom f6e13f2f4a refactor: replace raw Obj* casts with Value struct in compiler, primitives, and vm
Introduce Value type system with explicit type tags (VAL_OBJ, VAL_NUM, VAL_BOOL, etc.) and update all macros (AS_FN, IS_BOOL, OBJ_VAL) to operate on Value structs instead of raw Obj pointers. Migrate constant table entries, global slots, and function return values from NULL to NO_VAL/NULL_VAL. Add objectToValue helper and adjust markObj to accept Obj* directly.
2013-11-16 19:26:32 +00:00
Bob Nystrom e98ee92efa feat: add recursive fibonacci benchmark scripts in lua, python, wren and implicit fields design doc
Add three language implementations of a naive recursive fibonacci(35) benchmark
alongside a design document exploring implicit field declaration semantics for
the wren language, including nested class scoping and static field conventions.
2013-11-15 04:57:56 +00:00
Bob Nystrom f88fb3d9bb feat: add parameter list parsing and up to 8 function call primitives
Extract shared parameter list parsing from namedSignature into a new parameterList function called by function, and replace single fn_call primitive with fn_call0 through fn_call8 primitives to support up to 8 arguments. Add comprehensive fn_parameters.wren test covering 0-8 parameter calls.
2013-11-15 02:25:28 +00:00
Bob Nystrom 338dc2288f feat: add user-defined operator methods with signature compilation
Add support for user-defined operator methods in classes by introducing a `SignatureFn` field to `GrammarRule` and implementing four signature compilation functions: `namedSignature`, `infixSignature`, `unarySignature`, and `mixedSignature`. These functions handle parameter parsing and name mangling for different operator types (infix, prefix, and mixed unary/infix). The change includes a new test file `test/method_operators.wren` that validates all supported operator overloads including arithmetic, comparison, and prefix operators.
2013-11-14 18:27:33 +00:00
Bob Nystrom d56828b65b fix: remove mixfix method call support from compiler
The call function in compiler.c no longer supports multi-part mixfix method names like "foo.bar(arg) else { block } last". Instead, it now only builds a simple method name from the consumed token and adds spaces for argument count overloading. The loop that iterated over multiple name parts has been replaced with a single name extraction followed by optional parenthesized argument parsing.
2013-11-14 17:21:58 +00:00
Bob Nystrom 70218835f5 fix: improve nested block comment parsing and add error messages to consume function 2013-11-14 07:06:53 +00:00
Bob Nystrom c1f0688dae docs: add design notes for inner method dispatch and receiver-less calls
Add two design documents exploring language semantics:
- `beta-style inner.txt` outlines a dispatch mechanism for `inner` method calls
  using a two-list approach with method bodies and inheritance chain tracking
- `receiver-less calls.txt` analyzes implicit receiver resolution for bare
  function calls, proposing they resolve to `this` and using leading "." for
  top-level module function calls
2013-11-14 01:09:55 +00:00
Bob Nystrom c55150d9c4 feat: add skipBlockComment parser with nesting support and three test cases
Implement a new skipBlockComment function in the compiler that handles nested /* */ block comments by tracking a nesting counter. The parser advances through comment characters, incrementing nesting on opening /* and decrementing on closing */. Also add three test files: comment_block.wren verifying inline and nested block comments, comment_block_at_eof.wren for a block comment at end of file without newline, and comment_line_at_eof.wren for a line comment at end of file without newline.
2013-11-14 01:09:47 +00:00
Bob Nystrom ccbc2f8d4e feat: implement class inheritance with 'is' keyword and superclass method copying
Add support for class inheritance in the compiler by parsing the 'is' keyword to load a superclass and emit a CODE_SUBCLASS instruction. In the VM, extend newClass to accept a superclass parameter, copy inherited methods from the superclass into the new class, and store the superclass pointer in the ObjClass struct. Introduce a root Object class in the core library and set it as the superclass for all built-in types. Add a test file verifying method inheritance and arity-based dispatch.
2013-11-13 19:05:03 +00:00
Bob Nystrom 0850f389e1 chore: remove obsolete wren.1 man page and its Xcode project reference 2013-11-13 15:15:10 +00:00
Bob Nystrom 7eabd40181 feat: add unary operators and variable assignment with precedence handling
Implement unary negation for numbers and logical not for booleans, introduce PREC_ASSIGNMENT and PREC_UNARY precedence levels, refactor parse functions to accept allowAssignment parameter, add assignment tests for global/local/chained/error cases, and update if-syntax to support statement branches and assignment in conditions.
2013-11-13 15:10:52 +00:00
Bob Nystrom 944591339f feat: implement mark-sweep garbage collector with object linked list and pinned roots
Replace manual malloc/free for Obj allocations with GC-managed heap. Add Obj->next linked list, markObj traversal for all object types, and sweep phase in VM. Introduce MAX_PINNED stack for temporary GC roots during compilation. Refactor all object constructors (newBool, newFunction, newClass, newNum, newString) to accept VM pointer and register allocations. Move function constant registration earlier in compiler to ensure GC reachability. Add DEBUG_GC_STRESS and TRACE_MEMORY macros in new common.h header.
2013-11-12 16:32:35 +00:00
Bob Nystrom f8b27d63a3 refactor: replace direct casts with object conversion macros in vm.c and add AS_INSTANCE macro to vm.h 2013-11-10 22:21:14 +00:00
Bob Nystrom d0e0a776f4 feat: add static method support to class definitions with metaclass dispatch
Add TOKEN_STATIC keyword to the lexer, replacing the unused TOKEN_META, and extend the method() compiler function with an isStatic parameter. When isStatic is true, emit a new CODE_METACLASS instruction before CODE_METHOD to push the class's metaclass onto the stack, enabling static method dispatch. Also fix a buffer overflow bug in string_plus by allocating one extra byte for the null terminator, and refactor object initialization across vm.c by introducing a shared initObj helper to eliminate repeated type/flag assignments.
2013-11-10 19:46:13 +00:00
Bob Nystrom d74b4deff3 feat: add test file metrics with expect pattern and separate source/test output
Extend the metrics script to scan test/*.wren files, counting test files, TODOs, expectations via EXPECT_PATTERN, non-empty lines, and empty lines. Refactor output to print source and test sections with aligned formatting.
2013-11-10 19:12:38 +00:00
Bob Nystrom 0ea34e00fe feat: implement floating-point number literal parsing and arithmetic operations
Add support for floating-point number literals in the parser by detecting decimal points with trailing digits in `readNumber()`, and switch the compiler's `number()` function from `strtol` to `strtod` for proper double conversion. Update the `num_abs` primitive to use `fabs()` instead of manual negation. Extend test coverage across arithmetic, comparison, equality, modulus, and string conversion tests with floating-point cases, and add a new test for decimal point at end-of-file error detection.
2013-11-10 05:32:57 +00:00
Bob Nystrom c430056f70 feat: implement modulo operator for numbers with fmod and add test suite
Add num_mod primitive using fmod from math.h to support the % operator on number types. Register the new primitive in the numClass method table. Include a comprehensive test file covering positive, negative, and left-associative modulo cases, plus a divide-by-zero TODO comment in the existing division test.
2013-11-10 05:01:18 +00:00
Bob Nystrom 98d6bbbcc1 feat: add this keyword support with method detection and error handling
Introduce TOKEN_THIS token and `this` keyword parsing in the compiler, enabling the `this` expression for method contexts. Add `isMethod` flag to Compiler struct to track whether the current function is a method, and pass it through `initCompiler`. Implement `this_` parsing function that emits appropriate bytecode or errors when used outside a method. Remove the now-unnecessary `numLocals` field from ObjFn struct in vm.h. Add test cases for `this` at top level (expects error), in a method (valid usage), in a top-level function (expects error), and as a variable name (expects error).
2013-11-09 23:42:23 +00:00
Bob Nystrom 04dda643d0 fix: correct local variable storage and remove premature local slot allocation in vm
The compiler's storeVariable function was incorrectly emitting CODE_STORE_LOCAL for all variables, including locals that already have their value in the correct stack slot. This change renames storeVariable to defineVariable and adds logic to only emit CODE_STORE_GLOBAL for globals, while for locals it emits CODE_DUP to preserve the value through the subsequent POP. Additionally, the vm's callFunction no longer pre-allocates empty slots for locals, as they are now placed on the stack during compilation. New test files verify duplicate local/parameter detection and correct local variable ordering in block scopes.
2013-11-09 19:38:01 +00:00
Bob Nystrom d43a62e08c refactor: rename symbols to methods and defineName to declareVariable in compiler and VM
Rename the `SymbolTable symbols` field in `VM` struct to `methods` to clarify its purpose for method dispatch, and update all references across `vm.c`, `vm.h`, `primitives.c`, and `compiler.c`. Rename `defineName` to `declareVariable` to better reflect its actual behavior of declaring a variable without defining it. Remove the unused `internSymbol` helper function. Add `clearSymbolTable(&vm->globalSymbols)` to `freeVM` to fix a memory leak. Update `dumpCode` signature to take `VM*` and `ObjFn*` instead of `ObjBlock*`, and display method names via `getSymbolName`. Add `stackStart` field to `CallFrame` replacing `locals` for clearer semantics. Introduce `test/method_arity.wren` to validate method dispatch with up to 10 parameters.
2013-11-09 19:18:27 +00:00
Bob Nystrom 266881994f feat: add is operator for type checking and refactor primitives registration
Add TOKEN_IS token and CODE_IS bytecode to support runtime type checking via the `is` keyword. Refactor primitive registration by replacing `registerPrimitives` with `loadCore`, which compiles a core library string to define built-in classes (Bool, Class, Function, Num, Null, String, IO) and then attaches primitive methods to them. Update Primitive typedef to remove unused numArgs parameter, and add findGlobal helper to retrieve globals by name. Include test files for class syntax, class instantiation, and the new `is` operator.
2013-11-08 01:07:32 +00:00
Bob Nystrom 769e604dd1 feat: restructure compiler to bottom-up order removing forward declarations
Eliminate all forward declarations and reorder functions so each is defined before its first use, making recursion points explicit. Add semicolon counting to metrics script and include boolean equality test file.
2013-11-07 15:04:25 +00:00
Bob Nystrom 46977dceea feat: replace block syntax with fn keyword for function literals across compiler and tests
Rename `block` parsing rule to `fn` by adding TOKEN_FN token and function prefix rule, removing the old block prefix from TOKEN_LEFT_BRACE. Update compiler.c to replace `block()` with `function()` and add fn equality primitives (fn_eqeq, fn_bangeq) in primitives.c. Migrate test files: rename block_call.wren to fn_call.wren, block_syntax.wren to fn_syntax.wren, and replace `{ ... }` with `fn { ... }` or `fn expr` syntax. Remove obsolete bool_equality.wren test.
2013-11-06 15:47:47 +00:00
Bob Nystrom cae70590d8 refactor: rename ObjBlock to ObjFn and replace block_call with fn_call across compiler, vm, and primitives 2013-11-06 14:52:28 +00:00
Bob Nystrom 6aa4d4d041 fix: re-enable number and string toString tests after equality operator implementation
Remove the `// skip: == not implemented yet` comments from both `test/number_to_string.wren` and `test/string_to_string.wren` test files, allowing the previously skipped equality comparison tests to execute and validate the now-functional `==` operator.
2013-11-06 04:38:16 +00:00
Bob Nystrom 5341bdcbb3 feat: add null literal token and compiler support with keyword parsing and test cases
Introduce TOKEN_NULL to the token enum and register it as a prefix parse rule that emits CODE_NULL. Implement the null() compiler function to emit the null constant. Extend readName() to recognize the "null" keyword and map it to TOKEN_NULL. Add test files verifying null output, and that null, true, and false cannot be used as variable names.
2013-11-06 02:22:22 +00:00
Bob Nystrom 60d4c15be2 feat: add if/else expression parsing with jump bytecodes and null object support
Implement initial support for `if` expressions in the compiler, including new `TOKEN_IF` and `TOKEN_ELSE` tokens, `CODE_JUMP` and `CODE_JUMP_IF` bytecodes for conditional branching, and a `makeNull()` function in the VM to represent the value returned when an if-condition is false without an else clause. The `emit()` function now returns the index of the emitted bytecode to enable patching jump offsets. Also add a `nullClass` to the VM and a `OBJ_NULL` object type, along with a `dumpCode()` debugging helper and new test files for block syntax and if-expression behavior.
2013-11-05 23:40:21 +00:00
Bob Nystrom 8353c26596 feat: add == and != operators for bool, number, and string types with type-checking
Implement equality and inequality operators across three primitive types in the compiler and runtime. Add `bool_eqeq`/`bool_bangeq`, `num_eqeq`/`num_bangeq`, and `string_eqeq`/`string_bangeq` primitives that return false/true respectively when comparing against incompatible types. Update the parse rule table with `INFIX_OPERATOR` macros for `TOKEN_EQEQ` and `TOKEN_BANGEQ` at `PREC_COMPARISON` precedence. Include test files `bool_equality.wren`, `number_equality.wren`, and `string_equality.wren` covering cross-type comparisons and edge cases.
2013-11-05 17:57:57 +00:00
Bob Nystrom c466c60434 fix: refactor metrics script to count code, comment, and empty lines separately 2013-11-05 15:57:18 +00:00
Bob Nystrom 86158193b3 feat: replace special-cased block call method with primitive and remove source length parameter
Remove the `sourceLength` field from the parser and `compile()` signature, switching to null-terminated strings read by `readFile()`. Replace the special `METHOD_CALL` method type with a `block_call` primitive that invokes `callBlock()` on the fiber, and move the `Fiber` and `CallFrame` structs into the public header so primitives can access them. Add a test for block calling via the new primitive.
2013-11-05 15:56:59 +00:00
Bob Nystrom 074deebdfd feat: add boolean literals and number comparison operators to compiler and VM
Add TOKEN_TRUE/TOKEN_FALSE parsing with boolean() prefix rule emitting CODE_TRUE/CODE_FALSE bytecodes. Wire comparison operators <, >, <=, >= as infix rules with PREC_COMPARISON precedence. Implement num_lt, num_gt, num_lte, num_gte primitives returning bool values. Register boolClass with toString primitive and move class initialization into registerPrimitives(). Add OBJ_TRUE/OBJ_FALSE object types with makeBool() helper and AS_BOOL macro. Include test files for bool_toString and number_comparison.
2013-11-04 05:38:58 +00:00
Bob Nystrom 0fc8432442 feat: replace recursive descent parsing with Pratt parser for expression handling
Replace the hand-written recursive descent parsing functions (term, factor, primary) with a table-driven Pratt parser using ParseRule structs. The new implementation introduces a parsePrecedence function that dispatches to prefix and infix parse functions based on token type and precedence levels, enabling cleaner extensibility for future operators. Adds precedence enum (PREC_NONE through PREC_CALL) and ParseRule table mapping token types to their corresponding parse functions and binding power.
2013-11-03 18:16:14 +00:00
Bob Nystrom ffc04e97f7 feat: add toString method for numbers and strings with AS_NUM/AS_STRING macros
Introduce num_toString primitive that converts numeric values to string representation via sprintf with "%g" format, and add AS_NUM and AS_STRING accessor macros to vm.h for cleaner type extraction. Refactor existing numeric and string primitives to use these macros instead of direct struct member access. Include test files for number_toString and string_toString with basic equality checks (skipped due to unimplemented == operator).
2013-11-02 06:27:28 +00:00
Bob Nystrom 8ff3684cbd fix: allow files without trailing newline in block compilation
The compiler previously required a TOKEN_LINE after each statement in a block,
causing parse failures for files missing a final newline. Changed consume() to
match() with conditional break, and added a test case for no-trailing-newline
files.
2013-11-01 13:57:21 +00:00
Bob Nystrom 5a9330be0e feat: add interactive REPL mode with line-by-line compilation and execution
Extract main execution logic into runFile() and runRepl() functions, where runRepl() reads input line by line from stdin using a 1024-char buffer, compiles each line into a block, interprets it, and prints the resulting value. Update main() to dispatch to runRepl() when no arguments are given, or to runFile() with a single path argument, with a usage message for invalid argument counts.
2013-11-01 05:25:00 +00:00
Bob Nystrom ee0d2ad486 feat: add Python script to count files, lines, and TODO comments in src/*.[ch] 2013-11-01 04:54:04 +00:00
Bob Nystrom d3cffd521d feat: add parentheses grouping support in expression parser
Implement parenthesized expression parsing in the primary() function by matching TOKEN_LEFT_PAREN, recursively calling expression(), and consuming TOKEN_RIGHT_PAREN. Add grammar test cases in test/grammar.wren verifying correct operator precedence with parentheses, including nested grouping expressions.
2013-11-01 04:49:15 +00:00
Bob Nystrom 66961a3c1a feat: replace fixed-size file buffer with dynamic allocation and add error handling
Replace the hardcoded MAX_FILE_SIZE buffer with a dynamically allocated buffer sized to the actual file length. Add a failIf helper function for consistent error reporting with formatted messages, and apply it to handle file open, memory allocation, and read failures with appropriate exit codes. Fix the primitive method call to include the receiver in the argument count.
2013-10-31 21:39:41 +00:00