Skip to content

Commit 80d6ca8

Browse files
authored
Merge pull request #51 from samarium-lang/0.6.0
0.6.0
2 parents d0e3157 + 685973a commit 80d6ca8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+2781
-716
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 15 deletions
This file was deleted.

.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,4 @@
33
__pycache__
44
test
55
dist/
6-
site/
7-
src/*
8-
!src/samarium/
6+
site/

CHANGELOG.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,36 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [0.6.0] - 2024-06-14
9+
10+
### Added
11+
- `=>!` now accepts strings
12+
- Added an exception note for `string.split`
13+
- Allowed adding notes for `!!!`
14+
- Command system for the REPL
15+
- Data classes
16+
- Function composition
17+
- Made the semicolon optional for `!`, `!!!`, `*`, `**`
18+
- `math.max` and `math.min` now work with varargs
19+
- `String+`
20+
- `-String`
21+
- `~Table`
22+
- The REPL now automatically displays most expressions
23+
24+
### Changed
25+
- `String ++ -Number` now throws a type error (ambiguous operation)
26+
27+
### Fixed
28+
- Fixed function conversion when `@export`ing
29+
- Fixed internal name of `math.round`
30+
- Fixed `math.sqrt` using an incorrect variable name
31+
- Fixed multiline string handling in the REPL
32+
- Fixed REPL quitting on syntax error
33+
- `math.round` now correctly provides a default value for `ndigits`
34+
- The transpiler now correctly disallows literals around identifiers
35+
- `types.Boolean` should now correctly interract with external types
36+
- Various minor fixes to the transpiler
37+
838
## [0.5.3] - 2023-04-21
939

1040
### Fixed
@@ -401,4 +431,5 @@ Initial release 🚀
401431
[0.5.0]: https://github.com/samarium-lang/Samarium/compare/0.4.0...0.5.0
402432
[0.5.1]: https://github.com/samarium-lang/Samarium/compare/0.5.0...0.5.1
403433
[0.5.2]: https://github.com/samarium-lang/Samarium/compare/0.5.1...0.5.2
404-
[0.5.3]: https://github.com/samarium-lang/Samarium/compare/0.5.2...0.5.3
434+
[0.5.3]: https://github.com/samarium-lang/Samarium/compare/0.5.2...0.5.3
435+
[0.6.0]: https://github.com/samarium-lang/Samarium/compare/0.5.3...0.6.0

docs/arrays.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ If we want to remove duplicates of just one value, we can use `---`:
3131

3232
> `[/\\\, //, //, //\, /\\\, //, /\/] --- //` gives `[/\\\, //, //\, /\\\, /\/]`
3333
34-
Set-like union, interection, and symmetric difference can be obtained by using `|`, `&` and `^`, respectively:
34+
Set-like union, intersection, and symmetric difference can be obtained by using
35+
`|`, `&` and `^`, respectively:
3536

3637
```sm
3738
primes: [/\, //, /\/, ///, /\//, //\/]!;

docs/builtins.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,27 @@ This function will return what it writes to stdout (though not necessarily as
2828
a string), and can thus be used in an assignment statement for example.
2929

3030
```sm
31-
x: //\!;
31+
x: //\! == optional semicolon after !
3232
== the string "6" is written to stdout, and `x` now has the value 6 (number)
3333
```
3434

3535

3636
## THROW
3737

38-
Similarly to PRINTLN, objects can be written to standard error using `!!!`.
38+
Similarly to PRINTLN, some objects can be written to standard error using `!!!`.
3939
This will throw an error, and exit the program if the error is not caught.
40+
Only strings and arrays of length 2 can be used for throwing errors.
4041

41-
`"exception raised"!!!` will write `[Error] exception raised` to standard error.
42+
```sm
43+
"exception raised"!!! == optional semicolon after !!!
44+
== the following is written to stderr:
45+
== [Error] exception raised
46+
47+
== 2-element arrays can be used for adding notes to exceptions:
48+
["invalid date format", "use YYYY-MM-DD format"]!!!
49+
== [Error] invalid date format
50+
== [Note] use YYYY-MM-DD format
51+
```
4252

4353

4454
## EXIT
@@ -48,6 +58,11 @@ If a particular exit code is desired, it may be put after the exclamation mark:
4858

4959
`=>! //` will exit the program with exit code 3.
5060

61+
A string may also be supplied instead of a number, which will first write it to stderr
62+
and then exit the program with exit code 1.
63+
64+
> `=>! "uh oh";` is equivalent to `"uh oh\n" ~> /\; =>! /;`
65+
5166

5267
## HASH
5368

docs/classes.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,56 @@ multiply.outputs!; == [10, 21, 108]
199199
[^1]: Note that order will be preserved here — if both class `A` and class `B`
200200
implement a function `f`, and class `C` inherits them in the order `(A, B)`,
201201
then `C` will inherit `f` from class `A`, as it is inherited later.
202+
203+
204+
## Data Classes
205+
206+
Samarium has a shorthand syntax for defining classes whose main purpose is
207+
storing data:
208+
```sm
209+
@ Person {
210+
=> name age * {
211+
'name: name;
212+
'age: age;
213+
}
214+
215+
! * { * "$0($1, $2)" --- ['?!, 'name, 'age]; }
216+
:: other * { * ['name, 'age] :: [other.name, other.age]; }
217+
> other * { * ['name, 'age] > [other.name, other.age]; }
218+
>: other * { * ['name, 'age] >: [other.name, other.age]; }
219+
}
220+
```
221+
The above definition can be replaced with just
222+
```sm
223+
@! Person(name, age);
224+
```
225+
If we wish to define additional methods, we can just open a pair of braces after
226+
we specify the name and the fields like in a regular class:
227+
```sm
228+
@! Person(name, age) {
229+
birthday * { 'age+:; }
230+
}
231+
232+
p: Person("Jake", /\\//)! == Person("Jake", 19)
233+
p.birthday();
234+
p! == Person("Jake", 20)
235+
```
236+
If you'd like your dataclass to not have any fields, you can either leave the
237+
parentheses empty or omit them entirely:
238+
```sm
239+
@! Unit(); Unit()! == Unit()
240+
@! Unit; Unit()! == Unit()
241+
```
242+
243+
By default, all dataclass instances have a special method defined which returns
244+
its copy:
245+
```sm
246+
@! Person(name, age);
247+
248+
p: Person("Bob", /\\//);
249+
q: p;
250+
r: p$;
251+
q.name: "Alice";
252+
r.name: "Dave";
253+
p, q, r! == Person("Alice", 19) Person("Alice", 19) Person("Dave", 19)
254+
```

docs/examples.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
=> argv * {
88
file: argv<</>>;
99
data <~ file;
10-
data<<....-/>> ~> file;
10+
-data ~> file;
1111
}
1212
```
1313

docs/functions.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ b: /\;
2323
c: add(a, b);
2424
```
2525

26+
If no value is returned, the semicolon after `*` may be omitted:
27+
28+
```sm
29+
exit code * {
30+
? code {*}
31+
"Success"!;
32+
}
33+
```
2634

2735
## Main Function
2836

@@ -162,10 +170,62 @@ Primes below 100:
162170
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
163171
```
164172

173+
Just like with `*`, the semicolon after `**` can be omitted if no value is
174+
yielded:
175+
```sm
176+
foo * {**}
177+
178+
[]?!(foo())! == [null]
179+
```
180+
165181
Iterators support special `$` and cast `%` methods.
166182

167183
`Iterator%` returns the length of the iterator if available, and null otherwise.
168184

169185
`Iterator$` yields the next value of the iterator.
170186

171187
Iterators are always truthy.
188+
189+
190+
## Function Composition
191+
192+
Functions, types, and type aliases in Samarium can be composed together by using
193+
the `&` operator:
194+
195+
```sm
196+
(<-math.sqrt & <-operator.add)(//, //\)! == 3
197+
(<-types.Boolean & /?!)("1")! == true
198+
```
199+
```sm
200+
<=math.[abs, max, min];
201+
arrmap: []?! & <-iter.map;
202+
arrmap(abs, [/, `/, //\, -/\\, -/\/])! == [1, 0.5, 6, 4, 5]
203+
204+
high x * { * min([x, /\/]); }
205+
low x * { * max([x, \]); }
206+
clamp: high & low;
207+
208+
x: []?!(<<-//../\\/>>)! == [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8]
209+
arrmap(clamp, x)! == [0, 0, 0, 0, 1, 2, 3, 4, 5, 5, 5, 5]
210+
```
211+
```sm
212+
compose funcs... * {
213+
* <-iter.reduce(<-operator.and, funcs);
214+
}
215+
216+
repeat_function func times * {
217+
* compose(**[func]++times);
218+
}
219+
220+
foo x * {
221+
out: x +++ /?!("0.1")!
222+
* out;
223+
}
224+
225+
repeat_function(foo, /\/)(`/);
226+
== 0.9330329915368074
227+
== 0.9930924954370359
228+
== 0.9993070929904525
229+
== 0.9999306876841536
230+
== 0.999993068552217
231+
```

docs/null.md

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,4 @@ null:;
2020
y :: null && null ->? z!;
2121
```
2222

23-
The null value is not inserted implicitly for method calls, therefore:
24-
```sm
25-
V: .to_bit();
26-
```
27-
has to be written as
28-
```sm
29-
null:;
30-
V: null.to_bit();
31-
```
32-
or – without introducing any variables – as
33-
```sm
34-
V: (||).to_bit();
35-
```
36-
3723
Converting null to a number yields the value `0`.

docs/stdmath.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ Function                             | Use
2222
`is_int(x)` | Returns `1` if `x` is an integer, `0` otherwise. Equivalent to `x :: x$`.
2323
`is_prime(n)` | Returns `1` if `n` is prime, `0` otherwise.
2424
`lcm(a, b)` | Returns the least common multiple of `a` and `b`.<br>If any of the arguments is zero,<br> then the returned value is `0`.
25-
`max(array)` | Returns the largest value in `array`.
26-
`min(array)` | Returns the smallest value in `array`.
25+
`max(array[, values...])`  | Returns the largest value in `array` if only one argument is passed,<br>otherwise returns the largest value in `[array] + values`.
26+
`min(array[, values...])`  | Returns the smallest value in `array` if only one argument is passed,<br>otherwise returns the smallest value in `[array] + values`.
2727
`product(array)` | Multiplies the items of `array` from left to right and returns the total.<br>The `array`'s items must be numbers.
2828
`round(x[, ndigits])` | Returns `x` rounded to `ndigits` precision after the decimal point.<br>Works exactly like Python's [`round()`](https://docs.python.org/3/library/functions.html#round).
2929
`shl(a, b)` | Returns `a` shifted to the left by `b` bits.

0 commit comments

Comments
 (0)