Compare commits

..

1 Commits

Author SHA1 Message Date
995be33854 remove workflow 2025-06-28 00:10:28 -04:00
7 changed files with 294 additions and 388 deletions

View File

@@ -1,19 +0,0 @@
name: Trigger Bot Build
on:
push:
branches:
- dev
jobs:
trigger-bot-build:
runs-on: ubuntu-latest
steps:
- name: Send repository dispatch
env:
GITHUB_TOKEN: ${{ secrets.LAMM_TOKEN }}
run: |
curl -X POST -H "Accept: application/vnd.github.everest-preview+json" \
-H "Authorization: token $GITHUB_TOKEN" \
https://api.github.com/repos/minneelyyyy/bot/dispatches \
-d '{"event_type": "lamm-updated"}'

8
Cargo.lock generated
View File

@@ -1,6 +1,6 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
version = 4
[[package]]
name = "aho-corasick"
@@ -13,7 +13,7 @@ dependencies = [
[[package]]
name = "lamm"
version = "0.4.0"
version = "0.1.0"
dependencies = [
"regex",
]
@@ -26,9 +26,9 @@ checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
[[package]]
name = "regex"
version = "1.11.1"
version = "1.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"
checksum = "38200e5ee88914975b69f657f0801b6f6dccafd44fd9326302a4aaeecfacb1d8"
dependencies = [
"aho-corasick",
"memchr",

View File

@@ -77,53 +77,43 @@ export (e phi)
All functions in Lamm are **scoped** similarly to variables. Functions are declared using the `:` operator, which can be extended with more `:` and `.` characters to let Lamm know how many arguments the function takes.
```
: inc x
+ x 1
(inc 24) # => 25
: inc x + x 1
inc 24 # => 25
: sqrt x
** x 0.5
:. pythag a b
(sqrt + ** a 2.0 ** b 2.0)
(pythag 3 4) # => 5
:. pythag a b sqrt + ** a 2.0 ** b 2.0
pythag 3 4 # => 5
:::::. ten'args a b c d e f g h i j
[a b c d e f g h i j]
```
The parameter types and return type of functions can be declared using a special syntax unique to function and lambda definitions.
Calling a function requires parenthises around the call.
```
# Takes an x of `Any` type
: inc x
+ x 1
(inc 12) # => 13
: inc x + x 1
inc 12 # => 13
# Takes an x of `Int` and returns an `Int`
: inc ?. x Int -> Int
+ x 1
(inc 9) # => 10
: inc ?. x Int -> Int + x 1
inc 9 # => 10
```
The `?.` operator is unique to function declarations and is used to specify the type of an argument. There are also first class functions, here is the syntax for it.
```
# Applies a function to any value
:. apply : f x
(f x)
(apply sqrt 9) # => 3
:. apply : f x f x
apply \sqrt 9 # => 3
# Applies a function f which maps an Int to an Int to x
:. apply'int ?: f Int -> Int ?. x Int -> Int
(f x)
(apply'int sqrt 36) # => 6
:. apply'int ?: f Int -> Int ?. x Int -> Int f x
apply'int \sqrt 36 # => 6
```
The `:` operator inside of a function prototype tells Lamm that this argument must be a function where every argument and it's return type are all `Any`. This means that `: f` is essentially syntactic sugar for `?: f Any -> Any`. You can pass a function with just it's identifier.
The `:` operator inside of a function prototype tells Lamm that this argument must be a function where every argument and it's return type are all `Any`. This means that `: f` is essentially syntactic sugar for `?: f Any -> Any`. Also, in order to pass a function to a function, you must use the `\` operator, which tells Lamm not to call the function.
And of course, `:` and `?:` in function prototypes can also be extended depending on the number of arguments the function must take.
And off course, `:` and `?:` in function prototypes can also be extended depending on the number of arguments the function must take.
## Branching
@@ -182,27 +172,27 @@ Using these, you can build a lot of fundamental functional paradigm functions.
```
:. map : f ?. x [] -> []
?? bool x
[+ (f head x) (map f tail x)
+ f head x map \f tail x
empty
(map ;x ** x 2 [1 2 3 4 5 6 7 8 9 10]) # => [1 4 9 16 25 36 49 64 81 100]
map ;x ** x 2 [1 2 3 4 5 6 7 8 9 10] # => [1 4 9 16 25 36 49 64 81 100]
:: iterate : f i count -> []
?? > count 0
[+ i (iterate f (f i) - count 1)
+ i iterate \f f i - count 1
empty
(iterate (+ 1) 0 10) # => [0 1 2 3 4 5 6 7 8 9]
iterate (+ 1) 0 10 # => [0 1 2 3 4 5 6 7 8 9]
:. take ?. n Int ?. x [] -> []
?? > n 0
[+ head x (take - n 1 tail x)
+ head x take - n 1 tail x
empty
(take 3 [1 2 3 4 5]) # => [1 2 3]
take 3 [1 2 3 4 5] # => [1 2 3]
:. take'while ?: pred Any -> Bool ?. x [] -> []
?? && bool x (pred head x)
[+ head x (take'while pred tail x)
?? && bool x pred head x
+ head x take'while \pred tail x
empty
(take'while (> 10) [1 3 5 7 9 11 13 15 16]) # => [1 3 5 7 9]
take'while (> 10) [1 3 5 7 9 11 13 15 16] # => [1 3 5 7 9]
```
## Lambdas

View File

@@ -89,217 +89,227 @@ impl Executor {
locals
}
fn op_error(op: &Op, args: &[Value]) -> Error {
Error::new(format!("no overload of {op} matches the arguments {}",
args.iter().map(|x| format!("{x}")).collect::<Vec<_>>().join(", ")))
}
pub(crate) fn exec(&mut self, tree: ParseTree) -> Result<Value, Error> {
match tree {
ParseTree::Operator(op, args) => {
let args: Vec<Value> = args.into_iter()
.map(|x| self.exec(x)).collect::<Result<_, _>>()?;
match (&op, &args[..]) {
(Op::Add, [x, y]) => match (x, y) {
(Value::Int(x), Value::Int(y)) => Ok(Value::Int(x + y)),
(Value::Float(x), Value::Int(y)) => Ok(Value::Float(x + *y as f64)),
(Value::Int(x), Value::Float(y)) => Ok(Value::Float(*x as f64 + y)),
(Value::Float(x), Value::Float(y)) => Ok(Value::Float(x + y)),
(Value::String(x), Value::String(y)) => Ok(Value::String(format!("{x}{y}"))),
_ => Err(Self::op_error(&op, &args)),
},
(Op::Neg, [x]) => match x {
Value::Int(x) => Ok(Value::Int(-x)),
Value::Float(x) => Ok(Value::Float(-x)),
_ => Err(Self::op_error(&op, &args)),
},
(Op::Concat, [x, y]) => match (x, y) {
(Value::Array(xtype, x), Value::Nil) => Ok(Value::Array(xtype.clone(), x.clone())),
(Value::Nil, Value::Array(xtype, x)) => Ok(Value::Array(xtype.clone(), x.clone())),
(Value::Array(xtype, x), Value::Array(ytype, y)) =>
match op {
Op::Add => match &args[..] {
[Value::Int(x), Value::Int(y)] => Ok(Value::Int(x + y)),
[Value::Float(x), Value::Int(y)] => Ok(Value::Float(x + *y as f64)),
[Value::Int(x), Value::Float(y)] => Ok(Value::Float(*x as f64 + y)),
[Value::Float(x), Value::Float(y)] => Ok(Value::Float(x + y)),
[Value::String(x), Value::String(y)] => Ok(Value::String(format!("{x}{y}"))),
[Value::Array(xtype, x), Value::Array(ytype, y)] => {
if xtype != ytype {
Err(Error::new(format!("expected type {} but found {}", xtype, ytype)))
} else {
return Err(Error::new(format!("expected type {} but found {}", xtype, ytype)));
}
Ok(Value::Array(xtype.clone(), [x.clone(), y.clone()].concat()))
},
_ => Err(Self::op_error(&op, &args)),
[Value::Nil, x] => Ok(x.clone()),
[x, Value::Nil] => Ok(x.clone()),
[Value::Array(t, x), y] => {
let ytype = y.get_type();
if *t != ytype {
return Err(Error::new(format!("expected type {} but found {}", t, ytype)));
}
// NOTE: use y's type instead of the arrays type.
// an `empty` array has Any type, but any value will have a fixed type.
// this converts the empty array into a typed array.
Ok(Value::Array(ytype, [x.clone(), vec![y.clone()]].concat()))
},
(Op::Prepend, [x, y]) => match (x, y) {
(Value::Nil, Value::Array(xtype, x)) => Ok(Value::Array(xtype.clone(), x.clone())),
(x, Value::Array(t, y)) => {
[x, Value::Array(t, y)] => {
let xtype = x.get_type();
if *t != xtype {
Err(Error::new(format!("expected type {} but found {}", t, xtype)))
} else {
return Err(Error::new(format!("expected type {} but found {}", t, xtype)));
}
// NOTE: read above
Ok(Value::Array(xtype, [vec![x.clone()], y.clone()].concat()))
},
_ => Err(Error::new("todo: add".into())),
}
},
_ => Err(Self::op_error(&op, &args)),
},
(Op::Append, [x, y]) => match (x, y) {
(Value::Array(xtype, x), Value::Nil) => Ok(Value::Array(xtype.clone(), x.clone())),
(Value::Array(t, y), x) => {
let xtype = x.get_type();
if *t != xtype {
Err(Error::new(format!("expected type {} but found {}", t, xtype)))
} else {
Ok(Value::Array(xtype, [y.clone(), vec![x.clone()]].concat()))
Op::Sub => match &args[..] {
[Value::Int(x), Value::Int(y)] => Ok(Value::Int(x - y)),
[Value::Int(x), Value::Float(y)] => Ok(Value::Float(*x as f64 - y)),
[Value::Float(x), Value::Int(y)] => Ok(Value::Float(x - *y as f64)),
[Value::Float(x), Value::Float(y)] => Ok(Value::Float(x - y)),
[Value::Nil, x] => Ok(x.clone()),
[x, Value::Nil] => Ok(x.clone()),
_ => Err(Error::new("todo: actual error output".into())),
}
},
_ => Err(Self::op_error(&op, &args)),
},
(Op::Insert, [Value::Int(idx), x, Value::Array(t, y)]) => {
let mut y = y.clone();
let xtype = x.get_type();
if *t != xtype {
Err(Error::new(format!("expected type {} but found {}", t, xtype)))
} else if *idx as usize > y.len() {
Err(Error::new("attempt to insert out of array len".into()))
} else {
y.insert(*idx as usize, x.clone());
Ok(Value::Array(t.clone(), y))
Op::Mul => match &args[..] {
[Value::Int(x), Value::Int(y)] => Ok(Value::Int(x * y)),
[Value::Int(x), Value::Float(y)] => Ok(Value::Float(*x as f64 * y)),
[Value::Float(x), Value::Int(y)] => Ok(Value::Float(x * *y as f64)),
[Value::Float(x), Value::Float(y)] => Ok(Value::Float(x * y)),
[Value::Nil, x] => Ok(x.clone()),
[x, Value::Nil] => Ok(x.clone()),
_ => Err(Error::new("todo: actual error output".into())),
}
},
(Op::Sub, [x, y]) => match (x, y) {
(Value::Int(x), Value::Int(y)) => Ok(Value::Int(x - y)),
(Value::Int(x), Value::Float(y)) => Ok(Value::Float(*x as f64 - y)),
(Value::Float(x), Value::Int(y)) => Ok(Value::Float(x - *y as f64)),
(Value::Float(x), Value::Float(y)) => Ok(Value::Float(x - y)),
_ => Err(Self::op_error(&op, &args)),
},
(Op::Mul, [x, y]) => match (x, y) {
(Value::Int(x), Value::Int(y)) => Ok(Value::Int(x * y)),
(Value::Int(x), Value::Float(y)) => Ok(Value::Float(*x as f64 * y)),
(Value::Float(x), Value::Int(y)) => Ok(Value::Float(x * *y as f64)),
(Value::Float(x), Value::Float(y)) => Ok(Value::Float(x * y)),
_ => Err(Self::op_error(&op, &args)),
},
(Op::Div, [x, y]) => match (x, y) {
(Value::Int(x), Value::Int(y)) => Ok(Value::Float(*x as f64 / *y as f64)),
(Value::Float(x), Value::Int(y)) => Ok(Value::Float(x / *y as f64)),
(Value::Int(x), Value::Float(y)) => Ok(Value::Float(*x as f64 / y)),
(Value::Float(x), Value::Float(y)) => Ok(Value::Float(x / y)),
_ => Err(Self::op_error(&op, &args)),
},
(Op::FloorDiv, [x, y]) => match (x, y) {
(Value::Int(x), Value::Int(y)) => Ok(Value::Int(x / y)),
(Value::Float(x), Value::Int(y)) => Ok(Value::Int(*x as i64 / y)),
(Value::Int(x), Value::Float(y)) => Ok(Value::Int(x / *y as i64)),
(Value::Float(x), Value::Float(y)) => Ok(Value::Int(*x as i64 / *y as i64)),
_ => Err(Self::op_error(&op, &args)),
},
(Op::Exp, [x, y]) => match (x, y) {
(Value::Int(x), Value::Int(y)) => Ok(Value::Float((*x as f64).powf(*y as f64))),
(Value::Float(x), Value::Int(y)) => Ok(Value::Float(x.powf(*y as f64))),
(Value::Int(x), Value::Float(y)) => Ok(Value::Float((*x as f64).powf(*y))),
(Value::Float(x), Value::Float(y)) => Ok(Value::Float(x.powf(*y))),
_ => Err(Self::op_error(&op, &args)),
},
(Op::Mod, [x, y]) => match (x, y) {
(Value::Int(x), Value::Int(y)) => Ok(Value::Int(x % y)),
(Value::Float(x), Value::Int(y)) => Ok(Value::Float(x % *y as f64)),
(Value::Int(x), Value::Float(y)) => Ok(Value::Float(*x as f64 % y)),
(Value::Float(x), Value::Float(y)) => Ok(Value::Float(x % y)),
_ => Err(Self::op_error(&op, &args)),
},
(Op::GreaterThan, [x, y]) => match (x, y) {
(Value::Int(x), Value::Int(y)) => Ok(Value::Bool(x > y)),
(Value::Float(x), Value::Int(y)) => Ok(Value::Bool(*x > *y as f64)),
(Value::Int(x), Value::Float(y)) => Ok(Value::Bool(*x as f64 > *y)),
(Value::Float(x), Value::Float(y)) => Ok(Value::Bool(x > y)),
_ => Err(Self::op_error(&op, &args)),
},
(Op::GreaterThanOrEqualTo, [x, y]) => match (x, y) {
(Value::Int(x), Value::Int(y)) => Ok(Value::Bool(x >= y)),
(Value::Float(x), Value::Int(y)) => Ok(Value::Bool(*x >= *y as f64)),
(Value::Int(x), Value::Float(y)) => Ok(Value::Bool(*x as f64 >= *y)),
(Value::Float(x), Value::Float(y)) => Ok(Value::Bool(x >= y)),
_ => Err(Self::op_error(&op, &args)),
},
(Op::LessThan, [x, y]) => match (x, y) {
(Value::Int(x), Value::Int(y)) => Ok(Value::Bool(x < y)),
(Value::Float(x), Value::Int(y)) => Ok(Value::Bool(*x < *y as f64)),
(Value::Int(x), Value::Float(y)) => Ok(Value::Bool((*x as f64) < *y)),
(Value::Float(x), Value::Float(y)) => Ok(Value::Bool(x < y)),
_ => Err(Self::op_error(&op, &args)),
},
(Op::LessThanOrEqualTo, [x, y]) => match (x, y) {
(Value::Int(x), Value::Int(y)) => Ok(Value::Bool(x <= y)),
(Value::Float(x), Value::Int(y)) => Ok(Value::Bool(*x <= *y as f64)),
(Value::Int(x), Value::Float(y)) => Ok(Value::Bool(*x as f64 <= *y)),
(Value::Float(x), Value::Float(y)) => Ok(Value::Bool(x <= y)),
_ => Err(Self::op_error(&op, &args)),
},
(Op::EqualTo, [x, y]) => match (x, y) {
(Value::Int(x), Value::Int(y)) => Ok(Value::Bool(x == y)),
(Value::Float(x), Value::Int(y)) => Ok(Value::Bool(*x == *y as f64)),
(Value::Int(x), Value::Float(y)) => Ok(Value::Bool(*x as f64 == *y)),
(Value::Float(x), Value::Float(y)) => Ok(Value::Bool(x == y)),
_ => Err(Self::op_error(&op, &args)),
},
(Op::NotEqualTo, [x, y]) => match (x, y) {
(Value::Int(x), Value::Int(y)) => Ok(Value::Bool(x != y)),
(Value::Float(x), Value::Int(y)) => Ok(Value::Bool(*x != *y as f64)),
(Value::Int(x), Value::Float(y)) => Ok(Value::Bool(*x as f64 != *y)),
(Value::Float(x), Value::Float(y)) => Ok(Value::Bool(x != y)),
_ => Err(Self::op_error(&op, &args)),
},
(Op::Not, [Value::Bool(b)]) => Ok(Value::Bool(!b)),
(Op::Or, [x, y]) => match (x, y) {
(Value::Bool(x), Value::Bool(y)) => Ok(Value::Bool(*x || *y)),
_ => Err(Self::op_error(&op, &args)),
},
(Op::And, [x, y]) => match (x, y) {
(Value::Bool(x), Value::Bool(y)) => Ok(Value::Bool(*x && *y)),
_ => Err(Self::op_error(&op, &args)),
},
(Op::Compose, [_, v]) => Ok(v.clone()),
(Op::Head, [Value::Array(_, x)]) => Ok(x.first().ok_or(Error::new("passed an empty array to head".into()))?.clone()),
(Op::Tail, [Value::Array(t, x)]) => Ok(Value::Array(t.clone(), if x.len() > 0 { x[1..].to_vec() } else { vec![] })),
(Op::Init, [Value::Array(t, x)]) => Ok(Value::Array(t.clone(), if x.len() > 0 { x[..x.len() - 1].to_vec() } else { vec![] })),
(Op::Fini, [Value::Array(_, x)]) => Ok(x.last().ok_or(Error::new("passed an empty array to fini".into()))?.clone()),
(Op::Id, [x]) => Ok(x.clone()),
(Op::IntCast, [x]) => match x {
Op::Div => match &args[..] {
[Value::Int(x), Value::Int(y)] => Ok(Value::Float(*x as f64 / *y as f64)),
[Value::Float(x), Value::Int(y)] => Ok(Value::Float(x / *y as f64)),
[Value::Int(x), Value::Float(y)] => Ok(Value::Float(*x as f64 / y)),
[Value::Float(x), Value::Float(y)] => Ok(Value::Float(x / y)),
[Value::Nil, x] => Ok(x.clone()),
[x, Value::Nil] => Ok(x.clone()),
_ => Err(Error::new("todo: actual error output".into())),
}
Op::FloorDiv => match &args[..] {
[Value::Int(x), Value::Int(y)] => Ok(Value::Int(x / y)),
[Value::Float(x), Value::Int(y)] => Ok(Value::Int(*x as i64 / y)),
[Value::Int(x), Value::Float(y)] => Ok(Value::Int(x / *y as i64)),
[Value::Float(x), Value::Float(y)] => Ok(Value::Int(*x as i64 / *y as i64)),
[Value::Nil, x] => Ok(x.clone()),
[x, Value::Nil] => Ok(x.clone()),
_ => Err(Error::new("todo: actual error output".into())),
}
Op::Exp => match &args[..] {
[Value::Int(x), Value::Int(y)] => Ok(Value::Float((*x as f64).powf(*y as f64))),
[Value::Float(x), Value::Int(y)] => Ok(Value::Float(x.powf(*y as f64))),
[Value::Int(x), Value::Float(y)] => Ok(Value::Float((*x as f64).powf(*y))),
[Value::Float(x), Value::Float(y)] => Ok(Value::Float(x.powf(*y))),
[Value::Nil, x] => Ok(x.clone()),
[x, Value::Nil] => Ok(x.clone()),
_ => Err(Error::new("todo: fsadfdsf".into())),
}
Op::Mod => match &args[..] {
[Value::Int(x), Value::Int(y)] => Ok(Value::Int(x % y)),
[Value::Float(x), Value::Int(y)] => Ok(Value::Float(x % *y as f64)),
[Value::Int(x), Value::Float(y)] => Ok(Value::Float(*x as f64 % y)),
[Value::Float(x), Value::Float(y)] => Ok(Value::Float(x % y)),
[Value::Nil, x] => Ok(x.clone()),
[x, Value::Nil] => Ok(x.clone()),
_ => Err(Error::new("todo: actual error output".into())),
}
Op::GreaterThan => match &args[..] {
[Value::Int(x), Value::Int(y)] => Ok(Value::Bool(x > y)),
[Value::Float(x), Value::Int(y)] => Ok(Value::Bool(*x > *y as f64)),
[Value::Int(x), Value::Float(y)] => Ok(Value::Bool(*x as f64 > *y)),
[Value::Float(x), Value::Float(y)] => Ok(Value::Bool(x > y)),
_ => Err(Error::new("todo: actual error output".into())),
}
Op::GreaterThanOrEqualTo => match &args[..] {
[Value::Int(x), Value::Int(y)] => Ok(Value::Bool(x >= y)),
[Value::Float(x), Value::Int(y)] => Ok(Value::Bool(*x >= *y as f64)),
[Value::Int(x), Value::Float(y)] => Ok(Value::Bool(*x as f64 >= *y)),
[Value::Float(x), Value::Float(y)] => Ok(Value::Bool(x >= y)),
_ => Err(Error::new("todo: actual error output".into())),
}
Op::LessThan => match &args[..] {
[Value::Int(x), Value::Int(y)] => Ok(Value::Bool(x < y)),
[Value::Float(x), Value::Int(y)] => Ok(Value::Bool(*x < *y as f64)),
[Value::Int(x), Value::Float(y)] => Ok(Value::Bool((*x as f64) < *y)),
[Value::Float(x), Value::Float(y)] => Ok(Value::Bool(x < y)),
_ => Err(Error::new("todo: actual error output".into())),
}
Op::LessThanOrEqualTo => match &args[..] {
[Value::Int(x), Value::Int(y)] => Ok(Value::Bool(x <= y)),
[Value::Float(x), Value::Int(y)] => Ok(Value::Bool(*x <= *y as f64)),
[Value::Int(x), Value::Float(y)] => Ok(Value::Bool(*x as f64 <= *y)),
[Value::Float(x), Value::Float(y)] => Ok(Value::Bool(x <= y)),
_ => Err(Error::new("todo: actual error output".into())),
}
Op::EqualTo => match &args[..] {
[Value::Int(x), Value::Int(y)] => Ok(Value::Bool(x == y)),
[Value::Float(x), Value::Int(y)] => Ok(Value::Bool(*x == *y as f64)),
[Value::Int(x), Value::Float(y)] => Ok(Value::Bool(*x as f64 == *y)),
[Value::Float(x), Value::Float(y)] => Ok(Value::Bool(x == y)),
_ => Err(Error::new("todo: actual error output".into())),
}
Op::NotEqualTo => match &args[..] {
[Value::Int(x), Value::Int(y)] => Ok(Value::Bool(x != y)),
[Value::Float(x), Value::Int(y)] => Ok(Value::Bool(*x != *y as f64)),
[Value::Int(x), Value::Float(y)] => Ok(Value::Bool(*x as f64 != *y)),
[Value::Float(x), Value::Float(y)] => Ok(Value::Bool(x != y)),
_ => Err(Error::new("todo: actual error output".into())),
}
Op::Not => match &args[0] {
Value::Bool(b) => Ok(Value::Bool(!b)),
_ => Err(Error::new("todo: actual error output".into())),
}
Op::Or => match &args[..] {
[Value::Bool(x), Value::Bool(y)] => Ok(Value::Bool(*x || *y)),
[Value::Nil, x] => Ok(x.clone()),
[x, Value::Nil] => Ok(x.clone()),
_ => Err(Error::new("todo: actual error output".into())),
}
Op::And => match &args[..] {
[Value::Bool(x), Value::Bool(y)] => Ok(Value::Bool(*x && *y)),
[Value::Nil, x] => Ok(x.clone()),
[x, Value::Nil] => Ok(x.clone()),
_ => Err(Error::new("todo: actual error output".into())),
}
Op::Compose => match &args[..] {
[_, v] => Ok(v.clone()),
_ => Err(Error::new("todo: actual error output".into())),
}
Op::Head => match &args[0] {
Value::Array(_, x) => Ok(x.first().ok_or(Error::new(format!("passed an empty array to head")))?.clone()),
_ => Err(Error::new("head".into())),
}
Op::Tail => match &args[0] {
Value::Array(t, x) => Ok(Value::Array(t.clone(), if x.len() > 0 { x[1..].to_vec() } else { vec![] })),
_ => Err(Error::new("tail".into())),
}
Op::Init => match &args[0] {
Value::Array(t, x) => Ok(Value::Array(t.clone(), if x.len() > 0 { x[..x.len() - 1].to_vec() } else { vec![] })),
_ => Err(Error::new("init".into())),
}
Op::Fini => match &args[0] {
Value::Array(_, x) => Ok(x.last().ok_or(Error::new(format!("passed an empty array to fini")))?.clone()),
_ => Err(Error::new("fini".into())),
}
Op::Id => match &args[0] {
x => Ok(x.clone()),
}
Op::IntCast => match &args[0] {
Value::Int(x) => Ok(Value::Int(*x)),
Value::Float(x) => Ok(Value::Int(*x as i64)),
Value::Bool(x) => Ok(Value::Int(if *x { 1 } else { 0 })),
Value::String(x) => {
let r: i64 = x.parse().map_err(|_| Error::new(format!("failed to parse {} into {}", x, Type::Int)))?;
Ok(Value::Int(r))
},
_ => Err(Error::new(format!("no possible conversion from {} into {}", x, Type::Int))),
},
(Op::FloatCast, [x]) => match x {
}
x => Err(Error::new(format!("no possible conversion from {} into {}", x, Type::Int))),
}
Op::FloatCast => match &args[0] {
Value::Int(x) => Ok(Value::Float(*x as f64)),
Value::Float(x) => Ok(Value::Float(*x)),
Value::Bool(x) => Ok(Value::Float(if *x { 1.0 } else { 0.0 })),
Value::String(x) => {
let r: f64 = x.parse().map_err(|_| Error::new(format!("failed to parse {} into {}", x, Type::Float)))?;
Ok(Value::Float(r))
},
_ => Err(Error::new(format!("no possible conversion from {} into {}", x, Type::Float))),
},
(Op::BoolCast, [x]) => match x {
}
x => Err(Error::new(format!("no possible conversion from {} into {}", x, Type::Float))),
}
Op::BoolCast => match &args[0] {
Value::Int(x) => Ok(Value::Bool(*x != 0)),
Value::Float(x) => Ok(Value::Bool(*x != 0.0)),
Value::Bool(x) => Ok(Value::Bool(*x)),
Value::String(x) => Ok(Value::Bool(!x.is_empty())),
Value::Array(_, vec) => Ok(Value::Bool(!vec.is_empty())),
_ => Err(Error::new(format!("no possible conversion from {} into {}", x, Type::Bool))),
},
(Op::StringCast, [x]) => Ok(Value::String(format!("{}", x))),
(Op::Print, [x]) => match x {
x => Err(Error::new(format!("no possible conversion from {} into {}", x, Type::Bool))),
}
Op::StringCast => Ok(Value::String(format!("{}", &args[0]))),
Op::Print => match &args[0] {
Value::String(s) => {
println!("{s}");
Ok(Value::Nil)
}
_ => {
x => {
println!("{x}");
Ok(Value::Nil)
}
},
_ => Err(Self::op_error(&op, &args)),
}
_ => unreachable!(),
}
}
ParseTree::Equ(ident, body, scope) => {
@@ -309,9 +319,7 @@ impl Executor {
let value = self.exec(*body)?;
let g = self.globals.clone();
let r = self.add_local_mut(
ident.clone(),
Arc::new(Mutex::new(Object::value(value, g, self.locals.to_owned()))))
let r = self.add_local_mut(ident.clone(), Arc::new(Mutex::new(Object::value(value, g, self.locals.to_owned()))))
.exec(*scope);
self.locals.remove(&ident);
@@ -410,6 +418,13 @@ impl Executor {
Ok(Value::Nil)
}
ParseTree::NonCall(name) => {
let obj = self.get_object_mut(&name)?;
let v = obj.lock().unwrap().eval()?;
Ok(v)
}
ParseTree::_Local(_idx) => todo!(),
ParseTree::GeneratedFunction(function) => Ok(Value::Function(function.globals(self.globals.clone()).locals(self.locals.clone()))),
}

View File

@@ -119,6 +119,12 @@ impl Function {
ParseTree::Value(_) => body,
ParseTree::Nop => body,
ParseTree::Export(_) => body,
ParseTree::NonCall(ref var) => if let Some(idx) =
args.into_iter().position(|r| *r == *var) {
Box::new(ParseTree::_Local(idx))
} else {
body
}
ParseTree::GeneratedFunction(_) => todo!(),
}
}

View File

@@ -4,7 +4,6 @@ use super::error::Error;
use std::collections::HashMap;
use std::iter::Peekable;
use std::cmp::Ordering;
#[derive(Clone, Debug)]
pub(crate) enum ParseTree {
@@ -29,6 +28,7 @@ pub(crate) enum ParseTree {
GeneratedFunction(Function),
Nop,
NonCall(String),
Export(Vec<String>),
}
@@ -109,7 +109,6 @@ impl Parser {
let operators: HashMap<Op, FunctionType> = HashMap::from([
(Op::Add, FunctionType(Box::new(Type::Any), vec![Type::Any, Type::Any])),
(Op::Sub, FunctionType(Box::new(Type::Any), vec![Type::Any, Type::Any])),
(Op::Neg, FunctionType(Box::new(Type::Any), vec![Type::Any])),
(Op::Mul, FunctionType(Box::new(Type::Any), vec![Type::Any, Type::Any])),
(Op::Div, FunctionType(Box::new(Type::Float), vec![Type::Any, Type::Any])),
(Op::FloorDiv, FunctionType(Box::new(Type::Int), vec![Type::Any, Type::Any])),
@@ -126,10 +125,6 @@ impl Parser {
(Op::And, FunctionType(Box::new(Type::Bool), vec![Type::Bool, Type::Bool])),
(Op::Or, FunctionType(Box::new(Type::Bool), vec![Type::Bool, Type::Bool])),
(Op::Head, FunctionType(Box::new(Type::Any), vec![Type::Array(Box::new(Type::Any))])),
(Op::Concat, FunctionType(Box::new(Type::Array(Box::new(Type::Any))), vec![Type::Array(Box::new(Type::Any)), Type::Array(Box::new(Type::Any))])),
(Op::Prepend, FunctionType(Box::new(Type::Array(Box::new(Type::Any))), vec![Type::Any, Type::Array(Box::new(Type::Any))])),
(Op::Append, FunctionType(Box::new(Type::Array(Box::new(Type::Any))), vec![Type::Array(Box::new(Type::Any)), Type::Any])),
(Op::Insert, FunctionType(Box::new(Type::Array(Box::new(Type::Any))), vec![Type::Int, Type::Any, Type::Array(Box::new(Type::Any))])),
(Op::Tail, FunctionType(Box::new(Type::Array(Box::new(Type::Any))), vec![Type::Array(Box::new(Type::Any))])),
(Op::Init, FunctionType(Box::new(Type::Array(Box::new(Type::Any))), vec![Type::Array(Box::new(Type::Any))])),
(Op::Fini, FunctionType(Box::new(Type::Any), vec![Type::Array(Box::new(Type::Any))])),
@@ -176,7 +171,40 @@ impl Parser {
match token.token() {
TokenType::Constant(c) => Ok(Some(ParseTree::Value(c))),
TokenType::Identifier(ident) => Ok(Some(ParseTree::Variable(ident))),
TokenType::Identifier(ident) => {
match self.get_object_type(&ident).ok_or(
Error::new(format!("undefined identifier {ident}"))
.location(token.line, token.location))? {
Type::Function(f) => {
let f = f.clone();
let args = self.get_args(tokens, f.1.len())?;
if args.len() < f.1.len() {
let mut counter = 0;
let func_args: Vec<Type> = f.1.iter().skip(args.len()).cloned().collect();
let (names, types): (Vec<String>, Vec<Type>) = func_args
.into_iter()
.map(|t| {
counter += 1;
(format!("{counter}"), t)
}).unzip();
let function_type = FunctionType(f.0.clone(), types);
Ok(Some(ParseTree::Value(Value::Function(Function::lambda(
function_type,
names.clone(),
Box::new(ParseTree::FunctionCall(ident,
vec![
args,
names.into_iter().map(|x| ParseTree::Variable(x)).collect()
].concat())))))))
} else {
Ok(Some(ParseTree::FunctionCall(ident, args)))
}
}
_ => Ok(Some(ParseTree::Variable(ident))),
}
},
TokenType::Operator(op) => match op {
Op::OpenArray => {
let mut depth = 1;
@@ -210,7 +238,7 @@ impl Parser {
let tree = trees.into_iter().fold(
ParseTree::Value(Value::Array(Type::Any, vec![])),
|acc, x| ParseTree::Operator(Op::Append, vec![acc, x.clone()]),
|acc, x| ParseTree::Operator(Op::Add, vec![acc, x.clone()]),
);
Ok(Some(tree))
@@ -220,7 +248,7 @@ impl Parser {
// take tokens until we reach the end of this array
// if we don't collect them here it causes rust to overflow computing the types
let tokens = tokens.by_ref().take_while(|t| match t {
let array_tokens = tokens.by_ref().take_while(|t| match t {
Ok(t) => match t.token() {
TokenType::Operator(Op::OpenStatement) => {
depth += 1;
@@ -235,51 +263,15 @@ impl Parser {
_ => true,
}).collect::<Result<Vec<_>, Error>>()?;
let mut tokens = tokens
let array_tokens = array_tokens
.into_iter()
.map(|t| Ok(t))
.collect::<Vec<Result<Token, Error>>>()
.into_iter()
.peekable();
if let Some(Ok(Some(Type::Function(f)))) = tokens.peek()
.map(|t| t.clone().and_then(|t| match t.token() {
TokenType::Identifier(ident) =>
Ok(Some(self.get_object_type(&ident).ok_or(
Error::new(format!("undefined identifier {ident}"))
.location(token.line, token.location))?)),
_ => Ok(None),
}))
{
let token = tokens.next().unwrap().unwrap();
let params: Vec<ParseTree> = self.clone().trees(tokens).collect::<Result<_, Error>>()?;
match params.len().cmp(&f.1.len()) {
Ordering::Equal => Ok(Some(ParseTree::FunctionCall(token.lexeme, params))),
Ordering::Greater => Err(Error::new(format!("too many arguments to {}", token.lexeme)).location(token.line, token.location)),
Ordering::Less => {
let mut counter = 0;
let func_args: Vec<Type> = f.1.iter().skip(params.len()).cloned().collect();
let (names, types): (Vec<String>, Vec<Type>) = func_args
.into_iter()
.map(|t| {
counter += 1;
(format!("{counter}"), t)
}).unzip();
let function_type = FunctionType(f.0.clone(), types);
Ok(Some(ParseTree::Value(Value::Function(Function::lambda(
function_type,
names.clone(),
Box::new(ParseTree::FunctionCall(token.lexeme,
vec![
params,
names.into_iter().map(|x| ParseTree::Variable(x)).collect()
].concat())))))))
}
}
} else {
let trees: Vec<ParseTree> = self.clone().trees(tokens).collect::<Result<_, Error>>()?;
let trees: Vec<ParseTree> = self.clone().trees(array_tokens)
.collect::<Result<_, Error>>()?;
let tree = trees.into_iter().fold(
ParseTree::Nop,
@@ -287,7 +279,6 @@ impl Parser {
);
Ok(Some(tree))
}
},
Op::Equ => {
let token = tokens.next()
@@ -365,6 +356,10 @@ impl Parser {
},
Op::LambdaDefine(arg_count) => Ok(Some(ParseTree::LambdaDefinition(self.parse_lambda_definition(tokens, arg_count)?))),
Op::Empty => Ok(Some(ParseTree::Value(Value::Array(Type::Any, vec![])))),
Op::NonCall => {
let name = Self::get_identifier(tokens.next())?;
Ok(Some(ParseTree::NonCall(name)))
},
Op::If => {
let cond = self.parse(tokens)?
.ok_or(Error::new("? statement requires a condition".into())
@@ -391,7 +386,7 @@ impl Parser {
},
Op::Export => {
let token = tokens.next()
.ok_or(Error::new("export expects an identifer or multiple inside of parens".into())
.ok_or(Error::new("export expects one argument of [String], but found nothing".into())
.location(token.line, token.location.clone()))??;
let names = match token.token() {

View File

@@ -7,8 +7,6 @@ use crate::error::Error;
use super::Value;
use std::io::BufRead;
use std::ops::Range;
use std::fmt;
use std::fmt::Formatter;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Op {
@@ -19,7 +17,6 @@ pub enum Op {
FloorDiv,
Exp,
Equ,
Neg,
Mod,
LazyEqu,
TypeDeclaration,
@@ -45,10 +42,6 @@ pub enum Op {
Print,
OpenArray,
CloseArray,
Concat,
Prepend,
Append,
Insert,
OpenStatement,
CloseStatement,
Empty,
@@ -59,62 +52,7 @@ pub enum Op {
Init,
Fini,
Export,
}
impl fmt::Display for Op {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let s = match self {
Op::Add => "+",
Op::Sub => "-",
Op::Mul => "*",
Op::Div => "/",
Op::FloorDiv => "//",
Op::Exp => "**",
Op::Equ => "=",
Op::Neg => "_",
Op::Mod => "%",
Op::LazyEqu => ".",
Op::TypeDeclaration => "?.",
Op::FunctionDefine(_n) => ":",
Op::FunctionDeclare(_n) => "?:",
Op::LambdaDefine(_n) => ";",
Op::Arrow => "->",
Op::Compose => "~",
Op::Id => ",",
Op::If => "?",
Op::IfElse => "??",
Op::GreaterThan => ">",
Op::LessThan => "<",
Op::EqualTo => "==",
Op::NotEqualTo => "!=",
Op::GreaterThanOrEqualTo => ">=",
Op::LessThanOrEqualTo => ">=",
Op::Not => "!",
Op::IntCast => "int",
Op::FloatCast => "float",
Op::BoolCast => "bool",
Op::StringCast => "string",
Op::Print => "print",
Op::OpenArray => "[",
Op::CloseArray => "]",
Op::Concat => "++",
Op::Prepend => "[+",
Op::Append => "+]",
Op::Insert => "[+]",
Op::OpenStatement => "(",
Op::CloseStatement => ")",
Op::Empty => "empty",
Op::And => "&&",
Op::Or => "||",
Op::Head => "head",
Op::Tail => "tail",
Op::Init => "init",
Op::Fini => "fini",
Op::Export => "export",
};
write!(f, "{s}")
}
NonCall,
}
#[derive(Debug, Clone, PartialEq)]
@@ -145,7 +83,7 @@ impl TokenType {
"fini" => TokenType::Operator(Op::Fini),
"export" => TokenType::Operator(Op::Export),
// Built-in Types
// Types
"Any" => TokenType::Type(Type::Any),
"Int" => TokenType::Type(Type::Int),
"Float" => TokenType::Type(Type::Float),
@@ -251,7 +189,6 @@ impl<R: BufRead> Tokenizer<R> {
("**", Op::Exp),
("%", Op::Mod),
("=", Op::Equ),
("_", Op::Neg),
(".", Op::LazyEqu),
("?.", Op::TypeDeclaration),
(":", Op::FunctionDefine(1)),
@@ -270,15 +207,12 @@ impl<R: BufRead> Tokenizer<R> {
("!=", Op::NotEqualTo),
("[", Op::OpenArray),
("]", Op::CloseArray),
("++", Op::Concat),
("[+", Op::Prepend),
("+]", Op::Append),
("[+]", Op::Insert),
("(", Op::OpenStatement),
(")", Op::CloseStatement),
("!", Op::Not),
("&&", Op::And),
("||", Op::Or),
("\\", Op::NonCall),
]);
let c = if let Some(c) = self.next_char() {
@@ -290,7 +224,7 @@ impl<R: BufRead> Tokenizer<R> {
if c.is_alphanumeric() {
let mut token = String::from(c);
while let Some(c) = self.next_char_if(|&c| c.is_alphanumeric() || c == '.' || c == '\'' || c == '_') {
while let Some(c) = self.next_char_if(|&c| c.is_alphanumeric() || c == '.' || c == '\'') {
token.push(c);
}
@@ -432,18 +366,3 @@ impl<R: BufRead> Iterator for Tokenizer<R> {
self.tokenize().transpose()
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Cursor;
#[test]
fn meow() {
let program = "[+] 0 1 [2 3]";
let tokens: Vec<_> = Tokenizer::new(Arc::new(Mutex::new(CodeIter::new(Cursor::new(program))))).collect();
println!("{tokens:#?}");
}
}