Commit Graph

62 Commits

Author SHA1 Message Date
Kayne Ruse
b32ea9f309 Started working on a decompiler, called 'bytecode inspector'
It only has a few instructions for now, but I can flesh it out over
time.
2026-04-11 13:37:26 +10:00
Kayne Ruse
547229e150 Script tests re-added, all tests can run under gdb
Also fixed a minor bug with printing, and removed the ability to
configure the parser.

Added and updated QUICKSTART.md as a quick way to get people started.

There's some broken scripts under 'scripts/' that require functions to
work properly.
2026-04-10 15:28:56 +10:00
Kayne Ruse
842f041a50 VM test is passing 2026-04-07 21:34:05 +10:00
Kayne Ruse
f06218b9cd 'print' now works as expected
String literals are now stored in the bucket mid-compilation, but this
is fine, as the buckets are freed one the bytecode is complete.
2026-04-06 23:08:17 +10:00
Kayne Ruse
57fe9bb00d WIP: Compiles but still very broken 2026-04-05 17:04:30 +10:00
Kayne Ruse
d3b59eb0da WIP functions working, untested, memory issues, read more
I've ripped out the deep copying, and flattened the bucket usage, but
this results in no memory being freed or reused for the lifetime of the
program.

This is shown most clearly with this script:

```toy
fn makeCounter() {
	var counter: int = 0;

	fn increment() {
		return ++counter;
	}

	return increment;
}

var tally = makeCounter();

while (true) {
	var result = tally();

	if (result >= 10_000_000) {
		break;
	}
}
```

The number of calls vs amount of memory consumed is:

```
function calls -> memory used in megabytes
1_000 -> 0.138128
10_000 -> 2.235536
100_000 -> 21.7021
1_000_000 -> 216.1712
10_000_000 -> 1520.823
```

Obviously this needs to be fixed, as ballooning to gigabytes of memory
in only a moment isn't practical. That will be the next task - to find
some way to free memory that isn't needed anymore.

See #163, #160
2025-02-21 11:41:27 +11:00
Kayne Ruse
9fe6d6b218 WIP bad approach, read more
I build a self-referential system, then tried to copy only parts. I need
to step back and adjust my approach.

'Toy_private_deepCopyValue' and 'Toy_private_deepCopyScope' need to be
ripped out, and I need to simply accept there will be only one instance
of 'Toy_Bucket' that isn't freed until the top-level VM is.

I need an hour's break before I'll tackle this again.

See #163
2025-02-18 13:06:15 +11:00
Kayne Ruse
639250f028 WIP return keyword, read more
Functions are having issues with being copied around, especially
between buckets, leading to the scopes getting looped. The program gets
stuck in 'incrementRefCount()'.

It's past my time limit, so I'll keep working on it tomorrow with a
fresh mind.

All function stuff is still untested.

See #163
2025-02-17 19:10:24 +11:00
Kayne Ruse
02dfc996b4 Functions are successfully called, read more
Return keyword is not yet implemented.

Functions are untested.

See #163
2025-02-17 14:05:07 +11:00
Kayne Ruse
76d89fe0ad Reduced excessive calls to Toy_unwrapValue()
In practice, references only point to arrays or tables.

Fixed #176
2025-02-17 04:42:22 +11:00
Kayne Ruse
258968d7a4 WIP, Functions are declared, still not called 2025-02-11 11:55:35 +11:00
Kayne Ruse
336616a1bf Tweaked truthiness, fixed int to float coersion 2025-02-02 16:38:46 +11:00
Kayne Ruse
481d17f040 Tests are passing, added preserveScope to VM API 2025-01-31 13:51:53 +11:00
Kayne Ruse
bfed4e23f3 Repl works, also fixed a few small bugs 2025-01-31 10:16:06 +11:00
Kayne Ruse
002651f95d WIP, adjusting architecture, read more
The 'source' directory compiles, but the repl and tests are almost
untouched so far. There's no guarantee that the code in 'source' is
correct, so I'm branching this for a short time, until I'm confident the
whole project passes the CI again.

I'm adjusting the concepts of routines and bytecode to make them more
consistent, and tweaking the VM so it loads from an instance of
'Toy_Module'.

* 'Toy_ModuleBuilder' (formally 'Toy_Routine')

This is where the AST is compiled, producing a chunk of memory that can
be read by the VM. This will eventually operate on individual
user-defined functions as well.

* 'Toy_ModuleBundle' (formally 'Toy_Bytecode')

This collects one or more otherwise unrelated modules into one chunk of
memory, stored in sequence. It is also preprended with the version data for
Toy's reference implementation:

For each byte in the bytecode:

    0th: TOY_VERSION_MAJOR
    1st: TOY_VERSION_MINOR
    2nd: TOY_VERSION_PATCH
    3rd: (the number of modules in the bundle)
    4th and onwards: TOY_VERSION_BUILD

