Skip to content

Commit

Permalink
Add lambdas to E2 (#2829)
Browse files Browse the repository at this point in the history
* Initial lambda implementation

* Add ops on creation

* New implementation

* Functions are now tables, containing parameter signature, return type and inner function
* function:getParameterTypes()
* function:getReturnType()
* Don't reset global variables on `@strict`, fixing issue with top level locals not working as upvalues since they'd be reset by the runtime. I don't think this would actually affect anyone since you shouldn't be able to use variables before they're assigned, but it's `@strict` only behavior anyway.

* Fix missing parity change

I'd already fixed this bug with functions that have return values but this was not fixed for functions without return values. Also added a test case for this.

* More tests, enforce returns at compile time

* Add tests to ensure variadic parameters, void parameters and implicit parameters aren't allowed

* Fix lambdas potentially not returning at all codepaths like functions now are expected to do. Added a test for that.

* Fix early returns

I should stop prematurely optimizing this.. This is like the third or second time I've made this mistake lol

* Fix highlighting

Actually a really easy fix. Surprising

* Fix merge regression
  • Loading branch information
Vurv78 authored Nov 13, 2023
1 parent a4e6c4f commit 3522df9
Show file tree
Hide file tree
Showing 13 changed files with 416 additions and 130 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## SHOULD_FAIL:COMPILE

function test(X:void) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## SHOULD_FAIL:COMPILE

# Implicit number fallback is not going to be allowed.
const J = function(X) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## SHOULD_FAIL:COMPILE

const X = function() {
if (1) { return "str" }
# doesn't return string
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## SHOULD_FAIL:COMPILE

const X = function() {
if (1) { return "str" }
return 22
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## SHOULD_FAIL:COMPILE

const X = function(...A:array) {}
const Y = function(...A:table) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## SHOULD_FAIL:COMPILE

const X = function(X:void) {}
87 changes: 87 additions & 0 deletions data/expression2/tests/runtime/base/lambdas.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
## SHOULD_PASS:EXECUTE

# Returns

assert( (function() { return 55 })()[number] == 55 )
assert( (function() { return "str" })()[string] == "str" )

# Upvalues

const Wrapper = function(V:number) {
return function() {
return V
}
}

const F1 = Wrapper(55)[function]

if (1) {
if (2) {
local V = 22
assert(F1()[number] == 55)
}
}

assert(F1()[number] == 55)
assert(F1()[number] == 55)

const F2 = Wrapper(1238)[function]

assert(F2()[number] == 1238)
#local V = 21
assert(F2()[number] == 1238)

const IsEven = function(N:number) {
return N % 2 == 0
}

const Not = function(N:number) {
return !N
}

const IsOdd = function(N:number) {
return Not(IsEven(N)[number])[number]
}

assert(IsOdd(1)[number] == 1)
assert(IsOdd(2)[number] == 0)

assert( ((function() { return function() { return 55 } })()[function])()[number] == 55 )

const Identity = function(N:number) {
return N
}

assert(Identity(2)[number] == 2)
assert(Identity(2193921)[number] == 2193921)

local SayMessage = function() {}

const SetMessage = function(Message:string) {
SayMessage = function() {
return Message
}
}

SetMessage("There's a snake in my boot!")

assert( SayMessage()[string] == "There's a snake in my boot!" )
assert( SayMessage()[string] == "There's a snake in my boot!" )

SetMessage("Reach for the sky!")

assert( SayMessage()[string] == "Reach for the sky!" )

const EarlyReturn = function() {
return
}

Ran = 0

function wrapper() {
EarlyReturn()
Ran = 1
}

wrapper()
assert(Ran)
Loading

0 comments on commit 3522df9

Please sign in to comment.