Skip to content

Commit

Permalink
Don't require 'struct' prefix for structure types and values + change…
Browse files Browse the repository at this point in the history
… do blocks
  • Loading branch information
ClementNerma committed Dec 9, 2024
1 parent f37ac48 commit 33b2f74
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 46 deletions.
56 changes: 23 additions & 33 deletions crates/parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,7 @@ pub fn program(
just("map")
.not_followed_by(possible_ident_char)
.map(|_| SingleValueType::UntypedMap),
just("struct")
.ignore_then(ms)
.ignore_then(char('{'))
char('{')
.ignore_then(msnl)
.ignore_then(
ident
Expand Down Expand Up @@ -469,29 +467,22 @@ pub fn program(
let lambda = char('{')
.ignore_then(msnl)
.ignore_then(
choice::<FnSignature, _>((
char('|')
.ignore_then(msnl)
.ignore_then(
fn_arg
.separated_by(char(',').padded_by(msnl))
.spanned()
.map(RuntimeSpan::from)
.map(|args| FnSignature {
args,
ret_type: None,
}),
)
.then_ignore(msnl)
.then_ignore(char('|').critical_with_no_message())
.then_ignore(msnl),
// Arg-less
empty().spanned().map(|eaten| FnSignature {
args: RuntimeSpan::from(eaten.replace(vec![])),
ret_type: None,
}),
))
.spanned(),
char('|')
.ignore_then(msnl)
.ignore_then(
fn_arg
.separated_by(char(',').padded_by(msnl))
.spanned()
.map(RuntimeSpan::from)
.map(|args| FnSignature {
args,
ret_type: None,
}),
)
.then_ignore(msnl)
.then_ignore(char('|').critical_with_no_message())
.spanned()
.then_ignore(msnl),
)
.then(
raw_block
Expand Down Expand Up @@ -560,10 +551,10 @@ pub fn program(
.then_ignore(msnl)
.then_ignore(char('}').critical_with_no_message())
.map(Value::Map),
// Lambdas
lambda.clone().map(Value::Lambda),
// Structures
just("struct")
.ignore_then(msnl)
.ignore_then(char('{'))
char('{')
.ignore_then(msnl)
.ignore_then(
ident
Expand Down Expand Up @@ -593,8 +584,6 @@ pub fn program(
.ignore_then(ident.critical("expected a function name"))
.spanned()
.map(Value::FnAsValue),
// Lambdas
lambda.clone().map(Value::Lambda),
));

let single_op = choice::<SingleOp, _>((char('!').to(SingleOp::Neg),));
Expand Down Expand Up @@ -1666,8 +1655,9 @@ pub fn program(
//
// Base blocks
//
lookahead(char('{'))
.ignore_then(block.spanned())
just("do")
.ignore_then(s)
.ignore_then(block.spanned().critical("expected a block"))
.map(Instruction::DoBlock),
//
// Include statement
Expand Down
24 changes: 12 additions & 12 deletions docs/Tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ Most of the time, you should prefer explicitly using the `return` keyword as it
Structures have a rigid type that associated _fields_ and values:

```reshell
let person = struct {
let person = {
name: 'John',
age: 20
}
Expand All @@ -603,11 +603,11 @@ echo ($person.name) # Prints: Jack
Structures are typed like this:

```reshell
fn sayHello(person: struct { name: string, age: int }) {
fn sayHello(person: { name: string, age: int }) {
echo "Hello, `$person.name`!"
}
sayHello(struct {
sayHello({
name: 'John',
age: 20
})
Expand All @@ -616,7 +616,7 @@ sayHello(struct {
To debug a value of any type, we can use the `dbg` function:

```shell
dbg (struct {
dbg ({
name: 'John',
age: 20
})
Expand Down Expand Up @@ -746,12 +746,12 @@ for key, value in $map {
Maps can be typed using `map[<inner type>]`, e.g.:

```reshell
let value: map[int] = map(struct { a: 1, b: 2 })
let value: map[string] = map(struct { a: 'a', b: 'b' })
let value: map[int] = map { a: 1, b: 2 }
let value: map[string] = map { a: 'a', b: 'b' }
# we can also use the 'map' type alone, which is functionally equivalent
# to map[any]:
let value: map = map(struct { a: 1, b: 'c' })
let value: map = map { a: 1, b: 'c' }
```

### Type aliases
Expand All @@ -768,7 +768,7 @@ fn sayHello(person: Person) {
echo "Hello, `$person.name`!"
}
sayHello(struct {
sayHello({
name: 'Jack',
age: 20
})
Expand Down Expand Up @@ -864,8 +864,8 @@ To simplify some assignments, we can use _destructuring_:

```reshell
let [a, b, c] = [1, 2, 3]
let { a, b, c } = struct { a: 1, b: 2, c: 3 }
let { a, b, c } = map(struct { a: 1, b: 2, c: 3 })
let { a, b, c } = { a: 1, b: 2, c: 3 }
let { a, b, c } = map { a: 1, b: 2, c: 3 }
echo $a # Prints: 1
echo $b # Prints: 2
Expand All @@ -875,15 +875,15 @@ echo $c # Prints: 3
Variables can also be renamed:

```reshell
let { a: b } = struct { a: 1 }
let { a: b } = { a: 1 }
echo $b # Prints: 1
```

We can also do some nesting:

```reshell
let { a: [b, c, { d }] } = struct { a: [1, 2, struct { d: 3 }] }
let { a: [b, c, { d }] } = { a: [1, 2, { d: 3 }] }
dbg $b # Prints: 1
dbg $c # Prints: 2
Expand Down
2 changes: 1 addition & 1 deletion vscode-syntax/syntaxes/reshell.tmLanguage.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"name": "constant"
},
{
"match": "\\b(let|mut|if|else|for|in|while|typematch|match|case|continue|break|fn|return|throw|alias|type|try|catch|self|include|typeis)\\b",
"match": "\\b(let|mut|if|else|for|in|while|typematch|match|case|continue|break|fn|return|throw|alias|type|try|catch|do|self|include|typeis)\\b",
"name": "keyword"
},
{
Expand Down

0 comments on commit 33b2f74

Please sign in to comment.