Skip to content

Commit

Permalink
feat: added shebang support
Browse files Browse the repository at this point in the history
  • Loading branch information
sigmasoldi3r committed Nov 27, 2023
1 parent 408fc20 commit 06dbca5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
11 changes: 11 additions & 0 deletions examples/shebang.saturn
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env saturnus

class Printer {
fn print(self) {
print("Hello World!");
}
}

let obj = Printer {};

obj->print();
35 changes: 22 additions & 13 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ files next to your `.saturn` files.
Currently the CD is disabled, however you can grab the latest [artifacts from
the nightly branch][nightly], **BUT!**

[nightly]:
https://github.com/sigmasoldi3r/Saturnus/actions/workflows/build-artifacts.yml
[nightly]: https://github.com/sigmasoldi3r/Saturnus/actions/workflows/build-artifacts.yml

**BUT...** beware that the artifacts will be surely outdated.

Expand All @@ -76,6 +75,13 @@ Then you will have the executable at `target/release/saturnus`. (You need the

[rustup]: https://www.rust-lang.org/learn/get-started

Saturnus scripts can be made executable with the shebang, like:

```s
#!/usr/bin/env saturnus
print("Hello World!");
```

## Language Basics

> **Note**
Expand Down Expand Up @@ -137,9 +143,9 @@ invoking object methods. Otherwise you can run with the dot `.`, and provide the
`self` parameter, just like in Lua.

```rs
// Member dispatch
// Member dispatch, aka foo:bar() Lua's equivalent
foo->bar();
// Like usual
// Like usual, the static dispatch.
foo.bar(foo);
Foo.static_bar();
```
Expand Down Expand Up @@ -167,13 +173,16 @@ while let some = thing() {
print("Some is " ++ some);
}

// Note that the .. operator must be imported
use { operators: { `..` } } in std;

// Numeric for? Easy sugar:
for i in 1..10 {
print("i = " ++ i)
}

// Now, the classical foreach:
for entry in entries() {
for entry in Object.entries([1, 2, 3]) {
print(entry._0 ++ " = " ++ entry._1);
}
// Note: This is a raw iterator loop, and cannot be used in place of an
Expand Down Expand Up @@ -239,7 +248,7 @@ let anon = (a, b) => {

// And if an anonymous function ONLY has one expression inside (Without ";"),
// that expression is an implicit return statement:
collections::reduce([1, 2, 3], (a, b) => a + b);
collections.reduce([1, 2, 3], (a, b) => a + b);
// Pretty cool
```

Expand Down Expand Up @@ -267,8 +276,8 @@ class Person {

// Here you'll clearly see the difference:
let person = Person { name: "Mr. Foo" };
let name = person.get_name(); // Dynamic dispatch
Person::greet(person); // Static method dispatch!
let name = person->get_name(); // Dynamic dispatch
Person.greet(person); // Static method dispatch!
```

Polymorphism example, altough if you're familiar with the term ["Duck
Expand Down Expand Up @@ -298,8 +307,8 @@ let foo = Foo {};
let bar = Bar {};

let consumer = FooBarConsumer {};
print(consumer.consume(foo));
print(consumer.consume(bar));
print(consumer->consume(foo));
print(consumer->consume(bar));
```

Also you can decorate the code:
Expand All @@ -322,14 +331,14 @@ fn suite() {
Custom operators!

```rs
let { `..` } = use std; // You can bring em from libraries
let { operators: { `..` } } = use std; // You can bring em from libraries

// Or define in-situ
fn `->`(left, right) {
fn `-->`(left, right) {
return left ++ right ++ right ++ left;
}

let foo = "foo" -> "bar";
let foo = "foo" --> "bar";
// Will yield "foobarbarfoo"
```

Expand Down
2 changes: 1 addition & 1 deletion src/parser/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::helpers::generate_operator_function_name;
peg::parser! {
grammar saturnus_script() for str {
pub rule script() -> Script
= _ statements:statement() ** __ _
= ("#!" (!EOL() ANY())* EOL())? _ statements:statement() ** __ _
{ Script { statements } }

// Statements
Expand Down

0 comments on commit 06dbca5

Please sign in to comment.