TOY_VERSION_BUILD has always been a null terminated C-string, but from
here on, it begins at the word-alignment, and continues until the first
word-alignment after the null terminator.

As for the 3rd byte listed, since having more than 256 modules in one
bundle seems unlikely, I'm storing the count here, as it was otherwise
unused. This is a bit janky, but it works for now.

* 'Toy_Module'

This new structure represents a single complete unit of operation, such
as a single source file, or a user-defined function. It is divided into
three main sections, with various sub-sections.

    HEADER (all members are unsigned ints):
        total module size in bytes
        jumps count
        param count
        data count
        subs count
        code addr
        jumps addr (if jumps count > 0)
        param addr (if param count > 0)
        data addr (if data count > 0)
        subs addr (if subs count > 0)
    BODY:
        <raw opcodes, etc.>
    DATA:
        jumps table
            uint array, pointing to addresses in 'data' or 'subs'
        param table
            uint array, pointing to addresses in 'data'
        data
            heterogeneous data, including strings
        subs
            an array of modules, using recursive logic

The reference implementation as a whole uses a lot of recursion, so this
makes sense.

The goal of this rework is so 'Toy_Module' can be added as a member of
'Toy_Value', as a simple and logical way to handle functions. I'll
probably use the union pattern, similarly to Toy_String, so functions
can be written in C and Toy, and used without needing to worry which is
which.
2025-01-29 10:21:33 +11:00
Kayne Ruse
9141102f2e Fixed stack overflow caused by assignments, read more
Variable declaration was also causing this issue.

It was caused by a value being left on the stack after these statements.
It wasn't a quick fix, as chained assignments depended on it. Now, the
assignment opcode has a configuration option, indicating if the last
value should be left on the stack or not.

This also means the benchmark in 'scripts/benchpress.toy' will no longer
cause a stack overflow.

Fixed #171
2025-01-11 15:07:29 +11:00
Kayne Ruse
6f16c31f24 Prefix '++' working (postfix is next) 2025-01-09 16:45:48 +11:00
Kayne Ruse
b55192e513 Removed stubs for types as values
Also potentially fixed a bug in the previous commit, not certain
2025-01-09 11:46:29 +11:00
Kayne Ruse
23eb3e45df Keywords 'break' & 'continue' tested
There were a couple bugs - I'm glad I'm so thorough with these tests.

See #152
2024-12-30 16:56:57 +11:00
Kayne Ruse
b84a70cc34 Keywords 'break' and 'continue' are working, untested
See #152
2024-12-30 09:50:51 +11:00
Kayne Ruse
cc4ff3f6d8 Fixed logical AND and OR operators, read more
The definition of '&&':
  Return the first falsy value, or the last value, skipping the evaluation of other operands.

The definition of '||':
  Return the first truthy value, or the last value, skipping the evaluation of other operands.

Toy now follows these definitions.

Fixed #154
2024-12-26 16:09:16 +11:00
Kayne Ruse
24cfe7f539 Fixed some error states and error messages, read more
By leaving 'null' on the stack, it won't cause stack underflows in a
bunch of erroneous situations. This will allow the repl (and other
situations) to continue if they want to.

I've also fixed some error messages in toy_table.c, which were formatted
badly.

Closes #162
2024-12-26 14:33:26 +11:00
Kayne Ruse
9cb138a7d6 Added tables to integration tests, tweaked a lot of comments 2024-12-25 11:04:18 +11:00
Kayne Ruse
9e2cbb1f59 Quick fix for some self-referential table bugs 2024-12-24 16:47:58 +11:00
Kayne Ruse
b092b8ce50 Added tables to the scripts, untested
This also has a lot of bugfixing.
2024-12-24 16:08:42 +11:00
Kayne Ruse
4faa0c0476 Fixed nested assignment bug 2024-12-24 11:58:51 +11:00
Kayne Ruse
04c799954c Tweaked standard bucket sizes, see #160
Hyacinth: It's pronounced "Bouquet"!
2024-12-17 22:25:33 +11:00
Kayne Ruse
a28053d4e9 Reworked Toy_String as a union, enabled -Wpedantic
Toy now fits into the C spec.

Fixed #158

Addendum: MacOS test caught an error:

error: a function declaration without a prototype is deprecated in all versions of C

That took 3 attempts to fix correctly.

