Commit Graph

42 Commits

Author SHA1 Message Date
Bob Nystrom
ef93496fd4 fix: remove default constructors and require explicit construct new() for all user classes
The compiler no longer auto-generates a default `new()` constructor for classes. All classes must now explicitly define `construct new() {}` to be instantiable. This change removes the `createDefaultConstructor` function from the compiler, eliminates the `return_this` primitive used by Object's default initializer, and adds explicit constructors to all test and example classes. Core metaclasses (Bool, Class, Null, Num, Object, Range, Sequence) now correctly reject `new()` calls with runtime errors.
2015-09-01 15:16:04 +00:00
Bob Nystrom
c0d90460f0 fix: remove redundant ignoreNewlines call after list instantiation in wren_compiler.c
The ignoreNewlines() call following list instantiation via callMethod(compiler, 0, "new()", 5) was redundant because the subsequent do-while loop for compiling list elements already handles newline skipping internally. Removing this duplicate call eliminates unnecessary parser state manipulation without affecting list compilation behavior.
2015-08-31 14:46:17 +00:00
Bob Nystrom
075f487b6e refactor: restructure list/map compilation loops and extract syntax error tests
- Convert list() and map() compiler functions from if/do-while to do-while loops
- Remove redundant empty literal handling by breaking on right bracket/brace
- Add new test files for duplicate comma, duplicate trailing comma, and empty list/map with comma
- Expand newlines test to cover trailing comma and empty list/map cases
- Simplify trailing_comma tests by removing inline syntax error checks
- Update Test.fail() to handle variable arguments without kwargs
2015-08-31 14:23:36 +00:00
Lukas Werling
d2c8bacbaa feat: allow trailing comma in list literal by early break on right bracket
Add support for trailing commas in list literals by checking for TOKEN_RIGHT_BRACKET
after parsing the last element in the list() function. When a trailing comma is
followed by the closing bracket, the loop breaks immediately instead of attempting
to parse another element.

Include a test case verifying that lists with trailing commas parse correctly and
elements are accessible by index.
2015-05-30 20:18:37 +00:00
Lukas Werling
bf3aea5755 feat: allow trailing comma in map literal by checking right brace in parser
Add a check in the map parsing function to break when encountering a closing brace after a comma, enabling trailing commas in map literals. Include a test case verifying that maps with trailing commas parse and execute correctly.
2015-05-30 20:17:58 +00:00
Bob Nystrom
674f682ce1 style: reorder single-char token cases in nextToken and fix wren_utils.h comment 2015-08-22 14:56:38 +00:00
Bob Nystrom
4f053e7a3c feat: add matchChar helper and refactor twoCharToken, skipBlockComment, readNumber, and nextToken to use it 2015-08-22 06:08:11 +00:00
Bob Nystrom
e4adcd5c5b feat: store number and string literals directly as Value in parser struct
Replace the separate `string` ByteBuffer and `number` double fields in the Parser struct with a single `value` field of type Value. This unifies literal storage, simplifies the parser's state, and eliminates the need for separate buffers and conversion logic for different literal types.

