Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
4dee8bc
Error::CannotDo2 uses BinOperator instead of str
mpolosak Dec 5, 2024
607a2b2
Merge and::create_op and or::create_op
mpolosak Dec 5, 2024
70457ff
Add math::create_num_op
mpolosak Dec 5, 2024
4447f53
Simplify bin_op
mpolosak Dec 6, 2024
5b3c33f
Fix operators << >> add tests for them
mpolosak Dec 6, 2024
93ccde2
Fix break continue with nested loops
mpolosak Dec 6, 2024
faf139e
Fix interpreter.get_variable
mpolosak Dec 7, 2024
671784e
Add \ operator
mpolosak Dec 7, 2024
f528178
Rewrite quick_sort example
mpolosak Dec 7, 2024
c616fb6
Add filter example, it shows how ? operator should work with iterators
mpolosak Dec 10, 2024
efce1aa
example_scripts/filter add example how to pack array into iterator
mpolosak Dec 10, 2024
b9fb890
Add collect function to filter example showing how collect operator s…
mpolosak Dec 10, 2024
5239882
Add $] operator (collect)
mpolosak Dec 10, 2024
80a6bd6
Make UnaryOperator public better errors
mpolosak Dec 11, 2024
0dd3a40
Simplify UnaryOperation
mpolosak Dec 11, 2024
b345c26
$&& and $|| now work on iterators
mpolosak Dec 11, 2024
0959973
$& $| work on iterators
mpolosak Dec 11, 2024
3b40d13
$* on iterators
mpolosak Dec 11, 2024
a97e6f3
Fix prefix_op::tests
mpolosak Dec 11, 2024
585e5bb
Fix ! operator
mpolosak Dec 11, 2024
7a81661
$+ operator run on iterator
mpolosak Dec 11, 2024
2da9407
Add Type::iter_element
mpolosak Dec 11, 2024
f93614e
$ operator work on iterators
mpolosak Dec 11, 2024
d69f32b
? \ operators work on iterators
mpolosak Dec 11, 2024
567c4c0
@ operator run on iterator
mpolosak Dec 11, 2024
8644a9f
New Filter::tests && fix Type::iter_element
mpolosak Dec 12, 2024
c134bf3
Rewrite map::tests
mpolosak Dec 12, 2024
1e5bca4
Rewrite fizzbuzz
mpolosak Dec 12, 2024
8cfa250
Add ~ operator
mpolosak Dec 12, 2024
52f4dc5
Fix \ operator return type
mpolosak Dec 12, 2024
a729c5a
Fix quick_sort
mpolosak Dec 12, 2024
b93cedd
Remove replace example
mpolosak Dec 12, 2024
ce04a2a
? type operator work on iterators
mpolosak Dec 22, 2024
f902b22
Add Type::is_iterator
mpolosak Dec 22, 2024
85496cd
Update operators.md
mpolosak Dec 23, 2024
d1c812d
Add some test for reduce
mpolosak Dec 23, 2024
b3ddb0d
Fix $+ operator
mpolosak Dec 23, 2024
a184e77
$+ is defined as 3 lang functions
mpolosak Dec 23, 2024
f3d0c55
$* as 2 lang functions
mpolosak Dec 23, 2024
6b0f262
$& $| defined internaly as lang functions
mpolosak Dec 23, 2024
9813b7b
Define $&& and $|| as lang functions
mpolosak Dec 23, 2024
22ea764
$&& $|| internally replace with function call
mpolosak Dec 23, 2024
29fb0c5
Make $&& and $|| operators work again
mpolosak Dec 23, 2024
9c2d5ab
$& $| Internally as function call
mpolosak Dec 23, 2024
2cc7673
$* as function call
mpolosak Dec 23, 2024
7d507bd
$+ get replaced with function call
mpolosak Dec 23, 2024
d4abada
Remove for with index
mpolosak Dec 23, 2024
5bd714d
for loop now work on iterators
mpolosak Dec 23, 2024
29900bd
Revert "$+ get replaced with function call"
mpolosak Dec 25, 2024
972f0d3
Revert "$* as function call"
mpolosak Dec 25, 2024
5c51438
Export sum of reduce operators as functions
mpolosak Dec 25, 2024
91f2d73
Add test for $+ operator
mpolosak Dec 26, 2024
6c55e02
Allow postfix operator chaining
mpolosak Dec 26, 2024
7431cbc
Fix $+ tests
mpolosak Dec 26, 2024
10fb40d
Add test for $* operator
mpolosak Dec 27, 2024
feed93f
Add tests for $& and $| operators
mpolosak Dec 27, 2024
9ffd0f3
Add test for $] operator
mpolosak Dec 28, 2024
ac0e57a
Add tests for $&& and $|| operators
mpolosak Dec 28, 2024
18cb3f3
Add tests for ~ operator
mpolosak Jan 5, 2025
8c840d2
Add iterators.md
mpolosak Jan 8, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
200 changes: 115 additions & 85 deletions Cargo.lock