Addendum: 'No new line at the end of file' are you shitting me?
2024-12-15 15:52:06 +11:00
Kayne Ruse
93fce94e9c Locales are hard, sorry!
Fixed #159
2024-12-14 12:57:53 +11:00
Kayne Ruse
fce71a6cda Tweaked CFLAGS, and fixed related errors 2024-12-12 16:18:41 +11:00
Kayne Ruse
5f4dfdccc5 Updated docs and comments 2024-12-11 17:07:49 +11:00
Kayne Ruse
61a105db2d Compound assignment for arrays is working, untested, read more
I added reference values in 62ca7a1fb7,
but forgot to mention it. I'm now using references to assign to the
internals of an array, no matter how many levels deep it is.
2024-12-07 15:35:06 +11:00
Kayne Ruse
62ca7a1fb7 WIP: Implementing arrays into the script, read more
I had intended to solve the Advent of Code puzzles in Toy, but they
don't work without arrays. I wasn't able to enable arrays in time, so
I need to focus on doing things correctly.

The most immediate tasks are marked with 'URGENT', and a lot of tests
need writing.
2024-12-02 11:58:03 +11:00
Kayne Ruse
58cecafb3b WIP: Adding arrays to value structure, tests incomplete 2024-11-29 12:17:54 +11:00
Kayne Ruse
0b559ecb74 Fixed the lines marked as 'URGENT' 2024-11-23 10:07:05 +11:00
Kayne Ruse
7d4ea4881f WIP: Fixed print bug, tests incomplete, read more
I was sidetracked by a strange display bug - turns out it was caused by
pointers - this commit fixes it.

The tests for if-then-else still aren't finished, but I'm knocking off
as it's past my time limit. I've marked 'TODO' and 'URGENT' using
comments, so finding the issues should be easy.
2024-11-22 18:21:47 +11:00
Kayne Ruse
34577ecfe1 WIP: if-then-else, incomplete 2024-11-19 17:14:39 +11:00
Kayne Ruse
7398898a61 Added valgrind to the CI, fixed tests
This exposed an issue with my dev environment, which I had to patch.

Fixed #153
2024-11-17 18:49:40 +11:00
Kayne Ruse
2f9489d5fd Fixed a 'malformed assignment' issue, read more
I've also added some support for compiler errors in general, but these
will get expanded on later.

I've also quickly added a valgrind option to the tests and found a few
leaks. I'll deal with these later.

Summary of changes:

* Clarified the lifetime of the bytecode in memory
* Erroneous routines exit without compiling
* Empty VMs don't run
* Added a check for malformed assignments
* Renamed "routine" to "module" within the VM
* VM no longer tries to free the bytecode - must be done manually
* Started experimenting with valgrind, not yet ready
2024-11-16 21:02:37 +11:00
Kayne Ruse
be7e4ddd18 Reworked the tests, read more
I've brought the tests up to scratch, except for compounds im the
parser, because I'm too damn tired to do that over SSH. It looks like
collections are right-recursive, whixh was unintended but still works
just fine.

I've also added the '--verbose' flag to the repl to control the
debugging output.

Several obscure bugs have been fixed, and comments have been tweaked.

Mustfail tests are still needed, but that's a low priority. See #142.

Fixed #151
2024-11-12 22:04:07 +11:00
Kayne Ruse
b74aa63c1c Added verbose debugging option to the REPL 2024-11-12 11:16:50 +11:00
Kayne Ruse
436bd3ffca Added silent cmd args
Fixed #145
2024-11-10 10:27:50 +11:00
Kayne Ruse
1608a13b43 Implemented assert keyword, read more
The assert keyword works, but I want to add a cmd option to suppress or
disable the errors.

The tests need some serious TLC. I know that, but I'm kicking it down
the road for now.
2024-11-09 17:41:29 +11:00
Kayne Ruse
1925d41940 Added indexing to strings, tests still needed 2024-11-09 13:10:54 +11:00
Kayne Ruse
7173f7770f Added types and constness
Fixed #144
2024-11-02 11:39:47 +11:00
Kayne Ruse
d19ca1bcee Reworked variable equality and comparisons
Fixed #146
2024-10-30 19:58:55 +11:00
Kayne Ruse
c5206daaea Implemented scopes 2024-10-27 13:44:09 +11:00
Kayne Ruse
d22b18ed17 Variable access is working 2024-10-26 10:35:47 +11:00
Kayne Ruse
3148a56ce0 Added simple assignment, read more
I was coding earlier this week, but my brain was so foggy I ended up not
knowing what I was doing. After a few days break, I've cleaned up the
mess, which took hours.

Changes:
* Variables can be assigned
* Added new value types as placeholders
* Added 'compare' and 'assign' to the AST
* Added duplicate opcode
* Added functions to copy and free values
* Max name length is 255 chars
* Compound assigns are squeezed into one word

To be completed:

* Tests for this commit's changes
* Compound assignments
* Variable access
2024-10-25 22:48:24 +11:00
Kayne Ruse
694e262ee2 Tweaked 4-byte alignment function
Fixed #140

Thanks @8051enthusiast
2024-10-14 02:07:24 +11:00