The change modifies `makeNumber` to store parsed numbers as `NUM_VAL` directly into `parser->value`, and adds a guard in `addConstant` to return -1 early if the parser has encountered an error.
2015-08-22 05:54:30 +00:00
Bob Nystrom
16613c678b fix: rename constructor initializer keyword from "this" to "init" across compiler, core, and documentation
Rename the constructor initializer signature prefix from "this " to "init " in
wren_common.h's MAX_METHOD_SIGNATURE calculation, wren_compiler.c's
signatureToString and createDefaultConstructor functions, and wren_core.c's
default Object constructor primitive registration. This changes the syntactic
keyword used to define initializer methods in the Wren language.
2015-08-21 05:30:44 +00:00
Bob Nystrom
869ecb9f07 fix: correct error message and comment to use 'construct' instead of 'this' in constructor methods 2015-08-21 05:23:53 +00:00
minirop
d675e77081 chore: update error messages and comments to use 'construct' instead of 'this' for constructors
- Changed error message in `constructorSignature` from "Expect constructor name after 'this'." to "Expect constructor name after 'construct'."
- Updated comment in `createDefaultConstructor` from "It just invokes 'this new()' on the instance" to "It just invokes 'construct new()' on the instance"
2015-08-19 17:33:54 +00:00
Bob Nystrom
2dc209e585 feat: add foreign class support with allocation, construct opcodes, and CLI callback wiring
Implement the initial infrastructure for foreign classes in the Wren VM, including new opcodes (FOREIGN_CONSTRUCT, FOREIGN_CLASS), a WrenBindForeignClassFn callback type in the public API, and updated CLI vm.c to store and forward foreign binding callbacks via setForeignCallbacks(). The compiler now tracks whether a class is foreign via a new isForeign flag in ClassCompiler, emits CODE_FOREIGN_CONSTRUCT instead of CODE_CONSTRUCT for foreign class constructors, and errors on field definitions inside foreign classes. The debug dump and opcode header gain entries for the new opcodes, and wrenBindSuperclass handles the -1 numFields sentinel for foreign classes to prevent field inheritance from superclasses.
2015-08-15 19:07:53 +00:00
Bob Nystrom
8468ca7642 fix: fully initialize Signature struct fields in signatureFromToken and methodCall 2015-08-06 14:34:19 +00:00
Bob Nystrom
7d9e45e906 feat: replace 'this' keyword with 'construct' for constructor definitions across core lib, compiler, docs, and tests
Add TOKEN_CONSTRUCT as a new reserved word in the Wren compiler lexer and parser, and update the grammar rule table so that 'construct' triggers a constructor signature instead of 'this'. Modify all built-in class definitions in core.wren, wren_core.c, benchmark files, and test fixtures to use 'construct new(...)' and 'construct named(...)' syntax. Update documentation in classes.markdown and syntax.markdown to reflect the new keyword, and remove the deprecated test files for 'new' and infix class expressions.
2015-07-21 14:24:53 +00:00
Bob Nystrom
67f1480bf0 chore: migrate Travis CI to container-based builds and update README links
- Switch Travis CI from sudo-based apt-get to addons.apt.packages for gcc-multilib
- Enable container-based builds with `sudo: false`
- Update README.md call-to-action links to point to getting-started and browser playground
- Replace `printList_` with `printAll` in IO class and remove redundant `IO.` prefixes
- Add IO.read() method documentation and update IO.print docs for up to 16 arguments
- Reorganize community page with Libraries, Language bindings, and Tools sections
- Add wrenjs JavaScript binding and Sublime Text syntax highlighting
- Fix typo "no where" to "nowhere" in fibers documentation
- Add indentation to preprocessor defines in wren_common.h for consistency
- Remove unreachable `return 0` after UNREACHABLE() in wren_compiler.c
- Fix Windows color output by wrapping text in str() in test script
2015-07-19 19:49:23 +00:00
Bob Nystrom
177d53fe68 fix: align preprocessor indentation and remove dead code after UNREACHABLE in non-nan-tagged and non-computed-goto modes 2015-07-19 17:16:27 +00:00
Bob Nystrom
3ea9e0f2a9 fix: initialize signature type to SIG_GETTER in unarySignature function 2015-07-10 17:27:32 +00:00
Bob Nystrom
852f726c5a fix: cast enum sum to Code type in createConstructor emitShortArg call 2015-07-10 16:22:04 +00:00
Bob Nystrom
e19ff59cce feat: replace new keyword with this constructor syntax and ClassName.new() calls across core library and docs
Replace all uses of the `new` reserved word with explicit `this new()` constructor definitions and `ClassName.new()` instantiation calls in builtin/core.wren. Update all documentation files (classes.markdown, fiber.markdown, fn.markdown, sequence.markdown, error-handling.markdown, expressions.markdown, fibers.markdown, functions.markdown) to reflect the new constructor syntax, removing `new` keyword examples and replacing them with `ClassName.new()` patterns and `this new()` definitions.
2015-07-10 16:18:22 +00:00
minirop
dba104a696 feat: implement scientific notation parsing for number literals in wren compiler
Add support for parsing scientific notation (e.g., 2.55e2, -2.55e-2) in the
readNumber function of wren_compiler.c. The implementation handles optional
negative exponents, validates that exponent digits follow 'e'/'E', and emits
a lex error for unterminated scientific notation. Also adds comprehensive
test cases covering valid scientific literals, missing exponents, floating
exponents, missing fractional parts, and multiple exponents.
2015-07-10 16:07:48 +00:00
Bob Nystrom
5e4ecd4d41 fix: include method name in block argument debug name
When a block is passed as an argument to a method call, the generated
function's debug name now includes the method's signature followed by
" block argument" instead of the generic "(fn)" placeholder. This
improves debugging by showing which method the block belongs to.
2015-06-27 15:36:20 +00:00
Bob Nystrom
ab12bc97e0 fix: remove redundant free in setRootDirectory and fix memory leak in runFile 2015-06-27 06:01:07 +00:00
Bob Nystrom
cd64d67c11 fix: prevent memory leaks by freeing rootDirectory and clearing compiler constants 2015-06-27 05:49:24 +00:00
Bob Nystrom
a4f5ee0280 refactor: replace dedicated CODE_IS opcode with infix operator method call for "is" 2015-06-19 14:58:07 +00:00
Bob Nystrom
275b2ca4e4 refactor: remove redundant null checks before wrenMarkObj calls in compiler, value, and vm modules
The wrenMarkObj function already handles null pointers internally, making the explicit
`if (x != NULL)` guards unnecessary. This change removes three such checks in
wrenMarkCompiler, markFn, and collectGarbage, simplifying the code without changing
behavior.
2015-06-18 14:52:05 +00:00
André Althaus
7fa4676cc4 fix: clear compiler constants buffer in freeCompiler and endCompiler to prevent memory leak
The constants buffer was not being cleared when the compiler finished,
causing a memory leak since ownership of constants is transferred to
the new function via wrenNewFunction. Added wrenValueBufferClear calls
in both freeCompiler and endCompiler to properly release the buffer.
2015-06-03 19:47:16 +00:00
Bob Nystrom
1fd1c76661 fix: statically dispatch super() calls by binding superclass at compile time
The super() call resolution is changed from dynamic runtime lookup (walking the receiver's class hierarchy) to static binding at class definition time. This ensures correct method dispatch when a method containing a super call is inherited by another subclass.

Key changes:
- In `callSignature()`, when compiling a `CODE_SUPER_0` instruction, a constant slot is created and filled with NULL, to be populated with the superclass reference when the method is bound.
- In `getNumArguments()`, the `CODE_SUPER_0` through `CODE_SUPER_16` cases are moved after the newly added 2-argument opcodes (`CODE_JUMP`, `CODE_LOOP`, etc.) to maintain correct ordering.
- In `dumpInstruction()`, the debug output now prints the superclass constant index alongside the method symbol.
- In `runInterpreter()`, the super call execution reads the superclass directly from the function's constant table instead of deriving it from the receiver's class at runtime.
2015-06-02 14:33:39 +00:00
Bob Nystrom
13f63e0d04 feat: add wrenByteBufferFill and wrenMethodBufferFill for bulk buffer growth
Replace the loop-based single-element writes in readUnicodeEscape and wrenBindMethod with a new BufferFill macro that grows the buffer by a specified count in one allocation. The macro doubles capacity until it fits the requested count, then fills the new slots with the given data. The existing BufferWrite is reimplemented as a call to BufferFill with count=1.
2015-05-05 13:54:13 +00:00
Bob Nystrom
4b3b967674 chore: remove double space in finishParameterList and rephrase static method comment 2015-04-06 13:53:53 +00:00
Bob Nystrom
10a6a1d23f fix: restore parent compiler before freeing on compile error
When a compile error occurs, the compiler's code is freed but the parent compiler
was not restored as the active compiler. This could cause the garbage collector
to operate on a dangling compiler pointer if a collection happens after the error.
The fix calls wrenSetCompiler to restore the parent compiler before freeing,
ensuring the GC always sees a valid compiler state.
2015-04-04 06:12:14 +00:00
Bob Nystrom
d92a8f1c93 fix: replace foreign Meta.eval with primitive and suppress compile errors in wrenCompile 2015-04-04 04:22:58 +00:00
Bob Nystrom
0cdbe8bc5a fix: prevent local variables from being in scope during their own initializer 2015-04-01 14:05:40 +00:00
Patricio Mac Adden
1a3ef62f22 fix: replace incorrect wrenDebugPrintCode call with wrenDumpCode in endCompiler 2015-03-28 20:01:21 +00:00
Bob Nystrom
84ad34b281 fix: invert boolean return semantics of finishBlock to match new doc comment
The return value of finishBlock() was inverted so that it now returns true
for expression bodies (value left on stack) and false for statement bodies
(no value left). All internal return points and the call site in finishBody()
were updated accordingly to maintain correct code generation behavior.
2015-03-28 16:15:19 +00:00
Bob Nystrom
c3d0b66630 feat: add raw byte access methods and \x escape to String class
Add `byteAt(index)`, `codePointAt(index)`, and `bytes` sequence to String,
enabling direct byte-level manipulation of UTF-8 encoded strings. Introduce
`\x` hex escape sequence in string literals for specifying raw byte values.
Refactor Unicode escape parsing into a generic `readHexEscape` function
supporting variable digit counts. Implement `wrenUtf8Decode` utility for
decoding UTF-8 sequences from byte buffers. Add comprehensive tests for
`byteAt` including boundary conditions and error cases.
2015-03-28 03:44:07 +00:00
Bob Nystrom
b7904a89c9 feat: add String.fromCodePoint static method and refactor UTF-8 encoding into reusable utilities
Implement the `String.fromCodePoint(_)` primitive that creates a string from a Unicode code point, with validation for integer range 0–0x10ffff. Extract inline UTF-8 encoding logic from `readUnicodeEscape` in the compiler into new `wrenUtf8NumBytes` and `wrenUtf8Encode` utility functions in `wren_utils`, and add `wrenStringFromCodePoint` helper in `wren_value`. Update documentation for core classes to add "Methods" and "Static Methods" section headers, and include `String.fromCodePoint` docs with example. Add a test file `from_code_point.wren` covering various code points.
2015-03-27 14:43:36 +00:00
Bob Nystrom
a350d7c26a feat: add foreign method binding infrastructure with IO library integration
Implement the `WrenBindForeignMethodFn` callback type and `bindForeignMethodFn` configuration field to allow host applications to resolve foreign method declarations at class definition time. Refactor the IO library to use this new binding mechanism instead of the previous `wrenDefineStaticMethod` approach, adding `foreign` keyword support to the lexer and compiler. Introduce `freeCompiler` helper for cleanup, store module names in `ObjModule`, and update the VM to call the binder when processing `CODE_METHOD_INSTANCE` and `CODE_METHOD_STATIC` opcodes for foreign methods.
2015-03-24 14:58:15 +00:00
Bob Nystrom
15f9273aab refactor: remove unused WrenVM* parameter from buffer init, string find, and debug print functions
Remove the `WrenVM* vm` parameter from `wrenValueBufferInit`, `wrenByteBufferInit`,
`wrenIntBufferInit`, `wrenStringBufferInit`, `wrenSymbolTableInit`,
`wrenMethodBufferInit`, `wrenStringFind`, and `wrenDebugPrintStackTrace`
functions across the compiler, core, debug, utils, value, and VM modules.
Update all call sites and macro definitions to match the simplified signatures,
eliminating dead parameters that were never used within the function bodies.
2015-03-23 05:17:40 +00:00
Bob Nystrom
96a829fc8f refactor: replace ObjList manual array management with ValueBuffer in Compiler and ObjList
Replace the hand-rolled capacity/count/elements fields in ObjList with a
ValueBuffer struct, and update Compiler to use a ValueBuffer for its
constant pool instead of an ObjList. This eliminates the separate
ensureListCapacity and wrenListAdd functions, centralizing buffer growth
logic in wrenValueBufferWrite. Adjust all list primitives (add, clear,
count, iterate, iteratorValue) and the GC marking path to operate on the
embedded ValueBuffer. Add wrenMarkBuffer helper for marking buffer
contents during sweep.
2015-03-22 19:22:47 +00:00
Bob Nystrom
899a4ec76e chore: enable clang suspicious implicit conversion warning and fix related type narrowing issues
Add CLANG_WARN_SUSPICIOUS_IMPLICIT_CONVERSION = YES to both Debug and Release Xcode build configurations. Fix the resulting warnings by adding explicit casts in wren_compiler.c (readUnicodeEscape, emit, emitShort, emitByteArg) and wren_vm.c (READ_SHORT macro, makeCallStub bytecode assignment) to prevent unintended truncation or sign extension during implicit integer conversions.
2015-03-22 17:36:08 +00:00
Bob Nystrom
9314cbfd72 refactor: replace manual string concatenation with wrenStringFormat across VM core
Replace ad-hoc string construction using wrenNewString, wrenStringConcat, and sprintf with the new wrenStringFormat helper. This centralizes string formatting logic, reduces code duplication, and eliminates manual length calculations in wren_compiler.c, wren_core.c, wren_value.c, and wren_vm.c. Also introduces CONST_STRING macro for compile-time string literals and changes wrenNewUninitializedString return type from Value to ObjString* for consistency.
2015-03-16 05:32:20 +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