Large diffs are not rendered by default.

21 changes: 11 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[workspace]
[workspace.dependencies]
pest = "2.0"
lazy_static = "1.4.0"
simplesl_parser = { path="parser", version="0.1.0" }
pest = "2.7.14"
lazy_static = "1.5.0"
simplesl_parser = { path = "parser", version = "0.1.0" }
unescaper = "0.1.5"

[package]
Expand All @@ -18,18 +18,19 @@ description = "Simple Scripting Language"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rustyline = "14.0.0"
typle = "0.10.2"
simplesl_macros = { path="macros", version="0.1.0" }
rustyline = "15.0.0"
typle = "0.10.6"
simplesl_macros = { path = "macros", version = "0.1.0" }
match_any = "1.0.1"
duplicate = "1"
enum-as-inner = "0.6.0"
duplicate = "2.0.0"
enum-as-inner = "0.6.1"
derive_more = { version = "1.0.0", features = ["display"] }
pest.workspace = true
lazy_static.workspace = true
simplesl_parser.workspace = true
unescaper.workspace = true

[dev-dependencies]
itertools="0.13.0"
itertools = "0.13.0"
markdown = "0.3"
proptest = "1.0.0"
proptest = "1.0.0"
27 changes: 27 additions & 0 deletions docs/iterators.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Iterators
An iterator is a function taking no parameters and returning a tuple of two values, first being of type bool i.e. matching type () -> (bool, any). The iterator returns (true, value) when sequence has not ended yet and (false, value) when all values where already consumed.
## How to create an iterator?
An iterator can be written by user or created from an array using ~ operator.
```
iota := (start: int, end: int) -> () -> (bool, int) {
i := mut start;
return () -> (bool, int) {
val := *i;
if(val<end){
i+=1;
return (true, val);
}
return (false, val);
}
} //function creating iterator returning values from start to end
[1, 2.5, "3"]~ //creates operator from array
```
## Usage of iterator
You can call an iterator as any other function. You can use built in operators to create a new iterator from it (@, ?, ? type), reduce it (\$, \$*, \$&, \$|, \$&&, \$||), collect into array (\$]), partion (\\). You can iterate over it using for loop.
```
is_even = (a: int) {return a%2==0};
x := [23, 2, 12, 45, 0, 65, -2]~?is_even; //creates iterator returning only values of an array that are even
for e in x {
print(e);
} //prints elements of x iterator
```
134 changes: 73 additions & 61 deletions docs/operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@
| 2 | ! | NOT | Right-to-left |
| | - | Unary minus | |
| | * | Indirection | |
| 3 | @ | Array map | Left-to-right |
| 3 | @ | Map | Left-to-right |
| | ? | Array filtering | |
| | \\ | Array partition | |
| | $ expression | Array reducing | |
| | $+ | Array sum | |
| | $* | Array product | |
| | $&& | Logical and reduce(all) | |
| | $\|\| | Logical or reduce (any) | |
| | $& | Bitwise and reduce | |
| | $\| | Bitwise or reduce | |
| | ~ | Iterate | |
| 4 | ** | Exponentiation | |
| 5 | * | Multiplication | |
| | / | Division | |
Expand Down Expand Up @@ -56,11 +58,11 @@ array/string [index]
Index must be of type int. Indexing is 0-based. Indexing with values less than zero or greater than the length of the array/string results in an error.
Indexing of a string returns a string containing an UTF-8 character on the given position.

