Skip to content

Commit

Permalink
Syntax: single statement closure
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanjermakov committed Apr 10, 2024
1 parent 237c030 commit 9e52ca9
Show file tree
Hide file tree
Showing 12 changed files with 21 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn main() {
println(
shapes
.iter()
.map(|s| { s.area() })
.map(|s| s.area())
.collect<List<_>>()
.show()
)
Expand Down
4 changes: 2 additions & 2 deletions data/features.no
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ fn main() {
}

// closure
let square: |Num|: Num = |a| { a^2 }
let here = || { println("here") }
let square: |Num|: Num = |a| a^2
let here = || println("here")

// fn reference
let p = println
Expand Down
2 changes: 1 addition & 1 deletion example/example.no
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub fn main() {
println(
shapes
.iter()
.map(|s| { s.area() })
.map(|s| s.area())
.collect<List<_>>()
.show()
)
Expand Down
2 changes: 1 addition & 1 deletion example/rule110.no
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn fmtGen(gen: List<Bool>, total: Int): String {
let pad = repeat(" ", total - gen.iter().count()).collect<String>()
let g = gen
.iter()
.map(|b| { if b { "x" } else { " " } })
.map(|b| if b { "x" } else { " " })
.collect<String>()
pad.concat(g)
}
Expand Down
2 changes: 1 addition & 1 deletion nois.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ type ::= type-bounds | fn-type | hole
;
block ::= O-BRACE statement* C-BRACE
;
closure-expr ::= closure-params type-annot? block
closure-expr ::= closure-params type-annot? (block | statement)
;
closure-params ::= PIPE (param (COMMA param)*)? COMMA? PIPE
;
Expand Down
7 changes: 5 additions & 2 deletions src/ast/operand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { assert } from '../util/todo'
import { Expr, buildExpr } from './expr'
import { AstNode, Param, buildParam } from './index'
import { MatchExpr, Pattern, buildMatchExpr, buildNumber, buildPattern } from './match'
import { Block, buildBlock } from './statement'
import { Block, buildBlock, buildStatement } from './statement'
import { Type, buildType } from './type'

export type Operand = (
Expand Down Expand Up @@ -158,7 +158,10 @@ export const buildClosureExpr = (node: ParseNode): ClosureExpr => {
.filter(n => n.kind === 'param')
.map(n => buildParam(n))
const returnType = nodes.at(idx)?.kind === 'type-annot' ? buildType(nodes[idx++]) : undefined
const block = buildBlock(nodes[idx++])
const block: Block =
nodes[idx].kind === 'block'
? buildBlock(nodes[idx++])
: { kind: 'block', parseNode: nodes[idx], statements: [buildStatement(nodes[idx++])] }
return { kind: 'closure-expr', parseNode: node, params, block, returnType }
}

Expand Down
4 changes: 2 additions & 2 deletions src/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ pub fn main() {
println(
shapes
.iter()
.map(|s| { s.area() })
.map(|s| s.area())
.collect<List<_>>()
.show()
)
Expand Down Expand Up @@ -220,7 +220,7 @@ fn fmtGen(gen: List<Bool>, total: Int): String {
let pad = repeat(" ", total - gen.iter().count()).collect<String>()
let g = gen
.iter()
.map(|b| { if b { "x" } else { " " } })
.map(|b| if b { "x" } else { " " })
.collect<String>()
pad.concat(g)
}`
Expand Down
5 changes: 2 additions & 3 deletions src/parser/fns/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Parser } from '..'
import { syntaxError } from '../../error'
import { TokenKind, lexerKeywordKinds } from '../../lexer/lexer'
import { parseBlock, parseParam, parseStatement, parseUseStmt } from './statement'
import { parseTypeAnnot } from './type'
Expand Down Expand Up @@ -60,7 +59,7 @@ export const parseModule = (parser: Parser): void => {

/**
* TODO: closure generics
* closure-expr ::= closure-params type-annot? block
* closure-expr ::= closure-params type-annot? (block | statement)
*/
export const parseClosureExpr = (parser: Parser): void => {
const mark = parser.open()
Expand All @@ -71,7 +70,7 @@ export const parseClosureExpr = (parser: Parser): void => {
if (parser.at('o-brace')) {
parseBlock(parser)
} else {
parser.advanceWithError(syntaxError(parser, 'expected block'))
parseStatement(parser)
}
parser.close(mark, 'closure-expr')
}
Expand Down
4 changes: 2 additions & 2 deletions src/semantic/semantic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ fn main() {
const code = `\
fn main() {
while true {
(|| { break })()
(|| break)()
}
}`
const ctx = check(code)
Expand Down Expand Up @@ -611,7 +611,7 @@ type Foo(x: ||: Int)
impl Foo {
fn main() {
let foo = Foo::Foo(|| { 5 })
let foo = Foo::Foo(|| 5)
let a = (foo.x)()
a()
return unit
Expand Down
4 changes: 2 additions & 2 deletions src/std/future.no
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ impl Runtime {
s(res)
}
next.subscribers.clear()
self.pending.popAt(self.pending.iter().position(|e| { eqRef(next, e) })!)
self.pending.popAt(self.pending.iter().position(|e| eqRef(next, e))!)
unit
})
}
if self.queue.count() > 0 || self.pending.count() > 0 {
deferFor(|| { self.loop() }, self.pollingRate)
deferFor(|| self.loop(), self.pollingRate)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/std/iter/mod.no
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub trait Iter<T> {
fn next(self): Option<T>

fn count(self): Int {
self.fold(|acc, _| { acc + 1 }, 0)
self.fold(|acc, _| acc + 1, 0)
}

fn last(self): Option<T> {
Expand Down
2 changes: 1 addition & 1 deletion src/std/string.no
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl Collector<String> for String {
impl Collector<Char> for String {
fn fromIter(iter: Iter<Char>): Self {
// TODO: method ref
iter.map(|c| { c.show() }).collect<String>()
iter.map(|c| c.show()).collect<String>()
}
}

Expand Down

0 comments on commit 9e52ca9

Please sign in to comment.