+buildExpr (Var n) = do
+ binds <- ask
+ case binds Map.!? n of
+ Just x -> pure x
+ Nothing -> error $ "'" <> n <> "' doesn't exist in scope"
+
+buildExpr (BinOp op a b) = do
+ opA <- buildExpr a
+ opB <- buildExpr b
+ tmp <- instr opA opB
+ if isCmp
+ then uitofp tmp Type.double
+ else return tmp
+ where isCmp
+ | Cmp _ <- op = True
+ | otherwise = False
+ instr = case op of
+ K.Add -> fadd
+ K.Sub -> fsub
+ K.Mul -> fmul
+ K.Cmp LT -> fcmp OLT
+ K.Cmp GT -> fcmp OGT
+ K.Cmp EQ -> fcmp OEQ
+
+buildExpr (Call callee params) = do
+ paramOps <- mapM buildExpr params
+ let nam = fromString callee
+ -- get a pointer to the function
+ typ = FunctionType Type.double (replicate (length params) Type.double) False
+ ptrTyp = Type.PointerType typ (AddrSpace 0)
+ ref = GlobalReference ptrTyp nam
+ call (ConstantOperand ref) (zip paramOps (repeat []))