## ? type - Array filtering by type
## ? type - Filter by type
```
array ? type
iterator ? type
```
Filters array leaving only elements matching given type
Creates iterator returning only elements matching given type

## () - Function call
```
Expand All @@ -86,103 +88,113 @@ Calls the function with given arguments.
| ------- | ------ | ------------|
| mut T | T | Returns value contained in mut

## @ - Array maping operator
| lhs | rhs | result |
| ----------- | ---------------------------------------------- | ------ |
| [T] | (value: T) -> S | [S] |
| [T] | (index: int, value: T) -> S | [S] |
| (T, S, ...) | (value_t: T, value_s: S, ...) -> U | [U] |
| (T, S, ...) | (index: int, value_t: T, value_s: S, ...) -> U | [U] |
## @ - Map
| lhs | rhs | result |
| --------------- | ----------------| --------------- |
| () -> (bool, T) | (value: T) -> S | () -> (bool, S) |

## ? - Array filtering operator
| lhs | rhs | result |
| --- | ------------------------------ | -------|
| [T] | (value: T) -> bool | [T] |
| [T] | (index: int, value: T) -> bool | [T] |
## ? - Filter
| lhs | rhs | result |
| --------------- | ------------------------------ | --------------- |
| () -> (bool, T) | (value: T) -> bool | () -> (bool, T) |

Filters array using given function. Leaving only elements for which the function returned true.
Creates iterator returning only this elements of iterator on left for which function returned true

## $ - Array reducing operator
## \ - Partition
| lhs | rhs | result |
| --------------- | ------------------------------ | ---------- |
| () -> (bool, T) | (value: T) -> bool | ([T], [T]) |
| () -> (bool, T) | (index: int, value: T) -> bool | ([T], [T]) |

Returns tuple containing two arrays. First array contains all elements for which function returned
true, second array contains all other elements.

## $ - Reduce
```
array $ initial_value function
iterator $ initial_value function
```

| lhs | initial value | rhs | result |
| --- | --------------| ------------------------------ | ------- |
| [T] | S | (acc: T \| U, current: S) -> U | T \| U |
| lhs | initial value | rhs | result |
| --------------- | --------------| ------------------------------ | ------- |
| () -> (bool, T) | S | (acc: T \| U, current: S) -> U | T \| U |

## $+ - Array sum
## $+ - Sum
```
array $+
iterator $+
```
| lhs | result |
| ------- | ------- |
| [int] | int |
| [float] | float |
| [string]| string |
| lhs | result |
| -------------------- | ------- |
| () -> (bool, int) | int |
| () -> (bool, float) | float |
| () -> (bool, string) | string |

Calculates sum of all elements of array.
Calculates sum of all elements of iterator.

## $* - Array product
## $* - Product
```
array $*
iterator $*
```
| lhs | result |
| ------- | ------- |
| [int] | int |
| [float] | float |
| lhs | result |
| ------------------- | ------- |
| () -> (bool, int) | int |
| () -> (bool, float) | float |

Calculates product of all elements of array.
Calculates product of all elements of iterator.

## $&& - Logical and reduce (all)
```
array $&&
iterator $&&
```
| lhs | result |
| ------ | ------ |
| [bool] | bool |
| lhs | result |
| ------------------ | ------ |
| () -> (bool, bool) | bool |

Returns true value if all elements of array are true, false otherwise
Test if every element of iterator function is true

## $|| - Logical and reduce (any)
```
array $||
iterator $||
```
| lhs | result |
| ------ | ------ |
| [bool] | bool |
| lhs | result |
| ------------------ | ------ |
| () -> (bool, bool) | bool |

Returns true value if any of elements of array is true, false otherwise
Test if any element of iterator function is true

## $& - Bitwise and reduce
```
array $&
iterator $&
```
| lhs | result |
| ------- | ------- |
| [int] | int |
| [bool] | bool |
| lhs | result |
| ----------------- | ------- |
| () -> (bool, int) | int |

Equivalent of:
```
array $!0 (acc: int, curr: int) -> int {return acc & curr}
array $true (acc: bool, curr: bool) -> bool {return acc & curr}
iterator $!0 (acc: int, curr: int) -> int {return acc & curr}
```

## $| - Bitwise or reduce
```
array $|
iterator $|
```
| lhs | result |
| ------- | ------- |
| [int] | int |
| [bool] | bool |
| lhs | result |
| ----------------- | ------- |
| () -> (bool, int) | int |

Equivalent of:
```
array $0 (acc: int, curr: int) -> int {return acc | curr}
array $false (acc: bool, curr: bool) -> bool {return acc | curr}
iterator $0 (acc: int, curr: int) -> int {return acc | curr}
```

## ~ - Iterate
```
iterator $|
```
| lhs | result |
| --- | --------------- |
| [T] | () -> (bool, T) |
Create iterator over elements of array

## ** - Exponentiation
| lhs | rhs | result | description |
Expand Down
3 changes: 1 addition & 2 deletions docs/statements.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ while ident:type = expression statment
## for
```
for element_ident in expression statment
for index_ident, element_ident in expression statment
```
The expression must be an array
The expression must be an iterator

## break
```
Expand Down
32 changes: 31 additions & 1 deletion docs/stdlib.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,34 @@ Returns the lowercase equivalent of `string`.
Returns the uppercase equivalent of `string`.

