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

Allow using anonymous functions in operator expressions #12015

Open
wants to merge 5 commits into
base: development
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/core/ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ let gen_doc_text_opt = Option.map gen_doc_text

let get_own_doc_opt = Option.map_default (fun d -> d.doc_own) None

let is_postfix (e,_) op = match op with
let is_postfix op = match op with
| Increment | Decrement | Not -> true
| Neg | NegBits | Spread -> false

Expand Down
13 changes: 11 additions & 2 deletions src/syntax/grammar.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,7 @@ and parse_block_var = function%parser
and parse_block_elt s = match%parser s with
| [ [%let vl,p = parse_block_var] ] ->
(EVars vl,p)
| [ (Kwd Function,p1); [%let e = parse_function p1 false]; semicolon as _s ] -> e
| [ (Kwd Inline,p1) ] ->
begin match%parser s with
| [ (Kwd Function,_); [%let e = parse_function p1 true]; semicolon as _s ] -> e
Expand Down Expand Up @@ -1487,7 +1488,15 @@ and expr s = match%parser s with
syntax_error (Expected [")";",";":"]) s (expr_next (EParenthesis e, punion p1 (pos e)) s))
)
| [ (BkOpen,p1); [%let e = parse_array_decl p1] ] -> expr_next e s
| [ (Kwd Function,p1); [%let e = parse_function p1 false]; ] -> e
| [ (Kwd Function,p1); [%let e = parse_function p1 false]; [%s s]; ] ->
begin match Stream.peek s with
| Some (POpen,_) | Some (BkOpen,_) ->
e
| Some (Unop op, _) when is_postfix op ->
e
| _ ->
expr_next e s
end
| [ (Unop op,p1); expr as e ] -> make_unop op e p1
| [ (Spread,p1); expr as e ] -> make_unop Spread e (punion p1 (pos e))
| [ (Binop OpSub,p1); expr as e ] ->
Expand Down Expand Up @@ -1611,7 +1620,7 @@ and expr_next' e1 s = match%parser s with
make_binop OpGt e1 e2)
| [ (Binop op,_); secure_expr as e2 ] -> make_binop op e1 e2
| [ (Spread,_); secure_expr as e2 ] -> make_binop OpInterval e1 e2
| [ (Unop op,p) ] when is_postfix e1 op ->
| [ (Unop op,p) ] when is_postfix op ->
expr_next (EUnop (op,Postfix,e1), punion (pos e1) p) s
| [ (Question,_); expr as e2 ] ->
begin match%parser s with
Expand Down
14 changes: 14 additions & 0 deletions tests/misc/projects/Issue10782/ArrayAccess.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class C {
public function new() {}

public function call() {
trace("called!");
}
}

function main() {
final f = function() {
trace("f!");
}
[new C()][0].call();
}
22 changes: 22 additions & 0 deletions tests/misc/projects/Issue10782/Main.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
@:callable
abstract Function(Int->Int) to Int->Int from Int->Int {
@:op(a * b)
static inline function chain(a:Function, b:Function):Function {
return function(i) {
return a(b(i));
};
}
}

class Main {
static function main() {
var f:Function = function(a) {
return a + 1;
};

var g = function(a) { return a * a; } * f;
if (g(10) != 121) {
throw "Incorrect function return value";
}
}
}
7 changes: 7 additions & 0 deletions tests/misc/projects/Issue10782/NoPostfix.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function main() {
var i = 0;
final f = function() {
trace("f!");
}
--i;
}
1 change: 1 addition & 0 deletions tests/misc/projects/Issue10782/compile-array-access.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--run ArrayAccess
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ArrayAccess.hx:5: called!
1 change: 1 addition & 0 deletions tests/misc/projects/Issue10782/compile-no-postfix.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--run NoPostfix
1 change: 1 addition & 0 deletions tests/misc/projects/Issue10782/compile.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--run Main
24 changes: 24 additions & 0 deletions tests/misc/projects/Issue5854/Main.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Main {
static function main() {
final f = (_) -> {
throw "This function shouldn't have been called!!";
}
("hello");

final f = function(_) {
throw "This function shouldn't have been called!!";
}
("hello");

// named locals
function f(_) {
throw "This function shouldn't have been called!!";
}
("hello");

final g = function f(_) {
throw "This function shouldn't have been called!!";
}
("hello");
}
}
10 changes: 0 additions & 10 deletions tests/misc/projects/Issue5854/NamedLocal.hx

This file was deleted.

1 change: 1 addition & 0 deletions tests/misc/projects/Issue5854/Nested.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
final a = (function() {return 1;} ());
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
class UnnamedLocal {
class UnnamedLValue {
static function main() {
function(str) {
trace(str);
}
("hello");

(str) -> {
trace(str);
}
("hello");
}
}
2 changes: 1 addition & 1 deletion tests/misc/projects/Issue5854/compile-fail.hxml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
--main UnnamedLocal
--main UnnamedLValue
3 changes: 2 additions & 1 deletion tests/misc/projects/Issue5854/compile-fail.hxml.stderr
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
UnnamedLocal.hx:3: lines 3-5 : Unnamed lvalue functions are not supported
UnnamedLValue.hx:3: lines 3-5 : Unnamed lvalue functions are not supported
UnnamedLValue.hx:8: lines 8-10 : Unnamed lvalue functions are not supported
1 change: 1 addition & 0 deletions tests/misc/projects/Issue5854/compile-nested-fail.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--main Nested
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Nested.hx:1: characters 37-38 : Expected ) or , or :
2 changes: 1 addition & 1 deletion tests/misc/projects/Issue5854/compile.hxml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
--run NamedLocal
--main Main
Empty file.
Loading