Compare commits
130 Commits
main
...
better_err
Author | SHA1 | Date | |
---|---|---|---|
|
8da5ae6cc4 | ||
|
4f80c500a2 | ||
|
d9f0b44bed | ||
|
0290bb3bf2 | ||
|
772c56a6df | ||
|
42a5f599f7 | ||
|
f2bae26e1c | ||
|
987cc172c1 | ||
|
24f57c1529 | ||
|
49e46d045b | ||
|
6954857fdd | ||
|
f09caabfcb | ||
|
e4b385d2fb | ||
|
f576868a7a | ||
|
f9e4b4623b | ||
|
bf97a34771 | ||
|
480e05f561 | ||
|
9d798e5e58 | ||
|
f0cf43b486 | ||
|
e2c9619fa6 | ||
|
f35cdd0e8d | ||
|
c00e1275fd | ||
|
e4a948ba94 | ||
|
86de66c4d2 | ||
|
813a79a415 | ||
|
0b27944d11 | ||
|
f58a5f14b5 | ||
|
2f60de79a2 | ||
|
398b140d79 | ||
|
25a0c62dcf | ||
|
583262f9e8 | ||
|
ce11f1cd0f | ||
|
3fe5365586 | ||
|
23d9945c48 | ||
|
92d0915a71 | ||
|
b3e0af41bb | ||
|
121861cc8e | ||
|
a2ae53f8e4 | ||
|
e06a24cf20 | ||
|
1e3fcde57a | ||
|
c73c7e0d6a | ||
|
ef134c0335 | ||
|
e580d68809 | ||
|
f4ade4938c | ||
|
fa587e38cd | ||
|
8b004b45fa | ||
|
7e4ddd3dc4 | ||
|
2f5dab84a7 | ||
|
94b7f98362 | ||
|
1af75bc516 | ||
|
807b2e6ce0 | ||
|
bf1e7e4072 | ||
|
442532ecd3 | ||
|
4dd4b8ff7e | ||
|
35d7d3b1c8 | ||
|
a4d68fa02e | ||
|
2b95094eca | ||
|
4871dbd048 | ||
|
647f3d4463 | ||
|
fb2488c850 | ||
|
f6bfe0975b | ||
|
ffe5d2ad61 | ||
|
23298c8538 | ||
|
77f1627132 | ||
|
6bd419125f | ||
|
a5f2e2a9bd | ||
|
a0ef6d2777 | ||
|
d727096422 | ||
|
979afdbcb9 | ||
|
316e8a6a58 | ||
|
ecd16e3aef | ||
|
e3b5b96dc2 | ||
|
6ded94f7d0 | ||
|
0347d10db7 | ||
|
77faf67191 | ||
|
f8adaa7971 | ||
|
dee9bcfc33 | ||
|
f3bf55fe72 | ||
|
b557c487cc | ||
|
b8b720b877 | ||
|
23b8beb291 | ||
|
5ae4742840 | ||
|
0f8645d2eb | ||
|
01ce043379 | ||
|
2ce2e2c2d3 | ||
|
61b1b7bf90 | ||
|
bf2b16c30f | ||
|
c497b90a16 | ||
|
fc245348b4 | ||
|
615fef6ebc | ||
|
86992078e9 | ||
|
681176282c | ||
|
22d1ceb3e8 | ||
|
de3d7e834c | ||
|
b6f9b35b4c | ||
|
34ab24c4c9 | ||
|
cda217f6ef | ||
|
82ac6744ca | ||
|
182b14e5f4 | ||
|
74cc0025d6 | ||
|
db7eb5965d | ||
|
1e1593298d | ||
|
04c96c9edb | ||
|
45d2e7e742 | ||
|
cf51822128 | ||
|
4f7ba56d1f | ||
|
ce1612a30c | ||
|
1ef0da5dd1 | ||
|
4a4b2b22ed | ||
|
6c803cdf5a | ||
|
a7ee8f8e57 | ||
|
8908630a21 | ||
|
6f582bff06 | ||
|
f5965fdb44 | ||
|
cfe0b83192 | ||
|
4fa2ce5e78 | ||
|
40d4f48878 | ||
|
ef0ac40dbe | ||
|
a4f12c8f7d | ||
|
9f4e630544 | ||
|
be23ee6c44 | ||
|
d943185db8 | ||
|
d4342b0623 | ||
|
48754f92a4 | ||
|
096d8d00bc | ||
|
9c3205d4c1 | ||
|
6c78cffe56 | ||
|
35fc591c76 | ||
|
eff2ed90d5 | ||
|
86aea78c21 |
|
@ -12,3 +12,9 @@ imbl = "3.0.0"
|
||||||
struct_scalpel = "0.1.1"
|
struct_scalpel = "0.1.1"
|
||||||
ran = "2.0.1"
|
ran = "2.0.1"
|
||||||
rust-embed = "8.5.0"
|
rust-embed = "8.5.0"
|
||||||
|
boxing = "0.1.2"
|
||||||
|
ordered-float = "4.5.0"
|
||||||
|
index_vec = "0.1.4"
|
||||||
|
num-derive = "0.4.2"
|
||||||
|
num-traits = "0.2.19"
|
||||||
|
regex = "1.11.1"
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
& this file, uniquely, gets `base` loaded as context. See src/base.janet for exports
|
& this file, uniquely, gets `base` loaded as context. See src/base.janet for exports
|
||||||
|
|
||||||
let base = base
|
& let base = base
|
||||||
|
|
||||||
& some forward declarations
|
& some forward declarations
|
||||||
& TODO: fix this so that we don't need (as many of) them
|
& TODO: fix this so that we don't need (as many of) them
|
||||||
fn and
|
& fn and
|
||||||
fn append
|
fn append
|
||||||
fn apply_command
|
fn apply_command
|
||||||
fn assoc
|
fn assoc
|
||||||
|
@ -25,7 +25,6 @@ fn turn/rad
|
||||||
fn unbox
|
fn unbox
|
||||||
fn update!
|
fn update!
|
||||||
|
|
||||||
& the very base: know something's type
|
|
||||||
fn type {
|
fn type {
|
||||||
"Returns a keyword representing the type of the value passed in."
|
"Returns a keyword representing the type of the value passed in."
|
||||||
(x) -> base :type (x)
|
(x) -> base :type (x)
|
||||||
|
@ -124,19 +123,19 @@ fn not {
|
||||||
(_) -> false
|
(_) -> false
|
||||||
}
|
}
|
||||||
|
|
||||||
fn neq? {
|
& fn neq? {
|
||||||
"Returns true if none of the arguments have the same value."
|
& "Returns true if none of the arguments have the same value."
|
||||||
(x) -> false
|
& (x) -> false
|
||||||
(x, y) -> not (eq? (x, y))
|
& (x, y) -> not (eq? (x, y))
|
||||||
(x, y, ...zs) -> if eq? (x, y)
|
& (x, y, ...zs) -> if eq? (x, y)
|
||||||
then false
|
& then false
|
||||||
else loop (y, zs) with {
|
& else loop (y, zs) with {
|
||||||
(a, []) -> neq? (a, x)
|
& (a, []) -> neq? (a, x)
|
||||||
(a, [b, ...cs]) -> if neq? (a, x)
|
& (a, [b, ...cs]) -> if neq? (a, x)
|
||||||
then recur (b, cs)
|
& then recur (b, cs)
|
||||||
else false
|
& else false
|
||||||
}
|
& }
|
||||||
}
|
& }
|
||||||
|
|
||||||
& tuples: not a lot you can do with them functionally
|
& tuples: not a lot you can do with them functionally
|
||||||
fn tuple? {
|
fn tuple? {
|
||||||
|
@ -589,10 +588,10 @@ fn zero? {
|
||||||
fn gt? {
|
fn gt? {
|
||||||
"Returns true if numbers are in decreasing order."
|
"Returns true if numbers are in decreasing order."
|
||||||
(x as :number) -> true
|
(x as :number) -> true
|
||||||
(x as :number, y as :number) -> base :gt (x, y)
|
(x as :number, y as :number) -> base :gt? (x, y)
|
||||||
(x, y, ...zs) -> loop (y, zs) with {
|
(x, y, ...zs) -> loop (y, zs) with {
|
||||||
(a, [b]) -> base :gt (a, b)
|
(a, [b]) -> base :gt? (a, b)
|
||||||
(a, [b, ...cs]) -> if base :gt (a, b)
|
(a, [b, ...cs]) -> if base :gt? (a, b)
|
||||||
then recur (b, cs)
|
then recur (b, cs)
|
||||||
else false
|
else false
|
||||||
}
|
}
|
||||||
|
@ -601,10 +600,10 @@ fn gt? {
|
||||||
fn gte? {
|
fn gte? {
|
||||||
"Returns true if numbers are in decreasing or flat order."
|
"Returns true if numbers are in decreasing or flat order."
|
||||||
(x as :number) -> true
|
(x as :number) -> true
|
||||||
(x as :number, y as :number) -> base :gte (x, y)
|
(x as :number, y as :number) -> base :gte? (x, y)
|
||||||
(x, y, ...zs) -> loop (y, zs) with {
|
(x, y, ...zs) -> loop (y, zs) with {
|
||||||
(a, [b]) -> base :gte (a, b)
|
(a, [b]) -> base :gte? (a, b)
|
||||||
(a, [b, ...cs]) -> if base :gte (a, b)
|
(a, [b, ...cs]) -> if base :gte? (a, b)
|
||||||
then recur (b, cs)
|
then recur (b, cs)
|
||||||
else false
|
else false
|
||||||
}
|
}
|
||||||
|
@ -613,10 +612,10 @@ fn gte? {
|
||||||
fn lt? {
|
fn lt? {
|
||||||
"Returns true if numbers are in increasing order."
|
"Returns true if numbers are in increasing order."
|
||||||
(x as :number) -> true
|
(x as :number) -> true
|
||||||
(x as :number, y as :number) -> base :lt (x, y)
|
(x as :number, y as :number) -> base :lt? (x, y)
|
||||||
(x, y, ...zs) -> loop (y, zs) with {
|
(x, y, ...zs) -> loop (y, zs) with {
|
||||||
(a, [b]) -> base :lt (a, b)
|
(a, [b]) -> base :lt? (a, b)
|
||||||
(a, [b, ...cs]) -> if base :lt (a, b)
|
(a, [b, ...cs]) -> if base :lt? (a, b)
|
||||||
then recur (b, cs)
|
then recur (b, cs)
|
||||||
else false
|
else false
|
||||||
}
|
}
|
||||||
|
@ -625,10 +624,10 @@ fn lt? {
|
||||||
fn lte? {
|
fn lte? {
|
||||||
"Returns true if numbers are in increasing or flat order."
|
"Returns true if numbers are in increasing or flat order."
|
||||||
(x as :number) -> true
|
(x as :number) -> true
|
||||||
(x as :number, y as :number) -> base :lte (x, y)
|
(x as :number, y as :number) -> base :lte? (x, y)
|
||||||
(x, y, ...zs) -> loop (y, zs) with {
|
(x, y, ...zs) -> loop (y, zs) with {
|
||||||
(a, [b]) -> base :lte (a, b)
|
(a, [b]) -> base :lte? (a, b)
|
||||||
(a, [b, ...cs]) -> if base :lte (a, b)
|
(a, [b, ...cs]) -> if base :lte? (a, b)
|
||||||
then recur (b, cs)
|
then recur (b, cs)
|
||||||
else false
|
else false
|
||||||
}
|
}
|
||||||
|
@ -669,7 +668,7 @@ fn odd? {
|
||||||
fn min {
|
fn min {
|
||||||
"Returns the number in its arguments that is closest to negative infinity."
|
"Returns the number in its arguments that is closest to negative infinity."
|
||||||
(x as :number) -> x
|
(x as :number) -> x
|
||||||
(x as :number, y as :number) -> if lt? (x, y) then x else y
|
(x as :number, y as :number) -> if base :lt? (x, y) then x else y
|
||||||
(x, y, ...zs) -> fold (min, zs, min (x, y))
|
(x, y, ...zs) -> fold (min, zs, min (x, y))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -698,7 +697,7 @@ fn at {
|
||||||
|
|
||||||
fn first {
|
fn first {
|
||||||
"Returns the first element of a list or tuple."
|
"Returns the first element of a list or tuple."
|
||||||
(xs) if ordered? (xs) -> at (xs, 0)
|
(xs) if ordered? -> at (xs, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn second {
|
fn second {
|
||||||
|
@ -738,22 +737,22 @@ fn keyword? {
|
||||||
|
|
||||||
& TODO: determine if Ludus should have a `keyword` function that takes a string and returns a keyword. Too many panics, it has weird memory consequences, etc.
|
& TODO: determine if Ludus should have a `keyword` function that takes a string and returns a keyword. Too many panics, it has weird memory consequences, etc.
|
||||||
|
|
||||||
& TODO: make `and` and `or` special forms which lazily evaluate arguments
|
& & TODO: make `and` and `or` special forms which lazily evaluate arguments
|
||||||
fn and {
|
& fn and {
|
||||||
"Returns true if all values passed in are truthy. Note that this does not short-circuit: all arguments are evaulated before they are passed in."
|
& "Returns true if all values passed in are truthy. Note that this does not short-circuit: all arguments are evaulated before they are passed in."
|
||||||
() -> true
|
& () -> true
|
||||||
(x) -> bool (x)
|
& (x) -> bool (x)
|
||||||
(x, y) -> base :and (x, y)
|
& (x, y) -> base :and (x, y)
|
||||||
(x, y, ...zs) -> fold (and, zs, base :and (x, y))
|
& (x, y, ...zs) -> fold (and, zs, base :and (x, y))
|
||||||
}
|
& }
|
||||||
|
|
||||||
fn or {
|
& fn or {
|
||||||
"Returns true if any value passed in is truthy. Note that this does not short-circuit: all arguments are evaluated before they are passed in."
|
& "Returns true if any value passed in is truthy. Note that this does not short-circuit: all arguments are evaluated before they are passed in."
|
||||||
() -> true
|
& () -> true
|
||||||
(x) -> bool (x)
|
& (x) -> bool (x)
|
||||||
(x, y) -> base :or (x, y)
|
& (x, y) -> base :or (x, y)
|
||||||
(x, y, ...zs) -> fold (or, zs, base :or (x, y))
|
& (x, y, ...zs) -> fold (or, zs, base :or (x, y))
|
||||||
}
|
& }
|
||||||
|
|
||||||
fn assoc {
|
fn assoc {
|
||||||
"Takes a dict, key, and value, and returns a new dict with the key set to value."
|
"Takes a dict, key, and value, and returns a new dict with the key set to value."
|
||||||
|
@ -1308,7 +1307,7 @@ box state = nil
|
||||||
#{
|
#{
|
||||||
abs & math
|
abs & math
|
||||||
add & math
|
add & math
|
||||||
and & bool
|
& and & bool
|
||||||
angle & math
|
angle & math
|
||||||
any? & dicts lists strings sets tuples
|
any? & dicts lists strings sets tuples
|
||||||
append & lists sets
|
append & lists sets
|
||||||
|
@ -1402,7 +1401,7 @@ box state = nil
|
||||||
ok & results
|
ok & results
|
||||||
ok? & results
|
ok? & results
|
||||||
& omit & set
|
& omit & set
|
||||||
or & bool
|
& or & bool
|
||||||
ordered? & lists tuples strings
|
ordered? & lists tuples strings
|
||||||
pc! & turtles
|
pc! & turtles
|
||||||
pd! & turtles
|
pd! & turtles
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
255
bytecode_thoughts.md
Normal file
255
bytecode_thoughts.md
Normal file
|
@ -0,0 +1,255 @@
|
||||||
|
# Working notes on bytecode stuff
|
||||||
|
|
||||||
|
### 2024-12-15
|
||||||
|
So far, I've done the easy stuff: constants, and ifs.
|
||||||
|
|
||||||
|
There's still some easy stuff left:
|
||||||
|
* [ ] lists
|
||||||
|
* [ ] dicts
|
||||||
|
* [ ] when
|
||||||
|
* [ ] panic
|
||||||
|
|
||||||
|
So I'll do those next.
|
||||||
|
|
||||||
|
But then we've got two doozies: patterns and bindings, and tuples.
|
||||||
|
|
||||||
|
#### Tuples make things hard
|
||||||
|
In fact, it's tuples that make things hard.
|
||||||
|
The idea is that, when possible, tuples should be stored on the stack.
|
||||||
|
That makes them a different creature than anything else.
|
||||||
|
But the goal is to be able, in a function call, to just push a tuple onto the stack, and then match against it.
|
||||||
|
Because a tuple _isn't_ just another `Value`, that makes things challenging.
|
||||||
|
BUT: matching against all other `Values` should be straightforward enough?
|
||||||
|
|
||||||
|
I think that the way to do this is to reify patterns.
|
||||||
|
Rather than try to emit bytecodes to embody patterns, the patterns are some kind of data that get compiled and pushed onto a stack like keywords and interned strings and whatnot.
|
||||||
|
And then you can push a pattern onto the stack right behind a value, and then have a `match` opcode that pops them off.
|
||||||
|
|
||||||
|
Things get a bit gnarly since patterns can be nested. I'll start with the basic cases and run from there.
|
||||||
|
|
||||||
|
But when things get *very* gnarly is considering tuples on the stack.
|
||||||
|
How do you pop off a tuple?
|
||||||
|
|
||||||
|
Two thoughts:
|
||||||
|
1. Just put tuples on the heap. And treat function arguments/matching differently.
|
||||||
|
2. Have a "register" that stages values to be pattern matched.
|
||||||
|
|
||||||
|
##### Regarding the first option
|
||||||
|
I recall seeing somebody somewhere make a comment that trying to represent function arguments as tuples caused tons of pain.
|
||||||
|
I can see why that would be the case, from an implementation standpoint.
|
||||||
|
We should have _values_, and don't do fancy bookkeeping if we don't have to.
|
||||||
|
|
||||||
|
_Conceptually_, it makes a great deal of sense to think of tuples as being deeply the same as function invocation.
|
||||||
|
But _practically_, they are different things, especially with Rust underneath.
|
||||||
|
|
||||||
|
This feels like this cuts along the grain, and so this is what I will try.
|
||||||
|
|
||||||
|
I suspect that I'll end up specializing a lot around function arguments and calling, but that feels more tractable than the bookkeeping around stack-based tuples.
|
||||||
|
|
||||||
|
### 2024-12-17
|
||||||
|
Next thoughts: take some things systematically rather than choosing an approach first.
|
||||||
|
|
||||||
|
#### Things that always match
|
||||||
|
* Placeholder.
|
||||||
|
- I _think_ this is just a no-op. A `let` expression leaves its rhs pushed on the stack.
|
||||||
|
|
||||||
|
* Word: put something on the stack, and bind a name.
|
||||||
|
- This should follow the logic of locals as articulated in _Crafting Interpreters_.
|
||||||
|
|
||||||
|
In both of these cases, there's no conditional logic, simply a bind.
|
||||||
|
|
||||||
|
#### Things that never bind
|
||||||
|
* Atomic values: put the rhs on the stack, then do an equality check, and panic if it fails. Leave the thing on the stack.
|
||||||
|
|
||||||
|
#### Analysis
|
||||||
|
In terms of bytecode, I think one thing to do, in the simple case, is to do the following:
|
||||||
|
* `push` a `pattern` onto the stack
|
||||||
|
* `match`--pops the pattern and the value off the stack, and then applies the pattern to the value. It leaves the value on the stack, and pushes a special value onto the stack representing a match, or not.
|
||||||
|
- We'll probably want `match-1`, `match-2`, `match-3`, etc., opcodes for matching a value that's that far back in the stack. E.g., `match-1` matches against not the top element, but the `top - 1` element.
|
||||||
|
- This is _specifically_ for matching function arguments and `loop` forms.
|
||||||
|
* There are a few different things we might do from here:
|
||||||
|
- `panic_if_no_match`: panic if the last thing is a `no_match`, or just keep going if not.
|
||||||
|
- `jump_if_no_match`: in a `match` form or a function, we'll want to move to the next clause if there's no match, so jump to the next clause's `pattern` `push` code.
|
||||||
|
* Compound patterns are going to be more complex.
|
||||||
|
- I think, for example, what you're going to need to do is to get opcodes that work on our data structures, so, for example, when you have a `match_compound` opcode and you start digging into the pattern.
|
||||||
|
* Compound patterns are specifically _data structures_. So simple structures should be stack-allocated, and and complex structures should be pointers to something on the heap. Maybe?
|
||||||
|
|
||||||
|
#### A little note
|
||||||
|
For instructions that need more than 256 possibilities, we'll need to mush two `u8`s together into a `u16`. The one liner for this is:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
let number = ((first as u16) << 8) | second as u16;
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Oy, stacks and expressions
|
||||||
|
One thing that's giving me grief is when to pop and when to note on the value stack.
|
||||||
|
|
||||||
|
So, like, we need to make sure that a line of code leaves the stack exactly where it was before it ran, with the exception of binding forms: `let`, `fn`, `box`, etc. Those leave one (or more!) items on the stack.
|
||||||
|
|
||||||
|
In the simplest case, we have a line of code that's just a constant:
|
||||||
|
|
||||||
|
```
|
||||||
|
false
|
||||||
|
```
|
||||||
|
This should emit the bytecode instructions (more or less):
|
||||||
|
```
|
||||||
|
push false
|
||||||
|
pop
|
||||||
|
```
|
||||||
|
The push comes from the `false` value.
|
||||||
|
The pop comes from the end of a (nonbinding) line.
|
||||||
|
|
||||||
|
The problem is that there's no way (at all, in Ludus) to distinguish between an expression that's just a constant and a line that is a complete line of code that's an expression.
|
||||||
|
|
||||||
|
So if we have the following:
|
||||||
|
```
|
||||||
|
let foo = false
|
||||||
|
```
|
||||||
|
We want:
|
||||||
|
```
|
||||||
|
push false
|
||||||
|
```
|
||||||
|
Or, rather, given that `foo` is a word pattern, what we actually want is:
|
||||||
|
```
|
||||||
|
push false # constant
|
||||||
|
push pattern/word # load pattern
|
||||||
|
pop
|
||||||
|
pop # compare
|
||||||
|
push false # for the binding
|
||||||
|
```
|
||||||
|
|
||||||
|
But it's worth it here to explore Ludus's semantics.
|
||||||
|
It's the case that there are actually only three binding forms (for now): `let`, `fn`, and `box`.
|
||||||
|
Figuring out `let` will help a great deal.
|
||||||
|
Match also binds things, but at the very least, match doesn't bind with expressions on the rhs, but a single value.
|
||||||
|
|
||||||
|
Think, too about expressions: everything comes down to a single value (of course), even tuples (especially now that I'm separating function calls from tuple values (probably)).
|
||||||
|
So: anything that *isn't* a binding form should, before the `pop` from the end of a line, only leave a single value on the stack.
|
||||||
|
Which suggests that, as odd as it is, pushing a single `nil` onto the stack, just to pop it, might make sense.
|
||||||
|
Or, perhaps the thing to do is to peek: if the line in question is binding or not, then emit different bytecode.
|
||||||
|
That's probably the thing to do. Jesus, Scott.
|
||||||
|
|
||||||
|
And **another** thing worth internalizing: every single instruction that's not an explicit push or pop should leave the stack length unchanged.
|
||||||
|
So store and load need always to swap in a `nil`
|
||||||
|
|
||||||
|
### 2024-12-23
|
||||||
|
Compiling functions.
|
||||||
|
|
||||||
|
So I'm working through the functions chapter of _CI_, and there are a few things that I'm trying to wrap my head around.
|
||||||
|
|
||||||
|
First, I'm thinking that since we're not using raw pointers, we'll need some functional indirection to get our current byte.
|
||||||
|
|
||||||
|
So one of the hard things here is that, unlike with Lox, Ludus doesn't have fixed-arity functions. That means that the bindings for function calls can't be as dead simple as in Lox. More to the point, because we don't know everything statically, we'll need to do some dynamic magic.
|
||||||
|
|
||||||
|
The Bob Nystrom program uses three useful auxiliary constructs to make functions straightforward:
|
||||||
|
|
||||||
|
* `CallFrame`s, which know which function is being called, has their own instruction pointer, and an offset for the first stack slot that can be used by the function.
|
||||||
|
|
||||||
|
```c
|
||||||
|
typedef struct {
|
||||||
|
ObjFunction* function;
|
||||||
|
uint8_t* ip;
|
||||||
|
Value* slots;
|
||||||
|
} CallFrame;
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
Or the Rust equivalent:
|
||||||
|
```rust
|
||||||
|
struct CallFrame {
|
||||||
|
function: LFn,
|
||||||
|
ip: usize,
|
||||||
|
stack_root: usize,
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
* `Closure`s, which are actual objects that live alongside functions. They have a reference to a function and to an array of "upvalues"...
|
||||||
|
* `Upvalue`s, which are ways of pointing to values _below_ the `stack_root` of the call frame.
|
||||||
|
|
||||||
|
##### Digression: Prelude
|
||||||
|
I decided to skip the Prelude resolution in the compiler and only work with locals. But actually, closures, arguments, and the prelude are kind of the same problem: referring to values that aren't currently available on the stack.
|
||||||
|
|
||||||
|
We do, however, know at compile time the following:
|
||||||
|
* If a binding's target is on the stack, in a closure, or in the prelude.
|
||||||
|
* This does, however, require that the function arguments work in a different way.
|
||||||
|
|
||||||
|
The way to do this, I reckon, is this:
|
||||||
|
* Limit arguments (to, say, no more than 7).
|
||||||
|
* A `CallFrame` includes an arity field.
|
||||||
|
* It also includes an array of length 7.
|
||||||
|
* Each `match` operation in function arguments clones from the call frame, and the first instruction for any given body (i.e. once we've done the match) is to clear the arguments registers in the `CallFrame`, thus decrementing all the refcounts of all the heap-allocated objects.
|
||||||
|
* And the current strategy of scoping and popping in the current implementation of `match` will work just fine!
|
||||||
|
|
||||||
|
Meanwhile, we don't actually need upvalues, because bindings cannot change in Ludus. So instead of upvalues and their indirection, we can just emit a bunch of instructions to have a `values` field on a closure. The compiler, meanwhile, will know how to extract and emit instructions both to emit those values *and* to offer correct offsets.
|
||||||
|
|
||||||
|
The only part I haven't figured out quite yet is how to encode access to what's stored in a closure.
|
||||||
|
|
||||||
|
Also, I'm not certain we need the indirection of a closure object in Ludus. The function object itself can do the work, no?
|
||||||
|
|
||||||
|
And the compiler knows which function it's closing over, and we can emit a bunch of instructions to close stuff over easily, after compiling the function and putting it in the constants table. The way to do this is to yank the value to the top of the stack using normal name resolution procedures, and then use a two-byte operand, `Op::Close` + index of the function in the constants table.
|
||||||
|
|
||||||
|
##### End of digression.
|
||||||
|
And, because we know exactly is bound in a given closure, we can actually emit instructions to close over a given value easily.
|
||||||
|
|
||||||
|
#### A small optimization
|
||||||
|
The lifetimes make things complicated; but I'm not sure that I would want to actually manage them manually, given how much they make my head hurt with Rust. I do get the sense that we will, at some point, need some lifetimes. A `Chunk` right now is chunky, with lots of owned `vec`s.
|
||||||
|
|
||||||
|
Uncle Bob separates `Chunk`s and `Compiler`s, which, yes! But then we have a problem: all of the information to climb back to source code is in the `Compiler` and not in the `Chunk`. How to manage that encoding?
|
||||||
|
|
||||||
|
(Also the keyword and string intern tables should be global, and not only in a single compiler, since we're about to get nested compilers...)
|
||||||
|
|
||||||
|
### 2024-12-24
|
||||||
|
Other interesting optimizations abound:
|
||||||
|
* `add`, `sub`, `inc`, `dec`, `type`, and other extremely frequently used, simple functions can be compiled directly to built-in opcodes. We still need functions for them, with the same arities, for higher order function use.
|
||||||
|
- The special-case logic is in the `Synthetic` compiler branch, rather than anywhere else.
|
||||||
|
- It's probably best to disallow re-binding these names anywhere _except_ Prelude, where we'll want them shadowed.
|
||||||
|
- We can enforce this in `Validator` rather than `Compiler`.
|
||||||
|
* `or` and `and` are likewise built-in, but because they don't evaluate their arguments eagerly, that's another, different special case that's a series of eval, `jump_if_false`, eval, `jump_if_false`, instructions.
|
||||||
|
* More to the point, the difference between `or` and `and` here and the built-ins is that `or` and `and` are variadic, where I was originally thinking about `and` and co. as fixed-arity, with variadic behaviours defined by a shadowing/backing Ludus function. That isn't necessary, I don't think.
|
||||||
|
* Meanwhile, `and` and `or` will also, of necessity, have backing shadowing functions.
|
||||||
|
|
||||||
|
#### More on CallFrames and arg passing
|
||||||
|
* We don't actually need the arguments register! I was complicating things. The stack between the `stack_root` and the top will be _exactly_ the same as an arguments register would have been in my imagination. So we can determine the number of arguments passed in with `stack.len() - stack_root`, and we can access argument positions with `stack_root + n`, since the first argument is at `stack_root`.
|
||||||
|
- This has the added benefit of not having to do any dances to keep the refcount of any heap-allocated objects as low as possible. No extra `Clone`s here.
|
||||||
|
* In addition, we need two `check_arity` ops: one for fixed-arity clauses, and one for clauses with splatterns. Easily enough done. Remember: opcodes are for special cases!
|
||||||
|
|
||||||
|
#### Tail calls
|
||||||
|
* The way to implement tail calls is actually now really straightforward! The idea is to simply have a `TailCall` rather than a `Call` opcode. In place of creating a new stack frame and pushing it to the call stack on top of the old call frame, you pop the old call frame, then push the new one to the call stack.
|
||||||
|
* That does mean the `Compiler` will need to keep track of tail calls. This should be pretty straightforward, actually, and the logic is already there in `Validator`.
|
||||||
|
* The thing here is that the new stack frame simply requires the same return location as the old one it's replacing.
|
||||||
|
* That reminds me that there's an issue in terms of keeping track of not just the IP, but the chunk. In Lox, the IP is a pointer to a `u8`, which works great in C. But in Rust, we can't use a raw pointer like that, but an index into a `vec<u8>`. Which means the return location needs both a chunk and an index, not just a `u8` pointer:
|
||||||
|
```rust
|
||||||
|
struct StackFrame<'a> {
|
||||||
|
function: LFn,
|
||||||
|
stack_root: usize,
|
||||||
|
return: (&'a Chunk, usize),
|
||||||
|
}
|
||||||
|
```
|
||||||
|
(I hate that there's a lifetime here.)
|
||||||
|
|
||||||
|
This gives us a way to access everything we need: where to return to, the root of the stack, the chunk (function->chunk), the closures (function->closures).
|
||||||
|
|
||||||
|
### 2024-12-26
|
||||||
|
One particular concern here, which needs some work: recursion is challenging.
|
||||||
|
|
||||||
|
In particular, the issue is that if, as I have been planning, a function closes over all its values at the moment it is compiled, the only value type that requires updating is a function. A function can be declared but not yet defined, and then when another function that uses that function is defined, the closed-over value will be to the declaration but not the definition.
|
||||||
|
|
||||||
|
One way to handle this, I think is using `std::cell::OnceCell`. Rather than a `RefCell`, `OnceCell` has no runtime overhead. Instead, what happens is you effectively put a `None` in the cell. Then, once you have the value you want to put in there, you call `set` on the `OnceCell`, and it does what it needs to.
|
||||||
|
|
||||||
|
This allows for the closures to be closed over right after compilation.
|
||||||
|
|
||||||
|
### 2024-12-27
|
||||||
|
Working on `loop` and `recur`, rather than `fn`--this is the gentler slope.
|
||||||
|
And discovering that we actually need a `[Value; 15]` return register.
|
||||||
|
`recur` needs to push all the arguments to the stack, then yank them off into the return register, then pop back to the loop root, then push all the things back onto the stack, then jump to the beginning of the loop.
|
||||||
|
And that also means I need a different `Value` variant that's a true `Nothing`, not even `nil`, which will _never_ end up anywhere other than a placeholder value in the register and on the stack.
|
||||||
|
|
||||||
|
So, next steps:
|
||||||
|
1. Add `Value::Nothing` and fix all the compiler errors
|
||||||
|
2. Make the return register `[Value; 15]`, populated with `Value::Nothing`s at initialization.
|
||||||
|
3. Update `load` and `store` to work with the array rather than a single value.
|
||||||
|
4. Create `store_n` and `load_n` to work with multiple values.
|
||||||
|
5. Create a `Vm.arity` method that computes how many non-nothings were passed into the register.
|
||||||
|
6. Then, implement `recur`
|
||||||
|
7. And, then, fix up jump indexes in `loop`
|
||||||
|
8. Fix all the off-by-one errors in the jumps
|
631
may_2025_thoughts.md
Normal file
631
may_2025_thoughts.md
Normal file
|
@ -0,0 +1,631 @@
|
||||||
|
# Catching back up
|
||||||
|
## May 2025
|
||||||
|
|
||||||
|
### Bugs
|
||||||
|
|
||||||
|
#### `match` is not popping things correctly
|
||||||
|
```
|
||||||
|
=== source code ===
|
||||||
|
|
||||||
|
let foo = match :foo with {
|
||||||
|
:foo -> 1
|
||||||
|
:bar -> 2
|
||||||
|
:baz -> 3
|
||||||
|
}
|
||||||
|
foo
|
||||||
|
|
||||||
|
=== chunk: test ===
|
||||||
|
IDX | CODE | INFO
|
||||||
|
0000: reset_match
|
||||||
|
0001: constant 0000: :foo
|
||||||
|
0003: match_constant 0000: :foo
|
||||||
|
0005: jump_if_no_match 0006
|
||||||
|
0007: constant 0001: 1
|
||||||
|
0009: store
|
||||||
|
0010: pop
|
||||||
|
0011: jump 0023
|
||||||
|
0013: match_constant 0002: :bar
|
||||||
|
0015: jump_if_no_match 0006
|
||||||
|
0017: constant 0003: 2
|
||||||
|
0019: store
|
||||||
|
0020: pop
|
||||||
|
0021: jump 0013
|
||||||
|
0023: match_constant 0004: :baz
|
||||||
|
0025: jump_if_no_match 0006
|
||||||
|
0027: constant 0005: 3
|
||||||
|
0029: store
|
||||||
|
0030: pop
|
||||||
|
0031: jump 0003
|
||||||
|
0033: panic_no_match
|
||||||
|
0034: load
|
||||||
|
0035: match_word
|
||||||
|
0036: panic_if_no_match
|
||||||
|
0037: push_binding 0000
|
||||||
|
0039: store
|
||||||
|
0040: pop
|
||||||
|
0041: pop
|
||||||
|
0042: load
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
=== vm run: test ===
|
||||||
|
0000: [] nil
|
||||||
|
0000: reset_match
|
||||||
|
0001: [] nil
|
||||||
|
0001: constant 0000: :foo
|
||||||
|
0003: [:10] nil
|
||||||
|
0003: match_constant 0000: :foo
|
||||||
|
0005: [:10] nil
|
||||||
|
0005: jump_if_no_match 0006
|
||||||
|
0007: [:10] nil
|
||||||
|
0007: constant 0001: 1
|
||||||
|
0009: [:10|1] nil
|
||||||
|
0009: store
|
||||||
|
0010: [:10|nil] 1
|
||||||
|
0010: pop
|
||||||
|
0011: [:10] 1
|
||||||
|
0011: jump 0023
|
||||||
|
0036: [:10] 1
|
||||||
|
0036: panic_if_no_match
|
||||||
|
0037: [:10] 1 <== Should "return" from match here
|
||||||
|
0037: push_binding 0000
|
||||||
|
0039: [:10|:10] 1
|
||||||
|
0039: store
|
||||||
|
0040: [:10|nil] :10
|
||||||
|
0040: pop
|
||||||
|
0041: [:10] :10
|
||||||
|
0041: pop
|
||||||
|
0042: [] :10
|
||||||
|
0042: load
|
||||||
|
:foo
|
||||||
|
```
|
||||||
|
Should return `1`.
|
||||||
|
|
||||||
|
Instruction `0037` is where it goes off the rails.
|
||||||
|
|
||||||
|
### Things left undone
|
||||||
|
Many. But things that were actively under development and in a state of unfinishedness:
|
||||||
|
|
||||||
|
1. Tuple patterns
|
||||||
|
2. Loops
|
||||||
|
3. Function calls
|
||||||
|
|
||||||
|
#### Tuple patterns
|
||||||
|
This is the blocking issue for loops, function calls, etc.
|
||||||
|
You need tuple pattern matching to get proper looping and function calls.
|
||||||
|
Here are some of the issues I'm having:
|
||||||
|
* Is it possible to represent tuples on the stack? Right now they're allocated on the heap, which isn't great for function calls.
|
||||||
|
* How to represent complex patterns? There are a few possibilities:
|
||||||
|
- Hard-coded into the bytecode (this is probably the thing to do?)
|
||||||
|
- Represented as a data structure, which itself would have to be allocated on the heap
|
||||||
|
- Some hybrid of the two:
|
||||||
|
* Easy scalar values are hard-coded: `nil`, `true`, `:foo`, `10` are all built into the bytecode + constants table
|
||||||
|
* Perhaps dict patterns will have to be data structures
|
||||||
|
|
||||||
|
#### Patterns, generally
|
||||||
|
On reflection, I think the easiest (perhaps not simplest!) way to go is to model the patterns as separate datatypes stored per-chunk in a vec.
|
||||||
|
The idea is that we push a value onto the stack, and then have a `match` instruction that takes an index into the pattern vec.
|
||||||
|
We don't even need to store all pattern types in that vec: constants (which already get stored in the constant vec), interpolations, and compound patterns.
|
||||||
|
`nil`, `false`, etc. are singletons and can be handled like (but not exactly as) the `nil` and `false` _values_.
|
||||||
|
This also means we can outsource the pattern matching mechanics to Rust, which means we don't have to fuss with "efficient compiling of pattern matching" titchiness.
|
||||||
|
This also has the benefit, while probably being fast _enough_, of reflecting the conceptual domain of Ludus, in which patterns and values are different DSLs within the language.
|
||||||
|
So: model it that way.
|
||||||
|
|
||||||
|
### Now that we've got tuple patterns
|
||||||
|
#### May 23, 2025
|
||||||
|
A few thoughts:
|
||||||
|
* Patterns aren't _things_, they're complex conditional forms. I had not really learned that; note that the "compiling pattern matching efficiently" paper is about how to optimize those conditionals. The tuple pattern compilation more closely resembles an `if` or `when` form.
|
||||||
|
* Tuple patterns break the nice stack-based semantics of binding. So do other patterns. That means I had to separate out bindings and the stack. I did this by introducing a representation of the stack into the compiler (it's just a stack-depth counter).
|
||||||
|
- This ended up being rather titchy. I think there's a lot of room to simplify things by avoiding manipulating this counter directly. My sense is that I should probably move a lot of the `emit_op` calls into methods that ensure all the bookkeeping happens automatically.
|
||||||
|
* `when` is much closer to `if` than `match`; remember that!
|
||||||
|
* Function calls should be different from tuple pattern matching. Tuples are currently (and maybe forever?) allocated on the heap. Function calls should *not* have to pass through the heap. The good news: `Arguments` is already a different AST node type than `Tuple`; we'll want an `ArgumentsPattern` pattern node type that's different from (and thus compiled differently than) `TuplePattern`. They'll be similar--the matching logic is the same, after all--but the arguments will be on the stack already, and won't need to be unpacked in the same way.
|
||||||
|
- One difficulty will be matching against different arities? But actually, we should compile these arities as different functions.
|
||||||
|
- Given splats, can we actually compile functions into different arities? Consider the following:
|
||||||
|
```
|
||||||
|
fn foo {
|
||||||
|
(x) -> & arity 1
|
||||||
|
(y, z) -> & arity 2
|
||||||
|
(x, y, 2) -> & arity 3
|
||||||
|
(...z) -> & arity 0+
|
||||||
|
}
|
||||||
|
```
|
||||||
|
`fn(1, 2, 3)` and `fn(1, 2, 4)` would invoke different "arities" of the function, 3 and 0+, respectively. I suspect the simpler thing really is to just compile each function as a singleton, and just keep track of the number of arguments you're matching.
|
||||||
|
* Before we get to function calls, `loop`/`recur` make sense as the starting ground. I had started that before, and there's some code in those branches of the compiler. But I ran into tuple pattern matching. That's now done, although actually, the `loop`/`recur` situation probably needs a rewrite from the ground up.
|
||||||
|
* Just remember: we're not aiming for *fast*, we're aiming for *fast enough*. And I don't have a ton of time. So the thing to do is to be as little clever as possible.
|
||||||
|
* I suspect the dominoes will fall reasonably quickly from here through the following:
|
||||||
|
- [x] `list` and `dict` patterns
|
||||||
|
- [x] updating `repeat`
|
||||||
|
- [x] standing up `loop`/`recur`
|
||||||
|
- [x] standing up functions
|
||||||
|
- [x] more complex synthetic expressions
|
||||||
|
- [x] `do` expressions
|
||||||
|
* That will get me a lot of the way there. What's left after that which might be challenging?
|
||||||
|
- [x] string interpolation
|
||||||
|
- [x] splats
|
||||||
|
- [ ] splatterns
|
||||||
|
- [x] string patterns
|
||||||
|
- [x] partial application
|
||||||
|
- [ ] tail calls
|
||||||
|
- [ ] stack traces in panics
|
||||||
|
- [ ] actually good lexing, parsing, and validation errors. I got some of the way there in the fall, but everything needs to be "good enough."
|
||||||
|
* After that, we're in integration hell: taking this thing and putting it together for Computer Class 1. Other things that I want (e.g., `test` forms) are for later on.
|
||||||
|
* There's then a whole host of things I'll need to get done for CC2:
|
||||||
|
- some kind of actual parsing strategy (that's good enough for "Dissociated Press"/Markov chains)
|
||||||
|
- actors
|
||||||
|
- animation in the frontend
|
||||||
|
* In addition to a lot of this, I think I need some kind of testing solution. The Janet interpreter is pretty well-behaved.
|
||||||
|
|
||||||
|
### Now that we've got some additional opcodes and loop/recur working
|
||||||
|
#### 2025-05-27
|
||||||
|
The `loop` compilation is _almost_ the same as a function body. That said, the thing that's different is that we don't know the arity of the function that's called.
|
||||||
|
|
||||||
|
A few possibilities:
|
||||||
|
* Probably the best option: enforce a new requirement that splat patterns in function clauses *must* be longer than any explicit arity of the function. So, taking the above:
|
||||||
|
```
|
||||||
|
fn foo {
|
||||||
|
(x) -> & arity 1
|
||||||
|
(y, z) -> & arity 2
|
||||||
|
(x, y, 2) -> & arity 3
|
||||||
|
(...z) -> & arity 0+
|
||||||
|
}
|
||||||
|
```
|
||||||
|
This would give you a validation error that splats must be longer than any other arity.
|
||||||
|
Similarly, we could enforce this:
|
||||||
|
```
|
||||||
|
fn foo {
|
||||||
|
(x) -> & arity 1
|
||||||
|
(x, y) -> & arity 2
|
||||||
|
(x, ...) & arity n > 1; error! too short
|
||||||
|
(x, y, ...) & arity n > 2; ok!
|
||||||
|
}
|
||||||
|
```
|
||||||
|
The algorithm for compiling functions ends up being a little bit titchy, because we'll have to store multiple functions (i.e. chunks) with different arities.
|
||||||
|
Each arity gets a different chunk.
|
||||||
|
And the call function opcode comes with a second argument that specifies the number of arguments, 0 to 7.
|
||||||
|
(That means we only get a max of 7 arguments, unless I decide to give the call opcode two bytes, and make the call/return register much bigger.)
|
||||||
|
|
||||||
|
Todo, then:
|
||||||
|
* [x] reduce the call/return register to 7
|
||||||
|
* [x] implement single-arity compilation
|
||||||
|
* [x] implement single-arity calls, but with two bytes
|
||||||
|
* [x] compile multiple-arity functions
|
||||||
|
* [x] add closures
|
||||||
|
|
||||||
|
### Some thoughts while chattin w/ MNL
|
||||||
|
On `or` and `and`: these should be reserved words with special casing in the parser.
|
||||||
|
You can't pass them as functions, because that would change their semantics.
|
||||||
|
So they *look* like functions, but they don't *behave* like functions.
|
||||||
|
In Clojure, if you try `(def foo or)`, you get an error that you "can't take the value of a macro."
|
||||||
|
I'll need to change Ludus so that `or` and `and` expressions actually generate different AST nodes, and then compile them from there.
|
||||||
|
|
||||||
|
AND: done.
|
||||||
|
|
||||||
|
### Implementing functions & calls, finally
|
||||||
|
#### 2025-06-01
|
||||||
|
Just to explain where I am to myself:
|
||||||
|
* I have a rough draft (probably not yet fully functional) of function compilation in the compiler.
|
||||||
|
* Now I have to implement two op codes: `Call` and `Return`.
|
||||||
|
* I now need to implement call frames.
|
||||||
|
* A frame in _Crafting Interpreters_ has: a pointer to a Lox function object, an ip, and an index into the value stack that indicates the stack bottom for this function call. Taking each of these in turn:
|
||||||
|
* The lifetime for the pointer to the function:
|
||||||
|
- The pointer to the function object cannot, I think, be an explicit lifetime reference, since I don't think I know how to prove to the Rust borrow checker that the function will live long enough, especially since it's inside an `Rc`.
|
||||||
|
- That suggests that I actually need the whole `Value::Fn` struct and not just the inner `LFn` struct, so I can borrow it.
|
||||||
|
* The ip and stack bottom are just placeholders and don't change.
|
||||||
|
|
||||||
|
### Partially applied functions
|
||||||
|
#### 2025-06-05
|
||||||
|
Partially applied functions are a little complicated, because they involve both the compiler and the VM.
|
||||||
|
Maybe.
|
||||||
|
My sense is that, perhaps, the way to do them is actually to create a different value branch.
|
||||||
|
They ....
|
||||||
|
|
||||||
|
### Jumping! Numbers! And giving up the grind
|
||||||
|
#### 2025-06-05
|
||||||
|
Ok! So.
|
||||||
|
This won't be ready for next week.
|
||||||
|
That's clear enough now--even though I've made swell progress!
|
||||||
|
One thing I just discovered, which, well, it feels silly I haven't found this before.
|
||||||
|
Jump instructions, all of them, need 16 bits, not 8.
|
||||||
|
|
||||||
|
That means some fancy bit shifting, and likely some refactoring of the compiler & vm to make them easier to work with.
|
||||||
|
|
||||||
|
For reference, here's the algorithm for munging u8s and u16s:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
let a: u16 = 14261;
|
||||||
|
let b_high: u8 = (a >> 8) as u8;
|
||||||
|
let b_low: u8 = a as u8;
|
||||||
|
let c: u16 = ((b_high as u16) << 8) + b_low as u16;
|
||||||
|
println!("{a} // {b_high}/{b_low} // {c}");
|
||||||
|
```
|
||||||
|
|
||||||
|
To reiterate the punch list that *I would have needed for Computer Class 1*:
|
||||||
|
* [x] jump instructions need 16 bits of operand
|
||||||
|
- Whew, that took longer than I expected
|
||||||
|
* [x] splatterns
|
||||||
|
- [ ] validator should ensure splatterns are the longest patterns in a form
|
||||||
|
* [ ] improve validator
|
||||||
|
- [ ] Tuples may not be longer than n members
|
||||||
|
- [ ] Loops may not have splatterns
|
||||||
|
- [ ] Identify others
|
||||||
|
* [x] add guards to loop forms
|
||||||
|
* [x] check loop forms against function calls: do they still work the way we want them to?
|
||||||
|
* [x] tail call elimination
|
||||||
|
* [x] stack traces in panics
|
||||||
|
|
||||||
|
* [ ] actually good error messages
|
||||||
|
- [ ] parsing
|
||||||
|
- [ ] my memory is that validator messages are already good?
|
||||||
|
- [ ] panics, esp. no match panics
|
||||||
|
* [ ] getting to prelude
|
||||||
|
- [ ] `base` should load into Prelude
|
||||||
|
- [ ] prelude should run properly
|
||||||
|
- [ ] prelude should be loaded into every context
|
||||||
|
* [ ] packaging things up
|
||||||
|
- [ ] add a `to_json` method for values
|
||||||
|
- [ ] teach Rudus to speak our protocols (stdout and turtle graphics)
|
||||||
|
- [ ] there should be a Rust function that takes Ludus source and returns valid Ludus status json
|
||||||
|
- [ ] compile Rust to WASM
|
||||||
|
- [ ] wire Rust-based WASM into JS
|
||||||
|
- [ ] FINALLY, test Rudus against Ludus test cases
|
||||||
|
|
||||||
|
So this is the work of the week of June 16, maybe?
|
||||||
|
|
||||||
|
Just trying to get a sense of what needs to happen for CC2:
|
||||||
|
* [ ] Actor model (objects, Spacewar!)
|
||||||
|
* [ ] Animation hooked into the web frontend (Spacewar!)
|
||||||
|
* [ ] Text input (Spacewar!)
|
||||||
|
- [ ] Makey makey for alternate input?
|
||||||
|
* [ ] Saving and loading data into Ludus (perceptrons, dissociated press)
|
||||||
|
* [ ] Finding corpuses for Dissociated Press
|
||||||
|
|
||||||
|
### Final touches on semantics, or lots of bugs
|
||||||
|
#### 2025-06-19
|
||||||
|
* Main code is fucking up bindings in functions
|
||||||
|
* Why?
|
||||||
|
* In the middle of doing TCO, looks like it works for `do` forms based on the bytecode, but I ran into these weird binding problems while trying to test that in the vm
|
||||||
|
* Once that's put to bed, I believe TCO fully works
|
||||||
|
|
||||||
|
_Edited to add: all the above is, I think, fixed._
|
||||||
|
|
||||||
|
* But then I need to test it actually works in its intended use case: recursive calls
|
||||||
|
* Which means I need to test recursive calls
|
||||||
|
* And, once that's done, I think I have a COMPLETE SEMANTICALLY CORRECT INTERPRETER.
|
||||||
|
* After that, jesus, it's time for base > prelude > test cases
|
||||||
|
|
||||||
|
#### Later
|
||||||
|
So this is my near-term TODO:
|
||||||
|
|
||||||
|
* [x] recursive calls
|
||||||
|
- [x] direct recursive calls
|
||||||
|
* [x] stash a function declaration before compiling the function, hang onto that declaration
|
||||||
|
* [x] update that declaration to a definition after compiling the function
|
||||||
|
- [x] mutual recursive
|
||||||
|
* [x] check to make sure a function has already been declared, hang onto that declaration
|
||||||
|
* [x] update that declaration to a definition after compiling the function...
|
||||||
|
* [x] but BEFORE we close over any upvalues, so the function will be an upvalue for itself
|
||||||
|
- I suspect this can be done not using anything other than an index into a chunk's `constants` vec--no fancy memory swapping or anything; and also--done in the compiler rather than the VM.
|
||||||
|
* [x] getting to prelude
|
||||||
|
- [x] `base` should load into Prelude
|
||||||
|
- [x] write a mock prelude with a few key functions from real prelude
|
||||||
|
- [x] a prelude should be loaded into every context
|
||||||
|
- [ ] the full prelude should run properly
|
||||||
|
* [ ] packaging things up
|
||||||
|
- [ ] add a `to_json` method for values
|
||||||
|
- [ ] teach Rudus to speak our protocols (stdout and turtle graphics)
|
||||||
|
- [ ] there should be a Rust function that takes Ludus source and returns valid Ludus status json
|
||||||
|
- [ ] compile Rust to WASM
|
||||||
|
- [ ] wire Rust-based WASM into JS
|
||||||
|
- [ ] FINALLY, test Rudus against Ludus test cases
|
||||||
|
|
||||||
|
And then: quality of life improvements:
|
||||||
|
* [ ] refactor messes
|
||||||
|
- [ ] The compiler should abstract over some of the very titchy bytecode instruction code
|
||||||
|
- [ ] Pull apart some gargantuan modules into smaller chunks: e.g., `Op` and `Chunk` should be their own modules
|
||||||
|
- [ ] Identify code smells
|
||||||
|
- [ ] Fix some of them
|
||||||
|
* [ ] improve validator
|
||||||
|
- [ ] Tuples may not be longer than n members
|
||||||
|
- [ ] Loops may not have splatterns
|
||||||
|
- [ ] Identify others
|
||||||
|
- [ ] Splats in functions must be the same arity, and greater than any explicit arity
|
||||||
|
* [ ] actually good error messages
|
||||||
|
- [ ] parsing
|
||||||
|
- [ ] my memory is that validator messages are already good?
|
||||||
|
- [ ] panics, esp. no match panics
|
||||||
|
* [ ] panics should be able to refernce the line number where they fail
|
||||||
|
* [ ] that suggests that we need a mapping from bytecodes to AST nodes
|
||||||
|
* The way I had been planning on doing this is having a vec that moves in lockstep with bytecode that's just references to ast nodes, which are `'static`, so that shouldn't be too bad. But this is per-chunk, which means we need a reference to that vec in the VM. My sense is that what we want is actually a separate data structure that holds the AST nodes--we'll only need them in the sad path, which can be slow.
|
||||||
|
|
||||||
|
### Bugs discovered while trying to compile prelude
|
||||||
|
#### 2025-06-20
|
||||||
|
Consider the following code:
|
||||||
|
```
|
||||||
|
fn one {
|
||||||
|
(x as :number) -> {
|
||||||
|
fn two () -> :number
|
||||||
|
two
|
||||||
|
}
|
||||||
|
(x as :bool) -> {
|
||||||
|
fn two () -> :bool
|
||||||
|
two
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
The second clause causes a panic in the compiler.
|
||||||
|
I'm not entirely sure what's going on.
|
||||||
|
That said, I'm pretty sure the root of it is that the fact that `two` was already bound in the first clause means that it's in the constants vector in that chunk under the name "two."
|
||||||
|
|
||||||
|
...
|
||||||
|
|
||||||
|
So that's fixed!
|
||||||
|
|
||||||
|
But:
|
||||||
|
Prelude functions are not properly closing over their upvalues.
|
||||||
|
Which is not right.
|
||||||
|
We get: `get_upvalue 000` and a panic: `index out of bounds: the len is 0 but the index is 0`.
|
||||||
|
The vec in question is the LFn::Defined.closed.
|
||||||
|
|
||||||
|
...
|
||||||
|
|
||||||
|
Just patched up the `if` alternative branch unconditional jump, which was jumping too far.
|
||||||
|
|
||||||
|
Now it really is just some pretty systematic testing of prelude functions, including the problems with upvalues, which I haven't yet been able to recreate.
|
||||||
|
|
||||||
|
### On proceeding from here
|
||||||
|
#### 2025-06-021
|
||||||
|
Rather than doing everything by hand right now, I think the way to go about things is to figure out how to do as much automated bugfixing as possible.
|
||||||
|
That means reprioritizing some things.
|
||||||
|
So here's a short punch list of things to do in that register:
|
||||||
|
* [x] Hook validator back in to both source AND prelude code
|
||||||
|
- [x] Validator should know about the environment for global/prelude function
|
||||||
|
- [x] Run validator on current prelude to fix current known errors
|
||||||
|
* [ ] Do what it takes to compile this interpreter into Ludus's JS environment
|
||||||
|
- [ ] JSONify Ludus values
|
||||||
|
- [ ] Write a function that's source code to JSON result
|
||||||
|
- [ ] Expose this to a WASM compiler
|
||||||
|
- [ ] Patch this into a JS file
|
||||||
|
- [ ] Automate this build process
|
||||||
|
* [ ] Start testing against the cases in `ludus-test`
|
||||||
|
* [ ] Systematically debug prelude
|
||||||
|
- [ ] Bring it in function by function, testing each in turn
|
||||||
|
|
||||||
|
***
|
||||||
|
I've started working on systematically going through the Prelude.
|
||||||
|
I've found a closure error.
|
||||||
|
This is in `map`/`mapping`.
|
||||||
|
What's happening is that the inner function, `mapping`, is closing over values directly from a stack that no longer exists.
|
||||||
|
What we need to have happen is that if a function is closing over values _inside_ a function, it needs to capture not the upvalues directly from the stack, but _from the enclosing closure_.
|
||||||
|
I think I need to consult Uncle Bob Nystrom to get a sense of what to do here.
|
||||||
|
***
|
||||||
|
So I found the minimal test case:
|
||||||
|
```
|
||||||
|
let foo = {
|
||||||
|
let thing = :thing
|
||||||
|
let bar = :bar
|
||||||
|
let baz = :baz
|
||||||
|
fn quux () -> {
|
||||||
|
fn frobulate () -> (bar, baz)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foo ()
|
||||||
|
```
|
||||||
|
`frobulate` is closed over when `quux` is called, when the stack looks completely different than it does when `quux` is defined.
|
||||||
|
If you remove line 2, binding `thing`, then you don't get a panic, but when you call `foo () ()`, the result is `(fn quux, fn frobulate)`.
|
||||||
|
The problem is that `frobulate`'s upvalues are indexes into the stack, rather than having some relation to `quux`'s upvalues.
|
||||||
|
What needs to happen is that an enclosing function needs to capture, define, and pass down the upvalues for its enclosed functions.
|
||||||
|
|
||||||
|
I'm having an exact problem that Uncle Bob is describing at
|
||||||
|
https://craftinginterpreters.com/closures.html#flattening-upvalues.
|
||||||
|
I need to study and adapt this exact set of problems.
|
||||||
|
I believe I need to take the strategy he uses with closures being different from functions, etc.
|
||||||
|
So: rework the closures strategy here.
|
||||||
|
|
||||||
|
***
|
||||||
|
|
||||||
|
Closures strategy mostly unfucked.
|
||||||
|
Now I'm having some difficulty with bindings, again?
|
||||||
|
Current situation: still trying to get `map` and `fold` to work properly.
|
||||||
|
The bindings inside of non-trivial functions goes weird.
|
||||||
|
The scope depths are all out of whack.
|
||||||
|
And the stack positions on stuff are also totally weird.
|
||||||
|
One thing: the way we are now resolving upvalues means that nothing should ever reach back further in the stack than the stack base in the vm.
|
||||||
|
So now we'll do all bindings relative to the stack base.
|
||||||
|
UGH.
|
||||||
|
|
||||||
|
***
|
||||||
|
|
||||||
|
So now I have a bug that I'm dying to figure out.
|
||||||
|
But it's time to go to bed.
|
||||||
|
|
||||||
|
When `recur` is in the alternatve branch of an if, it thinks there's one more value on the stack than there really is.
|
||||||
|
To the best of my ability to tell, `if` has proper stack behaviour.
|
||||||
|
So the question is what's happening in the interaction between the `jump_if_false` instruction and `recur`.
|
||||||
|
|
||||||
|
To wit, the following code works just fine:
|
||||||
|
```
|
||||||
|
fn not {
|
||||||
|
(false) -> true
|
||||||
|
(nil) -> true
|
||||||
|
(_) -> false
|
||||||
|
}
|
||||||
|
|
||||||
|
let test = 2
|
||||||
|
loop ([1, 2, 3]) with {
|
||||||
|
([]) -> false
|
||||||
|
([x]) -> eq? (x, test)
|
||||||
|
([x, ...xs]) -> if not(eq? (x, test))
|
||||||
|
then recur (xs)
|
||||||
|
else true
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
But the following code does not:
|
||||||
|
```
|
||||||
|
let test = 2
|
||||||
|
loop ([1, 2, 3]) with {
|
||||||
|
([]) -> false
|
||||||
|
([x]) -> eq? (x, test)
|
||||||
|
([x, ...xs]) -> if eq? (x, test)
|
||||||
|
then true
|
||||||
|
else recur (xs)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Meanwhile, other `loop`/`recur` forms seem to work okay for me.
|
||||||
|
So: ugh.
|
||||||
|
|
||||||
|
### Grinding and grinding
|
||||||
|
#### 2025-06-22
|
||||||
|
Got 'er done.
|
||||||
|
Fixed `loop`/`recur` and many other stack shananigans.
|
||||||
|
I don't believe I've fixed everything yet.
|
||||||
|
I may be surprised, though.
|
||||||
|
|
||||||
|
Currently fixing little bugs in prelude.
|
||||||
|
Here's a list of things that need doing:
|
||||||
|
* [ ] Escape characters in strings: \n, \t, and \{, \}.
|
||||||
|
* [ ] `doc!` needs to print the patterns of a function.
|
||||||
|
* [ ] I need to return to the question of whether/how strings are ordered; do we use `at`, or do we want `char_at`? etc.
|
||||||
|
* [ ] Original implementation of `butlast` is breaking stack discipline; I don't know why. It ends up returning from evaluating one of the arguments straight into a `load` instruction. Something about tail calls and ternary synthetic expressions and base functions. (For now, I can call `slice` instead of `base :slice` and it works.)
|
||||||
|
- Original version of `update` also had this same problem with `assoc`; fixed it by calling the Ludus, rather than Rust, function.
|
||||||
|
- I need this fixed for optimization reasons.
|
||||||
|
- I _think_ I just fixed this by fixing tail position tracking in collections
|
||||||
|
- [ ] test this
|
||||||
|
- I did not fix it.
|
||||||
|
* [x] Dict patterns are giving me stack discipline grief. Why is stack discipline so hard?
|
||||||
|
* [x] This is in the service of getting turtle graphics working
|
||||||
|
* Other forms in the language need help:
|
||||||
|
* [x] repeat needs its stack discipline updated, it currently crashes the compiler
|
||||||
|
|
||||||
|
### More closure problems
|
||||||
|
#### 2025-06-23
|
||||||
|
My solution to closures wasn't quite right.
|
||||||
|
I can't use Uncle Bob's strategy of the recursive call, since Rust's ownership semantics make this onerous at best.
|
||||||
|
My solution: introduce the concept of a "compiler depth," with 0 being the global scope.
|
||||||
|
If the compiler's at 0 depth, we can pull it out of the environment.
|
||||||
|
If the compiler's at a depth > 0, then we can ask the enclosing compiler to stash the upvalue.
|
||||||
|
And thus we get what we need.
|
||||||
|
|
||||||
|
But: some functions in prelude aren't properly getting their closures, and I don't know why, since they *are* getting them properly in user scripts.
|
||||||
|
Take `apply_command`.
|
||||||
|
|
||||||
|
Next step: sort out if any other functions aren't getting things closed over properly.
|
||||||
|
|
||||||
|
PROBLEM: forward-declared functions weren't at the top of the stack when `Op::SetUpvalue` was called.
|
||||||
|
So all of `apply_command`'s upvalues were being attached to the function declared before it (which was sitting right there at the top of the stack.)
|
||||||
|
|
||||||
|
SOLUTION: test to see if the function has been forward-declared, and if it has, bring it to the top fo the stack.
|
||||||
|
|
||||||
|
NEW PROBLEM: a lot of instructions in the VM don't properly offset from the call frame's stack base, which leads to weirdness when doing things inside function calls.
|
||||||
|
|
||||||
|
NEW SOLUTION: create a function that does the offset properly, and replace everywhere we directly access the stack.
|
||||||
|
This is the thing I am about to do.
|
||||||
|
|
||||||
|
### I think the interpreter, uh, works?
|
||||||
|
#### 2025-06-24
|
||||||
|
I'm sure I'll find some small problems.
|
||||||
|
But right now the thing works.
|
||||||
|
At the moment, I'm thinking about how to get good error messages.
|
||||||
|
Panics are difficult.
|
||||||
|
And I'm worried about ariadne as the error reporting crate.
|
||||||
|
Since it writes to stdout, it has all kinds of escape codes.
|
||||||
|
I need a plain ass string, at least for the web frontend.
|
||||||
|
So.
|
||||||
|
|
||||||
|
Current task, however, is how to get reasonable panic error messages.
|
||||||
|
Let's simplify the problem.
|
||||||
|
|
||||||
|
First, let's get tracebacks and line numbers.
|
||||||
|
We use Chumsky spanned ASTs.
|
||||||
|
The span is just a range into an str.
|
||||||
|
What I can do is pretty stupidly just generate line numbers from the spans in the compiler, and from there, get a reasonable traceback.
|
||||||
|
So instead of using Ariadne's fancy report builder, let's just do something more what we already have here:
|
||||||
|
|
||||||
|
```
|
||||||
|
Ludus panicked! no match
|
||||||
|
on line 1 in input,
|
||||||
|
calling: add
|
||||||
|
with arguments: ("foo")
|
||||||
|
expected match with one of:
|
||||||
|
()
|
||||||
|
(x as :number)
|
||||||
|
(x as :number, y as :number)
|
||||||
|
(x, y, ...zs)
|
||||||
|
((x1, y1), (x2, y2))
|
||||||
|
>>> add ("foo")
|
||||||
|
......^
|
||||||
|
```
|
||||||
|
We need:
|
||||||
|
* the location of the function call in terms of the line number
|
||||||
|
* the arguments used
|
||||||
|
* the patterns expected to match (special cases: `let` vs `match` vs `fn`)
|
||||||
|
|
||||||
|
That means, for bookkeeping, we need:
|
||||||
|
* In the compiler, line number
|
||||||
|
* In the VM, the arguments
|
||||||
|
* In the VM, the pattern AST node.
|
||||||
|
|
||||||
|
In Janet-Ludus, there are only a few types of panic:
|
||||||
|
* `fn-no-match`: no match against a function call
|
||||||
|
* `let-no-match`: no match against a `let` binding
|
||||||
|
* `match-no-match`: no match against a `match` form
|
||||||
|
* `generic-panic`: everything else
|
||||||
|
* `runtime-error`: an internal Ludus error
|
||||||
|
|
||||||
|
The first three are simply formatting differences.
|
||||||
|
There are no tracebacks.
|
||||||
|
|
||||||
|
Tracebacks should be easy enough, although there's some fiddly bits.
|
||||||
|
While it's nice to have the carret, the brutalist attempt here should be just to give us the line--since the carret isn't exactly precise in the Janet interpereter.
|
||||||
|
And the traceback should look something like:
|
||||||
|
|
||||||
|
```
|
||||||
|
calling foo with (:bar, :baz)
|
||||||
|
at line 12 in input
|
||||||
|
calling bar with ()
|
||||||
|
at line 34 in prelude
|
||||||
|
calling baz with (1, 2, 3)
|
||||||
|
at line 12 in input
|
||||||
|
```
|
||||||
|
|
||||||
|
Which means, again: function names, ip->line conversion, and arguments.
|
||||||
|
|
||||||
|
The runtime needs a representation of the patterns in _any_ matching form.
|
||||||
|
The speed is so much greater now that I'm not so concerned about little optimizations.
|
||||||
|
So: a chunk needs a vec of patterns-representations. (I'm thinking simply a `Vec<String>`.)
|
||||||
|
So does a function, for `doc!`.
|
||||||
|
Same same re: `Vec<String>`.
|
||||||
|
A VM needs a register for the scrutinee (which with function calls is just the arguments, already captured).
|
||||||
|
A VM also needs a register for the pattern.
|
||||||
|
So when there's a no match, we just yank the pattern and the scrutinee out of these registers.
|
||||||
|
|
||||||
|
This all seems very straightforward compared to the compiling & VM stuff.
|
||||||
|
|
||||||
|
Here's some stub code I wrote for dealing with ranges, source, line numbers:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
let str = "foo bar baz\nquux frob\nthing thing thing";
|
||||||
|
let range = 0..4;
|
||||||
|
|
||||||
|
println!("{}", str.get(range).unwrap());
|
||||||
|
|
||||||
|
let lines: Vec<&str> = str.split_terminator("\n").collect();
|
||||||
|
|
||||||
|
println!("{:?}", lines);
|
||||||
|
|
||||||
|
let idx = 20;
|
||||||
|
|
||||||
|
let mut line_no = 1;
|
||||||
|
for i in 0..idx {
|
||||||
|
if str.chars().nth(i).unwrap() == '\n' {
|
||||||
|
line_no += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("line {line_no}: {}", lines[line_no - 1]);
|
||||||
|
```
|
||||||
|
|
||||||
|
|
5
sandbox.ld
Normal file
5
sandbox.ld
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
let foo = :foo
|
||||||
|
|
||||||
|
repeat foo {
|
||||||
|
print! ("hi")
|
||||||
|
}
|
709
sandbox_run.txt
Normal file
709
sandbox_run.txt
Normal file
|
@ -0,0 +1,709 @@
|
||||||
|
closing over in type at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in eq? at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in eq? at 2: fn eq?
|
||||||
|
closing over in first at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in rest at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in inc at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in dec at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in count at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in any? at 1: fn empty?
|
||||||
|
closing over in any? at 2: fn not
|
||||||
|
closing over in list at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in append at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in fold at 1: fn fold
|
||||||
|
closing over in fold at 2: fn first
|
||||||
|
closing over in fold at 3: fn rest
|
||||||
|
closing over in foldr at 1: fn foldr
|
||||||
|
closing over in foldr at 2: fn first
|
||||||
|
closing over in foldr at 3: fn rest
|
||||||
|
closing over in map at 1: fn map
|
||||||
|
closing over in map at 2: fn append
|
||||||
|
closing over in map at 3: fn fold
|
||||||
|
closing over in filter at 1: fn filter
|
||||||
|
closing over in filter at 2: fn append
|
||||||
|
closing over in filter at 3: fn fold
|
||||||
|
closing over in keep at 1: fn some?
|
||||||
|
closing over in keep at 2: fn filter
|
||||||
|
closing over in concat at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in concat at 2: fn concat
|
||||||
|
closing over in concat at 3: fn fold
|
||||||
|
closing over in contains? at 1: fn first
|
||||||
|
closing over in contains? at 2: fn eq?
|
||||||
|
closing over in contains? at 3: fn rest
|
||||||
|
closing over in print! at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in show at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in report! at 1: fn print!
|
||||||
|
closing over in report! at 2: fn show
|
||||||
|
closing over in report! at 3: fn concat
|
||||||
|
closing over in doc! at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in doc! at 2: fn print!
|
||||||
|
closing over in string at 1: fn show
|
||||||
|
closing over in string at 2: fn string
|
||||||
|
closing over in string at 3: fn concat
|
||||||
|
closing over in join at 1: fn join
|
||||||
|
closing over in join at 2: fn concat
|
||||||
|
closing over in join at 3: fn fold
|
||||||
|
closing over in split at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in trim at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in upcase at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in downcase at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in chars at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in chars/safe at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in strip at 1: fn strip
|
||||||
|
closing over in words at 1: fn strip
|
||||||
|
closing over in words at 2: fn split
|
||||||
|
closing over in words at 3: fn empty?
|
||||||
|
closing over in words at 4: fn append
|
||||||
|
closing over in words at 5: fn fold
|
||||||
|
closing over in sentence at 1: fn join
|
||||||
|
closing over in to_number at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in unbox at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in store! at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in update! at 1: fn unbox
|
||||||
|
closing over in update! at 2: fn store!
|
||||||
|
closing over in add at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in add at 2: fn add
|
||||||
|
closing over in add at 3: fn fold
|
||||||
|
closing over in sub at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in sub at 2: fn sub
|
||||||
|
closing over in sub at 3: fn fold
|
||||||
|
closing over in mult at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in mult at 2: fn mult
|
||||||
|
closing over in mult at 3: fn fold
|
||||||
|
closing over in div at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in div at 2: fn mult
|
||||||
|
closing over in div at 3: fn fold
|
||||||
|
closing over in div at 4: fn div
|
||||||
|
closing over in div/0 at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in div/0 at 2: fn mult
|
||||||
|
closing over in div/0 at 3: fn fold
|
||||||
|
closing over in div/0 at 4: fn div/0
|
||||||
|
closing over in div/safe at 1: fn div
|
||||||
|
closing over in div/safe at 2: fn mult
|
||||||
|
closing over in div/safe at 3: fn fold
|
||||||
|
closing over in div/safe at 4: fn div/safe
|
||||||
|
closing over in inv at 1: fn div
|
||||||
|
closing over in inv/0 at 1: fn div/0
|
||||||
|
closing over in inv/safe at 1: fn div/safe
|
||||||
|
closing over in neg at 1: fn mult
|
||||||
|
closing over in gt? at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in gte? at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in lt? at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in lte? at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in between? at 1: fn gte?
|
||||||
|
closing over in between? at 2: fn lt?
|
||||||
|
closing over in neg? at 1: fn lt?
|
||||||
|
closing over in pos? at 1: fn gt?
|
||||||
|
closing over in abs at 1: fn neg?
|
||||||
|
closing over in abs at 2: fn mult
|
||||||
|
closing over in turn/deg at 1: fn mult
|
||||||
|
closing over in deg/turn at 1: fn div
|
||||||
|
closing over in turn/rad at 1: 6.283185307179586
|
||||||
|
closing over in turn/rad at 2: fn mult
|
||||||
|
closing over in rad/turn at 1: 6.283185307179586
|
||||||
|
closing over in rad/turn at 2: fn div
|
||||||
|
closing over in deg/rad at 1: 6.283185307179586
|
||||||
|
closing over in deg/rad at 2: fn div
|
||||||
|
closing over in deg/rad at 3: fn mult
|
||||||
|
closing over in rad/deg at 1: 6.283185307179586
|
||||||
|
closing over in rad/deg at 2: fn div
|
||||||
|
closing over in rad/deg at 3: fn mult
|
||||||
|
closing over in sin at 1: fn turn/rad
|
||||||
|
closing over in sin at 2: #{:tan fn tan/base, ...
|
||||||
|
closing over in sin at 3: fn deg/rad
|
||||||
|
closing over in cos at 1: fn turn/rad
|
||||||
|
closing over in cos at 2: #{:tan fn tan/base, ...
|
||||||
|
closing over in cos at 3: fn deg/rad
|
||||||
|
closing over in tan at 1: fn turn/rad
|
||||||
|
closing over in tan at 2: #{:tan fn tan/base, ...
|
||||||
|
closing over in tan at 3: fn deg/rad
|
||||||
|
closing over in rotate at 1: fn rotate
|
||||||
|
closing over in rotate at 2: fn cos
|
||||||
|
closing over in rotate at 3: fn mult
|
||||||
|
closing over in rotate at 4: fn sin
|
||||||
|
closing over in rotate at 5: fn sub
|
||||||
|
closing over in rotate at 6: fn add
|
||||||
|
closing over in atan/2 at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in atan/2 at 2: fn rad/turn
|
||||||
|
closing over in atan/2 at 3: fn atan/2
|
||||||
|
closing over in atan/2 at 4: fn rad/deg
|
||||||
|
closing over in angle at 1: fn atan/2
|
||||||
|
closing over in angle at 2: fn sub
|
||||||
|
closing over in mod at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in mod/0 at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in mod/safe at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in even? at 1: fn mod
|
||||||
|
closing over in even? at 2: fn eq?
|
||||||
|
closing over in odd? at 1: fn mod
|
||||||
|
closing over in odd? at 2: fn eq?
|
||||||
|
closing over in square at 1: fn mult
|
||||||
|
closing over in sqrt at 1: fn neg?
|
||||||
|
closing over in sqrt at 2: fn not
|
||||||
|
closing over in sqrt at 3: #{:tan fn tan/base, ...
|
||||||
|
closing over in sqrt/safe at 1: fn neg?
|
||||||
|
closing over in sqrt/safe at 2: fn not
|
||||||
|
closing over in sqrt/safe at 3: #{:tan fn tan/base, ...
|
||||||
|
closing over in sum_of_squares at 1: fn square
|
||||||
|
closing over in sum_of_squares at 2: fn add
|
||||||
|
closing over in sum_of_squares at 3: fn sum_of_squares
|
||||||
|
closing over in sum_of_squares at 4: fn fold
|
||||||
|
closing over in dist at 1: fn sum_of_squares
|
||||||
|
closing over in dist at 2: fn sqrt
|
||||||
|
closing over in dist at 3: fn dist
|
||||||
|
closing over in heading/vector at 1: fn neg
|
||||||
|
closing over in heading/vector at 2: fn add
|
||||||
|
closing over in heading/vector at 3: fn cos
|
||||||
|
closing over in heading/vector at 4: fn sin
|
||||||
|
closing over in floor at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in ceil at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in round at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in range at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in at at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in second at 1: fn ordered?
|
||||||
|
closing over in second at 2: fn at
|
||||||
|
closing over in last at 1: fn ordered?
|
||||||
|
closing over in last at 2: fn count
|
||||||
|
closing over in last at 3: fn dec
|
||||||
|
closing over in last at 4: fn at
|
||||||
|
closing over in slice at 1: fn slice
|
||||||
|
closing over in slice at 2: fn gte?
|
||||||
|
closing over in slice at 3: fn count
|
||||||
|
closing over in slice at 4: fn gt?
|
||||||
|
closing over in slice at 5: fn neg?
|
||||||
|
closing over in slice at 6: #{:tan fn tan/base, ...
|
||||||
|
closing over in butlast at 1: fn count
|
||||||
|
closing over in butlast at 2: fn dec
|
||||||
|
closing over in butlast at 3: fn slice
|
||||||
|
closing over in assoc at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in dissoc at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in get at 1: fn get
|
||||||
|
closing over in get at 2: #{:tan fn tan/base, ...
|
||||||
|
closing over in update at 1: fn get
|
||||||
|
closing over in update at 2: fn assoc
|
||||||
|
closing over in keys at 1: fn list
|
||||||
|
closing over in keys at 2: fn first
|
||||||
|
closing over in keys at 3: fn map
|
||||||
|
closing over in values at 1: fn list
|
||||||
|
closing over in values at 2: fn second
|
||||||
|
closing over in values at 3: fn map
|
||||||
|
closing over in has? at 1: fn has?
|
||||||
|
closing over in has? at 2: fn get
|
||||||
|
closing over in has? at 3: fn some?
|
||||||
|
closing over in dict at 1: fn assoc
|
||||||
|
closing over in dict at 2: fn fold
|
||||||
|
closing over in dict at 3: fn list
|
||||||
|
closing over in dict at 4: fn dict
|
||||||
|
closing over in each! at 1: fn each!
|
||||||
|
closing over in random at 1: #{:tan fn tan/base, ...
|
||||||
|
closing over in random at 2: fn random
|
||||||
|
closing over in random at 3: fn mult
|
||||||
|
closing over in random at 4: fn sub
|
||||||
|
closing over in random at 5: fn add
|
||||||
|
closing over in random at 6: fn count
|
||||||
|
closing over in random at 7: fn floor
|
||||||
|
closing over in random at 8: fn at
|
||||||
|
closing over in random at 9: fn keys
|
||||||
|
closing over in random at 10: fn get
|
||||||
|
closing over in random_int at 1: fn random
|
||||||
|
closing over in random_int at 2: fn floor
|
||||||
|
closing over in add_command! at 1: box { [] }
|
||||||
|
closing over in add_command! at 2: fn append
|
||||||
|
closing over in add_command! at 3: fn update!
|
||||||
|
closing over in add_command! at 4: box { #{:pencolor :w...
|
||||||
|
closing over in add_command! at 5: fn unbox
|
||||||
|
closing over in add_command! at 6: fn apply_command
|
||||||
|
closing over in add_command! at 7: fn store!
|
||||||
|
closing over in forward! at 1: fn add_command!
|
||||||
|
closing over in back! at 1: fn add_command!
|
||||||
|
closing over in left! at 1: fn add_command!
|
||||||
|
closing over in right! at 1: fn add_command!
|
||||||
|
closing over in penup! at 1: fn add_command!
|
||||||
|
closing over in pendown! at 1: fn add_command!
|
||||||
|
closing over in pencolor! at 1: fn add_command!
|
||||||
|
closing over in penwidth! at 1: fn add_command!
|
||||||
|
closing over in background! at 1: fn add_command!
|
||||||
|
closing over in home! at 1: fn add_command!
|
||||||
|
closing over in clear! at 1: fn add_command!
|
||||||
|
closing over in goto! at 1: fn add_command!
|
||||||
|
closing over in goto! at 2: fn goto!
|
||||||
|
closing over in setheading! at 1: fn add_command!
|
||||||
|
closing over in showturtle! at 1: fn add_command!
|
||||||
|
closing over in hideturtle! at 1: fn add_command!
|
||||||
|
closing over in loadstate! at 1: fn add_command!
|
||||||
|
closing over in apply_command at 1: fn assoc
|
||||||
|
closing over in apply_command at 2: fn add
|
||||||
|
closing over in apply_command at 3: fn update
|
||||||
|
closing over in apply_command at 4: fn sub
|
||||||
|
closing over in apply_command at 5: fn heading/vector
|
||||||
|
closing over in apply_command at 6: fn mult
|
||||||
|
closing over in position at 1: box { #{:pencolor :w...
|
||||||
|
closing over in position at 2: fn unbox
|
||||||
|
closing over in heading at 1: box { #{:pencolor :w...
|
||||||
|
closing over in heading at 2: fn unbox
|
||||||
|
closing over in pendown? at 1: box { #{:pencolor :w...
|
||||||
|
closing over in pendown? at 2: fn unbox
|
||||||
|
closing over in pencolor at 1: box { #{:pencolor :w...
|
||||||
|
closing over in pencolor at 2: fn unbox
|
||||||
|
closing over in penwidth at 1: box { #{:pencolor :w...
|
||||||
|
closing over in penwidth at 2: fn unbox
|
||||||
|
binding `hellos!` in sandbox.ld
|
||||||
|
stack depth: 1; match depth: 0
|
||||||
|
at stack index: 0
|
||||||
|
new locals: hellos!@0//0
|
||||||
|
***function clause matching: : (times)
|
||||||
|
binding `times` in hellos!
|
||||||
|
stack depth: 1; match depth: 0
|
||||||
|
at stack index: 0
|
||||||
|
new locals: times@0//0
|
||||||
|
resolving binding `times` in hellos!
|
||||||
|
locals: times@0//0
|
||||||
|
at locals position 0
|
||||||
|
***calling function print! stack depth: 2
|
||||||
|
resolving binding `print!` in hellos!
|
||||||
|
locals: times@0//0
|
||||||
|
as enclosing upvalue 0
|
||||||
|
***after 1 args stack depth: 4
|
||||||
|
leaving scope 1
|
||||||
|
***leaving block before pop stack depth: 2
|
||||||
|
popping back from 2 to 2
|
||||||
|
=== function chuncktion: hellos!/1 ===
|
||||||
|
IDX | CODE | INFO
|
||||||
|
0000: reset_match
|
||||||
|
0001: ***function clause matching: : (times)
|
||||||
|
0003: match_depth 000
|
||||||
|
0005: match
|
||||||
|
0006: binding `times` in hellos!
|
||||||
|
0008: stack depth: 1; match depth: 0
|
||||||
|
0010: at stack index: 0
|
||||||
|
0012: new locals: times@0//0
|
||||||
|
0014: jump_if_no_match 00003
|
||||||
|
0017: jump 00000
|
||||||
|
0020: jump_if_no_match 00049
|
||||||
|
0023: resolving binding `times` in hellos!
|
||||||
|
locals: times@0//0
|
||||||
|
0025: at locals position 0
|
||||||
|
0027: push_binding 000
|
||||||
|
0029: truncate
|
||||||
|
0030: jump 00001
|
||||||
|
0033: decrement
|
||||||
|
0034: duplicate
|
||||||
|
0035: jump_if_zero 00027
|
||||||
|
0038: ***calling function print! stack depth: 2
|
||||||
|
0040: constant 00000: "hi!"
|
||||||
|
0043: resolving binding `print!` in hellos!
|
||||||
|
locals: times@0//0
|
||||||
|
0045: as enclosing upvalue 0
|
||||||
|
0047: get_upvalue 000
|
||||||
|
0049: ***after 1 args stack depth: 4
|
||||||
|
0051: call 001
|
||||||
|
0053: store
|
||||||
|
0054: leaving scope 1
|
||||||
|
0056: ***leaving block before pop stack depth: 2
|
||||||
|
0058: popping back from 2 to 2
|
||||||
|
0060: load
|
||||||
|
0061: pop
|
||||||
|
0062: jump_back 00029
|
||||||
|
0065: pop
|
||||||
|
0066: constant 00001: nil
|
||||||
|
0069: store
|
||||||
|
0070: pop
|
||||||
|
0071: return
|
||||||
|
0072: panic_no_match
|
||||||
|
resolving binding `print!` in sandbox.ld
|
||||||
|
locals: hellos!@0//0
|
||||||
|
as global
|
||||||
|
***calling function hellos! stack depth: 1
|
||||||
|
resolving binding `hellos!` in sandbox.ld
|
||||||
|
locals: hellos!@0//0
|
||||||
|
at locals position 0
|
||||||
|
***after 1 args stack depth: 3
|
||||||
|
leaving scope 0
|
||||||
|
releasing binding hellos!@0//0
|
||||||
|
***leaving block before pop stack depth: 1
|
||||||
|
popping back from 1 to 0
|
||||||
|
=== source code ===
|
||||||
|
fn hellos! (times) -> repeat times {
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
hellos! (:foo)
|
||||||
|
|
||||||
|
=== chunk: sandbox.ld ===
|
||||||
|
IDX | CODE | INFO
|
||||||
|
0000: constant 00000: fn hellos!
|
||||||
|
0003: binding `hellos!` in sandbox.ld
|
||||||
|
0005: stack depth: 1; match depth: 0
|
||||||
|
0007: at stack index: 0
|
||||||
|
0009: new locals: hellos!@0//0
|
||||||
|
0011: resolving binding `print!` in sandbox.ld
|
||||||
|
locals: hellos!@0//0
|
||||||
|
0013: as global
|
||||||
|
0015: constant 00001: :print!
|
||||||
|
0018: push_global
|
||||||
|
0019: set_upvalue
|
||||||
|
0020: ***calling function hellos! stack depth: 1
|
||||||
|
0022: constant 00002: :foo
|
||||||
|
0025: resolving binding `hellos!` in sandbox.ld
|
||||||
|
locals: hellos!@0//0
|
||||||
|
0027: at locals position 0
|
||||||
|
0029: push_binding 000
|
||||||
|
0031: ***after 1 args stack depth: 3
|
||||||
|
0033: call 001
|
||||||
|
0035: store
|
||||||
|
0036: leaving scope 0
|
||||||
|
0038: releasing binding hellos!@0//0
|
||||||
|
0040: ***leaving block before pop stack depth: 1
|
||||||
|
0042: popping back from 1 to 0
|
||||||
|
0044: pop
|
||||||
|
0045: load
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
=== vm run ===
|
||||||
|
0000: [] (_,_,_,_,_,_,_,_)
|
||||||
|
0000: constant 00000: fn hellos!
|
||||||
|
0003: [->fn hellos!<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0003: binding `hellos!` in sandbox.ld
|
||||||
|
0005: [->fn hellos!<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0005: stack depth: 1; match depth: 0
|
||||||
|
0007: [->fn hellos!<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0007: at stack index: 0
|
||||||
|
0009: [->fn hellos!<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0009: new locals: hellos!@0//0
|
||||||
|
0011: [->fn hellos!<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0011: resolving binding `print!` in sandbox.ld
|
||||||
|
locals: hellos!@0//0
|
||||||
|
0013: [->fn hellos!<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0013: as global
|
||||||
|
0015: [->fn hellos!<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0015: constant 00001: :print!
|
||||||
|
0018: [->fn hellos!<-|:print!] (_,_,_,_,_,_,_,_)
|
||||||
|
0018: push_global
|
||||||
|
0019: [->fn hellos!<-|fn print!] (_,_,_,_,_,_,_,_)
|
||||||
|
0019: set_upvalue
|
||||||
|
closing over in hellos! at 1: fn print!
|
||||||
|
0020: [->fn hellos!<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0020: ***calling function hellos! stack depth: 1
|
||||||
|
0022: [->fn hellos!<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0022: constant 00002: :foo
|
||||||
|
0025: [->fn hellos!<-|:foo] (_,_,_,_,_,_,_,_)
|
||||||
|
0025: resolving binding `hellos!` in sandbox.ld
|
||||||
|
locals: hellos!@0//0
|
||||||
|
0027: [->fn hellos!<-|:foo] (_,_,_,_,_,_,_,_)
|
||||||
|
0027: at locals position 0
|
||||||
|
0029: [->fn hellos!<-|:foo] (_,_,_,_,_,_,_,_)
|
||||||
|
0029: push_binding 000
|
||||||
|
0031: [->fn hellos!<-|:foo|fn hellos!] (_,_,_,_,_,_,_,_)
|
||||||
|
0031: ***after 1 args stack depth: 3
|
||||||
|
0033: [->fn hellos!<-|:foo|fn hellos!] (_,_,_,_,_,_,_,_)
|
||||||
|
0033: call 001
|
||||||
|
=== calling into fn hellos!/1 ===
|
||||||
|
0000: [fn hellos!|->:foo<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0000: reset_match
|
||||||
|
0001: [fn hellos!|->:foo<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0001: ***function clause matching: : (times)
|
||||||
|
0003: [fn hellos!|->:foo<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0003: match_depth 000
|
||||||
|
0005: [fn hellos!|->:foo<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0005: match
|
||||||
|
0006: [fn hellos!|->:foo<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0006: binding `times` in hellos!
|
||||||
|
0008: [fn hellos!|->:foo<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0008: stack depth: 1; match depth: 0
|
||||||
|
0010: [fn hellos!|->:foo<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0010: at stack index: 0
|
||||||
|
0012: [fn hellos!|->:foo<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0012: new locals: times@0//0
|
||||||
|
0014: [fn hellos!|->:foo<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0014: jump_if_no_match 00003
|
||||||
|
0017: [fn hellos!|->:foo<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0017: jump 00000
|
||||||
|
0020: [fn hellos!|->:foo<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0020: jump_if_no_match 00049
|
||||||
|
0023: [fn hellos!|->:foo<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0023: resolving binding `times` in hellos!
|
||||||
|
locals: times@0//0
|
||||||
|
0025: [fn hellos!|->:foo<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0025: at locals position 0
|
||||||
|
0027: [fn hellos!|->:foo<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0027: push_binding 000
|
||||||
|
0029: [fn hellos!|->:foo<-|:foo] (_,_,_,_,_,_,_,_)
|
||||||
|
0029: truncate
|
||||||
|
got frame: CallFrame: hellos!/1 @ 30
|
||||||
|
11..54 || (times) -> repeat times {
|
||||||
|
{
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
11..54 || (times) -> repeat times {
|
||||||
|
{
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
11..54 || (times) -> repeat times {
|
||||||
|
{
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
11..54 || (times) -> repeat times {
|
||||||
|
{
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
11..54 || (times) -> repeat times {
|
||||||
|
{
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
12..17 || times
|
||||||
|
12..17 || times
|
||||||
|
12..17 || times
|
||||||
|
12..17 || times
|
||||||
|
12..17 || times
|
||||||
|
12..17 || times
|
||||||
|
12..17 || times
|
||||||
|
12..17 || times
|
||||||
|
12..17 || times
|
||||||
|
11..54 || (times) -> repeat times {
|
||||||
|
{
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
11..54 || (times) -> repeat times {
|
||||||
|
{
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
11..54 || (times) -> repeat times {
|
||||||
|
{
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
11..54 || (times) -> repeat times {
|
||||||
|
{
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
11..54 || (times) -> repeat times {
|
||||||
|
{
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
11..54 || (times) -> repeat times {
|
||||||
|
{
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
11..54 || (times) -> repeat times {
|
||||||
|
{
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
11..54 || (times) -> repeat times {
|
||||||
|
{
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
11..54 || (times) -> repeat times {
|
||||||
|
{
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
29..34 || times
|
||||||
|
29..34 || times
|
||||||
|
29..34 || times
|
||||||
|
29..34 || times
|
||||||
|
29..34 || times
|
||||||
|
29..34 || times
|
||||||
|
22..54 || repeat times {
|
||||||
|
{
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
22..54 || repeat times {
|
||||||
|
{
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
22..54 || repeat times {
|
||||||
|
{
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
22..54 || repeat times {
|
||||||
|
{
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
22..54 || repeat times {
|
||||||
|
{
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
22..54 || repeat times {
|
||||||
|
{
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
22..54 || repeat times {
|
||||||
|
{
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
22..54 || repeat times {
|
||||||
|
{
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
22..54 || repeat times {
|
||||||
|
{
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
38..52 || print! ("hi!")
|
||||||
|
38..52 || print! ("hi!")
|
||||||
|
46..51 || "hi!"
|
||||||
|
38..52 || print! ("hi!")
|
||||||
|
38..52 || print! ("hi!")
|
||||||
|
38..52 || print! ("hi!")
|
||||||
|
38..52 || print! ("hi!")
|
||||||
|
38..52 || print! ("hi!")
|
||||||
|
38..52 || print! ("hi!")
|
||||||
|
38..52 || print! ("hi!")
|
||||||
|
38..52 || print! ("hi!")
|
||||||
|
38..52 || print! ("hi!")
|
||||||
|
38..52 || print! ("hi!")
|
||||||
|
35..54 || {
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
35..54 || {
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
35..54 || {
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
35..54 || {
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
35..54 || {
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
35..54 || {
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
35..54 || {
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
35..54 || {
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
22..54 || repeat times {
|
||||||
|
{
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
22..54 || repeat times {
|
||||||
|
{
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
22..54 || repeat times {
|
||||||
|
{
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
22..54 || repeat times {
|
||||||
|
{
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
22..54 || repeat times {
|
||||||
|
{
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
22..54 || repeat times {
|
||||||
|
{
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
11..54 || (times) -> repeat times {
|
||||||
|
{
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
11..54 || (times) -> repeat times {
|
||||||
|
{
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
11..54 || (times) -> repeat times {
|
||||||
|
{
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
11..54 || (times) -> repeat times {
|
||||||
|
{
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
at instr: 30
|
||||||
|
repeat times {
|
||||||
|
{
|
||||||
|
|
||||||
|
print! ("hi!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
22..54
|
||||||
|
CallFrame: user script/0 @ 35
|
||||||
|
[31mError:[0m Ludus panicked! `repeat` expects a number; it got `:foo`
|
||||||
|
[38;5;246m╭[0m[38;5;246m─[0m[38;5;246m[[0m sandbox.ld:1:23 [38;5;246m][0m
|
||||||
|
[38;5;246m│[0m
|
||||||
|
[38;5;246m1 │[0m [33m╭[0m[33m─[0m[33m▶[0m[33m [0m[33mf[0m[33mn[0m[33m [0m[33mh[0m[33me[0m[33ml[0m[33ml[0m[33mo[0m[33ms[0m[33m![0m[33m [0m[33m([0m[33mt[0m[33mi[0m[33mm[0m[33me[0m[33ms[0m[33m)[0m[33m [0m[33m-[0m[33m>[0m[33m [0m[31mr[0m[31me[0m[31mp[0m[31me[0m[31ma[0m[31mt[0m[31m [0m[31mt[0m[31mi[0m[31mm[0m[31me[0m[31ms[0m[31m [0m[31m{[0m
|
||||||
|
[38;5;240m ┆[0m [33m┆[0m
|
||||||
|
[38;5;246m6 │[0m [33m├[0m[33m─[0m[33m▶[0m[33m [0m[33mh[0m[33me[0m[33ml[0m[33ml[0m[33mo[0m[33ms[0m[33m![0m[33m [0m[33m([0m[33m:[0m[33mf[0m[33mo[0m[33mo[0m[33m)[0m
|
||||||
|
[38;5;240m │[0m [33m│[0m
|
||||||
|
[38;5;240m │[0m [33m╰[0m[33m─[0m[33m─[0m[33m─[0m[33m─[0m[33m─[0m[33m─[0m[33m─[0m[33m─[0m[33m─[0m[33m─[0m[33m─[0m[33m─[0m[33m─[0m[33m─[0m[33m─[0m[33m─[0m[33m─[0m[33m─[0m[33m─[0m[33m─[0m (1) calling `user script` with ()
|
||||||
|
[38;5;246m───╯[0m
|
249
scratch/first.txt
Normal file
249
scratch/first.txt
Normal file
|
@ -0,0 +1,249 @@
|
||||||
|
=== vm run: test ===
|
||||||
|
0000: [] (_,_,_,_,_,_,_,_)
|
||||||
|
0000: reset_match
|
||||||
|
0001: [] (_,_,_,_,_,_,_,_)
|
||||||
|
0001: constant 00000: 2
|
||||||
|
0004: [->2<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0004: match
|
||||||
|
0005: [->2<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0005: panic_if_no_match
|
||||||
|
0006: [->2<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0006: push_list
|
||||||
|
0007: [->2<-|[]] (_,_,_,_,_,_,_,_)
|
||||||
|
0007: constant 00001: 1
|
||||||
|
0010: [->2<-|[]|1] (_,_,_,_,_,_,_,_)
|
||||||
|
0010: append_list
|
||||||
|
0011: [->2<-|[1]] (_,_,_,_,_,_,_,_)
|
||||||
|
0011: constant 00000: 2
|
||||||
|
0014: [->2<-|[1]|2] (_,_,_,_,_,_,_,_)
|
||||||
|
0014: append_list
|
||||||
|
0015: [->2<-|[1, 2]] (_,_,_,_,_,_,_,_)
|
||||||
|
0015: constant 00002: 3
|
||||||
|
0018: [->2<-|[1, 2]|3] (_,_,_,_,_,_,_,_)
|
||||||
|
0018: append_list
|
||||||
|
0019: [->2<-|[1, 2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0019: ***entering loop with stack depth of 2
|
||||||
|
0021: [->2<-|[1, 2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0021: store_n 001
|
||||||
|
0023: [->2<-] ([1, 2, 3],_,_,_,_,_,_,_)
|
||||||
|
0023: ***after store, stack depth is now 2
|
||||||
|
0025: [->2<-] ([1, 2, 3],_,_,_,_,_,_,_)
|
||||||
|
0025: load
|
||||||
|
0026: [->2<-|[1, 2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0026: ***after load, stack depth is now 2
|
||||||
|
0028: [->2<-|[1, 2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0028: reset_match
|
||||||
|
0029: [->2<-|[1, 2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0029: match_depth 000
|
||||||
|
0031: [->2<-|[1, 2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0031: match_list 000
|
||||||
|
0033: [->2<-|[1, 2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0033: jump_if_no_match 00006
|
||||||
|
0042: [->2<-|[1, 2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0042: jump_if_no_match 00010
|
||||||
|
0055: [->2<-|[1, 2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0055: reset_match
|
||||||
|
0056: [->2<-|[1, 2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0056: match_depth 000
|
||||||
|
0058: [->2<-|[1, 2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0058: match_list 001
|
||||||
|
0060: [->2<-|[1, 2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0060: jump_if_no_match 00012
|
||||||
|
0075: [->2<-|[1, 2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0075: jump_if_no_match 00030
|
||||||
|
0108: [->2<-|[1, 2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0108: reset_match
|
||||||
|
0109: [->2<-|[1, 2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0109: match_depth 000
|
||||||
|
0111: [->2<-|[1, 2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0111: match_splatted_list 002
|
||||||
|
0113: [->2<-|[1, 2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0113: jump_if_no_match 00019
|
||||||
|
0116: [->2<-|[1, 2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0116: load_splatted_list 002
|
||||||
|
0118: [->2<-|[1, 2, 3]|1|[2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0118: match_depth 001
|
||||||
|
0120: [->2<-|[1, 2, 3]|1|[2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0120: match
|
||||||
|
0121: [->2<-|[1, 2, 3]|1|[2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0121: jump_if_no_match 00010
|
||||||
|
0124: [->2<-|[1, 2, 3]|1|[2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0124: match_depth 000
|
||||||
|
0126: [->2<-|[1, 2, 3]|1|[2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0126: match
|
||||||
|
0127: [->2<-|[1, 2, 3]|1|[2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0127: jump_if_no_match 00004
|
||||||
|
0130: [->2<-|[1, 2, 3]|1|[2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0130: jump 00002
|
||||||
|
0135: [->2<-|[1, 2, 3]|1|[2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0135: jump_if_no_match 00068
|
||||||
|
0138: [->2<-|[1, 2, 3]|1|[2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0138: ***before visiting body, the stack depth is 4
|
||||||
|
0140: [->2<-|[1, 2, 3]|1|[2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0140: ***calling function eq? stack depth: 4
|
||||||
|
0142: [->2<-|[1, 2, 3]|1|[2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0142: ***calling function first stack depth: 4
|
||||||
|
0144: [->2<-|[1, 2, 3]|1|[2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0144: resolving binding `xs` in test
|
||||||
|
0146: [->2<-|[1, 2, 3]|1|[2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0146: push_binding 003
|
||||||
|
0148: [->2<-|[1, 2, 3]|1|[2, 3]|[2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0148: resolving binding `first` in test
|
||||||
|
0150: [->2<-|[1, 2, 3]|1|[2, 3]|[2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0150: constant 00004: :first
|
||||||
|
0153: [->2<-|[1, 2, 3]|1|[2, 3]|[2, 3]|:first] (_,_,_,_,_,_,_,_)
|
||||||
|
0153: push_global
|
||||||
|
0154: [->2<-|[1, 2, 3]|1|[2, 3]|[2, 3]|fn first] (_,_,_,_,_,_,_,_)
|
||||||
|
0154: ***after 1 args stack depth: 6
|
||||||
|
0156: [->2<-|[1, 2, 3]|1|[2, 3]|[2, 3]|fn first] (_,_,_,_,_,_,_,_)
|
||||||
|
0156: call 001
|
||||||
|
=== calling into fn first/1 ===
|
||||||
|
0000: [2|[1, 2, 3]|1|[2, 3]|->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0000: reset_match
|
||||||
|
0001: [2|[1, 2, 3]|1|[2, 3]|->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0001: match_depth 000
|
||||||
|
0003: [2|[1, 2, 3]|1|[2, 3]|->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0003: match_list 000
|
||||||
|
0005: [2|[1, 2, 3]|1|[2, 3]|->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0005: jump_if_no_match 00006
|
||||||
|
0014: [2|[1, 2, 3]|1|[2, 3]|->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0014: jump_if_no_match 00003
|
||||||
|
0020: [2|[1, 2, 3]|1|[2, 3]|->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0020: jump_if_no_match 00005
|
||||||
|
0028: [2|[1, 2, 3]|1|[2, 3]|->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0028: match_depth 000
|
||||||
|
0030: [2|[1, 2, 3]|1|[2, 3]|->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0030: constant 00000: :list
|
||||||
|
0033: [2|[1, 2, 3]|1|[2, 3]|->[2, 3]<-|:list] (_,_,_,_,_,_,_,_)
|
||||||
|
0033: match_type
|
||||||
|
0034: [2|[1, 2, 3]|1|[2, 3]|->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0034: jump_if_no_match 00003
|
||||||
|
0037: [2|[1, 2, 3]|1|[2, 3]|->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0037: jump 00000
|
||||||
|
0040: [2|[1, 2, 3]|1|[2, 3]|->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0040: jump_if_no_match 00024
|
||||||
|
0043: [2|[1, 2, 3]|1|[2, 3]|->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0043: ***accessing keyword: base :first stack depth: 1
|
||||||
|
0045: [2|[1, 2, 3]|1|[2, 3]|->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0045: resolving binding `base` in first
|
||||||
|
0047: [2|[1, 2, 3]|1|[2, 3]|->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0047: get_upvalue 000
|
||||||
|
0049: [2|[1, 2, 3]|1|[2, 3]|->[2, 3]<-|#{:rest fn rest/base...] (_,_,_,_,_,_,_,_)
|
||||||
|
0049: constant 00001: :first
|
||||||
|
0052: [2|[1, 2, 3]|1|[2, 3]|->[2, 3]<-|#{:rest fn rest/base...|:first] (_,_,_,_,_,_,_,_)
|
||||||
|
0052: get_key
|
||||||
|
0053: [2|[1, 2, 3]|1|[2, 3]|->[2, 3]<-|fn first/base] (_,_,_,_,_,_,_,_)
|
||||||
|
0053: ***after keyword access stack depth: 2
|
||||||
|
0055: [2|[1, 2, 3]|1|[2, 3]|->[2, 3]<-|fn first/base] (_,_,_,_,_,_,_,_)
|
||||||
|
0055: stash
|
||||||
|
0056: [2|[1, 2, 3]|1|[2, 3]|->[2, 3]<-|fn first/base] (fn first/base,_,_,_,_,_,_,_)
|
||||||
|
0056: pop
|
||||||
|
0057: [2|[1, 2, 3]|1|[2, 3]|->[2, 3]<-] (fn first/base,_,_,_,_,_,_,_)
|
||||||
|
0057: resolving binding `xs` in first
|
||||||
|
0059: [2|[1, 2, 3]|1|[2, 3]|->[2, 3]<-] (fn first/base,_,_,_,_,_,_,_)
|
||||||
|
0059: push_binding 000
|
||||||
|
0061: [2|[1, 2, 3]|1|[2, 3]|->[2, 3]<-|[2, 3]] (fn first/base,_,_,_,_,_,_,_)
|
||||||
|
0061: load
|
||||||
|
0062: [2|[1, 2, 3]|1|[2, 3]|->[2, 3]<-|[2, 3]|fn first/base] (_,_,_,_,_,_,_,_)
|
||||||
|
0062: tail_call 001
|
||||||
|
=== tail call into fn first/base/1 from first ===
|
||||||
|
0158: [->2<-|[1, 2, 3]|1|[2, 3]|2] (_,_,_,_,_,_,_,_)
|
||||||
|
0158: resolving binding `test` in test
|
||||||
|
0160: [->2<-|[1, 2, 3]|1|[2, 3]|2] (_,_,_,_,_,_,_,_)
|
||||||
|
0160: push_binding 000
|
||||||
|
0162: [->2<-|[1, 2, 3]|1|[2, 3]|2|2] (_,_,_,_,_,_,_,_)
|
||||||
|
0162: resolving binding `eq?` in test
|
||||||
|
0164: [->2<-|[1, 2, 3]|1|[2, 3]|2|2] (_,_,_,_,_,_,_,_)
|
||||||
|
0164: constant 00003: :eq?
|
||||||
|
0167: [->2<-|[1, 2, 3]|1|[2, 3]|2|2|:eq?] (_,_,_,_,_,_,_,_)
|
||||||
|
0167: push_global
|
||||||
|
0168: [->2<-|[1, 2, 3]|1|[2, 3]|2|2|fn eq?] (_,_,_,_,_,_,_,_)
|
||||||
|
0168: ***after 2 args stack depth: 7
|
||||||
|
0170: [->2<-|[1, 2, 3]|1|[2, 3]|2|2|fn eq?] (_,_,_,_,_,_,_,_)
|
||||||
|
0170: call 002
|
||||||
|
=== calling into fn eq?/2 ===
|
||||||
|
0000: [2|[1, 2, 3]|1|[2, 3]|->2<-|2] (_,_,_,_,_,_,_,_)
|
||||||
|
0000: reset_match
|
||||||
|
0001: [2|[1, 2, 3]|1|[2, 3]|->2<-|2] (_,_,_,_,_,_,_,_)
|
||||||
|
0001: match_depth 001
|
||||||
|
0003: [2|[1, 2, 3]|1|[2, 3]|->2<-|2] (_,_,_,_,_,_,_,_)
|
||||||
|
0003: match
|
||||||
|
0004: [2|[1, 2, 3]|1|[2, 3]|->2<-|2] (_,_,_,_,_,_,_,_)
|
||||||
|
0004: jump_if_no_match 00009
|
||||||
|
0007: [2|[1, 2, 3]|1|[2, 3]|->2<-|2] (_,_,_,_,_,_,_,_)
|
||||||
|
0007: match_depth 000
|
||||||
|
0009: [2|[1, 2, 3]|1|[2, 3]|->2<-|2] (_,_,_,_,_,_,_,_)
|
||||||
|
0009: match
|
||||||
|
0010: [2|[1, 2, 3]|1|[2, 3]|->2<-|2] (_,_,_,_,_,_,_,_)
|
||||||
|
0010: jump_if_no_match 00003
|
||||||
|
0013: [2|[1, 2, 3]|1|[2, 3]|->2<-|2] (_,_,_,_,_,_,_,_)
|
||||||
|
0013: jump 00000
|
||||||
|
0016: [2|[1, 2, 3]|1|[2, 3]|->2<-|2] (_,_,_,_,_,_,_,_)
|
||||||
|
0016: jump_if_no_match 00029
|
||||||
|
0019: [2|[1, 2, 3]|1|[2, 3]|->2<-|2] (_,_,_,_,_,_,_,_)
|
||||||
|
0019: ***accessing keyword: base :eq? stack depth: 2
|
||||||
|
0021: [2|[1, 2, 3]|1|[2, 3]|->2<-|2] (_,_,_,_,_,_,_,_)
|
||||||
|
0021: resolving binding `base` in eq?
|
||||||
|
0023: [2|[1, 2, 3]|1|[2, 3]|->2<-|2] (_,_,_,_,_,_,_,_)
|
||||||
|
0023: get_upvalue 000
|
||||||
|
0025: [2|[1, 2, 3]|1|[2, 3]|->2<-|2|#{:rest fn rest/base...] (_,_,_,_,_,_,_,_)
|
||||||
|
0025: constant 00000: :eq?
|
||||||
|
0028: [2|[1, 2, 3]|1|[2, 3]|->2<-|2|#{:rest fn rest/base...|:eq?] (_,_,_,_,_,_,_,_)
|
||||||
|
0028: get_key
|
||||||
|
0029: [2|[1, 2, 3]|1|[2, 3]|->2<-|2|fn eq?/base] (_,_,_,_,_,_,_,_)
|
||||||
|
0029: ***after keyword access stack depth: 3
|
||||||
|
0031: [2|[1, 2, 3]|1|[2, 3]|->2<-|2|fn eq?/base] (_,_,_,_,_,_,_,_)
|
||||||
|
0031: stash
|
||||||
|
0032: [2|[1, 2, 3]|1|[2, 3]|->2<-|2|fn eq?/base] (fn eq?/base,_,_,_,_,_,_,_)
|
||||||
|
0032: pop
|
||||||
|
0033: [2|[1, 2, 3]|1|[2, 3]|->2<-|2] (fn eq?/base,_,_,_,_,_,_,_)
|
||||||
|
0033: resolving binding `x` in eq?
|
||||||
|
0035: [2|[1, 2, 3]|1|[2, 3]|->2<-|2] (fn eq?/base,_,_,_,_,_,_,_)
|
||||||
|
0035: push_binding 000
|
||||||
|
0037: [2|[1, 2, 3]|1|[2, 3]|->2<-|2|2] (fn eq?/base,_,_,_,_,_,_,_)
|
||||||
|
0037: resolving binding `y` in eq?
|
||||||
|
0039: [2|[1, 2, 3]|1|[2, 3]|->2<-|2|2] (fn eq?/base,_,_,_,_,_,_,_)
|
||||||
|
0039: push_binding 001
|
||||||
|
0041: [2|[1, 2, 3]|1|[2, 3]|->2<-|2|2|2] (fn eq?/base,_,_,_,_,_,_,_)
|
||||||
|
0041: load
|
||||||
|
0042: [2|[1, 2, 3]|1|[2, 3]|->2<-|2|2|2|fn eq?/base] (_,_,_,_,_,_,_,_)
|
||||||
|
0042: tail_call 002
|
||||||
|
=== tail call into fn eq?/base/2 from eq? ===
|
||||||
|
0172: [->2<-|[1, 2, 3]|1|[2, 3]|true] (_,_,_,_,_,_,_,_)
|
||||||
|
0172: jump_if_false 00004
|
||||||
|
0175: [->2<-|[1, 2, 3]|1|[2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0175: true
|
||||||
|
0176: [->2<-|[1, 2, 3]|1|[2, 3]|true] (_,_,_,_,_,_,_,_)
|
||||||
|
0176: jump 00018
|
||||||
|
0197: [->2<-|[1, 2, 3]|1|[2, 3]|true] (_,_,_,_,_,_,_,_)
|
||||||
|
0197: ***after visiting loop body, the stack depth is 5
|
||||||
|
0199: [->2<-|[1, 2, 3]|1|[2, 3]|true] (_,_,_,_,_,_,_,_)
|
||||||
|
0199: store
|
||||||
|
0200: [->2<-|[1, 2, 3]|1|[2, 3]|_] (true,_,_,_,_,_,_,_)
|
||||||
|
0200: pop
|
||||||
|
0201: [->2<-|[1, 2, 3]|1|[2, 3]] (true,_,_,_,_,_,_,_)
|
||||||
|
0201: pop
|
||||||
|
0202: [->2<-|[1, 2, 3]|1] (true,_,_,_,_,_,_,_)
|
||||||
|
0202: pop
|
||||||
|
0203: [->2<-|[1, 2, 3]] (true,_,_,_,_,_,_,_)
|
||||||
|
0203: jump 00001
|
||||||
|
0207: [->2<-|[1, 2, 3]] (true,_,_,_,_,_,_,_)
|
||||||
|
0207: load
|
||||||
|
0208: [->2<-|[1, 2, 3]|true] (_,_,_,_,_,_,_,_)
|
||||||
|
0208: store
|
||||||
|
0209: [->2<-|[1, 2, 3]|_] (true,_,_,_,_,_,_,_)
|
||||||
|
0209: pop_n 002
|
||||||
|
0211: [->2<-] (true,_,_,_,_,_,_,_)
|
||||||
|
0211: load
|
||||||
|
0212: [->2<-] (_,_,_,_,_,_,_,_)
|
||||||
|
true
|
||||||
|
|
||||||
|
**********
|
||||||
|
**********
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
291
scratch/second.txt
Normal file
291
scratch/second.txt
Normal file
|
@ -0,0 +1,291 @@
|
||||||
|
=== vm run: test ===
|
||||||
|
0000: [] (_,_,_,_,_,_,_,_)
|
||||||
|
0000: reset_match
|
||||||
|
0001: [] (_,_,_,_,_,_,_,_)
|
||||||
|
0001: constant 00000: 4
|
||||||
|
0004: [->4<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0004: match
|
||||||
|
0005: [->4<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0005: panic_if_no_match
|
||||||
|
0006: [->4<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0006: push_list
|
||||||
|
0007: [->4<-|[]] (_,_,_,_,_,_,_,_)
|
||||||
|
0007: constant 00001: 1
|
||||||
|
0010: [->4<-|[]|1] (_,_,_,_,_,_,_,_)
|
||||||
|
0010: append_list
|
||||||
|
0011: [->4<-|[1]] (_,_,_,_,_,_,_,_)
|
||||||
|
0011: constant 00002: 2
|
||||||
|
0014: [->4<-|[1]|2] (_,_,_,_,_,_,_,_)
|
||||||
|
0014: append_list
|
||||||
|
0015: [->4<-|[1, 2]] (_,_,_,_,_,_,_,_)
|
||||||
|
0015: constant 00003: 3
|
||||||
|
0018: [->4<-|[1, 2]|3] (_,_,_,_,_,_,_,_)
|
||||||
|
0018: append_list
|
||||||
|
0019: [->4<-|[1, 2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0019: ***entering loop with stack depth of 2
|
||||||
|
0021: [->4<-|[1, 2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0021: store_n 001
|
||||||
|
0023: [->4<-] ([1, 2, 3],_,_,_,_,_,_,_)
|
||||||
|
0023: ***after store, stack depth is now 2
|
||||||
|
0025: [->4<-] ([1, 2, 3],_,_,_,_,_,_,_)
|
||||||
|
0025: load
|
||||||
|
0026: [->4<-|[1, 2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0026: ***after load, stack depth is now 2
|
||||||
|
0028: [->4<-|[1, 2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0028: reset_match
|
||||||
|
0029: [->4<-|[1, 2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0029: match_depth 000
|
||||||
|
0031: [->4<-|[1, 2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0031: match_list 000
|
||||||
|
0033: [->4<-|[1, 2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0033: jump_if_no_match 00006
|
||||||
|
0042: [->4<-|[1, 2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0042: jump_if_no_match 00010
|
||||||
|
0055: [->4<-|[1, 2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0055: reset_match
|
||||||
|
0056: [->4<-|[1, 2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0056: match_depth 000
|
||||||
|
0058: [->4<-|[1, 2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0058: match_list 001
|
||||||
|
0060: [->4<-|[1, 2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0060: jump_if_no_match 00012
|
||||||
|
0075: [->4<-|[1, 2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0075: jump_if_no_match 00030
|
||||||
|
0108: [->4<-|[1, 2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0108: reset_match
|
||||||
|
0109: [->4<-|[1, 2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0109: match_depth 000
|
||||||
|
0111: [->4<-|[1, 2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0111: match_splatted_list 002
|
||||||
|
0113: [->4<-|[1, 2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0113: jump_if_no_match 00019
|
||||||
|
0116: [->4<-|[1, 2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0116: load_splatted_list 002
|
||||||
|
0118: [->4<-|[1, 2, 3]|1|[2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0118: match_depth 001
|
||||||
|
0120: [->4<-|[1, 2, 3]|1|[2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0120: match
|
||||||
|
0121: [->4<-|[1, 2, 3]|1|[2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0121: jump_if_no_match 00010
|
||||||
|
0124: [->4<-|[1, 2, 3]|1|[2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0124: match_depth 000
|
||||||
|
0126: [->4<-|[1, 2, 3]|1|[2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0126: match
|
||||||
|
0127: [->4<-|[1, 2, 3]|1|[2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0127: jump_if_no_match 00004
|
||||||
|
0130: [->4<-|[1, 2, 3]|1|[2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0130: jump 00002
|
||||||
|
0135: [->4<-|[1, 2, 3]|1|[2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0135: jump_if_no_match 00068
|
||||||
|
0138: [->4<-|[1, 2, 3]|1|[2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0138: ***before visiting body, the stack depth is 4
|
||||||
|
0140: [->4<-|[1, 2, 3]|1|[2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0140: ***calling function eq? stack depth: 4
|
||||||
|
0142: [->4<-|[1, 2, 3]|1|[2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0142: ***calling function first stack depth: 4
|
||||||
|
0144: [->4<-|[1, 2, 3]|1|[2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0144: resolving binding `xs` in test
|
||||||
|
0146: [->4<-|[1, 2, 3]|1|[2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0146: push_binding 003
|
||||||
|
0148: [->4<-|[1, 2, 3]|1|[2, 3]|[2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0148: resolving binding `first` in test
|
||||||
|
0150: [->4<-|[1, 2, 3]|1|[2, 3]|[2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0150: constant 00005: :first
|
||||||
|
0153: [->4<-|[1, 2, 3]|1|[2, 3]|[2, 3]|:first] (_,_,_,_,_,_,_,_)
|
||||||
|
0153: push_global
|
||||||
|
0154: [->4<-|[1, 2, 3]|1|[2, 3]|[2, 3]|fn first] (_,_,_,_,_,_,_,_)
|
||||||
|
0154: ***after 1 args stack depth: 6
|
||||||
|
0156: [->4<-|[1, 2, 3]|1|[2, 3]|[2, 3]|fn first] (_,_,_,_,_,_,_,_)
|
||||||
|
0156: call 001
|
||||||
|
=== calling into fn first/1 ===
|
||||||
|
0000: [4|[1, 2, 3]|1|[2, 3]|->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0000: reset_match
|
||||||
|
0001: [4|[1, 2, 3]|1|[2, 3]|->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0001: match_depth 000
|
||||||
|
0003: [4|[1, 2, 3]|1|[2, 3]|->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0003: match_list 000
|
||||||
|
0005: [4|[1, 2, 3]|1|[2, 3]|->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0005: jump_if_no_match 00006
|
||||||
|
0014: [4|[1, 2, 3]|1|[2, 3]|->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0014: jump_if_no_match 00003
|
||||||
|
0020: [4|[1, 2, 3]|1|[2, 3]|->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0020: jump_if_no_match 00005
|
||||||
|
0028: [4|[1, 2, 3]|1|[2, 3]|->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0028: match_depth 000
|
||||||
|
0030: [4|[1, 2, 3]|1|[2, 3]|->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0030: constant 00000: :list
|
||||||
|
0033: [4|[1, 2, 3]|1|[2, 3]|->[2, 3]<-|:list] (_,_,_,_,_,_,_,_)
|
||||||
|
0033: match_type
|
||||||
|
0034: [4|[1, 2, 3]|1|[2, 3]|->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0034: jump_if_no_match 00003
|
||||||
|
0037: [4|[1, 2, 3]|1|[2, 3]|->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0037: jump 00000
|
||||||
|
0040: [4|[1, 2, 3]|1|[2, 3]|->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0040: jump_if_no_match 00024
|
||||||
|
0043: [4|[1, 2, 3]|1|[2, 3]|->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0043: ***accessing keyword: base :first stack depth: 1
|
||||||
|
0045: [4|[1, 2, 3]|1|[2, 3]|->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0045: resolving binding `base` in first
|
||||||
|
0047: [4|[1, 2, 3]|1|[2, 3]|->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0047: get_upvalue 000
|
||||||
|
0049: [4|[1, 2, 3]|1|[2, 3]|->[2, 3]<-|#{:append fn append/...] (_,_,_,_,_,_,_,_)
|
||||||
|
0049: constant 00001: :first
|
||||||
|
0052: [4|[1, 2, 3]|1|[2, 3]|->[2, 3]<-|#{:append fn append/...|:first] (_,_,_,_,_,_,_,_)
|
||||||
|
0052: get_key?
|
||||||
|
0053: [4|[1, 2, 3]|1|[2, 3]|->[2, 3]<-|fn first/base] (_,_,_,_,_,_,_,_)
|
||||||
|
0053: ***after keyword access stack depth: 2
|
||||||
|
0055: [4|[1, 2, 3]|1|[2, 3]|->[2, 3]<-|fn first/base] (_,_,_,_,_,_,_,_)
|
||||||
|
0055: stash
|
||||||
|
0056: [4|[1, 2, 3]|1|[2, 3]|->[2, 3]<-|fn first/base] (fn first/base,_,_,_,_,_,_,_)
|
||||||
|
0056: pop
|
||||||
|
0057: [4|[1, 2, 3]|1|[2, 3]|->[2, 3]<-] (fn first/base,_,_,_,_,_,_,_)
|
||||||
|
0057: resolving binding `xs` in first
|
||||||
|
0059: [4|[1, 2, 3]|1|[2, 3]|->[2, 3]<-] (fn first/base,_,_,_,_,_,_,_)
|
||||||
|
0059: push_binding 000
|
||||||
|
0061: [4|[1, 2, 3]|1|[2, 3]|->[2, 3]<-|[2, 3]] (fn first/base,_,_,_,_,_,_,_)
|
||||||
|
0061: load
|
||||||
|
0062: [4|[1, 2, 3]|1|[2, 3]|->[2, 3]<-|[2, 3]|fn first/base] (_,_,_,_,_,_,_,_)
|
||||||
|
0062: tail_call 001
|
||||||
|
=== tail call into fn first/base/1 from first ===
|
||||||
|
0158: [->4<-|[1, 2, 3]|1|[2, 3]|2] (_,_,_,_,_,_,_,_)
|
||||||
|
0158: resolving binding `test` in test
|
||||||
|
0160: [->4<-|[1, 2, 3]|1|[2, 3]|2] (_,_,_,_,_,_,_,_)
|
||||||
|
0160: push_binding 000
|
||||||
|
0162: [->4<-|[1, 2, 3]|1|[2, 3]|2|4] (_,_,_,_,_,_,_,_)
|
||||||
|
0162: resolving binding `eq?` in test
|
||||||
|
0164: [->4<-|[1, 2, 3]|1|[2, 3]|2|4] (_,_,_,_,_,_,_,_)
|
||||||
|
0164: constant 00004: :eq?
|
||||||
|
0167: [->4<-|[1, 2, 3]|1|[2, 3]|2|4|:eq?] (_,_,_,_,_,_,_,_)
|
||||||
|
0167: push_global
|
||||||
|
0168: [->4<-|[1, 2, 3]|1|[2, 3]|2|4|fn eq?] (_,_,_,_,_,_,_,_)
|
||||||
|
0168: ***after 2 args stack depth: 7
|
||||||
|
0170: [->4<-|[1, 2, 3]|1|[2, 3]|2|4|fn eq?] (_,_,_,_,_,_,_,_)
|
||||||
|
0170: call 002
|
||||||
|
=== calling into fn eq?/2 ===
|
||||||
|
0000: [4|[1, 2, 3]|1|[2, 3]|->2<-|4] (_,_,_,_,_,_,_,_)
|
||||||
|
0000: reset_match
|
||||||
|
0001: [4|[1, 2, 3]|1|[2, 3]|->2<-|4] (_,_,_,_,_,_,_,_)
|
||||||
|
0001: match_depth 001
|
||||||
|
0003: [4|[1, 2, 3]|1|[2, 3]|->2<-|4] (_,_,_,_,_,_,_,_)
|
||||||
|
0003: match
|
||||||
|
0004: [4|[1, 2, 3]|1|[2, 3]|->2<-|4] (_,_,_,_,_,_,_,_)
|
||||||
|
0004: jump_if_no_match 00009
|
||||||
|
0007: [4|[1, 2, 3]|1|[2, 3]|->2<-|4] (_,_,_,_,_,_,_,_)
|
||||||
|
0007: match_depth 000
|
||||||
|
0009: [4|[1, 2, 3]|1|[2, 3]|->2<-|4] (_,_,_,_,_,_,_,_)
|
||||||
|
0009: match
|
||||||
|
0010: [4|[1, 2, 3]|1|[2, 3]|->2<-|4] (_,_,_,_,_,_,_,_)
|
||||||
|
0010: jump_if_no_match 00003
|
||||||
|
0013: [4|[1, 2, 3]|1|[2, 3]|->2<-|4] (_,_,_,_,_,_,_,_)
|
||||||
|
0013: jump 00000
|
||||||
|
0016: [4|[1, 2, 3]|1|[2, 3]|->2<-|4] (_,_,_,_,_,_,_,_)
|
||||||
|
0016: jump_if_no_match 00029
|
||||||
|
0019: [4|[1, 2, 3]|1|[2, 3]|->2<-|4] (_,_,_,_,_,_,_,_)
|
||||||
|
0019: ***accessing keyword: base :eq? stack depth: 2
|
||||||
|
0021: [4|[1, 2, 3]|1|[2, 3]|->2<-|4] (_,_,_,_,_,_,_,_)
|
||||||
|
0021: resolving binding `base` in eq?
|
||||||
|
0023: [4|[1, 2, 3]|1|[2, 3]|->2<-|4] (_,_,_,_,_,_,_,_)
|
||||||
|
0023: get_upvalue 000
|
||||||
|
0025: [4|[1, 2, 3]|1|[2, 3]|->2<-|4|#{:append fn append/...] (_,_,_,_,_,_,_,_)
|
||||||
|
0025: constant 00000: :eq?
|
||||||
|
0028: [4|[1, 2, 3]|1|[2, 3]|->2<-|4|#{:append fn append/...|:eq?] (_,_,_,_,_,_,_,_)
|
||||||
|
0028: get_key
|
||||||
|
0029: [4|[1, 2, 3]|1|[2, 3]|->2<-|4|fn eq?/base] (_,_,_,_,_,_,_,_)
|
||||||
|
0029: ***after keyword access stack depth: 3
|
||||||
|
0031: [4|[1, 2, 3]|1|[2, 3]|->2<-|4|fn eq?/base] (_,_,_,_,_,_,_,_)
|
||||||
|
0031: stash
|
||||||
|
0032: [4|[1, 2, 3]|1|[2, 3]|->2<-|4|fn eq?/base] (fn eq?/base,_,_,_,_,_,_,_)
|
||||||
|
0032: pop
|
||||||
|
0033: [4|[1, 2, 3]|1|[2, 3]|->2<-|4] (fn eq?/base,_,_,_,_,_,_,_)
|
||||||
|
0033: resolving binding `x` in eq?
|
||||||
|
0035: [4|[1, 2, 3]|1|[2, 3]|->2<-|4] (fn eq?/base,_,_,_,_,_,_,_)
|
||||||
|
0035: push_binding 000
|
||||||
|
0037: [4|[1, 2, 3]|1|[2, 3]|->2<-|4|2] (fn eq?/base,_,_,_,_,_,_,_)
|
||||||
|
0037: resolving binding `y` in eq?
|
||||||
|
0039: [4|[1, 2, 3]|1|[2, 3]|->2<-|4|2] (fn eq?/base,_,_,_,_,_,_,_)
|
||||||
|
0039: push_binding 001
|
||||||
|
0041: [4|[1, 2, 3]|1|[2, 3]|->2<-|4|2|4] (fn eq?/base,_,_,_,_,_,_,_)
|
||||||
|
0041: load
|
||||||
|
0042: [4|[1, 2, 3]|1|[2, 3]|->2<-|4|2|4|fn eq?/base] (_,_,_,_,_,_,_,_)
|
||||||
|
0042: tail_call 002
|
||||||
|
=== tail call into fn eq?/base/2 from eq? ===
|
||||||
|
0172: [->4<-|[1, 2, 3]|1|[2, 3]|false] (_,_,_,_,_,_,_,_)
|
||||||
|
0172: jump_if_false 00004
|
||||||
|
0179: [->4<-|[1, 2, 3]|1|[2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0179: before visiting recur args the compiler thinks the stack depth is 5
|
||||||
|
0181: [->4<-|[1, 2, 3]|1|[2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0181: recur arg: 0
|
||||||
|
0183: [->4<-|[1, 2, 3]|1|[2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0183: resolving binding `xs` in test
|
||||||
|
0185: [->4<-|[1, 2, 3]|1|[2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0185: push_binding 003
|
||||||
|
0187: [->4<-|[1, 2, 3]|1|[2, 3]|[2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0187: after visiting recur args the compiler thinks the stack depth is 6
|
||||||
|
0189: [->4<-|[1, 2, 3]|1|[2, 3]|[2, 3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0189: store_n 001
|
||||||
|
0191: [->4<-|[1, 2, 3]|1|[2, 3]] ([2, 3],_,_,_,_,_,_,_)
|
||||||
|
0191: pop_n 004
|
||||||
|
0193: [] ([2, 3],_,_,_,_,_,_,_)
|
||||||
|
0193: load
|
||||||
|
0194: [->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0194: jump_back 00168
|
||||||
|
0026: [->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0026: ***after load, stack depth is now 2
|
||||||
|
0028: [->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0028: reset_match
|
||||||
|
0029: [->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0029: match_depth 000
|
||||||
|
0031: [->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0031: match_list 000
|
||||||
|
0033: [->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0033: jump_if_no_match 00006
|
||||||
|
0042: [->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0042: jump_if_no_match 00010
|
||||||
|
0055: [->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0055: reset_match
|
||||||
|
0056: [->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0056: match_depth 000
|
||||||
|
0058: [->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0058: match_list 001
|
||||||
|
0060: [->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0060: jump_if_no_match 00012
|
||||||
|
0075: [->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0075: jump_if_no_match 00030
|
||||||
|
0108: [->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0108: reset_match
|
||||||
|
0109: [->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0109: match_depth 000
|
||||||
|
0111: [->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0111: match_splatted_list 002
|
||||||
|
0113: [->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0113: jump_if_no_match 00019
|
||||||
|
0116: [->[2, 3]<-] (_,_,_,_,_,_,_,_)
|
||||||
|
0116: load_splatted_list 002
|
||||||
|
0118: [->[2, 3]<-|2|[3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0118: match_depth 001
|
||||||
|
0120: [->[2, 3]<-|2|[3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0120: match
|
||||||
|
0121: [->[2, 3]<-|2|[3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0121: jump_if_no_match 00010
|
||||||
|
0124: [->[2, 3]<-|2|[3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0124: match_depth 000
|
||||||
|
0126: [->[2, 3]<-|2|[3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0126: match
|
||||||
|
0127: [->[2, 3]<-|2|[3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0127: jump_if_no_match 00004
|
||||||
|
0130: [->[2, 3]<-|2|[3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0130: jump 00002
|
||||||
|
0135: [->[2, 3]<-|2|[3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0135: jump_if_no_match 00068
|
||||||
|
0138: [->[2, 3]<-|2|[3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0138: ***before visiting body, the stack depth is 4
|
||||||
|
0140: [->[2, 3]<-|2|[3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0140: ***calling function eq? stack depth: 4
|
||||||
|
0142: [->[2, 3]<-|2|[3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0142: ***calling function first stack depth: 4
|
||||||
|
0144: [->[2, 3]<-|2|[3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0144: resolving binding `xs` in test
|
||||||
|
0146: [->[2, 3]<-|2|[3]] (_,_,_,_,_,_,_,_)
|
||||||
|
0146: push_binding 003
|
||||||
|
thread 'main' panicked at src/vm.rs:313:51:
|
||||||
|
index out of bounds: the len is 3 but the index is 3
|
471
src/base.rs
471
src/base.rs
|
@ -4,40 +4,44 @@ use ran::ran_f64;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub enum BaseFn<'src> {
|
pub enum BaseFn {
|
||||||
Nullary(fn() -> Value<'src>),
|
Nullary(&'static str, fn() -> Value),
|
||||||
Unary(fn(&Value<'src>) -> Value<'src>),
|
Unary(&'static str, fn(&Value) -> Value),
|
||||||
Binary(fn(&Value<'src>, &Value<'src>) -> Value<'src>),
|
Binary(&'static str, fn(&Value, &Value) -> Value),
|
||||||
Ternary(fn(&Value<'src>, &Value<'src>, &Value<'src>) -> Value<'src>),
|
Ternary(&'static str, fn(&Value, &Value, &Value) -> Value),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn eq<'src>(x: &Value<'src>, y: &Value<'src>) -> Value<'src> {
|
pub fn eq(x: &Value, y: &Value) -> Value {
|
||||||
Value::Boolean(x == y)
|
if x == y {
|
||||||
}
|
Value::True
|
||||||
|
} else {
|
||||||
pub fn add<'src>(x: &Value<'src>, y: &Value<'src>) -> Value<'src> {
|
Value::False
|
||||||
match (x, y) {
|
|
||||||
(Value::Number(x), Value::Number(y)) => Value::Number(x + y),
|
|
||||||
_ => unreachable!("internal Ludus error"),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sub<'src>(x: &Value<'src>, y: &Value<'src>) -> Value<'src> {
|
pub fn add(x: &Value, y: &Value) -> Value {
|
||||||
|
match (x, y) {
|
||||||
|
(Value::Number(x), Value::Number(y)) => Value::Number(x + y),
|
||||||
|
_ => unreachable!("internal Ludus error: wrong arguments to base add: {x}, {y}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn sub(x: &Value, y: &Value) -> Value {
|
||||||
match (x, y) {
|
match (x, y) {
|
||||||
(Value::Number(x), Value::Number(y)) => Value::Number(x - y),
|
(Value::Number(x), Value::Number(y)) => Value::Number(x - y),
|
||||||
_ => unreachable!("internal Ludus error"),
|
_ => unreachable!("internal Ludus error"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn unbox<'src>(x: &Value<'src>) -> Value<'src> {
|
pub fn unbox(x: &Value) -> Value {
|
||||||
match x {
|
match x {
|
||||||
Value::Box(_, cell) => cell.borrow().clone(),
|
Value::Box(cell) => cell.as_ref().borrow().clone(),
|
||||||
_ => unreachable!("internal Ludus error"),
|
_ => unreachable!("internal Ludus error"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn store<'src>(b: &Value<'src>, val: &Value<'src>) -> Value<'src> {
|
pub fn store(b: &Value, val: &Value) -> Value {
|
||||||
if let Value::Box(_, cell) = b {
|
if let Value::Box(cell) = b {
|
||||||
cell.replace(val.clone());
|
cell.replace(val.clone());
|
||||||
val.clone()
|
val.clone()
|
||||||
} else {
|
} else {
|
||||||
|
@ -47,103 +51,88 @@ pub fn store<'src>(b: &Value<'src>, val: &Value<'src>) -> Value<'src> {
|
||||||
|
|
||||||
// TODO: do better than returning just the docstr
|
// TODO: do better than returning just the docstr
|
||||||
// name, patterns, AND docstring
|
// name, patterns, AND docstring
|
||||||
pub fn doc<'src>(f: &Value<'src>) -> Value<'src> {
|
pub fn doc(f: &Value) -> Value {
|
||||||
match f {
|
match f {
|
||||||
Value::Fn(f) => {
|
Value::Fn(f) => f.as_ref().doc(),
|
||||||
let name = &f.name;
|
_ => Value::Interned("no documentation found"),
|
||||||
let doc = &f.doc;
|
|
||||||
if let Some(docstr) = doc {
|
|
||||||
Value::AllocatedString(Rc::new(format!("{name}: {docstr}")))
|
|
||||||
} else {
|
|
||||||
Value::AllocatedString(Rc::new(format!("{name}: no documentation found")))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => Value::InternedString("no documentation found"),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn and<'src>(x: &Value<'src>, y: &Value<'src>) -> Value<'src> {
|
pub fn assoc(dict: &Value, key: &Value, value: &Value) -> Value {
|
||||||
Value::Boolean(x.bool() && y.bool())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn assoc<'src>(dict: &Value<'src>, key: &Value<'src>, value: &Value<'src>) -> Value<'src> {
|
|
||||||
match (dict, key) {
|
match (dict, key) {
|
||||||
(Value::Dict(d), Value::Keyword(k)) => Value::Dict(d.update(k, value.clone())),
|
(Value::Dict(d), Value::Keyword(k)) => Value::Dict(Box::new(d.update(k, value.clone()))),
|
||||||
_ => unreachable!("internal Ludus error"),
|
_ => unreachable!("internal Ludus error calling assoc with ({dict}, {key}, {value})"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn r#bool<'src>(x: &Value<'src>) -> Value<'src> {
|
pub fn r#bool(x: &Value) -> Value {
|
||||||
Value::Boolean(x.bool())
|
match x {
|
||||||
|
Value::Nil | Value::False => Value::False,
|
||||||
|
_ => Value::True,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn chars<'src>(x: &Value<'src>) -> Value<'src> {
|
pub fn chars(x: &Value) -> Value {
|
||||||
match x {
|
match x {
|
||||||
Value::InternedString(s) => {
|
Value::Interned(s) => {
|
||||||
let chars = s.chars();
|
let chars = s.chars();
|
||||||
|
|
||||||
let mut charlist = vector![];
|
let mut charlist = vector![];
|
||||||
for char in chars {
|
for char in chars {
|
||||||
if char.is_ascii() {
|
if char.is_ascii() {
|
||||||
charlist.push_back(Value::AllocatedString(Rc::new(char.to_string())))
|
charlist.push_back(Value::String(Rc::new(char.to_string())))
|
||||||
} else {
|
} else {
|
||||||
return Value::Tuple(Rc::new(vec![
|
return Value::Tuple(Rc::new(vec![
|
||||||
Value::Keyword("err"),
|
Value::Keyword("err"),
|
||||||
Value::AllocatedString(Rc::new(format!(
|
Value::String(Rc::new(format!("{char} is not an ascii character"))),
|
||||||
"{char} is not an ascii character"
|
|
||||||
))),
|
|
||||||
]));
|
]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Value::Tuple(Rc::new(vec![Value::Keyword("ok"), Value::List(charlist)]))
|
Value::Tuple(Rc::new(vec![
|
||||||
|
Value::Keyword("ok"),
|
||||||
|
Value::List(Box::new(charlist)),
|
||||||
|
]))
|
||||||
}
|
}
|
||||||
Value::AllocatedString(s) => {
|
Value::String(s) => {
|
||||||
let chars = s.chars();
|
let chars = s.chars();
|
||||||
|
|
||||||
let mut charlist = vector![];
|
let mut charlist = vector![];
|
||||||
for char in chars {
|
for char in chars {
|
||||||
if char.is_ascii() {
|
if char.is_ascii() {
|
||||||
charlist.push_back(Value::AllocatedString(Rc::new(char.to_string())))
|
charlist.push_back(Value::String(Rc::new(char.to_string())))
|
||||||
} else {
|
} else {
|
||||||
return Value::Tuple(Rc::new(vec![
|
return Value::Tuple(Rc::new(vec![
|
||||||
Value::Keyword("err"),
|
Value::Keyword("err"),
|
||||||
Value::AllocatedString(Rc::new(format!(
|
Value::String(Rc::new(format!("{char} is not an ascii character"))),
|
||||||
"{char} is not an ascii character"
|
|
||||||
))),
|
|
||||||
]));
|
]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Value::Tuple(Rc::new(vec![Value::Keyword("ok"), Value::List(charlist)]))
|
Value::Tuple(Rc::new(vec![
|
||||||
|
Value::Keyword("ok"),
|
||||||
|
Value::List(Box::new(charlist)),
|
||||||
|
]))
|
||||||
}
|
}
|
||||||
_ => unreachable!("internal Ludus error"),
|
_ => unreachable!("internal Ludus error"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: figure out how to get to opportunistic mutation here
|
// TODO: figure out how to get to opportunistic mutation here
|
||||||
pub fn concat<'src>(x: &Value<'src>, y: &Value<'src>) -> Value<'src> {
|
pub fn concat(x: &Value, y: &Value) -> Value {
|
||||||
match (x, y) {
|
match (x, y) {
|
||||||
(Value::InternedString(x), Value::InternedString(y)) => {
|
(Value::Interned(x), Value::Interned(y)) => Value::String(Rc::new(format!("{x}{y}"))),
|
||||||
Value::AllocatedString(Rc::new(format!("{x}{y}")))
|
(Value::String(x), Value::String(y)) => Value::String(Rc::new(format!("{x}{y}"))),
|
||||||
}
|
(Value::String(x), Value::Interned(y)) => Value::String(Rc::new(format!("{x}{y}"))),
|
||||||
(Value::AllocatedString(x), Value::AllocatedString(y)) => {
|
(Value::Interned(x), Value::String(y)) => Value::String(Rc::new(format!("{x}{y}"))),
|
||||||
Value::AllocatedString(Rc::new(format!("{x}{y}")))
|
|
||||||
}
|
|
||||||
(Value::AllocatedString(x), Value::InternedString(y)) => {
|
|
||||||
Value::AllocatedString(Rc::new(format!("{x}{y}")))
|
|
||||||
}
|
|
||||||
(Value::InternedString(x), Value::AllocatedString(y)) => {
|
|
||||||
Value::AllocatedString(Rc::new(format!("{x}{y}")))
|
|
||||||
}
|
|
||||||
(Value::List(x), Value::List(y)) => {
|
(Value::List(x), Value::List(y)) => {
|
||||||
let mut newlist = x.clone();
|
let mut newlist = *x.clone();
|
||||||
newlist.append(y.clone());
|
newlist.append(*y.clone());
|
||||||
Value::List(newlist)
|
Value::List(Box::new(newlist))
|
||||||
}
|
}
|
||||||
_ => unreachable!("internal Ludus error"),
|
_ => unreachable!("internal Ludus error"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn append<'src>(x: &Value<'src>, y: &Value<'src>) -> Value<'src> {
|
pub fn append(x: &Value, y: &Value) -> Value {
|
||||||
match x {
|
match x {
|
||||||
Value::List(list) => {
|
Value::List(list) => {
|
||||||
let mut newlist = list.clone();
|
let mut newlist = list.clone();
|
||||||
|
@ -154,35 +143,35 @@ pub fn append<'src>(x: &Value<'src>, y: &Value<'src>) -> Value<'src> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn dec<'src>(x: &Value<'src>) -> Value<'src> {
|
pub fn dec(x: &Value) -> Value {
|
||||||
match x {
|
match x {
|
||||||
Value::Number(n) => Value::Number(n - 1.0),
|
Value::Number(n) => Value::Number(n - 1.0),
|
||||||
_ => unreachable!("internal Ludus error"),
|
_ => unreachable!("internal Ludus error"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn inc<'src>(x: &Value<'src>) -> Value<'src> {
|
pub fn inc(x: &Value) -> Value {
|
||||||
match x {
|
match x {
|
||||||
Value::Number(n) => Value::Number(n + 1.0),
|
Value::Number(n) => Value::Number(n + 1.0),
|
||||||
_ => unreachable!("internal Ludus error"),
|
_ => unreachable!("internal Ludus error"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn div<'src>(x: &Value<'src>, y: &Value<'src>) -> Value<'src> {
|
pub fn div(x: &Value, y: &Value) -> Value {
|
||||||
match (x, y) {
|
match (x, y) {
|
||||||
(Value::Number(x), Value::Number(y)) => Value::Number(x / y),
|
(Value::Number(x), Value::Number(y)) => Value::Number(x / y),
|
||||||
_ => unreachable!("internal Ludus error"),
|
_ => unreachable!("internal Ludus error"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn mult<'src>(x: &Value<'src>, y: &Value<'src>) -> Value<'src> {
|
pub fn mult(x: &Value, y: &Value) -> Value {
|
||||||
match (x, y) {
|
match (x, y) {
|
||||||
(Value::Number(x), Value::Number(y)) => Value::Number(x * y),
|
(Value::Number(x), Value::Number(y)) => Value::Number(x * y),
|
||||||
_ => unreachable!("internal Ludus error"),
|
_ => unreachable!("internal Ludus error"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn dissoc<'src>(dict: &Value<'src>, key: &Value<'src>) -> Value<'src> {
|
pub fn dissoc(dict: &Value, key: &Value) -> Value {
|
||||||
match (dict, key) {
|
match (dict, key) {
|
||||||
(Value::Dict(dict), Value::Keyword(key)) => {
|
(Value::Dict(dict), Value::Keyword(key)) => {
|
||||||
let mut new = dict.clone();
|
let mut new = dict.clone();
|
||||||
|
@ -193,7 +182,7 @@ pub fn dissoc<'src>(dict: &Value<'src>, key: &Value<'src>) -> Value<'src> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn first<'src>(ordered: &Value<'src>) -> Value<'src> {
|
pub fn first(ordered: &Value) -> Value {
|
||||||
match ordered {
|
match ordered {
|
||||||
Value::List(list) => match list.front() {
|
Value::List(list) => match list.front() {
|
||||||
Some(n) => n.clone(),
|
Some(n) => n.clone(),
|
||||||
|
@ -209,7 +198,7 @@ pub fn first<'src>(ordered: &Value<'src>) -> Value<'src> {
|
||||||
|
|
||||||
// TODO: figure out how to handle negative numbers
|
// TODO: figure out how to handle negative numbers
|
||||||
// the cast from f64 to usize discards sign info
|
// the cast from f64 to usize discards sign info
|
||||||
pub fn at<'src>(ordered: &Value<'src>, i: &Value<'src>) -> Value<'src> {
|
pub fn at(ordered: &Value, i: &Value) -> Value {
|
||||||
match (ordered, i) {
|
match (ordered, i) {
|
||||||
(Value::List(list), Value::Number(n)) => {
|
(Value::List(list), Value::Number(n)) => {
|
||||||
let i = *n as usize;
|
let i = *n as usize;
|
||||||
|
@ -229,7 +218,7 @@ pub fn at<'src>(ordered: &Value<'src>, i: &Value<'src>) -> Value<'src> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get<'src>(dict: &Value<'src>, key: &Value<'src>) -> Value<'src> {
|
pub fn get(dict: &Value, key: &Value) -> Value {
|
||||||
match (dict, key) {
|
match (dict, key) {
|
||||||
(Value::Dict(dict), Value::Keyword(key)) => match dict.get(key) {
|
(Value::Dict(dict), Value::Keyword(key)) => match dict.get(key) {
|
||||||
Some(x) => x.clone(),
|
Some(x) => x.clone(),
|
||||||
|
@ -239,7 +228,7 @@ pub fn get<'src>(dict: &Value<'src>, key: &Value<'src>) -> Value<'src> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn last<'src>(ordered: &Value<'src>) -> Value<'src> {
|
pub fn last(ordered: &Value) -> Value {
|
||||||
match ordered {
|
match ordered {
|
||||||
Value::List(list) => match list.last() {
|
Value::List(list) => match list.last() {
|
||||||
Some(x) => x.clone(),
|
Some(x) => x.clone(),
|
||||||
|
@ -253,12 +242,8 @@ pub fn last<'src>(ordered: &Value<'src>) -> Value<'src> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn or<'src>(x: &Value<'src>, y: &Value<'src>) -> Value<'src> {
|
|
||||||
Value::Boolean(x.bool() || y.bool())
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: fix this: x is a list of all the args passed to Ludus's print!
|
// TODO: fix this: x is a list of all the args passed to Ludus's print!
|
||||||
pub fn print<'src>(x: &Value<'src>) -> Value<'src> {
|
pub fn print(x: &Value) -> Value {
|
||||||
let Value::List(args) = x else {
|
let Value::List(args) = x else {
|
||||||
unreachable!("internal Ludus error")
|
unreachable!("internal Ludus error")
|
||||||
};
|
};
|
||||||
|
@ -266,35 +251,41 @@ pub fn print<'src>(x: &Value<'src>) -> Value<'src> {
|
||||||
.iter()
|
.iter()
|
||||||
.map(|val| format!("{val}"))
|
.map(|val| format!("{val}"))
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.join("");
|
.join(" ");
|
||||||
println!("{out}");
|
println!("{out}");
|
||||||
Value::Keyword("ok")
|
Value::Keyword("ok")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn show<'src>(x: &Value<'src>) -> Value<'src> {
|
pub fn show(x: &Value) -> Value {
|
||||||
Value::AllocatedString(Rc::new(format!("{x}")))
|
Value::String(Rc::new(format!("{x}")))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn rest<'src>(ordered: &Value<'src>) -> Value<'src> {
|
pub fn rest(ordered: &Value) -> Value {
|
||||||
match ordered {
|
match ordered {
|
||||||
Value::List(list) => Value::List(list.clone().split_at(1).1),
|
Value::List(list) => Value::List(Box::new(list.clone().split_at(1).1)),
|
||||||
Value::Tuple(tuple) => Value::List(Vector::from_iter(tuple.iter().next().cloned())),
|
Value::Tuple(tuple) => {
|
||||||
|
Value::List(Box::new(Vector::from_iter(tuple.iter().next().cloned())))
|
||||||
|
}
|
||||||
|
Value::Interned(str) => Value::String(Rc::new(str.get(1..).unwrap_or("").to_string())),
|
||||||
|
Value::String(str) => Value::String(Rc::new(
|
||||||
|
str.clone().as_str().get(1..).unwrap_or("").to_string(),
|
||||||
|
)),
|
||||||
_ => unreachable!("internal Ludus error"),
|
_ => unreachable!("internal Ludus error"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn count<'src>(coll: &Value<'src>) -> Value<'src> {
|
pub fn count(coll: &Value) -> Value {
|
||||||
match coll {
|
match coll {
|
||||||
Value::Dict(d) => Value::Number(d.len() as f64),
|
Value::Dict(d) => Value::Number(d.len() as f64),
|
||||||
Value::List(l) => Value::Number(l.len() as f64),
|
Value::List(l) => Value::Number(l.len() as f64),
|
||||||
Value::Tuple(t) => Value::Number(t.len() as f64),
|
Value::Tuple(t) => Value::Number(t.len() as f64),
|
||||||
Value::AllocatedString(s) => Value::Number(s.len() as f64),
|
Value::String(s) => Value::Number(s.len() as f64),
|
||||||
Value::InternedString(s) => Value::Number(s.len() as f64),
|
Value::Interned(s) => Value::Number(s.len() as f64),
|
||||||
_ => unreachable!("internal Ludus error"),
|
_ => unreachable!("internal Ludus error"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn range<'src>(start: &Value<'src>, end: &Value<'src>) -> Value<'src> {
|
pub fn range(start: &Value, end: &Value) -> Value {
|
||||||
match (start, end) {
|
match (start, end) {
|
||||||
(Value::Number(start), Value::Number(end)) => {
|
(Value::Number(start), Value::Number(end)) => {
|
||||||
let start = *start as isize;
|
let start = *start as isize;
|
||||||
|
@ -303,25 +294,25 @@ pub fn range<'src>(start: &Value<'src>, end: &Value<'src>) -> Value<'src> {
|
||||||
for n in start..end {
|
for n in start..end {
|
||||||
range.push_back(Value::Number(n as f64))
|
range.push_back(Value::Number(n as f64))
|
||||||
}
|
}
|
||||||
Value::List(range)
|
Value::List(Box::new(range))
|
||||||
}
|
}
|
||||||
_ => unreachable!("internal Ludus error"),
|
_ => unreachable!("internal Ludus error"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn slice<'src>(ordered: &Value<'src>, start: &Value<'src>, end: &Value<'src>) -> Value<'src> {
|
pub fn slice(ordered: &Value, start: &Value, end: &Value) -> Value {
|
||||||
match (ordered, start, end) {
|
match (ordered, start, end) {
|
||||||
(Value::List(list), Value::Number(start), Value::Number(end)) => {
|
(Value::List(list), Value::Number(start), Value::Number(end)) => {
|
||||||
let mut newlist = list.clone();
|
let mut newlist = list.clone();
|
||||||
let start = std::cmp::max(*start as usize, 0);
|
let start = std::cmp::max(*start as usize, 0);
|
||||||
let end = std::cmp::min(*end as usize, list.len());
|
let end = std::cmp::min(*end as usize, list.len());
|
||||||
Value::List(newlist.slice(start..end))
|
Value::List(Box::new(newlist.slice(start..end)))
|
||||||
}
|
}
|
||||||
// TODO: figure out something better to do than return an empty string on a bad slice
|
// TODO: figure out something better to do than return an empty string on a bad slice
|
||||||
(Value::AllocatedString(string), Value::Number(start), Value::Number(end)) => {
|
(Value::String(string), Value::Number(start), Value::Number(end)) => {
|
||||||
let start = std::cmp::max(*start as usize, 0);
|
let start = std::cmp::max(*start as usize, 0);
|
||||||
let end = std::cmp::min(*end as usize, string.len());
|
let end = std::cmp::min(*end as usize, string.len());
|
||||||
Value::AllocatedString(Rc::new(
|
Value::String(Rc::new(
|
||||||
string
|
string
|
||||||
.clone()
|
.clone()
|
||||||
.as_str()
|
.as_str()
|
||||||
|
@ -330,19 +321,19 @@ pub fn slice<'src>(ordered: &Value<'src>, start: &Value<'src>, end: &Value<'src>
|
||||||
.to_string(),
|
.to_string(),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
(Value::InternedString(string), Value::Number(start), Value::Number(end)) => {
|
(Value::Interned(string), Value::Number(start), Value::Number(end)) => {
|
||||||
let start = std::cmp::max(*start as usize, 0);
|
let start = std::cmp::max(*start as usize, 0);
|
||||||
let end = std::cmp::min(*end as usize, string.len());
|
let end = std::cmp::min(*end as usize, string.len());
|
||||||
Value::AllocatedString(Rc::new(string.get(start..end).unwrap_or("").to_string()))
|
Value::String(Rc::new(string.get(start..end).unwrap_or("").to_string()))
|
||||||
}
|
}
|
||||||
_ => unreachable!("internal Ludus error"),
|
_ => unreachable!("internal Ludus error"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn list<'src>(x: &Value<'src>) -> Value<'src> {
|
pub fn list(x: &Value) -> Value {
|
||||||
match x {
|
match x {
|
||||||
Value::List(_) => x.clone(),
|
Value::List(_) => x.clone(),
|
||||||
Value::Tuple(t) => Value::List(Vector::from_iter(t.iter().cloned())),
|
Value::Tuple(t) => Value::List(Box::new(Vector::from_iter(t.iter().cloned()))),
|
||||||
Value::Dict(d) => {
|
Value::Dict(d) => {
|
||||||
let kvs = d.iter();
|
let kvs = d.iter();
|
||||||
let mut list = vector![];
|
let mut list = vector![];
|
||||||
|
@ -350,292 +341,292 @@ pub fn list<'src>(x: &Value<'src>) -> Value<'src> {
|
||||||
let kv = Value::Tuple(Rc::new(vec![Value::Keyword(key), value.clone()]));
|
let kv = Value::Tuple(Rc::new(vec![Value::Keyword(key), value.clone()]));
|
||||||
list.push_back(kv);
|
list.push_back(kv);
|
||||||
}
|
}
|
||||||
Value::List(list)
|
Value::List(Box::new(list))
|
||||||
}
|
}
|
||||||
_ => Value::List(vector![x.clone()]),
|
_ => Value::List(Box::new(vector![x.clone()])),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn number<'src>(x: &Value<'src>) -> Value<'src> {
|
pub fn number(x: &Value) -> Value {
|
||||||
match x {
|
match x {
|
||||||
Value::InternedString(string) => match string.parse::<f64>() {
|
Value::Interned(string) => match string.parse::<f64>() {
|
||||||
Ok(n) => Value::Tuple(Rc::new(vec![Value::Keyword("ok"), Value::Number(n)])),
|
Ok(n) => Value::Tuple(Rc::new(vec![Value::Keyword("ok"), Value::Number(n)])),
|
||||||
Err(_) => Value::Tuple(Rc::new(vec![
|
Err(_) => Value::Tuple(Rc::new(vec![
|
||||||
Value::Keyword("err"),
|
Value::Keyword("err"),
|
||||||
Value::AllocatedString(Rc::new(format!("could not parse `{string}` as a number"))),
|
Value::String(Rc::new(format!("could not parse `{string}` as a number"))),
|
||||||
])),
|
])),
|
||||||
},
|
},
|
||||||
Value::AllocatedString(string) => match string.parse::<f64>() {
|
Value::String(string) => match string.parse::<f64>() {
|
||||||
Ok(n) => Value::Tuple(Rc::new(vec![Value::Keyword("ok"), Value::Number(n)])),
|
Ok(n) => Value::Tuple(Rc::new(vec![Value::Keyword("ok"), Value::Number(n)])),
|
||||||
Err(_) => Value::Tuple(Rc::new(vec![
|
Err(_) => Value::Tuple(Rc::new(vec![
|
||||||
Value::Keyword("err"),
|
Value::Keyword("err"),
|
||||||
Value::AllocatedString(Rc::new(format!("could not parse `{string}` as a number"))),
|
Value::String(Rc::new(format!("could not parse `{string}` as a number"))),
|
||||||
])),
|
])),
|
||||||
},
|
},
|
||||||
_ => unreachable!("internal Ludus error"),
|
_ => unreachable!("internal Ludus error"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn r#type<'src>(x: &Value<'src>) -> Value<'src> {
|
pub fn r#type(x: &Value) -> Value {
|
||||||
match x {
|
match x {
|
||||||
Value::Nil => Value::Keyword("nil"),
|
Value::Nil => Value::Keyword("nil"),
|
||||||
Value::Number(_) => Value::Keyword("number"),
|
Value::Number(_) => Value::Keyword("number"),
|
||||||
Value::Boolean(_) => Value::Keyword("boolean"),
|
Value::True | Value::False => Value::Keyword("bool"),
|
||||||
Value::Keyword(_) => Value::Keyword("keyword"),
|
Value::Keyword(_) => Value::Keyword("keyword"),
|
||||||
Value::Tuple(_) => Value::Keyword("tuple"),
|
Value::Tuple(_) => Value::Keyword("tuple"),
|
||||||
Value::InternedString(_) => Value::Keyword("string"),
|
Value::Interned(_) => Value::Keyword("string"),
|
||||||
Value::AllocatedString(_) => Value::Keyword("string"),
|
Value::String(_) => Value::Keyword("string"),
|
||||||
Value::List(_) => Value::Keyword("list"),
|
Value::List(_) => Value::Keyword("list"),
|
||||||
Value::Dict(_) => Value::Keyword("dict"),
|
Value::Dict(_) => Value::Keyword("dict"),
|
||||||
Value::Fn(_) => Value::Keyword("fn"),
|
Value::Fn(_) => Value::Keyword("fn"),
|
||||||
Value::Box(_, _) => Value::Keyword("box"),
|
Value::Box(_) => Value::Keyword("box"),
|
||||||
Value::Placeholder => unreachable!("internal Ludus error"),
|
Value::BaseFn(_) => Value::Keyword("fn"),
|
||||||
Value::Args(_) => unreachable!("internal Ludus error"),
|
Value::Partial(_) => Value::Keyword("fn"),
|
||||||
Value::Base(_) => Value::Keyword("fn"),
|
Value::Nothing => unreachable!(),
|
||||||
Value::Recur(..) => unreachable!("internal Ludus error"),
|
|
||||||
Value::FnDecl(_) => Value::Keyword("fn"),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn split<'src>(source: &Value<'src>, splitter: &Value) -> Value<'src> {
|
pub fn split(source: &Value, splitter: &Value) -> Value {
|
||||||
match (source, splitter) {
|
match (source, splitter) {
|
||||||
(Value::AllocatedString(source), Value::AllocatedString(splitter)) => {
|
(Value::String(source), Value::String(splitter)) => {
|
||||||
|
println!("splitting {source} with {splitter}");
|
||||||
let parts = source.split_terminator(splitter.as_str());
|
let parts = source.split_terminator(splitter.as_str());
|
||||||
let mut list = vector![];
|
let mut list = vector![];
|
||||||
for part in parts {
|
for part in parts {
|
||||||
list.push_back(Value::AllocatedString(Rc::new(part.to_string())));
|
list.push_back(Value::String(Rc::new(part.to_string())));
|
||||||
}
|
}
|
||||||
Value::List(list)
|
Value::List(Box::new(list))
|
||||||
}
|
}
|
||||||
(Value::AllocatedString(source), Value::InternedString(splitter)) => {
|
(Value::String(_), Value::Interned(splitter)) => {
|
||||||
let parts = source.split_terminator(splitter);
|
split(source, &Value::String(Rc::new(splitter.to_string())))
|
||||||
let mut list = vector![];
|
|
||||||
for part in parts {
|
|
||||||
list.push_back(Value::AllocatedString(Rc::new(part.to_string())));
|
|
||||||
}
|
|
||||||
Value::List(list)
|
|
||||||
}
|
}
|
||||||
(Value::InternedString(source), Value::AllocatedString(splitter)) => {
|
(Value::Interned(source), Value::String(_)) => {
|
||||||
let parts = source.split_terminator(splitter.as_str());
|
split(&Value::String(Rc::new(source.to_string())), splitter)
|
||||||
let mut list = vector![];
|
|
||||||
for part in parts {
|
|
||||||
list.push_back(Value::AllocatedString(Rc::new(part.to_string())));
|
|
||||||
}
|
|
||||||
Value::List(list)
|
|
||||||
}
|
}
|
||||||
(Value::InternedString(source), Value::InternedString(splitter)) => {
|
(Value::Interned(source), Value::Interned(splitter)) => {
|
||||||
let parts = source.split_terminator(splitter);
|
let source = Value::String(Rc::new(source.to_string()));
|
||||||
let mut list = vector![];
|
let splitter = Value::String(Rc::new(splitter.to_string()));
|
||||||
for part in parts {
|
split(&source, &splitter)
|
||||||
list.push_back(Value::AllocatedString(Rc::new(part.to_string())));
|
|
||||||
}
|
|
||||||
Value::List(list)
|
|
||||||
}
|
}
|
||||||
_ => unreachable!("internal Ludus error"),
|
_ => unreachable!("internal Ludus error"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn upcase<'src>(string: &Value<'src>) -> Value<'src> {
|
pub fn upcase(string: &Value) -> Value {
|
||||||
match string {
|
match string {
|
||||||
Value::AllocatedString(string) => Value::AllocatedString(Rc::new(string.to_uppercase())),
|
Value::String(string) => Value::String(Rc::new(string.to_uppercase())),
|
||||||
Value::InternedString(string) => Value::AllocatedString(Rc::new(string.to_uppercase())),
|
Value::Interned(string) => Value::String(Rc::new(string.to_uppercase())),
|
||||||
_ => unreachable!("internal Ludus error"),
|
_ => unreachable!("internal Ludus error"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn downcase<'src>(string: &Value<'src>) -> Value<'src> {
|
pub fn downcase(string: &Value) -> Value {
|
||||||
match string {
|
match string {
|
||||||
Value::AllocatedString(string) => Value::AllocatedString(Rc::new(string.to_lowercase())),
|
Value::String(string) => Value::String(Rc::new(string.to_lowercase())),
|
||||||
Value::InternedString(string) => Value::AllocatedString(Rc::new(string.to_lowercase())),
|
Value::Interned(string) => Value::String(Rc::new(string.to_lowercase())),
|
||||||
_ => unreachable!("internal Ludus error"),
|
_ => unreachable!("internal Ludus error"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn trim<'src>(string: &Value<'src>) -> Value<'src> {
|
pub fn trim(string: &Value) -> Value {
|
||||||
match string {
|
match string {
|
||||||
Value::AllocatedString(string) => {
|
Value::String(string) => Value::String(Rc::new(string.trim().to_string())),
|
||||||
Value::AllocatedString(Rc::new(string.trim().to_string()))
|
Value::Interned(string) => Value::String(Rc::new(string.trim().to_string())),
|
||||||
}
|
|
||||||
Value::InternedString(string) => Value::AllocatedString(Rc::new(string.trim().to_string())),
|
|
||||||
_ => unreachable!("internal Ludus error"),
|
_ => unreachable!("internal Ludus error"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn triml<'src>(string: &Value<'src>) -> Value<'src> {
|
pub fn triml(string: &Value) -> Value {
|
||||||
match string {
|
match string {
|
||||||
Value::AllocatedString(string) => {
|
Value::String(string) => Value::String(Rc::new(string.trim_start().to_string())),
|
||||||
Value::AllocatedString(Rc::new(string.trim_start().to_string()))
|
Value::Interned(string) => Value::String(Rc::new(string.trim_start().to_string())),
|
||||||
}
|
|
||||||
Value::InternedString(string) => {
|
|
||||||
Value::AllocatedString(Rc::new(string.trim_start().to_string()))
|
|
||||||
}
|
|
||||||
_ => unreachable!("internal Ludus error"),
|
_ => unreachable!("internal Ludus error"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn trimr<'src>(string: &Value<'src>) -> Value<'src> {
|
pub fn trimr(string: &Value) -> Value {
|
||||||
match string {
|
match string {
|
||||||
Value::AllocatedString(string) => {
|
Value::String(string) => Value::String(Rc::new(string.trim_end().to_string())),
|
||||||
Value::AllocatedString(Rc::new(string.trim_end().to_string()))
|
Value::Interned(string) => Value::String(Rc::new(string.trim_end().to_string())),
|
||||||
}
|
|
||||||
Value::InternedString(string) => {
|
|
||||||
Value::AllocatedString(Rc::new(string.trim_end().to_string()))
|
|
||||||
}
|
|
||||||
_ => unreachable!("internal Ludus error"),
|
_ => unreachable!("internal Ludus error"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn atan_2<'src>(x: &Value, y: &Value) -> Value<'src> {
|
pub fn atan_2(x: &Value, y: &Value) -> Value {
|
||||||
match (x, y) {
|
match (x, y) {
|
||||||
(Value::Number(x), Value::Number(y)) => Value::Number(x.atan2(*y)),
|
(Value::Number(x), Value::Number(y)) => Value::Number(x.atan2(*y)),
|
||||||
_ => unreachable!("internal Ludus error"),
|
_ => unreachable!("internal Ludus error"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ceil<'src>(x: &Value) -> Value<'src> {
|
pub fn ceil(x: &Value) -> Value {
|
||||||
match x {
|
match x {
|
||||||
Value::Number(x) => Value::Number(x.ceil()),
|
Value::Number(x) => Value::Number(x.ceil()),
|
||||||
_ => unreachable!("internal Ludus error"),
|
_ => unreachable!("internal Ludus error"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn cos<'src>(x: &Value) -> Value<'src> {
|
pub fn cos(x: &Value) -> Value {
|
||||||
match x {
|
match x {
|
||||||
Value::Number(x) => Value::Number(x.cos()),
|
Value::Number(x) => Value::Number(x.cos()),
|
||||||
_ => unreachable!("internal Ludus error"),
|
_ => unreachable!("internal Ludus error"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn floor<'src>(x: &Value) -> Value<'src> {
|
pub fn floor(x: &Value) -> Value {
|
||||||
match x {
|
match x {
|
||||||
Value::Number(x) => Value::Number(x.floor()),
|
Value::Number(x) => Value::Number(x.floor()),
|
||||||
_ => unreachable!("internal Ludus error"),
|
_ => unreachable!("internal Ludus error"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn random<'src>() -> Value<'src> {
|
pub fn random() -> Value {
|
||||||
Value::Number(ran_f64())
|
Value::Number(ran_f64())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn round<'src>(x: &Value) -> Value<'src> {
|
pub fn round(x: &Value) -> Value {
|
||||||
match x {
|
match x {
|
||||||
Value::Number(x) => Value::Number(x.round()),
|
Value::Number(x) => Value::Number(x.round()),
|
||||||
_ => unreachable!("internal Ludus error"),
|
_ => unreachable!("internal Ludus error"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sin<'src>(x: &Value) -> Value<'src> {
|
pub fn sin(x: &Value) -> Value {
|
||||||
match x {
|
match x {
|
||||||
Value::Number(x) => Value::Number(x.sin()),
|
Value::Number(x) => Value::Number(x.sin()),
|
||||||
_ => unreachable!("internal Ludus error"),
|
_ => unreachable!("internal Ludus error"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sqrt<'src>(x: &Value) -> Value<'src> {
|
pub fn sqrt(x: &Value) -> Value {
|
||||||
match x {
|
match x {
|
||||||
Value::Number(x) => Value::Number(x.sqrt()),
|
Value::Number(x) => Value::Number(x.sqrt()),
|
||||||
_ => unreachable!("internal Ludus error"),
|
_ => unreachable!("internal Ludus error"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn tan<'src>(x: &Value) -> Value<'src> {
|
pub fn tan(x: &Value) -> Value {
|
||||||
match x {
|
match x {
|
||||||
Value::Number(x) => Value::Number(x.tan()),
|
Value::Number(x) => Value::Number(x.tan()),
|
||||||
_ => unreachable!("internal Ludus error"),
|
_ => unreachable!("internal Ludus error"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn gt<'src>(x: &Value, y: &Value) -> Value<'src> {
|
pub fn gt(x: &Value, y: &Value) -> Value {
|
||||||
match (x, y) {
|
match (x, y) {
|
||||||
(Value::Number(x), Value::Number(y)) => Value::Boolean(x > y),
|
(Value::Number(x), Value::Number(y)) => {
|
||||||
|
if x > y {
|
||||||
|
Value::True
|
||||||
|
} else {
|
||||||
|
Value::False
|
||||||
|
}
|
||||||
|
}
|
||||||
_ => unreachable!("internal Ludus error"),
|
_ => unreachable!("internal Ludus error"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn gte<'src>(x: &Value, y: &Value) -> Value<'src> {
|
pub fn gte(x: &Value, y: &Value) -> Value {
|
||||||
match (x, y) {
|
match (x, y) {
|
||||||
(Value::Number(x), Value::Number(y)) => Value::Boolean(x >= y),
|
(Value::Number(x), Value::Number(y)) => {
|
||||||
|
if x >= y {
|
||||||
|
Value::True
|
||||||
|
} else {
|
||||||
|
Value::False
|
||||||
|
}
|
||||||
|
}
|
||||||
_ => unreachable!("internal Ludus error"),
|
_ => unreachable!("internal Ludus error"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn lt<'src>(x: &Value, y: &Value) -> Value<'src> {
|
pub fn lt(x: &Value, y: &Value) -> Value {
|
||||||
match (x, y) {
|
match (x, y) {
|
||||||
(Value::Number(x), Value::Number(y)) => Value::Boolean(x < y),
|
(Value::Number(x), Value::Number(y)) => {
|
||||||
|
if x < y {
|
||||||
|
Value::True
|
||||||
|
} else {
|
||||||
|
Value::False
|
||||||
|
}
|
||||||
|
}
|
||||||
_ => unreachable!("internal Ludus error"),
|
_ => unreachable!("internal Ludus error"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn lte<'src>(x: &Value, y: &Value) -> Value<'src> {
|
pub fn lte(x: &Value, y: &Value) -> Value {
|
||||||
match (x, y) {
|
match (x, y) {
|
||||||
(Value::Number(x), Value::Number(y)) => Value::Boolean(x <= y),
|
(Value::Number(x), Value::Number(y)) => {
|
||||||
|
if x <= y {
|
||||||
|
Value::True
|
||||||
|
} else {
|
||||||
|
Value::False
|
||||||
|
}
|
||||||
|
}
|
||||||
_ => unreachable!("internal Ludus error"),
|
_ => unreachable!("internal Ludus error"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn r#mod<'src>(x: &Value, y: &Value) -> Value<'src> {
|
pub fn r#mod(x: &Value, y: &Value) -> Value {
|
||||||
match (x, y) {
|
match (x, y) {
|
||||||
(Value::Number(x), Value::Number(y)) => Value::Number(x % y),
|
(Value::Number(x), Value::Number(y)) => Value::Number(x % y),
|
||||||
_ => unreachable!("internal Ludus error"),
|
_ => unreachable!("internal Ludus error"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn base<'src>() -> Vec<(String, Value<'src>)> {
|
pub fn make_base() -> Value {
|
||||||
let members = vec![
|
let members = vec![
|
||||||
("add", Value::Base(BaseFn::Binary(add))),
|
("add", Value::BaseFn(BaseFn::Binary("add", add))),
|
||||||
("and", Value::Base(BaseFn::Binary(and))),
|
("append", Value::BaseFn(BaseFn::Binary("append", append))),
|
||||||
("append", Value::Base(BaseFn::Binary(append))),
|
("assoc", Value::BaseFn(BaseFn::Ternary("assoc", assoc))),
|
||||||
("assoc", Value::Base(BaseFn::Ternary(assoc))),
|
("at", Value::BaseFn(BaseFn::Binary("at", at))),
|
||||||
("at", Value::Base(BaseFn::Binary(at))),
|
("atan_2", Value::BaseFn(BaseFn::Binary("atan_2", atan_2))),
|
||||||
("atan_2", Value::Base(BaseFn::Binary(atan_2))),
|
("bool", Value::BaseFn(BaseFn::Unary("bool", r#bool))),
|
||||||
("bool", Value::Base(BaseFn::Unary(r#bool))),
|
("ceil", Value::BaseFn(BaseFn::Unary("ceil", ceil))),
|
||||||
("ceil", Value::Base(BaseFn::Unary(ceil))),
|
("chars", Value::BaseFn(BaseFn::Unary("chars", chars))),
|
||||||
("chars", Value::Base(BaseFn::Unary(chars))),
|
("concat", Value::BaseFn(BaseFn::Binary("concat", concat))),
|
||||||
("concat", Value::Base(BaseFn::Binary(concat))),
|
("cos", Value::BaseFn(BaseFn::Unary("cos", cos))),
|
||||||
("cos", Value::Base(BaseFn::Unary(cos))),
|
("count", Value::BaseFn(BaseFn::Unary("count", count))),
|
||||||
("count", Value::Base(BaseFn::Unary(count))),
|
("dec", Value::BaseFn(BaseFn::Unary("dec", dec))),
|
||||||
("dec", Value::Base(BaseFn::Unary(dec))),
|
("dissoc", Value::BaseFn(BaseFn::Binary("dissoc", dissoc))),
|
||||||
("dissoc", Value::Base(BaseFn::Binary(dissoc))),
|
("div", Value::BaseFn(BaseFn::Binary("div", div))),
|
||||||
("div", Value::Base(BaseFn::Binary(div))),
|
("doc!", Value::BaseFn(BaseFn::Unary("doc!", doc))),
|
||||||
("doc!", Value::Base(BaseFn::Unary(doc))),
|
(
|
||||||
("downcase", Value::Base(BaseFn::Unary(downcase))),
|
"downcase",
|
||||||
("eq?", Value::Base(BaseFn::Binary(eq))),
|
Value::BaseFn(BaseFn::Unary("downcase", downcase)),
|
||||||
("first", Value::Base(BaseFn::Unary(first))),
|
),
|
||||||
("floor", Value::Base(BaseFn::Unary(floor))),
|
("eq?", Value::BaseFn(BaseFn::Binary("eq?", eq))),
|
||||||
("get", Value::Base(BaseFn::Binary(get))),
|
("first", Value::BaseFn(BaseFn::Unary("first", first))),
|
||||||
("gt?", Value::Base(BaseFn::Binary(gt))),
|
("floor", Value::BaseFn(BaseFn::Unary("floor", floor))),
|
||||||
("gte?", Value::Base(BaseFn::Binary(gte))),
|
("get", Value::BaseFn(BaseFn::Binary("get", get))),
|
||||||
("inc", Value::Base(BaseFn::Unary(inc))),
|
("gt?", Value::BaseFn(BaseFn::Binary("gt?", gt))),
|
||||||
("last", Value::Base(BaseFn::Unary(last))),
|
("gte?", Value::BaseFn(BaseFn::Binary("gte?", gte))),
|
||||||
("list", Value::Base(BaseFn::Unary(list))),
|
("inc", Value::BaseFn(BaseFn::Unary("inc", inc))),
|
||||||
("lt?", Value::Base(BaseFn::Binary(lt))),
|
("last", Value::BaseFn(BaseFn::Unary("last", last))),
|
||||||
("lte?", Value::Base(BaseFn::Binary(lte))),
|
("list", Value::BaseFn(BaseFn::Unary("list", list))),
|
||||||
("mod", Value::Base(BaseFn::Binary(r#mod))),
|
("lt?", Value::BaseFn(BaseFn::Binary("lt?", lt))),
|
||||||
("mult", Value::Base(BaseFn::Binary(mult))),
|
("lte?", Value::BaseFn(BaseFn::Binary("lte?", lte))),
|
||||||
("number", Value::Base(BaseFn::Unary(number))),
|
("mod", Value::BaseFn(BaseFn::Binary("mod", r#mod))),
|
||||||
("or", Value::Base(BaseFn::Binary(or))),
|
("mult", Value::BaseFn(BaseFn::Binary("mult", mult))),
|
||||||
|
("number", Value::BaseFn(BaseFn::Unary("number", number))),
|
||||||
("pi", Value::Number(std::f64::consts::PI)),
|
("pi", Value::Number(std::f64::consts::PI)),
|
||||||
("print!", Value::Base(BaseFn::Unary(print))),
|
("print!", Value::BaseFn(BaseFn::Unary("print!", print))),
|
||||||
("random", Value::Base(BaseFn::Nullary(random))),
|
("random", Value::BaseFn(BaseFn::Nullary("random", random))),
|
||||||
("range", Value::Base(BaseFn::Binary(range))),
|
("range", Value::BaseFn(BaseFn::Binary("range", range))),
|
||||||
("rest", Value::Base(BaseFn::Unary(rest))),
|
("rest", Value::BaseFn(BaseFn::Unary("rest", rest))),
|
||||||
("round", Value::Base(BaseFn::Unary(round))),
|
("round", Value::BaseFn(BaseFn::Unary("round", round))),
|
||||||
("show", Value::Base(BaseFn::Unary(show))),
|
("show", Value::BaseFn(BaseFn::Unary("show", show))),
|
||||||
("sin", Value::Base(BaseFn::Unary(sin))),
|
("sin", Value::BaseFn(BaseFn::Unary("sin", sin))),
|
||||||
("slice", Value::Base(BaseFn::Ternary(slice))),
|
("slice", Value::BaseFn(BaseFn::Ternary("slice", slice))),
|
||||||
("split", Value::Base(BaseFn::Binary(split))),
|
("split", Value::BaseFn(BaseFn::Binary("split", split))),
|
||||||
("sqrt", Value::Base(BaseFn::Unary(sqrt))),
|
("sqrt", Value::BaseFn(BaseFn::Unary("sqrt", sqrt))),
|
||||||
("sqrt_2", Value::Number(std::f64::consts::SQRT_2)),
|
("sqrt_2", Value::Number(std::f64::consts::SQRT_2)),
|
||||||
("store!", Value::Base(BaseFn::Binary(store))),
|
("store!", Value::BaseFn(BaseFn::Binary("store!", store))),
|
||||||
("sub", Value::Base(BaseFn::Binary(sub))),
|
("sub", Value::BaseFn(BaseFn::Binary("sub", sub))),
|
||||||
("tan", Value::Base(BaseFn::Unary(tan))),
|
("tan", Value::BaseFn(BaseFn::Unary("tan", tan))),
|
||||||
("trim", Value::Base(BaseFn::Unary(trim))),
|
("trim", Value::BaseFn(BaseFn::Unary("trim", trim))),
|
||||||
("triml", Value::Base(BaseFn::Unary(triml))),
|
("triml", Value::BaseFn(BaseFn::Unary("triml", triml))),
|
||||||
("trimr", Value::Base(BaseFn::Unary(trimr))),
|
("trimr", Value::BaseFn(BaseFn::Unary("trimr", trimr))),
|
||||||
("type", Value::Base(BaseFn::Unary(r#type))),
|
("type", Value::BaseFn(BaseFn::Unary("type", r#type))),
|
||||||
("unbox", Value::Base(BaseFn::Unary(unbox))),
|
("unbox", Value::BaseFn(BaseFn::Unary("unbox", unbox))),
|
||||||
("upcase", Value::Base(BaseFn::Unary(upcase))),
|
("upcase", Value::BaseFn(BaseFn::Unary("upcase", upcase))),
|
||||||
];
|
];
|
||||||
let pkg = Value::Dict(HashMap::from(members));
|
Value::Dict(Box::new(HashMap::from(members)))
|
||||||
vec![("base".to_string(), pkg)]
|
|
||||||
}
|
}
|
||||||
|
|
108
src/chunk.rs
Normal file
108
src/chunk.rs
Normal file
|
@ -0,0 +1,108 @@
|
||||||
|
use crate::op::Op;
|
||||||
|
use crate::parser::Ast;
|
||||||
|
use crate::spans::Spanned;
|
||||||
|
use crate::value::Value;
|
||||||
|
use chumsky::prelude::SimpleSpan;
|
||||||
|
use imbl::HashMap;
|
||||||
|
use num_traits::FromPrimitive;
|
||||||
|
use regex::Regex;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct StrPattern {
|
||||||
|
pub words: Vec<String>,
|
||||||
|
pub re: Regex,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct Chunk {
|
||||||
|
pub constants: Vec<Value>,
|
||||||
|
pub bytecode: Vec<u8>,
|
||||||
|
pub nodes: Vec<&'static Ast>,
|
||||||
|
pub spans: Vec<SimpleSpan>,
|
||||||
|
pub keywords: Vec<&'static str>,
|
||||||
|
pub string_patterns: Vec<StrPattern>,
|
||||||
|
pub env: HashMap<&'static str, Value>,
|
||||||
|
pub msgs: Vec<String>,
|
||||||
|
pub src: &'static str,
|
||||||
|
pub input: &'static str,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Chunk {
|
||||||
|
pub fn new(env: HashMap<&'static str, Value>, src: &'static str, input: &'static str) -> Chunk {
|
||||||
|
Chunk {
|
||||||
|
constants: vec![],
|
||||||
|
bytecode: vec![],
|
||||||
|
nodes: vec![],
|
||||||
|
spans: vec![],
|
||||||
|
keywords: vec![],
|
||||||
|
string_patterns: vec![],
|
||||||
|
env,
|
||||||
|
msgs: vec![],
|
||||||
|
src,
|
||||||
|
input,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn dissasemble_instr(&self, i: &mut usize) {
|
||||||
|
let op = Op::from_u8(self.bytecode[*i]).unwrap();
|
||||||
|
use Op::*;
|
||||||
|
match op {
|
||||||
|
Pop | Store | Stash | Load | Nil | True | False | MatchNil | MatchTrue | MatchFalse
|
||||||
|
| PanicIfNoMatch | ResetMatch | GetKey | PanicNoWhen | PanicNoMatch | TypeOf
|
||||||
|
| Duplicate | Decrement | ToInt | Noop | LoadTuple | LoadList | Eq | Add | Sub
|
||||||
|
| Mult | Div | Unbox | BoxStore | Assert | Get | At | Not | Panic | EmptyString
|
||||||
|
| ConcatStrings | Stringify | MatchType | Return | UnconditionalMatch | Print
|
||||||
|
| AppendList | ConcatList | PushList | PushDict | AppendDict | ConcatDict | Nothing
|
||||||
|
| PushGlobal | SetUpvalue => {
|
||||||
|
println!("{i:04}: {op}")
|
||||||
|
}
|
||||||
|
Constant | MatchConstant => {
|
||||||
|
let high = self.bytecode[*i + 1];
|
||||||
|
let low = self.bytecode[*i + 2];
|
||||||
|
let idx = ((high as usize) << 8) + low as usize;
|
||||||
|
let value = &self.constants[idx].show();
|
||||||
|
println!("{i:04}: {:16} {idx:05}: {value}", op.to_string());
|
||||||
|
*i += 2;
|
||||||
|
}
|
||||||
|
Msg => {
|
||||||
|
let msg_idx = self.bytecode[*i + 1];
|
||||||
|
let msg = &self.msgs[msg_idx as usize];
|
||||||
|
println!("{i:04}: {msg}");
|
||||||
|
*i += 1;
|
||||||
|
}
|
||||||
|
PushBinding | MatchTuple | MatchSplattedTuple | LoadSplattedTuple | MatchList
|
||||||
|
| MatchSplattedList | LoadSplattedList | MatchDict | MatchSplattedDict
|
||||||
|
| DropDictEntry | LoadDictValue | PushTuple | PushBox | MatchDepth | PopN | StoreN
|
||||||
|
| Call | GetUpvalue | Partial | MatchString | PushStringMatches | TailCall | LoadN => {
|
||||||
|
let next = self.bytecode[*i + 1];
|
||||||
|
println!("{i:04}: {:16} {next:03}", op.to_string());
|
||||||
|
*i += 1;
|
||||||
|
}
|
||||||
|
Jump | JumpIfFalse | JumpIfTrue | JumpIfNoMatch | JumpIfMatch | JumpBack
|
||||||
|
| JumpIfZero => {
|
||||||
|
let high = self.bytecode[*i + 1];
|
||||||
|
let low = self.bytecode[*i + 2];
|
||||||
|
let len = ((high as u16) << 8) + low as u16;
|
||||||
|
println!("{i:04}: {:16} {len:05}", op.to_string());
|
||||||
|
*i += 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn dissasemble(&self) {
|
||||||
|
println!("IDX | CODE | INFO");
|
||||||
|
let mut i = 0;
|
||||||
|
while i < self.bytecode.len() {
|
||||||
|
self.dissasemble_instr(&mut i);
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// pub fn kw_from(&self, kw: &str) -> Option<Value> {
|
||||||
|
// self.kw_index_from(kw).map(Value::Keyword)
|
||||||
|
// }
|
||||||
|
|
||||||
|
// pub fn kw_index_from(&self, kw: &str) -> Option<usize> {
|
||||||
|
// self.keywords.iter().position(|s| *s == kw)
|
||||||
|
// }
|
||||||
|
}
|
1437
src/compiler.rs
Normal file
1437
src/compiler.rs
Normal file
File diff suppressed because it is too large
Load Diff
157
src/errors.rs
157
src/errors.rs
|
@ -1,48 +1,129 @@
|
||||||
use crate::process::{LErr, Trace};
|
// use crate::process::{LErr, Trace};
|
||||||
|
use crate::parser::Ast;
|
||||||
|
use crate::spans::Spanned;
|
||||||
use crate::validator::VErr;
|
use crate::validator::VErr;
|
||||||
use crate::value::Value;
|
use crate::value::Value;
|
||||||
|
use crate::vm::CallFrame;
|
||||||
use ariadne::{sources, Color, Label, Report, ReportKind};
|
use ariadne::{sources, Color, Label, Report, ReportKind};
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
|
||||||
pub fn report_panic(err: LErr) {
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
let mut srcs = HashSet::new();
|
pub enum PanicInfo {
|
||||||
let mut stack = vec![];
|
RepeatNumberType(Value),
|
||||||
let mut order = 1;
|
NoMatch,
|
||||||
for entry in err.trace.iter().rev() {
|
LetMatchFail,
|
||||||
let Trace {
|
MatchFail,
|
||||||
callee,
|
FnMatchFail,
|
||||||
caller,
|
Internal(&'static str),
|
||||||
function,
|
ListSplat(Value),
|
||||||
arguments,
|
DictSplat(Value),
|
||||||
input,
|
DecType(Value),
|
||||||
src,
|
WhenFail,
|
||||||
} = entry;
|
AddType(Value, Value),
|
||||||
let (_, first_span) = callee;
|
SubType(Value, Value),
|
||||||
let (_, second_span) = caller;
|
MultType(Value, Value),
|
||||||
let Value::Fn(f) = function else {
|
DivType(Value, Value),
|
||||||
unreachable!()
|
UnboxType(Value),
|
||||||
};
|
StoreType(Value),
|
||||||
let fn_name = f.name.clone();
|
Assert,
|
||||||
let i = first_span.start;
|
Explicit(Value),
|
||||||
let j = second_span.end;
|
GetType(Value),
|
||||||
let label = Label::new((entry.input, i..j))
|
AtIdxType(Value),
|
||||||
.with_color(Color::Yellow)
|
AtCollectionType(Value),
|
||||||
.with_message(format!("({order}) calling `{fn_name}` with `{arguments}`"));
|
PartialType(Value),
|
||||||
order += 1;
|
ArityMismatch(Value, Vec<Value>),
|
||||||
stack.push(label);
|
CallType(Value),
|
||||||
srcs.insert((*input, *src));
|
DivisionByZero,
|
||||||
}
|
|
||||||
|
|
||||||
Report::build(ReportKind::Error, (err.input, err.span.into_range()))
|
|
||||||
.with_message(format!("Ludus panicked! {}", err.msg))
|
|
||||||
.with_label(Label::new((err.input, err.span.into_range())).with_color(Color::Red))
|
|
||||||
.with_labels(stack)
|
|
||||||
.with_note(err.extra)
|
|
||||||
.finish()
|
|
||||||
.print(sources(srcs.iter().copied()))
|
|
||||||
.unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl PanicInfo {
|
||||||
|
pub fn msg(&self) -> String {
|
||||||
|
use PanicInfo::*;
|
||||||
|
match self {
|
||||||
|
RepeatNumberType(value) => format!("`repeat` expects a number; it got `{value}`"),
|
||||||
|
_ => todo!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
pub struct Panic {
|
||||||
|
pub frame: CallFrame,
|
||||||
|
pub call_stack: Vec<CallFrame>,
|
||||||
|
pub info: PanicInfo,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Panic {
|
||||||
|
pub fn report(&self) {
|
||||||
|
println!("got frame: {}", self.frame);
|
||||||
|
let mut srcs = HashSet::new();
|
||||||
|
let ast_spans = std::iter::zip(
|
||||||
|
self.frame.chunk().spans.iter(),
|
||||||
|
self.frame.chunk().nodes.iter(),
|
||||||
|
);
|
||||||
|
let ast_spans = ast_spans
|
||||||
|
.map(|(span, ast)| format!("{span} || {}", ast.show()))
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join("\n");
|
||||||
|
println!("{ast_spans}");
|
||||||
|
let msg = self.info.msg();
|
||||||
|
let input = self.frame.chunk().input;
|
||||||
|
let src = self.frame.chunk().src;
|
||||||
|
srcs.insert((input, src));
|
||||||
|
let mut traceback = format!("Ludus panicked! {msg}");
|
||||||
|
for frame in self.call_stack.iter().rev() {
|
||||||
|
let name = frame.function.as_fn().name();
|
||||||
|
let args = frame
|
||||||
|
.args
|
||||||
|
.iter()
|
||||||
|
.map(|arg| arg.show())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(", ");
|
||||||
|
traceback = format!("{traceback}\n calling `{name}` with ({args})");
|
||||||
|
}
|
||||||
|
println!("{traceback}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// pub fn report_panic(err: LErr) {
|
||||||
|
// let mut srcs = HashSet::new();
|
||||||
|
// let mut stack = vec![];
|
||||||
|
// let mut order = 1;
|
||||||
|
// for entry in err.trace.iter().rev() {
|
||||||
|
// let Trace {
|
||||||
|
// callee,
|
||||||
|
// caller,
|
||||||
|
// function,
|
||||||
|
// arguments,
|
||||||
|
// input,
|
||||||
|
// src,
|
||||||
|
// } = entry;
|
||||||
|
// let (_, first_span) = callee;
|
||||||
|
// let (_, second_span) = caller;
|
||||||
|
// let Value::Fn(f) = function else {
|
||||||
|
// unreachable!()
|
||||||
|
// };
|
||||||
|
// let fn_name = f.borrow().name.clone();
|
||||||
|
// let i = first_span.start;
|
||||||
|
// let j = second_span.end;
|
||||||
|
// let label = Label::new((entry.input, i..j))
|
||||||
|
// .with_color(Color::Yellow)
|
||||||
|
// .with_message(format!("({order}) calling `{fn_name}` with `{arguments}`"));
|
||||||
|
// order += 1;
|
||||||
|
// stack.push(label);
|
||||||
|
// srcs.insert((*input, *src));
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Report::build(ReportKind::Error, (err.input, err.span.into_range()))
|
||||||
|
// .with_message(format!("Ludus panicked! {}", err.msg))
|
||||||
|
// .with_label(Label::new((err.input, err.span.into_range())).with_color(Color::Red))
|
||||||
|
// .with_labels(stack)
|
||||||
|
// .with_note(err.extra)
|
||||||
|
// .finish()
|
||||||
|
// .print(sources(srcs.iter().copied()))
|
||||||
|
// .unwrap();
|
||||||
|
// }
|
||||||
|
|
||||||
pub fn report_invalidation(errs: Vec<VErr>) {
|
pub fn report_invalidation(errs: Vec<VErr>) {
|
||||||
for err in errs {
|
for err in errs {
|
||||||
Report::build(ReportKind::Error, (err.input, err.span.into_range()))
|
Report::build(ReportKind::Error, (err.input, err.span.into_range()))
|
||||||
|
|
|
@ -56,7 +56,7 @@ pub fn lexer(
|
||||||
"nil" => Token::Nil,
|
"nil" => Token::Nil,
|
||||||
// todo: hard code these as type constructors
|
// todo: hard code these as type constructors
|
||||||
"as" | "box" | "do" | "else" | "fn" | "if" | "let" | "loop" | "match" | "panic!"
|
"as" | "box" | "do" | "else" | "fn" | "if" | "let" | "loop" | "match" | "panic!"
|
||||||
| "recur" | "repeat" | "then" | "when" | "with" => Token::Reserved(word),
|
| "recur" | "repeat" | "then" | "when" | "with" | "or" | "and" => Token::Reserved(word),
|
||||||
_ => Token::Word(word),
|
_ => Token::Word(word),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
321
src/main.rs
321
src/main.rs
|
@ -1,214 +1,211 @@
|
||||||
// an implementation of Ludus
|
|
||||||
|
|
||||||
// curently left undone (and not adding for a while yet):
|
|
||||||
// * sets
|
|
||||||
// * interpolated strings & string patterns
|
|
||||||
// * pkgs, namespaces, imports, `use` forms
|
|
||||||
// * with forms
|
|
||||||
// * test forms
|
|
||||||
// * ignored words
|
|
||||||
|
|
||||||
// todo:
|
|
||||||
// * [x] rewrite fn parser to use chumsky::Recursive::declare/define
|
|
||||||
// - [x] do this to extract/simplify/DRY things like tuple patterns, fn clauses, etc.
|
|
||||||
// * [x] Work around chumsky::Stream::from_iter().spanned disappearing in most recent version
|
|
||||||
// * [x] investigate using labels (which is behind a compiler flag, somehow)
|
|
||||||
// * [ ] write parsing errors
|
|
||||||
// * [ ] wire up Ariadne parsing errors
|
|
||||||
// * [x] add stack traces and code locations to panics
|
|
||||||
// * [x] validation
|
|
||||||
// * [x] break this out into multiple files
|
|
||||||
// * [x] write a tree-walk VM
|
|
||||||
// - [x] learn how to deal with lifetimes
|
|
||||||
// - [x] with stack mechanics and refcounting
|
|
||||||
// - [ ] with tail-call optimization (nb: this may not be possible w/ a TW-VM)
|
|
||||||
// - [ ] with all the necessary forms for current Ludus
|
|
||||||
// * [x] guards in match clauses
|
|
||||||
// * [x] `as` patterns
|
|
||||||
// * [x] splat patterns in tuples, lists, dicts
|
|
||||||
// * [x] splats in list and dict literals
|
|
||||||
// * [x] `loop` and `recur`
|
|
||||||
// * [x] string patterns
|
|
||||||
// * [x] string interpolation
|
|
||||||
// * [x] docstrings
|
|
||||||
// * [x] write `base` in Rust
|
|
||||||
// * [ ] turn this into a library function
|
|
||||||
// * [ ] compile this into WASM
|
|
||||||
// * [ ] perf testing
|
|
||||||
|
|
||||||
use chumsky::{input::Stream, prelude::*};
|
use chumsky::{input::Stream, prelude::*};
|
||||||
use rust_embed::Embed;
|
use imbl::HashMap;
|
||||||
|
use std::env;
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
mod spans;
|
const DEBUG_SCRIPT_COMPILE: bool = true;
|
||||||
|
const DEBUG_SCRIPT_RUN: bool = true;
|
||||||
mod lexer;
|
const DEBUG_PRELUDE_COMPILE: bool = false;
|
||||||
use crate::lexer::*;
|
const DEBUG_PRELUDE_RUN: bool = false;
|
||||||
|
|
||||||
mod value;
|
|
||||||
use crate::value::*;
|
|
||||||
|
|
||||||
mod parser;
|
|
||||||
use crate::parser::*;
|
|
||||||
|
|
||||||
mod base;
|
mod base;
|
||||||
use crate::base::*;
|
|
||||||
|
mod spans;
|
||||||
|
use crate::spans::Spanned;
|
||||||
|
|
||||||
|
mod lexer;
|
||||||
|
use crate::lexer::lexer;
|
||||||
|
|
||||||
|
mod parser;
|
||||||
|
use crate::parser::{parser, Ast};
|
||||||
|
|
||||||
mod validator;
|
mod validator;
|
||||||
use crate::validator::*;
|
use crate::validator::Validator;
|
||||||
|
|
||||||
mod process;
|
|
||||||
use crate::process::*;
|
|
||||||
|
|
||||||
mod errors;
|
mod errors;
|
||||||
use crate::errors::*;
|
use crate::errors::report_invalidation;
|
||||||
|
|
||||||
#[derive(Embed)]
|
mod chunk;
|
||||||
#[folder = "assets/"]
|
mod op;
|
||||||
struct Asset;
|
|
||||||
|
|
||||||
pub fn prelude<'src>() -> (
|
mod compiler;
|
||||||
Vec<(String, Value<'src>)>,
|
use crate::compiler::Compiler;
|
||||||
std::collections::HashMap<*const Ast, FnInfo>,
|
|
||||||
) {
|
|
||||||
let prelude = Asset::get("prelude.ld").unwrap().data.into_owned();
|
|
||||||
// we know for sure Prelude should live through the whole run of the program
|
|
||||||
let leaked = Box::leak(Box::new(prelude));
|
|
||||||
let prelude = std::str::from_utf8(leaked).unwrap();
|
|
||||||
|
|
||||||
let (ptoks, perrs) = lexer().parse(prelude).into_output_errors();
|
mod value;
|
||||||
if !perrs.is_empty() {
|
use value::Value;
|
||||||
println!("Errors lexing Prelude");
|
|
||||||
println!("{:?}", perrs);
|
|
||||||
panic!();
|
|
||||||
}
|
|
||||||
|
|
||||||
let ptoks = ptoks.unwrap();
|
mod vm;
|
||||||
|
use vm::Vm;
|
||||||
|
|
||||||
let (p_ast, perrs) = parser()
|
const PRELUDE: &str = include_str!("../assets/test_prelude.ld");
|
||||||
.parse(Stream::from_iter(ptoks).map((0..prelude.len()).into(), |(t, s)| (t, s)))
|
|
||||||
|
pub fn prelude() -> HashMap<&'static str, Value> {
|
||||||
|
let tokens = lexer().parse(PRELUDE).into_output_errors().0.unwrap();
|
||||||
|
let (parsed, parse_errors) = parser()
|
||||||
|
.parse(Stream::from_iter(tokens).map((0..PRELUDE.len()).into(), |(t, s)| (t, s)))
|
||||||
.into_output_errors();
|
.into_output_errors();
|
||||||
if !perrs.is_empty() {
|
|
||||||
println!("Errors parsing Prelude");
|
if !parse_errors.is_empty() {
|
||||||
println!("{:?}", perrs);
|
println!("ERROR PARSING PRELUDE:");
|
||||||
|
println!("{:?}", parse_errors);
|
||||||
panic!();
|
panic!();
|
||||||
}
|
}
|
||||||
|
|
||||||
let prelude_parsed = Box::leak(Box::new(p_ast.unwrap()));
|
let parsed = parsed.unwrap();
|
||||||
let base_pkg = Box::leak(Box::new(base()));
|
let (ast, span) = &parsed;
|
||||||
|
|
||||||
let mut v6or = Validator::new(
|
let base = base::make_base();
|
||||||
&prelude_parsed.0,
|
let mut base_env = imbl::HashMap::new();
|
||||||
prelude_parsed.1,
|
base_env.insert("base", base.clone());
|
||||||
"prelude",
|
|
||||||
prelude,
|
|
||||||
base_pkg,
|
|
||||||
);
|
|
||||||
v6or.validate();
|
|
||||||
|
|
||||||
if !v6or.errors.is_empty() {
|
let mut validator = Validator::new(ast, span, "prelude", PRELUDE, base_env);
|
||||||
report_invalidation(v6or.errors);
|
|
||||||
panic!("interal Ludus error: invalid prelude")
|
validator.validate();
|
||||||
|
|
||||||
|
if !validator.errors.is_empty() {
|
||||||
|
println!("VALIDATION ERRORS IN PRLUDE:");
|
||||||
|
report_invalidation(validator.errors);
|
||||||
|
panic!();
|
||||||
}
|
}
|
||||||
|
|
||||||
let static_vec = Box::leak(Box::new(vec![]));
|
let parsed: &'static Spanned<Ast> = Box::leak(Box::new(parsed));
|
||||||
|
let mut compiler = Compiler::new(
|
||||||
let mut base_ctx = Process::<'src> {
|
parsed,
|
||||||
input: "prelude",
|
"prelude",
|
||||||
src: prelude,
|
PRELUDE,
|
||||||
locals: base_pkg.clone(),
|
"prelude",
|
||||||
ast: &prelude_parsed.0,
|
0,
|
||||||
span: prelude_parsed.1,
|
HashMap::new(),
|
||||||
prelude: static_vec,
|
DEBUG_PRELUDE_COMPILE,
|
||||||
fn_info: v6or.fn_info,
|
);
|
||||||
};
|
compiler.emit_constant(base);
|
||||||
|
compiler.bind("base");
|
||||||
let prelude = base_ctx.eval();
|
compiler.compile();
|
||||||
|
|
||||||
let mut p_ctx = vec![];
|
|
||||||
|
|
||||||
|
let chunk = compiler.chunk;
|
||||||
|
let mut vm = Vm::new(chunk, DEBUG_PRELUDE_RUN);
|
||||||
|
let prelude = vm.run().clone().unwrap();
|
||||||
match prelude {
|
match prelude {
|
||||||
Ok(Value::Dict(p_dict)) => {
|
Value::Dict(hashmap) => *hashmap,
|
||||||
for (key, value) in p_dict.iter() {
|
_ => unreachable!(),
|
||||||
p_ctx.push((key.to_string(), value.clone()))
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(_) => {
|
|
||||||
println!("Bad Prelude export");
|
|
||||||
panic!();
|
|
||||||
}
|
|
||||||
Err(LErr { msg, .. }) => {
|
|
||||||
println!("Error running Prelude");
|
|
||||||
println!("{:?}", msg);
|
|
||||||
panic!();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
(p_ctx, base_ctx.fn_info)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run(src: &'static str) {
|
pub fn run(src: &'static str, input: &'static str) {
|
||||||
let (tokens, lex_errs) = lexer().parse(src).into_output_errors();
|
let (tokens, lex_errs) = lexer().parse(src).into_output_errors();
|
||||||
if !lex_errs.is_empty() {
|
if !lex_errs.is_empty() {
|
||||||
println!("{:?}", lex_errs);
|
println!("{:?}", lex_errs);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let tokens = tokens.unwrap();
|
let tokens = tokens.unwrap();
|
||||||
let to_parse = tokens.clone();
|
|
||||||
|
|
||||||
let (parse_result, parse_errors) = parser()
|
let (parse_result, parse_errors) = parser()
|
||||||
.parse(Stream::from_iter(to_parse).map((0..src.len()).into(), |(t, s)| (t, s)))
|
.parse(Stream::from_iter(tokens).map((0..src.len()).into(), |(t, s)| (t, s)))
|
||||||
.into_output_errors();
|
.into_output_errors();
|
||||||
if !parse_errors.is_empty() {
|
if !parse_errors.is_empty() {
|
||||||
println!("{:?}", parse_errors);
|
println!("{:?}", parse_errors);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let parsed = parse_result.unwrap();
|
// ::sigh:: The AST should be 'static
|
||||||
|
// This simplifies lifetimes, and
|
||||||
|
// in any event, the AST should live forever
|
||||||
|
let parsed: &'static Spanned<Ast> = Box::leak(Box::new(parse_result.unwrap()));
|
||||||
|
|
||||||
let (prelude_ctx, mut prelude_fn_info) = prelude();
|
let prelude = prelude();
|
||||||
let prelude_ctx = Box::leak(Box::new(prelude_ctx));
|
// let prelude = imbl::HashMap::new();
|
||||||
|
|
||||||
let mut v6or = Validator::new(&parsed.0, parsed.1, "script", src, prelude_ctx);
|
let mut validator = Validator::new(&parsed.0, &parsed.1, input, src, prelude.clone());
|
||||||
|
validator.validate();
|
||||||
|
|
||||||
v6or.validate();
|
if !validator.errors.is_empty() {
|
||||||
|
println!("Ludus found some validation errors:");
|
||||||
if !v6or.errors.is_empty() {
|
report_invalidation(validator.errors);
|
||||||
report_invalidation(v6or.errors);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
prelude_fn_info.extend(&mut v6or.fn_info.into_iter());
|
let mut compiler = Compiler::new(parsed, input, src, input, 0, prelude, DEBUG_SCRIPT_COMPILE);
|
||||||
|
// let base = base::make_base();
|
||||||
|
// compiler.emit_constant(base);
|
||||||
|
// compiler.bind("base");
|
||||||
|
|
||||||
let mut proc = Process {
|
compiler.compile();
|
||||||
input: "script",
|
if DEBUG_SCRIPT_COMPILE {
|
||||||
src,
|
println!("=== source code ===");
|
||||||
locals: vec![],
|
println!("{src}");
|
||||||
prelude: prelude_ctx,
|
compiler.disassemble();
|
||||||
ast: &parsed.0,
|
println!("\n\n")
|
||||||
span: parsed.1,
|
|
||||||
fn_info: prelude_fn_info,
|
|
||||||
};
|
|
||||||
|
|
||||||
let result = proc.eval();
|
|
||||||
|
|
||||||
match result {
|
|
||||||
Ok(result) => println!("{}", result),
|
|
||||||
Err(err) => report_panic(err),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if DEBUG_SCRIPT_RUN {
|
||||||
|
println!("=== vm run ===");
|
||||||
|
}
|
||||||
|
|
||||||
|
let vm_chunk = compiler.chunk;
|
||||||
|
|
||||||
|
let mut vm = Vm::new(vm_chunk, DEBUG_SCRIPT_RUN);
|
||||||
|
let result = vm.run();
|
||||||
|
let output = match result {
|
||||||
|
Ok(val) => val.to_string(),
|
||||||
|
Err(panic) => {
|
||||||
|
panic.report();
|
||||||
|
std::process::exit(1)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if DEBUG_SCRIPT_RUN {
|
||||||
|
vm.print_stack();
|
||||||
|
}
|
||||||
|
println!("{output}");
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn ld_fmt(src: &'static str) -> Result<String, String> {
|
||||||
|
let (tokens, lex_errs) = lexer().parse(src).into_output_errors();
|
||||||
|
if !lex_errs.is_empty() {
|
||||||
|
println!("{:?}", lex_errs);
|
||||||
|
return Err(format!("{:?}", lex_errs));
|
||||||
|
}
|
||||||
|
|
||||||
|
let tokens = tokens.unwrap();
|
||||||
|
|
||||||
|
let (parse_result, parse_errors) = parser()
|
||||||
|
.parse(Stream::from_iter(tokens).map((0..src.len()).into(), |(t, s)| (t, s)))
|
||||||
|
.into_output_errors();
|
||||||
|
if !parse_errors.is_empty() {
|
||||||
|
return Err(format!("{:?}", parse_errors));
|
||||||
|
}
|
||||||
|
|
||||||
|
// ::sigh:: The AST should be 'static
|
||||||
|
// This simplifies lifetimes, and
|
||||||
|
// in any event, the AST should live forever
|
||||||
|
let parsed: &'static Spanned<Ast> = Box::leak(Box::new(parse_result.unwrap()));
|
||||||
|
|
||||||
|
Ok(parsed.0.show())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
let src = "
|
// env::set_var("RUST_BACKTRACE", "1");
|
||||||
fn sum_to {
|
// let src: &'static str = fs::read_to_string("sandbox.ld").unwrap().leak();
|
||||||
(n) -> sum_to (n, 0)
|
// run(src, "sandbox.ld");
|
||||||
(1, acc) -> acc
|
let str = "foo bar baz\nquux frob\nthing thing thing";
|
||||||
(n, acc) -> sum_to (dec (n), add (n, acc))
|
let range = 0..4;
|
||||||
}
|
|
||||||
|
|
||||||
sum_to (10000)
|
println!("{}", range.start);
|
||||||
";
|
|
||||||
run(src);
|
println!("{}", str.get(range).unwrap());
|
||||||
// struct_scalpel::print_dissection_info::<value::Value>()
|
|
||||||
// struct_scalpel::print_dissection_info::<parser::Ast>();
|
let lines: Vec<&str> = str.split_terminator("\n").collect();
|
||||||
// println!("{}", std::mem::size_of::<parser::Ast>())
|
|
||||||
|
println!("{:?}", lines);
|
||||||
|
|
||||||
|
let idx = 20;
|
||||||
|
|
||||||
|
let mut line_no = 1;
|
||||||
|
for i in 0..idx {
|
||||||
|
if str.chars().nth(i).unwrap() == '\n' {
|
||||||
|
line_no += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("line {line_no}: {}", lines[line_no - 1]);
|
||||||
}
|
}
|
||||||
|
|
58
src/memory_sandbox.rs
Normal file
58
src/memory_sandbox.rs
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
use imbl::{HashMap, Vector};
|
||||||
|
use index_vec::Idx;
|
||||||
|
use std::cell::RefCell;
|
||||||
|
use std::ops::Range;
|
||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
struct Word(&'static str);
|
||||||
|
|
||||||
|
struct Keyword(&'static str);
|
||||||
|
|
||||||
|
struct Interned(&'static str);
|
||||||
|
|
||||||
|
enum StringPart {
|
||||||
|
Word(&'static str),
|
||||||
|
Data(&'static str),
|
||||||
|
Inline(&'static str),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
|
struct LBox {
|
||||||
|
name: usize,
|
||||||
|
cell: RefCell<Value>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
|
struct Fn {
|
||||||
|
name: &'static str,
|
||||||
|
body: Vec<String>,
|
||||||
|
//...etc
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
|
enum Value {
|
||||||
|
Nil,
|
||||||
|
Placeholder,
|
||||||
|
Boolean(bool),
|
||||||
|
Keyword(usize),
|
||||||
|
Interned(usize),
|
||||||
|
FnDecl(usize),
|
||||||
|
String(Rc<String>),
|
||||||
|
Number(f64),
|
||||||
|
Tuple(Rc<Vec<Value>>),
|
||||||
|
List(Box<Vector<Value>>),
|
||||||
|
Dict(Box<HashMap<&'static str, Value>>),
|
||||||
|
Box(Rc<LBox>),
|
||||||
|
Fn(Rc<RefCell<Fn>>),
|
||||||
|
}
|
||||||
|
|
||||||
|
fn futz() {
|
||||||
|
let foo: &'static str = "foo";
|
||||||
|
let baz: Vec<u8> = vec![];
|
||||||
|
let bar: Range<usize> = 1..3;
|
||||||
|
let quux: Vector<u8> = Vector::new();
|
||||||
|
let fuzz = Rc::new(quux);
|
||||||
|
let blah = Box::new(foo);
|
||||||
|
let val = Value::Number(12.09);
|
||||||
|
let foo: f64 = 12.0;
|
||||||
|
}
|
212
src/old_main.rs
Normal file
212
src/old_main.rs
Normal file
|
@ -0,0 +1,212 @@
|
||||||
|
// an implementation of Ludus
|
||||||
|
|
||||||
|
// curently left undone (and not adding for a while yet):
|
||||||
|
// * sets
|
||||||
|
// * interpolated strings & string patterns
|
||||||
|
// * pkgs, namespaces, imports, `use` forms
|
||||||
|
// * with forms
|
||||||
|
// * test forms
|
||||||
|
// * ignored words
|
||||||
|
|
||||||
|
// todo:
|
||||||
|
// * [x] rewrite fn parser to use chumsky::Recursive::declare/define
|
||||||
|
// - [x] do this to extract/simplify/DRY things like tuple patterns, fn clauses, etc.
|
||||||
|
// * [x] Work around chumsky::Stream::from_iter().spanned disappearing in most recent version
|
||||||
|
// * [x] investigate using labels (which is behind a compiler flag, somehow)
|
||||||
|
// * [ ] write parsing errors
|
||||||
|
// * [ ] wire up Ariadne parsing errors
|
||||||
|
// * [x] add stack traces and code locations to panics
|
||||||
|
// * [x] validation
|
||||||
|
// * [x] break this out into multiple files
|
||||||
|
// * [x] write a tree-walk VM
|
||||||
|
// - [x] learn how to deal with lifetimes
|
||||||
|
// - [x] with stack mechanics and refcounting
|
||||||
|
// - [ ] with tail-call optimization (nb: this may not be possible w/ a TW-VM)
|
||||||
|
// - [ ] with all the necessary forms for current Ludus
|
||||||
|
// * [x] guards in match clauses
|
||||||
|
// * [x] `as` patterns
|
||||||
|
// * [x] splat patterns in tuples, lists, dicts
|
||||||
|
// * [x] splats in list and dict literals
|
||||||
|
// * [x] `loop` and `recur`
|
||||||
|
// * [x] string patterns
|
||||||
|
// * [x] string interpolation
|
||||||
|
// * [x] docstrings
|
||||||
|
// * [x] write `base` in Rust
|
||||||
|
// * [ ] turn this into a library function
|
||||||
|
// * [ ] compile this into WASM
|
||||||
|
// * [ ] perf testing
|
||||||
|
|
||||||
|
use chumsky::{input::Stream, prelude::*};
|
||||||
|
use rust_embed::Embed;
|
||||||
|
|
||||||
|
mod spans;
|
||||||
|
|
||||||
|
mod lexer;
|
||||||
|
use crate::lexer::*;
|
||||||
|
|
||||||
|
mod value;
|
||||||
|
use crate::value::*;
|
||||||
|
|
||||||
|
mod parser;
|
||||||
|
use crate::parser::*;
|
||||||
|
|
||||||
|
mod base;
|
||||||
|
use crate::base::*;
|
||||||
|
|
||||||
|
mod validator;
|
||||||
|
use crate::validator::*;
|
||||||
|
|
||||||
|
mod process;
|
||||||
|
use crate::process::*;
|
||||||
|
|
||||||
|
mod errors;
|
||||||
|
use crate::errors::*;
|
||||||
|
|
||||||
|
mod byte_values;
|
||||||
|
mod compiler;
|
||||||
|
mod memory_sandbox;
|
||||||
|
|
||||||
|
#[derive(Embed)]
|
||||||
|
#[folder = "assets/"]
|
||||||
|
struct Asset;
|
||||||
|
|
||||||
|
pub fn prelude<'src>() -> (
|
||||||
|
Vec<(String, Value<'src>)>,
|
||||||
|
std::collections::HashMap<*const Ast, FnInfo>,
|
||||||
|
) {
|
||||||
|
let prelude = Asset::get("prelude.ld").unwrap().data.into_owned();
|
||||||
|
// we know for sure Prelude should live through the whole run of the program
|
||||||
|
let leaked = Box::leak(Box::new(prelude));
|
||||||
|
let prelude = std::str::from_utf8(leaked).unwrap();
|
||||||
|
|
||||||
|
let (ptoks, perrs) = lexer().parse(prelude).into_output_errors();
|
||||||
|
if !perrs.is_empty() {
|
||||||
|
println!("Errors lexing Prelude");
|
||||||
|
println!("{:?}", perrs);
|
||||||
|
panic!();
|
||||||
|
}
|
||||||
|
|
||||||
|
let ptoks = ptoks.unwrap();
|
||||||
|
|
||||||
|
let (p_ast, perrs) = parser()
|
||||||
|
.parse(Stream::from_iter(ptoks).map((0..prelude.len()).into(), |(t, s)| (t, s)))
|
||||||
|
.into_output_errors();
|
||||||
|
if !perrs.is_empty() {
|
||||||
|
println!("Errors parsing Prelude");
|
||||||
|
println!("{:?}", perrs);
|
||||||
|
panic!();
|
||||||
|
}
|
||||||
|
|
||||||
|
let prelude_parsed = Box::leak(Box::new(p_ast.unwrap()));
|
||||||
|
let base_pkg = base();
|
||||||
|
|
||||||
|
let mut v6or = Validator::new(
|
||||||
|
&prelude_parsed.0,
|
||||||
|
prelude_parsed.1,
|
||||||
|
"prelude",
|
||||||
|
prelude,
|
||||||
|
&base_pkg,
|
||||||
|
);
|
||||||
|
v6or.validate();
|
||||||
|
|
||||||
|
if !v6or.errors.is_empty() {
|
||||||
|
report_invalidation(v6or.errors);
|
||||||
|
panic!("interal Ludus error: invalid prelude")
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut base_ctx = Process::<'src> {
|
||||||
|
input: "prelude",
|
||||||
|
src: prelude,
|
||||||
|
locals: base_pkg.clone(),
|
||||||
|
ast: &prelude_parsed.0,
|
||||||
|
span: prelude_parsed.1,
|
||||||
|
prelude: vec![],
|
||||||
|
fn_info: v6or.fn_info,
|
||||||
|
};
|
||||||
|
|
||||||
|
let prelude = base_ctx.eval();
|
||||||
|
|
||||||
|
let mut p_ctx = vec![];
|
||||||
|
|
||||||
|
match prelude {
|
||||||
|
Ok(Value::Dict(p_dict)) => {
|
||||||
|
for (key, value) in p_dict.iter() {
|
||||||
|
p_ctx.push((key.to_string(), value.clone()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(_) => {
|
||||||
|
println!("Bad Prelude export");
|
||||||
|
panic!();
|
||||||
|
}
|
||||||
|
Err(LErr { msg, .. }) => {
|
||||||
|
println!("Error running Prelude");
|
||||||
|
println!("{:?}", msg);
|
||||||
|
panic!();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
(p_ctx, base_ctx.fn_info)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run(src: &'static str) {
|
||||||
|
let (tokens, lex_errs) = lexer().parse(src).into_output_errors();
|
||||||
|
if !lex_errs.is_empty() {
|
||||||
|
println!("{:?}", lex_errs);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let tokens = tokens.unwrap();
|
||||||
|
let to_parse = tokens.clone();
|
||||||
|
|
||||||
|
let (parse_result, parse_errors) = parser()
|
||||||
|
.parse(Stream::from_iter(to_parse).map((0..src.len()).into(), |(t, s)| (t, s)))
|
||||||
|
.into_output_errors();
|
||||||
|
if !parse_errors.is_empty() {
|
||||||
|
println!("{:?}", parse_errors);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let parsed = parse_result.unwrap();
|
||||||
|
|
||||||
|
let (prelude_ctx, mut prelude_fn_info) = prelude();
|
||||||
|
|
||||||
|
let mut v6or = Validator::new(&parsed.0, parsed.1, "script", src, &prelude_ctx);
|
||||||
|
|
||||||
|
v6or.validate();
|
||||||
|
|
||||||
|
if !v6or.errors.is_empty() {
|
||||||
|
report_invalidation(v6or.errors);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
prelude_fn_info.extend(&mut v6or.fn_info.into_iter());
|
||||||
|
|
||||||
|
let mut proc = Process {
|
||||||
|
input: "script",
|
||||||
|
src,
|
||||||
|
locals: vec![],
|
||||||
|
prelude: prelude_ctx,
|
||||||
|
ast: &parsed.0,
|
||||||
|
span: parsed.1,
|
||||||
|
fn_info: prelude_fn_info,
|
||||||
|
};
|
||||||
|
|
||||||
|
let result = proc.eval();
|
||||||
|
|
||||||
|
match result {
|
||||||
|
Ok(result) => println!("{}", result),
|
||||||
|
Err(err) => report_panic(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn main() {
|
||||||
|
let src = "
|
||||||
|
loop (100000, 1) with {
|
||||||
|
(1, acc) -> acc
|
||||||
|
(n, acc) -> recur (dec (n), add (n, acc))
|
||||||
|
}
|
||||||
|
";
|
||||||
|
run(src);
|
||||||
|
// struct_scalpel::print_dissection_info::<value::Value>()
|
||||||
|
// struct_scalpel::print_dissection_info::<parser::Ast>();
|
||||||
|
// println!("{}", std::mem::size_of::<parser::Ast>())
|
||||||
|
}
|
193
src/old_value.rs
Normal file
193
src/old_value.rs
Normal file
|
@ -0,0 +1,193 @@
|
||||||
|
use crate::base::*;
|
||||||
|
use crate::parser::*;
|
||||||
|
use crate::spans::*;
|
||||||
|
use imbl::*;
|
||||||
|
use std::cell::RefCell;
|
||||||
|
use std::fmt;
|
||||||
|
use std::rc::Rc;
|
||||||
|
use struct_scalpel::Dissectible;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct Fn<'src> {
|
||||||
|
pub name: String,
|
||||||
|
pub body: &'src Vec<Spanned<Ast>>,
|
||||||
|
pub doc: Option<String>,
|
||||||
|
pub enclosing: Vec<(String, Value<'src>)>,
|
||||||
|
pub has_run: bool,
|
||||||
|
pub input: &'static str,
|
||||||
|
pub src: &'static str,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Dissectible)]
|
||||||
|
pub enum Value<'src> {
|
||||||
|
Nil,
|
||||||
|
Placeholder,
|
||||||
|
Boolean(bool),
|
||||||
|
Number(f64),
|
||||||
|
Keyword(&'static str),
|
||||||
|
InternedString(&'static str),
|
||||||
|
AllocatedString(Rc<String>),
|
||||||
|
// on the heap for now
|
||||||
|
Tuple(Rc<Vec<Self>>),
|
||||||
|
Args(Rc<Vec<Self>>),
|
||||||
|
List(Vector<Self>),
|
||||||
|
Dict(HashMap<&'static str, Self>),
|
||||||
|
Box(&'static str, Rc<RefCell<Self>>),
|
||||||
|
Fn(Rc<RefCell<Fn<'src>>>),
|
||||||
|
FnDecl(&'static str),
|
||||||
|
Base(BaseFn<'src>),
|
||||||
|
Recur(Vec<Self>),
|
||||||
|
// Set(HashSet<Self>),
|
||||||
|
// Sets are hard
|
||||||
|
// Sets require Eq
|
||||||
|
// Eq is not implemented on f64, because NaNs
|
||||||
|
// We could use ordered_float::NotNan
|
||||||
|
// Let's defer that
|
||||||
|
// We're not really using sets in Ludus
|
||||||
|
|
||||||
|
// Other things we're not implementing yet:
|
||||||
|
// pkgs, nses, tests
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'src> Clone for Value<'src> {
|
||||||
|
fn clone(&self) -> Value<'src> {
|
||||||
|
match self {
|
||||||
|
Value::Nil => Value::Nil,
|
||||||
|
Value::Boolean(b) => Value::Boolean(*b),
|
||||||
|
Value::InternedString(s) => Value::InternedString(s),
|
||||||
|
Value::AllocatedString(s) => Value::AllocatedString(s.clone()),
|
||||||
|
Value::Keyword(s) => Value::Keyword(s),
|
||||||
|
Value::Number(n) => Value::Number(*n),
|
||||||
|
Value::Tuple(t) => Value::Tuple(t.clone()),
|
||||||
|
Value::Args(a) => Value::Args(a.clone()),
|
||||||
|
Value::Fn(f) => Value::Fn(f.clone()),
|
||||||
|
Value::FnDecl(name) => Value::FnDecl(name),
|
||||||
|
Value::List(l) => Value::List(l.clone()),
|
||||||
|
Value::Dict(d) => Value::Dict(d.clone()),
|
||||||
|
Value::Box(name, b) => Value::Box(name, b.clone()),
|
||||||
|
Value::Placeholder => Value::Placeholder,
|
||||||
|
Value::Base(b) => Value::Base(b.clone()),
|
||||||
|
Value::Recur(..) => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Value<'_> {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
Value::Nil => write!(f, "nil"),
|
||||||
|
Value::Boolean(b) => write!(f, "{b}"),
|
||||||
|
Value::Number(n) => write!(f, "{n}"),
|
||||||
|
Value::Keyword(k) => write!(f, ":{k}"),
|
||||||
|
Value::InternedString(s) => write!(f, "\"{s}\""),
|
||||||
|
Value::AllocatedString(s) => write!(f, "\"{s}\""),
|
||||||
|
Value::Fn(fun) => write!(f, "fn {}", fun.borrow().name),
|
||||||
|
Value::FnDecl(name) => write!(f, "fn {name}"),
|
||||||
|
Value::Tuple(t) | Value::Args(t) => write!(
|
||||||
|
f,
|
||||||
|
"({})",
|
||||||
|
t.iter()
|
||||||
|
.map(|x| x.to_string())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(", ")
|
||||||
|
),
|
||||||
|
Value::List(l) => write!(
|
||||||
|
f,
|
||||||
|
"[{}]",
|
||||||
|
l.iter()
|
||||||
|
.map(|x| x.to_string())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(", ")
|
||||||
|
),
|
||||||
|
Value::Dict(d) => write!(
|
||||||
|
f,
|
||||||
|
"#{{{}}}",
|
||||||
|
d.iter()
|
||||||
|
.map(|(k, v)| format!(":{k} {v}"))
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(", ")
|
||||||
|
),
|
||||||
|
Value::Box(name, value) => {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"box {}: [{}]",
|
||||||
|
name,
|
||||||
|
&value.try_borrow().unwrap().to_string()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Value::Placeholder => write!(f, "_"),
|
||||||
|
Value::Base(..) => unreachable!(),
|
||||||
|
Value::Recur(..) => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Value<'_> {
|
||||||
|
pub fn bool(&self) -> bool {
|
||||||
|
!matches!(self, Value::Nil | Value::Boolean(false))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'src> PartialEq for Value<'src> {
|
||||||
|
fn eq(&self, other: &Value<'src>) -> bool {
|
||||||
|
match (self, other) {
|
||||||
|
// value equality types
|
||||||
|
(Value::Nil, Value::Nil) => true,
|
||||||
|
(Value::Boolean(x), Value::Boolean(y)) => x == y,
|
||||||
|
(Value::Number(x), Value::Number(y)) => x == y,
|
||||||
|
(Value::InternedString(x), Value::InternedString(y)) => x == y,
|
||||||
|
(Value::AllocatedString(x), Value::AllocatedString(y)) => x == y,
|
||||||
|
(Value::InternedString(x), Value::AllocatedString(y)) => *x == **y,
|
||||||
|
(Value::AllocatedString(x), Value::InternedString(y)) => **x == *y,
|
||||||
|
(Value::Keyword(x), Value::Keyword(y)) => x == y,
|
||||||
|
(Value::Tuple(x), Value::Tuple(y)) => x == y,
|
||||||
|
(Value::List(x), Value::List(y)) => x == y,
|
||||||
|
(Value::Dict(x), Value::Dict(y)) => x == y,
|
||||||
|
// reference equality types
|
||||||
|
(Value::Fn(x), Value::Fn(y)) => {
|
||||||
|
Rc::<RefCell<Fn<'_>>>::as_ptr(x) == Rc::<RefCell<Fn<'_>>>::as_ptr(y)
|
||||||
|
}
|
||||||
|
(Value::Box(_, x), Value::Box(_, y)) => {
|
||||||
|
Rc::<RefCell<Value<'_>>>::as_ptr(x) == Rc::<RefCell<Value<'_>>>::as_ptr(y)
|
||||||
|
}
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Eq for Value<'_> {}
|
||||||
|
|
||||||
|
impl Value<'_> {
|
||||||
|
pub fn interpolate(&self) -> String {
|
||||||
|
match self {
|
||||||
|
Value::Nil => String::new(),
|
||||||
|
Value::Boolean(b) => format!("{b}"),
|
||||||
|
Value::Number(n) => format!("{n}"),
|
||||||
|
Value::Keyword(k) => format!(":{k}"),
|
||||||
|
Value::AllocatedString(s) => format!("{s}"),
|
||||||
|
Value::InternedString(s) => s.to_string(),
|
||||||
|
Value::Box(_, x) => x.borrow().interpolate(),
|
||||||
|
Value::Tuple(xs) => xs
|
||||||
|
.iter()
|
||||||
|
.map(|x| x.interpolate())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(", "),
|
||||||
|
Value::List(xs) => xs
|
||||||
|
.iter()
|
||||||
|
.map(|x| x.interpolate())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(", "),
|
||||||
|
Value::Dict(xs) => xs
|
||||||
|
.iter()
|
||||||
|
.map(|(k, v)| format!(":{} {}", k, v.interpolate()))
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(", "),
|
||||||
|
Value::Fn(x) => format!("fn {}", x.borrow().name),
|
||||||
|
Value::FnDecl(name) => format!("fn {name}"),
|
||||||
|
Value::Placeholder => unreachable!(),
|
||||||
|
Value::Args(_) => unreachable!(),
|
||||||
|
Value::Recur(_) => unreachable!(),
|
||||||
|
Value::Base(_) => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
533
src/old_vm.rs
Normal file
533
src/old_vm.rs
Normal file
|
@ -0,0 +1,533 @@
|
||||||
|
use crate::base::*;
|
||||||
|
use crate::parser::*;
|
||||||
|
use crate::value::*;
|
||||||
|
use imbl::HashMap;
|
||||||
|
use imbl::Vector;
|
||||||
|
use std::cell::RefCell;
|
||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct LudusError {
|
||||||
|
pub msg: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
// oy
|
||||||
|
// lifetimes are a mess
|
||||||
|
// I need 'src kind of everywhere
|
||||||
|
// But (maybe) using 'src in eval
|
||||||
|
// for ctx
|
||||||
|
// means I can't borrow it mutably
|
||||||
|
// I guess the question is how to get
|
||||||
|
// the branches for Ast::Block and Ast::If
|
||||||
|
// to work with a mutable borrow of ctx
|
||||||
|
|
||||||
|
// pub struct Ctx<'src> {
|
||||||
|
// pub locals: Vec<(&'src str, Value<'src>)>,
|
||||||
|
// // pub names: Vec<&'src str>,
|
||||||
|
// // pub values: Vec<Value<'src>>,
|
||||||
|
// }
|
||||||
|
|
||||||
|
// impl<'src> Ctx<'src> {
|
||||||
|
// pub fn resolve(&self, name: &'src str) -> Value {
|
||||||
|
// if let Some((_, val)) = self.locals.iter().rev().find(|(bound, _)| *bound == name) {
|
||||||
|
// val.clone()
|
||||||
|
// } else {
|
||||||
|
// unreachable!()
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// pub fn store(&mut self, name: &'src str, value: Value<'src>) {
|
||||||
|
// self.locals.push((name, value));
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
type Context<'src> = Vec<(String, Value<'src>)>;
|
||||||
|
|
||||||
|
pub fn match_eq<T, U>(x: T, y: T, z: U) -> Option<U>
|
||||||
|
where
|
||||||
|
T: PartialEq,
|
||||||
|
{
|
||||||
|
if x == y {
|
||||||
|
Some(z)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn match_pattern<'src, 'a>(
|
||||||
|
patt: &Pattern,
|
||||||
|
val: &Value<'src>,
|
||||||
|
ctx: &'a mut Context<'src>,
|
||||||
|
) -> Option<&'a mut Context<'src>> {
|
||||||
|
match (patt, val) {
|
||||||
|
(Pattern::Nil, Value::Nil) => Some(ctx),
|
||||||
|
(Pattern::Placeholder, _) => Some(ctx),
|
||||||
|
(Pattern::Number(x), Value::Number(y)) => match_eq(x, y, ctx),
|
||||||
|
(Pattern::Boolean(x), Value::Boolean(y)) => match_eq(x, y, ctx),
|
||||||
|
(Pattern::Keyword(x), Value::Keyword(y)) => match_eq(x, y, ctx),
|
||||||
|
(Pattern::String(x), Value::InternedString(y)) => match_eq(x, y, ctx),
|
||||||
|
(Pattern::String(x), Value::AllocatedString(y)) => match_eq(&x.to_string(), y, ctx),
|
||||||
|
(Pattern::Interpolated(_, StringMatcher(matcher)), Value::InternedString(y)) => {
|
||||||
|
match matcher(y.to_string()) {
|
||||||
|
Some(matches) => {
|
||||||
|
let mut matches = matches
|
||||||
|
.iter()
|
||||||
|
.map(|(word, string)| {
|
||||||
|
(
|
||||||
|
word.clone(),
|
||||||
|
Value::AllocatedString(Rc::new(string.clone())),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
ctx.append(&mut matches);
|
||||||
|
Some(ctx)
|
||||||
|
}
|
||||||
|
None => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
(Pattern::Word(w), val) => {
|
||||||
|
ctx.push((w.to_string(), val.clone()));
|
||||||
|
Some(ctx)
|
||||||
|
}
|
||||||
|
(Pattern::As(word, type_str), value) => {
|
||||||
|
let ludus_type = r#type(value);
|
||||||
|
let type_kw = Value::Keyword(type_str);
|
||||||
|
if type_kw == ludus_type {
|
||||||
|
ctx.push((word.to_string(), value.clone()));
|
||||||
|
Some(ctx)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// todo: add splats to these match clauses
|
||||||
|
(Pattern::Tuple(x), Value::Tuple(y)) => {
|
||||||
|
let has_splat = x
|
||||||
|
.iter()
|
||||||
|
.any(|patt| matches!(patt, (Pattern::Splattern(_), _)));
|
||||||
|
if x.len() > y.len() || (!has_splat && x.len() != y.len()) {
|
||||||
|
return None;
|
||||||
|
};
|
||||||
|
let to = ctx.len();
|
||||||
|
for i in 0..x.len() {
|
||||||
|
if let Pattern::Splattern(patt) = &x[i].0 {
|
||||||
|
let mut list = Vector::new();
|
||||||
|
for i in i..y.len() {
|
||||||
|
list.push_back(y[i].clone())
|
||||||
|
}
|
||||||
|
let list = Value::List(list);
|
||||||
|
match_pattern(&patt.0, &list, ctx);
|
||||||
|
} else if match_pattern(&x[i].0, &y[i], ctx).is_none() {
|
||||||
|
while ctx.len() > to {
|
||||||
|
ctx.pop();
|
||||||
|
}
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(ctx)
|
||||||
|
}
|
||||||
|
(Pattern::List(x), Value::List(y)) => {
|
||||||
|
let has_splat = x
|
||||||
|
.iter()
|
||||||
|
.any(|patt| matches!(patt, (Pattern::Splattern(_), _)));
|
||||||
|
if x.len() > y.len() || (!has_splat && x.len() != y.len()) {
|
||||||
|
return None;
|
||||||
|
};
|
||||||
|
let to = ctx.len();
|
||||||
|
for (i, (patt, _)) in x.iter().enumerate() {
|
||||||
|
if let Pattern::Splattern(patt) = &patt {
|
||||||
|
let list = Value::List(y.skip(i));
|
||||||
|
match_pattern(&patt.0, &list, ctx);
|
||||||
|
} else if match_pattern(patt, y.get(i).unwrap(), ctx).is_none() {
|
||||||
|
while ctx.len() > to {
|
||||||
|
ctx.pop();
|
||||||
|
}
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(ctx)
|
||||||
|
}
|
||||||
|
// TODO: optimize this on several levels
|
||||||
|
// - [ ] opportunistic mutation
|
||||||
|
// - [ ] get rid of all the pointer indirection in word splats
|
||||||
|
(Pattern::Dict(x), Value::Dict(y)) => {
|
||||||
|
let has_splat = x
|
||||||
|
.iter()
|
||||||
|
.any(|patt| matches!(patt, (Pattern::Splattern(_), _)));
|
||||||
|
if x.len() > y.len() || (!has_splat && x.len() != y.len()) {
|
||||||
|
return None;
|
||||||
|
};
|
||||||
|
let to = ctx.len();
|
||||||
|
let mut matched = vec![];
|
||||||
|
for (pattern, _) in x {
|
||||||
|
match pattern {
|
||||||
|
Pattern::Pair(key, patt) => {
|
||||||
|
if let Some(val) = y.get(key) {
|
||||||
|
if match_pattern(&patt.0, val, ctx).is_none() {
|
||||||
|
while ctx.len() > to {
|
||||||
|
ctx.pop();
|
||||||
|
}
|
||||||
|
return None;
|
||||||
|
} else {
|
||||||
|
matched.push(key);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return None;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
Pattern::Splattern(pattern) => match pattern.0 {
|
||||||
|
Pattern::Word(w) => {
|
||||||
|
// TODO: find a way to take ownership
|
||||||
|
// this will ALWAYS make structural changes, because of this clone
|
||||||
|
// we want opportunistic mutation if possible
|
||||||
|
let mut unmatched = y.clone();
|
||||||
|
for key in matched.iter() {
|
||||||
|
unmatched.remove(*key);
|
||||||
|
}
|
||||||
|
ctx.push((w.to_string(), Value::Dict(unmatched)));
|
||||||
|
}
|
||||||
|
Pattern::Placeholder => (),
|
||||||
|
_ => unreachable!(),
|
||||||
|
},
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(ctx)
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn match_clauses<'src>(
|
||||||
|
value: &Value<'src>,
|
||||||
|
clauses: &'src [MatchClause],
|
||||||
|
ctx: &mut Context<'src>,
|
||||||
|
) -> Result<Value<'src>, LudusError> {
|
||||||
|
let to = ctx.len();
|
||||||
|
for MatchClause { patt, body, guard } in clauses.iter() {
|
||||||
|
if let Some(ctx) = match_pattern(&patt.0, value, ctx) {
|
||||||
|
let pass_guard = match guard {
|
||||||
|
None => true,
|
||||||
|
Some((ast, _)) => {
|
||||||
|
let guard_res = eval(ast, ctx);
|
||||||
|
match &guard_res {
|
||||||
|
Err(_) => return guard_res,
|
||||||
|
Ok(val) => val.bool(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if !pass_guard {
|
||||||
|
while ctx.len() > to {
|
||||||
|
ctx.pop();
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let res = eval(&body.0, ctx);
|
||||||
|
while ctx.len() > to {
|
||||||
|
ctx.pop();
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(LudusError {
|
||||||
|
msg: "no match".to_string(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn apply<'src>(
|
||||||
|
callee: Value<'src>,
|
||||||
|
caller: Value<'src>,
|
||||||
|
ctx: &mut Context,
|
||||||
|
) -> Result<Value<'src>, LudusError> {
|
||||||
|
match (callee, caller) {
|
||||||
|
(Value::Keyword(kw), Value::Dict(dict)) => {
|
||||||
|
if let Some(val) = dict.get(kw) {
|
||||||
|
Ok(val.clone())
|
||||||
|
} else {
|
||||||
|
Ok(Value::Nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
(Value::Dict(dict), Value::Keyword(kw)) => {
|
||||||
|
if let Some(val) = dict.get(kw) {
|
||||||
|
Ok(val.clone())
|
||||||
|
} else {
|
||||||
|
Ok(Value::Nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
(Value::Fn(f), Value::Tuple(args)) => {
|
||||||
|
let args = Value::Tuple(args);
|
||||||
|
match_clauses(&args, f.body, ctx)
|
||||||
|
}
|
||||||
|
(Value::Fn(_f), Value::Args(_args)) => todo!(),
|
||||||
|
(_, Value::Keyword(_)) => Ok(Value::Nil),
|
||||||
|
(_, Value::Args(_)) => Err(LudusError {
|
||||||
|
msg: "you may only call a function".to_string(),
|
||||||
|
}),
|
||||||
|
(Value::Base(f), Value::Tuple(args)) => match f {
|
||||||
|
Base::Nullary(f) => {
|
||||||
|
if args.len() != 0 {
|
||||||
|
Err(LudusError {
|
||||||
|
msg: "wrong arity: expected 0 arguments".to_string(),
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
Ok(f())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Base::Unary(f) => {
|
||||||
|
if args.len() != 1 {
|
||||||
|
Err(LudusError {
|
||||||
|
msg: "wrong arity: expected 1 argument".to_string(),
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
Ok(f(&args[0]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Base::Binary(r#fn) => {
|
||||||
|
if args.len() != 2 {
|
||||||
|
Err(LudusError {
|
||||||
|
msg: "wrong arity: expected 2 arguments".to_string(),
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
Ok(r#fn(&args[0], &args[1]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Base::Ternary(f) => {
|
||||||
|
if args.len() != 3 {
|
||||||
|
Err(LudusError {
|
||||||
|
msg: "wrong arity: expected 3 arguments".to_string(),
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
Ok(f(&args[0], &args[1], &args[2]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn eval<'src, 'a>(
|
||||||
|
ast: &'src Ast,
|
||||||
|
ctx: &'a mut Vec<(String, Value<'src>)>,
|
||||||
|
) -> Result<Value<'src>, LudusError> {
|
||||||
|
match ast {
|
||||||
|
Ast::Nil => Ok(Value::Nil),
|
||||||
|
Ast::Boolean(b) => Ok(Value::Boolean(*b)),
|
||||||
|
Ast::Number(n) => Ok(Value::Number(*n)),
|
||||||
|
Ast::Keyword(k) => Ok(Value::Keyword(k)),
|
||||||
|
Ast::String(s) => Ok(Value::InternedString(s)),
|
||||||
|
Ast::Interpolated(parts) => {
|
||||||
|
let mut interpolated = String::new();
|
||||||
|
for part in parts {
|
||||||
|
match &part.0 {
|
||||||
|
StringPart::Data(s) => interpolated.push_str(s.as_str()),
|
||||||
|
StringPart::Word(w) => {
|
||||||
|
let val = if let Some((_, value)) =
|
||||||
|
ctx.iter().rev().find(|(name, _)| w == name)
|
||||||
|
{
|
||||||
|
value.clone()
|
||||||
|
} else {
|
||||||
|
return Err(LudusError {
|
||||||
|
msg: format!("unbound name {w}"),
|
||||||
|
});
|
||||||
|
};
|
||||||
|
interpolated.push_str(val.interpolate().as_str())
|
||||||
|
}
|
||||||
|
StringPart::Inline(_) => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(Value::AllocatedString(Rc::new(interpolated)))
|
||||||
|
}
|
||||||
|
Ast::Block(exprs) => {
|
||||||
|
let to = ctx.len();
|
||||||
|
let mut result = Value::Nil;
|
||||||
|
for (expr, _) in exprs {
|
||||||
|
result = eval(expr, ctx)?;
|
||||||
|
}
|
||||||
|
while ctx.len() > to {
|
||||||
|
ctx.pop();
|
||||||
|
}
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
|
Ast::If(cond, if_true, if_false) => {
|
||||||
|
let truthy = eval(&cond.0, ctx)?.bool();
|
||||||
|
if truthy {
|
||||||
|
eval(&if_true.0, ctx)
|
||||||
|
} else {
|
||||||
|
eval(&if_false.0, ctx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ast::List(members) => {
|
||||||
|
let mut vect = Vector::new();
|
||||||
|
for member in members {
|
||||||
|
if let Ast::Splat(_) = member.0 {
|
||||||
|
let to_splat = eval(&member.0, ctx)?;
|
||||||
|
match to_splat {
|
||||||
|
Value::List(list) => vect.append(list),
|
||||||
|
_ => {
|
||||||
|
return Err(LudusError {
|
||||||
|
msg: "only lists may be splatted into lists".to_string(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
vect.push_back(eval(&member.0, ctx)?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(Value::List(vect))
|
||||||
|
}
|
||||||
|
Ast::Tuple(members) => {
|
||||||
|
let mut vect = Vec::new();
|
||||||
|
for member in members {
|
||||||
|
vect.push(eval(&member.0, ctx)?);
|
||||||
|
}
|
||||||
|
Ok(Value::Tuple(Rc::new(vect)))
|
||||||
|
}
|
||||||
|
Ast::Word(w) | Ast::Splat(w) => {
|
||||||
|
let val = if let Some((_, value)) = ctx.iter().rev().find(|(name, _)| w == name) {
|
||||||
|
value.clone()
|
||||||
|
} else {
|
||||||
|
return Err(LudusError {
|
||||||
|
msg: format!("unbound name {w}"),
|
||||||
|
});
|
||||||
|
};
|
||||||
|
Ok(val)
|
||||||
|
}
|
||||||
|
Ast::Let(patt, expr) => {
|
||||||
|
let val = eval(&expr.0, ctx)?;
|
||||||
|
match match_pattern(&patt.0, &val, ctx) {
|
||||||
|
Some(_) => Ok(val),
|
||||||
|
None => Err(LudusError {
|
||||||
|
msg: "No match".to_string(),
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ast::Placeholder => Ok(Value::Placeholder),
|
||||||
|
Ast::Error => unreachable!(),
|
||||||
|
Ast::Arguments(a) => {
|
||||||
|
let mut args = vec![];
|
||||||
|
for (arg, _) in a.iter() {
|
||||||
|
let arg = eval(arg, ctx)?;
|
||||||
|
args.push(arg);
|
||||||
|
}
|
||||||
|
if args.iter().any(|arg| matches!(arg, Value::Placeholder)) {
|
||||||
|
Ok(Value::Args(Rc::new(args)))
|
||||||
|
} else {
|
||||||
|
Ok(Value::Tuple(Rc::new(args)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ast::Dict(terms) => {
|
||||||
|
let mut dict = HashMap::new();
|
||||||
|
for term in terms {
|
||||||
|
let (term, _) = term;
|
||||||
|
match term {
|
||||||
|
Ast::Pair(key, value) => {
|
||||||
|
let value = eval(&value.0, ctx)?;
|
||||||
|
dict.insert(*key, value);
|
||||||
|
}
|
||||||
|
Ast::Splat(_) => {
|
||||||
|
let resolved = eval(term, ctx)?;
|
||||||
|
let Value::Dict(to_splat) = resolved else {
|
||||||
|
return Err(LudusError {
|
||||||
|
msg: "cannot splat non-dict into dict".to_string(),
|
||||||
|
});
|
||||||
|
};
|
||||||
|
dict = to_splat.union(dict);
|
||||||
|
}
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(Value::Dict(dict))
|
||||||
|
}
|
||||||
|
Ast::Box(name, expr) => {
|
||||||
|
let val = eval(&expr.0, ctx)?;
|
||||||
|
let boxed = Value::Box(name, Rc::new(RefCell::new(val)));
|
||||||
|
ctx.push((name.to_string(), boxed.clone()));
|
||||||
|
Ok(boxed)
|
||||||
|
}
|
||||||
|
Ast::Synthetic(root, first, rest) => {
|
||||||
|
let root = eval(&root.0, ctx)?;
|
||||||
|
let first = eval(&first.0, ctx)?;
|
||||||
|
let mut curr = apply(root, first, ctx)?;
|
||||||
|
for term in rest.iter() {
|
||||||
|
let next = eval(&term.0, ctx)?;
|
||||||
|
curr = apply(curr, next, ctx)?;
|
||||||
|
}
|
||||||
|
Ok(curr)
|
||||||
|
}
|
||||||
|
Ast::When(clauses) => {
|
||||||
|
for clause in clauses.iter() {
|
||||||
|
let WhenClause { cond, body } = &clause.0;
|
||||||
|
if eval(&cond.0, ctx)?.bool() {
|
||||||
|
return eval(&body.0, ctx);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
Err(LudusError {
|
||||||
|
msg: "no match".to_string(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Ast::Match(value, clauses) => {
|
||||||
|
let value = eval(&value.0, ctx)?;
|
||||||
|
match_clauses(&value, clauses, ctx)
|
||||||
|
}
|
||||||
|
Ast::Fn(name, clauses, doc) => {
|
||||||
|
let doc = doc.map(|s| s.to_string());
|
||||||
|
let the_fn = Value::Fn::<'src>(Rc::new(Fn::<'src> {
|
||||||
|
name: name.to_string(),
|
||||||
|
body: clauses,
|
||||||
|
doc,
|
||||||
|
}));
|
||||||
|
ctx.push((name.to_string(), the_fn.clone()));
|
||||||
|
Ok(the_fn)
|
||||||
|
}
|
||||||
|
Ast::FnDeclaration(_name) => todo!(),
|
||||||
|
Ast::Panic(msg) => {
|
||||||
|
let msg = eval(&msg.0, ctx)?;
|
||||||
|
Err(LudusError {
|
||||||
|
msg: msg.to_string(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Ast::Repeat(times, body) => {
|
||||||
|
let times_num = match eval(×.0, ctx) {
|
||||||
|
Ok(Value::Number(n)) => n as usize,
|
||||||
|
_ => {
|
||||||
|
return Err(LudusError {
|
||||||
|
msg: "repeat may only take numbers".to_string(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
};
|
||||||
|
for _ in 0..times_num {
|
||||||
|
eval(&body.0, ctx)?;
|
||||||
|
}
|
||||||
|
Ok(Value::Nil)
|
||||||
|
}
|
||||||
|
Ast::Do(terms) => {
|
||||||
|
let mut result = eval(&terms[0].0, ctx)?;
|
||||||
|
for (term, _) in terms.iter().skip(1) {
|
||||||
|
let next = eval(term, ctx)?;
|
||||||
|
let arg = Value::Tuple(Rc::new(vec![result]));
|
||||||
|
result = apply(next, arg, ctx)?;
|
||||||
|
}
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
|
Ast::Pair(..) => {
|
||||||
|
unreachable!()
|
||||||
|
}
|
||||||
|
Ast::Loop(init, clauses) => {
|
||||||
|
let mut args = eval(&init.0, ctx)?;
|
||||||
|
loop {
|
||||||
|
let result = match_clauses(&args, clauses, ctx)?;
|
||||||
|
if let Value::Recur(recur_args) = result {
|
||||||
|
args = Value::Tuple(Rc::new(recur_args));
|
||||||
|
} else {
|
||||||
|
return Ok(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ast::Recur(args) => {
|
||||||
|
let mut vect = Vec::new();
|
||||||
|
for arg in args {
|
||||||
|
vect.push(eval(&arg.0, ctx)?);
|
||||||
|
}
|
||||||
|
Ok(Value::Recur(vect))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
226
src/op.rs
Normal file
226
src/op.rs
Normal file
|
@ -0,0 +1,226 @@
|
||||||
|
use num_derive::{FromPrimitive, ToPrimitive};
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, FromPrimitive, ToPrimitive)]
|
||||||
|
pub enum Op {
|
||||||
|
Noop,
|
||||||
|
Nothing,
|
||||||
|
Nil,
|
||||||
|
True,
|
||||||
|
False,
|
||||||
|
Constant,
|
||||||
|
Jump,
|
||||||
|
JumpIfFalse,
|
||||||
|
JumpIfTrue,
|
||||||
|
Pop,
|
||||||
|
PopN,
|
||||||
|
PushBinding,
|
||||||
|
PushGlobal,
|
||||||
|
Store,
|
||||||
|
StoreN,
|
||||||
|
Stash,
|
||||||
|
Load,
|
||||||
|
LoadN,
|
||||||
|
ResetMatch,
|
||||||
|
UnconditionalMatch,
|
||||||
|
MatchNil,
|
||||||
|
MatchTrue,
|
||||||
|
MatchFalse,
|
||||||
|
PanicIfNoMatch,
|
||||||
|
MatchConstant,
|
||||||
|
MatchString,
|
||||||
|
PushStringMatches,
|
||||||
|
MatchType,
|
||||||
|
MatchTuple,
|
||||||
|
MatchSplattedTuple,
|
||||||
|
PushTuple,
|
||||||
|
LoadTuple,
|
||||||
|
LoadSplattedTuple,
|
||||||
|
MatchList,
|
||||||
|
MatchSplattedList,
|
||||||
|
LoadList,
|
||||||
|
LoadSplattedList,
|
||||||
|
PushList,
|
||||||
|
AppendList,
|
||||||
|
ConcatList,
|
||||||
|
PushDict,
|
||||||
|
AppendDict,
|
||||||
|
ConcatDict,
|
||||||
|
LoadDictValue,
|
||||||
|
MatchDict,
|
||||||
|
MatchSplattedDict,
|
||||||
|
DropDictEntry,
|
||||||
|
PushBox,
|
||||||
|
GetKey,
|
||||||
|
PanicNoWhen,
|
||||||
|
JumpIfNoMatch,
|
||||||
|
JumpIfMatch,
|
||||||
|
PanicNoMatch,
|
||||||
|
TypeOf,
|
||||||
|
JumpBack,
|
||||||
|
JumpIfZero,
|
||||||
|
Duplicate,
|
||||||
|
Decrement,
|
||||||
|
ToInt,
|
||||||
|
MatchDepth,
|
||||||
|
Panic,
|
||||||
|
EmptyString,
|
||||||
|
ConcatStrings,
|
||||||
|
Stringify,
|
||||||
|
|
||||||
|
Call,
|
||||||
|
TailCall,
|
||||||
|
Return,
|
||||||
|
Partial,
|
||||||
|
|
||||||
|
Eq,
|
||||||
|
Add,
|
||||||
|
Sub,
|
||||||
|
Mult,
|
||||||
|
Div,
|
||||||
|
Unbox,
|
||||||
|
BoxStore,
|
||||||
|
Assert,
|
||||||
|
Get,
|
||||||
|
At,
|
||||||
|
|
||||||
|
Not,
|
||||||
|
Print,
|
||||||
|
SetUpvalue,
|
||||||
|
GetUpvalue,
|
||||||
|
|
||||||
|
Msg,
|
||||||
|
// Inc,
|
||||||
|
// Dec,
|
||||||
|
// Gt,
|
||||||
|
// Gte,
|
||||||
|
// Lt,
|
||||||
|
// Lte,
|
||||||
|
// Mod,
|
||||||
|
// Round,
|
||||||
|
// Ceil,
|
||||||
|
// Floor,
|
||||||
|
// Random,
|
||||||
|
// Sqrt,
|
||||||
|
|
||||||
|
// Assoc,
|
||||||
|
// Concat,
|
||||||
|
// Conj,
|
||||||
|
// Count,
|
||||||
|
// Disj,
|
||||||
|
// Dissoc,
|
||||||
|
// Range,
|
||||||
|
// Rest,
|
||||||
|
// Slice,
|
||||||
|
|
||||||
|
// "atan_2" math/atan2
|
||||||
|
// "chars" chars
|
||||||
|
// "cos" math/cos
|
||||||
|
// "doc" doc
|
||||||
|
// "downcase" string/ascii-lower
|
||||||
|
// "pi" math/pi
|
||||||
|
// "show" show
|
||||||
|
// "sin" math/sin
|
||||||
|
// "split" string/split
|
||||||
|
// "str_slice" string/slice
|
||||||
|
// "tan" math/tan
|
||||||
|
// "trim" string/trim
|
||||||
|
// "triml" string/triml
|
||||||
|
// "trimr" string/trimr
|
||||||
|
// "upcase" string/ascii-upper
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Display for Op {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
|
use Op::*;
|
||||||
|
let rep = match self {
|
||||||
|
Msg => "msg",
|
||||||
|
Noop => "noop",
|
||||||
|
Nothing => "nothing",
|
||||||
|
Nil => "nil",
|
||||||
|
True => "true",
|
||||||
|
False => "false",
|
||||||
|
Constant => "constant",
|
||||||
|
Jump => "jump",
|
||||||
|
JumpIfFalse => "jump_if_false",
|
||||||
|
JumpIfTrue => "jump_if_true",
|
||||||
|
Pop => "pop",
|
||||||
|
PopN => "pop_n",
|
||||||
|
PushBinding => "push_binding",
|
||||||
|
PushGlobal => "push_global",
|
||||||
|
Store => "store",
|
||||||
|
StoreN => "store_n",
|
||||||
|
Stash => "stash",
|
||||||
|
Load => "load",
|
||||||
|
LoadN => "load_n",
|
||||||
|
UnconditionalMatch => "match",
|
||||||
|
MatchNil => "match_nil",
|
||||||
|
MatchTrue => "match_true",
|
||||||
|
MatchFalse => "match_false",
|
||||||
|
ResetMatch => "reset_match",
|
||||||
|
PanicIfNoMatch => "panic_if_no_match",
|
||||||
|
MatchConstant => "match_constant",
|
||||||
|
MatchString => "match_string",
|
||||||
|
PushStringMatches => "push_string_matches",
|
||||||
|
MatchType => "match_type",
|
||||||
|
MatchTuple => "match_tuple",
|
||||||
|
MatchSplattedTuple => "match_splatted_tuple",
|
||||||
|
PushTuple => "push_tuple",
|
||||||
|
LoadTuple => "load_tuple",
|
||||||
|
LoadSplattedTuple => "load_splatted_tuple",
|
||||||
|
MatchList => "match_list",
|
||||||
|
MatchSplattedList => "match_splatted_list",
|
||||||
|
LoadList => "load_list",
|
||||||
|
LoadSplattedList => "load_splatted_list",
|
||||||
|
PushList => "push_list",
|
||||||
|
AppendList => "append_list",
|
||||||
|
ConcatList => "concat_list",
|
||||||
|
PushDict => "push_dict",
|
||||||
|
AppendDict => "append_dict",
|
||||||
|
ConcatDict => "concat_dict",
|
||||||
|
LoadDictValue => "load_dict_value",
|
||||||
|
MatchDict => "match_dict",
|
||||||
|
MatchSplattedDict => "match_splatted_dict",
|
||||||
|
DropDictEntry => "drop_dict_entry",
|
||||||
|
PushBox => "push_box",
|
||||||
|
GetKey => "get_key",
|
||||||
|
PanicNoWhen => "panic_no_when",
|
||||||
|
JumpIfNoMatch => "jump_if_no_match",
|
||||||
|
JumpIfMatch => "jump_if_match",
|
||||||
|
PanicNoMatch => "panic_no_match",
|
||||||
|
TypeOf => "type_of",
|
||||||
|
JumpBack => "jump_back",
|
||||||
|
JumpIfZero => "jump_if_zero",
|
||||||
|
Decrement => "decrement",
|
||||||
|
ToInt => "truncate",
|
||||||
|
Duplicate => "duplicate",
|
||||||
|
MatchDepth => "match_depth",
|
||||||
|
Panic => "panic",
|
||||||
|
EmptyString => "empty_string",
|
||||||
|
ConcatStrings => "concat_strings",
|
||||||
|
Stringify => "stringify",
|
||||||
|
Print => "print",
|
||||||
|
|
||||||
|
Eq => "eq",
|
||||||
|
Add => "add",
|
||||||
|
Sub => "sub",
|
||||||
|
Mult => "mult",
|
||||||
|
Div => "div",
|
||||||
|
Unbox => "unbox",
|
||||||
|
BoxStore => "box_store",
|
||||||
|
Assert => "assert",
|
||||||
|
Get => "get",
|
||||||
|
At => "at",
|
||||||
|
|
||||||
|
Not => "not",
|
||||||
|
|
||||||
|
Call => "call",
|
||||||
|
Return => "return",
|
||||||
|
Partial => "partial",
|
||||||
|
TailCall => "tail_call",
|
||||||
|
|
||||||
|
SetUpvalue => "set_upvalue",
|
||||||
|
GetUpvalue => "get_upvalue",
|
||||||
|
};
|
||||||
|
write!(f, "{rep}")
|
||||||
|
}
|
||||||
|
}
|
318
src/parser.rs
318
src/parser.rs
|
@ -4,35 +4,6 @@ use chumsky::{input::ValueInput, prelude::*, recursive::Recursive};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use struct_scalpel::Dissectible;
|
use struct_scalpel::Dissectible;
|
||||||
|
|
||||||
// #[derive(Clone, Debug, PartialEq)]
|
|
||||||
// pub struct WhenClause {
|
|
||||||
// pub cond: Spanned<Ast>,
|
|
||||||
// pub body: Spanned<Ast>,
|
|
||||||
// }
|
|
||||||
|
|
||||||
// impl fmt::Display for WhenClause {
|
|
||||||
// fn fmt(self: &WhenClause, f: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
// write!(f, "cond: {}, body: {}", self.cond.0, self.body.0)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// #[derive(Clone, Debug, PartialEq)]
|
|
||||||
// pub struct MatchClause {
|
|
||||||
// pub patt: Spanned<Pattern>,
|
|
||||||
// pub guard: Option<Spanned<Ast>>,
|
|
||||||
// pub body: Spanned<Ast>,
|
|
||||||
// }
|
|
||||||
|
|
||||||
// impl fmt::Display for MatchClause {
|
|
||||||
// fn fmt(self: &MatchClause, f: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
// write!(
|
|
||||||
// f,
|
|
||||||
// "pattern: {}, guard: {:?} body: {}",
|
|
||||||
// self.patt.0, self.guard, self.body.0
|
|
||||||
// )
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||||
pub enum StringPart {
|
pub enum StringPart {
|
||||||
Data(String),
|
Data(String),
|
||||||
|
@ -51,12 +22,21 @@ impl fmt::Display for StringPart {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct LFn {
|
||||||
|
name: &'static str,
|
||||||
|
clauses: Vec<Spanned<Ast>>,
|
||||||
|
doc: Option<&'static str>,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Dissectible)]
|
#[derive(Clone, Debug, PartialEq, Dissectible)]
|
||||||
pub enum Ast {
|
pub enum Ast {
|
||||||
// a special Error node
|
// a special Error node
|
||||||
// may come in handy?
|
// may come in handy?
|
||||||
Error,
|
Error,
|
||||||
|
|
||||||
|
And,
|
||||||
|
Or,
|
||||||
|
|
||||||
// expression nodes
|
// expression nodes
|
||||||
Placeholder,
|
Placeholder,
|
||||||
Nil,
|
Nil,
|
||||||
|
@ -83,7 +63,8 @@ pub enum Ast {
|
||||||
Box<Option<Spanned<Self>>>,
|
Box<Option<Spanned<Self>>>,
|
||||||
Box<Spanned<Self>>,
|
Box<Spanned<Self>>,
|
||||||
),
|
),
|
||||||
Fn(&'static str, Vec<Spanned<Self>>, Option<&'static str>),
|
Fn(&'static str, Box<Spanned<Ast>>, Option<&'static str>),
|
||||||
|
FnBody(Vec<Spanned<Ast>>),
|
||||||
FnDeclaration(&'static str),
|
FnDeclaration(&'static str),
|
||||||
Panic(Box<Spanned<Self>>),
|
Panic(Box<Spanned<Self>>),
|
||||||
Do(Vec<Spanned<Self>>),
|
Do(Vec<Spanned<Self>>),
|
||||||
|
@ -110,10 +91,168 @@ pub enum Ast {
|
||||||
DictPattern(Vec<Spanned<Self>>),
|
DictPattern(Vec<Spanned<Self>>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Ast {
|
||||||
|
pub fn show(&self) -> String {
|
||||||
|
use Ast::*;
|
||||||
|
match self {
|
||||||
|
And => "and".to_string(),
|
||||||
|
Or => "or".to_string(),
|
||||||
|
Error => unreachable!(),
|
||||||
|
Nil | NilPattern => "nil".to_string(),
|
||||||
|
String(s) | StringPattern(s) => format!("\"{s}\""),
|
||||||
|
Interpolated(strs) | InterpolatedPattern(strs, _) => {
|
||||||
|
let mut out = "".to_string();
|
||||||
|
out = format!("\"{out}");
|
||||||
|
for (part, _) in strs {
|
||||||
|
out = format!("{out}{part}");
|
||||||
|
}
|
||||||
|
format!("{out}\"")
|
||||||
|
}
|
||||||
|
Boolean(b) | BooleanPattern(b) => b.to_string(),
|
||||||
|
Number(n) | NumberPattern(n) => n.to_string(),
|
||||||
|
Keyword(k) | KeywordPattern(k) => format!(":{k}"),
|
||||||
|
Word(w) | WordPattern(w) => w.to_string(),
|
||||||
|
Block(lines) => {
|
||||||
|
let mut out = "{\n".to_string();
|
||||||
|
for (line, _) in lines {
|
||||||
|
out = format!("{out}\n {}", line.show());
|
||||||
|
}
|
||||||
|
format!("{out}\n}}")
|
||||||
|
}
|
||||||
|
If(cond, then, r#else) => format!(
|
||||||
|
"if {}\n then {}\n else {}",
|
||||||
|
cond.0.show(),
|
||||||
|
then.0.show(),
|
||||||
|
r#else.0.show()
|
||||||
|
),
|
||||||
|
Let(pattern, expression) => {
|
||||||
|
format!("let {} = {}", pattern.0.show(), expression.0.show())
|
||||||
|
}
|
||||||
|
Dict(entries) | DictPattern(entries) => {
|
||||||
|
format!(
|
||||||
|
"#{{{}}}",
|
||||||
|
entries
|
||||||
|
.iter()
|
||||||
|
.map(|(pair, _)| pair.show())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(", ")
|
||||||
|
)
|
||||||
|
}
|
||||||
|
List(members) | ListPattern(members) => format!(
|
||||||
|
"[{}]",
|
||||||
|
members
|
||||||
|
.iter()
|
||||||
|
.map(|(member, _)| member.show())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(", ")
|
||||||
|
),
|
||||||
|
Arguments(members) => format!(
|
||||||
|
"({})",
|
||||||
|
members
|
||||||
|
.iter()
|
||||||
|
.map(|(member, _)| member.show())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(", ")
|
||||||
|
),
|
||||||
|
Tuple(members) | TuplePattern(members) => format!(
|
||||||
|
"({})",
|
||||||
|
members
|
||||||
|
.iter()
|
||||||
|
.map(|(member, _)| member.show())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(", ")
|
||||||
|
),
|
||||||
|
Synthetic(root, first, rest) => format!(
|
||||||
|
"{} {} {}",
|
||||||
|
root.0.show(),
|
||||||
|
first.0.show(),
|
||||||
|
rest.iter()
|
||||||
|
.map(|(term, _)| term.show())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(" ")
|
||||||
|
),
|
||||||
|
When(clauses) => format!(
|
||||||
|
"when {{\n {}\n}}",
|
||||||
|
clauses
|
||||||
|
.iter()
|
||||||
|
.map(|(clause, _)| clause.show())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join("\n ")
|
||||||
|
),
|
||||||
|
Placeholder | PlaceholderPattern => "_".to_string(),
|
||||||
|
LBox(name, rhs) => format!("box {name} = {}", rhs.0.show()),
|
||||||
|
Match(scrutinee, clauses) => format!(
|
||||||
|
"match {} with {{\n {}\n}}",
|
||||||
|
scrutinee.0.show(),
|
||||||
|
clauses
|
||||||
|
.iter()
|
||||||
|
.map(|(clause, _)| clause.show())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join("\n ")
|
||||||
|
),
|
||||||
|
FnBody(clauses) => clauses
|
||||||
|
.iter()
|
||||||
|
.map(|(clause, _)| clause.show())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join("\n ")
|
||||||
|
.to_string(),
|
||||||
|
Fn(name, body, doc) => {
|
||||||
|
let mut out = format!("fn {name} {{\n");
|
||||||
|
if let Some(doc) = doc {
|
||||||
|
out = format!("{out} {doc}\n");
|
||||||
|
}
|
||||||
|
format!("{out} {}\n}}", body.0.show())
|
||||||
|
}
|
||||||
|
FnDeclaration(name) => format!("fn {name}"),
|
||||||
|
Panic(expr) => format!("panic! {}", expr.0.show()),
|
||||||
|
Do(terms) => {
|
||||||
|
format!(
|
||||||
|
"do {}",
|
||||||
|
terms
|
||||||
|
.iter()
|
||||||
|
.map(|(term, _)| term.show())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(" > ")
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Repeat(times, body) => format!("repeat {} {{\n{}\n}}", times.0.show(), body.0.show()),
|
||||||
|
Splat(word) => format!("...{}", word),
|
||||||
|
Splattern(pattern) => format!("...{}", pattern.0.show()),
|
||||||
|
AsPattern(word, type_keyword) => format!("{word} as :{type_keyword}"),
|
||||||
|
Pair(key, value) | PairPattern(key, value) => format!(":{key} {}", value.0.show()),
|
||||||
|
Loop(init, body) => format!(
|
||||||
|
"loop {} with {{\n {}\n}}",
|
||||||
|
init.0.show(),
|
||||||
|
body.iter()
|
||||||
|
.map(|(clause, _)| clause.show())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join("\n ")
|
||||||
|
),
|
||||||
|
Recur(args) => format!(
|
||||||
|
"recur ({})",
|
||||||
|
args.iter()
|
||||||
|
.map(|(arg, _)| arg.show())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(", ")
|
||||||
|
),
|
||||||
|
MatchClause(pattern, guard, body) => {
|
||||||
|
let mut out = pattern.0.show().to_string();
|
||||||
|
if let Some(guard) = guard.as_ref() {
|
||||||
|
out = format!("{out} if {}", guard.0.show());
|
||||||
|
}
|
||||||
|
format!("{out} -> {}", body.0.show())
|
||||||
|
}
|
||||||
|
WhenClause(cond, body) => format!("{} -> {}", cond.0.show(), body.0.show()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl fmt::Display for Ast {
|
impl fmt::Display for Ast {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
use Ast::*;
|
use Ast::*;
|
||||||
match self {
|
match self {
|
||||||
|
And => write!(f, "And"),
|
||||||
|
Or => write!(f, "Or"),
|
||||||
Error => write!(f, "Error"),
|
Error => write!(f, "Error"),
|
||||||
Nil => write!(f, "nil"),
|
Nil => write!(f, "nil"),
|
||||||
String(s) => write!(f, "String: \"{}\"", s),
|
String(s) => write!(f, "String: \"{}\"", s),
|
||||||
|
@ -164,7 +303,15 @@ impl fmt::Display for Ast {
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.join("\n")
|
.join("\n")
|
||||||
),
|
),
|
||||||
Tuple(t) | Ast::Arguments(t) => write!(
|
Arguments(a) => write!(
|
||||||
|
f,
|
||||||
|
"Arguments: ({})",
|
||||||
|
a.iter()
|
||||||
|
.map(|(line, _)| line.to_string())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join("\n")
|
||||||
|
),
|
||||||
|
Tuple(t) => write!(
|
||||||
f,
|
f,
|
||||||
"Tuple: ({})",
|
"Tuple: ({})",
|
||||||
t.iter()
|
t.iter()
|
||||||
|
@ -191,7 +338,7 @@ impl fmt::Display for Ast {
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.join("\n")
|
.join("\n")
|
||||||
),
|
),
|
||||||
Placeholder => todo!(),
|
Placeholder => write!(f, "Placeholder"),
|
||||||
LBox(_name, _rhs) => todo!(),
|
LBox(_name, _rhs) => todo!(),
|
||||||
Match(value, clauses) => {
|
Match(value, clauses) => {
|
||||||
write!(
|
write!(
|
||||||
|
@ -205,11 +352,10 @@ impl fmt::Display for Ast {
|
||||||
.join("\n")
|
.join("\n")
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
Fn(name, clauses, _) => {
|
FnBody(clauses) => {
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
"fn: {}\n{}",
|
"{}",
|
||||||
name,
|
|
||||||
clauses
|
clauses
|
||||||
.iter()
|
.iter()
|
||||||
.map(|clause| clause.0.to_string())
|
.map(|clause| clause.0.to_string())
|
||||||
|
@ -217,6 +363,9 @@ impl fmt::Display for Ast {
|
||||||
.join("\n")
|
.join("\n")
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
Fn(name, body, ..) => {
|
||||||
|
write!(f, "fn: {name}\n{}", body.0)
|
||||||
|
}
|
||||||
FnDeclaration(_name) => todo!(),
|
FnDeclaration(_name) => todo!(),
|
||||||
Panic(_expr) => todo!(),
|
Panic(_expr) => todo!(),
|
||||||
Do(terms) => {
|
Do(terms) => {
|
||||||
|
@ -343,75 +492,6 @@ impl fmt::Debug for StringMatcher {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// #[derive(Clone, Debug, PartialEq)]
|
|
||||||
// pub enum Pattern {
|
|
||||||
// Nil,
|
|
||||||
// Boolean(bool),
|
|
||||||
// Number(f64),
|
|
||||||
// String(&'static str),
|
|
||||||
// Interpolated(Vec<Spanned<StringPart>>, StringMatcher),
|
|
||||||
// Keyword(&'static str),
|
|
||||||
// Word(&'static str),
|
|
||||||
// As(&'static str, &'static str),
|
|
||||||
// Splattern(Box<Spanned<Self>>),
|
|
||||||
// Placeholder,
|
|
||||||
// Tuple(Vec<Spanned<Self>>),
|
|
||||||
// List(Vec<Spanned<Self>>),
|
|
||||||
// Pair(&'static str, Box<Spanned<Self>>),
|
|
||||||
// Dict(Vec<Spanned<Self>>),
|
|
||||||
// }
|
|
||||||
|
|
||||||
// impl fmt::Display for Pattern {
|
|
||||||
// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
// match self {
|
|
||||||
// Pattern::Nil => write!(f, "nil"),
|
|
||||||
// Pattern::Boolean(b) => write!(f, "{}", b),
|
|
||||||
// Pattern::Number(n) => write!(f, "{}", n),
|
|
||||||
// Pattern::String(s) => write!(f, "{}", s),
|
|
||||||
// Pattern::Keyword(k) => write!(f, ":{}", k),
|
|
||||||
// Pattern::Word(w) => write!(f, "{}", w),
|
|
||||||
// Pattern::As(w, t) => write!(f, "{} as {}", w, t),
|
|
||||||
// Pattern::Splattern(p) => write!(f, "...{}", p.0),
|
|
||||||
// Pattern::Placeholder => write!(f, "_"),
|
|
||||||
// Pattern::Tuple(t) => write!(
|
|
||||||
// f,
|
|
||||||
// "({})",
|
|
||||||
// t.iter()
|
|
||||||
// .map(|x| x.0.to_string())
|
|
||||||
// .collect::<Vec<_>>()
|
|
||||||
// .join(", ")
|
|
||||||
// ),
|
|
||||||
// Pattern::List(l) => write!(
|
|
||||||
// f,
|
|
||||||
// "({})",
|
|
||||||
// l.iter()
|
|
||||||
// .map(|x| x.0.to_string())
|
|
||||||
// .collect::<Vec<_>>()
|
|
||||||
// .join(", ")
|
|
||||||
// ),
|
|
||||||
// Pattern::Dict(entries) => write!(
|
|
||||||
// f,
|
|
||||||
// "#{{{}}}",
|
|
||||||
// entries
|
|
||||||
// .iter()
|
|
||||||
// .map(|(pair, _)| pair.to_string())
|
|
||||||
// .collect::<Vec<_>>()
|
|
||||||
// .join(", ")
|
|
||||||
// ),
|
|
||||||
// Pattern::Pair(key, value) => write!(f, ":{} {}", key, value.0),
|
|
||||||
// Pattern::Interpolated(strprts, _) => write!(
|
|
||||||
// f,
|
|
||||||
// "interpolated: \"{}\"",
|
|
||||||
// strprts
|
|
||||||
// .iter()
|
|
||||||
// .map(|part| part.0.to_string())
|
|
||||||
// .collect::<Vec<_>>()
|
|
||||||
// .join("")
|
|
||||||
// ),
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
fn is_word_char(c: char) -> bool {
|
fn is_word_char(c: char) -> bool {
|
||||||
if c.is_ascii_alphanumeric() {
|
if c.is_ascii_alphanumeric() {
|
||||||
return true;
|
return true;
|
||||||
|
@ -420,6 +500,7 @@ fn is_word_char(c: char) -> bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_string(s: &'static str, span: SimpleSpan) -> Result<Vec<Spanned<StringPart>>, String> {
|
fn parse_string(s: &'static str, span: SimpleSpan) -> Result<Vec<Spanned<StringPart>>, String> {
|
||||||
|
// println!("parsing string pattern: {s}");
|
||||||
let mut parts = vec![];
|
let mut parts = vec![];
|
||||||
let mut current_part = String::new();
|
let mut current_part = String::new();
|
||||||
let mut start = span.start;
|
let mut start = span.start;
|
||||||
|
@ -488,6 +569,12 @@ fn parse_string(s: &'static str, span: SimpleSpan) -> Result<Vec<Spanned<StringP
|
||||||
StringPart::Inline(current_part),
|
StringPart::Inline(current_part),
|
||||||
SimpleSpan::new(start, span.end),
|
SimpleSpan::new(start, span.end),
|
||||||
))
|
))
|
||||||
|
} else if !current_part.is_empty() {
|
||||||
|
let part_len = current_part.len();
|
||||||
|
parts.push((
|
||||||
|
StringPart::Data(current_part),
|
||||||
|
SimpleSpan::new(start, part_len),
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(parts)
|
Ok(parts)
|
||||||
|
@ -718,7 +805,11 @@ where
|
||||||
.delimited_by(just(Token::Punctuation("(")), just(Token::Punctuation(")")))
|
.delimited_by(just(Token::Punctuation("(")), just(Token::Punctuation(")")))
|
||||||
.map_with(|args, e| (Arguments(args), e.span()));
|
.map_with(|args, e| (Arguments(args), e.span()));
|
||||||
|
|
||||||
let synth_root = word.or(keyword);
|
let or = just(Token::Reserved("or")).map_with(|_, e| (Or, e.span()));
|
||||||
|
|
||||||
|
let and = just(Token::Reserved("and")).map_with(|_, e| (And, e.span()));
|
||||||
|
|
||||||
|
let synth_root = or.or(and).or(word).or(keyword);
|
||||||
|
|
||||||
let synth_term = keyword.or(args);
|
let synth_term = keyword.or(args);
|
||||||
|
|
||||||
|
@ -922,7 +1013,12 @@ where
|
||||||
|
|
||||||
let lambda = just(Token::Reserved("fn"))
|
let lambda = just(Token::Reserved("fn"))
|
||||||
.ignore_then(fn_unguarded.clone())
|
.ignore_then(fn_unguarded.clone())
|
||||||
.map_with(|clause, e| (Fn("anonymous", vec![clause], None), e.span()));
|
.map_with(|clause, e| {
|
||||||
|
(
|
||||||
|
Fn("", Box::new((Ast::FnBody(vec![clause]), e.span())), None),
|
||||||
|
e.span(),
|
||||||
|
)
|
||||||
|
});
|
||||||
|
|
||||||
let fn_clauses = fn_clause
|
let fn_clauses = fn_clause
|
||||||
.clone()
|
.clone()
|
||||||
|
@ -1010,7 +1106,10 @@ where
|
||||||
} else {
|
} else {
|
||||||
unreachable!()
|
unreachable!()
|
||||||
};
|
};
|
||||||
(Fn(name, vec![clause], None), e.span())
|
(
|
||||||
|
Fn(name, Box::new((Ast::FnBody(vec![clause]), e.span())), None),
|
||||||
|
e.span(),
|
||||||
|
)
|
||||||
});
|
});
|
||||||
|
|
||||||
let docstr = select! {Token::String(s) => s};
|
let docstr = select! {Token::String(s) => s};
|
||||||
|
@ -1032,7 +1131,10 @@ where
|
||||||
} else {
|
} else {
|
||||||
unreachable!()
|
unreachable!()
|
||||||
};
|
};
|
||||||
(Fn(name, clauses, docstr), e.span())
|
(
|
||||||
|
Fn(name, Box::new((Ast::FnBody(clauses), e.span())), docstr),
|
||||||
|
e.span(),
|
||||||
|
)
|
||||||
});
|
});
|
||||||
|
|
||||||
let fn_ = fn_named.or(fn_compound).or(fn_decl);
|
let fn_ = fn_named.or(fn_compound).or(fn_decl);
|
||||||
|
|
|
@ -54,7 +54,7 @@ pub struct Process<'src> {
|
||||||
pub input: &'static str,
|
pub input: &'static str,
|
||||||
pub src: &'static str,
|
pub src: &'static str,
|
||||||
pub locals: Vec<(String, Value<'src>)>,
|
pub locals: Vec<(String, Value<'src>)>,
|
||||||
pub prelude: &'src Vec<(String, Value<'src>)>,
|
pub prelude: Vec<(String, Value<'src>)>,
|
||||||
pub ast: &'src Ast,
|
pub ast: &'src Ast,
|
||||||
pub span: SimpleSpan,
|
pub span: SimpleSpan,
|
||||||
pub fn_info: std::collections::HashMap<*const Ast, FnInfo>,
|
pub fn_info: std::collections::HashMap<*const Ast, FnInfo>,
|
||||||
|
@ -296,28 +296,25 @@ impl<'src> Process<'src> {
|
||||||
// can't just use the `caller` value b/c borrow checker nonsense
|
// can't just use the `caller` value b/c borrow checker nonsense
|
||||||
let args = Tuple(args);
|
let args = Tuple(args);
|
||||||
let to = self.locals.len();
|
let to = self.locals.len();
|
||||||
if !f.has_run {
|
let mut f = f.borrow_mut();
|
||||||
for i in 0..f.enclosing.len() {
|
for i in 0..f.enclosing.len() {
|
||||||
let (name, value) = f.enclosing[i].clone();
|
let (name, value) = f.enclosing[i].clone();
|
||||||
if !f.has_run && matches!(value, Value::FnDecl(_)) {
|
if !f.has_run && matches!(value, Value::FnDecl(_)) {
|
||||||
let defined = self.resolve(&name);
|
let defined = self.resolve(&name);
|
||||||
match defined {
|
match defined {
|
||||||
Ok(Value::Fn(defined)) => {
|
Ok(Value::Fn(defined)) => f.enclosing[i] = (name.clone(), Fn(defined)),
|
||||||
f.enclosing[i] = (name.clone(), Fn(defined))
|
Ok(Value::FnDecl(_)) => {
|
||||||
}
|
return self.panic(format!(
|
||||||
Ok(Value::FnDecl(_)) => {
|
"function `{name}` called before it was defined"
|
||||||
return self.panic(format!(
|
))
|
||||||
"function `{name}` called before it was defined"
|
|
||||||
))
|
|
||||||
}
|
|
||||||
|
|
||||||
_ => unreachable!("internal Ludus error"),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_ => unreachable!("internal Ludus error"),
|
||||||
}
|
}
|
||||||
self.locals.push(f.enclosing[i].clone());
|
|
||||||
}
|
}
|
||||||
f.has_run = true;
|
self.locals.push(f.enclosing[i].clone());
|
||||||
}
|
}
|
||||||
|
f.has_run = true;
|
||||||
let input = self.input;
|
let input = self.input;
|
||||||
let src = self.src;
|
let src = self.src;
|
||||||
self.input = f.input;
|
self.input = f.input;
|
||||||
|
|
|
@ -1,18 +1,24 @@
|
||||||
|
// TODO:
|
||||||
|
// * [ ] ensure `or` and `and` never get passed by reference
|
||||||
|
// * [ ] ensure no placeholder in `or` and `and` args
|
||||||
|
// * [ ] ensure loops have fixed arity (no splats)
|
||||||
|
// * [ ] ensure fn pattern splats are always highest (and same) arity
|
||||||
|
|
||||||
use crate::parser::*;
|
use crate::parser::*;
|
||||||
use crate::spans::{Span, Spanned};
|
use crate::spans::{Span, Spanned};
|
||||||
use crate::value::Value;
|
use crate::value::Value;
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub struct VErr {
|
pub struct VErr<'a> {
|
||||||
pub msg: String,
|
pub msg: String,
|
||||||
pub span: Span,
|
pub span: &'a Span,
|
||||||
pub input: &'static str,
|
pub input: &'static str,
|
||||||
pub src: &'static str,
|
pub src: &'static str,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl VErr {
|
impl<'a> VErr<'a> {
|
||||||
pub fn new(msg: String, span: Span, input: &'static str, src: &'static str) -> VErr {
|
pub fn new(msg: String, span: &'a Span, input: &'static str, src: &'static str) -> VErr<'a> {
|
||||||
VErr {
|
VErr {
|
||||||
msg,
|
msg,
|
||||||
span,
|
span,
|
||||||
|
@ -54,13 +60,13 @@ fn match_arities(arities: &HashSet<Arity>, num_args: u8) -> bool {
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub struct Validator<'a> {
|
pub struct Validator<'a> {
|
||||||
pub locals: Vec<(String, Span, FnInfo)>,
|
pub locals: Vec<(String, &'a Span, FnInfo)>,
|
||||||
pub prelude: &'a Vec<(String, Value<'a>)>,
|
pub prelude: imbl::HashMap<&'static str, Value>,
|
||||||
pub input: &'static str,
|
pub input: &'static str,
|
||||||
pub src: &'static str,
|
pub src: &'static str,
|
||||||
pub ast: &'a Ast,
|
pub ast: &'a Ast,
|
||||||
pub span: Span,
|
pub span: &'a Span,
|
||||||
pub errors: Vec<VErr>,
|
pub errors: Vec<VErr<'a>>,
|
||||||
pub fn_info: HashMap<*const Ast, FnInfo>,
|
pub fn_info: HashMap<*const Ast, FnInfo>,
|
||||||
status: VStatus,
|
status: VStatus,
|
||||||
}
|
}
|
||||||
|
@ -68,10 +74,10 @@ pub struct Validator<'a> {
|
||||||
impl<'a> Validator<'a> {
|
impl<'a> Validator<'a> {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
ast: &'a Ast,
|
ast: &'a Ast,
|
||||||
span: Span,
|
span: &'a Span,
|
||||||
input: &'static str,
|
input: &'static str,
|
||||||
src: &'static str,
|
src: &'static str,
|
||||||
prelude: &'a Vec<(String, Value<'a>)>,
|
prelude: imbl::HashMap<&'static str, Value>,
|
||||||
) -> Validator<'a> {
|
) -> Validator<'a> {
|
||||||
Validator {
|
Validator {
|
||||||
input,
|
input,
|
||||||
|
@ -112,7 +118,7 @@ impl<'a> Validator<'a> {
|
||||||
|| self.prelude.iter().any(|(bound, _)| name == *bound)
|
|| self.prelude.iter().any(|(bound, _)| name == *bound)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bound(&self, name: &str) -> Option<&(String, Span, FnInfo)> {
|
fn bound(&self, name: &str) -> Option<&(String, &Span, FnInfo)> {
|
||||||
match self.locals.iter().rev().find(|(bound, ..)| name == bound) {
|
match self.locals.iter().rev().find(|(bound, ..)| name == bound) {
|
||||||
Some(binding) => Some(binding),
|
Some(binding) => Some(binding),
|
||||||
None => None,
|
None => None,
|
||||||
|
@ -146,7 +152,7 @@ impl<'a> Validator<'a> {
|
||||||
fn visit(&mut self, node: &'a Spanned<Ast>) {
|
fn visit(&mut self, node: &'a Spanned<Ast>) {
|
||||||
let (expr, span) = node;
|
let (expr, span) = node;
|
||||||
self.ast = expr;
|
self.ast = expr;
|
||||||
self.span = *span;
|
self.span = span;
|
||||||
self.validate();
|
self.validate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -165,7 +171,7 @@ impl<'a> Validator<'a> {
|
||||||
Interpolated(parts) => {
|
Interpolated(parts) => {
|
||||||
for part in parts {
|
for part in parts {
|
||||||
if let (StringPart::Word(name), span) = part {
|
if let (StringPart::Word(name), span) = part {
|
||||||
self.span = *span;
|
self.span = span;
|
||||||
if !self.resolved(name.as_str()) {
|
if !self.resolved(name.as_str()) {
|
||||||
self.err(format!("unbound name `{name}`"));
|
self.err(format!("unbound name `{name}`"));
|
||||||
} else {
|
} else {
|
||||||
|
@ -278,6 +284,9 @@ impl<'a> Validator<'a> {
|
||||||
// check arity against fn info if first term is word and second term is args
|
// check arity against fn info if first term is word and second term is args
|
||||||
Synthetic(first, second, rest) => {
|
Synthetic(first, second, rest) => {
|
||||||
match (&first.0, &second.0) {
|
match (&first.0, &second.0) {
|
||||||
|
(Ast::And, Ast::Arguments(_)) | (Ast::Or, Ast::Arguments(_)) => {
|
||||||
|
self.visit(second.as_ref())
|
||||||
|
}
|
||||||
(Ast::Word(_), Ast::Keyword(_)) => self.visit(first.as_ref()),
|
(Ast::Word(_), Ast::Keyword(_)) => self.visit(first.as_ref()),
|
||||||
(Ast::Keyword(_), Ast::Arguments(args)) => {
|
(Ast::Keyword(_), Ast::Arguments(args)) => {
|
||||||
if args.len() != 1 {
|
if args.len() != 1 {
|
||||||
|
@ -298,7 +307,10 @@ impl<'a> Validator<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => unreachable!(),
|
_ => unreachable!(
|
||||||
|
"malformed synthetic root with\nfirst: {}\nsecond: {}",
|
||||||
|
first.0, second.0
|
||||||
|
),
|
||||||
}
|
}
|
||||||
for term in rest {
|
for term in rest {
|
||||||
self.visit(term);
|
self.visit(term);
|
||||||
|
@ -362,7 +374,8 @@ impl<'a> Validator<'a> {
|
||||||
self.declare_fn(name.to_string());
|
self.declare_fn(name.to_string());
|
||||||
self.status.tail_position = tailpos;
|
self.status.tail_position = tailpos;
|
||||||
}
|
}
|
||||||
Fn(name, clauses, ..) => {
|
FnBody(..) => unreachable!(),
|
||||||
|
Fn(name, body, ..) => {
|
||||||
let mut is_declared = false;
|
let mut is_declared = false;
|
||||||
match self.bound(name) {
|
match self.bound(name) {
|
||||||
Some((_, _, FnInfo::Declared)) => is_declared = true,
|
Some((_, _, FnInfo::Declared)) => is_declared = true,
|
||||||
|
@ -380,22 +393,21 @@ impl<'a> Validator<'a> {
|
||||||
let from = self.status.used_bindings.len();
|
let from = self.status.used_bindings.len();
|
||||||
let mut arities = HashSet::new();
|
let mut arities = HashSet::new();
|
||||||
|
|
||||||
|
let (Ast::FnBody(clauses), _) = body.as_ref() else {
|
||||||
|
unreachable!()
|
||||||
|
};
|
||||||
|
|
||||||
for clause in clauses {
|
for clause in clauses {
|
||||||
// TODO: validate all parts of clauses
|
// we have to do this explicitly here because of arity checking
|
||||||
let (expr, span) = clause;
|
let (expr, span) = clause;
|
||||||
self.ast = expr;
|
self.ast = expr;
|
||||||
self.span = *span;
|
self.span = span;
|
||||||
// add clause arity to arities
|
// add clause arity to arities
|
||||||
arities.insert(self.arity());
|
arities.insert(self.arity());
|
||||||
self.validate();
|
self.validate();
|
||||||
}
|
}
|
||||||
|
|
||||||
// this should be right
|
// collect info about what the function closes over
|
||||||
// we can't bind anything that's already bound,
|
|
||||||
// even in arg names
|
|
||||||
// so anything that is already bound and used
|
|
||||||
// will, of necessity, be closed over
|
|
||||||
// we don't want to try to close over locals in functions
|
|
||||||
let mut closed_over = HashSet::new();
|
let mut closed_over = HashSet::new();
|
||||||
for binding in self.status.used_bindings.iter().skip(from) {
|
for binding in self.status.used_bindings.iter().skip(from) {
|
||||||
if self.bound(binding.as_str()).is_some() {
|
if self.bound(binding.as_str()).is_some() {
|
||||||
|
@ -458,7 +470,7 @@ impl<'a> Validator<'a> {
|
||||||
for clause in body {
|
for clause in body {
|
||||||
let (expr, span) = clause;
|
let (expr, span) = clause;
|
||||||
self.ast = expr;
|
self.ast = expr;
|
||||||
self.span = *span;
|
self.span = span;
|
||||||
let arity = self.arity();
|
let arity = self.arity();
|
||||||
// dbg!(&arity);
|
// dbg!(&arity);
|
||||||
match arity {
|
match arity {
|
||||||
|
@ -511,7 +523,7 @@ impl<'a> Validator<'a> {
|
||||||
InterpolatedPattern(parts, _) => {
|
InterpolatedPattern(parts, _) => {
|
||||||
for (part, span) in parts {
|
for (part, span) in parts {
|
||||||
if let StringPart::Word(name) = part {
|
if let StringPart::Word(name) = part {
|
||||||
self.span = *span;
|
self.span = span;
|
||||||
match self.bound(name) {
|
match self.bound(name) {
|
||||||
Some(_) => self.err(format!("name `{name}` is already bound")),
|
Some(_) => self.err(format!("name `{name}` is already bound")),
|
||||||
None => self.bind(name.to_string()),
|
None => self.bind(name.to_string()),
|
||||||
|
@ -543,7 +555,7 @@ impl<'a> Validator<'a> {
|
||||||
(PlaceholderPattern, _) => (),
|
(PlaceholderPattern, _) => (),
|
||||||
(WordPattern(name), span) => match self.bound(name) {
|
(WordPattern(name), span) => match self.bound(name) {
|
||||||
Some(_) => {
|
Some(_) => {
|
||||||
self.span = *span;
|
self.span = span;
|
||||||
self.err(format!("name `{name}` is already bound"))
|
self.err(format!("name `{name}` is already bound"))
|
||||||
}
|
}
|
||||||
None => self.bind(name.to_string()),
|
None => self.bind(name.to_string()),
|
||||||
|
@ -570,7 +582,7 @@ impl<'a> Validator<'a> {
|
||||||
}
|
}
|
||||||
PairPattern(_, patt) => self.visit(patt.as_ref()),
|
PairPattern(_, patt) => self.visit(patt.as_ref()),
|
||||||
// terminals can never be invalid
|
// terminals can never be invalid
|
||||||
Nil | Boolean(_) | Number(_) | Keyword(_) | String(_) => (),
|
Nil | Boolean(_) | Number(_) | Keyword(_) | String(_) | And | Or => (),
|
||||||
// terminal patterns can never be invalid
|
// terminal patterns can never be invalid
|
||||||
NilPattern | BooleanPattern(..) | NumberPattern(..) | StringPattern(..)
|
NilPattern | BooleanPattern(..) | NumberPattern(..) | StringPattern(..)
|
||||||
| KeywordPattern(..) | PlaceholderPattern => (),
|
| KeywordPattern(..) | PlaceholderPattern => (),
|
||||||
|
|
485
src/value.rs
485
src/value.rs
|
@ -1,193 +1,332 @@
|
||||||
use crate::base::*;
|
use crate::base::BaseFn;
|
||||||
use crate::parser::*;
|
use crate::chunk::Chunk;
|
||||||
use crate::spans::*;
|
// use crate::parser::Ast;
|
||||||
use imbl::*;
|
// use crate::spans::Spanned;
|
||||||
|
use imbl::{HashMap, Vector};
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::fmt;
|
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use struct_scalpel::Dissectible;
|
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Fn<'src> {
|
pub enum LFn {
|
||||||
pub name: String,
|
Declared {
|
||||||
pub body: &'src Vec<Spanned<Ast>>,
|
name: &'static str,
|
||||||
pub doc: Option<String>,
|
},
|
||||||
pub enclosing: Vec<(String, Value<'src>)>,
|
Defined {
|
||||||
pub has_run: bool,
|
name: &'static str,
|
||||||
pub input: &'static str,
|
doc: Option<&'static str>,
|
||||||
pub src: &'static str,
|
arities: Vec<u8>,
|
||||||
|
chunks: Vec<Chunk>,
|
||||||
|
splat: u8,
|
||||||
|
closed: RefCell<Vec<Value>>,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Dissectible)]
|
impl LFn {
|
||||||
pub enum Value<'src> {
|
pub fn close(&self, value: Value) {
|
||||||
|
match self {
|
||||||
|
LFn::Declared { .. } => unreachable!(),
|
||||||
|
LFn::Defined { closed, .. } => {
|
||||||
|
let shown = value.show();
|
||||||
|
closed.borrow_mut().push(value);
|
||||||
|
let pos = closed.borrow().len();
|
||||||
|
if crate::DEBUG_SCRIPT_RUN {
|
||||||
|
println!("closing over in {} at {pos}: {shown}", self.name(),);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn doc(&self) -> Value {
|
||||||
|
match self {
|
||||||
|
LFn::Declared { name } => {
|
||||||
|
Value::String(Rc::new(format!("fn {name}: undefined function")))
|
||||||
|
}
|
||||||
|
LFn::Defined {
|
||||||
|
name,
|
||||||
|
doc: Some(doc),
|
||||||
|
..
|
||||||
|
} => Value::String(Rc::new(format!("fn {name}\n{doc}"))),
|
||||||
|
LFn::Defined { name, .. } => {
|
||||||
|
Value::String(Rc::new(format!("fn {name}: no documentation")))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn accepts(&self, arity: u8) -> bool {
|
||||||
|
match self {
|
||||||
|
LFn::Defined { arities, splat, .. } => {
|
||||||
|
if arities.contains(&arity) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if *splat == 0 {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
let max_arity = arities.iter().fold(0, |a, b| a.max(*b));
|
||||||
|
arity > max_arity
|
||||||
|
}
|
||||||
|
LFn::Declared { .. } => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn splat_arity(&self) -> u8 {
|
||||||
|
match self {
|
||||||
|
LFn::Defined { splat, .. } => *splat,
|
||||||
|
LFn::Declared { .. } => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn name(&self) -> &'static str {
|
||||||
|
match self {
|
||||||
|
LFn::Declared { name } | LFn::Defined { name, .. } => name,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn chunk(&self, arity: u8) -> &Chunk {
|
||||||
|
match self {
|
||||||
|
LFn::Declared { .. } => unreachable!(),
|
||||||
|
LFn::Defined {
|
||||||
|
arities,
|
||||||
|
splat,
|
||||||
|
chunks,
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
let chunk_pos = arities.iter().position(|a| arity == *a);
|
||||||
|
match chunk_pos {
|
||||||
|
Some(pos) => &chunks[pos],
|
||||||
|
None => &chunks[*splat as usize],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn upvalue(&self, idx: u8) -> Value {
|
||||||
|
match self {
|
||||||
|
LFn::Declared { .. } => unreachable!(),
|
||||||
|
LFn::Defined { closed, .. } => closed.borrow()[idx as usize].clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
|
pub struct Partial {
|
||||||
|
pub args: Vec<Value>,
|
||||||
|
pub name: &'static str,
|
||||||
|
pub function: Value,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub enum Value {
|
||||||
|
Nothing,
|
||||||
Nil,
|
Nil,
|
||||||
Placeholder,
|
True,
|
||||||
Boolean(bool),
|
False,
|
||||||
Number(f64),
|
|
||||||
Keyword(&'static str),
|
Keyword(&'static str),
|
||||||
InternedString(&'static str),
|
Interned(&'static str),
|
||||||
AllocatedString(Rc<String>),
|
String(Rc<String>),
|
||||||
// on the heap for now
|
Number(f64),
|
||||||
Tuple(Rc<Vec<Self>>),
|
Tuple(Rc<Vec<Value>>),
|
||||||
Args(Rc<Vec<Self>>),
|
List(Box<Vector<Value>>),
|
||||||
List(Vector<Self>),
|
Dict(Box<HashMap<&'static str, Value>>),
|
||||||
Dict(HashMap<&'static str, Self>),
|
Box(Rc<RefCell<Value>>),
|
||||||
Box(&'static str, Rc<RefCell<Self>>),
|
Fn(Rc<LFn>),
|
||||||
Fn(Rc<Fn<'src>>),
|
BaseFn(BaseFn),
|
||||||
FnDecl(&'static str),
|
Partial(Rc<Partial>),
|
||||||
Base(BaseFn<'src>),
|
|
||||||
Recur(Vec<Self>),
|
|
||||||
// Set(HashSet<Self>),
|
|
||||||
// Sets are hard
|
|
||||||
// Sets require Eq
|
|
||||||
// Eq is not implemented on f64, because NaNs
|
|
||||||
// We could use ordered_float::NotNan
|
|
||||||
// Let's defer that
|
|
||||||
// We're not really using sets in Ludus
|
|
||||||
|
|
||||||
// Other things we're not implementing yet:
|
|
||||||
// pkgs, nses, tests
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'src> Clone for Value<'src> {
|
impl PartialEq for Value {
|
||||||
fn clone(&self) -> Value<'src> {
|
fn eq(&self, other: &Value) -> bool {
|
||||||
match self {
|
use Value::*;
|
||||||
Value::Nil => Value::Nil,
|
|
||||||
Value::Boolean(b) => Value::Boolean(*b),
|
|
||||||
Value::InternedString(s) => Value::InternedString(s),
|
|
||||||
Value::AllocatedString(s) => Value::AllocatedString(s.clone()),
|
|
||||||
Value::Keyword(s) => Value::Keyword(s),
|
|
||||||
Value::Number(n) => Value::Number(*n),
|
|
||||||
Value::Tuple(t) => Value::Tuple(t.clone()),
|
|
||||||
Value::Args(a) => Value::Args(a.clone()),
|
|
||||||
Value::Fn(f) => Value::Fn(f.clone()),
|
|
||||||
Value::FnDecl(name) => Value::FnDecl(name),
|
|
||||||
Value::List(l) => Value::List(l.clone()),
|
|
||||||
Value::Dict(d) => Value::Dict(d.clone()),
|
|
||||||
Value::Box(name, b) => Value::Box(name, b.clone()),
|
|
||||||
Value::Placeholder => Value::Placeholder,
|
|
||||||
Value::Base(b) => Value::Base(b.clone()),
|
|
||||||
Value::Recur(..) => unreachable!(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display for Value<'_> {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
match self {
|
|
||||||
Value::Nil => write!(f, "nil"),
|
|
||||||
Value::Boolean(b) => write!(f, "{b}"),
|
|
||||||
Value::Number(n) => write!(f, "{n}"),
|
|
||||||
Value::Keyword(k) => write!(f, ":{k}"),
|
|
||||||
Value::InternedString(s) => write!(f, "\"{s}\""),
|
|
||||||
Value::AllocatedString(s) => write!(f, "\"{s}\""),
|
|
||||||
Value::Fn(fun) => write!(f, "fn {}", fun.borrow().name),
|
|
||||||
Value::FnDecl(name) => write!(f, "fn {name}"),
|
|
||||||
Value::Tuple(t) | Value::Args(t) => write!(
|
|
||||||
f,
|
|
||||||
"({})",
|
|
||||||
t.iter()
|
|
||||||
.map(|x| x.to_string())
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join(", ")
|
|
||||||
),
|
|
||||||
Value::List(l) => write!(
|
|
||||||
f,
|
|
||||||
"[{}]",
|
|
||||||
l.iter()
|
|
||||||
.map(|x| x.to_string())
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join(", ")
|
|
||||||
),
|
|
||||||
Value::Dict(d) => write!(
|
|
||||||
f,
|
|
||||||
"#{{{}}}",
|
|
||||||
d.iter()
|
|
||||||
.map(|(k, v)| format!(":{k} {v}"))
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join(", ")
|
|
||||||
),
|
|
||||||
Value::Box(name, value) => {
|
|
||||||
write!(
|
|
||||||
f,
|
|
||||||
"box {}: [{}]",
|
|
||||||
name,
|
|
||||||
&value.try_borrow().unwrap().to_string()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
Value::Placeholder => write!(f, "_"),
|
|
||||||
Value::Base(..) => unreachable!(),
|
|
||||||
Value::Recur(..) => unreachable!(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Value<'_> {
|
|
||||||
pub fn bool(&self) -> bool {
|
|
||||||
!matches!(self, Value::Nil | Value::Boolean(false))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'src> PartialEq for Value<'src> {
|
|
||||||
fn eq(&self, other: &Value<'src>) -> bool {
|
|
||||||
match (self, other) {
|
match (self, other) {
|
||||||
// value equality types
|
(Nothing, Nothing) | (Nil, Nil) | (True, True) | (False, False) => true,
|
||||||
(Value::Nil, Value::Nil) => true,
|
(Keyword(str1), Keyword(str2)) | (Interned(str1), Interned(str2)) => str1 == str2,
|
||||||
(Value::Boolean(x), Value::Boolean(y)) => x == y,
|
(String(x), String(y)) => x == y,
|
||||||
(Value::Number(x), Value::Number(y)) => x == y,
|
(String(x), Interned(y)) => x.as_ref() == y,
|
||||||
(Value::InternedString(x), Value::InternedString(y)) => x == y,
|
(Interned(x), String(y)) => x == y.as_ref(),
|
||||||
(Value::AllocatedString(x), Value::AllocatedString(y)) => x == y,
|
(Number(x), Number(y)) => x == y,
|
||||||
(Value::InternedString(x), Value::AllocatedString(y)) => *x == **y,
|
(Tuple(x), Tuple(y)) => x == y,
|
||||||
(Value::AllocatedString(x), Value::InternedString(y)) => **x == *y,
|
(List(x), List(y)) => x == y,
|
||||||
(Value::Keyword(x), Value::Keyword(y)) => x == y,
|
(Dict(x), Dict(y)) => x == y,
|
||||||
(Value::Tuple(x), Value::Tuple(y)) => x == y,
|
(Box(x), Box(y)) => std::ptr::eq(x.as_ref().as_ptr(), y.as_ref().as_ptr()),
|
||||||
(Value::List(x), Value::List(y)) => x == y,
|
(Fn(x), Fn(y)) => std::ptr::eq(x, y),
|
||||||
(Value::Dict(x), Value::Dict(y)) => x == y,
|
(BaseFn(x), BaseFn(y)) => std::ptr::eq(x, y),
|
||||||
// reference equality types
|
(Partial(x), Partial(y)) => x == y,
|
||||||
(Value::Fn(x), Value::Fn(y)) => {
|
|
||||||
Rc::<RefCell<Fn<'_>>>::as_ptr(x) == Rc::<RefCell<Fn<'_>>>::as_ptr(y)
|
|
||||||
}
|
|
||||||
(Value::Box(_, x), Value::Box(_, y)) => {
|
|
||||||
Rc::<RefCell<Value<'_>>>::as_ptr(x) == Rc::<RefCell<Value<'_>>>::as_ptr(y)
|
|
||||||
}
|
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Eq for Value<'_> {}
|
impl std::fmt::Display for Value {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
impl Value<'_> {
|
use Value::*;
|
||||||
pub fn interpolate(&self) -> String {
|
|
||||||
match self {
|
match self {
|
||||||
Value::Nil => String::new(),
|
Nothing => write!(f, "_"),
|
||||||
Value::Boolean(b) => format!("{b}"),
|
Nil => write!(f, "nil"),
|
||||||
Value::Number(n) => format!("{n}"),
|
True => write!(f, "true"),
|
||||||
Value::Keyword(k) => format!(":{k}"),
|
False => write!(f, "false"),
|
||||||
Value::AllocatedString(s) => format!("{s}"),
|
Keyword(str) => write!(f, ":{str}"),
|
||||||
Value::InternedString(s) => s.to_string(),
|
Interned(str) => write!(f, "\"{str}\""),
|
||||||
Value::Box(_, x) => x.borrow().interpolate(),
|
String(str) => write!(f, "\"{str}\""),
|
||||||
Value::Tuple(xs) => xs
|
Number(n) => write!(f, "{n}"),
|
||||||
.iter()
|
Tuple(members) => write!(
|
||||||
.map(|x| x.interpolate())
|
f,
|
||||||
.collect::<Vec<_>>()
|
"({})",
|
||||||
.join(", "),
|
members
|
||||||
Value::List(xs) => xs
|
.iter()
|
||||||
.iter()
|
.map(|x| x.to_string())
|
||||||
.map(|x| x.interpolate())
|
.collect::<Vec<_>>()
|
||||||
.collect::<Vec<_>>()
|
.join(", ")
|
||||||
.join(", "),
|
),
|
||||||
Value::Dict(xs) => xs
|
List(members) => write!(
|
||||||
.iter()
|
f,
|
||||||
.map(|(k, v)| format!(":{} {}", k, v.interpolate()))
|
"[{}]",
|
||||||
.collect::<Vec<_>>()
|
members
|
||||||
.join(", "),
|
.iter()
|
||||||
Value::Fn(x) => format!("fn {}", x.borrow().name),
|
.map(|x| x.to_string())
|
||||||
Value::FnDecl(name) => format!("fn {name}"),
|
.collect::<Vec<_>>()
|
||||||
Value::Placeholder => unreachable!(),
|
.join(", ")
|
||||||
Value::Args(_) => unreachable!(),
|
),
|
||||||
Value::Recur(_) => unreachable!(),
|
Dict(members) => write!(
|
||||||
Value::Base(_) => unreachable!(),
|
f,
|
||||||
|
"#{{{}}}",
|
||||||
|
members
|
||||||
|
.iter()
|
||||||
|
.map(|(k, v)| format!("{k} {v}"))
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(", ")
|
||||||
|
),
|
||||||
|
Box(value) => write!(f, "box {{ {} }}", value.as_ref().borrow()),
|
||||||
|
Fn(lfn) => write!(f, "fn {}", lfn.name()),
|
||||||
|
BaseFn(inner) => {
|
||||||
|
let name = match inner {
|
||||||
|
crate::base::BaseFn::Nullary(name, _)
|
||||||
|
| crate::base::BaseFn::Unary(name, _)
|
||||||
|
| crate::base::BaseFn::Binary(name, _)
|
||||||
|
| crate::base::BaseFn::Ternary(name, _) => name,
|
||||||
|
};
|
||||||
|
write!(f, "fn {name}/base")
|
||||||
|
}
|
||||||
|
Partial(partial) => write!(f, "fn {}/partial", partial.name),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Value {
|
||||||
|
pub fn show(&self) -> String {
|
||||||
|
use Value::*;
|
||||||
|
let mut out = match &self {
|
||||||
|
Nil => "nil".to_string(),
|
||||||
|
True => "true".to_string(),
|
||||||
|
False => "false".to_string(),
|
||||||
|
Number(n) => format!("{n}"),
|
||||||
|
Interned(str) => format!("\"{str}\""),
|
||||||
|
String(str) => {
|
||||||
|
let str_str = str.to_string();
|
||||||
|
format!("\"{str_str}\"")
|
||||||
|
}
|
||||||
|
Keyword(str) => format!(":{str}"),
|
||||||
|
Tuple(t) => {
|
||||||
|
let members = t.iter().map(|e| e.show()).collect::<Vec<_>>().join(", ");
|
||||||
|
format!("({members})")
|
||||||
|
}
|
||||||
|
List(l) => {
|
||||||
|
let members = l.iter().map(|e| e.show()).collect::<Vec<_>>().join(", ");
|
||||||
|
format!("[{members}]")
|
||||||
|
}
|
||||||
|
Dict(d) => {
|
||||||
|
let members = d
|
||||||
|
.iter()
|
||||||
|
.map(|(k, v)| {
|
||||||
|
let key_show = Value::Keyword(k).show();
|
||||||
|
let value_show = v.show();
|
||||||
|
format!("{key_show} {value_show}")
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(", ");
|
||||||
|
format!("#{{{members}}}")
|
||||||
|
}
|
||||||
|
Box(x) => format!("box {{ {} }}", x.as_ref().borrow().show()),
|
||||||
|
Fn(lfn) => format!("fn {}", lfn.name()),
|
||||||
|
Partial(partial) => format!("fn {}/partial", partial.name),
|
||||||
|
BaseFn(_) => format!("{self}"),
|
||||||
|
Nothing => "_".to_string(),
|
||||||
|
};
|
||||||
|
if out.len() > 20 {
|
||||||
|
out.truncate(20);
|
||||||
|
format!("{out}...")
|
||||||
|
} else {
|
||||||
|
out
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn stringify(&self) -> String {
|
||||||
|
use Value::*;
|
||||||
|
match &self {
|
||||||
|
Nil => "nil".to_string(),
|
||||||
|
True => "true".to_string(),
|
||||||
|
False => "false".to_string(),
|
||||||
|
Number(n) => format!("{n}"),
|
||||||
|
Interned(str) => str.to_string(),
|
||||||
|
Keyword(str) => str.to_string(),
|
||||||
|
Tuple(t) => {
|
||||||
|
let members = t
|
||||||
|
.iter()
|
||||||
|
.map(|e| e.stringify())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(", ");
|
||||||
|
members.to_string()
|
||||||
|
}
|
||||||
|
List(l) => {
|
||||||
|
let members = l
|
||||||
|
.iter()
|
||||||
|
.map(|e| e.stringify())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(", ");
|
||||||
|
members.to_string()
|
||||||
|
}
|
||||||
|
Dict(d) => {
|
||||||
|
let members = d
|
||||||
|
.iter()
|
||||||
|
.map(|(k, v)| {
|
||||||
|
let key_show = Value::Keyword(k).stringify();
|
||||||
|
let value_show = v.stringify();
|
||||||
|
format!("{key_show} {value_show}")
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(", ");
|
||||||
|
members.to_string()
|
||||||
|
}
|
||||||
|
String(s) => s.as_ref().clone(),
|
||||||
|
Box(x) => x.as_ref().borrow().stringify(),
|
||||||
|
Fn(lfn) => format!("fn {}", lfn.name()),
|
||||||
|
_ => todo!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn type_of(&self) -> &'static str {
|
||||||
|
use Value::*;
|
||||||
|
match self {
|
||||||
|
Nothing => unreachable!(),
|
||||||
|
Nil => "nil",
|
||||||
|
True => "bool",
|
||||||
|
False => "bool",
|
||||||
|
Keyword(..) => "keyword",
|
||||||
|
Interned(..) => "string",
|
||||||
|
String(..) => "string",
|
||||||
|
Number(..) => "number",
|
||||||
|
Tuple(..) => "tuple",
|
||||||
|
List(..) => "list",
|
||||||
|
Dict(..) => "dict",
|
||||||
|
Box(..) => "box",
|
||||||
|
Fn(..) => "fn",
|
||||||
|
BaseFn(..) => "fn",
|
||||||
|
Partial(..) => "fn",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn as_fn(&self) -> &LFn {
|
||||||
|
match self {
|
||||||
|
Value::Fn(inner) => inner.as_ref(),
|
||||||
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user