## len(variable: [any]|string) -> int
Returns length of `variable`.
Returns length of `variable`.

# Operators
This part of the standard library contains some functions doing the same as built-in operators

## bitand_reduce(iter: () -> (bool, int)) -> int
The same as `iter$&`

## bitor_reduce(iter: () -> (bool, int)) -> int
The same as `iter$|`

## all(iter: () -> (bool, bool)) -> bool
The same as `iter$&&`

## any(iter: () -> (bool, bool)) -> bool
The same as `iter$||`

## int_product(iter: () -> (bool, int)) -> int
The same as `iter$*`

## float_product(iter: () -> (bool, float)) -> float
The same as `iter$*`

## int_sum(iter: () -> (bool, int)) -> int
The same as `iter$+`

## float_sum(iter: () -> (bool, float)) -> float
The same as `iter$+`

## string_sum(iter: () -> (bool, string)) -> string
The same as `iter$+`
42 changes: 42 additions & 0 deletions example_scripts/filter
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
i := mut 0;
x := () -> (bool, int) {
i += 1;
return (*i <= 5, *i);
}

filter := (func: () -> (bool, int), predicate: (int) -> bool) -> () -> (bool, int) {
return () -> (bool, int) {
loop {
res := func();
(con, value) := res;
if !con || predicate(value) return res;
}
return (false, 0);
}
}

a := filter(x, (a:int) -> bool {return a%2 == 0})

iter := (array: [int]) -> () -> (bool, int) {
i := mut -1;
len := len(array)
return () -> (bool, int) {
i+=1;
if *i < len {
return (true, array[*i])
}
return (false, 0)
}
}

collect := (iter: ()->(bool,int)) -> [int]{
res := mut [int] [];
loop {
(con, value) := iter();
if (!con) {
break;
}
res += [value];
}
return *res;
}
15 changes: 10 additions & 5 deletions example_scripts/fizzbuzz
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ fizzbuzz := (number: int) -> string|int {
};

/*
Returns array filled whith increasing values, starting with value
Length of returned array equals size
Returns iterator of values from start to end
*/
iota := (value: int, size: int) -> [int] {
return [value; size] @ (index: int, value:int) -> int { return index+value};
iota := (start: int, end: int) -> () -> (bool, int) {
i:=mut start;
return () -> (bool, int) {
result := *i;
if(result > end) return (false, 0);
i+=1;
return (true, result);
}
};

iota(1, 15) @ fizzbuzz @ print;
iota(1, 15) @ fizzbuzz @ print $];
Loading
Loading