Compare commits
18 Commits
c29f689252
...
dev
| Author | SHA1 | Date | |
|---|---|---|---|
| 14a0a36b7f | |||
| 5dedce8f6d | |||
| b953a372c2 | |||
| eeacbc6473 | |||
| fbd95ecafb | |||
| 4c4c69d40b | |||
| 9e709c4cfc | |||
| c872cc9341 | |||
| db08c6d0b5 | |||
| b00e1fd0b4 | |||
| 48d2c009fb | |||
| 4c614c1937 | |||
| a1ebbcac03 | |||
| 5ee06de1ba | |||
| db6750c481 | |||
| 83ac160a42 | |||
|
|
c5dcf88f6b | ||
|
|
7a76943120 |
19
.github/workflows/notify-bot-updated.yml
vendored
19
.github/workflows/notify-bot-updated.yml
vendored
@@ -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
8
Cargo.lock
generated
@@ -1,6 +1,6 @@
|
|||||||
# This file is automatically @generated by Cargo.
|
# This file is automatically @generated by Cargo.
|
||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
version = 4
|
version = 3
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "aho-corasick"
|
name = "aho-corasick"
|
||||||
@@ -13,7 +13,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lamm"
|
name = "lamm"
|
||||||
version = "0.1.0"
|
version = "0.4.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"regex",
|
"regex",
|
||||||
]
|
]
|
||||||
@@ -26,9 +26,9 @@ checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "regex"
|
name = "regex"
|
||||||
version = "1.11.0"
|
version = "1.11.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "38200e5ee88914975b69f657f0801b6f6dccafd44fd9326302a4aaeecfacb1d8"
|
checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"aho-corasick",
|
"aho-corasick",
|
||||||
"memchr",
|
"memchr",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "lamm"
|
name = "lamm"
|
||||||
version = "0.3.0"
|
version = "0.4.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
description = "a simple, functional paradigm programming language which uses Polish notation"
|
description = "a simple, functional paradigm programming language which uses Polish notation"
|
||||||
|
|||||||
68
README.md
68
README.md
@@ -64,12 +64,12 @@ You can introduce a variable to global scope using the `export` builtin function
|
|||||||
```
|
```
|
||||||
# A very useful constant
|
# A very useful constant
|
||||||
= pi 3.1415926
|
= pi 3.1415926
|
||||||
export ["pi"]
|
export pi
|
||||||
|
|
||||||
# Some more useful constants
|
# Some more useful constants
|
||||||
= e 2.71828
|
= e 2.71828
|
||||||
= phi 1.6180339887
|
= phi 1.6180339887
|
||||||
export ["e" "phi"]
|
export (e phi)
|
||||||
```
|
```
|
||||||
|
|
||||||
## Functions
|
## Functions
|
||||||
@@ -77,43 +77,53 @@ 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.
|
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 x
|
||||||
inc 24 # => 25
|
+ x 1
|
||||||
|
(inc 24) # => 25
|
||||||
|
|
||||||
:. pythag a b sqrt + ** a 2.0 ** b 2.0
|
: sqrt x
|
||||||
pythag 3 4 # => 5
|
** x 0.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
|
:::::. ten'args a b c d e f g h i j
|
||||||
[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.
|
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
|
# Takes an x of `Any` type
|
||||||
: inc x + x 1
|
: inc x
|
||||||
inc 12 # => 13
|
+ x 1
|
||||||
|
(inc 12) # => 13
|
||||||
|
|
||||||
# Takes an x of `Int` and returns an `Int`
|
# Takes an x of `Int` and returns an `Int`
|
||||||
: inc ?. x Int -> Int + x 1
|
: inc ?. x Int -> Int
|
||||||
inc 9 # => 10
|
+ 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.
|
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
|
# Applies a function to any value
|
||||||
:. apply : f x f x
|
:. apply : f x
|
||||||
apply \sqrt 9 # => 3
|
(f x)
|
||||||
|
(apply sqrt 9) # => 3
|
||||||
|
|
||||||
# Applies a function f which maps an Int to an Int to x
|
# 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 ?: f Int -> Int ?. x Int -> Int
|
||||||
apply'int \sqrt 36 # => 6
|
(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`. Also, in order to pass a function to a function, you must use the `\` operator, which tells Lamm not to call the function.
|
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.
|
||||||
|
|
||||||
And off course, `:` and `?:` in function prototypes can also be extended depending on the number of arguments the function must take.
|
And of course, `:` and `?:` in function prototypes can also be extended depending on the number of arguments the function must take.
|
||||||
|
|
||||||
## Branching
|
## Branching
|
||||||
|
|
||||||
@@ -171,28 +181,28 @@ Using these, you can build a lot of fundamental functional paradigm functions.
|
|||||||
|
|
||||||
```
|
```
|
||||||
:. map : f ?. x [] -> []
|
:. map : f ?. x [] -> []
|
||||||
?? bool x
|
?? bool x
|
||||||
+ f head x map \f tail x
|
[+ (f head x) (map f tail x)
|
||||||
empty
|
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 -> []
|
:: iterate : f i count -> []
|
||||||
?? > count 0
|
?? > count 0
|
||||||
+ i iterate \f f i - count 1
|
[+ i (iterate f (f i) - count 1)
|
||||||
empty
|
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 [] -> []
|
:. take ?. n Int ?. x [] -> []
|
||||||
?? > n 0
|
?? > n 0
|
||||||
+ head x take - n 1 tail x
|
[+ head x (take - n 1 tail x)
|
||||||
empty
|
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 [] -> []
|
:. take'while ?: pred Any -> Bool ?. x [] -> []
|
||||||
?? && bool x pred head x
|
?? && bool x (pred head x)
|
||||||
+ head x take'while \pred tail x
|
[+ head x (take'while pred tail x)
|
||||||
empty
|
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
|
## Lambdas
|
||||||
|
|||||||
6
TODO.md
6
TODO.md
@@ -9,14 +9,10 @@
|
|||||||
- tuples
|
- tuples
|
||||||
- `extern "C"` functions
|
- `extern "C"` functions
|
||||||
- modules (`import` function)
|
- modules (`import` function)
|
||||||
|
- a standard library
|
||||||
- structs
|
- structs
|
||||||
- data types (need an IO object for stateful functions to return)
|
- data types (need an IO object for stateful functions to return)
|
||||||
- unpacking type parameters (`(x:xs)` in Haskell for example)
|
- unpacking type parameters (`(x:xs)` in Haskell for example)
|
||||||
- type variables in function parameters and data types
|
- type variables in function parameters and data types
|
||||||
- automatic Int to Float casting if a parameter expects a float
|
- automatic Int to Float casting if a parameter expects a float
|
||||||
- `[x..y]` array generators
|
- `[x..y]` array generators
|
||||||
- `(+)` = `;.x y + x y`
|
|
||||||
|
|
||||||
## Maybe Add
|
|
||||||
|
|
||||||
- `/` for float division and `//` for integer division
|
|
||||||
10
src/error.rs
10
src/error.rs
@@ -54,17 +54,21 @@ impl fmt::Display for Error {
|
|||||||
|
|
||||||
if let Some(code) = &self.code {
|
if let Some(code) = &self.code {
|
||||||
let mut lines = code.lines();
|
let mut lines = code.lines();
|
||||||
let linect = match lines.nth(*line) {
|
let linect = match lines.nth(*line - 1) {
|
||||||
Some(l) => l,
|
Some(l) => l,
|
||||||
None => return Ok(()), // there should probably be an error if the line number is somehow out of range
|
None => return Ok(()), // there should probably be an error if the line number is somehow out of range
|
||||||
};
|
};
|
||||||
|
|
||||||
write!(f, "\n| --> {filename}:{line}:{}\n| {linect}\n", loc.start)?;
|
let numspaces = " ".repeat((*line as f64).log10() as usize + 1);
|
||||||
|
|
||||||
|
write!(f, "\n --> {filename}:{line}:{}\n", loc.start)?;
|
||||||
|
write!(f, "{numspaces} |\n")?;
|
||||||
|
write!(f, "{line} | {linect}\n")?;
|
||||||
|
|
||||||
let spaces = " ".repeat(loc.start);
|
let spaces = " ".repeat(loc.start);
|
||||||
let pointers: String = loc.clone().map(|_| '^').collect();
|
let pointers: String = loc.clone().map(|_| '^').collect();
|
||||||
|
|
||||||
write!(f, "|{spaces}{pointers}")?;
|
write!(f, "{numspaces} |{spaces}{pointers}")?;
|
||||||
|
|
||||||
if let Some(note) = &self.note {
|
if let Some(note) = &self.note {
|
||||||
write!(f, " {note}")?;
|
write!(f, " {note}")?;
|
||||||
|
|||||||
409
src/executor.rs
409
src/executor.rs
@@ -20,7 +20,7 @@ impl Executor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn values<I>(mut self, iter: I) -> impl Iterator<Item = Result<Value, Error>>
|
pub(crate) fn _values<I>(mut self, iter: I) -> impl Iterator<Item = Result<Value, Error>>
|
||||||
where
|
where
|
||||||
I: Iterator<Item = Result<ParseTree, Error>>
|
I: Iterator<Item = Result<ParseTree, Error>>
|
||||||
{
|
{
|
||||||
@@ -46,6 +46,11 @@ impl Executor {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn add_local_mut(&mut self, k: String, v: Arc<Mutex<Object>>) -> &mut Self {
|
||||||
|
self.locals.insert(k, v);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
fn _get_object(&self, ident: &String) -> Result<&Arc<Mutex<Object>>, Error> {
|
fn _get_object(&self, ident: &String) -> Result<&Arc<Mutex<Object>>, Error> {
|
||||||
self.locals.get(ident).or(self.globals.get(ident))
|
self.locals.get(ident).or(self.globals.get(ident))
|
||||||
.ok_or(Error::new(format!("undefined identifier {}", ident.clone())))
|
.ok_or(Error::new(format!("undefined identifier {}", ident.clone())))
|
||||||
@@ -84,227 +89,217 @@ impl Executor {
|
|||||||
locals
|
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> {
|
pub(crate) fn exec(&mut self, tree: ParseTree) -> Result<Value, Error> {
|
||||||
match tree {
|
match tree {
|
||||||
ParseTree::Operator(op, args) => {
|
ParseTree::Operator(op, args) => {
|
||||||
let args: Vec<Value> = args.into_iter()
|
let args: Vec<Value> = args.into_iter()
|
||||||
.map(|x| self.exec(x)).collect::<Result<_, _>>()?;
|
.map(|x| self.exec(x)).collect::<Result<_, _>>()?;
|
||||||
|
|
||||||
match op {
|
match (&op, &args[..]) {
|
||||||
Op::Add => match &args[..] {
|
(Op::Add, [x, y]) => match (x, y) {
|
||||||
[Value::Int(x), Value::Int(y)] => Ok(Value::Int(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::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::Int(x), Value::Float(y)) => Ok(Value::Float(*x as f64 + y)),
|
||||||
[Value::Float(x), Value::Float(y)] => Ok(Value::Float(x + 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::String(x), Value::String(y)) => Ok(Value::String(format!("{x}{y}"))),
|
||||||
[Value::Array(xtype, x), Value::Array(ytype, 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)) =>
|
||||||
if xtype != ytype {
|
if xtype != ytype {
|
||||||
return Err(Error::new(format!("expected type {} but found {}", xtype, ytype)));
|
Err(Error::new(format!("expected type {} but found {}", xtype, ytype)))
|
||||||
}
|
} else {
|
||||||
|
Ok(Value::Array(xtype.clone(), [x.clone(), y.clone()].concat()))
|
||||||
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()),
|
(Op::Prepend, [x, y]) => match (x, y) {
|
||||||
[Value::Array(t, x), y] => {
|
(Value::Nil, Value::Array(xtype, x)) => Ok(Value::Array(xtype.clone(), x.clone())),
|
||||||
let ytype = y.get_type();
|
(x, Value::Array(t, y)) => {
|
||||||
|
|
||||||
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()))
|
|
||||||
},
|
|
||||||
[x, Value::Array(t, y)] => {
|
|
||||||
let xtype = x.get_type();
|
let xtype = x.get_type();
|
||||||
|
|
||||||
if *t != xtype {
|
if *t != xtype {
|
||||||
return Err(Error::new(format!("expected type {} but found {}", t, xtype)));
|
Err(Error::new(format!("expected type {} but found {}", t, xtype)))
|
||||||
|
} else {
|
||||||
|
Ok(Value::Array(xtype, [vec![x.clone()], y.clone()].concat()))
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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::Sub => match &args[..] {
|
(Op::Append, [x, y]) => match (x, y) {
|
||||||
[Value::Int(x), Value::Int(y)] => Ok(Value::Int(x - y)),
|
(Value::Array(xtype, x), Value::Nil) => Ok(Value::Array(xtype.clone(), x.clone())),
|
||||||
[Value::Int(x), Value::Float(y)] => Ok(Value::Float(*x as f64 - y)),
|
(Value::Array(t, y), x) => {
|
||||||
[Value::Float(x), Value::Int(y)] => Ok(Value::Float(x - *y as f64)),
|
let xtype = x.get_type();
|
||||||
[Value::Float(x), Value::Float(y)] => Ok(Value::Float(x - y)),
|
if *t != xtype {
|
||||||
[Value::Nil, x] => Ok(x.clone()),
|
Err(Error::new(format!("expected type {} but found {}", t, xtype)))
|
||||||
[x, Value::Nil] => Ok(x.clone()),
|
} else {
|
||||||
_ => Err(Error::new("todo: actual error output".into())),
|
Ok(Value::Array(xtype, [y.clone(), vec![x.clone()]].concat()))
|
||||||
}
|
}
|
||||||
Op::Mul => match &args[..] {
|
},
|
||||||
[Value::Int(x), Value::Int(y)] => Ok(Value::Int(x * y)),
|
_ => Err(Self::op_error(&op, &args)),
|
||||||
[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)),
|
(Op::Insert, [Value::Int(idx), x, Value::Array(t, y)]) => {
|
||||||
[Value::Float(x), Value::Float(y)] => Ok(Value::Float(x * y)),
|
let mut y = y.clone();
|
||||||
[Value::Nil, x] => Ok(x.clone()),
|
let xtype = x.get_type();
|
||||||
[x, Value::Nil] => Ok(x.clone()),
|
if *t != xtype {
|
||||||
_ => Err(Error::new("todo: actual error output".into())),
|
Err(Error::new(format!("expected type {} but found {}", t, xtype)))
|
||||||
}
|
} else if *idx as usize > y.len() {
|
||||||
Op::Div => match &args[..] {
|
Err(Error::new("attempt to insert out of array len".into()))
|
||||||
[Value::Int(x), Value::Int(y)] => Ok(Value::Float(*x as f64 / *y as f64)),
|
} else {
|
||||||
[Value::Float(x), Value::Int(y)] => Ok(Value::Float(x / *y as f64)),
|
y.insert(*idx as usize, x.clone());
|
||||||
[Value::Int(x), Value::Float(y)] => Ok(Value::Float(*x as f64 / y)),
|
Ok(Value::Array(t.clone(), y))
|
||||||
[Value::Float(x), Value::Float(y)] => Ok(Value::Float(x / y)),
|
}
|
||||||
[Value::Nil, x] => Ok(x.clone()),
|
},
|
||||||
[x, Value::Nil] => Ok(x.clone()),
|
(Op::Sub, [x, y]) => match (x, y) {
|
||||||
_ => Err(Error::new("todo: actual error output".into())),
|
(Value::Int(x), Value::Int(y)) => Ok(Value::Int(x - y)),
|
||||||
}
|
(Value::Int(x), Value::Float(y)) => Ok(Value::Float(*x as f64 - y)),
|
||||||
Op::FloorDiv => match &args[..] {
|
(Value::Float(x), Value::Int(y)) => Ok(Value::Float(x - *y as f64)),
|
||||||
[Value::Int(x), Value::Int(y)] => Ok(Value::Int(x / y)),
|
(Value::Float(x), Value::Float(y)) => Ok(Value::Float(x - y)),
|
||||||
[Value::Float(x), Value::Int(y)] => Ok(Value::Int(*x as i64 / y)),
|
_ => Err(Self::op_error(&op, &args)),
|
||||||
[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)),
|
(Op::Mul, [x, y]) => match (x, y) {
|
||||||
[Value::Nil, x] => Ok(x.clone()),
|
(Value::Int(x), Value::Int(y)) => Ok(Value::Int(x * y)),
|
||||||
[x, Value::Nil] => Ok(x.clone()),
|
(Value::Int(x), Value::Float(y)) => Ok(Value::Float(*x as f64 * y)),
|
||||||
_ => Err(Error::new("todo: actual error output".into())),
|
(Value::Float(x), Value::Int(y)) => Ok(Value::Float(x * *y as f64)),
|
||||||
}
|
(Value::Float(x), Value::Float(y)) => Ok(Value::Float(x * y)),
|
||||||
Op::Exp => match &args[..] {
|
_ => Err(Self::op_error(&op, &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))),
|
(Op::Div, [x, y]) => match (x, y) {
|
||||||
[Value::Int(x), Value::Float(y)] => Ok(Value::Float((*x as f64).powf(*y))),
|
(Value::Int(x), Value::Int(y)) => Ok(Value::Float(*x as f64 / *y as f64)),
|
||||||
[Value::Float(x), Value::Float(y)] => Ok(Value::Float(x.powf(*y))),
|
(Value::Float(x), Value::Int(y)) => Ok(Value::Float(x / *y as f64)),
|
||||||
[Value::Nil, x] => Ok(x.clone()),
|
(Value::Int(x), Value::Float(y)) => Ok(Value::Float(*x as f64 / y)),
|
||||||
[x, Value::Nil] => Ok(x.clone()),
|
(Value::Float(x), Value::Float(y)) => Ok(Value::Float(x / y)),
|
||||||
_ => Err(Error::new("todo: fsadfdsf".into())),
|
_ => Err(Self::op_error(&op, &args)),
|
||||||
}
|
},
|
||||||
Op::Mod => match &args[..] {
|
(Op::FloorDiv, [x, y]) => match (x, y) {
|
||||||
[Value::Int(x), Value::Int(y)] => Ok(Value::Int(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::Float(x), Value::Int(y)) => Ok(Value::Int(*x as i64 / y)),
|
||||||
[Value::Int(x), Value::Float(y)] => Ok(Value::Float(*x as f64 % y)),
|
(Value::Int(x), Value::Float(y)) => Ok(Value::Int(x / *y as i64)),
|
||||||
[Value::Float(x), Value::Float(y)] => Ok(Value::Float(x % y)),
|
(Value::Float(x), Value::Float(y)) => Ok(Value::Int(*x as i64 / *y as i64)),
|
||||||
[Value::Nil, x] => Ok(x.clone()),
|
_ => Err(Self::op_error(&op, &args)),
|
||||||
[x, Value::Nil] => Ok(x.clone()),
|
},
|
||||||
_ => Err(Error::new("todo: actual error output".into())),
|
(Op::Exp, [x, y]) => match (x, y) {
|
||||||
}
|
(Value::Int(x), Value::Int(y)) => Ok(Value::Float((*x as f64).powf(*y as f64))),
|
||||||
Op::GreaterThan => match &args[..] {
|
(Value::Float(x), Value::Int(y)) => Ok(Value::Float(x.powf(*y as f64))),
|
||||||
[Value::Int(x), Value::Int(y)] => Ok(Value::Bool(x > y)),
|
(Value::Int(x), Value::Float(y)) => Ok(Value::Float((*x as f64).powf(*y))),
|
||||||
[Value::Float(x), Value::Int(y)] => Ok(Value::Bool(*x > *y as f64)),
|
(Value::Float(x), Value::Float(y)) => Ok(Value::Float(x.powf(*y))),
|
||||||
[Value::Int(x), Value::Float(y)] => Ok(Value::Bool(*x as f64 > *y)),
|
_ => Err(Self::op_error(&op, &args)),
|
||||||
[Value::Float(x), Value::Float(y)] => Ok(Value::Bool(x > y)),
|
},
|
||||||
_ => Err(Error::new("todo: actual error output".into())),
|
(Op::Mod, [x, y]) => match (x, y) {
|
||||||
}
|
(Value::Int(x), Value::Int(y)) => Ok(Value::Int(x % y)),
|
||||||
Op::GreaterThanOrEqualTo => match &args[..] {
|
(Value::Float(x), Value::Int(y)) => Ok(Value::Float(x % *y as f64)),
|
||||||
[Value::Int(x), Value::Int(y)] => Ok(Value::Bool(x >= y)),
|
(Value::Int(x), Value::Float(y)) => Ok(Value::Float(*x as f64 % y)),
|
||||||
[Value::Float(x), Value::Int(y)] => Ok(Value::Bool(*x >= *y as f64)),
|
(Value::Float(x), Value::Float(y)) => Ok(Value::Float(x % y)),
|
||||||
[Value::Int(x), Value::Float(y)] => Ok(Value::Bool(*x as f64 >= *y)),
|
_ => Err(Self::op_error(&op, &args)),
|
||||||
[Value::Float(x), Value::Float(y)] => Ok(Value::Bool(x >= y)),
|
},
|
||||||
_ => Err(Error::new("todo: actual error output".into())),
|
(Op::GreaterThan, [x, y]) => match (x, y) {
|
||||||
}
|
(Value::Int(x), Value::Int(y)) => Ok(Value::Bool(x > y)),
|
||||||
Op::LessThan => match &args[..] {
|
(Value::Float(x), Value::Int(y)) => Ok(Value::Bool(*x > *y as f64)),
|
||||||
[Value::Int(x), Value::Int(y)] => Ok(Value::Bool(x < y)),
|
(Value::Int(x), Value::Float(y)) => Ok(Value::Bool(*x as f64 > *y)),
|
||||||
[Value::Float(x), Value::Int(y)] => Ok(Value::Bool(*x < *y as f64)),
|
(Value::Float(x), Value::Float(y)) => Ok(Value::Bool(x > y)),
|
||||||
[Value::Int(x), Value::Float(y)] => Ok(Value::Bool((*x as f64) < *y)),
|
_ => Err(Self::op_error(&op, &args)),
|
||||||
[Value::Float(x), Value::Float(y)] => Ok(Value::Bool(x < y)),
|
},
|
||||||
_ => Err(Error::new("todo: actual error output".into())),
|
(Op::GreaterThanOrEqualTo, [x, y]) => match (x, y) {
|
||||||
}
|
(Value::Int(x), Value::Int(y)) => Ok(Value::Bool(x >= y)),
|
||||||
Op::LessThanOrEqualTo => match &args[..] {
|
(Value::Float(x), Value::Int(y)) => Ok(Value::Bool(*x >= *y as f64)),
|
||||||
[Value::Int(x), Value::Int(y)] => Ok(Value::Bool(x <= y)),
|
(Value::Int(x), Value::Float(y)) => Ok(Value::Bool(*x as f64 >= *y)),
|
||||||
[Value::Float(x), Value::Int(y)] => Ok(Value::Bool(*x <= *y as f64)),
|
(Value::Float(x), Value::Float(y)) => Ok(Value::Bool(x >= y)),
|
||||||
[Value::Int(x), Value::Float(y)] => Ok(Value::Bool(*x as f64 <= *y)),
|
_ => Err(Self::op_error(&op, &args)),
|
||||||
[Value::Float(x), Value::Float(y)] => Ok(Value::Bool(x <= y)),
|
},
|
||||||
_ => Err(Error::new("todo: actual error output".into())),
|
(Op::LessThan, [x, y]) => match (x, y) {
|
||||||
}
|
(Value::Int(x), Value::Int(y)) => Ok(Value::Bool(x < y)),
|
||||||
Op::EqualTo => match &args[..] {
|
(Value::Float(x), Value::Int(y)) => Ok(Value::Bool(*x < *y as f64)),
|
||||||
[Value::Int(x), Value::Int(y)] => Ok(Value::Bool(x == y)),
|
(Value::Int(x), Value::Float(y)) => Ok(Value::Bool((*x as f64) < *y)),
|
||||||
[Value::Float(x), Value::Int(y)] => Ok(Value::Bool(*x == *y as f64)),
|
(Value::Float(x), Value::Float(y)) => Ok(Value::Bool(x < y)),
|
||||||
[Value::Int(x), Value::Float(y)] => Ok(Value::Bool(*x as f64 == *y)),
|
_ => Err(Self::op_error(&op, &args)),
|
||||||
[Value::Float(x), Value::Float(y)] => Ok(Value::Bool(x == y)),
|
},
|
||||||
_ => Err(Error::new("todo: actual error output".into())),
|
(Op::LessThanOrEqualTo, [x, y]) => match (x, y) {
|
||||||
}
|
(Value::Int(x), Value::Int(y)) => Ok(Value::Bool(x <= y)),
|
||||||
Op::NotEqualTo => match &args[..] {
|
(Value::Float(x), Value::Int(y)) => Ok(Value::Bool(*x <= *y as f64)),
|
||||||
[Value::Int(x), Value::Int(y)] => Ok(Value::Bool(x != y)),
|
(Value::Int(x), Value::Float(y)) => Ok(Value::Bool(*x as f64 <= *y)),
|
||||||
[Value::Float(x), Value::Int(y)] => Ok(Value::Bool(*x != *y as f64)),
|
(Value::Float(x), Value::Float(y)) => Ok(Value::Bool(x <= y)),
|
||||||
[Value::Int(x), Value::Float(y)] => Ok(Value::Bool(*x as f64 != *y)),
|
_ => Err(Self::op_error(&op, &args)),
|
||||||
[Value::Float(x), Value::Float(y)] => Ok(Value::Bool(x != y)),
|
},
|
||||||
_ => Err(Error::new("todo: actual error output".into())),
|
(Op::EqualTo, [x, y]) => match (x, y) {
|
||||||
}
|
(Value::Int(x), Value::Int(y)) => Ok(Value::Bool(x == y)),
|
||||||
Op::Not => match &args[0] {
|
(Value::Float(x), Value::Int(y)) => Ok(Value::Bool(*x == *y as f64)),
|
||||||
Value::Bool(b) => Ok(Value::Bool(!b)),
|
(Value::Int(x), Value::Float(y)) => Ok(Value::Bool(*x as f64 == *y)),
|
||||||
_ => Err(Error::new("todo: actual error output".into())),
|
(Value::Float(x), Value::Float(y)) => Ok(Value::Bool(x == y)),
|
||||||
}
|
_ => Err(Self::op_error(&op, &args)),
|
||||||
Op::Or => match &args[..] {
|
},
|
||||||
[Value::Bool(x), Value::Bool(y)] => Ok(Value::Bool(*x || *y)),
|
(Op::NotEqualTo, [x, y]) => match (x, y) {
|
||||||
[Value::Nil, x] => Ok(x.clone()),
|
(Value::Int(x), Value::Int(y)) => Ok(Value::Bool(x != y)),
|
||||||
[x, Value::Nil] => Ok(x.clone()),
|
(Value::Float(x), Value::Int(y)) => Ok(Value::Bool(*x != *y as f64)),
|
||||||
_ => Err(Error::new("todo: actual error output".into())),
|
(Value::Int(x), Value::Float(y)) => Ok(Value::Bool(*x as f64 != *y)),
|
||||||
}
|
(Value::Float(x), Value::Float(y)) => Ok(Value::Bool(x != y)),
|
||||||
Op::And => match &args[..] {
|
_ => Err(Self::op_error(&op, &args)),
|
||||||
[Value::Bool(x), Value::Bool(y)] => Ok(Value::Bool(*x && *y)),
|
},
|
||||||
[Value::Nil, x] => Ok(x.clone()),
|
(Op::Not, [Value::Bool(b)]) => Ok(Value::Bool(!b)),
|
||||||
[x, Value::Nil] => Ok(x.clone()),
|
(Op::Or, [x, y]) => match (x, y) {
|
||||||
_ => Err(Error::new("todo: actual error output".into())),
|
(Value::Bool(x), Value::Bool(y)) => Ok(Value::Bool(*x || *y)),
|
||||||
}
|
_ => Err(Self::op_error(&op, &args)),
|
||||||
Op::Compose => match &args[..] {
|
},
|
||||||
[_, v] => Ok(v.clone()),
|
(Op::And, [x, y]) => match (x, y) {
|
||||||
_ => Err(Error::new("todo: actual error output".into())),
|
(Value::Bool(x), Value::Bool(y)) => Ok(Value::Bool(*x && *y)),
|
||||||
}
|
_ => Err(Self::op_error(&op, &args)),
|
||||||
Op::Head => match &args[0] {
|
},
|
||||||
Value::Array(_, x) => Ok(x.first().ok_or(Error::new(format!("passed an empty array to head")))?.clone()),
|
(Op::Compose, [_, v]) => Ok(v.clone()),
|
||||||
_ => Err(Error::new("head".into())),
|
(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::Tail => match &args[0] {
|
(Op::Init, [Value::Array(t, x)]) => Ok(Value::Array(t.clone(), if x.len() > 0 { x[..x.len() - 1].to_vec() } else { vec![] })),
|
||||||
Value::Array(t, x) => Ok(Value::Array(t.clone(), if x.len() > 0 { x[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()),
|
||||||
_ => Err(Error::new("tail".into())),
|
(Op::Id, [x]) => Ok(x.clone()),
|
||||||
}
|
(Op::IntCast, [x]) => match x {
|
||||||
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::Int(x) => Ok(Value::Int(*x)),
|
||||||
Value::Float(x) => Ok(Value::Int(*x as i64)),
|
Value::Float(x) => Ok(Value::Int(*x as i64)),
|
||||||
Value::Bool(x) => Ok(Value::Int(if *x { 1 } else { 0 })),
|
Value::Bool(x) => Ok(Value::Int(if *x { 1 } else { 0 })),
|
||||||
Value::String(x) => {
|
Value::String(x) => {
|
||||||
let r: i64 = x.parse().map_err(|_| Error::new(format!("failed to parse {} into {}", x, Type::Int)))?;
|
let r: i64 = x.parse().map_err(|_| Error::new(format!("failed to parse {} into {}", x, Type::Int)))?;
|
||||||
Ok(Value::Int(r))
|
Ok(Value::Int(r))
|
||||||
}
|
},
|
||||||
x => Err(Error::new(format!("no possible conversion from {} into {}", x, Type::Int))),
|
_ => Err(Error::new(format!("no possible conversion from {} into {}", x, Type::Int))),
|
||||||
}
|
},
|
||||||
Op::FloatCast => match &args[0] {
|
(Op::FloatCast, [x]) => match x {
|
||||||
Value::Int(x) => Ok(Value::Float(*x as f64)),
|
Value::Int(x) => Ok(Value::Float(*x as f64)),
|
||||||
Value::Float(x) => Ok(Value::Float(*x)),
|
Value::Float(x) => Ok(Value::Float(*x)),
|
||||||
Value::Bool(x) => Ok(Value::Float(if *x { 1.0 } else { 0.0 })),
|
Value::Bool(x) => Ok(Value::Float(if *x { 1.0 } else { 0.0 })),
|
||||||
Value::String(x) => {
|
Value::String(x) => {
|
||||||
let r: f64 = x.parse().map_err(|_| Error::new(format!("failed to parse {} into {}", x, Type::Float)))?;
|
let r: f64 = x.parse().map_err(|_| Error::new(format!("failed to parse {} into {}", x, Type::Float)))?;
|
||||||
Ok(Value::Float(r))
|
Ok(Value::Float(r))
|
||||||
}
|
},
|
||||||
x => Err(Error::new(format!("no possible conversion from {} into {}", x, Type::Float))),
|
_ => Err(Error::new(format!("no possible conversion from {} into {}", x, Type::Float))),
|
||||||
}
|
},
|
||||||
Op::BoolCast => match &args[0] {
|
(Op::BoolCast, [x]) => match x {
|
||||||
Value::Int(x) => Ok(Value::Bool(*x != 0)),
|
Value::Int(x) => Ok(Value::Bool(*x != 0)),
|
||||||
Value::Float(x) => Ok(Value::Bool(*x != 0.0)),
|
Value::Float(x) => Ok(Value::Bool(*x != 0.0)),
|
||||||
Value::Bool(x) => Ok(Value::Bool(*x)),
|
Value::Bool(x) => Ok(Value::Bool(*x)),
|
||||||
Value::String(x) => Ok(Value::Bool(!x.is_empty())),
|
Value::String(x) => Ok(Value::Bool(!x.is_empty())),
|
||||||
Value::Array(_, vec) => Ok(Value::Bool(!vec.is_empty())),
|
Value::Array(_, vec) => Ok(Value::Bool(!vec.is_empty())),
|
||||||
x => Err(Error::new(format!("no possible conversion from {} into {}", x, Type::Bool))),
|
_ => Err(Error::new(format!("no possible conversion from {} into {}", x, Type::Bool))),
|
||||||
}
|
},
|
||||||
Op::StringCast => Ok(Value::String(format!("{}", &args[0]))),
|
(Op::StringCast, [x]) => Ok(Value::String(format!("{}", x))),
|
||||||
Op::Print => match &args[0] {
|
(Op::Print, [x]) => match x {
|
||||||
Value::String(s) => {
|
Value::String(s) => {
|
||||||
println!("{s}");
|
println!("{s}");
|
||||||
Ok(Value::Nil)
|
Ok(Value::Nil)
|
||||||
}
|
}
|
||||||
x => {
|
_ => {
|
||||||
println!("{x}");
|
println!("{x}");
|
||||||
Ok(Value::Nil)
|
Ok(Value::Nil)
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
_ => unreachable!(),
|
_ => Err(Self::op_error(&op, &args)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ParseTree::Equ(ident, body, scope) => {
|
ParseTree::Equ(ident, body, scope) => {
|
||||||
@@ -314,10 +309,14 @@ impl Executor {
|
|||||||
let value = self.exec(*body)?;
|
let value = self.exec(*body)?;
|
||||||
let g = self.globals.clone();
|
let g = self.globals.clone();
|
||||||
|
|
||||||
Executor::new()
|
let r = self.add_local_mut(
|
||||||
.locals(self.locals.clone())
|
ident.clone(),
|
||||||
.add_local(ident, Arc::new(Mutex::new(Object::value(value, g, self.locals.to_owned()))))
|
Arc::new(Mutex::new(Object::value(value, g, self.locals.to_owned()))))
|
||||||
.exec(*scope)
|
.exec(*scope);
|
||||||
|
|
||||||
|
self.locals.remove(&ident);
|
||||||
|
|
||||||
|
r
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
ParseTree::LazyEqu(ident, body, scope) => {
|
ParseTree::LazyEqu(ident, body, scope) => {
|
||||||
@@ -325,22 +324,27 @@ impl Executor {
|
|||||||
Err(Error::new(format!("attempt to override value of variable {ident}")))
|
Err(Error::new(format!("attempt to override value of variable {ident}")))
|
||||||
} else {
|
} else {
|
||||||
let g = self.globals.clone();
|
let g = self.globals.clone();
|
||||||
Executor::new()
|
let r = self.add_local_mut(ident.clone(), Arc::new(Mutex::new(Object::variable(*body, g, self.locals.to_owned()))))
|
||||||
.locals(self.locals.clone())
|
.exec(*scope);
|
||||||
.add_local(ident, Arc::new(Mutex::new(Object::variable(*body, g, self.locals.to_owned()))))
|
|
||||||
.exec(*scope)
|
self.locals.remove(&ident);
|
||||||
|
|
||||||
|
r
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
ParseTree::FunctionDefinition(func, scope) => {
|
ParseTree::FunctionDefinition(func, scope) => {
|
||||||
|
let name = func.name().unwrap().to_string();
|
||||||
let g = self.globals.clone();
|
let g = self.globals.clone();
|
||||||
Executor::new()
|
let r = self.add_local_mut(name.clone(),
|
||||||
.locals(self.locals.clone())
|
|
||||||
.add_local(func.name().unwrap().to_string(),
|
|
||||||
Arc::new(Mutex::new(Object::function(
|
Arc::new(Mutex::new(Object::function(
|
||||||
func
|
func
|
||||||
.globals(g)
|
.globals(g)
|
||||||
.locals(self.locals.clone()), HashMap::new(), HashMap::new()))))
|
.locals(self.locals.clone()), HashMap::new(), HashMap::new()))))
|
||||||
.exec(*scope)
|
.exec(*scope);
|
||||||
|
|
||||||
|
self.locals.remove(&name);
|
||||||
|
|
||||||
|
r
|
||||||
},
|
},
|
||||||
ParseTree::FunctionCall(ident, args) => {
|
ParseTree::FunctionCall(ident, args) => {
|
||||||
let obj = self.get_object_mut(&ident)?;
|
let obj = self.get_object_mut(&ident)?;
|
||||||
@@ -406,13 +410,6 @@ impl Executor {
|
|||||||
|
|
||||||
Ok(Value::Nil)
|
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::_Local(_idx) => todo!(),
|
||||||
ParseTree::GeneratedFunction(function) => Ok(Value::Function(function.globals(self.globals.clone()).locals(self.locals.clone()))),
|
ParseTree::GeneratedFunction(function) => Ok(Value::Function(function.globals(self.globals.clone()).locals(self.locals.clone()))),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -119,12 +119,6 @@ impl Function {
|
|||||||
ParseTree::Value(_) => body,
|
ParseTree::Value(_) => body,
|
||||||
ParseTree::Nop => body,
|
ParseTree::Nop => body,
|
||||||
ParseTree::Export(_) => 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!(),
|
ParseTree::GeneratedFunction(_) => todo!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
31
src/lib.rs
31
src/lib.rs
@@ -196,7 +196,7 @@ impl<R: BufRead> CodeIter<R> {
|
|||||||
reader,
|
reader,
|
||||||
code: String::new(),
|
code: String::new(),
|
||||||
pos: 0,
|
pos: 0,
|
||||||
line: 0,
|
line: 1,
|
||||||
column: 0,
|
column: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -246,7 +246,7 @@ impl<R: BufRead> Iterator for CodeIter<R> {
|
|||||||
self.column = 0;
|
self.column = 0;
|
||||||
self.pos += 1;
|
self.pos += 1;
|
||||||
|
|
||||||
None
|
Some('\n')
|
||||||
},
|
},
|
||||||
c => {
|
c => {
|
||||||
self.column += 1;
|
self.column += 1;
|
||||||
@@ -305,27 +305,18 @@ impl<R: BufRead> Runtime<R> {
|
|||||||
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl<R: BufRead> Iterator for Runtime<R> {
|
pub fn add_globals<Globals: IntoIterator<Item = (String, Value)>>(self, globals: Globals) -> Self {
|
||||||
type Item = Result<Value, Error>;
|
globals.into_iter().fold(self, |acc, (key, value)| acc.add_global(&key, value))
|
||||||
|
}
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
pub fn values(&self) -> impl Iterator<Item = Result<Value, Error>> + use<'_, R> {
|
||||||
let tokenizer = Tokenizer::new(self.reader.clone());
|
let tokenizer = Tokenizer::new(self.reader.clone());
|
||||||
|
let parser = Parser::new().add_globals(self.global_types.clone());
|
||||||
|
|
||||||
let tree = Parser::new()
|
Executor::new()
|
||||||
.add_globals(self.global_types.clone())
|
.add_globals(self.globals.clone())
|
||||||
.parse(&mut tokenizer.peekable());
|
._values(parser.trees(tokenizer.peekable()))
|
||||||
|
.map(|r| r.map_err(|e| e.code(self.code()).file(self.filename.clone())))
|
||||||
let tree = match tree.map_err(|e| e
|
|
||||||
.code(self.code())
|
|
||||||
.file(self.filename.clone()))
|
|
||||||
{
|
|
||||||
Ok(Some(tree)) => tree,
|
|
||||||
Ok(None) => return None,
|
|
||||||
Err(e) => return Some(Err(e))
|
|
||||||
};
|
|
||||||
|
|
||||||
Some(Executor::new().add_globals(self.globals.clone()).exec(tree))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,7 +3,7 @@ use std::io::{self, BufReader};
|
|||||||
fn main() {
|
fn main() {
|
||||||
let runtime = lamm::Runtime::new(BufReader::new(io::stdin()), "<stdin>");
|
let runtime = lamm::Runtime::new(BufReader::new(io::stdin()), "<stdin>");
|
||||||
|
|
||||||
for value in runtime {
|
for value in runtime.values() {
|
||||||
match value {
|
match value {
|
||||||
Ok(v) => println!("=> {v}"),
|
Ok(v) => println!("=> {v}"),
|
||||||
Err(e) => eprintln!("error: {e}"),
|
Err(e) => eprintln!("error: {e}"),
|
||||||
|
|||||||
214
src/parser.rs
214
src/parser.rs
@@ -1,10 +1,10 @@
|
|||||||
|
|
||||||
use super::{Value, Type, Function, FunctionType};
|
use super::{Value, Type, Function, FunctionType};
|
||||||
use super::tokenizer::{Token, TokenType, Op};
|
use super::tokenizer::{Token, TokenType, Op};
|
||||||
use super::error::Error;
|
use super::error::Error;
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::iter::Peekable;
|
use std::iter::Peekable;
|
||||||
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub(crate) enum ParseTree {
|
pub(crate) enum ParseTree {
|
||||||
@@ -29,7 +29,6 @@ pub(crate) enum ParseTree {
|
|||||||
GeneratedFunction(Function),
|
GeneratedFunction(Function),
|
||||||
|
|
||||||
Nop,
|
Nop,
|
||||||
NonCall(String),
|
|
||||||
Export(Vec<String>),
|
Export(Vec<String>),
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,20 +66,25 @@ impl Parser {
|
|||||||
items.into_iter().fold(self, |acc, (k, v)| acc.add_global(k, v))
|
items.into_iter().fold(self, |acc, (k, v)| acc.add_global(k, v))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn locals(mut self, locals: HashMap<String, Type>) -> Self {
|
pub(crate) fn _add_local(mut self, k: String, v: Type) -> Self {
|
||||||
self.locals = locals;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn add_local(mut self, k: String, v: Type) -> Self {
|
|
||||||
self.locals.insert(k, v);
|
self.locals.insert(k, v);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn _add_locals<Items: Iterator<Item = (String, Type)>>(mut self, items: Items) -> Self {
|
pub(crate) fn _add_locals<Items: Iterator<Item = (String, Type)>>(self, items: Items) -> Self {
|
||||||
items.for_each(|(name, t)| {
|
items.fold(self, |acc, (key, value)| acc._add_local(key, value))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_local_mut(&mut self, k: String, v: Type) -> &mut Self {
|
||||||
|
self.locals.insert(k, v);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_locals_mut<Items: IntoIterator<Item = (String, Type)>>(&mut self, items: Items) -> &mut Self {
|
||||||
|
for (name, t) in items {
|
||||||
self.locals.insert(name, t);
|
self.locals.insert(name, t);
|
||||||
});
|
}
|
||||||
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,6 +109,7 @@ impl Parser {
|
|||||||
let operators: HashMap<Op, FunctionType> = HashMap::from([
|
let operators: HashMap<Op, FunctionType> = HashMap::from([
|
||||||
(Op::Add, FunctionType(Box::new(Type::Any), vec![Type::Any, Type::Any])),
|
(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::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::Mul, FunctionType(Box::new(Type::Any), vec![Type::Any, Type::Any])),
|
||||||
(Op::Div, FunctionType(Box::new(Type::Float), 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])),
|
(Op::FloorDiv, FunctionType(Box::new(Type::Int), vec![Type::Any, Type::Any])),
|
||||||
@@ -121,6 +126,10 @@ impl Parser {
|
|||||||
(Op::And, FunctionType(Box::new(Type::Bool), vec![Type::Bool, Type::Bool])),
|
(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::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::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::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::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))])),
|
(Op::Fini, FunctionType(Box::new(Type::Any), vec![Type::Array(Box::new(Type::Any))])),
|
||||||
@@ -167,40 +176,7 @@ impl Parser {
|
|||||||
|
|
||||||
match token.token() {
|
match token.token() {
|
||||||
TokenType::Constant(c) => Ok(Some(ParseTree::Value(c))),
|
TokenType::Constant(c) => Ok(Some(ParseTree::Value(c))),
|
||||||
TokenType::Identifier(ident) => {
|
TokenType::Identifier(ident) => Ok(Some(ParseTree::Variable(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 {
|
TokenType::Operator(op) => match op {
|
||||||
Op::OpenArray => {
|
Op::OpenArray => {
|
||||||
let mut depth = 1;
|
let mut depth = 1;
|
||||||
@@ -229,14 +205,12 @@ impl Parser {
|
|||||||
.into_iter()
|
.into_iter()
|
||||||
.peekable();
|
.peekable();
|
||||||
|
|
||||||
let trees: Vec<ParseTree> = Parser::new()
|
let trees: Vec<ParseTree> = self.clone().trees(array_tokens)
|
||||||
.locals(self.locals.to_owned())
|
|
||||||
.trees(array_tokens)
|
|
||||||
.collect::<Result<_, Error>>()?;
|
.collect::<Result<_, Error>>()?;
|
||||||
|
|
||||||
let tree = trees.into_iter().fold(
|
let tree = trees.into_iter().fold(
|
||||||
ParseTree::Value(Value::Array(Type::Any, vec![])),
|
ParseTree::Value(Value::Array(Type::Any, vec![])),
|
||||||
|acc, x| ParseTree::Operator(Op::Add, vec![acc, x.clone()]),
|
|acc, x| ParseTree::Operator(Op::Append, vec![acc, x.clone()]),
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok(Some(tree))
|
Ok(Some(tree))
|
||||||
@@ -246,7 +220,7 @@ impl Parser {
|
|||||||
|
|
||||||
// take tokens until we reach the end of this array
|
// 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
|
// if we don't collect them here it causes rust to overflow computing the types
|
||||||
let array_tokens = tokens.by_ref().take_while(|t| match t {
|
let tokens = tokens.by_ref().take_while(|t| match t {
|
||||||
Ok(t) => match t.token() {
|
Ok(t) => match t.token() {
|
||||||
TokenType::Operator(Op::OpenStatement) => {
|
TokenType::Operator(Op::OpenStatement) => {
|
||||||
depth += 1;
|
depth += 1;
|
||||||
@@ -261,24 +235,59 @@ impl Parser {
|
|||||||
_ => true,
|
_ => true,
|
||||||
}).collect::<Result<Vec<_>, Error>>()?;
|
}).collect::<Result<Vec<_>, Error>>()?;
|
||||||
|
|
||||||
let array_tokens = array_tokens
|
let mut tokens = tokens
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|t| Ok(t))
|
.map(|t| Ok(t))
|
||||||
.collect::<Vec<Result<Token, Error>>>()
|
.collect::<Vec<Result<Token, Error>>>()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.peekable();
|
.peekable();
|
||||||
|
|
||||||
let trees: Vec<ParseTree> = Parser::new()
|
if let Some(Ok(Some(Type::Function(f)))) = tokens.peek()
|
||||||
.locals(self.locals.to_owned())
|
.map(|t| t.clone().and_then(|t| match t.token() {
|
||||||
.trees(array_tokens)
|
TokenType::Identifier(ident) =>
|
||||||
.collect::<Result<_, Error>>()?;
|
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>>()?;
|
||||||
|
|
||||||
let tree = trees.into_iter().fold(
|
match params.len().cmp(&f.1.len()) {
|
||||||
ParseTree::Nop,
|
Ordering::Equal => Ok(Some(ParseTree::FunctionCall(token.lexeme, params))),
|
||||||
|acc, x| ParseTree::Operator(Op::Compose, vec![acc, x.clone()]),
|
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(tree))
|
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 tree = trees.into_iter().fold(
|
||||||
|
ParseTree::Nop,
|
||||||
|
|acc, x| ParseTree::Operator(Op::Compose, vec![acc, x.clone()]),
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(Some(tree))
|
||||||
|
}
|
||||||
},
|
},
|
||||||
Op::Equ => {
|
Op::Equ => {
|
||||||
let token = tokens.next()
|
let token = tokens.next()
|
||||||
@@ -287,21 +296,24 @@ impl Parser {
|
|||||||
.note("expected an identifier after this token".into()))??;
|
.note("expected an identifier after this token".into()))??;
|
||||||
|
|
||||||
if let TokenType::Identifier(ident) = token.token() {
|
if let TokenType::Identifier(ident) = token.token() {
|
||||||
let body = Box::new(self.parse(tokens)?.ok_or(Error::new(format!("the variable `{ident}` has no value"))
|
let body = self.parse(tokens)?.ok_or(Error::new(format!("the variable `{ident}` has no value"))
|
||||||
.location(token.line, token.location.clone())
|
.location(token.line, token.location.clone())
|
||||||
.note("expected a value after this identifier".into()))?);
|
.note("expected a value after this identifier".into()))?;
|
||||||
|
|
||||||
let scope = Parser::new()
|
let scope = self.add_local_mut(ident.clone(), Type::Any)
|
||||||
.locals(self.locals.clone())
|
|
||||||
.add_local(ident.clone(), Type::Any)
|
|
||||||
.parse(tokens)?
|
.parse(tokens)?
|
||||||
.ok_or(Error::new("variable declaration requires a scope defined after it".into())
|
.ok_or(Error::new("variable declaration requires a scope defined after it".into())
|
||||||
.location(token.line, token.location)
|
.location(token.line, token.location)
|
||||||
.note(format!("this variable {ident} has no scope")))?;
|
.note(format!("this variable {ident} has no scope")))?;
|
||||||
|
|
||||||
|
// temporary fix: just remove the identifier
|
||||||
|
// ignore errors removing, in the case that the symbol was already exported, it won't be present in locals
|
||||||
|
// this comes down to a basic architectural error. globals need to stick to the parser while locals need to be scoped.
|
||||||
|
self.locals.remove(&ident);
|
||||||
|
|
||||||
Ok(Some(ParseTree::Equ(
|
Ok(Some(ParseTree::Equ(
|
||||||
ident.clone(),
|
ident.clone(),
|
||||||
body,
|
Box::new(body),
|
||||||
Box::new(scope))
|
Box::new(scope))
|
||||||
))
|
))
|
||||||
} else {
|
} else {
|
||||||
@@ -310,7 +322,7 @@ impl Parser {
|
|||||||
},
|
},
|
||||||
Op::LazyEqu => {
|
Op::LazyEqu => {
|
||||||
let token = tokens.next()
|
let token = tokens.next()
|
||||||
.ok_or(Error::new("no identifier given for = expression".into())
|
.ok_or(Error::new("no identifier given for . expression".into())
|
||||||
.location(token.line, token.location)
|
.location(token.line, token.location)
|
||||||
.note("expected an identifier after this token".into()))??;
|
.note("expected an identifier after this token".into()))??;
|
||||||
|
|
||||||
@@ -319,14 +331,16 @@ impl Parser {
|
|||||||
.location(token.line, token.location.clone())
|
.location(token.line, token.location.clone())
|
||||||
.note("expected a value after this identifier".into()))?);
|
.note("expected a value after this identifier".into()))?);
|
||||||
|
|
||||||
let scope = Parser::new()
|
let scope = self.add_local_mut(ident.clone(), Type::Any)
|
||||||
.locals(self.locals.clone())
|
|
||||||
.add_local(ident.clone(), Type::Any)
|
|
||||||
.parse(tokens)?
|
.parse(tokens)?
|
||||||
.ok_or(Error::new("variable declaration requires a scope defined after it".into())
|
.ok_or(Error::new("variable declaration requires a scope defined after it".into())
|
||||||
.location(token.line, token.location)
|
.location(token.line, token.location)
|
||||||
.note(format!("this variable {ident} has no scope")))?;
|
.note(format!("this variable {ident} has no scope")))?;
|
||||||
|
|
||||||
|
// temporary fix: just remove the identifier
|
||||||
|
// ignore errors removing, in the case that the symbol was already exported, it won't be present in locals
|
||||||
|
self.locals.remove(&ident);
|
||||||
|
|
||||||
Ok(Some(ParseTree::LazyEqu(
|
Ok(Some(ParseTree::LazyEqu(
|
||||||
ident.clone(),
|
ident.clone(),
|
||||||
body,
|
body,
|
||||||
@@ -339,22 +353,18 @@ impl Parser {
|
|||||||
Op::FunctionDefine(arg_count) => {
|
Op::FunctionDefine(arg_count) => {
|
||||||
let f = self.parse_function_definition(tokens, arg_count)?;
|
let f = self.parse_function_definition(tokens, arg_count)?;
|
||||||
|
|
||||||
let scope = Parser::new()
|
let scope = self.add_local_mut(f.name().unwrap().to_string(), Type::Function(f.get_type()))
|
||||||
.locals(self.locals.clone())
|
|
||||||
.add_local(f.name().unwrap().to_string(), Type::Function(f.get_type()))
|
|
||||||
.parse(tokens)?
|
.parse(tokens)?
|
||||||
.ok_or(Error::new("function declaration requires a scope defined after it".into())
|
.ok_or(Error::new("function declaration requires a scope defined after it".into())
|
||||||
.location(token.line, token.location)
|
.location(token.line, token.location)
|
||||||
.note(format!("this function {} has no scope", f.name().unwrap())))?;
|
.note(format!("this function {} has no scope", f.name().unwrap())))?;
|
||||||
|
|
||||||
|
self.locals.remove(f.name().unwrap());
|
||||||
|
|
||||||
Ok(Some(ParseTree::FunctionDefinition( f.clone(), Box::new(scope))))
|
Ok(Some(ParseTree::FunctionDefinition( f.clone(), Box::new(scope))))
|
||||||
},
|
},
|
||||||
Op::LambdaDefine(arg_count) => Ok(Some(ParseTree::LambdaDefinition(self.parse_lambda_definition(tokens, arg_count)?))),
|
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::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 => {
|
Op::If => {
|
||||||
let cond = self.parse(tokens)?
|
let cond = self.parse(tokens)?
|
||||||
.ok_or(Error::new("? statement requires a condition".into())
|
.ok_or(Error::new("? statement requires a condition".into())
|
||||||
@@ -367,19 +377,49 @@ impl Parser {
|
|||||||
},
|
},
|
||||||
Op::IfElse => {
|
Op::IfElse => {
|
||||||
let cond = self.parse(tokens)?
|
let cond = self.parse(tokens)?
|
||||||
.ok_or(Error::new("?? statement requires a condition".into())
|
.ok_or(Error::new("?? statement requires a condition".into())
|
||||||
.location(token.line, token.location.clone()))?;
|
.location(token.line, token.location.clone()))?;
|
||||||
let truebranch = self.parse(tokens)?
|
let truebranch = self.parse(tokens)?
|
||||||
.ok_or(Error::new("?? statement requires a branch".into())
|
.ok_or(Error::new("?? statement requires a branch".into())
|
||||||
.location(token.line, token.location.clone()))?;
|
.location(token.line, token.location.clone()))?;
|
||||||
let falsebranch = self.parse(tokens)?
|
let falsebranch = self.parse(tokens)?
|
||||||
.ok_or(Error::new("?? statement requires a false branch".into())
|
.ok_or(Error::new("?? statement requires a false branch".into())
|
||||||
.location(token.line, token.location))?;
|
.location(token.line, token.location))?;
|
||||||
|
|
||||||
Ok(Some(ParseTree::IfElse(
|
Ok(Some(ParseTree::IfElse(
|
||||||
Box::new(cond), Box::new(truebranch), Box::new(falsebranch))))
|
Box::new(cond), Box::new(truebranch), Box::new(falsebranch))))
|
||||||
},
|
},
|
||||||
Op::Export => todo!(),
|
Op::Export => {
|
||||||
|
let token = tokens.next()
|
||||||
|
.ok_or(Error::new("export expects an identifer or multiple inside of parens".into())
|
||||||
|
.location(token.line, token.location.clone()))??;
|
||||||
|
|
||||||
|
let names = match token.token() {
|
||||||
|
TokenType::Identifier(ident) => vec![ident],
|
||||||
|
TokenType::Operator(Op::OpenStatement) => {
|
||||||
|
tokens
|
||||||
|
.take_while(|token| !matches!(token.clone().map(|token| token.token()), Ok(TokenType::Operator(Op::CloseStatement))))
|
||||||
|
.map(|token| token.map(|token| match token.token() {
|
||||||
|
TokenType::Identifier(ident) => Ok(ident),
|
||||||
|
_ => Err(Error::new(format!("expected an identifier")).location(token.line, token.location))
|
||||||
|
})?)
|
||||||
|
.collect::<Result<_, Error>>()?
|
||||||
|
}
|
||||||
|
_ => return Err(Error::new("export expects one or more identifiers".into()).location(token.line, token.location)),
|
||||||
|
};
|
||||||
|
|
||||||
|
for name in &names {
|
||||||
|
let (name, t) = self.locals.remove_entry(name)
|
||||||
|
.ok_or(
|
||||||
|
Error::new(format!("attempt to export {name}, which is not in local scope"))
|
||||||
|
.location(token.line, token.location.clone())
|
||||||
|
)?;
|
||||||
|
|
||||||
|
self.globals.insert(name, t);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Some(ParseTree::Export(names)))
|
||||||
|
},
|
||||||
op => self.parse_operator(tokens, op).map(|x| Some(x)),
|
op => self.parse_operator(tokens, op).map(|x| Some(x)),
|
||||||
},
|
},
|
||||||
_ => Err(Error::new(format!("the token {} was unexpected", token.lexeme)).location(token.line, token.location)),
|
_ => Err(Error::new(format!("the token {} was unexpected", token.lexeme)).location(token.line, token.location)),
|
||||||
@@ -396,8 +436,7 @@ impl Parser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Ok(Function::lambda(t, args, Box::new(
|
Ok(Function::lambda(t, args, Box::new(
|
||||||
Parser::new()
|
self.clone().add_locals_mut(locals).parse(tokens)?.ok_or(Error::new("lambda requires a body".into()))?)))
|
||||||
.locals(locals).parse(tokens)?.ok_or(Error::new("lambda requires a body".into()))?)))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_function_definition<I: Iterator<Item = Result<Token, Error>>>(&mut self, tokens: &mut Peekable<I>, arg_count: usize) -> Result<Function, Error> {
|
fn parse_function_definition<I: Iterator<Item = Result<Token, Error>>>(&mut self, tokens: &mut Peekable<I>, arg_count: usize) -> Result<Function, Error> {
|
||||||
@@ -413,8 +452,7 @@ impl Parser {
|
|||||||
locals.insert(name.clone(), Type::Function(t.clone()));
|
locals.insert(name.clone(), Type::Function(t.clone()));
|
||||||
|
|
||||||
Ok(Function::named(&name, t, args, Box::new(
|
Ok(Function::named(&name, t, args, Box::new(
|
||||||
Parser::new()
|
self.clone().add_locals_mut(locals).parse(tokens)?.ok_or(Error::new("function requires a body".into()))?)))
|
||||||
.locals(locals).parse(tokens)?.ok_or(Error::new("function requires a body".into()))?)))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_function_declaration<I: Iterator<Item = Result<Token, Error>>>(
|
fn parse_function_declaration<I: Iterator<Item = Result<Token, Error>>>(
|
||||||
|
|||||||
150
src/tokenizer.rs
150
src/tokenizer.rs
@@ -1,4 +1,4 @@
|
|||||||
use std::collections::{VecDeque, HashMap};
|
use std::collections::HashMap;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
use crate::{CodeIter, Type};
|
use crate::{CodeIter, Type};
|
||||||
@@ -7,6 +7,8 @@ use crate::error::Error;
|
|||||||
use super::Value;
|
use super::Value;
|
||||||
use std::io::BufRead;
|
use std::io::BufRead;
|
||||||
use std::ops::Range;
|
use std::ops::Range;
|
||||||
|
use std::fmt;
|
||||||
|
use std::fmt::Formatter;
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
pub enum Op {
|
pub enum Op {
|
||||||
@@ -17,6 +19,7 @@ pub enum Op {
|
|||||||
FloorDiv,
|
FloorDiv,
|
||||||
Exp,
|
Exp,
|
||||||
Equ,
|
Equ,
|
||||||
|
Neg,
|
||||||
Mod,
|
Mod,
|
||||||
LazyEqu,
|
LazyEqu,
|
||||||
TypeDeclaration,
|
TypeDeclaration,
|
||||||
@@ -42,6 +45,10 @@ pub enum Op {
|
|||||||
Print,
|
Print,
|
||||||
OpenArray,
|
OpenArray,
|
||||||
CloseArray,
|
CloseArray,
|
||||||
|
Concat,
|
||||||
|
Prepend,
|
||||||
|
Append,
|
||||||
|
Insert,
|
||||||
OpenStatement,
|
OpenStatement,
|
||||||
CloseStatement,
|
CloseStatement,
|
||||||
Empty,
|
Empty,
|
||||||
@@ -52,7 +59,62 @@ pub enum Op {
|
|||||||
Init,
|
Init,
|
||||||
Fini,
|
Fini,
|
||||||
Export,
|
Export,
|
||||||
NonCall,
|
}
|
||||||
|
|
||||||
|
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}")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
@@ -71,9 +133,9 @@ impl TokenType {
|
|||||||
"true" => TokenType::Constant(Value::Bool(true)),
|
"true" => TokenType::Constant(Value::Bool(true)),
|
||||||
"false" => TokenType::Constant(Value::Bool(false)),
|
"false" => TokenType::Constant(Value::Bool(false)),
|
||||||
"nil" => TokenType::Constant(Value::Nil),
|
"nil" => TokenType::Constant(Value::Nil),
|
||||||
"int" => TokenType::Operator(Op::IntCast),
|
"int" => TokenType::Operator(Op::IntCast),
|
||||||
"float" => TokenType::Operator(Op::FloatCast),
|
"float" => TokenType::Operator(Op::FloatCast),
|
||||||
"bool" => TokenType::Operator(Op::BoolCast),
|
"bool" => TokenType::Operator(Op::BoolCast),
|
||||||
"string" => TokenType::Operator(Op::StringCast),
|
"string" => TokenType::Operator(Op::StringCast),
|
||||||
"print" => TokenType::Operator(Op::Print),
|
"print" => TokenType::Operator(Op::Print),
|
||||||
"empty" => TokenType::Operator(Op::Empty),
|
"empty" => TokenType::Operator(Op::Empty),
|
||||||
@@ -83,7 +145,7 @@ impl TokenType {
|
|||||||
"fini" => TokenType::Operator(Op::Fini),
|
"fini" => TokenType::Operator(Op::Fini),
|
||||||
"export" => TokenType::Operator(Op::Export),
|
"export" => TokenType::Operator(Op::Export),
|
||||||
|
|
||||||
// Types
|
// Built-in Types
|
||||||
"Any" => TokenType::Type(Type::Any),
|
"Any" => TokenType::Type(Type::Any),
|
||||||
"Int" => TokenType::Type(Type::Int),
|
"Int" => TokenType::Type(Type::Int),
|
||||||
"Float" => TokenType::Type(Type::Float),
|
"Float" => TokenType::Type(Type::Float),
|
||||||
@@ -137,14 +199,12 @@ impl Token {
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub(crate) struct Tokenizer<R: BufRead> {
|
pub(crate) struct Tokenizer<R: BufRead> {
|
||||||
reader: Arc<Mutex<CodeIter<R>>>,
|
reader: Arc<Mutex<CodeIter<R>>>,
|
||||||
tokens: VecDeque<Token>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<R: BufRead> Tokenizer<R> {
|
impl<R: BufRead> Tokenizer<R> {
|
||||||
pub fn new(reader: Arc<Mutex<CodeIter<R>>>) -> Self {
|
pub fn new(reader: Arc<Mutex<CodeIter<R>>>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
reader,
|
reader,
|
||||||
tokens: VecDeque::new(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -181,7 +241,7 @@ impl<R: BufRead> Tokenizer<R> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Tokenizes more input and adds them to the internal queue
|
/// Tokenizes more input and adds them to the internal queue
|
||||||
fn tokenize(&mut self) -> Result<(), Error> {
|
fn tokenize(&mut self) -> Result<Option<Token>, Error> {
|
||||||
let operators: HashMap<&'static str, Op> = HashMap::from([
|
let operators: HashMap<&'static str, Op> = HashMap::from([
|
||||||
("+", Op::Add),
|
("+", Op::Add),
|
||||||
("-", Op::Sub),
|
("-", Op::Sub),
|
||||||
@@ -191,6 +251,7 @@ impl<R: BufRead> Tokenizer<R> {
|
|||||||
("**", Op::Exp),
|
("**", Op::Exp),
|
||||||
("%", Op::Mod),
|
("%", Op::Mod),
|
||||||
("=", Op::Equ),
|
("=", Op::Equ),
|
||||||
|
("_", Op::Neg),
|
||||||
(".", Op::LazyEqu),
|
(".", Op::LazyEqu),
|
||||||
("?.", Op::TypeDeclaration),
|
("?.", Op::TypeDeclaration),
|
||||||
(":", Op::FunctionDefine(1)),
|
(":", Op::FunctionDefine(1)),
|
||||||
@@ -209,32 +270,33 @@ impl<R: BufRead> Tokenizer<R> {
|
|||||||
("!=", Op::NotEqualTo),
|
("!=", Op::NotEqualTo),
|
||||||
("[", Op::OpenArray),
|
("[", Op::OpenArray),
|
||||||
("]", Op::CloseArray),
|
("]", Op::CloseArray),
|
||||||
|
("++", Op::Concat),
|
||||||
|
("[+", Op::Prepend),
|
||||||
|
("+]", Op::Append),
|
||||||
|
("[+]", Op::Insert),
|
||||||
("(", Op::OpenStatement),
|
("(", Op::OpenStatement),
|
||||||
(")", Op::CloseStatement),
|
(")", Op::CloseStatement),
|
||||||
("!", Op::Not),
|
("!", Op::Not),
|
||||||
("&&", Op::And),
|
("&&", Op::And),
|
||||||
("||", Op::Or),
|
("||", Op::Or),
|
||||||
("\\", Op::NonCall),
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
let c = if let Some(c) = self.next_char() {
|
let c = if let Some(c) = self.next_char() {
|
||||||
c
|
c
|
||||||
} else {
|
} else {
|
||||||
return Ok(());
|
return Ok(None);
|
||||||
};
|
};
|
||||||
|
|
||||||
if c.is_alphanumeric() {
|
if c.is_alphanumeric() {
|
||||||
let mut token = String::from(c);
|
let mut token = String::from(c);
|
||||||
|
|
||||||
while let Some(c) = self.next_char_if(|&c| c.is_alphanumeric() || c == '.' || c == '\'') {
|
while let Some(c) = self.next_char_if(|&c| c.is_alphanumeric() || c == '.' || c == '\'' || c == '_') {
|
||||||
token.push(c);
|
token.push(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
let (line, column) = self.getpos();
|
let (line, column) = self.getpos();
|
||||||
|
|
||||||
self.tokens.push_back(Token::new(TokenType::parse(&token)
|
Ok(Some(Token::new(TokenType::parse(&token).map_err(|e| e.location(line, column - token.len() + 1..column + 1))?, token.clone(), line, column - token.len() + 1)))
|
||||||
.map_err(|e| e.location(line, column - token.len() + 1..column + 1))?, token.clone(), line, column - token.len() + 1));
|
|
||||||
self.tokenize()
|
|
||||||
} else if c == '#' {
|
} else if c == '#' {
|
||||||
while self.next_char_if(|&c| c != '\n').is_some() {}
|
while self.next_char_if(|&c| c != '\n').is_some() {}
|
||||||
self.tokenize()
|
self.tokenize()
|
||||||
@@ -244,10 +306,15 @@ impl<R: BufRead> Tokenizer<R> {
|
|||||||
|
|
||||||
while let Some(c) = self.next_char() {
|
while let Some(c) = self.next_char() {
|
||||||
match c {
|
match c {
|
||||||
'"' => break,
|
'"' => {
|
||||||
|
let (line, col) = self.getpos();
|
||||||
|
|
||||||
|
return Ok(Some(Token::new(TokenType::Constant(
|
||||||
|
Value::String(token.clone())), token, line, col)));
|
||||||
|
}
|
||||||
'\n' => return Err(
|
'\n' => return Err(
|
||||||
Error::new("Unclosed string literal".into())
|
Error::new("Unclosed string literal".into())
|
||||||
.location(line, col..self.getpos().1)
|
.location(line, col..col+token.len()+1)
|
||||||
.note("newlines are not allowed in string literals (try \\n)".into())),
|
.note("newlines are not allowed in string literals (try \\n)".into())),
|
||||||
'\\' => match self.next_char() {
|
'\\' => match self.next_char() {
|
||||||
Some('\\') => token.push('\\'),
|
Some('\\') => token.push('\\'),
|
||||||
@@ -258,20 +325,16 @@ impl<R: BufRead> Tokenizer<R> {
|
|||||||
Some(c) => token.push(c),
|
Some(c) => token.push(c),
|
||||||
None => return Err(
|
None => return Err(
|
||||||
Error::new("Unclosed string literal".into())
|
Error::new("Unclosed string literal".into())
|
||||||
.location(line, col..self.getpos().1)
|
.location(line, col..token.len()+1)
|
||||||
.note("end of file found before \"".into())),
|
.note("end of file found before \"".into())),
|
||||||
}
|
}
|
||||||
_ => token.push(c),
|
_ => token.push(c),
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
let (line, col) = self.getpos();
|
Err(Error::new("Unclosed string literal".into())
|
||||||
|
.location(line, col..self.getpos().1+1)
|
||||||
self.tokens.push_back(
|
.note("end of file found before \"".into()))
|
||||||
Token::new(TokenType::Constant(
|
|
||||||
Value::String(token.clone())), token, line, col));
|
|
||||||
|
|
||||||
self.tokenize()
|
|
||||||
} else if operators.keys().any(|x| x.starts_with(c)) {
|
} else if operators.keys().any(|x| x.starts_with(c)) {
|
||||||
let mut token = String::from(c);
|
let mut token = String::from(c);
|
||||||
|
|
||||||
@@ -309,9 +372,7 @@ impl<R: BufRead> Tokenizer<R> {
|
|||||||
|
|
||||||
let token = Token::new(t, token, line, col);
|
let token = Token::new(t, token, line, col);
|
||||||
|
|
||||||
self.tokens.push_back(token);
|
return Ok(Some(token));
|
||||||
|
|
||||||
break;
|
|
||||||
} else {
|
} else {
|
||||||
let next = match self.next_char_if(is_expected) {
|
let next = match self.next_char_if(is_expected) {
|
||||||
Some(c) => c,
|
Some(c) => c,
|
||||||
@@ -345,8 +406,7 @@ impl<R: BufRead> Tokenizer<R> {
|
|||||||
|
|
||||||
let token = Token::new(t, token, line, col);
|
let token = Token::new(t, token, line, col);
|
||||||
|
|
||||||
self.tokens.push_back(token);
|
return Ok(Some(token))
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -354,16 +414,13 @@ impl<R: BufRead> Tokenizer<R> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.tokenize()
|
|
||||||
} else if c.is_whitespace() {
|
} else if c.is_whitespace() {
|
||||||
self.tokenize()
|
self.tokenize()
|
||||||
} else {
|
} else {
|
||||||
let (line, col) = self.getpos();
|
let (line, col) = self.getpos();
|
||||||
|
|
||||||
return Err(
|
Err(Error::new(format!("an unidentified character {c} was found"))
|
||||||
Error::new(format!("an unidentified character {c} was found"))
|
.location(line, col..col+1))
|
||||||
.location(line, col - 1..col));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -372,16 +429,7 @@ impl<R: BufRead> Iterator for Tokenizer<R> {
|
|||||||
type Item = Result<Token, Error>;
|
type Item = Result<Token, Error>;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
if let Some(token) = self.tokens.pop_front() {
|
self.tokenize().transpose()
|
||||||
return Some(Ok(token));
|
|
||||||
} else {
|
|
||||||
match self.tokenize() {
|
|
||||||
Ok(_) => (),
|
|
||||||
Err(e) => return Some(Err(e)),
|
|
||||||
};
|
|
||||||
|
|
||||||
self.next()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -391,13 +439,11 @@ mod tests {
|
|||||||
use std::io::Cursor;
|
use std::io::Cursor;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn a() {
|
fn meow() {
|
||||||
let program = ": f a * 12 a f 12";
|
let program = "[+] 0 1 [2 3]";
|
||||||
|
|
||||||
let tokenizer = Tokenizer::new(Arc::new(Mutex::new(CodeIter::new(Cursor::new(program)))));
|
let tokens: Vec<_> = Tokenizer::new(Arc::new(Mutex::new(CodeIter::new(Cursor::new(program))))).collect();
|
||||||
|
|
||||||
let t: Vec<_> = tokenizer.collect();
|
println!("{tokens:#?}");
|
||||||
|
|
||||||
println!("{t:#?}");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user