Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minispec: LambdaExpr #2235

Merged
merged 1 commit into from
Mar 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 9 additions & 0 deletions demo/lambda1/lambda.gop
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
func f(x float64, t func(float64) float64) float64 {
return t(x)
}

echo f(1.0, x => 2 * x)
echo f(5.0, (x) => {
return 2 * x
})

22 changes: 14 additions & 8 deletions doc/spec/mini/mini.gop
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ ConstSpec = IdentifierList ?Type ?("=" ExpressionList)

IdentifierList = IDENT % ","

LambdaExprList = LambdaExpr % ","

ExpressionList = Expression % ","

VarDecl = "var" (VarSpec | "(" *(VarSpec ";") ")")

VarSpec = IdentifierList ("=" ExpressionList | Type ?("=" ExpressionList))
VarSpec = IdentifierList ("=" ExpressionList | Type ?("=" LambdaExprList))

TypeDecl = "type" (TypeSpec | "(" *(TypeSpec ";") ")")

Expand Down Expand Up @@ -81,7 +83,7 @@ Statement =
FallthroughStmt | IfStmt | SwitchStmt | ForStmt | DeferStmt | Block |
LabeledStmt | CommandStmt | SimpleStmt

ReturnStmt = "return" ?ExpressionList
ReturnStmt = "return" ?LambdaExprList

BreakStmt = "break" ?IDENT

Expand Down Expand Up @@ -113,11 +115,11 @@ IncDecStmt = Expression ("++" | "--")

Assignment = ExpressionList (
"=" | "+=" | "-=" | "|=" | "^=" |
"*=" | "/=" | "%=" | "<<=" | ">>=" | "&=" | "&^=") ExpressionList
"*=" | "/=" | "%=" | "<<=" | ">>=" | "&=" | "&^=") LambdaExprList

ExpressionStmt = Expression

CommandStmt = IDENT ?("." IDENT) ExpressionList
CommandStmt = IDENT ?("." IDENT) LambdaExprList

EmptyStmt = ""

Expand Down Expand Up @@ -155,6 +157,10 @@ MethodElem = IDENT Signature

// -----------------------------------------------------------------

LambdaExpr = ("(" ?(IDENT % ",") ")" | ?IDENT) "=>" LambdaBody | Expression

LambdaBody = Block | "(" LambdaExpr % "," ")" | LambdaExpr

RangeExpr = rangeExprEnd | Expression ?rangeExprEnd

rangeExprEnd = ":" Expression ?(":" ?Expression)
Expand All @@ -171,7 +177,7 @@ PrimaryExpr = Operand *(
CallOrConversion | SelectorOrTypeAssertion | IndexOrSlice | ErrWrap)

Operand =
INT ?UNIT | FLOAT ?UNIT | STRING | CHAR | RAT | IMAG | "(" Expression ")" |
INT ?UNIT | FLOAT ?UNIT | STRING | CHAR | RAT | IMAG | "(" LambdaExpr ")" |
LiteralValue | CompositeLit | FunctionLit | Env | "c" ++ QSTRING | "py" ++ QSTRING |
ListCompositeLit | DomainTextLit | NamedCompositeLit | IDENT

Expand All @@ -183,7 +189,7 @@ NamedCompositeLit = TypeName ++ "{" ElementList ?"," "}"

CompositeLit = (MapType | StructType) LiteralValue

ListCompositeLit = LBRACK ?("..." | Expression % ",") (RBRACK ++ Type LiteralValue | RBRACK)
ListCompositeLit = LBRACK ?("..." | LambdaExpr % ",") (RBRACK ++ Type LiteralValue | RBRACK)

LiteralValue = "{" ElementList ?"," "}"

Expand All @@ -193,11 +199,11 @@ KeyedElement = ?(Key ":") Element

Key = LiteralValue | Expression

Element = LiteralValue | Expression
Element = LiteralValue | LambdaExpr

FunctionLit = "func" Signature Block

CallOrConversion = "(" ?(Expression % ",") ?"..." ?"," ")"
CallOrConversion = "(" ?(LambdaExpr % ",") ?"..." ?"," ")"

SelectorOrTypeAssertion = "." (IDENT | "(" Type ")")

Expand Down