summaryrefslogtreecommitdiff
path: root/fig-utils/src/Fig/Utils/FFI.hs
diff options
context:
space:
mode:
authorLLLL Colonq <llll@colonq>2025-06-16 03:33:52 -0400
committerLLLL Colonq <llll@colonq>2025-06-16 03:33:52 -0400
commit7c3e41979478d6826f73a956a26c967aae1687a2 (patch)
tree093f9e418f95046fb8eccc3ed0f4c3bdbe1417bf /fig-utils/src/Fig/Utils/FFI.hs
parent0f8a0bf2c0dce27cb832896731e2047e07310ebc (diff)
fig-utils: Guile FFI
Diffstat (limited to 'fig-utils/src/Fig/Utils/FFI.hs')
-rw-r--r--fig-utils/src/Fig/Utils/FFI.hs23
1 files changed, 23 insertions, 0 deletions
diff --git a/fig-utils/src/Fig/Utils/FFI.hs b/fig-utils/src/Fig/Utils/FFI.hs
new file mode 100644
index 0000000..b5ee80e
--- /dev/null
+++ b/fig-utils/src/Fig/Utils/FFI.hs
@@ -0,0 +1,23 @@
+module Fig.Utils.FFI where
+
+import Fig.Prelude
+
+import Foreign.Ptr (Ptr, nullPtr)
+import Foreign.Storable (Storable(..))
+import Foreign.C.String
+import Foreign.Marshal.Alloc
+
+foreign import ccall "check_answer" c_check_answer :: Ptr CString -> CString -> CString -> IO Int
+
+checkAnswer :: Text -> Text -> IO (Either Text Bool)
+checkAnswer tcode tanswer =
+ withCString (unpack tcode) $ \code ->
+ withCString (unpack tanswer) $ \answer ->
+ alloca $ \rerr -> do
+ res <- c_check_answer rerr code answer
+ err <- peek rerr
+ if err == nullPtr
+ then pure . Right $ res /= 0
+ else do
+ msg <- peekCString err
+ pure . Left $ pack msg