From 045eeeb2804240f34a490af0ceef351908ade7bc Mon Sep 17 00:00:00 2001 From: hallgren Date: Fri, 17 Jan 2014 14:02:35 +0000 Subject: [PATCH] haskell-bind: add a cabal file and examples/pgf-shell.hs * The haskell-bind.cabal file makes it easy to build the haskell binding and use it in ghci. * pgf-shell.hs is a simple example of how to use the haskell binding. --- .../haskell-bind/examples/pgf-shell.hs | 62 +++++++++++++++++++ src/runtime/haskell-bind/haskell-bind.cabal | 32 ++++++++++ 2 files changed, 94 insertions(+) create mode 100644 src/runtime/haskell-bind/examples/pgf-shell.hs create mode 100644 src/runtime/haskell-bind/haskell-bind.cabal diff --git a/src/runtime/haskell-bind/examples/pgf-shell.hs b/src/runtime/haskell-bind/examples/pgf-shell.hs new file mode 100644 index 000000000..04b6522da --- /dev/null +++ b/src/runtime/haskell-bind/examples/pgf-shell.hs @@ -0,0 +1,62 @@ +-- | pgf-shell: A simple shell to illustrate the use of the Haskell binding +-- to the C implementation of the PGF run-time system. +-- +-- The shell has 3 commands: +-- +-- * parse: p +-- * linearize: l +-- * translate: t + +import Control.Monad(forever) +import Data.Char(isSpace) +import System.IO(hFlush,stdout) +import System.IO.Error(catchIOError) +import System.Environment +import CRuntimeFFI +import CId + +main = getPGF =<< getArgs + +getPGF [path] = pgfShell =<< readPGF path +getPGF _ = putStrLn "Usage: pgf-shell " + +pgfShell pgf = + forever $ do putStr "> "; hFlush stdout + execute pgf =<< readLn + +execute pgf cmd = + case cmd of + L lang tree -> do c <- getConcr' pgf lang + putStrLn $ linearize c tree + P lang s -> do c <- getConcr' pgf lang + printl $ parse c (startCat pgf) s + T from to s -> do cfrom <- getConcr' pgf from + cto <- getConcr' pgf to + putl [linearize cto t|(t,_)<-parse cfrom (startCat pgf) s] + _ -> putStrLn "Huh?" + `catchIOError` print + +getConcr' pgf lang = + maybe (fail $ "Concrete syntax not found: "++show lang) return $ + getConcr pgf lang + +printl xs = putl $ map show xs +putl = putStr . unlines + +-- | Abstracy syntax of shell commands +data Command = P CId String | L CId Tree | T CId CId String deriving Show + +-- | Shell command parser +instance Read Command where + readsPrec _ s = + [(P l r2,"") | ("p",r1)<-lex s, + (l,r2) <- reads' r1] + ++ [(L l t,"") | ("l",r1)<-lex s, + (l,r2)<- reads' r1, + Just t<-[readExpr r2]] + ++ [(T l1 l2 r3,"") | ("t",r1)<-lex s, + (l1,r2)<-reads' r1, + (l2,r3)<-reads' r2] + +-- | Workaround for deficiency in instance Read CId +reads' s = reads (dropWhile isSpace s) diff --git a/src/runtime/haskell-bind/haskell-bind.cabal b/src/runtime/haskell-bind/haskell-bind.cabal new file mode 100644 index 000000000..ea4f41cf6 --- /dev/null +++ b/src/runtime/haskell-bind/haskell-bind.cabal @@ -0,0 +1,32 @@ +-- Initial haskell-bind.cabal generated by cabal init. For further +-- documentation, see http://haskell.org/cabal/users-guide/ + +name: haskell-bind +version: 0.1.0.0 +-- synopsis: +-- description: +homepage: http://www.grammaticalframework.org +license: LGPL-3 +license-file: LICENSE +author: Inari +-- maintainer: +-- copyright: +category: Language +build-type: Simple +extra-source-files: README +cabal-version: >=1.10 + +library + exposed-modules: CId, CRuntimeFFI + other-modules: Gu, PgfLow + build-depends: base >=4.6 && <4.7, bytestring >=0.10 && <0.11, pretty >=1.1 && <1.2 + -- hs-source-dirs: + build-tools: hsc2hs + extra-libraries: gu pgf + default-language: Haskell2010 + +executable pgf-shell + main-is: pgf-shell.hs + hs-source-dirs: examples + build-depends: base, haskell-bind + default-language: Haskell2010