summaryrefslogtreecommitdiff
path: root/src/compiler/GF/Command/Abstract.hs
diff options
context:
space:
mode:
authorkrasimir <krasimir@chalmers.se>2009-12-13 18:50:29 +0000
committerkrasimir <krasimir@chalmers.se>2009-12-13 18:50:29 +0000
commitf85232947e74ee7ef8c7b0ad2338212e7e68f1be (patch)
tree667b886a5e3a4b026a63d4e3597f32497d824761 /src/compiler/GF/Command/Abstract.hs
parentd88a865faff59c98fc91556ff8700b10ee5f2df8 (diff)
reorganize the directories under src, and rescue the JavaScript interpreter from deprecated
Diffstat (limited to 'src/compiler/GF/Command/Abstract.hs')
-rw-r--r--src/compiler/GF/Command/Abstract.hs79
1 files changed, 79 insertions, 0 deletions
diff --git a/src/compiler/GF/Command/Abstract.hs b/src/compiler/GF/Command/Abstract.hs
new file mode 100644
index 000000000..1f7c4014e
--- /dev/null
+++ b/src/compiler/GF/Command/Abstract.hs
@@ -0,0 +1,79 @@
+module GF.Command.Abstract where
+
+import PGF.CId
+import PGF.Data
+
+type Ident = String
+
+type CommandLine = [Pipe]
+
+type Pipe = [Command]
+
+data Command
+ = Command Ident [Option] Argument
+ deriving (Eq,Ord,Show)
+
+data Option
+ = OOpt Ident
+ | OFlag Ident Value
+ deriving (Eq,Ord,Show)
+
+data Value
+ = VId Ident
+ | VInt Int
+ | VStr String
+ deriving (Eq,Ord,Show)
+
+data Argument
+ = AExpr Expr
+ | ANoArg
+ | AMacro Ident
+ deriving (Eq,Ord,Show)
+
+valCIdOpts :: String -> CId -> [Option] -> CId
+valCIdOpts flag def opts =
+ case [v | OFlag f (VId v) <- opts, f == flag] of
+ (v:_) -> mkCId v
+ _ -> def
+
+valIntOpts :: String -> Int -> [Option] -> Int
+valIntOpts flag def opts =
+ case [v | OFlag f (VInt v) <- opts, f == flag] of
+ (v:_) -> v
+ _ -> def
+
+valStrOpts :: String -> String -> [Option] -> String
+valStrOpts flag def opts =
+ case [v | OFlag f v <- opts, f == flag] of
+ (VStr v:_) -> v
+ (VId v:_) -> v
+ (VInt v:_) -> show v
+ _ -> def
+
+isOpt :: String -> [Option] -> Bool
+isOpt o opts = elem o [x | OOpt x <- opts]
+
+isFlag :: String -> [Option] -> Bool
+isFlag o opts = elem o [x | OFlag x _ <- opts]
+
+optsAndFlags :: [Option] -> ([Option],[Option])
+optsAndFlags = foldr add ([],[]) where
+ add o (os,fs) = case o of
+ OOpt _ -> (o:os,fs)
+ OFlag _ _ -> (os,o:fs)
+
+prOpt :: Option -> String
+prOpt o = case o of
+ OOpt i -> i
+ OFlag f x -> f ++ "=" ++ show x
+
+mkOpt :: String -> Option
+mkOpt = OOpt
+
+-- abbreviation convention from gf commands
+getCommandOp s = case break (=='_') s of
+ (a:_,_:b:_) -> [a,b] -- axx_byy --> ab
+ _ -> case s of
+ [a,b] -> s -- ab --> ab
+ a:_ -> [a] -- axx --> a
+