Skip to content

Commit

Permalink
fix undefined handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bristermitten committed Dec 7, 2023
1 parent 34bbcb0 commit 85a0df4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
28 changes: 21 additions & 7 deletions src/Elara/Emit/Expr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Control.Lens
import Data.Traversable (for)
import Elara.AST.Name
import Elara.AST.VarRef
import Elara.Core
import Elara.Core as Core
import Elara.Data.Unique
import Elara.Emit.Lambda
import Elara.Emit.Operator
Expand Down Expand Up @@ -92,11 +92,20 @@ generateInstructions' p (Var (Normal (Id (Global' qn@(Qualified n mn)) t))) tApp
(translateOperatorName n)
(generateFieldType t)
)
else do
-- it's a method, so we have to create a Func currying it
cName <- gets (.thisClassName)
inst <- etaExpand qn t cName
emit inst
else case t of
FuncTy{} -> do
-- it's a method function, so we have to create a Func currying it
cName <- gets (.thisClassName)
inst <- etaExpand qn t cName
emit inst
_ -> do
-- no args function (eg undefined)
emit
( InvokeStatic
(ClassInfoType $ createModuleName mn)
(translateOperatorName n)
(generateMethodDescriptor t)
)

case tApps of
[] -> pass
Expand Down Expand Up @@ -126,7 +135,7 @@ generateCaseInstructions ::
(Member (State MethodCreationState) r, Member CodeBuilder r, Member ClassBuilder r, Member UniqueGen r) =>
Expr JVMBinder ->
Maybe JVMBinder ->
[Elara.Core.Alt JVMBinder] ->
[Core.Alt JVMBinder] ->
Sem r ()
generateCaseInstructions -- hardcode if/else impl
scrutinee
Expand Down Expand Up @@ -262,6 +271,11 @@ generateLitInstructions (Int i) =
[ LDC (LDCInt (fromIntegral i))
, InvokeStatic (ClassInfoType "java.lang.Integer") "valueOf" (MethodDescriptor [PrimitiveFieldType JVM.Int] (TypeReturn (ObjectFieldType "java.lang.Integer")))
]
generateLitInstructions (Core.Char c) =
pure
[ LDC (LDCInt (fromEnum c))
, InvokeStatic (ClassInfoType "java.lang.Character") "valueOf" (MethodDescriptor [PrimitiveFieldType JVM.Char] (TypeReturn (ObjectFieldType "java.lang.Character")))
]
generateLitInstructions other = error $ "Not implemented: " <> showPretty other

generatePrimInstructions :: Monad m => Text -> m [Instruction]
Expand Down
2 changes: 1 addition & 1 deletion src/Elara/Emit/Lambda.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ etaExpand funcName funcType@(FuncTy i o) thisClassName = do
(Var $ Normal $ Id (Global' funcName) funcType) -- f
(Var $ JVMLocal 0) -- x
)
etaExpand _ _ _ = error "etaExpand called on non-function type"
etaExpand n t c = error $ "etaExpand called on non-function type: " <> show (n, t, c)

{- | Creates the bytecode for a lambda expression
This involves a few steps:
Expand Down

0 comments on commit 85a0df4

Please sign in to comment.