Skip to content

Instantly share code, notes, and snippets.

@oguzalb
Created July 21, 2023 14:08
Show Gist options
  • Save oguzalb/590d034cde3fae9c3bc1c278c687246c to your computer and use it in GitHub Desktop.
Save oguzalb/590d034cde3fae9c3bc1c278c687246c to your computer and use it in GitHub Desktop.
adding arrow functions to Python grammar
diff --git a/Grammar/Tokens b/Grammar/Tokens
index 618ae81..29bcfeb 100644
--- a/Grammar/Tokens
+++ b/Grammar/Tokens
@@ -11,6 +11,7 @@ RPAR ')'
LSQB '['
RSQB ']'
COLON ':'
+FUNC_SIGN '=>'
COMMA ','
SEMI ';'
PLUS '+'
diff --git a/Grammar/python.gram b/Grammar/python.gram
index c1863ae..337a6d0 100644
--- a/Grammar/python.gram
+++ b/Grammar/python.gram
@@ -674,6 +674,7 @@ expressions[expr_ty]:
expression[expr_ty] (memo):
| invalid_expression
| invalid_legacy_expression
+ | lambarrowdef
| a=disjunction 'if' b=disjunction 'else' c=expression { _PyAST_IfExp(b, a, c, EXTRA) }
| disjunction
| lambdef
@@ -858,6 +859,9 @@ group[expr_ty]:
lambdef[expr_ty]:
| 'lambda' a=[lambda_params] ':' b=expression {
_PyAST_Lambda((a) ? a : CHECK(arguments_ty, _PyPegen_empty_arguments(p)), b, EXTRA) }
+lambarrowdef[expr_ty]:
+ | '(' a=[params] ')' '=>' b=expression {
+ _PyAST_Lambda((a) ? a : CHECK(arguments_ty, _PyPegen_empty_arguments(p)), b, EXTRA) }
lambda_params[arguments_ty]:
| invalid_lambda_parameters
@oguzalb
Copy link
Author

oguzalb commented Jul 21, 2023

git clone https://github.com/python/cpython.git --depth 1
./configure
make regen-token
make regen-pegen
make
./python.exe

@angelhdzdev
Copy link

Is this really happening!? Can we finally use arrow functions in Python!? Or did I misunderstand the goal of this patch?

tasks: list[Task] = filter(myArray, (task: Task) => task.isCompleted)

Cheers!

@oguzalb
Copy link
Author

oguzalb commented Jul 23, 2023

@angelhdzmultimedia
Hey hi! I was just trying to show people that it's easy to change the Python grammar = ) I don't think this kind of a change will happen.
Though, you are of course free to make the change and play with the arrow functions in local projects! Cheers!

@angelhdzdev
Copy link

Thank you very much! Nice work. 🔥🤝

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment