Skip to content

Commit 1a74233

Browse files
committed
Extra test cases around lambdas and "no main"
1 parent 5f6f845 commit 1a74233

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

lib/BytecodeCompiler.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ compileExpr (Parser.Cast from to) = do
372372
compileType (Parser.Var "Int" _) = [Push $ DInt 0]
373373
compileType (Parser.Var "Float" _) = [Push $ DFloat 0.0]
374374
compileType (Parser.Var "Double" _) = [Push $ DDouble 0.0]
375+
compileType (Parser.Var "String" _) = [Push $ DString ""]
375376
compileType (Parser.Var "CPtr" _) = [Push $ DCPtr $ ptrToWordPtr nullPtr]
376377
compileType x = error $ "Type " ++ show x ++ " is not implemented"
377378
compileExpr st@(Parser.Struct _ fields) = do

tests/IntegrationSpec.hs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,25 @@ spec = do
215215
end
216216
|]
217217
`shouldReturn` "[2,3,4]\n"
218+
it "Can use a lambda calling a nested function" $ do
219+
compileAndRun
220+
[r|
221+
let main => IO = do
222+
let square (x: Int) = x * x
223+
let toThird (x: Int) = x * square x
224+
println map (\x -> toThird x), [1, 2, 3]
225+
end
226+
|]
227+
`shouldReturn` "[1,8,27]\n"
228+
it "Can use strict values in lambda" $ do
229+
compileAndRun
230+
[r|
231+
let main => IO = do
232+
let strict = $2
233+
println map (\x -> x * strict), [1, 2, 3]
234+
end
235+
|]
236+
`shouldReturn` "[2,4,6]\n"
218237
describe "Recursion" $ do
219238
it "Can use recursion" $ do
220239
compileAndRun
@@ -258,6 +277,31 @@ spec = do
258277
|]
259278
`shouldReturn` "14\n"
260279

280+
describe "No main" $ do
281+
it "Hello World" $ do
282+
compileAndRun
283+
[r|
284+
println "Hello, World!"
285+
|]
286+
`shouldReturn` "Hello, World!\n"
287+
it "\"99\" bottles of beer" $ do
288+
compileAndRun
289+
[r|
290+
let bottles (i: Int) => IO = do
291+
if i > 0 then do
292+
println ^i : " bottles of beer on the wall, " : ^i : " bottles of beer."
293+
println "Take one down and pass it around, " : ((i) - 1) as String : " bottles of beer on the wall.\n"
294+
bottles (i)-1
295+
else do
296+
println "No more bottles of beer on the wall, no more bottles of beer."
297+
println "Go to the store and buy some more, 99 bottles of beer on the wall."
298+
end
299+
end
300+
301+
bottles 3
302+
|]
303+
`shouldReturn` "3 bottles of beer on the wall, 3 bottles of beer.\nTake one down and pass it around, 2 bottles of beer on the wall.\n\n2 bottles of beer on the wall, 2 bottles of beer.\nTake one down and pass it around, 1 bottles of beer on the wall.\n\n1 bottles of beer on the wall, 1 bottles of beer.\nTake one down and pass it around, 0 bottles of beer on the wall.\n\nNo more bottles of beer on the wall, no more bottles of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall.\n"
304+
261305
-- describe "Import"
262306
-- it "Can find function based on type with lists, multiple definitions and pattern matching" $ do
263307
-- compileAndRun

0 commit comments

Comments
 (0)