4
4
module GrLang (initialize ) where
5
5
6
6
import Control.Monad
7
- import Control.Monad.Except (ExceptT (.. ), runExceptT )
8
- import qualified Control.Monad.Except as ExceptT
7
+ import Control.Monad.Except (ExceptT (.. ), runExceptT )
8
+ import qualified Control.Monad.Except as ExceptT
9
9
import Control.Monad.Reader
10
- import Control.Monad.Trans (lift )
10
+ import Control.Monad.Trans (lift )
11
11
import Data.Array.IO
12
12
import Data.IORef
13
- import Data.Map (Map )
14
- import qualified Data.Map as Map
13
+ import Data.Map (Map )
14
+ import qualified Data.Map as Map
15
15
import Data.Monoid
16
- import Data.Set (Set )
17
- import qualified Data.Set as Set
18
- import Data.Text (Text )
19
- import qualified Data.Text as Text
20
- import qualified Data.Text.Encoding as Text
21
- import Data.Text.Prettyprint.Doc (Pretty (.. ))
22
- import Foreign.Lua ( Lua )
23
- import qualified Foreign.Lua as Lua
16
+ import Data.Set (Set )
17
+ import qualified Data.Set as Set
18
+ import Data.Text (Text )
19
+ import qualified Data.Text as Text
20
+ import qualified Data.Text.Encoding as Text
21
+ import Data.Text.Prettyprint.Doc (Pretty (.. ))
22
+ import Foreign.Lua ( FromLuaStack , Lua , ToLuaStack )
23
+ import qualified Foreign.Lua as Lua
24
24
25
25
import Abstract.Category
26
+ import Abstract.Category.Adhesive
26
27
import Abstract.Category.Limit
27
- import Abstract.Category.Adhesive
28
- import Abstract.Rewriting.DPO
29
- import Base.Annotation (Annotated (.. ))
30
- import qualified Data.Graphs as TypeGraph
31
- import Data.TypedGraph (EdgeId , Node (.. ), NodeId )
32
- import qualified GrLang.Compiler as GrLang
33
- import GrLang.Monad (MonadGrLang )
34
- import qualified GrLang.Monad as GrLang
35
- import qualified GrLang.Parser as GrLang
28
+ import Abstract.Rewriting.DPO
29
+ import Base.Annotation (Annotated (.. ))
30
+ import qualified Data.Graphs as TypeGraph
31
+ import Data.TypedGraph (EdgeId , Node (.. ), NodeId )
32
+ import qualified GrLang.Compiler as GrLang
33
+ import GrLang.Monad (MonadGrLang )
34
+ import qualified GrLang.Monad as GrLang
35
+ import qualified GrLang.Parser as GrLang
36
36
import GrLang.Value
37
- import qualified Image.Dot.TypedGraph as Dot
37
+ import qualified Image.Dot.TypedGraph as Dot
38
38
import Util.Lua
39
39
40
40
data GrLangState = GrLangState
@@ -116,12 +116,12 @@ instance MonadGrLang (ReaderT GrLangState Lua) where
116
116
117
117
getValue (A _ name) = do
118
118
liftLua $ Lua. getglobal (Text. unpack name)
119
- hasTable <- liftLua $ Lua. istable ( - 1 )
119
+ hasTable <- liftLua $ Lua. istable Lua. stackTop
120
120
if not hasTable
121
121
then GrLang. throwError Nothing " Value is not a table"
122
122
else do
123
- liftLua $ Lua. getfield ( - 1 ) indexKey
124
- result <- liftLua $ Lua. tointegerx ( - 1 )
123
+ liftLua $ Lua. getfield Lua. stackTop indexKey
124
+ result <- liftLua $ Lua. tointegerx Lua. stackTop
125
125
liftLua $ Lua. pop 2
126
126
case result of
127
127
Nothing -> GrLang. throwError Nothing " Value has no index"
@@ -247,79 +247,80 @@ grLangNamingContext = Dot.Ctx
247
247
initGrLang :: GrLangState -> Lua ()
248
248
initGrLang globalState = do
249
249
setNative " GrLang"
250
- [ (" getNodeTypes" , Lua. pushHaskellFunction
251
- (map Text. unpack . Map. keys <$> liftIO (readIORef $ nodeTypes globalState) :: Lua [String ])
250
+ [ (" getNodeTypes" , haskellFn0 globalState $ do
251
+ types <- get nodeTypes
252
+ return . map Text. unpack $ Map. keys types
252
253
)
253
- , (" getEdgeTypes" , Lua. pushHaskellFunction . runGrLang' globalState $ do
254
- types <- Map. keys <$> get edgeTypes
255
- forM types $ \ (name, srcId, tgtId) -> do
256
- tgraph <- get typeGraph
257
- let Just srcType = TypeGraph. lookupNode srcId tgraph
258
- Just tgtType = TypeGraph. lookupNode tgtId tgraph
259
- return . Text. unpack $ formatEdgeType name (nodeName srcType) (nodeName tgtType)
254
+ , (" getEdgeTypes" , haskellFn0 globalState $ do
255
+ types <- Map. keys <$> get edgeTypes
256
+ forM types $ \ (name, srcId, tgtId) -> do
257
+ tgraph <- get typeGraph
258
+ let Just srcType = TypeGraph. lookupNode srcId tgraph
259
+ Just tgtType = TypeGraph. lookupNode tgtId tgraph
260
+ return . Text. unpack $ formatEdgeType name (nodeName srcType) (nodeName tgtType)
260
261
)
261
- , (" addNodeType" , Lua. pushHaskellFunction $ \ name -> runGrLang' globalState $
262
+ , (" addNodeType" , haskellFn1 globalState $ \ name ->
262
263
GrLang. addNodeType (A Nothing name)
263
264
)
264
- , (" addEdgeType" , Lua. pushHaskellFunction $ \ name srcName tgtName -> runGrLang' globalState $
265
+ , (" addEdgeType" , haskellFn3 globalState $ \ name srcName tgtName ->
265
266
GrLang. addEdgeType (A Nothing name) (A Nothing srcName) (A Nothing tgtName)
266
267
)
267
- , (" toString" , Lua. pushHaskellFunction $ \ idx -> runGrLang' globalState $
268
+ , (" toString" , haskellFn1 globalState $ \ idx ->
268
269
show . pretty <$> lookupGrLangValue idx
269
270
)
270
- , (" equals" , Lua. pushHaskellFunction $ \ idxA idxB -> runGrLang' globalState $ do
271
+ , (" equals" , haskellFn2 globalState $ \ idxA idxB -> do
271
272
valA <- lookupGrLangValue idxA
272
273
valB <- lookupGrLangValue idxB
273
274
return (valA == valB)
274
275
)
275
- , (" deallocate" , Lua. pushHaskellFunction $ \ idx -> runGrLang' globalState $
276
+ , (" deallocate" , haskellFn1 globalState $ \ idx ->
276
277
freeGrLang idx
277
278
)
278
- , (" toDot" , Lua. pushHaskellFunction $ \ idx name -> runGrLang' globalState $ do
279
+ , (" toDot" , haskellFn2 globalState $ \ idx name -> do
279
280
VGraph graph <- lookupGrLangValue idx
280
281
return . show $ Dot. typedGraph grLangNamingContext (pretty $ Text. decodeUtf8 name) graph
281
282
)
282
- , (" compileFile" , Lua. pushHaskellFunction $ \ path -> runGrLang' globalState $
283
+ , (" compileFile" , haskellFn1 globalState $ \ path ->
283
284
GrLang. compileFile path
284
285
)
285
286
]
286
287
287
288
setNative " Graph"
288
- [ (" parse" , Lua. pushHaskellFunction $ \ string -> runGrLang' globalState $ do
289
+ [ (" parse" , haskellFn1 globalState $ \ string -> do
289
290
graph <- GrLang. compileGraph =<< GrLang. parseGraph " <repl>" (string :: String )
290
291
allocateGrLang (VGraph graph)
291
292
),
292
- (" identity" , Lua. pushHaskellFunction $ \ idx -> runGrLang' globalState $ do
293
+ (" identity" , haskellFn1 globalState $ \ idx -> do
293
294
VGraph graph <- lookupGrLangValue idx
294
295
allocateGrLang (VMorph $ identity graph)
295
296
)
296
297
]
297
298
298
299
setNative " Morphism"
299
- [ (" parse" , Lua. pushHaskellFunction $ \ domIdx codIdx string -> runGrLang' globalState $ do
300
+ [ (" parse" , haskellFn3 globalState $ \ domIdx codIdx string -> do
300
301
VGraph dom <- lookupGrLangValue domIdx
301
302
VGraph cod <- lookupGrLangValue codIdx
302
303
morphism <- GrLang. compileMorphism Nothing dom cod =<< GrLang. parseMorphism " <repl>" (string :: String )
303
304
allocateGrLang (VMorph morphism)
304
305
),
305
- (" compose" , Lua. pushHaskellFunction $ \ idF idG -> runGrLang' globalState $ do
306
+ (" compose" , haskellFn2 globalState $ \ idF idG -> do
306
307
VMorph f <- lookupGrLangValue idF
307
308
VMorph g <- lookupGrLangValue idG
308
309
allocateGrLang (VMorph $ f <&> g)
309
310
),
310
- (" isMonic" , Lua. pushHaskellFunction $ \ idx -> runGrLang' globalState $ do
311
+ (" isMonic" , haskellFn1 globalState $ \ idx -> do
311
312
VMorph f <- lookupGrLangValue idx
312
313
return (isMonic f)
313
314
),
314
- (" isEpic" , Lua. pushHaskellFunction $ \ idx -> runGrLang' globalState $ do
315
+ (" isEpic" , haskellFn1 globalState $ \ idx -> do
315
316
VMorph f <- lookupGrLangValue idx
316
317
return (isEpic f)
317
318
),
318
- (" isIsomorphism" , Lua. pushHaskellFunction $ \ idx -> runGrLang' globalState $ do
319
+ (" isIsomorphism" , haskellFn1 globalState $ \ idx -> do
319
320
VMorph f <- lookupGrLangValue idx
320
321
return (isIsomorphism f)
321
322
),
322
- (" calculatePullback" , Lua. pushHaskellFunction $ \ idF idG -> runGrLang' globalState $ do
323
+ (" calculatePullback" , haskellFn2 globalState $ \ idF idG -> do
323
324
VMorph f <- lookupGrLangValue idF
324
325
VMorph g <- lookupGrLangValue idG
325
326
let (f', g') = calculatePullback f g
@@ -328,7 +329,7 @@ initGrLang globalState = do
328
329
<*> allocateGrLang (VMorph f')
329
330
<*> allocateGrLang (VMorph g')
330
331
),
331
- (" calculateInitialPushout" , Lua. pushHaskellFunction $ \ idx -> runGrLang' globalState $ do
332
+ (" calculateInitialPushout" , haskellFn1 globalState $ \ idx -> do
332
333
VMorph f <- lookupGrLangValue idx
333
334
let (b, f', c) = calculateMInitialPushout f
334
335
(,,,,)
@@ -338,15 +339,15 @@ initGrLang globalState = do
338
339
<*> allocateGrLang (VMorph f')
339
340
<*> allocateGrLang (VMorph c)
340
341
),
341
- (" subobjectIntersection" , Lua. pushHaskellFunction $ \ idA idB -> runGrLang' globalState $ do
342
+ (" subobjectIntersection" , haskellFn2 globalState $ \ idA idB -> do
342
343
VMorph a <- lookupGrLangValue idA
343
344
VMorph b <- lookupGrLangValue idB
344
345
let c = subobjectIntersection a b
345
346
(,)
346
347
<$> (allocateGrLang . VGraph $ domain c)
347
348
<*> (allocateGrLang . VMorph $ c)
348
349
),
349
- (" subobjectUnion" , Lua. pushHaskellFunction $ \ idA idB -> runGrLang' globalState $ do
350
+ (" subobjectUnion" , haskellFn2 globalState $ \ idA idB -> do
350
351
VMorph a <- lookupGrLangValue idA
351
352
VMorph b <- lookupGrLangValue idB
352
353
let c = subobjectUnion a b
@@ -357,27 +358,27 @@ initGrLang globalState = do
357
358
]
358
359
359
360
setNative " Rule"
360
- [ (" parse" , Lua. pushHaskellFunction $ \ string -> runGrLang' globalState $ do
361
+ [ (" parse" , haskellFn1 globalState $ \ string -> do
361
362
rule <- GrLang. compileRule =<< GrLang. parseRule " <repl>" (string :: String )
362
363
allocateGrLang (VRule rule)
363
364
)
364
- , (" getLeftObject" , Lua. pushHaskellFunction $ \ idRule -> runGrLang' globalState $ do
365
+ , (" getLeftObject" , haskellFn1 globalState $ \ idRule -> do
365
366
VRule rule <- lookupGrLangValue idRule
366
367
allocateGrLang (VGraph $ leftObject rule)
367
368
)
368
- , (" getRightObject" , Lua. pushHaskellFunction $ \ idRule -> runGrLang' globalState $ do
369
+ , (" getRightObject" , haskellFn1 globalState $ \ idRule -> do
369
370
VRule rule <- lookupGrLangValue idRule
370
371
allocateGrLang (VGraph $ rightObject rule)
371
372
)
372
- , (" getInterface" , Lua. pushHaskellFunction $ \ idRule -> runGrLang' globalState $ do
373
+ , (" getInterface" , haskellFn1 globalState $ \ idRule -> do
373
374
VRule rule <- lookupGrLangValue idRule
374
375
allocateGrLang (VGraph $ interfaceObject rule)
375
376
)
376
- , (" getLeftMorphism" , Lua. pushHaskellFunction $ \ idRule -> runGrLang' globalState $ do
377
+ , (" getLeftMorphism" , haskellFn1 globalState $ \ idRule -> do
377
378
VRule rule <- lookupGrLangValue idRule
378
379
allocateGrLang (VMorph $ leftMorphism rule)
379
380
)
380
- , (" getRightMorphism" , Lua. pushHaskellFunction $ \ idRule -> runGrLang' globalState $ do
381
+ , (" getRightMorphism" , haskellFn1 globalState $ \ idRule -> do
381
382
VRule rule <- lookupGrLangValue idRule
382
383
allocateGrLang (VMorph $ rightMorphism rule)
383
384
)
@@ -391,6 +392,24 @@ initGrLang globalState = do
391
392
Lua. setfield tableIdx " native"
392
393
Lua. pop 1
393
394
395
+ haskellFn0 :: ToLuaStack a => GrLangState -> ExceptT GrLang. Error LuaGrLang a -> Lua ()
396
+ haskellFn0 globalState f = pushFunction (runGrLang' globalState f)
397
+
398
+ haskellFn1 ::
399
+ (FromLuaStack a , ToLuaStack b ) =>
400
+ GrLangState -> (a -> ExceptT GrLang. Error LuaGrLang b ) -> Lua ()
401
+ haskellFn1 globalState f = pushFunction (runGrLang' globalState . f)
402
+
403
+ haskellFn2 ::
404
+ (FromLuaStack a , FromLuaStack b , ToLuaStack c ) =>
405
+ GrLangState -> (a -> b -> ExceptT GrLang. Error LuaGrLang c ) -> Lua ()
406
+ haskellFn2 globalState f = pushFunction (\ x y -> runGrLang' globalState (f x y))
407
+
408
+ haskellFn3 ::
409
+ (FromLuaStack a , FromLuaStack b , FromLuaStack c , ToLuaStack d ) =>
410
+ GrLangState -> (a -> b -> c -> ExceptT GrLang. Error LuaGrLang d ) -> Lua ()
411
+ haskellFn3 globalState f = pushFunction (\ x y z -> runGrLang' globalState (f x y z))
412
+
394
413
createTable :: Foldable t => t (String , Lua () ) -> Lua ()
395
414
createTable contents = do
396
415
Lua. createtable 0 (length contents)
0 commit comments