Skip to content

Commit

Permalink
Thunk lambdas (fixes #18)
Browse files Browse the repository at this point in the history
  • Loading branch information
rockofox committed Dec 15, 2024
1 parent 78113f5 commit 1aca426
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
5 changes: 3 additions & 2 deletions lib/BytecodeCompiler.hs
Original file line number Diff line number Diff line change
Expand Up @@ -634,9 +634,10 @@ compileExpr (Parser.Impl name for methods) expectedType = do
compileExpr (Parser.Lambda args body) expectedType = do
fId <- allocId
curCon <- gets currentContext
let args' = if args == [Parser.Placeholder] then [] else args
let name = "__lambda" ++ show fId
let def = Parser.FuncDef name args body
let dec = Parser.FuncDec name (replicate (length args + 1) expectedType) []
let def = Parser.FuncDef name args' body
let dec = Parser.FuncDec name (replicate (length args' + 1) Parser.Any) []
let fun = Parser.Function [def] dec
_ <- compileExpr fun expectedType
lets' <- gets functions
Expand Down
10 changes: 5 additions & 5 deletions lib/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ binOpTable =
, [binary "as" Cast]
, [prefix "$" StrictEval]
, [prefix "!" Not]
, [binary "++" ListAdd]
, [binary "**" Power, binary "*" Mul, binary "/" Div]
, [binary "+" Add, binary "-" Sub]
, [binaryAny binaryFunctionCall]
, -- , [prefix "-" UnaryMinus]
[binary "**" Power, binary "*" Mul, binary "/" Div]
, [binary "%" Modulo]
[binary "%" Modulo]
, [binary ":" ListConcat]
, [binary "++" ListAdd]
, [binary "+" Add, binary "-" Sub]
, -- , [binary ">>" Then]
[binary "|>" Pipeline]
, [binary ">>=" bindExpr]
Expand Down Expand Up @@ -488,7 +488,7 @@ listPattern = do
lambda :: Parser Expr
lambda = do
symbol "\\"
args <- some (var <|> parens listPattern <|> array) <?> "lambda arguments"
args <- some (var <|> parens listPattern <|> array <|> placeholder) <?> "lambda arguments"
symbol "->"
Lambda args <$> expr <?> "lambda body"

Expand Down
2 changes: 1 addition & 1 deletion lib/Verifier.hs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ verifyExpr (FuncCall name args (Position (start, _))) = do
let eTypes = concatMap (\matchingBinding -> ([FancyError start (Set.singleton (ErrorFail ("Argument types do not match on " ++ name ++ ", expected: " ++ show matchingBinding.args ++ ", got: " ++ show argumentTypes))) | not fta])) matchingBindings
return $ [FancyError start (Set.singleton (ErrorFail $ "Could not find relevant binding for " ++ name)) | null matchingBindings] ++ eArgs ++ eTypes ++ eNoMatchi
verifyExpr (Lambda args body) = do
let argsAsBindings = map (\(Var name' _) -> VBinding{name = name', args = [], ttype = Any, generics = []}) args
let argsAsBindings = map (\case (Var name' _) -> VBinding{name = name', args = [], ttype = Any, generics = []}; Placeholder -> VBinding{name = "__placeholder", args = [], ttype = Any, generics = []}; _ -> error "Invalid lambda argument") args
modify (\state -> state{frames = (VerifierFrame{bindings = Set.fromList argsAsBindings, ttypes = Map.empty, ftype = Any, fname = "__lambda"}) : frames state})
bodyErrors <- verifyExpr body
modify (\state -> state{frames = tail (frames state)})
Expand Down
2 changes: 1 addition & 1 deletion share/std/test.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ impl Monad for TestSuite = do
sequence TestSuite{name: n, results: r} TestSuite{name: n2, results: r2} = TestSuite{name: n, results: r ++ r2}
end

let testCase (n: String f: (Any -> Bool)) : TestSuite = TestSuite{results: [TestResult{name: n, result: f 1}]}
let testCase (n: String f: (Bool)) : TestSuite = TestSuite{results: [TestResult{name: n, result: f _}]}

let evalTestSuite (suite: TestSuite) : IO = do
let resultss = suite.results
Expand Down

0 comments on commit 1aca426

Please sign in to comment.