use ByteString internally in Ident, CId and Label

This commit is contained in:
kr.angelov
2008-05-21 13:10:54 +00:00
parent e8bbd458cb
commit 314f5cc5e7
65 changed files with 6275 additions and 6432 deletions

View File

@@ -46,7 +46,7 @@ cf2rule :: CFRule -> ((Ident,Info),(Ident,Info))
cf2rule (fun, (cat, items)) = (def,ldef) where cf2rule (fun, (cat, items)) = (def,ldef) where
f = cfFun2Ident fun f = cfFun2Ident fun
def = (f, AbsFun (yes (mkProd (args', Cn (cfCat2Ident cat), []))) nope) def = (f, AbsFun (yes (mkProd (args', Cn (cfCat2Ident cat), []))) nope)
args0 = zip (map (mkIdent "x") [0..]) items args0 = zip (map (identV "x") [0..]) items
args = [(v, Cn (cfCat2Ident c)) | (v, CFNonterm c) <- args0] args = [(v, Cn (cfCat2Ident c)) | (v, CFNonterm c) <- args0]
args' = [(zIdent "_", Cn (cfCat2Ident c)) | (_, CFNonterm c) <- args0] args' = [(zIdent "_", Cn (cfCat2Ident c)) | (_, CFNonterm c) <- args0]
ldef = (f, CncFun ldef = (f, CncFun

View File

@@ -39,6 +39,7 @@ import GF.Data.Operations
import qualified GF.Infra.Modules as M import qualified GF.Infra.Modules as M
import Data.Char import Data.Char
import qualified Data.ByteString.Char8 as BS
import Control.Arrow (first) import Control.Arrow (first)
type Context = [(Ident,Exp)] type Context = [(Ident,Exp)]
@@ -73,7 +74,7 @@ mapInfoTerms f i = case i of
_ -> i _ -> i
setFlag :: String -> String -> [Flag] -> [Flag] setFlag :: String -> String -> [Flag] -> [Flag]
setFlag n v fs = flagCanon n v : [f | f@(Flg (IC n') _) <- fs, n' /= n] setFlag n v fs = flagCanon n v : [f | f@(Flg (IC n') _) <- fs, n' /= BS.pack n]
flagIncomplete :: Flag flagIncomplete :: Flag
flagIncomplete = flagCanon "incomplete" "true" flagIncomplete = flagCanon "incomplete" "true"
@@ -86,7 +87,7 @@ hasFlagCanon f (_,M.ModMod mo) = elem f $ M.flags mo
hasFlagCanon f _ = True ---- safe, useless hasFlagCanon f _ = True ---- safe, useless
flagCanon :: String -> String -> Flag flagCanon :: String -> String -> Flag
flagCanon f v = Flg (identC f) (identC v) flagCanon f v = Flg (identC (BS.pack f)) (identC (BS.pack v))
-- for Ha-Jo 20/2/2005 -- for Ha-Jo 20/2/2005

View File

@@ -12,6 +12,7 @@ module GF.Command.Commands (
import GF.Command.AbsGFShell hiding (Tree) import GF.Command.AbsGFShell hiding (Tree)
import GF.Command.PPrTree import GF.Command.PPrTree
import GF.Command.ParGFShell import GF.Command.ParGFShell
import GF.GFCC.CId
import GF.GFCC.ShowLinearize import GF.GFCC.ShowLinearize
import GF.GFCC.API import GF.GFCC.API
import GF.GFCC.Macros import GF.GFCC.Macros
@@ -131,10 +132,10 @@ allCommands mgr = Map.fromAscList [
optLin opts t = unlines [linea lang t | lang <- optLangs opts] where optLin opts t = unlines [linea lang t | lang <- optLangs opts] where
linea lang = case opts of linea lang = case opts of
_ | isOpt "all" opts -> allLinearize gr (cid lang) _ | isOpt "all" opts -> allLinearize gr (mkCId lang)
_ | isOpt "table" opts -> tableLinearize gr (cid lang) _ | isOpt "table" opts -> tableLinearize gr (mkCId lang)
_ | isOpt "term" opts -> termLinearize gr (cid lang) _ | isOpt "term" opts -> termLinearize gr (mkCId lang)
_ | isOpt "record" opts -> recordLinearize gr (cid lang) _ | isOpt "record" opts -> recordLinearize gr (mkCId lang)
_ -> linearize mgr lang _ -> linearize mgr lang

View File

@@ -21,7 +21,7 @@ tree2exp t = case t of
TStr s -> tree (AS s) [] TStr s -> tree (AS s) []
TFloat d -> tree (AF d) [] TFloat d -> tree (AF d) []
where where
i2i (Ident s) = CId s i2i (Ident s) = mkCId s
prExp :: Exp -> String prExp :: Exp -> String
prExp = printTree . exp2tree prExp = printTree . exp2tree
@@ -36,4 +36,4 @@ exp2tree (DTr xs at ts) = tabs (map i4i xs) (tapp at (map exp2tree ts))
tapp (AS i) [] = TStr i tapp (AS i) [] = TStr i
tapp (AF i) [] = TFloat i tapp (AF i) [] = TFloat i
tapp (AM i) [] = TId (Ident "?") ---- tapp (AM i) [] = TId (Ident "?") ----
i4i (CId s) = Ident s i4i s = Ident (prCId s)

View File

@@ -24,6 +24,7 @@ import GF.Grammar.PrGrammar (prt)
import GF.Data.Operations import GF.Data.Operations
import Data.List import Data.List
import qualified GF.Infra.Modules as M import qualified GF.Infra.Modules as M
import qualified Data.ByteString.Char8 as BS
type OptSpec = [Integer] --- type OptSpec = [Integer] ---
@@ -110,7 +111,7 @@ factor c i t = case t of
--- we hope this will be fresh and don't check... in GFC would be safe --- we hope this will be fresh and don't check... in GFC would be safe
qqIdent c i = identC ("q_" ++ prt c ++ "__" ++ show i) qqIdent c i = identC (BS.pack ("q_" ++ prt c ++ "__" ++ show i))
-- we need to replace subterms -- we need to replace subterms

View File

@@ -42,7 +42,7 @@ mkRules conf f t = (fun f ty, lin f (takeWhile (/=':') t)) where
args = mkArgs conf ts args = mkArgs conf ts
ty = concat [a ++ " -> " | a <- map snd args] ++ val ty = concat [a ++ " -> " | a <- map snd args] ++ val
(ts,val) = let tt = lexTerm t in (init tt,last tt) (ts,val) = let tt = lexTerm t in (init tt,last tt)
--- f = mkIdent t --- f = identV t
fun c a = unwords [" fun", c, ":",a,";"] fun c a = unwords [" fun", c, ":",a,";"]
lin c a = unwords $ [" lin", c] ++ map fst args ++ ["=",a,";"] lin c a = unwords $ [" lin", c] ++ map fst args ++ ["=",a,";"]

View File

@@ -130,9 +130,9 @@ evalCncInfo opts gr cnc abs (c,info) = do
CncCat ptyp pde ppr -> do CncCat ptyp pde ppr -> do
pde' <- case (ptyp,pde) of pde' <- case (ptyp,pde) of
(Yes typ, Yes de) -> (Yes typ, Yes de) ->
liftM yes $ pEval ([(strVar, typeStr)], typ) de liftM yes $ pEval ([(varStr, typeStr)], typ) de
(Yes typ, Nope) -> (Yes typ, Nope) ->
liftM yes $ mkLinDefault gr typ >>= partEval noOptions gr ([(strVar, typeStr)],typ) liftM yes $ mkLinDefault gr typ >>= partEval noOptions gr ([(varStr, typeStr)],typ)
(May b, Nope) -> (May b, Nope) ->
return $ May b return $ May b
_ -> return pde -- indirection _ -> return pde -- indirection
@@ -248,7 +248,7 @@ recordExpand typ trm = case unComputed typ of
mkLinDefault :: SourceGrammar -> Type -> Err Term mkLinDefault :: SourceGrammar -> Type -> Err Term
mkLinDefault gr typ = do mkLinDefault gr typ = do
case unComputed typ of case unComputed typ of
RecType lts -> mapPairsM mkDefField lts >>= (return . Abs strVar . R . mkAssign) RecType lts -> mapPairsM mkDefField lts >>= (return . Abs varStr . R . mkAssign)
_ -> prtBad "linearization type must be a record type, not" typ _ -> prtBad "linearization type must be a record type, not" typ
where where
mkDefField typ = case unComputed typ of mkDefField typ = case unComputed typ of
@@ -256,7 +256,7 @@ mkLinDefault gr typ = do
t' <- mkDefField t t' <- mkDefField t
let T _ cs = mkWildCases t' let T _ cs = mkWildCases t'
return $ T (TWild p) cs return $ T (TWild p) cs
Sort "Str" -> return $ Vr strVar Sort "Str" -> return $ Vr varStr
QC q p -> lookupFirstTag gr q p QC q p -> lookupFirstTag gr q p
RecType r -> do RecType r -> do
let (ls,ts) = unzip r let (ls,ts) = unzip r

View File

@@ -23,6 +23,7 @@ import GF.Infra.Ident
import GF.Infra.Modules import GF.Infra.Modules
import GF.Grammar.Macros import GF.Grammar.Macros
import GF.Grammar.Lookup import GF.Grammar.Lookup
import GF.Grammar.Predef
import GF.Data.Operations import GF.Data.Operations
@@ -60,4 +61,4 @@ remlTerm gr trm = case trm of
look c = err (const $ return defLinType) return $ lookupLincat gr m c look c = err (const $ return defLinType) return $ lookupLincat gr m c
m = case [cnc | (cnc,ModMod m) <- modules gr, isModCnc m] of m = case [cnc | (cnc,ModMod m) <- modules gr, isModCnc m] of
cnc:_ -> cnc -- actually there is always exactly one cnc:_ -> cnc -- actually there is always exactly one
_ -> zIdent "CNC" _ -> cCNC

View File

@@ -29,6 +29,7 @@ module GF.Compile.Rename (renameGrammar,
import GF.Grammar.Grammar import GF.Grammar.Grammar
import GF.Grammar.Values import GF.Grammar.Values
import GF.Grammar.Predef
import GF.Infra.Modules import GF.Infra.Modules
import GF.Infra.Ident import GF.Infra.Ident
import GF.Grammar.Macros import GF.Grammar.Macros
@@ -90,11 +91,9 @@ renameIdentTerm env@(act,imps) t =
[(m, st) | (OSimple _ m, st) <- imps] -- qualif is always possible [(m, st) | (OSimple _ m, st) <- imps] -- qualif is always possible
-- this facility is mainly for BWC with GF1: you need not import PredefAbs -- this facility is mainly for BWC with GF1: you need not import PredefAbs
predefAbs c s = case c of predefAbs c s
IC "Int" -> return $ Q cPredefAbs cInt | isPredefCat c = return $ Q cPredefAbs c
IC "Float" -> return $ Q cPredefAbs cFloat | otherwise = Bad s
IC "String" -> return $ Q cPredefAbs cString
_ -> Bad s
ident alt c = case lookupTree prt c act of ident alt c = case lookupTree prt c act of
Ok f -> return $ f c Ok f -> return $ f c
@@ -104,7 +103,6 @@ renameIdentTerm env@(act,imps) t =
fs -> case nub [f c | f <- fs] of fs -> case nub [f c | f <- fs] of
[tr] -> return tr [tr] -> return tr
ts@(t:_) -> trace ("WARNING: conflict" +++ unwords (map prt ts)) (return t) ts@(t:_) -> trace ("WARNING: conflict" +++ unwords (map prt ts)) (return t)
---- ts -> return $ Strs $ (cnIC "#conflict") : reverse ts
-- a warning will be generated in CheckGrammar, and the head returned -- a warning will be generated in CheckGrammar, and the head returned
-- in next V: -- in next V:
-- Bad $ "conflicting imports:" +++ unwords (map prt ts) -- Bad $ "conflicting imports:" +++ unwords (map prt ts)

View File

@@ -33,6 +33,7 @@ import GF.Data.Utilities (updateNthM, sortNub)
import qualified Data.Map as Map import qualified Data.Map as Map
import qualified Data.Set as Set import qualified Data.Set as Set
import qualified Data.List as List import qualified Data.List as List
import qualified Data.ByteString.Char8 as BS
import Data.Array import Data.Array
import Data.Maybe import Data.Maybe
@@ -81,24 +82,24 @@ expandHOAS funs lins lincats = (funs' ++ hoFuns ++ varFuns,
modifyRec f (R xs) = R (f xs) modifyRec f (R xs) = R (f xs)
modifyRec _ t = error $ "Not a record: " ++ show t modifyRec _ t = error $ "Not a record: " ++ show t
varCat = CId "_Var" varCat = mkCId "_Var"
catName :: (Int,CId) -> CId catName :: (Int,CId) -> CId
catName (0,c) = c catName (0,c) = c
catName (n,CId c) = CId ("_" ++ show n ++ c) catName (n,c) = mkCId ("_" ++ show n ++ prt c)
funName :: (Int,CId) -> CId funName :: (Int,CId) -> CId
funName (n,CId c) = CId ("__" ++ show n ++ c) funName (n,c) = mkCId ("__" ++ show n ++ prt c)
varFunName :: CId -> CId varFunName :: CId -> CId
varFunName (CId c) = CId ("_Var_" ++ c) varFunName c = mkCId ("_Var_" ++ prt c)
-- replaces __NCat with _B and _Var_Cat with _. -- replaces __NCat with _B and _Var_Cat with _.
-- the temporary names are just there to avoid name collisions. -- the temporary names are just there to avoid name collisions.
fixHoasFuns :: FGrammar -> FGrammar fixHoasFuns :: FGrammar -> FGrammar
fixHoasFuns (rs, cs) = ([FRule (fixName n) args cat lins | FRule n args cat lins <- rs], cs) fixHoasFuns (rs, cs) = ([FRule (fixName n) args cat lins | FRule n args cat lins <- rs], cs)
where fixName (Name (CId ('_':'_':_)) p) = Name (CId "_B") p where fixName (Name (CId n) p) | BS.pack "__" `BS.isPrefixOf` n = Name (mkCId "_B") p
fixName (Name (CId n) p) | "_Var_" `List.isPrefixOf` n = Name wildCId p | BS.pack "_Var_" `BS.isPrefixOf` n = Name wildCId p
fixName n = n fixName n = n
convert :: [(CId,(Type,Exp))] -> TermMap -> TermMap -> FGrammar convert :: [(CId,(Type,Exp))] -> TermMap -> TermMap -> FGrammar
@@ -291,10 +292,10 @@ data ProtoFCat = PFCat CId [FPath] [(FPath,FIndex)]
protoFCat :: CId -> ProtoFCat protoFCat :: CId -> ProtoFCat
protoFCat cat = PFCat cat [] [] protoFCat cat = PFCat cat [] []
emptyFRulesEnv = FRulesEnv 0 (ins fcatString (CId "String") [[0]] [] $ emptyFRulesEnv = FRulesEnv 0 (ins fcatString (mkCId "String") [[0]] [] $
ins fcatInt (CId "Int") [[0]] [] $ ins fcatInt (mkCId "Int") [[0]] [] $
ins fcatFloat (CId "Float") [[0]] [] $ ins fcatFloat (mkCId "Float") [[0]] [] $
ins fcatVar (CId "_Var") [[0]] [] $ ins fcatVar (mkCId "_Var") [[0]] [] $
Map.empty) [] Map.empty) []
where where
ins fcat cat rcs tcs fcatSet = ins fcat cat rcs tcs fcatSet =
@@ -340,7 +341,7 @@ genFCatArg cnc_defs ctype env@(FRulesEnv last_id fcatSet rules) (PFCat cat rcs t
(either_fcat,last_id1,tmap1,rules1) (either_fcat,last_id1,tmap1,rules1)
= foldBM (\tcs st (either_fcat,last_id,tmap,rules) -> = foldBM (\tcs st (either_fcat,last_id,tmap,rules) ->
let (last_id1,tmap1,fcat_arg) = addArg tcs last_id tmap let (last_id1,tmap1,fcat_arg) = addArg tcs last_id tmap
rule = FRule (Name (CId "_") [Unify [0]]) [fcat_arg] fcat rule = FRule (Name wildCId [Unify [0]]) [fcat_arg] fcat
(listArray (0,length rcs-1) [listArray (0,0) [FSymCat fcat_arg lbl 0] | lbl <- [0..length rcs-1]]) (listArray (0,length rcs-1) [listArray (0,0) [FSymCat fcat_arg lbl 0] | lbl <- [0..length rcs-1]])
in if st in if st
then (Right fcat, last_id1,tmap1,rule:rules) then (Right fcat, last_id1,tmap1,rule:rules)

View File

@@ -89,7 +89,7 @@ sameECat :: ECat -> ECat -> Bool
sameECat ec1 ec2 = ecat2scat ec1 == ecat2scat ec2 sameECat ec1 ec2 = ecat2scat ec1 == ecat2scat ec2
coercionName :: Name coercionName :: Name
coercionName = Name Ident.wildIdent [Unify [0]] coercionName = Name Ident.identW [Unify [0]]
isCoercion :: Name -> Bool isCoercion :: Name -> Bool
isCoercion (Name fun [Unify [0]]) = Ident.isWildIdent fun isCoercion (Name fun [Unify [0]]) = Ident.isWildIdent fun

View File

@@ -1,3 +1,4 @@
{-# LANGUAGE PatternGuards #-}
---------------------------------------------------------------------- ----------------------------------------------------------------------
-- | -- |
-- Module : CheckGrammar -- Module : CheckGrammar
@@ -29,11 +30,12 @@ import GF.Infra.Modules
import GF.Grammar.Refresh ---- import GF.Grammar.Refresh ----
import GF.Devel.TypeCheck import GF.Devel.TypeCheck
import GF.Grammar.Values (cPredefAbs) --- import GF.Grammar.Predef (cPredef, cPredefAbs) ---
import GF.Grammar.PrGrammar import GF.Grammar.PrGrammar
import GF.Grammar.Lookup import GF.Grammar.Lookup
import GF.Grammar.LookAbs import GF.Grammar.LookAbs
import GF.Grammar.Predef
import GF.Grammar.Macros import GF.Grammar.Macros
import GF.Grammar.ReservedWords ---- import GF.Grammar.ReservedWords ----
import GF.Grammar.PatternMatch import GF.Grammar.PatternMatch
@@ -334,16 +336,10 @@ computeLType gr t = do
checkInContext g $ comp t checkInContext g $ comp t
where where
comp ty = case ty of comp ty = case ty of
_ | Just _ <- isTypeInts ty -> return ty ---- shouldn't be needed
App (Q (IC "Predef") (IC "Ints")) _ -> return ty ---- shouldn't be needed | ty == typeInt -> return ty ---- shouldn't be needed
Q (IC "Predef") (IC "Int") -> return ty ---- shouldn't be needed | ty == typeFloat -> return ty ---- shouldn't be needed
Q (IC "Predef") (IC "Float") -> return ty ---- shouldn't be needed | ty == typeError -> return ty ---- shouldn't be needed
Q (IC "Predef") (IC "Error") -> return ty ---- shouldn't be needed
Q m c | elem c [cPredef,cPredefAbs] -> return ty
Q m c | elem c [zIdent "Int"] ->
return $ linTypeInt
Q m c | elem c [zIdent "Float",zIdent "String"] -> return defLinType ----
Q m ident -> checkIn ("module" +++ prt m) $ do Q m ident -> checkIn ("module" +++ prt m) $ do
ty' <- checkErr (lookupResDef gr m ident) ty' <- checkErr (lookupResDef gr m ident)
@@ -525,7 +521,7 @@ inferLType gr trm = case trm of
check2 (flip justCheck typeStr) Glue s1 s2 typeStr ---- typeTok check2 (flip justCheck typeStr) Glue s1 s2 typeStr ---- typeTok
---- hack from Rename.identRenameTerm, to live with files with naming conflicts 18/6/2007 ---- hack from Rename.identRenameTerm, to live with files with naming conflicts 18/6/2007
Strs (Cn (IC "#conflict") : ts) -> do Strs (Cn c : ts) | c == cConflict -> do
trace ("WARNING: unresolved constant, could be any of" +++ unwords (map prt ts)) (infer $ head ts) trace ("WARNING: unresolved constant, could be any of" +++ unwords (map prt ts)) (infer $ head ts)
-- checkWarn ("WARNING: unresolved constant, could be any of" +++ unwords (map prt ts)) -- checkWarn ("WARNING: unresolved constant, could be any of" +++ unwords (map prt ts))
-- infer $ head ts -- infer $ head ts
@@ -964,7 +960,7 @@ checkIfEqLType env t u trm = do
alpha g t u = case (t,u) of alpha g t u = case (t,u) of
-- error (the empty type!) is subtype of any other type -- error (the empty type!) is subtype of any other type
(_,Q (IC "Predef") (IC "Error")) -> True (_,u) | u == typeError -> True
-- contravariance -- contravariance
(Prod x a b, Prod y c d) -> alpha g c a && alpha ((x,y):g) b d (Prod x a b, Prod y c d) -> alpha g c a && alpha ((x,y):g) b d
@@ -976,13 +972,9 @@ checkIfEqLType env t u trm = do
(ExtR r s, t) -> alpha g r t || alpha g s t (ExtR r s, t) -> alpha g r t || alpha g s t
-- the following say that Ints n is a subset of Int and of Ints m >= n -- the following say that Ints n is a subset of Int and of Ints m >= n
(App (Q (IC "Predef") (IC "Ints")) (EInt n), (t,u) | Just m <- isTypeInts t, Just n <- isTypeInts t -> m >= n
App (Q (IC "Predef") (IC "Ints")) (EInt m)) -> m >= n | Just _ <- isTypeInts t, u == typeInt -> True ---- check size!
(App (Q (IC "Predef") (IC "Ints")) (EInt n), | t == typeInt, Just _ <- isTypeInts t -> True ---- why this ???? AR 11/12/2005
Q (IC "Predef") (IC "Int")) -> True ---- check size!
(Q (IC "Predef") (IC "Int"), ---- why this ???? AR 11/12/2005
App (Q (IC "Predef") (IC "Ints")) (EInt n)) -> True
---- this should be made in Rename ---- this should be made in Rename
(Q m a, Q n b) | a == b -> elem m (allExtendsPlus env n) (Q m a, Q n b) | a == b -> elem m (allExtendsPlus env n)

View File

@@ -235,7 +235,7 @@ transCatDef x = case x of
constyp = mkProd (cont ++ [cd, M.mkDecl lc]) lc constyp = mkProd (cont ++ [cd, M.mkDecl lc]) lc
consfund = (consId, absFun constyp) ---- (yes constyp) (yes G.EData)) consfund = (consId, absFun constyp) ---- (yes constyp) (yes G.EData))
return [catd,nilfund,consfund] return [catd,nilfund,consfund]
mkId x i = if isWildIdent x then (mkIdent "x" i) else x mkId x i = if isWildIdent x then (identV "x" i) else x
transFunDef :: FunDef -> Err ([Ident], G.Type) transFunDef :: FunDef -> Err ([Ident], G.Type)
transFunDef x = case x of transFunDef x = case x of

View File

@@ -21,6 +21,7 @@ import GF.Infra.Option
import GF.Data.Str import GF.Data.Str
import GF.Grammar.PrGrammar import GF.Grammar.PrGrammar
import GF.Infra.Modules import GF.Infra.Modules
import GF.Grammar.Predef
import GF.Grammar.Macros import GF.Grammar.Macros
import GF.Grammar.Lookup import GF.Grammar.Lookup
import GF.Grammar.Refresh import GF.Grammar.Refresh
@@ -50,8 +51,8 @@ computeTermOpt rec gr = comput True where
comput full g t = ---- errIn ("subterm" +++ prt t) $ --- for debugging comput full g t = ---- errIn ("subterm" +++ prt t) $ --- for debugging
case t of case t of
Q (IC "Predef") _ -> return t Q p c | p == cPredef -> return t
Q p c -> look p c | otherwise -> look p c
-- if computed do nothing -- if computed do nothing
Computed t' -> return $ unComputed t' Computed t' -> return $ unComputed t'
@@ -89,7 +90,7 @@ computeTermOpt rec gr = comput True where
_ | not (null [() | FV _ <- as']) -> compApp g (mkApp h' as') _ | not (null [() | FV _ <- as']) -> compApp g (mkApp h' as')
c@(QC _ _) -> do c@(QC _ _) -> do
return $ mkApp c as' return $ mkApp c as'
Q (IC "Predef") f -> do Q mod f | mod == cPredef -> do
(t',b) <- appPredefined (mkApp h' as') (t',b) <- appPredefined (mkApp h' as')
if b then return t' else comp g t' if b then return t' else comp g t'
@@ -446,8 +447,8 @@ computeTermOpt rec gr = comput True where
-- | argument variables cannot be glued -- | argument variables cannot be glued
checkNoArgVars :: Term -> Err Term checkNoArgVars :: Term -> Err Term
checkNoArgVars t = case t of checkNoArgVars t = case t of
Vr (IA _) -> Bad $ glueErrorMsg $ prt t Vr (IA _ _) -> Bad $ glueErrorMsg $ prt t
Vr (IAV _) -> Bad $ glueErrorMsg $ prt t Vr (IAV _ _ _) -> Bad $ glueErrorMsg $ prt t
_ -> composOp checkNoArgVars t _ -> composOp checkNoArgVars t
glueErrorMsg s = glueErrorMsg s =

View File

@@ -40,7 +40,7 @@ mainGFC xx = do
targetName :: Options -> CId -> String targetName :: Options -> CId -> String
targetName opts abs = case getOptVal opts (aOpt "target") of targetName opts abs = case getOptVal opts (aOpt "target") of
Just n -> n Just n -> n
_ -> prIdent abs _ -> prCId abs
targetNameGFCC :: Options -> CId -> FilePath targetNameGFCC :: Options -> CId -> FilePath
targetNameGFCC opts abs = targetName opts abs ++ ".gfcc" targetNameGFCC opts abs = targetName opts abs ++ ".gfcc"

View File

@@ -175,15 +175,14 @@ fInstance m (cat,rules) =
--type HSkeleton = [(OIdent, [(OIdent, [OIdent])])] --type HSkeleton = [(OIdent, [(OIdent, [OIdent])])]
hSkeleton :: GFCC -> (String,HSkeleton) hSkeleton :: GFCC -> (String,HSkeleton)
hSkeleton gr = hSkeleton gr =
(pr (absname gr), (prCId (absname gr),
[(pr c, [(pr f, map pr cs) | (f, (cs,_)) <- fs]) | [(prCId c, [(prCId f, map prCId cs) | (f, (cs,_)) <- fs]) |
fs@((_, (_,c)):_) <- fns] fs@((_, (_,c)):_) <- fns]
) )
where where
fns = groupBy valtypg (sortBy valtyps (map jty (Map.assocs (funs (abstract gr))))) fns = groupBy valtypg (sortBy valtyps (map jty (Map.assocs (funs (abstract gr)))))
valtyps (_, (_,x)) (_, (_,y)) = compare x y valtyps (_, (_,x)) (_, (_,y)) = compare x y
valtypg (_, (_,x)) (_, (_,y)) = x == y valtypg (_, (_,x)) (_, (_,y)) = x == y
pr (CId c) = c
jty (f,(ty,_)) = (f,catSkeleton ty) jty (f,(ty,_)) = (f,catSkeleton ty)
updateSkeleton :: OIdent -> HSkeleton -> (OIdent, [OIdent]) -> HSkeleton updateSkeleton :: OIdent -> HSkeleton -> (OIdent, [OIdent]) -> HSkeleton

View File

@@ -24,7 +24,7 @@ gfcc2js :: D.GFCC -> String
gfcc2js gfcc = gfcc2js gfcc =
encodeUTF8 $ JS.printTree $ JS.Program [JS.ElStmt $ JS.SDeclOrExpr $ JS.Decl [JS.DInit (JS.Ident n) grammar]] encodeUTF8 $ JS.printTree $ JS.Program [JS.ElStmt $ JS.SDeclOrExpr $ JS.Decl [JS.DInit (JS.Ident n) grammar]]
where where
n = D.printCId $ D.absname gfcc n = prCId $ D.absname gfcc
as = D.abstract gfcc as = D.abstract gfcc
cs = Map.assocs (D.concretes gfcc) cs = Map.assocs (D.concretes gfcc)
start = M.lookStartCat gfcc start = M.lookStartCat gfcc
@@ -36,16 +36,16 @@ abstract2js :: String -> D.Abstr -> JS.Expr
abstract2js start ds = new "GFAbstract" [JS.EStr start, JS.EObj $ map absdef2js (Map.assocs (D.funs ds))] abstract2js start ds = new "GFAbstract" [JS.EStr start, JS.EObj $ map absdef2js (Map.assocs (D.funs ds))]
absdef2js :: (CId,(D.Type,D.Exp)) -> JS.Property absdef2js :: (CId,(D.Type,D.Exp)) -> JS.Property
absdef2js (CId f,(typ,_)) = absdef2js (f,(typ,_)) =
let (args,CId cat) = M.catSkeleton typ in let (args,cat) = M.catSkeleton typ in
JS.Prop (JS.StringPropName f) (new "Type" [JS.EArray [JS.EStr x | CId x <- args], JS.EStr cat]) JS.Prop (JS.IdentPropName (JS.Ident (prCId f))) (new "Type" [JS.EArray [JS.EStr (prCId x) | x <- args], JS.EStr (prCId cat)])
concrete2js :: String -> String -> (CId,D.Concr) -> JS.Property concrete2js :: String -> String -> (CId,D.Concr) -> JS.Property
concrete2js start n (CId c, cnc) = concrete2js start n (c, cnc) =
JS.Prop l (new "GFConcrete" ([(JS.EObj $ ((map (cncdef2js n c) ds) ++ litslins))] ++ JS.Prop l (new "GFConcrete" ([(JS.EObj $ ((map (cncdef2js n (prCId c)) ds) ++ litslins))] ++
maybe [] (parser2js start) (D.parser cnc))) maybe [] (parser2js start) (D.parser cnc)))
where where
l = JS.StringPropName c l = JS.IdentPropName (JS.Ident (prCId c))
ds = concatMap Map.assocs [D.lins cnc, D.opers cnc, D.lindefs cnc] ds = concatMap Map.assocs [D.lins cnc, D.opers cnc, D.lindefs cnc]
litslins = [JS.Prop (JS.StringPropName "Int") (JS.EFun [children] [JS.SReturn $ new "Arr" [JS.EIndex (JS.EVar children) (JS.EInt 0)]]), litslins = [JS.Prop (JS.StringPropName "Int") (JS.EFun [children] [JS.SReturn $ new "Arr" [JS.EIndex (JS.EVar children) (JS.EInt 0)]]),
JS.Prop (JS.StringPropName "Float") (JS.EFun [children] [JS.SReturn $ new "Arr" [JS.EIndex (JS.EVar children) (JS.EInt 0)]]), JS.Prop (JS.StringPropName "Float") (JS.EFun [children] [JS.SReturn $ new "Arr" [JS.EIndex (JS.EVar children) (JS.EInt 0)]]),
@@ -53,7 +53,7 @@ concrete2js start n (CId c, cnc) =
cncdef2js :: String -> String -> (CId,D.Term) -> JS.Property cncdef2js :: String -> String -> (CId,D.Term) -> JS.Property
cncdef2js n l (CId f, t) = JS.Prop (JS.StringPropName f) (JS.EFun [children] [JS.SReturn (term2js n l t)]) cncdef2js n l (f, t) = JS.Prop (JS.IdentPropName (JS.Ident (prCId f))) (JS.EFun [children] [JS.SReturn (term2js n l t)])
term2js :: String -> String -> D.Term -> JS.Expr term2js :: String -> String -> D.Term -> JS.Expr
term2js n l t = f t term2js n l t = f t
@@ -66,7 +66,7 @@ term2js n l t = f t
D.K t -> tokn2js t D.K t -> tokn2js t
D.V i -> JS.EIndex (JS.EVar children) (JS.EInt i) D.V i -> JS.EIndex (JS.EVar children) (JS.EInt i)
D.C i -> new "Int" [JS.EInt i] D.C i -> new "Int" [JS.EInt i]
D.F (CId f) -> JS.ECall (JS.EMember (JS.EIndex (JS.EMember (JS.EVar $ JS.Ident n) (JS.Ident "concretes")) (JS.EStr l)) (JS.Ident "rule")) [JS.EStr f, JS.EVar children] D.F f -> JS.ECall (JS.EMember (JS.EIndex (JS.EMember (JS.EVar $ JS.Ident n) (JS.Ident "concretes")) (JS.EStr l)) (JS.Ident "rule")) [JS.EStr (prCId f), JS.EVar children]
D.FV xs -> new "Variants" (map f xs) D.FV xs -> new "Variants" (map f xs)
D.W str x -> new "Suffix" [JS.EStr str, f x] D.W str x -> new "Suffix" [JS.EStr str, f x]
D.RP x y -> new "Rp" [f x, f y] D.RP x y -> new "Rp" [f x, f y]
@@ -95,15 +95,15 @@ parser2js start p = [new "Parser" [JS.EStr start,
JS.EArray $ map frule2js (Array.elems (allRules p)), JS.EArray $ map frule2js (Array.elems (allRules p)),
JS.EObj $ map cats (Map.assocs (startupCats p))]] JS.EObj $ map cats (Map.assocs (startupCats p))]]
where where
cats (CId c,is) = JS.Prop (JS.StringPropName c) (JS.EArray (map JS.EInt is)) cats (c,is) = JS.Prop (JS.IdentPropName (JS.Ident (prCId c))) (JS.EArray (map JS.EInt is))
frule2js :: FRule -> JS.Expr frule2js :: FRule -> JS.Expr
frule2js (FRule n args res lins) = new "Rule" [JS.EInt res, name2js n, JS.EArray (map JS.EInt args), lins2js lins] frule2js (FRule n args res lins) = new "Rule" [JS.EInt res, name2js n, JS.EArray (map JS.EInt args), lins2js lins]
name2js :: FName -> JS.Expr name2js :: FName -> JS.Expr
name2js n = case n of name2js n = case n of
Name (CId "_") [p] -> fromProfile p Name f [p] | f == wildCId -> fromProfile p
Name f ps -> new "FunApp" $ [JS.EStr $ prCId f, JS.EArray (map fromProfile ps)] Name f ps -> new "FunApp" $ [JS.EStr $ prCId f, JS.EArray (map fromProfile ps)]
where where
fromProfile :: Profile (SyntaxForest CId) -> JS.Expr fromProfile :: Profile (SyntaxForest CId) -> JS.Expr
fromProfile (Unify []) = new "MetaVar" [] fromProfile (Unify []) = new "MetaVar" []

View File

@@ -5,7 +5,6 @@ import GF.Command.Importing
import GF.Command.Commands import GF.Command.Commands
import GF.GFCC.API import GF.GFCC.API
import GF.System.Arch (fetchCommand)
import GF.Devel.UseIO import GF.Devel.UseIO
import GF.Devel.Arch import GF.Devel.Arch
import GF.Infra.Option ---- Haskell's option lib import GF.Infra.Option ---- Haskell's option lib
@@ -21,7 +20,8 @@ mainGFI xx = do
loop :: GFEnv -> IO GFEnv loop :: GFEnv -> IO GFEnv
loop gfenv0 = do loop gfenv0 = do
let env = commandenv gfenv0 let env = commandenv gfenv0
s <- fetchCommand (prompt env) putStrFlush (prompt env)
s <- getLine
let gfenv = gfenv0 {history = s : history gfenv0} let gfenv = gfenv0 {history = s : history gfenv0}
case words s of case words s of

View File

@@ -53,13 +53,13 @@ typPredefined c@(IC f) = case f of
"plus" -> return $ mkFunType [cnPredef "Int",cnPredef "Int"] (cnPredef "Int") "plus" -> return $ mkFunType [cnPredef "Int",cnPredef "Int"] (cnPredef "Int")
---- "read" -> (P : Type) -> Tok -> P ---- "read" -> (P : Type) -> Tok -> P
"show" -> return $ mkProds -- (P : PType) -> P -> Tok "show" -> return $ mkProds -- (P : PType) -> P -> Tok
([(identC "P",typePType),(wildIdent,Vr (identC "P"))],typeStr,[]) ([(identC "P",typePType),(identW,Vr (identC "P"))],typeStr,[])
"toStr" -> return $ mkProds -- (L : Type) -> L -> Str "toStr" -> return $ mkProds -- (L : Type) -> L -> Str
([(identC "L",typeType),(wildIdent,Vr (identC "L"))],typeStr,[]) ([(identC "L",typeType),(identW,Vr (identC "L"))],typeStr,[])
"mapStr" -> "mapStr" ->
let ty = identC "L" in let ty = identC "L" in
return $ mkProds -- (L : Type) -> (Str -> Str) -> L -> L return $ mkProds -- (L : Type) -> (Str -> Str) -> L -> L
([(ty,typeType),(wildIdent,mkFunType [typeStr] typeStr),(wildIdent,Vr ty)],Vr ty,[]) ([(ty,typeType),(identW,mkFunType [typeStr] typeStr),(identW,Vr ty)],Vr ty,[])
"take" -> return $ mkFunType [cnPredef "Int",typeStr] typeStr "take" -> return $ mkFunType [cnPredef "Int",typeStr] typeStr
"tk" -> return $ mkFunType [cnPredef "Int",typeStr] typeStr "tk" -> return $ mkFunType [cnPredef "Int",typeStr] typeStr
_ -> prtBad "unknown in Predef:" c _ -> prtBad "unknown in Predef:" c

View File

@@ -81,7 +81,7 @@ typeSkeleton typ = do
-- construct types and terms -- construct types and terms
mkFunType :: [Type] -> Type -> Type mkFunType :: [Type] -> Type -> Type
mkFunType tt t = mkProd ([(wildIdent, ty) | ty <- tt]) t -- nondep prod mkFunType tt t = mkProd ([(identW, ty) | ty <- tt]) t -- nondep prod
mkApp :: Term -> [Term] -> Term mkApp :: Term -> [Term] -> Term
mkApp = foldl App mkApp = foldl App
@@ -121,7 +121,7 @@ unzipR :: [Assign] -> ([Label],[Term])
unzipR r = (ls, map snd ts) where (ls,ts) = unzip r unzipR r = (ls, map snd ts) where (ls,ts) = unzip r
mkDecl :: Term -> Decl mkDecl :: Term -> Decl
mkDecl typ = (wildIdent, typ) mkDecl typ = (identW, typ)
mkLet :: [LocalDef] -> Term -> Term mkLet :: [LocalDef] -> Term -> Term
mkLet defs t = foldr Let t defs mkLet defs t = foldr Let t defs
@@ -336,7 +336,7 @@ changeTableType co i = case i of
patt2term :: Patt -> Term patt2term :: Patt -> Term
patt2term pt = case pt of patt2term pt = case pt of
PV x -> Vr x PV x -> Vr x
PW -> Vr wildIdent --- not parsable, should not occur PW -> Vr identW --- not parsable, should not occur
PC c pp -> mkApp (Con c) (map patt2term pp) PC c pp -> mkApp (Con c) (map patt2term pp)
PP p c pp -> mkApp (QC p c) (map patt2term pp) PP p c pp -> mkApp (QC p c) (map patt2term pp)
PR r -> R [assign l (patt2term p) | (l,p) <- r] PR r -> R [assign l (patt2term p) | (l,p) <- r]

View File

@@ -71,7 +71,7 @@ prModule :: SourceModule -> String
prModule = cprintTree . trModule prModule = cprintTree . trModule
instance Print Judgement where instance Print Judgement where
prt j = cprintTree $ trAnyDef (wildIdent, j) prt j = cprintTree $ trAnyDef (identW, j)
---- prt_ = prExp ---- prt_ = prExp
instance Print Term where instance Print Term where

View File

@@ -1,3 +1,4 @@
{-# LANGUAGE PatternGuards #-}
module GF.Devel.GrammarToGFCC (prGrammar2gfcc,mkCanon2gfcc,addParsers) where module GF.Devel.GrammarToGFCC (prGrammar2gfcc,mkCanon2gfcc,addParsers) where
import GF.Devel.OptimizeGF (unshareModule) import GF.Devel.OptimizeGF (unshareModule)
@@ -9,6 +10,7 @@ import qualified GF.GFCC.Macros as CM
import qualified GF.GFCC.DataGFCC as C import qualified GF.GFCC.DataGFCC as C
import qualified GF.GFCC.DataGFCC as D import qualified GF.GFCC.DataGFCC as D
import GF.GFCC.CId import GF.GFCC.CId
import GF.Grammar.Predef
import qualified GF.Grammar.Abstract as A import qualified GF.Grammar.Abstract as A
import qualified GF.Grammar.Macros as GM import qualified GF.Grammar.Macros as GM
--import qualified GF.Grammar.Compute as Compute --import qualified GF.Grammar.Compute as Compute
@@ -28,6 +30,7 @@ import GF.Text.UTF8
import Data.List import Data.List
import Data.Char (isDigit,isSpace) import Data.Char (isDigit,isSpace)
import qualified Data.Map as Map import qualified Data.Map as Map
import qualified Data.ByteString.Char8 as BS
import Debug.Trace ---- import Debug.Trace ----
-- when developing, swap commenting -- when developing, swap commenting
@@ -46,7 +49,7 @@ mkCanon2gfcc :: Options -> String -> SourceGrammar -> (String,D.GFCC)
mkCanon2gfcc opts cnc gr = mkCanon2gfcc opts cnc gr =
(prIdent abs, (canon2gfcc opts pars . reorder abs . canon2canon abs) gr) (prIdent abs, (canon2gfcc opts pars . reorder abs . canon2canon abs) gr)
where where
abs = err error id $ M.abstractOfConcrete gr (identC cnc) abs = err error id $ M.abstractOfConcrete gr (identC (BS.pack cnc))
pars = mkParamLincat gr pars = mkParamLincat gr
-- Adds parsers for all concretes -- Adds parsers for all concretes
@@ -67,9 +70,9 @@ canon2gfcc opts pars cgr@(M.MGrammar ((a,M.ModMod abm):cms)) =
an = (i2i a) an = (i2i a)
cns = map (i2i . fst) cms cns = map (i2i . fst) cms
abs = D.Abstr aflags funs cats catfuns abs = D.Abstr aflags funs cats catfuns
gflags = Map.fromList [(CId fg,x) | Just x <- [getOptVal opts (aOpt fg)]] gflags = Map.fromList [(mkCId fg,x) | Just x <- [getOptVal opts (aOpt fg)]]
where fg = "firstlang" where fg = "firstlang"
aflags = Map.fromList [(CId f,x) | Opt (f,[x]) <- M.flags abm] aflags = Map.fromList [(mkCId f,x) | Opt (f,[x]) <- M.flags abm]
mkDef pty = case pty of mkDef pty = case pty of
Yes t -> mkExp t Yes t -> mkExp t
_ -> CM.primNotion _ -> CM.primNotion
@@ -89,7 +92,7 @@ canon2gfcc opts pars cgr@(M.MGrammar ((a,M.ModMod abm):cms)) =
(lang,D.Concr flags lins opers lincats lindefs printnames params fcfg) (lang,D.Concr flags lins opers lincats lindefs printnames params fcfg)
where where
js = tree2list (M.jments mo) js = tree2list (M.jments mo)
flags = Map.fromList [(CId f,x) | Opt (f,[x]) <- M.flags mo] flags = Map.fromList [(mkCId f,x) | Opt (f,[x]) <- M.flags mo]
opers = Map.fromAscList [] -- opers will be created as optimization opers = Map.fromAscList [] -- opers will be created as optimization
utf = if elem (Opt ("coding",["utf8"])) (M.flags mo) utf = if elem (Opt ("coding",["utf8"])) (M.flags mo)
then D.convertStringsInTerm decodeUTF8 else id then D.convertStringsInTerm decodeUTF8 else id
@@ -107,7 +110,7 @@ canon2gfcc opts pars cgr@(M.MGrammar ((a,M.ModMod abm):cms)) =
fcfg = Nothing fcfg = Nothing
i2i :: Ident -> CId i2i :: Ident -> CId
i2i = CId . prIdent i2i = CId . ident2bs
mkType :: A.Type -> C.Type mkType :: A.Type -> C.Type
mkType t = case GM.typeForm t of mkType t = case GM.typeForm t of
@@ -131,7 +134,7 @@ mkExp t = case t of
mkPatt p = uncurry CM.tree $ case p of mkPatt p = uncurry CM.tree $ case p of
A.PP _ c ps -> (C.AC (i2i c), map mkPatt ps) A.PP _ c ps -> (C.AC (i2i c), map mkPatt ps)
A.PV x -> (C.AV (i2i x), []) A.PV x -> (C.AV (i2i x), [])
A.PW -> (C.AV CM.wildCId, []) A.PW -> (C.AV wildCId, [])
A.PInt i -> (C.AI i, []) A.PInt i -> (C.AI i, [])
mkContext :: A.Context -> [C.Hypo] mkContext :: A.Context -> [C.Hypo]
@@ -139,10 +142,10 @@ mkContext hyps = [C.Hyp (i2i x) (mkType ty) | (x,ty) <- hyps]
mkTerm :: Term -> C.Term mkTerm :: Term -> C.Term
mkTerm tr = case tr of mkTerm tr = case tr of
Vr (IA (_,i)) -> C.V i Vr (IA _ i) -> C.V i
Vr (IAV (_,_,i)) -> C.V i Vr (IAV _ _ i) -> C.V i
Vr (IC s) | isDigit (last s) -> Vr (IC s) | isDigit (BS.last s) ->
C.V (read (reverse (takeWhile (/='_') (reverse s)))) C.V ((read . BS.unpack . snd . BS.spanEnd isDigit) s)
---- from gf parser of gfc ---- from gf parser of gfc
EInt i -> C.C $ fromInteger i EInt i -> C.C $ fromInteger i
R rs -> C.R [mkTerm t | (_, (_,t)) <- rs] R rs -> C.R [mkTerm t | (_, (_,t)) <- rs]
@@ -162,7 +165,7 @@ mkTerm tr = case tr of
C.K (C.KP (strings td) [C.Var (strings u) (strings v) | (u,v) <- tvs]) C.K (C.KP (strings td) [C.Var (strings u) (strings v) | (u,v) <- tvs])
_ -> prtTrace tr $ C.S [C.K (C.KS (A.prt tr +++ "66662"))] ---- for debugging _ -> prtTrace tr $ C.S [C.K (C.KS (A.prt tr +++ "66662"))] ---- for debugging
where where
mkLab (LIdent l) = case l of mkLab (LIdent l) = case BS.unpack l of
'_':ds -> (read ds) :: Int '_':ds -> (read ds) :: Int
_ -> prtTrace tr $ 66663 _ -> prtTrace tr $ 66663
strings t = case t of strings t = case t of
@@ -182,8 +185,8 @@ mkCType t = case t of
Table pt vt -> case pt of Table pt vt -> case pt of
EInt i -> C.R $ replicate (1 + fromInteger i) $ mkCType vt EInt i -> C.R $ replicate (1 + fromInteger i) $ mkCType vt
RecType rs -> mkCType $ foldr Table vt (map snd rs) RecType rs -> mkCType $ foldr Table vt (map snd rs)
Sort "Str" -> C.S [] --- Str only Sort s | s == cStr -> C.S [] --- Str only
App (Q (IC "Predef") (IC "Ints")) (EInt i) -> C.C $ fromInteger i _ | Just i <- GM.isTypeInts t -> C.C $ fromInteger i
_ -> error $ "mkCType " ++ show t _ -> error $ "mkCType " ++ show t
-- encoding showable lincats (as in source gf) as terms -- encoding showable lincats (as in source gf) as terms
@@ -204,7 +207,7 @@ mkParamLincat sgr lang cat = errVal (C.R [C.S []]) $ do
p' <- mkPType p p' <- mkPType p
v' <- mkPType v v' <- mkPType v
return $ C.S [p',v'] return $ C.S [p',v']
Sort "Str" -> return $ C.S [] Sort s | s == cStr -> return $ C.S []
_ -> return $ _ -> return $
C.FV $ map (kks . filter showable . prt_) $ C.FV $ map (kks . filter showable . prt_) $
errVal [] $ Look.allParamValues sgr typ errVal [] $ Look.allParamValues sgr typ
@@ -225,7 +228,7 @@ reorder abs cg = M.MGrammar $
adefs = sorted2tree $ sortIds $ adefs = sorted2tree $ sortIds $
predefADefs ++ Look.allOrigInfos cg abs predefADefs ++ Look.allOrigInfos cg abs
predefADefs = predefADefs =
[(IC c, AbsCat (Yes []) Nope) | c <- ["Float","Int","String"]] [(c, AbsCat (Yes []) Nope) | c <- [cFloat,cInt,cString]]
aflags = nubFlags $ aflags = nubFlags $
concat [M.flags mo | (_,mo) <- M.allModMod cg, M.isModAbs mo] concat [M.flags mo | (_,mo) <- M.allModMod cg, M.isModAbs mo]
@@ -238,10 +241,7 @@ reorder abs cg = M.MGrammar $
Just r <- [lookup i (M.allExtendSpecs cg la)]] Just r <- [lookup i (M.allExtendSpecs cg la)]]
predefCDefs = predefCDefs =
(IC "Int", CncCat (Yes Look.linTypeInt) Nope Nope) : [(c, CncCat (Yes GM.defLinType) Nope Nope) | c <- [cInt,cFloat,cString]]
[(IC c, CncCat (Yes GM.defLinType) Nope Nope) |
---- lindef,printname
c <- ["Float","String"]]
sortIds = sortBy (\ (f,_) (g,_) -> compare f g) sortIds = sortBy (\ (f,_) (g,_) -> compare f g)
nubFlags = nubBy (\ (Opt (f,_)) (Opt (g,_)) -> f == g) nubFlags = nubBy (\ (Opt (f,_)) (Opt (g,_)) -> f == g)
@@ -369,13 +369,11 @@ paramValues cgr = (labels,untyps,typs) where
untyps = untyps =
Map.fromList $ concatMap Map.toList [typ | (_,typ) <- Map.toList typs] Map.fromList $ concatMap Map.toList [typ | (_,typ) <- Map.toList typs]
lincats = lincats =
[(IC "Int",[f | let RecType fs = Look.linTypeInt, f <- fs])] ++ [(cat,[f | let RecType fs = GM.defLinType, f <- fs]) | cat <- [cInt,cFloat, cString]] ++
[(IC cat,[(LIdent "s",GM.typeStr)]) | cat <- ["Float", "String"]] ++
reverse ---- TODO: really those lincats that are reached reverse ---- TODO: really those lincats that are reached
---- reverse is enough to expel overshadowed ones... ---- reverse is enough to expel overshadowed ones...
[(cat,ls) | (_,(cat,CncCat (Yes ty) _ _)) <- jments, [(cat,ls) | (_,(cat,CncCat (Yes ty) _ _)) <- jments,
RecType ls <- [unlockTy ty]] RecType ls <- [unlockTy ty]]
---- [(cat,(unlockTyp ls)) | (_,(cat,CncCat (Yes (RecType ls)) _ _)) <- jments]
labels = Map.fromList $ concat labels = Map.fromList $ concat
[((cat,[lab]),(typ,i)): [((cat,[lab]),(typ,i)):
[((cat,[LVar v]),(typ,toInteger (mx + v))) | v <- [0,1]] ++ ---- 1 or 2 vars [((cat,[LVar v]),(typ,toInteger (mx + v))) | v <- [0,1]] ++ ---- 1 or 2 vars
@@ -449,7 +447,7 @@ term2term cgr env@(labels,untyps,typs) tr = case tr of
doVar tr = case getLab tr of doVar tr = case getLab tr of
Ok (cat, lab) -> do Ok (cat, lab) -> do
k <- readSTM >>= return . length k <- readSTM >>= return . length
let tr' = Vr $ identC $ show k ----- let tr' = Vr $ identC $ (BS.pack (show k)) -----
let tyvs = case Map.lookup (cat,lab) labels of let tyvs = case Map.lookup (cat,lab) labels of
Just (ty,_) -> case Map.lookup ty typs of Just (ty,_) -> case Map.lookup ty typs of
@@ -472,10 +470,10 @@ term2term cgr env@(labels,untyps,typs) tr = case tr of
-- this goes recursively into tables (ignored) and records (accumulated) -- this goes recursively into tables (ignored) and records (accumulated)
getLab tr = case tr of getLab tr = case tr of
Vr (IA (cat, _)) -> return (identC cat,[]) Vr (IA cat _) -> return (identC cat,[])
Vr (IAV (cat,_,_)) -> return (identC cat,[]) Vr (IAV cat _ _) -> return (identC cat,[])
Vr (IC s) -> return (identC cat,[]) where Vr (IC s) -> return (identC cat,[]) where
cat = takeWhile (/='_') s ---- also to match IAVs; no _ in a cat tolerated cat = BS.takeWhile (/='_') s ---- also to match IAVs; no _ in a cat tolerated
---- init (reverse (dropWhile (/='_') (reverse s))) ---- from gf parser ---- init (reverse (dropWhile (/='_') (reverse s))) ---- from gf parser
---- Vr _ -> error $ "getLab " ++ show tr ---- Vr _ -> error $ "getLab " ++ show tr
P p lab2 -> do P p lab2 -> do
@@ -518,7 +516,7 @@ term2term cgr env@(labels,untyps,typs) tr = case tr of
mkCurrySel t p = S t p -- done properly in CheckGFCC mkCurrySel t p = S t p -- done properly in CheckGFCC
mkLab k = LIdent (("_" ++ show k)) mkLab k = LIdent (BS.pack ("_" ++ show k))
-- remove lock fields; in fact, any empty records and record types -- remove lock fields; in fact, any empty records and record types
unlock = filter notlock where unlock = filter notlock where

View File

@@ -1,3 +1,4 @@
{-# LANGUAGE PatternGuards #-}
---------------------------------------------------------------------- ----------------------------------------------------------------------
-- | -- |
-- Module : Optimize -- Module : Optimize
@@ -20,6 +21,7 @@ import GF.Infra.Modules
import GF.Grammar.PrGrammar import GF.Grammar.PrGrammar
import GF.Grammar.Macros import GF.Grammar.Macros
import GF.Grammar.Lookup import GF.Grammar.Lookup
import GF.Grammar.Predef
import GF.Grammar.Refresh import GF.Grammar.Refresh
import GF.Devel.Compute import GF.Devel.Compute
import GF.Compile.BackOpt import GF.Compile.BackOpt
@@ -128,9 +130,9 @@ evalCncInfo opts gr cnc abs (c,info) = do
CncCat ptyp pde ppr -> do CncCat ptyp pde ppr -> do
pde' <- case (ptyp,pde) of pde' <- case (ptyp,pde) of
(Yes typ, Yes de) -> (Yes typ, Yes de) ->
liftM yes $ pEval ([(strVar, typeStr)], typ) de liftM yes $ pEval ([(varStr, typeStr)], typ) de
(Yes typ, Nope) -> (Yes typ, Nope) ->
liftM yes $ mkLinDefault gr typ >>= partEval noOptions gr ([(strVar, typeStr)],typ) liftM yes $ mkLinDefault gr typ >>= partEval noOptions gr ([(varStr, typeStr)],typ)
(May b, Nope) -> (May b, Nope) ->
return $ May b return $ May b
_ -> return pde -- indirection _ -> return pde -- indirection
@@ -161,72 +163,20 @@ partEval opts gr (context, val) trm = errIn ("parteval" +++ prt_ trm) $ do
let vars = map fst context let vars = map fst context
args = map Vr vars args = map Vr vars
subst = [(v, Vr v) | v <- vars] subst = [(v, Vr v) | v <- vars]
trm1 = mkApp trm args trm1 = mkApp trm args
trm3 <- if globalTable trm2 <- computeTerm gr subst trm1
then etaExpand subst trm1 >>= outCase subst trm3 <- if rightType trm2
else etaExpand subst trm1 then computeTerm gr subst trm2
else recordExpand val trm2 >>= computeTerm gr subst
return $ mkAbs vars trm3 return $ mkAbs vars trm3
where
-- don't eta expand records of right length (correct by type checking)
rightType (R rs) = case val of
RecType ts -> length rs == length ts
_ -> False
rightType _ = False
where
globalTable = oElem showAll opts --- i -all
comp g t = {- refreshTerm t >>= -} computeTerm gr g t
etaExpand su t = do
t' <- comp su t
case t' of
R _ | rightType t' -> comp su t' --- return t' wo noexpand...
_ -> recordExpand val t' >>= comp su
-- don't eta expand records of right length (correct by type checking)
rightType t = case (t,val) of
(R rs, RecType ts) -> length rs == length ts
_ -> False
outCase subst t = do
pts <- getParams context
let (args,ptyps) = unzip $ filter (flip occur t . fst) pts
if null args
then return t
else do
let argtyp = RecType $ tuple2recordType ptyps
let pvars = map (Vr . zIdent . prt) args -- gets eliminated
patt <- term2patt $ R $ tuple2record $ pvars
let t' = replace (zip args pvars) t
t1 <- comp subst $ T (TTyped argtyp) [(patt, t')]
return $ S t1 $ R $ tuple2record args
--- notice: this assumes that all lin types follow the "old JFP style"
getParams = liftM concat . mapM getParam
getParam (argv,RecType rs) = return
[(P (Vr argv) lab, ptyp) | (lab,ptyp) <- rs, not (isLinLabel lab)]
---getParam (_,ty) | ty==typeStr = return [] --- in lindef
getParam (av,ty) =
Bad ("record type expected not" +++ prt ty +++ "for" +++ prt av)
--- all lin types are rec types
replace :: [(Term,Term)] -> Term -> Term
replace reps trm = case trm of
-- this is the important case
P _ _ -> maybe trm id $ lookup trm reps
_ -> composSafeOp (replace reps) trm
occur t trm = case trm of
-- this is the important case
P _ _ -> t == trm
S x y -> occur t y || occur t x
App f x -> occur t x || occur t f
Abs _ f -> occur t f
R rs -> any (occur t) (map (snd . snd) rs)
T _ cs -> any (occur t) (map snd cs)
C x y -> occur t x || occur t y
Glue x y -> occur t x || occur t y
ExtR x y -> occur t x || occur t y
FV ts -> any (occur t) ts
V _ ts -> any (occur t) ts
Let (_,(_,x)) y -> occur t x || occur t y
_ -> False
-- here we must be careful not to reduce -- here we must be careful not to reduce
@@ -246,8 +196,8 @@ recordExpand typ trm = case unComputed typ of
mkLinDefault :: SourceGrammar -> Type -> Err Term mkLinDefault :: SourceGrammar -> Type -> Err Term
mkLinDefault gr typ = do mkLinDefault gr typ = do
case unComputed typ of case unComputed typ of
RecType lts -> mapPairsM mkDefField lts >>= (return . Abs strVar . R . mkAssign) RecType lts -> mapPairsM mkDefField lts >>= (return . Abs varStr . R . mkAssign)
_ -> liftM (Abs strVar) $ mkDefField typ _ -> liftM (Abs varStr) $ mkDefField typ
---- _ -> prtBad "linearization type must be a record type, not" typ ---- _ -> prtBad "linearization type must be a record type, not" typ
where where
mkDefField typ = case unComputed typ of mkDefField typ = case unComputed typ of
@@ -255,13 +205,13 @@ mkLinDefault gr typ = do
t' <- mkDefField t t' <- mkDefField t
let T _ cs = mkWildCases t' let T _ cs = mkWildCases t'
return $ T (TWild p) cs return $ T (TWild p) cs
Sort "Str" -> return $ Vr strVar Sort s | s == cStr -> return $ Vr varStr
QC q p -> lookupFirstTag gr q p QC q p -> lookupFirstTag gr q p
RecType r -> do RecType r -> do
let (ls,ts) = unzip r let (ls,ts) = unzip r
ts' <- mapM mkDefField ts ts' <- mapM mkDefField ts
return $ R $ [assign l t | (l,t) <- zip ls ts'] return $ R $ [assign l t | (l,t) <- zip ls ts']
_ | isTypeInts typ -> return $ EInt 0 -- exists in all as first val _ | Just _ <- isTypeInts typ -> return $ EInt 0 -- exists in all as first val
_ -> prtBad "linearization type field cannot be" typ _ -> prtBad "linearization type field cannot be" typ
-- | Form the printname: if given, compute. If not, use the computed -- | Form the printname: if given, compute. If not, use the computed

View File

@@ -30,6 +30,7 @@ import GF.Data.Operations
import Control.Monad import Control.Monad
import Data.Map (Map) import Data.Map (Map)
import qualified Data.Map as Map import qualified Data.Map as Map
import qualified Data.ByteString.Char8 as BS
import Data.List import Data.List
optModule :: (Ident, SourceModInfo) -> (Ident, SourceModInfo) optModule :: (Ident, SourceModInfo) -> (Ident, SourceModInfo)
@@ -88,7 +89,7 @@ factor c i t = case t of
--- we hope this will be fresh and don't check... in GFC would be safe --- we hope this will be fresh and don't check... in GFC would be safe
qqIdent c i = identC ("q_" ++ prt c ++ "__" ++ show i) qqIdent c i = identC (BS.pack ("q_" ++ prt c ++ "__" ++ show i))
-- we need to replace subterms -- we need to replace subterms
@@ -190,7 +191,7 @@ unsubexpModule mo@(i,m) = case m of
ResOper pty (Yes t) -> [(c, ResOper pty (Yes (unparTerm t)))] ResOper pty (Yes t) -> [(c, ResOper pty (Yes (unparTerm t)))]
_ -> [(c,info)] _ -> [(c,info)]
unparTerm t = case t of unparTerm t = case t of
Q m c@(IC ('A':'\'':'\'':_)) -> --- name convention of subexp opers Q m c | isOperIdent c -> --- name convention of subexp opers
errVal t $ liftM unparTerm $ lookupResDef gr m c errVal t $ liftM unparTerm $ lookupResDef gr m c
_ -> C.composSafeOp unparTerm t _ -> C.composSafeOp unparTerm t
gr = M.MGrammar [mo] gr = M.MGrammar [mo]
@@ -217,12 +218,12 @@ addSubexpConsts mo tree lins = do
return (f,ResOper ty (Yes trm')) return (f,ResOper ty (Yes trm'))
_ -> return (f,def) _ -> return (f,def)
recomp f t = case Map.lookup t tree of recomp f t = case Map.lookup t tree of
Just (_,id) | ident id /= f -> return $ Q mo (ident id) Just (_,id) | operIdent id /= f -> return $ Q mo (operIdent id)
_ -> C.composOp (recomp f) t _ -> C.composOp (recomp f) t
list = Map.toList tree list = Map.toList tree
oper id trm = (ident id, ResOper (Yes (EInt 8)) (Yes trm)) oper id trm = (operIdent id, ResOper (Yes (EInt 8)) (Yes trm))
--- impossible type encoding generated opers --- impossible type encoding generated opers
getSubtermsMod :: Ident -> [(Ident,Info)] -> TermM (Map Term (Int,Int)) getSubtermsMod :: Ident -> [(Ident,Info)] -> TermM (Map Term (Int,Int))
@@ -266,6 +267,10 @@ collectSubterms mo t = case t of
writeSTM (Map.insert t (count,id) ts, next) writeSTM (Map.insert t (count,id) ts, next)
return t --- only because of composOp return t --- only because of composOp
ident :: Int -> Ident operIdent :: Int -> Ident
ident i = identC ("A''" ++ show i) --- operIdent i = identC (operPrefix `BS.append` (BS.pack (show i))) ---
isOperIdent :: Ident -> Bool
isOperIdent id = BS.isPrefixOf operPrefix (ident2bs id)
operPrefix = BS.pack ("A''")

View File

@@ -185,7 +185,7 @@ importsOfModule (MModule _ typ body) = modType typ (modBody body [])
opens NoOpens xs = xs opens NoOpens xs = xs
opens (OpenIn os) xs = foldr open xs os opens (OpenIn os) xs = foldr open xs os
modName (PIdent (_,s)) = s modName (PIdent (_,s)) = BS.unpack s
-- | options can be passed to the compiler by comments in @--#@, in the main file -- | options can be passed to the compiler by comments in @--#@, in the main file

View File

@@ -22,6 +22,7 @@ module GF.Devel.TC (AExp(..),
) where ) where
import GF.Data.Operations import GF.Data.Operations
import GF.Grammar.Predef
import GF.Grammar.Abstract import GF.Grammar.Abstract
import GF.Devel.AbsCompute import GF.Devel.AbsCompute
@@ -145,10 +146,9 @@ checkInferExp th tenv@(k,_,_) e typ = do
inferExp :: Theory -> TCEnv -> Exp -> Err (AExp, Val, [(Val,Val)]) inferExp :: Theory -> TCEnv -> Exp -> Err (AExp, Val, [(Val,Val)])
inferExp th tenv@(k,rho,gamma) e = case e of inferExp th tenv@(k,rho,gamma) e = case e of
Vr x -> mkAnnot (AVr x) $ noConstr $ lookupVar gamma x Vr x -> mkAnnot (AVr x) $ noConstr $ lookupVar gamma x
Q m c Q m c | m == cPredefAbs && isPredefCat c
| m == cPredefAbs && (elem c (map identC ["Int","String","Float"])) -> -> return (ACn (m,c) vType, vType, [])
return (ACn (m,c) vType, vType, []) | otherwise -> mkAnnot (ACn (m,c)) $ noConstr $ lookupConst th (m,c)
| otherwise -> mkAnnot (ACn (m,c)) $ noConstr $ lookupConst th (m,c)
QC m c -> mkAnnot (ACn (m,c)) $ noConstr $ lookupConst th (m,c) ---- QC m c -> mkAnnot (ACn (m,c)) $ noConstr $ lookupConst th (m,c) ----
EInt i -> return (AInt i, valAbsInt, []) EInt i -> return (AInt i, valAbsInt, [])
EFloat i -> return (AFloat i, valAbsFloat, []) EFloat i -> return (AFloat i, valAbsFloat, [])
@@ -164,12 +164,6 @@ inferExp th tenv@(k,rho,gamma) e = case e of
return $ (AApp f' a' b', b', csf ++ csa) return $ (AApp f' a' b', b', csf ++ csa)
_ -> prtBad ("Prod expected for function" +++ prt f +++ "instead of") typ _ -> prtBad ("Prod expected for function" +++ prt f +++ "instead of") typ
_ -> prtBad "cannot infer type of expression" e _ -> prtBad "cannot infer type of expression" e
where
predefAbs c s = case c of
IC "Int" -> return $ const $ Q cPredefAbs cInt
IC "Float" -> return $ const $ Q cPredefAbs cFloat
IC "String" -> return $ const $ Q cPredefAbs cString
_ -> Bad s
checkEqs :: Theory -> TCEnv -> (Fun,Trm) -> Val -> Err [(Val,Val)] checkEqs :: Theory -> TCEnv -> (Fun,Trm) -> Val -> Err [(Val,Val)]
checkEqs th tenv@(k,rho,gamma) (fun@(m,f),def) val = case def of checkEqs th tenv@(k,rho,gamma) (fun@(m,f),def) val = case def of
@@ -188,9 +182,9 @@ checkEqs th tenv@(k,rho,gamma) (fun@(m,f),def) val = case def of
(_,cs2) <- errIn (show bds) $ checkExp th tenv' df typ (_,cs2) <- errIn (show bds) $ checkExp th tenv' df typ
return $ (cs1 ++ cs2) return $ (cs1 ++ cs2)
p2t p (ps,i,g) = case p of p2t p (ps,i,g) = case p of
PW -> (meta (MetaSymb i) : ps, i+1, g) PW -> (Meta (MetaSymb i) : ps, i+1, g)
PV IW -> (meta (MetaSymb i) : ps, i+1, g) PV IW -> (Meta (MetaSymb i) : ps, i+1, g)
PV x -> (meta (MetaSymb i) : ps, i+1,upd x i g) PV x -> (Meta (MetaSymb i) : ps, i+1,upd x i g)
PString s -> ( K s : ps, i, g) PString s -> ( K s : ps, i, g)
PInt n -> (EInt n : ps, i, g) PInt n -> (EInt n : ps, i, g)
PFloat n -> (EFloat n : ps, i, g) PFloat n -> (EFloat n : ps, i, g)
@@ -238,9 +232,9 @@ checkBranch th tenv b@(ps,t) ty = errIn ("branch" +++ show b) $
ps2ts k = foldr p2t ([],0,[],k) ps2ts k = foldr p2t ([],0,[],k)
p2t p (ps,i,g,k) = case p of p2t p (ps,i,g,k) = case p of
PW -> (meta (MetaSymb i) : ps, i+1,g,k) PW -> (Meta (MetaSymb i) : ps, i+1,g,k)
PV IW -> (meta (MetaSymb i) : ps, i+1,g,k) PV IW -> (Meta (MetaSymb i) : ps, i+1,g,k)
PV x -> (vr x : ps, i, upd x k g,k+1) PV x -> (Vr x : ps, i, upd x k g,k+1)
PString s -> (K s : ps, i, g, k) PString s -> (K s : ps, i, g, k)
PInt n -> (EInt n : ps, i, g, k) PInt n -> (EInt n : ps, i, g, k)
PFloat n -> (EFloat n : ps, i, g, k) PFloat n -> (EFloat n : ps, i, g, k)

View File

@@ -41,7 +41,6 @@ import GF.Formalism.Utilities
import qualified GF.GFCC.CId as AbsGFCC import qualified GF.GFCC.CId as AbsGFCC
import GF.Infra.PrintClass import GF.Infra.PrintClass
------------------------------------------------------------ ------------------------------------------------------------
-- Token -- Token
type FToken = String type FToken = String
@@ -72,7 +71,7 @@ data FSymbol
type FName = NameProfile AbsGFCC.CId type FName = NameProfile AbsGFCC.CId
isCoercionF :: FName -> Bool isCoercionF :: FName -> Bool
isCoercionF (Name fun [Unify [0]]) = fun == AbsGFCC.CId "_" isCoercionF (Name fun [Unify [0]]) = fun == AbsGFCC.wildCId
isCoercionF _ = False isCoercionF _ = False
@@ -87,7 +86,7 @@ data FRule = FRule FName [FCat] FCat (Array FIndex (Array FPointPos FSymbol)
-- pretty-printing -- pretty-printing
instance Print AbsGFCC.CId where instance Print AbsGFCC.CId where
prt (AbsGFCC.CId s) = s prt = AbsGFCC.prCId
instance Print FSymbol where instance Print FSymbol where
prt (FSymCat c l n) = "($" ++ prt n ++ "!" ++ prt l ++ ")" prt (FSymCat c l n) = "($" ++ prt n ++ "!" ++ prt l ++ ")"

View File

@@ -27,7 +27,7 @@ type Var = Ident.Ident
type Label = AbsGFC.Label type Label = AbsGFC.Label
anyVar :: Var anyVar :: Var
anyVar = Ident.wildIdent anyVar = Ident.identW
---------------------------------------------------------------------- ----------------------------------------------------------------------
-- * simple GFC -- * simple GFC

View File

@@ -84,12 +84,12 @@ file2gfcc f = do
g <- parseGrammar s g <- parseGrammar s
return $ toGFCC g return $ toGFCC g
linearize mgr lang = GF.GFCC.Linearize.linearize (gfcc mgr) (CId lang) linearize mgr lang = GF.GFCC.Linearize.linearize (gfcc mgr) (mkCId lang)
parse mgr lang cat s = parse mgr lang cat s =
case lookParser (gfcc mgr) (CId lang) of case lookParser (gfcc mgr) (mkCId lang) of
Nothing -> error "no parser" Nothing -> error "no parser"
Just pinfo -> case parseFCF "bottomup" pinfo (CId cat) (words s) of Just pinfo -> case parseFCF "bottomup" pinfo (mkCId cat) (words s) of
Ok x -> x Ok x -> x
Bad s -> error s Bad s -> error s
@@ -104,23 +104,20 @@ parseAllLang mgr cat s =
generateRandom mgr cat = do generateRandom mgr cat = do
gen <- newStdGen gen <- newStdGen
return $ genRandom gen (gfcc mgr) (CId cat) return $ genRandom gen (gfcc mgr) (mkCId cat)
generateAll mgr cat = generate (gfcc mgr) (CId cat) Nothing generateAll mgr cat = generate (gfcc mgr) (mkCId cat) Nothing
generateAllDepth mgr cat = generate (gfcc mgr) (CId cat) generateAllDepth mgr cat = generate (gfcc mgr) (mkCId cat)
readTree _ = pTree readTree _ = pTree
showTree = prExp showTree = prExp
prIdent :: CId -> String abstractName mgr = prCId (absname (gfcc mgr))
prIdent (CId s) = s
abstractName mgr = prIdent (absname (gfcc mgr)) languages mgr = [prCId l | l <- cncnames (gfcc mgr)]
languages mgr = [l | CId l <- cncnames (gfcc mgr)] categories mgr = [prCId c | c <- Map.keys (cats (abstract (gfcc mgr)))]
categories mgr = [c | CId c <- Map.keys (cats (abstract (gfcc mgr)))]
startCat mgr = lookStartCat (gfcc mgr) startCat mgr = lookStartCat (gfcc mgr)

View File

@@ -1,14 +1,15 @@
module GF.GFCC.CId ( module GF.GFCC.CId (CId(..), wildCId, mkCId, prCId) where
module GF.GFCC.Raw.AbsGFCCRaw,
prCId,
cId
) where
import GF.GFCC.Raw.AbsGFCCRaw (CId(CId)) import GF.Infra.PrintClass
import Data.ByteString.Char8 as BS
newtype CId = CId BS.ByteString deriving (Eq,Ord,Show)
wildCId :: CId
wildCId = CId (BS.singleton '_')
mkCId :: String -> CId
mkCId s = CId (BS.pack s)
prCId :: CId -> String prCId :: CId -> String
prCId (CId s) = s prCId (CId x) = BS.unpack x
cId :: String -> CId
cId = CId

View File

@@ -45,7 +45,7 @@ labelBoolErr ms iob = do
checkConcrete :: GFCC -> (CId,Concr) -> Err ((CId,Concr),Bool) checkConcrete :: GFCC -> (CId,Concr) -> Err ((CId,Concr),Bool)
checkConcrete gfcc (lang,cnc) = checkConcrete gfcc (lang,cnc) =
labelBoolErr ("happened in language " ++ printCId lang) $ do labelBoolErr ("happened in language " ++ prCId lang) $ do
(rs,bs) <- mapM checkl (Map.assocs (lins cnc)) >>= return . unzip (rs,bs) <- mapM checkl (Map.assocs (lins cnc)) >>= return . unzip
return ((lang,cnc{lins = Map.fromAscList rs}),and bs) return ((lang,cnc{lins = Map.fromAscList rs}),and bs)
where where
@@ -53,7 +53,7 @@ checkConcrete gfcc (lang,cnc) =
checkLin :: GFCC -> CId -> (CId,Term) -> Err ((CId,Term),Bool) checkLin :: GFCC -> CId -> (CId,Term) -> Err ((CId,Term),Bool)
checkLin gfcc lang (f,t) = checkLin gfcc lang (f,t) =
labelBoolErr ("happened in function " ++ printCId f) $ do labelBoolErr ("happened in function " ++ prCId f) $ do
(t',b) <- checkTerm (lintype gfcc lang f) t --- $ inline gfcc lang t (t',b) <- checkTerm (lintype gfcc lang f) t --- $ inline gfcc lang t
return ((f,t'),b) return ((f,t'),b)

View File

@@ -1,6 +1,7 @@
module GF.GFCC.DataGFCC where module GF.GFCC.DataGFCC where
import GF.GFCC.CId import GF.GFCC.CId
import GF.Infra.PrintClass(prt)
import GF.Infra.CompactPrint import GF.Infra.CompactPrint
import GF.Text.UTF8 import GF.Text.UTF8
import GF.Formalism.FCFG import GF.Formalism.FCFG
@@ -90,21 +91,17 @@ data Equation =
statGFCC :: GFCC -> String statGFCC :: GFCC -> String
statGFCC gfcc = unlines [ statGFCC gfcc = unlines [
"Abstract\t" ++ pr (absname gfcc), "Abstract\t" ++ prt (absname gfcc),
"Concretes\t" ++ unwords (lmap pr (cncnames gfcc)), "Concretes\t" ++ unwords (lmap prt (cncnames gfcc)),
"Categories\t" ++ unwords (lmap pr (keys (cats (abstract gfcc)))) "Categories\t" ++ unwords (lmap prt (keys (cats (abstract gfcc))))
] ]
where pr (CId s) = s
printCId :: CId -> String
printCId (CId s) = s
-- merge two GFCCs; fails is differens absnames; priority to second arg -- merge two GFCCs; fails is differens absnames; priority to second arg
unionGFCC :: GFCC -> GFCC -> GFCC unionGFCC :: GFCC -> GFCC -> GFCC
unionGFCC one two = case absname one of unionGFCC one two = case absname one of
CId "" -> two -- extending empty grammar n | n == wildCId -> two -- extending empty grammar
n | n == absname two -> one { -- extending grammar with same abstract | n == absname two -> one { -- extending grammar with same abstract
concretes = Data.Map.union (concretes two) (concretes one), concretes = Data.Map.union (concretes two) (concretes one),
cncnames = Data.List.union (cncnames two) (cncnames one) cncnames = Data.List.union (cncnames two) (cncnames one)
} }
@@ -112,7 +109,7 @@ unionGFCC one two = case absname one of
emptyGFCC :: GFCC emptyGFCC :: GFCC
emptyGFCC = GFCC { emptyGFCC = GFCC {
absname = CId "", absname = wildCId,
cncnames = [] , cncnames = [] ,
gflags = empty, gflags = empty,
abstract = error "empty grammar, no abstract", abstract = error "empty grammar, no abstract",

View File

@@ -36,8 +36,8 @@ genRandom gen gfcc cat = genTrees (randomRs (0.0, 1.0 :: Double) gen) cat where
(genTrees ds2 cat) -- else (drop k ds) (genTrees ds2 cat) -- else (drop k ds)
genTree rs = gett rs where genTree rs = gett rs where
gett ds (CId "String") = (tree (AS "foo") [], 1) gett ds cid | cid == mkCId "String" = (tree (AS "foo") [], 1)
gett ds (CId "Int") = (tree (AI 12345) [], 1) gett ds cid | cid == mkCId "Int" = (tree (AI 12345) [], 1)
gett [] _ = (tree (AS "TIMEOUT") [], 1) ---- gett [] _ = (tree (AS "TIMEOUT") [], 1) ----
gett ds cat = case fns cat of gett ds cat = case fns cat of
[] -> (tree (AM 0) [],1) [] -> (tree (AM 0) [],1)

View File

@@ -3,6 +3,7 @@ module GF.GFCC.Linearize where
import GF.GFCC.Macros import GF.GFCC.Macros
import GF.GFCC.DataGFCC import GF.GFCC.DataGFCC
import GF.GFCC.CId import GF.GFCC.CId
import GF.Infra.PrintClass
import Data.Map import Data.Map
import Data.List import Data.List
@@ -35,7 +36,7 @@ linExp mcfg lang tree@(DTr xs at trees) =
--- [C lst, kks (show i), C size] where --- [C lst, kks (show i), C size] where
--- lst = mod (fromInteger i) 10 ; size = if i < 10 then 0 else 1 --- lst = mod (fromInteger i) 10 ; size = if i < 10 then 0 else 1
AF d -> R [kks (show d)] AF d -> R [kks (show d)]
AV x -> TM (prCId x) AV x -> TM (prt x)
AM i -> TM (show i) AM i -> TM (show i)
where where
lin = linExp mcfg lang lin = linExp mcfg lang
@@ -44,8 +45,8 @@ linExp mcfg lang tree@(DTr xs at trees) =
addB t addB t
| Data.List.null xs = t | Data.List.null xs = t
| otherwise = case t of | otherwise = case t of
R ts -> R $ ts ++ (Data.List.map (kks . prCId) xs) R ts -> R $ ts ++ (Data.List.map (kks . prt) xs)
TM s -> R $ t : (Data.List.map (kks . prCId) xs) TM s -> R $ t : (Data.List.map (kks . prt) xs)
compute :: GFCC -> CId -> [Term] -> Term -> Term compute :: GFCC -> CId -> [Term] -> Term -> Term
compute mcfg lang args = comp where compute mcfg lang args = comp where

View File

@@ -4,7 +4,7 @@ import GF.GFCC.CId
import GF.GFCC.DataGFCC import GF.GFCC.DataGFCC
import GF.Formalism.FCFG (FGrammar) import GF.Formalism.FCFG (FGrammar)
import GF.Parsing.FCFG.PInfo (FCFPInfo, fcfPInfoToFGrammar) import GF.Parsing.FCFG.PInfo (FCFPInfo, fcfPInfoToFGrammar)
----import GF.GFCC.PrintGFCC import GF.Infra.PrintClass
import Control.Monad import Control.Monad
import Data.Map import Data.Map
import Data.Maybe import Data.Maybe
@@ -39,7 +39,7 @@ lookFCFG :: GFCC -> CId -> Maybe FGrammar
lookFCFG gfcc lang = fmap fcfPInfoToFGrammar $ lookParser gfcc lang lookFCFG gfcc lang = fmap fcfPInfoToFGrammar $ lookParser gfcc lang
lookStartCat :: GFCC -> String lookStartCat :: GFCC -> String
lookStartCat gfcc = fromMaybe "S" $ msum $ Data.List.map (Data.Map.lookup (CId "startcat")) lookStartCat gfcc = fromMaybe "S" $ msum $ Data.List.map (Data.Map.lookup (mkCId "startcat"))
[gflags gfcc, aflags (abstract gfcc)] [gflags gfcc, aflags (abstract gfcc)]
lookGlobalFlag :: GFCC -> CId -> String lookGlobalFlag :: GFCC -> CId -> String
@@ -87,12 +87,6 @@ contextLength :: Type -> Int
contextLength ty = case ty of contextLength ty = case ty of
DTyp hyps _ _ -> length hyps DTyp hyps _ _ -> length hyps
cid :: String -> CId
cid = CId
wildCId :: CId
wildCId = cid "_"
exp0 :: Exp exp0 :: Exp
exp0 = tree (AM 0) [] exp0 = tree (AM 0) []
@@ -100,7 +94,7 @@ primNotion :: Exp
primNotion = EEq [] primNotion = EEq []
term0 :: CId -> Term term0 :: CId -> Term
term0 = TM . prCId term0 = TM . prt
tm0 :: Term tm0 :: Term
tm0 = TM "?" tm0 = TM "?"

View File

@@ -75,7 +75,7 @@ addSubexpConsts tree cnc = cnc {
W s t -> W s (recomp f t) W s t -> W s (recomp f t)
P t p -> P (recomp f t) (recomp f p) P t p -> P (recomp f t) (recomp f p)
_ -> t _ -> t
fid n = CId $ "_" ++ show n fid n = mkCId $ "_" ++ show n
rec field = Map.fromAscList [(f,recomp f trm) | (f,trm) <- Map.assocs (field cnc)] rec field = Map.fromAscList [(f,recomp f trm) | (f,trm) <- Map.assocs (field cnc)]

View File

@@ -1,14 +1,11 @@
module GF.GFCC.Raw.AbsGFCCRaw where module GF.GFCC.Raw.AbsGFCCRaw where
-- Haskell module generated by the BNF converter
newtype CId = CId String deriving (Eq,Ord,Show)
data Grammar = data Grammar =
Grm [RExp] Grm [RExp]
deriving (Eq,Ord,Show) deriving (Eq,Ord,Show)
data RExp = data RExp =
App CId [RExp] App String [RExp]
| AInt Integer | AInt Integer
| AStr String | AStr String
| AFlt Double | AFlt Double

View File

@@ -1,8 +1,10 @@
module GF.GFCC.Raw.ConvertGFCC (toGFCC,fromGFCC) where module GF.GFCC.Raw.ConvertGFCC (toGFCC,fromGFCC) where
import GF.GFCC.CId
import GF.GFCC.DataGFCC import GF.GFCC.DataGFCC
import GF.GFCC.Raw.AbsGFCCRaw import GF.GFCC.Raw.AbsGFCCRaw
import GF.Infra.PrintClass
import GF.Data.Assoc import GF.Data.Assoc
import GF.Formalism.FCFG import GF.Formalism.FCFG
import GF.Formalism.Utilities (NameProfile(..), Profile(..), SyntaxForest(..)) import GF.Formalism.Utilities (NameProfile(..), Profile(..), SyntaxForest(..))
@@ -18,29 +20,29 @@ pgfMajorVersion, pgfMinorVersion :: Integer
toGFCC :: Grammar -> GFCC toGFCC :: Grammar -> GFCC
toGFCC (Grm [ toGFCC (Grm [
App (CId "pgf") (AInt v1 : AInt v2 : App a []:cs), App "pgf" (AInt v1 : AInt v2 : App a []:cs),
App (CId "flags") gfs, App "flags" gfs,
ab@( ab@(
App (CId "abstract") [ App "abstract" [
App (CId "fun") fs, App "fun" fs,
App (CId "cat") cts App "cat" cts
]), ]),
App (CId "concrete") ccs App "concrete" ccs
]) = GFCC { ]) = GFCC {
absname = a, absname = mkCId a,
cncnames = [c | App c [] <- cs], cncnames = [mkCId c | App c [] <- cs],
gflags = fromAscList [(f,v) | App f [AStr v] <- gfs], gflags = fromAscList [(mkCId f,v) | App f [AStr v] <- gfs],
abstract = abstract =
let let
aflags = fromAscList [(f,v) | App f [AStr v] <- gfs] aflags = fromAscList [(mkCId f,v) | App f [AStr v] <- gfs]
lfuns = [(f,(toType typ,toExp def)) | App f [typ, def] <- fs] lfuns = [(mkCId f,(toType typ,toExp def)) | App f [typ, def] <- fs]
funs = fromAscList lfuns funs = fromAscList lfuns
lcats = [(c, Prelude.map toHypo hyps) | App c hyps <- cts] lcats = [(mkCId c, Prelude.map toHypo hyps) | App c hyps <- cts]
cats = fromAscList lcats cats = fromAscList lcats
catfuns = fromAscList catfuns = fromAscList
[(cat,[f | (f, (DTyp _ c _,_)) <- lfuns, c==cat]) | (cat,_) <- lcats] [(cat,[f | (f, (DTyp _ c _,_)) <- lfuns, c==cat]) | (cat,_) <- lcats]
in Abstr aflags funs cats catfuns, in Abstr aflags funs cats catfuns,
concretes = fromAscList [(lang, toConcr ts) | App lang ts <- ccs] concretes = fromAscList [(mkCId lang, toConcr ts) | App lang ts <- ccs]
} }
where where
@@ -57,71 +59,71 @@ toConcr = foldl add (Concr {
}) })
where where
add :: Concr -> RExp -> Concr add :: Concr -> RExp -> Concr
add cnc (App (CId "flags") ts) = cnc { cflags = fromAscList [(f,v) | App f [AStr v] <- ts] } add cnc (App "flags" ts) = cnc { cflags = fromAscList [(mkCId f,v) | App f [AStr v] <- ts] }
add cnc (App (CId "lin") ts) = cnc { lins = mkTermMap ts } add cnc (App "lin" ts) = cnc { lins = mkTermMap ts }
add cnc (App (CId "oper") ts) = cnc { opers = mkTermMap ts } add cnc (App "oper" ts) = cnc { opers = mkTermMap ts }
add cnc (App (CId "lincat") ts) = cnc { lincats = mkTermMap ts } add cnc (App "lincat" ts) = cnc { lincats = mkTermMap ts }
add cnc (App (CId "lindef") ts) = cnc { lindefs = mkTermMap ts } add cnc (App "lindef" ts) = cnc { lindefs = mkTermMap ts }
add cnc (App (CId "printname") ts) = cnc { printnames = mkTermMap ts } add cnc (App "printname" ts) = cnc { printnames = mkTermMap ts }
add cnc (App (CId "param") ts) = cnc { paramlincats = mkTermMap ts } add cnc (App "param" ts) = cnc { paramlincats = mkTermMap ts }
add cnc (App (CId "parser") ts) = cnc { parser = Just (toPInfo ts) } add cnc (App "parser" ts) = cnc { parser = Just (toPInfo ts) }
toPInfo :: [RExp] -> FCFPInfo toPInfo :: [RExp] -> FCFPInfo
toPInfo [App (CId "rules") rs, App (CId "startupcats") cs] = buildFCFPInfo (rules, cats) toPInfo [App "rules" rs, App "startupcats" cs] = buildFCFPInfo (rules, cats)
where where
rules = lmap toFRule rs rules = lmap toFRule rs
cats = fromList [(c, lmap expToInt fs) | App c fs <- cs] cats = fromList [(mkCId c, lmap expToInt fs) | App c fs <- cs]
toFRule :: RExp -> FRule toFRule :: RExp -> FRule
toFRule (App (CId "rule") toFRule (App "rule"
[n, [n,
App (CId "cats") (rt:at), App "cats" (rt:at),
App (CId "R") ls]) = FRule name args res lins App "R" ls]) = FRule name args res lins
where where
name = toFName n name = toFName n
args = lmap expToInt at args = lmap expToInt at
res = expToInt rt res = expToInt rt
lins = mkArray [mkArray [toSymbol s | s <- l] | App (CId "S") l <- ls] lins = mkArray [mkArray [toSymbol s | s <- l] | App "S" l <- ls]
toFName :: RExp -> FName toFName :: RExp -> FName
toFName (App (CId "_A") [x]) = Name (CId "_") [Unify [expToInt x]] toFName (App "_A" [x]) = Name wildCId [Unify [expToInt x]]
toFName (App f ts) = Name f (lmap toProfile ts) toFName (App f ts) = Name (mkCId f) (lmap toProfile ts)
where where
toProfile :: RExp -> Profile (SyntaxForest CId) toProfile :: RExp -> Profile (SyntaxForest CId)
toProfile AMet = Unify [] toProfile AMet = Unify []
toProfile (App (CId "_A") [t]) = Unify [expToInt t] toProfile (App "_A" [t]) = Unify [expToInt t]
toProfile (App (CId "_U") ts) = Unify [expToInt t | App (CId "_A") [t] <- ts] toProfile (App "_U" ts) = Unify [expToInt t | App "_A" [t] <- ts]
toProfile t = Constant (toSyntaxForest t) toProfile t = Constant (toSyntaxForest t)
toSyntaxForest :: RExp -> SyntaxForest CId toSyntaxForest :: RExp -> SyntaxForest CId
toSyntaxForest AMet = FMeta toSyntaxForest AMet = FMeta
toSyntaxForest (App n ts) = FNode n [lmap toSyntaxForest ts] toSyntaxForest (App n ts) = FNode (mkCId n) [lmap toSyntaxForest ts]
toSyntaxForest (AStr s) = FString s toSyntaxForest (AStr s) = FString s
toSyntaxForest (AInt i) = FInt i toSyntaxForest (AInt i) = FInt i
toSyntaxForest (AFlt f) = FFloat f toSyntaxForest (AFlt f) = FFloat f
toSymbol :: RExp -> FSymbol toSymbol :: RExp -> FSymbol
toSymbol (App (CId "P") [c,n,l]) = FSymCat (expToInt c) (expToInt l) (expToInt n) toSymbol (App "P" [c,n,l]) = FSymCat (expToInt c) (expToInt l) (expToInt n)
toSymbol (AStr t) = FSymTok t toSymbol (AStr t) = FSymTok t
toType :: RExp -> Type toType :: RExp -> Type
toType e = case e of toType e = case e of
App cat [App (CId "H") hypos, App (CId "X") exps] -> App cat [App "H" hypos, App "X" exps] ->
DTyp (lmap toHypo hypos) cat (lmap toExp exps) DTyp (lmap toHypo hypos) (mkCId cat) (lmap toExp exps)
_ -> error $ "type " ++ show e _ -> error $ "type " ++ show e
toHypo :: RExp -> Hypo toHypo :: RExp -> Hypo
toHypo e = case e of toHypo e = case e of
App x [typ] -> Hyp x (toType typ) App x [typ] -> Hyp (mkCId x) (toType typ)
_ -> error $ "hypo " ++ show e _ -> error $ "hypo " ++ show e
toExp :: RExp -> Exp toExp :: RExp -> Exp
toExp e = case e of toExp e = case e of
App (CId "App") [App fun [], App (CId "B") xs, App (CId "X") exps] -> App "App" [App fun [], App "B" xs, App "X" exps] ->
DTr [x | App x [] <- xs] (AC fun) (lmap toExp exps) DTr [mkCId x | App x [] <- xs] (AC (mkCId fun)) (lmap toExp exps)
App (CId "Eq") eqs -> App "Eq" eqs ->
EEq [Equ (lmap toExp ps) (toExp v) | App (CId "E") (v:ps) <- eqs] EEq [Equ (lmap toExp ps) (toExp v) | App "E" (v:ps) <- eqs]
App (CId "Var") [App i []] -> DTr [] (AV i) [] App "Var" [App i []] -> DTr [] (AV (mkCId i)) []
AMet -> DTr [] (AM 0) [] AMet -> DTr [] (AM 0) []
AInt i -> DTr [] (AI i) [] AInt i -> DTr [] (AI i) []
AFlt i -> DTr [] (AF i) [] AFlt i -> DTr [] (AF i) []
@@ -130,14 +132,14 @@ toExp e = case e of
toTerm :: RExp -> Term toTerm :: RExp -> Term
toTerm e = case e of toTerm e = case e of
App (CId "R") es -> R (lmap toTerm es) App "R" es -> R (lmap toTerm es)
App (CId "S") es -> S (lmap toTerm es) App "S" es -> S (lmap toTerm es)
App (CId "FV") es -> FV (lmap toTerm es) App "FV" es -> FV (lmap toTerm es)
App (CId "P") [e,v] -> P (toTerm e) (toTerm v) App "P" [e,v] -> P (toTerm e) (toTerm v)
App (CId "RP") [e,v] -> RP (toTerm e) (toTerm v) ---- App "RP" [e,v] -> RP (toTerm e) (toTerm v) ----
App (CId "W") [AStr s,v] -> W s (toTerm v) App "W" [AStr s,v] -> W s (toTerm v)
App (CId "A") [AInt i] -> V (fromInteger i) App "A" [AInt i] -> V (fromInteger i)
App f [] -> F f App f [] -> F (mkCId f)
AInt i -> C (fromInteger i) AInt i -> C (fromInteger i)
AMet -> TM "?" AMet -> TM "?"
AStr s -> K (KS s) ---- AStr s -> K (KS s) ----
@@ -149,129 +151,124 @@ toTerm e = case e of
fromGFCC :: GFCC -> Grammar fromGFCC :: GFCC -> Grammar
fromGFCC gfcc0 = Grm [ fromGFCC gfcc0 = Grm [
app "pgf" (AInt pgfMajorVersion:AInt pgfMinorVersion App "pgf" (AInt pgfMajorVersion:AInt pgfMinorVersion
: App (absname gfcc) [] : lmap (flip App []) (cncnames gfcc)), : App (prCId (absname gfcc)) [] : lmap (flip App [] . prCId) (cncnames gfcc)),
app "flags" [App f [AStr v] | (f,v) <- toList (gflags gfcc `union` aflags agfcc)], App "flags" [App (prCId f) [AStr v] | (f,v) <- toList (gflags gfcc `union` aflags agfcc)],
app "abstract" [ App "abstract" [
app "fun" [App f [fromType t,fromExp d] | (f,(t,d)) <- toList (funs agfcc)], App "fun" [App (prCId f) [fromType t,fromExp d] | (f,(t,d)) <- toList (funs agfcc)],
app "cat" [App f (lmap fromHypo hs) | (f,hs) <- toList (cats agfcc)] App "cat" [App (prCId f) (lmap fromHypo hs) | (f,hs) <- toList (cats agfcc)]
], ],
app "concrete" [App lang (fromConcrete c) | (lang,c) <- toList (concretes gfcc)] App "concrete" [App (prCId lang) (fromConcrete c) | (lang,c) <- toList (concretes gfcc)]
] ]
where where
gfcc = utf8GFCC gfcc0 gfcc = utf8GFCC gfcc0
app s = App (CId s)
agfcc = abstract gfcc agfcc = abstract gfcc
fromConcrete cnc = [ fromConcrete cnc = [
app "flags" [App f [AStr v] | (f,v) <- toList (cflags cnc)], App "flags" [App (prCId f) [AStr v] | (f,v) <- toList (cflags cnc)],
app "lin" [App f [fromTerm v] | (f,v) <- toList (lins cnc)], App "lin" [App (prCId f) [fromTerm v] | (f,v) <- toList (lins cnc)],
app "oper" [App f [fromTerm v] | (f,v) <- toList (opers cnc)], App "oper" [App (prCId f) [fromTerm v] | (f,v) <- toList (opers cnc)],
app "lincat" [App f [fromTerm v] | (f,v) <- toList (lincats cnc)], App "lincat" [App (prCId f) [fromTerm v] | (f,v) <- toList (lincats cnc)],
app "lindef" [App f [fromTerm v] | (f,v) <- toList (lindefs cnc)], App "lindef" [App (prCId f) [fromTerm v] | (f,v) <- toList (lindefs cnc)],
app "printname" [App f [fromTerm v] | (f,v) <- toList (printnames cnc)], App "printname" [App (prCId f) [fromTerm v] | (f,v) <- toList (printnames cnc)],
app "param" [App f [fromTerm v] | (f,v) <- toList (paramlincats cnc)] App "param" [App (prCId f) [fromTerm v] | (f,v) <- toList (paramlincats cnc)]
] ++ maybe [] (\p -> [fromPInfo p]) (parser cnc) ] ++ maybe [] (\p -> [fromPInfo p]) (parser cnc)
fromType :: Type -> RExp fromType :: Type -> RExp
fromType e = case e of fromType e = case e of
DTyp hypos cat exps -> DTyp hypos cat exps ->
App cat [ App (prCId cat) [
App (CId "H") (lmap fromHypo hypos), App "H" (lmap fromHypo hypos),
App (CId "X") (lmap fromExp exps)] App "X" (lmap fromExp exps)]
fromHypo :: Hypo -> RExp fromHypo :: Hypo -> RExp
fromHypo e = case e of fromHypo e = case e of
Hyp x typ -> App x [fromType typ] Hyp x typ -> App (prCId x) [fromType typ]
fromExp :: Exp -> RExp fromExp :: Exp -> RExp
fromExp e = case e of fromExp e = case e of
DTr xs (AC fun) exps -> DTr xs (AC fun) exps ->
App (CId "App") [App fun [], App (CId "B") (lmap (flip App []) xs), App (CId "X") (lmap fromExp exps)] App "App" [App (prCId fun) [], App "B" (lmap (flip App [] . prCId) xs), App "X" (lmap fromExp exps)]
DTr [] (AV x) [] -> App (CId "Var") [App x []] DTr [] (AV x) [] -> App "Var" [App (prCId x) []]
DTr [] (AS s) [] -> AStr s DTr [] (AS s) [] -> AStr s
DTr [] (AF d) [] -> AFlt d DTr [] (AF d) [] -> AFlt d
DTr [] (AI i) [] -> AInt (toInteger i) DTr [] (AI i) [] -> AInt (toInteger i)
DTr [] (AM _) [] -> AMet ---- DTr [] (AM _) [] -> AMet ----
EEq eqs -> EEq eqs ->
App (CId "Eq") [App (CId "E") (lmap fromExp (v:ps)) | Equ ps v <- eqs] App "Eq" [App "E" (lmap fromExp (v:ps)) | Equ ps v <- eqs]
_ -> error $ "exp " ++ show e _ -> error $ "exp " ++ show e
fromTerm :: Term -> RExp fromTerm :: Term -> RExp
fromTerm e = case e of fromTerm e = case e of
R es -> app "R" (lmap fromTerm es) R es -> App "R" (lmap fromTerm es)
S es -> app "S" (lmap fromTerm es) S es -> App "S" (lmap fromTerm es)
FV es -> app "FV" (lmap fromTerm es) FV es -> App "FV" (lmap fromTerm es)
P e v -> app "P" [fromTerm e, fromTerm v] P e v -> App "P" [fromTerm e, fromTerm v]
RP e v -> app "RP" [fromTerm e, fromTerm v] ---- RP e v -> App "RP" [fromTerm e, fromTerm v] ----
W s v -> app "W" [AStr s, fromTerm v] W s v -> App "W" [AStr s, fromTerm v]
C i -> AInt (toInteger i) C i -> AInt (toInteger i)
TM _ -> AMet TM _ -> AMet
F f -> App f [] F f -> App (prCId f) []
V i -> App (CId "A") [AInt (toInteger i)] V i -> App "A" [AInt (toInteger i)]
K (KS s) -> AStr s ---- K (KS s) -> AStr s ----
K (KP d vs) -> app "FV" (str d : [str v | Var v _ <- vs]) ---- K (KP d vs) -> App "FV" (str d : [str v | Var v _ <- vs]) ----
where where
app = App . CId str v = App "S" (lmap AStr v)
str v = app "S" (lmap AStr v)
-- ** Parsing info -- ** Parsing info
fromPInfo :: FCFPInfo -> RExp fromPInfo :: FCFPInfo -> RExp
fromPInfo p = app "parser" [ fromPInfo p = App "parser" [
app "rules" [fromFRule rule | rule <- Array.elems (allRules p)], App "rules" [fromFRule rule | rule <- Array.elems (allRules p)],
app "startupcats" [App f (lmap intToExp cs) | (f,cs) <- toList (startupCats p)] App "startupcats" [App (prCId f) (lmap intToExp cs) | (f,cs) <- toList (startupCats p)]
] ]
fromFRule :: FRule -> RExp fromFRule :: FRule -> RExp
fromFRule (FRule n args res lins) = fromFRule (FRule n args res lins) =
app "rule" [fromFName n, App "rule" [fromFName n,
app "cats" (intToExp res:lmap intToExp args), App "cats" (intToExp res:lmap intToExp args),
app "R" [app "S" [fromSymbol s | s <- Array.elems l] | l <- Array.elems lins] App "R" [App "S" [fromSymbol s | s <- Array.elems l] | l <- Array.elems lins]
] ]
fromFName :: FName -> RExp fromFName :: FName -> RExp
fromFName n = case n of fromFName n = case n of
Name (CId "_") [p] -> fromProfile p Name f ps | f == wildCId -> fromProfile (head ps)
Name f ps -> App f (lmap fromProfile ps) | otherwise -> App (prCId f) (lmap fromProfile ps)
where where
fromProfile :: Profile (SyntaxForest CId) -> RExp fromProfile :: Profile (SyntaxForest CId) -> RExp
fromProfile (Unify []) = AMet fromProfile (Unify []) = AMet
fromProfile (Unify [x]) = daughter x fromProfile (Unify [x]) = daughter x
fromProfile (Unify args) = app "_U" (lmap daughter args) fromProfile (Unify args) = App "_U" (lmap daughter args)
fromProfile (Constant forest) = fromSyntaxForest forest fromProfile (Constant forest) = fromSyntaxForest forest
daughter n = app "_A" [intToExp n] daughter n = App "_A" [intToExp n]
fromSyntaxForest :: SyntaxForest CId -> RExp fromSyntaxForest :: SyntaxForest CId -> RExp
fromSyntaxForest FMeta = AMet fromSyntaxForest FMeta = AMet
-- FIXME: is there always just one element here? -- FIXME: is there always just one element here?
fromSyntaxForest (FNode n [args]) = App n (lmap fromSyntaxForest args) fromSyntaxForest (FNode n [args]) = App (prCId n) (lmap fromSyntaxForest args)
fromSyntaxForest (FString s) = AStr s fromSyntaxForest (FString s) = AStr s
fromSyntaxForest (FInt i) = AInt i fromSyntaxForest (FInt i) = AInt i
fromSyntaxForest (FFloat f) = AFlt f fromSyntaxForest (FFloat f) = AFlt f
fromSymbol :: FSymbol -> RExp fromSymbol :: FSymbol -> RExp
fromSymbol (FSymCat c l n) = app "P" [intToExp c, intToExp n, intToExp l] fromSymbol (FSymCat c l n) = App "P" [intToExp c, intToExp n, intToExp l]
fromSymbol (FSymTok t) = AStr t fromSymbol (FSymTok t) = AStr t
-- ** Utilities -- ** Utilities
mkTermMap :: [RExp] -> Map CId Term mkTermMap :: [RExp] -> Map CId Term
mkTermMap ts = fromAscList [(f,toTerm v) | App f [v] <- ts] mkTermMap ts = fromAscList [(mkCId f,toTerm v) | App f [v] <- ts]
app :: String -> [RExp] -> RExp
app = App . CId
mkArray :: [a] -> Array.Array Int a mkArray :: [a] -> Array.Array Int a
mkArray xs = Array.listArray (0, length xs - 1) xs mkArray xs = Array.listArray (0, length xs - 1) xs
expToInt :: Integral a => RExp -> a expToInt :: Integral a => RExp -> a
expToInt (App (CId "neg") [AInt i]) = fromIntegral (negate i) expToInt (App "neg" [AInt i]) = fromIntegral (negate i)
expToInt (AInt i) = fromIntegral i expToInt (AInt i) = fromIntegral i
expToStr :: RExp -> String expToStr :: RExp -> String
expToStr (AStr s) = s expToStr (AStr s) = s
intToExp :: Integral a => a -> RExp intToExp :: Integral a => a -> RExp
intToExp x | x < 0 = App (CId "neg") [AInt (fromIntegral (negate x))] intToExp x | x < 0 = App "neg" [AInt (fromIntegral (negate x))]
| otherwise = AInt (fromIntegral x) | otherwise = AInt (fromIntegral x)

View File

@@ -1,9 +1,11 @@
module GF.GFCC.Raw.ParGFCCRaw (parseGrammar) where module GF.GFCC.Raw.ParGFCCRaw (parseGrammar) where
import GF.GFCC.CId
import GF.GFCC.Raw.AbsGFCCRaw import GF.GFCC.Raw.AbsGFCCRaw
import Control.Monad import Control.Monad
import Data.Char import Data.Char
import qualified Data.ByteString.Char8 as BS
parseGrammar :: String -> IO Grammar parseGrammar :: String -> IO Grammar
parseGrammar s = case runP pGrammar s of parseGrammar s = case runP pGrammar s of
@@ -27,7 +29,7 @@ pTerm n = skipSpaces >> (pParen <++ pApp <++ pNum <++ pStr <++ pMeta)
<++ <++
return (AInt (read x))) return (AInt (read x)))
pMeta = char '?' >> return AMet pMeta = char '?' >> return AMet
pIdent = liftM CId $ liftM2 (:) (satisfy isIdentFirst) (munch isIdentRest) pIdent = liftM2 (:) (satisfy isIdentFirst) (munch isIdentRest)
isIdentFirst c = c == '_' || isAlpha c isIdentFirst c = c == '_' || isAlpha c
isIdentRest c = c == '_' || c == '\'' || isAlphaNum c isIdentRest c = c == '_' || c == '\'' || isAlphaNum c

View File

@@ -1,9 +1,11 @@
module GF.GFCC.Raw.PrintGFCCRaw (printTree) where module GF.GFCC.Raw.PrintGFCCRaw (printTree) where
import GF.GFCC.CId
import GF.GFCC.Raw.AbsGFCCRaw import GF.GFCC.Raw.AbsGFCCRaw
import Data.List (intersperse) import Data.List (intersperse)
import Numeric (showFFloat) import Numeric (showFFloat)
import qualified Data.ByteString.Char8 as BS
printTree :: Grammar -> String printTree :: Grammar -> String
printTree g = prGrammar g "" printTree g = prGrammar g ""
@@ -12,8 +14,8 @@ prGrammar :: Grammar -> ShowS
prGrammar (Grm xs) = prRExpList xs prGrammar (Grm xs) = prRExpList xs
prRExp :: Int -> RExp -> ShowS prRExp :: Int -> RExp -> ShowS
prRExp _ (App x []) = prCId x prRExp _ (App x []) = showString x
prRExp n (App x xs) = p (prCId x . showChar ' ' . prRExpList xs) prRExp n (App x xs) = p (showString x . showChar ' ' . prRExpList xs)
where p s = if n == 0 then s else showChar '(' . s . showChar ')' where p s = if n == 0 then s else showChar '(' . s . showChar ')'
prRExp _ (AInt x) = shows x prRExp _ (AInt x) = shows x
prRExp _ (AStr x) = showChar '"' . concatS (map mkEsc x) . showChar '"' prRExp _ (AStr x) = showChar '"' . concatS (map mkEsc x) . showChar '"'
@@ -29,8 +31,5 @@ mkEsc s = case s of
prRExpList :: [RExp] -> ShowS prRExpList :: [RExp] -> ShowS
prRExpList = concatS . intersperse (showChar ' ') . map (prRExp 1) prRExpList = concatS . intersperse (showChar ' ') . map (prRExp 1)
prCId :: CId -> ShowS
prCId (CId x) = showString x
concatS :: [ShowS] -> ShowS concatS :: [ShowS] -> ShowS
concatS = foldr (.) id concatS = foldr (.) id

View File

@@ -15,12 +15,13 @@
module GF.Grammar.AppPredefined (isInPredefined, typPredefined, appPredefined module GF.Grammar.AppPredefined (isInPredefined, typPredefined, appPredefined
) where ) where
import GF.Data.Operations
import GF.Grammar.Grammar
import GF.Infra.Ident import GF.Infra.Ident
import GF.Data.Operations
import GF.Grammar.Predef
import GF.Grammar.Grammar
import GF.Grammar.Macros import GF.Grammar.Macros
import GF.Grammar.PrGrammar (prt,prt_,prtBad) import GF.Grammar.PrGrammar (prt,prt_,prtBad)
---- import PGrammar (pTrm) import qualified Data.ByteString.Char8 as BS
-- predefined function type signatures and definitions. AR 12/3/2003. -- predefined function type signatures and definitions. AR 12/3/2003.
@@ -28,75 +29,77 @@ isInPredefined :: Ident -> Bool
isInPredefined = err (const True) (const False) . typPredefined isInPredefined = err (const True) (const False) . typPredefined
typPredefined :: Ident -> Err Type typPredefined :: Ident -> Err Type
typPredefined c@(IC f) = case f of typPredefined f
"Int" -> return typePType | f == cInt = return typePType
"Float" -> return typePType | f == cFloat = return typePType
"Error" -> return typeType | f == cErrorType = return typeType
"Ints" -> return $ mkFunType [cnPredef "Int"] typePType | f == cInts = return $ mkFunType [typeInt] typePType
"PBool" -> return typePType | f == cPBool = return typePType
"error" -> return $ mkFunType [typeStr] (cnPredef "Error") -- non-can. of empty set | f == cError = return $ mkFunType [typeStr] typeError -- non-can. of empty set
"PFalse" -> return $ cnPredef "PBool" | f == cPFalse = return $ typePBool
"PTrue" -> return $ cnPredef "PBool" | f == cPTrue = return $ typePBool
"dp" -> return $ mkFunType [cnPredef "Int",typeTok] typeTok | f == cDp = return $ mkFunType [typeInt,typeTok] typeTok
"drop" -> return $ mkFunType [cnPredef "Int",typeTok] typeTok | f == cDrop = return $ mkFunType [typeInt,typeTok] typeTok
"eqInt" -> return $ mkFunType [cnPredef "Int",cnPredef "Int"] (cnPredef "PBool") | f == cEqInt = return $ mkFunType [typeInt,typeInt] typePBool
"lessInt"-> return $ mkFunType [cnPredef "Int",cnPredef "Int"] (cnPredef "PBool") | f == cLessInt = return $ mkFunType [typeInt,typeInt] typePBool
"eqStr" -> return $ mkFunType [typeTok,typeTok] (cnPredef "PBool") | f == cEqStr = return $ mkFunType [typeTok,typeTok] typePBool
"length" -> return $ mkFunType [typeTok] (cnPredef "Int") | f == cLength = return $ mkFunType [typeTok] typeInt
"occur" -> return $ mkFunType [typeTok,typeTok] (cnPredef "PBool") | f == cOccur = return $ mkFunType [typeTok,typeTok] typePBool
"occurs" -> return $ mkFunType [typeTok,typeTok] (cnPredef "PBool") | f == cOccurs = return $ mkFunType [typeTok,typeTok] typePBool
"plus" -> return $ mkFunType [cnPredef "Int",cnPredef "Int"] (cnPredef "Int") | f == cPlus = return $ mkFunType [typeInt,typeInt] (typeInt)
---- "read" -> (P : Type) -> Tok -> P ---- "read" -> (P : Type) -> Tok -> P
"show" -> return $ mkProd -- (P : PType) -> P -> Tok | f == cShow = return $ mkProd -- (P : PType) -> P -> Tok
([(zIdent "P",typePType),(wildIdent,Vr (zIdent "P"))],typeStr,[]) ([(varP,typePType),(identW,Vr varP)],typeStr,[])
"toStr" -> return $ mkProd -- (L : Type) -> L -> Str | f == cToStr = return $ mkProd -- (L : Type) -> L -> Str
([(zIdent "L",typeType),(wildIdent,Vr (zIdent "L"))],typeStr,[]) ([(varL,typeType),(identW,Vr varL)],typeStr,[])
"mapStr" -> | f == cMapStr = return $ mkProd -- (L : Type) -> (Str -> Str) -> L -> L
let ty = zIdent "L" in ([(varL,typeType),(identW,mkFunType [typeStr] typeStr),(identW,Vr varL)],Vr varL,[])
return $ mkProd -- (L : Type) -> (Str -> Str) -> L -> L | f == cTake = return $ mkFunType [typeInt,typeTok] typeTok
([(ty,typeType),(wildIdent,mkFunType [typeStr] typeStr),(wildIdent,Vr ty)],Vr ty,[]) | f == cTk = return $ mkFunType [typeInt,typeTok] typeTok
"take" -> return $ mkFunType [cnPredef "Int",typeTok] typeTok | otherwise = prtBad "unknown in Predef:" f
"tk" -> return $ mkFunType [cnPredef "Int",typeTok] typeTok
_ -> prtBad "unknown in Predef:" c varL :: Ident
typPredefined c = prtBad "unknown in Predef:" c varL = identC (BS.pack "L")
varP :: Ident
varP = identC (BS.pack "P")
appPredefined :: Term -> Err (Term,Bool) appPredefined :: Term -> Err (Term,Bool)
appPredefined t = case t of appPredefined t = case t of
App f x0 -> do App f x0 -> do
(x,_) <- appPredefined x0 (x,_) <- appPredefined x0
case f of case f of
-- one-place functions -- one-place functions
Q (IC "Predef") (IC f) -> case (f, x) of Q mod f | mod == cPredef ->
("length", K s) -> retb $ EInt $ toInteger $ length s case x of
_ -> retb t ---- prtBad "cannot compute predefined" t (K s) | f == cLength -> retb $ EInt $ toInteger $ length s
_ -> retb t
-- two-place functions -- two-place functions
App (Q (IC "Predef") (IC f)) z0 -> do App (Q mod f) z0 | mod == cPredef -> do
(z,_) <- appPredefined z0 (z,_) <- appPredefined z0
case (f, norm z, norm x) of case (norm z, norm x) of
("drop", EInt i, K s) -> retb $ K (drop (fi i) s) (EInt i, K s) | f == cDrop -> retb $ K (drop (fi i) s)
("take", EInt i, K s) -> retb $ K (take (fi i) s) (EInt i, K s) | f == cTake -> retb $ K (take (fi i) s)
("tk", EInt i, K s) -> retb $ K (take (max 0 (length s - fi i)) s) (EInt i, K s) | f == cTk -> retb $ K (take (max 0 (length s - fi i)) s)
("dp", EInt i, K s) -> retb $ K (drop (max 0 (length s - fi i)) s) (EInt i, K s) | f == cDp -> retb $ K (drop (max 0 (length s - fi i)) s)
("eqStr",K s, K t) -> retb $ if s == t then predefTrue else predefFalse (K s, K t) | f == cEqStr -> retb $ if s == t then predefTrue else predefFalse
("occur",K s, K t) -> retb $ if substring s t then predefTrue else predefFalse (K s, K t) | f == cOccur -> retb $ if substring s t then predefTrue else predefFalse
("occurs",K s, K t) -> retb $ if any (flip elem t) s then predefTrue else predefFalse (K s, K t) | f == cOccurs -> retb $ if any (flip elem t) s then predefTrue else predefFalse
("eqInt",EInt i, EInt j) -> retb $ if i==j then predefTrue else predefFalse (EInt i, EInt j) | f == cEqInt -> retb $ if i==j then predefTrue else predefFalse
("lessInt",EInt i, EInt j) -> retb $ if i<j then predefTrue else predefFalse (EInt i, EInt j) | f == cLessInt -> retb $ if i<j then predefTrue else predefFalse
("plus", EInt i, EInt j) -> retb $ EInt $ i+j (EInt i, EInt j) | f == cPlus -> retb $ EInt $ i+j
("show", _, t) -> retb $ foldr C Empty $ map K $ words $ prt t (_, t) | f == cShow -> retb $ foldr C Empty $ map K $ words $ prt t
("read", _, K s) -> retb $ str2tag s --- because of K, only works for atomic tags (_, K s) | f == cRead -> retb $ Cn (identC (BS.pack s)) --- because of K, only works for atomic tags
("toStr", _, t) -> trm2str t >>= retb (_, t) | f == cToStr -> trm2str t >>= retb
_ -> retb t ---- prtBad "cannot compute predefined" t _ -> retb t ---- prtBad "cannot compute predefined" t
-- three-place functions -- three-place functions
App (App (Q (IC "Predef") (IC f)) z0) y0 -> do App (App (Q mod f) z0) y0 | mod == cPredef -> do
(y,_) <- appPredefined y0 (y,_) <- appPredefined y0
(z,_) <- appPredefined z0 (z,_) <- appPredefined z0
case (f, z, y, x) of case (z, y, x) of
("mapStr",ty,op,t) -> retf $ mapStr ty op t (ty,op,t) | f == cMapStr -> retf $ mapStr ty op t
_ -> retb t ---- prtBad "cannot compute predefined" t _ -> retb t ---- prtBad "cannot compute predefined" t
_ -> retb t ---- prtBad "cannot compute predefined" t _ -> retb t ---- prtBad "cannot compute predefined" t
@@ -112,19 +115,8 @@ appPredefined t = case t of
-- read makes variables into constants -- read makes variables into constants
str2tag :: String -> Term predefTrue = Q cPredef cPTrue
str2tag s = case s of predefFalse = Q cPredef cPFalse
---- '\'' : cs -> mkCn $ pTrm $ init cs
_ -> Cn $ IC s ---
where
mkCn t = case t of
Vr i -> Cn i
App c a -> App (mkCn c) (mkCn a)
_ -> t
predefTrue = Q (IC "Predef") (IC "PTrue")
predefFalse = Q (IC "Predef") (IC "PFalse")
substring :: String -> String -> Bool substring :: String -> String -> Bool
substring s t = case (s,t) of substring s t = case (s,t) of

View File

@@ -48,7 +48,8 @@ module GF.Grammar.Grammar (SourceGrammar,
Con, Con,
Trm, Trm,
wildPatt, wildPatt,
varLabel varLabel, tupleLabel, linLabel, theLinLabel,
ident2label, label2ident
) where ) where
import GF.Data.Str import GF.Data.Str
@@ -58,6 +59,8 @@ import GF.Infra.Modules
import GF.Data.Operations import GF.Data.Operations
import qualified Data.ByteString.Char8 as BS
-- | grammar as presented to the compiler -- | grammar as presented to the compiler
type SourceGrammar = MGrammar Ident Option Info type SourceGrammar = MGrammar Ident Option Info
@@ -119,7 +122,7 @@ data Term =
| Cn Ident -- ^ constant | Cn Ident -- ^ constant
| Con Ident -- ^ constructor | Con Ident -- ^ constructor
| EData -- ^ to mark in definition that a fun is a constructor | EData -- ^ to mark in definition that a fun is a constructor
| Sort String -- ^ basic type | Sort Ident -- ^ basic type
| EInt Integer -- ^ integer literal | EInt Integer -- ^ integer literal
| EFloat Double -- ^ floating point literal | EFloat Double -- ^ floating point literal
| K String -- ^ string literal or token: @\"foo\"@ | K String -- ^ string literal or token: @\"foo\"@
@@ -210,7 +213,7 @@ data TInfo =
-- | record label -- | record label
data Label = data Label =
LIdent String LIdent BS.ByteString
| LVar Int | LVar Int
deriving (Read, Show, Eq, Ord) deriving (Read, Show, Eq, Ord)
@@ -238,7 +241,21 @@ type Con = Ident ---
varLabel :: Int -> Label varLabel :: Int -> Label
varLabel = LVar varLabel = LVar
tupleLabel, linLabel :: Int -> Label
tupleLabel i = LIdent $! BS.pack ('p':show i)
linLabel i = LIdent $! BS.pack ('s':show i)
theLinLabel :: Label
theLinLabel = LIdent (BS.singleton 's')
ident2label :: Ident -> Label
ident2label c = LIdent (ident2bs c)
label2ident :: Label -> Ident
label2ident (LIdent s) = identC s
label2ident (LVar i) = identC (BS.pack ('$':show i))
wildPatt :: Patt wildPatt :: Patt
wildPatt = PV wildIdent wildPatt = PV identW
type Trm = Term type Trm = Term

View File

@@ -16,8 +16,10 @@
module GF.Grammar.Lockfield (lockRecType, unlockRecord, lockLabel, isLockLabel) where module GF.Grammar.Lockfield (lockRecType, unlockRecord, lockLabel, isLockLabel) where
import GF.Grammar.Grammar import qualified Data.ByteString.Char8 as BS
import GF.Infra.Ident import GF.Infra.Ident
import GF.Grammar.Grammar
import GF.Grammar.Macros import GF.Grammar.Macros
import GF.Grammar.PrGrammar import GF.Grammar.PrGrammar
@@ -38,9 +40,12 @@ unlockRecord c ft = do
return $ mkAbs xs t' return $ mkAbs xs t'
lockLabel :: Ident -> Label lockLabel :: Ident -> Label
lockLabel c = LIdent $ "lock_" ++ prt c ---- lockLabel c = LIdent $! BS.append lockPrefix (ident2bs c)
isLockLabel :: Label -> Bool isLockLabel :: Label -> Bool
isLockLabel l = case l of isLockLabel l = case l of
LIdent c -> take 5 c == "lock_" LIdent c -> BS.isPrefixOf lockPrefix c
_ -> False _ -> False
lockPrefix = BS.pack "lock_"

View File

@@ -115,7 +115,7 @@ lookupRef gr binds at = case at of
refsForType :: (Val -> Type -> Bool) -> GFCGrammar -> Binds -> Val -> [(Term,(Val,Bool))] refsForType :: (Val -> Type -> Bool) -> GFCGrammar -> Binds -> Val -> [(Term,(Val,Bool))]
refsForType compat gr binds val = refsForType compat gr binds val =
-- bound variables --- never recursive? -- bound variables --- never recursive?
[(vr i, (t,False)) | (i,t) <- binds, Ok ty <- [val2exp t], compat val ty] ++ [(Vr i, (t,False)) | (i,t) <- binds, Ok ty <- [val2exp t], compat val ty] ++
-- integer and string literals -- integer and string literals
[(EInt i, (val,False)) | val == valAbsInt, i <- [0,1,2,5,11,1978]] ++ [(EInt i, (val,False)) | val == valAbsInt, i <- [0,1,2,5,11,1978]] ++
[(EFloat i, (val,False)) | val == valAbsFloat, i <- [3.1415926]] ++ [(EFloat i, (val,False)) | val == valAbsFloat, i <- [3.1415926]] ++

View File

@@ -1,3 +1,4 @@
{-# LANGUAGE PatternGuards #-}
---------------------------------------------------------------------- ----------------------------------------------------------------------
-- | -- |
-- Module : Lookup -- Module : Lookup
@@ -28,13 +29,13 @@ module GF.Grammar.Lookup (
allParamValues, allParamValues,
lookupAbsDef, lookupAbsDef,
lookupLincat, lookupLincat,
opersForType, opersForType
linTypeInt
) where ) where
import GF.Data.Operations import GF.Data.Operations
import GF.Grammar.Abstract import GF.Grammar.Abstract
import GF.Infra.Modules import GF.Infra.Modules
import GF.Grammar.Predef
import GF.Grammar.Lockfield import GF.Grammar.Lockfield
import Data.List (nub,sortBy) import Data.List (nub,sortBy)
@@ -192,8 +193,7 @@ allOrigInfos gr m = errVal [] $ do
allParamValues :: SourceGrammar -> Type -> Err [Term] allParamValues :: SourceGrammar -> Type -> Err [Term]
allParamValues cnc ptyp = case ptyp of allParamValues cnc ptyp = case ptyp of
App (Q (IC "Predef") (IC "Ints")) (EInt n) -> _ | Just n <- isTypeInts ptyp -> return [EInt i | i <- [0..n]]
return [EInt i | i <- [0..n]]
QC p c -> lookupParamValues cnc p c QC p c -> lookupParamValues cnc p c
Q p c -> lookupParamValues cnc p c ---- Q p c -> lookupParamValues cnc p c ----
RecType r -> do RecType r -> do
@@ -230,17 +230,8 @@ lookupAbsDef gr m c = errIn ("looking up absdef of" +++ prt c) $ do
_ -> return Nothing _ -> return Nothing
_ -> Bad $ prt m +++ "is not an abstract module" _ -> Bad $ prt m +++ "is not an abstract module"
linTypeInt :: Type
linTypeInt = defLinType
--- let ints k = App (Q (IC "Predef") (IC "Ints")) (EInt k) in
--- RecType [
--- (LIdent "last",ints 9),(LIdent "s", typeStr), (LIdent "size",ints 1)]
lookupLincat :: SourceGrammar -> Ident -> Ident -> Err Type lookupLincat :: SourceGrammar -> Ident -> Ident -> Err Type
lookupLincat gr m c | elem c [zIdent "Int"] = return linTypeInt lookupLincat gr m c | isPredefCat c = return defLinType --- ad hoc; not needed?
lookupLincat gr m c | elem c [zIdent "String", zIdent "Float"] =
return defLinType --- ad hoc; not needed?
lookupLincat gr m c = do lookupLincat gr m c = do
mi <- lookupModule gr m mi <- lookupModule gr m
case mi of case mi of
@@ -265,7 +256,7 @@ opersForType gr orig val =
Ok valt <- [valTypeCnc ty], Ok valt <- [valTypeCnc ty],
elem valt [val,orig] elem valt [val,orig]
] ++ ] ++
let cat = err zIdent snd (valCat orig) in --- ignore module let cat = err error snd (valCat orig) in --- ignore module
[(f,ty) | [(f,ty) |
Ok a <- [abstractOfConcrete gr i >>= lookupModMod gr], Ok a <- [abstractOfConcrete gr i >>= lookupModMod gr],
(f, AbsFun (Yes ty0) _) <- tree2list $ jments a, (f, AbsFun (Yes ty0) _) <- tree2list $ jments a,

View File

@@ -26,6 +26,7 @@ import GF.Grammar.Values
import GF.Grammar.Macros import GF.Grammar.Macros
import Control.Monad import Control.Monad
import qualified Data.ByteString.Char8 as BS
nodeTree :: Tree -> TrNode nodeTree :: Tree -> TrNode
argsTree :: Tree -> [Tree] argsTree :: Tree -> [Tree]
@@ -120,9 +121,6 @@ funAtom a = case a of
AtC f -> return f AtC f -> return f
_ -> prtBad "not function head" a _ -> prtBad "not function head" a
uBoundVar :: Ident
uBoundVar = zIdent "#h" -- used for suppressed bindings
atomIsMeta :: Atom -> Bool atomIsMeta :: Atom -> Bool
atomIsMeta atom = case atom of atomIsMeta atom = case atom of
AtM _ -> True AtM _ -> True
@@ -186,7 +184,7 @@ val2expP safe v = case v of
VCn c -> return $ qq c VCn c -> return $ qq c
VGen i x -> if safe VGen i x -> if safe
then prtBad "unsafe val2exp" v then prtBad "unsafe val2exp" v
else return $ vr $ x --- in editing, no alpha conversions presentv else return $ Vr $ x --- in editing, no alpha conversions presentv
where where
substVal g e = mapPairsM (val2expP safe) g >>= return . (\s -> substTerm [] s e) substVal g e = mapPairsM (val2expP safe) g >>= return . (\s -> substTerm [] s e)
@@ -278,7 +276,7 @@ mkJustProd :: Context -> Term -> Term
mkJustProd cont typ = mkProd (cont,typ,[]) mkJustProd cont typ = mkProd (cont,typ,[])
int2var :: Int -> Ident int2var :: Int -> Ident
int2var = zIdent . ('$':) . show int2var = identC . BS.pack . ('$':) . show
meta0 :: Meta meta0 :: Meta
meta0 = int2meta 0 meta0 = int2meta 0
@@ -301,12 +299,12 @@ qualifTerm m = qualif [] where
Cn c -> Q m c Cn c -> Q m c
Con c -> QC m c Con c -> QC m c
_ -> composSafeOp (qualif xs) t _ -> composSafeOp (qualif xs) t
chV x = string2var $ prIdent x chV x = string2var $ ident2bs x
string2var :: String -> Ident string2var :: BS.ByteString -> Ident
string2var s = case s of string2var s = case BS.unpack s of
c:'_':i -> identV (readIntArg i,[c]) --- c:'_':i -> identV (BS.singleton c) (readIntArg i) ---
_ -> zIdent s _ -> identC s
-- | reindex variables so that they tell nesting depth level -- | reindex variables so that they tell nesting depth level
reindexTerm :: Term -> Term reindexTerm :: Term -> Term
@@ -317,7 +315,7 @@ reindexTerm = qualif (0,[]) where
Vr x -> Vr $ look x g Vr x -> Vr $ look x g
_ -> composSafeOp (qualif dg) t _ -> composSafeOp (qualif dg) t
look x = maybe x id . lookup x --- if x is not in scope it is unchanged look x = maybe x id . lookup x --- if x is not in scope it is unchanged
ind x d = identC $ prIdent x ++ "_" ++ show d ind x d = identC $ ident2bs x `BS.append` BS.singleton '_' `BS.append` BS.pack (show d)
-- this method works for context-free abstract syntax -- this method works for context-free abstract syntax

View File

@@ -20,8 +20,10 @@ module GF.Grammar.Macros where
import GF.Data.Operations import GF.Data.Operations
import GF.Data.Str import GF.Data.Str
import GF.Grammar.Grammar
import GF.Infra.Ident import GF.Infra.Ident
import GF.Grammar.Grammar
import GF.Grammar.Values
import GF.Grammar.Predef
import GF.Grammar.PrGrammar import GF.Grammar.PrGrammar
import Control.Monad (liftM, liftM2) import Control.Monad (liftM, liftM2)
@@ -55,12 +57,6 @@ qq (m,c) = Q m c
typeForm :: Type -> Err (Context, Cat, [Term]) typeForm :: Type -> Err (Context, Cat, [Term])
typeForm = qTypeForm ---- no need to distinguish any more typeForm = qTypeForm ---- no need to distinguish any more
cPredef :: Ident
cPredef = identC "Predef"
cnPredef :: String -> Term
cnPredef f = Q cPredef (identC f)
typeFormCnc :: Type -> Err (Context, Type) typeFormCnc :: Type -> Err (Context, Type)
typeFormCnc t = case t of typeFormCnc t = case t of
Prod x a b -> do Prod x a b -> do
@@ -91,18 +87,11 @@ typeRawSkeleton typ =
type MCat = (Ident,Ident) type MCat = (Ident,Ident)
sortMCat :: String -> MCat
sortMCat s = (zIdent "_", zIdent s)
--- hack for Editing.actCat in empty state
errorCat :: MCat
errorCat = (zIdent "?", zIdent "?")
getMCat :: Term -> Err MCat getMCat :: Term -> Err MCat
getMCat t = case t of getMCat t = case t of
Q m c -> return (m,c) Q m c -> return (m,c)
QC m c -> return (m,c) QC m c -> return (m,c)
Sort s -> return $ sortMCat s Sort c -> return (identW, c)
App f _ -> getMCat f App f _ -> getMCat f
_ -> prtBad "no qualified constant" t _ -> prtBad "no qualified constant" t
@@ -213,12 +202,6 @@ mkAbs xx t = foldr Abs t xx
appCons :: Ident -> [Term] -> Term appCons :: Ident -> [Term] -> Term
appCons = mkApp . Cn appCons = mkApp . Cn
appc :: String -> [Term] -> Term
appc = appCons . zIdent
appqc :: String -> String -> [Term] -> Term
appqc q c = mkApp (Q (zIdent q) (zIdent c))
mkLet :: [LocalDef] -> Term -> Term mkLet :: [LocalDef] -> Term -> Term
mkLet defs t = foldr Let t defs mkLet defs t = foldr Let t defs
@@ -232,11 +215,8 @@ isVariable _ = False
eqIdent :: Ident -> Ident -> Bool eqIdent :: Ident -> Ident -> Bool
eqIdent = (==) eqIdent = (==)
zIdent :: String -> Ident
zIdent s = identC s
uType :: Type uType :: Type
uType = Cn (zIdent "UndefinedType") uType = Cn cUndefinedType
assign :: Label -> Term -> Assign assign :: Label -> Term -> Assign
assign l t = (l,(Nothing,t)) assign l t = (l,(Nothing,t))
@@ -253,15 +233,6 @@ mkAssign lts = [assign l t | (l,t) <- lts]
zipAssign :: [Label] -> [Term] -> [Assign] zipAssign :: [Label] -> [Term] -> [Assign]
zipAssign ls ts = [assign l t | (l,t) <- zip ls ts] zipAssign ls ts = [assign l t | (l,t) <- zip ls ts]
ident2label :: Ident -> Label
ident2label c = LIdent (prIdent c)
label2ident :: Label -> Ident
label2ident = identC . prLabel
prLabel :: Label -> String
prLabel = prt
mapAssignM :: Monad m => (Term -> m c) -> [Assign] -> m [(Label,(Maybe c,c))] mapAssignM :: Monad m => (Term -> m c) -> [Assign] -> m [(Label,(Maybe c,c))]
mapAssignM f = mapM (\ (ls,tv) -> liftM ((,) ls) (g tv)) mapAssignM f = mapM (\ (ls,tv) -> liftM ((,) ls) (g tv))
where g (t,v) = liftM2 (,) (maybe (return Nothing) (liftM Just . f) t) (f v) where g (t,v) = liftM2 (,) (maybe (return Nothing) (liftM Just . f) t) (f v)
@@ -280,41 +251,40 @@ mkRecType = mkRecTypeN 0
record2subst :: Term -> Err Substitution record2subst :: Term -> Err Substitution
record2subst t = case t of record2subst t = case t of
R fs -> return [(zIdent x, t) | (LIdent x,(_,t)) <- fs] R fs -> return [(identC x, t) | (LIdent x,(_,t)) <- fs]
_ -> prtBad "record expected, found" t _ -> prtBad "record expected, found" t
typeType, typePType, typeStr, typeTok, typeStrs :: Term typeType, typePType, typeStr, typeTok, typeStrs :: Term
typeType = srt "Type" typeType = Sort cType
typePType = srt "PType" typePType = Sort cPType
typeStr = srt "Str" typeStr = Sort cStr
typeTok = srt "Tok" typeTok = Sort cTok
typeStrs = srt "Strs" typeStrs = Sort cStrs
typeString, typeFloat, typeInt :: Term typeString, typeFloat, typeInt :: Term
typeInts :: Integer -> Term typeInts :: Integer -> Term
typePBool :: Term
typeError :: Term
typeString = constPredefRes "String" typeString = cnPredef cString
typeInt = constPredefRes "Int" typeInt = cnPredef cInt
typeFloat = constPredefRes "Float" typeFloat = cnPredef cFloat
typeInts i = App (constPredefRes "Ints") (EInt i) typeInts i = App (cnPredef cInts) (EInt i)
typePBool = cnPredef cPBool
typeError = cnPredef cErrorType
isTypeInts :: Term -> Bool isTypeInts :: Term -> Maybe Integer
isTypeInts ty = case ty of isTypeInts (App c (EInt i)) | c == cnPredef cInts = Just i
App c _ -> c == constPredefRes "Ints" isTypeInts _ = Nothing
_ -> False
constPredefRes :: String -> Term
constPredefRes s = Q (IC "Predef") (zIdent s)
isPredefConstant :: Term -> Bool isPredefConstant :: Term -> Bool
isPredefConstant t = case t of isPredefConstant t = case t of
Q (IC "Predef") _ -> True Q mod _ | mod == cPredef || mod == cPredefAbs -> True
Q (IC "PredefAbs") _ -> True _ -> False
_ -> False
isPredefAbsType :: Ident -> Bool cnPredef :: Ident -> Term
isPredefAbsType c = elem c [zIdent "Int", zIdent "String"] cnPredef f = Q cPredef f
mkSelects :: Term -> [Term] -> Term mkSelects :: Term -> [Term] -> Term
mkSelects t tt = foldl S t tt mkSelects t tt = foldl S t tt
@@ -327,18 +297,11 @@ mkCTable ids v = foldr ccase v ids where
ccase x t = T TRaw [(PV x,t)] ccase x t = T TRaw [(PV x,t)]
mkDecl :: Term -> Decl mkDecl :: Term -> Decl
mkDecl typ = (wildIdent, typ) mkDecl typ = (identW, typ)
eqStrIdent :: Ident -> Ident -> Bool eqStrIdent :: Ident -> Ident -> Bool
eqStrIdent = (==) eqStrIdent = (==)
tupleLabel, linLabel :: Int -> Label
tupleLabel i = LIdent $ "p" ++ show i
linLabel i = LIdent $ "s" ++ show i
theLinLabel :: Label
theLinLabel = LIdent "s"
tuple2record :: [Term] -> [Assign] tuple2record :: [Term] -> [Assign]
tuple2record ts = [assign (tupleLabel i) t | (i,t) <- zip [1..] ts] tuple2record ts = [assign (tupleLabel i) t | (i,t) <- zip [1..] ts]
@@ -352,10 +315,10 @@ mkCases :: Ident -> Term -> Term
mkCases x t = T TRaw [(PV x, t)] mkCases x t = T TRaw [(PV x, t)]
mkWildCases :: Term -> Term mkWildCases :: Term -> Term
mkWildCases = mkCases wildIdent mkWildCases = mkCases identW
mkFunType :: [Type] -> Type -> Type mkFunType :: [Type] -> Type -> Type
mkFunType tt t = mkProd ([(wildIdent, ty) | ty <- tt], t, []) -- nondep prod mkFunType tt t = mkProd ([(identW, ty) | ty <- tt], t, []) -- nondep prod
plusRecType :: Type -> Type -> Err Type plusRecType :: Type -> Type -> Err Type
plusRecType t1 t2 = case (unComputed t1, unComputed t2) of plusRecType t1 t2 = case (unComputed t1, unComputed t2) of
@@ -376,11 +339,7 @@ plusRecord t1 t2 =
-- | default linearization type -- | default linearization type
defLinType :: Type defLinType :: Type
defLinType = RecType [(LIdent "s", typeStr)] defLinType = RecType [(theLinLabel, typeStr)]
-- | refreshing variables
varX :: Int -> Ident
varX i = identV (i,"x")
-- | refreshing variables -- | refreshing variables
mkFreshVar :: [Ident] -> Ident mkFreshVar :: [Ident] -> Ident
@@ -414,28 +373,12 @@ float2term = EFloat
ident2terminal :: Ident -> Term ident2terminal :: Ident -> Term
ident2terminal = K . prIdent ident2terminal = K . prIdent
-- | create a constant
string2CnTrm :: String -> Term
string2CnTrm = Cn . zIdent
symbolOfIdent :: Ident -> String symbolOfIdent :: Ident -> String
symbolOfIdent = prIdent symbolOfIdent = prIdent
symid :: Ident -> String symid :: Ident -> String
symid = symbolOfIdent symid = symbolOfIdent
vr :: Ident -> Term
cn :: Ident -> Term
srt :: String -> Term
meta :: MetaSymb -> Term
cnIC :: String -> Term
vr = Vr
cn = Cn
srt = Sort
meta = Meta
cnIC = cn . IC
justIdentOf :: Term -> Maybe Ident justIdentOf :: Term -> Maybe Ident
justIdentOf (Vr x) = Just x justIdentOf (Vr x) = Just x
justIdentOf (Cn x) = Just x justIdentOf (Cn x) = Just x
@@ -490,9 +433,6 @@ linTypeStr = mkRecType linLabel [typeStr] -- default lintype {s :: Str}
linAsStr :: String -> Term linAsStr :: String -> Term
linAsStr s = mkRecord linLabel [K s] -- default linearization {s = s} linAsStr s = mkRecord linLabel [K s] -- default linearization {s = s}
linDefStr :: Term
linDefStr = Abs s (R [assign (linLabel 0) (Vr s)]) where s = zIdent "s"
term2patt :: Term -> Err Patt term2patt :: Term -> Err Patt
term2patt trm = case termForm trm of term2patt trm = case termForm trm of
Ok ([], Vr x, []) -> return (PV x) Ok ([], Vr x, []) -> return (PV x)
@@ -516,24 +456,24 @@ term2patt trm = case termForm trm of
Ok ([],K s, []) -> return $ PString s Ok ([],K s, []) -> return $ PString s
--- encodings due to excessive use of term-patt convs. AR 7/1/2005 --- encodings due to excessive use of term-patt convs. AR 7/1/2005
Ok ([], Cn (IC "@"), [Vr a,b]) -> do Ok ([], Cn id, [Vr a,b]) | id == cAs -> do
b' <- term2patt b b' <- term2patt b
return (PAs a b') return (PAs a b')
Ok ([], Cn (IC "-"), [a]) -> do Ok ([], Cn id, [a]) | id == cNeg -> do
a' <- term2patt a a' <- term2patt a
return (PNeg a') return (PNeg a')
Ok ([], Cn (IC "*"), [a]) -> do Ok ([], Cn id, [a]) | id == cRep -> do
a' <- term2patt a a' <- term2patt a
return (PRep a') return (PRep a')
Ok ([], Cn (IC "?"), []) -> do Ok ([], Cn id, []) | id == cRep -> do
return PChar return PChar
Ok ([], Cn (IC "[]"),[K s]) -> do Ok ([], Cn id,[K s]) | id == cChars -> do
return $ PChars s return $ PChars s
Ok ([], Cn (IC "+"), [a,b]) -> do Ok ([], Cn id, [a,b]) | id == cSeq -> do
a' <- term2patt a a' <- term2patt a
b' <- term2patt b b' <- term2patt b
return (PSeq a' b') return (PSeq a' b')
Ok ([], Cn (IC "|"), [a,b]) -> do Ok ([], Cn id, [a,b]) | id == cAlt -> do
a' <- term2patt a a' <- term2patt a
b' <- term2patt b b' <- term2patt b
return (PAlt a' b') return (PAlt a' b')
@@ -546,7 +486,7 @@ term2patt trm = case termForm trm of
patt2term :: Patt -> Term patt2term :: Patt -> Term
patt2term pt = case pt of patt2term pt = case pt of
PV x -> Vr x PV x -> Vr x
PW -> Vr wildIdent --- not parsable, should not occur PW -> Vr identW --- not parsable, should not occur
PVal t i -> Val t i PVal t i -> Val t i
PMacro c -> Cn c PMacro c -> Cn c
PM p c -> Q p c PM p c -> Q p c
@@ -560,13 +500,13 @@ patt2term pt = case pt of
PFloat i -> EFloat i PFloat i -> EFloat i
PString s -> K s PString s -> K s
PAs x p -> appc "@" [Vr x, patt2term p] --- an encoding PAs x p -> appCons cAs [Vr x, patt2term p] --- an encoding
PChar -> appc "?" [] --- an encoding PChar -> appCons cChar [] --- an encoding
PChars s -> appc "[]" [K s] --- an encoding PChars s -> appCons cChars [K s] --- an encoding
PSeq a b -> appc "+" [(patt2term a), (patt2term b)] --- an encoding PSeq a b -> appCons cSeq [(patt2term a), (patt2term b)] --- an encoding
PAlt a b -> appc "|" [(patt2term a), (patt2term b)] --- an encoding PAlt a b -> appCons cAlt [(patt2term a), (patt2term b)] --- an encoding
PRep a -> appc "*" [(patt2term a)] --- an encoding PRep a -> appCons cRep [(patt2term a)] --- an encoding
PNeg a -> appc "-" [(patt2term a)] --- an encoding PNeg a -> appCons cNeg [(patt2term a)] --- an encoding
redirectTerm :: Ident -> Term -> Term redirectTerm :: Ident -> Term -> Term
@@ -575,45 +515,12 @@ redirectTerm n t = case t of
Q _ f -> Q n f Q _ f -> Q n f
_ -> composSafeOp (redirectTerm n) t _ -> composSafeOp (redirectTerm n) t
-- | to gather s-fields; assumes term in normal form, preserves label
allLinFields :: Term -> Err [[(Label,Term)]]
allLinFields trm = case unComputed trm of
---- R rs -> return [[(l,t) | (l,(Just ty,t)) <- rs, isStrType ty]] -- good
R rs -> return [[(l,t) | (l,(_,t)) <- rs, isLinLabel l]] ---- bad
FV ts -> do
lts <- mapM allLinFields ts
return $ concat lts
_ -> prtBad "fields can only be sought in a record not in" trm
-- | deprecated
isLinLabel :: Label -> Bool
isLinLabel l = case l of
LIdent ('s':cs) | all isDigit cs -> True
_ -> False
-- | to gather ultimate cases in a table; preserves pattern list -- | to gather ultimate cases in a table; preserves pattern list
allCaseValues :: Term -> [([Patt],Term)] allCaseValues :: Term -> [([Patt],Term)]
allCaseValues trm = case unComputed trm of allCaseValues trm = case unComputed trm of
T _ cs -> [(p:ps, t) | (p,t0) <- cs, (ps,t) <- allCaseValues t0] T _ cs -> [(p:ps, t) | (p,t0) <- cs, (ps,t) <- allCaseValues t0]
_ -> [([],trm)] _ -> [([],trm)]
-- | to gather all linearizations; assumes normal form, preserves label and args
allLinValues :: Term -> Err [[(Label,[([Patt],Term)])]]
allLinValues trm = do
lts <- allLinFields trm
mapM (mapPairsM (return . allCaseValues)) lts
-- | to mark str parts of fields in a record f by a function f
markLinFields :: (Term -> Term) -> Term -> Term
markLinFields f t = case t of
R r -> R $ map mkField r
_ -> t
where
mkField (l,(_,t)) = if (isLinLabel l) then (assign l (mkTbl t)) else (assign l t)
mkTbl t = case t of
T i cs -> T i [(p, mkTbl v) | (p,v) <- cs]
_ -> f t
-- | to get a string from a term that represents a sequence of terminals -- | to get a string from a term that represents a sequence of terminals
strsFromTerm :: Term -> Err [Str] strsFromTerm :: Term -> Err [Str]
strsFromTerm t = case unComputed t of strsFromTerm t = case unComputed t of

View File

@@ -19,15 +19,15 @@ module GF.Grammar.Values (-- * values used in TC type checking
-- * for TC -- * for TC
valAbsInt, valAbsFloat, valAbsString, vType, valAbsInt, valAbsFloat, valAbsString, vType,
isPredefCat, isPredefCat,
cType, cPredefAbs, cInt, cFloat, cString,
eType, tree2exp, loc2treeFocus eType, tree2exp, loc2treeFocus
) where ) where
import GF.Data.Operations import GF.Data.Operations
import GF.Data.Zipper import GF.Data.Zipper
import GF.Grammar.Grammar
import GF.Infra.Ident import GF.Infra.Ident
import GF.Grammar.Grammar
import GF.Grammar.Predef
-- values used in TC type checking -- values used in TC type checking
@@ -67,26 +67,8 @@ valAbsString = VCn (cPredefAbs, cString)
vType :: Val vType :: Val
vType = VType vType = VType
cType :: Ident
cType = identC "Type" --- #0
cPredefAbs :: Ident
cPredefAbs = identC "PredefAbs"
cInt :: Ident
cInt = identC "Int"
cFloat :: Ident
cFloat = identC "Float"
cString :: Ident
cString = identC "String"
isPredefCat :: Ident -> Bool
isPredefCat c = elem c [cInt,cString,cFloat]
eType :: Exp eType :: Exp
eType = Sort "Type" eType = Sort cType
tree2exp :: Tree -> Exp tree2exp :: Tree -> Exp
tree2exp (Tr (N (bi,at,_,_,_),ts)) = foldr Abs (foldl App at' ts') bi' where tree2exp (Tr (N (bi,at,_,_,_),ts)) = foldr Abs (foldl App at' ts') bi' where

View File

@@ -13,45 +13,48 @@
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
module GF.Infra.Ident (-- * Identifiers module GF.Infra.Ident (-- * Identifiers
Ident(..), prIdent, Ident(..), ident2bs, prIdent,
identC, identV, identA, identAV, identW, identC, identV, identA, identAV, identW,
argIdent, strVar, wildIdent, isWildIdent, argIdent, varStr, varX, isWildIdent, varIndex,
newIdent, mkIdent, varIndex,
-- * refreshing identifiers -- * refreshing identifiers
IdState, initIdStateN, initIdState, IdState, initIdStateN, initIdState,
lookVar, refVar, refVarPlus lookVar, refVar, refVarPlus
) where ) where
import GF.Data.Operations import GF.Data.Operations
import qualified Data.ByteString.Char8 as BS
-- import Monad -- import Monad
-- | the constructors labelled /INTERNAL/ are -- | the constructors labelled /INTERNAL/ are
-- internal representation never returned by the parser -- internal representation never returned by the parser
data Ident = data Ident =
IC String -- ^ raw identifier after parsing, resolved in Rename IC !BS.ByteString -- ^ raw identifier after parsing, resolved in Rename
| IW -- ^ wildcard | IW -- ^ wildcard
-- --
-- below this constructor: internal representation never returned by the parser -- below this constructor: internal representation never returned by the parser
| IV (Int,String) -- ^ /INTERNAL/ variable | IV !BS.ByteString Int -- ^ /INTERNAL/ variable
| IA (String,Int) -- ^ /INTERNAL/ argument of cat at position | IA !BS.ByteString Int -- ^ /INTERNAL/ argument of cat at position
| IAV (String,Int,Int) -- ^ /INTERNAL/ argument of cat with bindings at position | IAV !BS.ByteString Int Int -- ^ /INTERNAL/ argument of cat with bindings at position
-- --
deriving (Eq, Ord, Show, Read) deriving (Eq, Ord, Show, Read)
prIdent :: Ident -> String ident2bs :: Ident -> BS.ByteString
prIdent i = case i of ident2bs i = case i of
IC s -> s IC s -> s
IV (n,s) -> s ++ "_" ++ show n IV s n -> BS.append s (BS.pack ('_':show n))
IA (s,j) -> s ++ "_" ++ show j IA s j -> BS.append s (BS.pack ('_':show j))
IAV (s,b,j) -> s ++ "_" ++ show b ++ "_" ++ show j IAV s b j -> BS.append s (BS.pack ('_':show b ++ '_':show j))
IW -> "_" IW -> BS.singleton '_'
identC :: String -> Ident prIdent :: Ident -> String
identV :: (Int, String) -> Ident prIdent i = BS.unpack $! ident2bs i
identA :: (String, Int) -> Ident
identAV:: (String, Int, Int) -> Ident identC :: BS.ByteString -> Ident
identV :: BS.ByteString -> Int -> Ident
identA :: BS.ByteString -> Int -> Ident
identAV:: BS.ByteString -> Int -> Int -> Ident
identW :: Ident identW :: Ident
(identC, identV, identA, identAV, identW) = (identC, identV, identA, identAV, identW) =
(IC, IV, IA, IAV, IW) (IC, IV, IA, IAV, IW)
@@ -61,31 +64,25 @@ identW :: Ident
-- | to mark argument variables -- | to mark argument variables
argIdent :: Int -> Ident -> Int -> Ident argIdent :: Int -> Ident -> Int -> Ident
argIdent 0 (IC c) i = identA (c,i) argIdent 0 (IC c) i = identA c i
argIdent b (IC c) i = identAV (c,b,i) argIdent b (IC c) i = identAV c b i
-- | used in lin defaults -- | used in lin defaults
strVar :: Ident varStr :: Ident
strVar = identA ("str",0) varStr = identA (BS.pack "str") 0
-- | wild card -- | refreshing variables
wildIdent :: Ident varX :: Int -> Ident
wildIdent = identW varX = identV (BS.singleton 'x')
isWildIdent :: Ident -> Bool isWildIdent :: Ident -> Bool
isWildIdent x = case x of isWildIdent x = case x of
IW -> True IW -> True
IC "_" -> True IC s | s == BS.pack "_" -> True
_ -> False _ -> False
newIdent :: Ident
newIdent = identC "#h"
mkIdent :: String -> Int -> Ident
mkIdent s i = identV (i,s)
varIndex :: Ident -> Int varIndex :: Ident -> Int
varIndex (IV (n,_)) = n varIndex (IV _ n) = n
varIndex _ = -1 --- other than IV should not count varIndex _ = -1 --- other than IV should not count
-- refreshing identifiers -- refreshing identifiers
@@ -99,7 +96,7 @@ initIdState :: IdState
initIdState = initIdStateN 0 initIdState = initIdStateN 0
lookVar :: Ident -> STM IdState Ident lookVar :: Ident -> STM IdState Ident
lookVar a@(IA _) = return a lookVar a@(IA _ _) = return a
lookVar x = do lookVar x = do
(sys,_) <- readSTM (sys,_) <- readSTM
stm (\s -> maybe (Bad ("cannot find" +++ show x +++ prParenth (show sys))) stm (\s -> maybe (Bad ("cannot find" +++ show x +++ prParenth (show sys)))
@@ -110,8 +107,8 @@ refVar :: Ident -> STM IdState Ident
----refVar IW = return IW --- no update of wildcard ----refVar IW = return IW --- no update of wildcard
refVar x = do refVar x = do
(_,m) <- readSTM (_,m) <- readSTM
let x' = IV (m, prIdent x) let x' = IV (ident2bs x) m
updateSTM (\ (sys,mx) -> ((x, x'):sys, mx + 1)) updateSTM (\(sys,mx) -> ((x, x'):sys, mx + 1))
return x' return x'
refVarPlus :: Ident -> STM IdState Ident refVarPlus :: Ident -> STM IdState Ident

View File

@@ -2,8 +2,9 @@ module GF.Source.AbsGF where
-- Haskell module generated by the BNF converter -- Haskell module generated by the BNF converter
newtype LString = LString String deriving (Eq,Ord,Show) import qualified Data.ByteString.Char8 as BS
newtype PIdent = PIdent ((Int,Int),String) deriving (Eq,Ord,Show) newtype LString = LString BS.ByteString deriving (Eq,Ord,Show)
newtype PIdent = PIdent ((Int,Int),BS.ByteString) deriving (Eq,Ord,Show)
data Grammar = data Grammar =
Gr [ModDef] Gr [ModDef]
deriving (Eq,Ord,Show) deriving (Eq,Ord,Show)

View File

@@ -5,6 +5,7 @@
entrypoints Grammar, ModDef, entrypoints Grammar, ModDef,
OldGrammar, --% OldGrammar, --%
ModHeader,
Exp ; -- let's see if more are needed Exp ; -- let's see if more are needed
comment "--" ; comment "--" ;

View File

@@ -21,10 +21,12 @@ module GF.Source.GrammarToSource ( trGrammar,
import GF.Data.Operations import GF.Data.Operations
import GF.Grammar.Grammar import GF.Grammar.Grammar
import GF.Grammar.Predef
import GF.Infra.Modules import GF.Infra.Modules
import GF.Infra.Option import GF.Infra.Option
import qualified GF.Source.AbsGF as P import qualified GF.Source.AbsGF as P
import GF.Infra.Ident import GF.Infra.Ident
import qualified Data.ByteString.Char8 as BS
-- | AR 13\/5\/2003 -- | AR 13\/5\/2003
-- --
@@ -96,7 +98,7 @@ trAnyDef (i,info) = let i' = tri i in case info of
ResOverload tysts -> ResOverload tysts ->
[P.DefOper [P.DDef [mkName i'] ( [P.DefOper [P.DDef [mkName i'] (
P.EApp (P.EIdent $ tri $ identC "overload") P.EApp (P.EIdent $ tri $ cOverload)
(P.ERecord [P.LDFull [i'] (trt ty) (trt fu) | (ty,fu) <- tysts]))]] (P.ERecord [P.LDFull [i'] (trt ty) (trt fu) | (ty,fu) <- tysts]))]]
CncCat (Yes ty) Nope _ -> CncCat (Yes ty) Nope _ ->
@@ -131,7 +133,7 @@ trPerh p = case p of
trFlag :: Option -> P.TopDef trFlag :: Option -> P.TopDef
trFlag o = case o of trFlag o = case o of
Opt (f,[x]) -> P.DefFlag [P.FlagDef (tri $ identC f) (tri $ identC x)] Opt (f,[x]) -> P.DefFlag [P.FlagDef (tri $ identC (BS.pack f)) (tri $ identC (BS.pack x))]
_ -> P.DefFlag [] --- warning? _ -> P.DefFlag [] --- warning?
trt :: Term -> P.Exp trt :: Term -> P.Exp
@@ -139,14 +141,12 @@ trt trm = case trm of
Vr s -> P.EIdent $ tri s Vr s -> P.EIdent $ tri s
Cn s -> P.ECons $ tri s Cn s -> P.ECons $ tri s
Con s -> P.EConstr $ tri s Con s -> P.EConstr $ tri s
Sort s -> P.ESort $ case s of Sort s -> P.ESort $! if s == cType then P.Sort_Type else
"Type" -> P.Sort_Type if s == cPType then P.Sort_PType else
"PType" -> P.Sort_PType if s == cTok then P.Sort_Tok else
"Tok" -> P.Sort_Tok if s == cStr then P.Sort_Str else
"Str" -> P.Sort_Str if s == cStrs then P.Sort_Strs else
"Strs" -> P.Sort_Strs error $ "not yet sort " +++ show trm
_ -> error $ "not yet sort " +++ show trm ----
App c a -> P.EApp (trt c) (trt a) App c a -> P.EApp (trt c) (trt a)
Abs x b -> P.EAbstr [trb x] (trt b) Abs x b -> P.EAbstr [trb x] (trt b)
Eqs pts -> P.EEqs [P.Equ (map trp ps) (trt t) | (ps,t) <- pts] Eqs pts -> P.EEqs [P.Equ (map trp ps) (trt t) | (ps,t) <- pts]
@@ -210,7 +210,7 @@ trp p = case p of
PC c a -> P.PC (tri c) (map trp a) PC c a -> P.PC (tri c) (map trp a)
PP p c [] -> P.PQ (tri p) (tri c) PP p c [] -> P.PQ (tri p) (tri c)
PP p c a -> P.PQC (tri p) (tri c) (map trp a) PP p c a -> P.PQC (tri p) (tri c) (map trp a)
PR r -> P.PR [P.PA [tri $ trLabelIdent l] (trp p) | (l,p) <- r] PR r -> P.PR [P.PA [tri $ label2ident l] (trp p) | (l,p) <- r]
PString s -> P.PStr s PString s -> P.PStr s
PInt i -> P.PInt i PInt i -> P.PInt i
PFloat i -> P.PFloat i PFloat i -> P.PFloat i
@@ -230,9 +230,9 @@ trp p = case p of
trAssign (lab, (mty, t)) = maybe (P.LDDef x t') (\ty -> P.LDFull x (trt ty) t') mty trAssign (lab, (mty, t)) = maybe (P.LDDef x t') (\ty -> P.LDFull x (trt ty) t') mty
where where
t' = trt t t' = trt t
x = [tri $ trLabelIdent lab] x = [tri $ label2ident lab]
trLabelling (lab,ty) = P.LDDecl [tri $ trLabelIdent lab] (trt ty) trLabelling (lab,ty) = P.LDDecl [tri $ label2ident lab] (trt ty)
trCase (patt, trm) = P.Case (trp patt) (trt trm) trCase (patt, trm) = P.Case (trp patt) (trt trm)
trCases (patts,trm) = P.Case (foldl1 P.PDisj (map trp patts)) (trt trm) trCases (patts,trm) = P.Case (foldl1 P.PDisj (map trp patts)) (trt trm)
@@ -240,7 +240,7 @@ trCases (patts,trm) = P.Case (foldl1 P.PDisj (map trp patts)) (trt trm)
trDecl (x,ty) = P.DDDec [trb x] (trt ty) trDecl (x,ty) = P.DDDec [trb x] (trt ty)
tri :: Ident -> P.PIdent tri :: Ident -> P.PIdent
tri = ppIdent . prIdent tri = ppIdent . ident2bs
ppIdent i = P.PIdent ((0,0),i) ppIdent i = P.PIdent ((0,0),i)
@@ -251,9 +251,5 @@ trLabel i = case i of
LIdent s -> P.LIdent $ ppIdent s LIdent s -> P.LIdent $ ppIdent s
LVar i -> P.LVar $ toInteger i LVar i -> P.LVar $ toInteger i
trLabelIdent i = identC $ case i of
LIdent s -> s
LVar i -> "v" ++ show i --- should not happen
mkName :: P.PIdent -> P.Name mkName :: P.PIdent -> P.Name
mkName = P.IdentName mkName = P.IdentName

View File

@@ -1,14 +1,15 @@
{-# OPTIONS -fglasgow-exts -cpp #-} {-# OPTIONS -fglasgow-exts -cpp #-}
{-# LINE 3 "GF/Source/LexGF.x" #-} {-# LINE 3 "LexGF.x" #-}
{-# OPTIONS -fno-warn-incomplete-patterns #-} {-# OPTIONS -fno-warn-incomplete-patterns #-}
module GF.Source.LexGF where module GF.Source.LexGF where
import GF.Source.SharedString
import qualified Data.ByteString.Char8 as BS import qualified Data.ByteString.Char8 as BS
#if __GLASGOW_HASKELL__ >= 603 #if __GLASGOW_HASKELL__ >= 603
#include "ghcconfig.h" #include "ghcconfig.h"
#else #elif defined(__GLASGOW_HASKELL__)
#include "config.h" #include "config.h"
#endif #endif
#if __GLASGOW_HASKELL__ >= 503 #if __GLASGOW_HASKELL__ >= 503
@@ -37,22 +38,23 @@ alex_deflt :: AlexAddr
alex_deflt = AlexA# "\x16\x00\xff\xff\x03\x00\x03\x00\xff\xff\xff\xff\x0b\x00\xff\xff\x0b\x00\x0b\x00\x0b\x00\x0b\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x15\x00\x15\x00\xff\xff\xff\xff\xff\xff\xff\xff\x1b\x00\x1b\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"# alex_deflt = AlexA# "\x16\x00\xff\xff\x03\x00\x03\x00\xff\xff\xff\xff\x0b\x00\xff\xff\x0b\x00\x0b\x00\x0b\x00\x0b\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x15\x00\x15\x00\xff\xff\xff\xff\xff\xff\xff\xff\x1b\x00\x1b\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"#
alex_accept = listArray (0::Int,34) [[],[],[(AlexAccSkip)],[(AlexAccSkip)],[],[(AlexAcc (alex_action_3))],[(AlexAccSkip)],[(AlexAccSkip)],[],[],[],[],[(AlexAcc (alex_action_3))],[(AlexAccSkip)],[(AlexAcc (alex_action_3))],[(AlexAcc (alex_action_3))],[(AlexAcc (alex_action_3))],[(AlexAcc (alex_action_3))],[(AlexAcc (alex_action_3))],[(AlexAcc (alex_action_4))],[],[],[(AlexAcc (alex_action_5))],[(AlexAcc (alex_action_5))],[(AlexAcc (alex_action_5))],[(AlexAcc (alex_action_7))],[],[],[],[(AlexAcc (alex_action_8))],[(AlexAcc (alex_action_9))],[(AlexAcc (alex_action_9))],[],[],[]] alex_accept = listArray (0::Int,34) [[],[],[(AlexAccSkip)],[(AlexAccSkip)],[],[(AlexAcc (alex_action_3))],[(AlexAccSkip)],[(AlexAccSkip)],[],[],[],[],[(AlexAcc (alex_action_3))],[(AlexAccSkip)],[(AlexAcc (alex_action_3))],[(AlexAcc (alex_action_3))],[(AlexAcc (alex_action_3))],[(AlexAcc (alex_action_3))],[(AlexAcc (alex_action_3))],[(AlexAcc (alex_action_4))],[],[],[(AlexAcc (alex_action_5))],[(AlexAcc (alex_action_5))],[(AlexAcc (alex_action_5))],[(AlexAcc (alex_action_7))],[],[],[],[(AlexAcc (alex_action_8))],[(AlexAcc (alex_action_9))],[(AlexAcc (alex_action_9))],[],[],[]]
{-# LINE 36 "GF/Source/LexGF.x" #-} {-# LINE 37 "LexGF.x" #-}
tok f p s = f p s tok f p s = f p s
share :: String -> String share :: BS.ByteString -> BS.ByteString
share = id share = shareString
data Tok = data Tok =
TS !String -- reserved words and symbols TS !BS.ByteString !Int -- reserved words and symbols
| TL !String -- string literals | TL !BS.ByteString -- string literals
| TI !String -- integer literals | TI !BS.ByteString -- integer literals
| TV !String -- identifiers | TV !BS.ByteString -- identifiers
| TD !String -- double precision float literals | TD !BS.ByteString -- double precision float literals
| TC !String -- character literals | TC !BS.ByteString -- character literals
| T_LString !String | T_LString !BS.ByteString
| T_PIdent !String | T_PIdent !BS.ByteString
deriving (Eq,Show,Ord) deriving (Eq,Show,Ord)
@@ -69,19 +71,19 @@ posLineCol (Pn _ l c) = (l,c)
mkPosToken t@(PT p _) = (posLineCol p, prToken t) mkPosToken t@(PT p _) = (posLineCol p, prToken t)
prToken t = case t of prToken t = case t of
PT _ (TS s) -> s PT _ (TS s _) -> s
PT _ (TI s) -> s PT _ (TL s) -> s
PT _ (TV s) -> s PT _ (TI s) -> s
PT _ (TD s) -> s PT _ (TV s) -> s
PT _ (TC s) -> s PT _ (TD s) -> s
PT _ (TC s) -> s
PT _ (T_LString s) -> s PT _ (T_LString s) -> s
PT _ (T_PIdent s) -> s PT _ (T_PIdent s) -> s
_ -> show t
data BTree = N | B String Tok BTree BTree deriving (Show) data BTree = N | B BS.ByteString Tok BTree BTree deriving (Show)
eitherResIdent :: (String -> Tok) -> String -> Tok eitherResIdent :: (BS.ByteString -> Tok) -> BS.ByteString -> Tok
eitherResIdent tv s = treeFind resWords eitherResIdent tv s = treeFind resWords
where where
treeFind N = tv s treeFind N = tv s
@@ -89,11 +91,12 @@ eitherResIdent tv s = treeFind resWords
| s > a = treeFind right | s > a = treeFind right
| s == a = t | s == a = t
resWords = b "lincat" (b "def" (b "Type" (b "Str" (b "PType" (b "Lin" N N) N) (b "Tok" (b "Strs" N N) N)) (b "cat" (b "case" (b "abstract" N N) N) (b "data" (b "concrete" N N) N))) (b "include" (b "fun" (b "fn" (b "flags" N N) N) (b "in" (b "grammar" N N) N)) (b "interface" (b "instance" (b "incomplete" N N) N) (b "lin" (b "let" N N) N)))) (b "resource" (b "out" (b "of" (b "lintype" (b "lindef" N N) N) (b "oper" (b "open" N N) N)) (b "pattern" (b "param" (b "package" N N) N) (b "printname" (b "pre" N N) N))) (b "union" (b "table" (b "strs" (b "reuse" N N) N) (b "transfer" (b "tokenizer" N N) N)) (b "where" (b "variants" (b "var" N N) N) (b "with" N N)))) resWords = b "def" 39 (b "=>" 20 (b "++" 10 (b "(" 5 (b "$" 3 (b "#" 2 (b "!" 1 N N) N) (b "%" 4 N N)) (b "**" 8 (b "*" 7 (b ")" 6 N N) N) (b "+" 9 N N))) (b "/" 15 (b "->" 13 (b "-" 12 (b "," 11 N N) N) (b "." 14 N N)) (b "<" 18 (b ";" 17 (b ":" 16 N N) N) (b "=" 19 N N)))) (b "[" 30 (b "PType" 25 (b "@" 23 (b "?" 22 (b ">" 21 N N) N) (b "Lin" 24 N N)) (b "Tok" 28 (b "Strs" 27 (b "Str" 26 N N) N) (b "Type" 29 N N))) (b "case" 35 (b "_" 33 (b "]" 32 (b "\\" 31 N N) N) (b "abstract" 34 N N)) (b "concrete" 37 (b "cat" 36 N N) (b "data" 38 N N))))) (b "package" 58 (b "let" 49 (b "in" 44 (b "fun" 42 (b "fn" 41 (b "flags" 40 N N) N) (b "grammar" 43 N N)) (b "instance" 47 (b "incomplete" 46 (b "include" 45 N N) N) (b "interface" 48 N N))) (b "of" 54 (b "lindef" 52 (b "lincat" 51 (b "lin" 50 N N) N) (b "lintype" 53 N N)) (b "oper" 56 (b "open" 55 N N) (b "out" 57 N N)))) (b "transfer" 68 (b "resource" 63 (b "pre" 61 (b "pattern" 60 (b "param" 59 N N) N) (b "printname" 62 N N)) (b "table" 66 (b "strs" 65 (b "reuse" 64 N N) N) (b "tokenizer" 67 N N))) (b "with" 73 (b "variants" 71 (b "var" 70 (b "union" 69 N N) N) (b "where" 72 N N)) (b "|" 75 (b "{" 74 N N) (b "}" 76 N N)))))
where b s = B s (TS s) where b s n = let bs = BS.pack s
in B bs (TS bs n)
unescapeInitTail :: String -> String unescapeInitTail :: BS.ByteString -> BS.ByteString
unescapeInitTail = unesc . tail where unescapeInitTail = BS.pack . unesc . tail . BS.unpack where
unesc s = case s of unesc s = case s of
'\\':c:cs | elem c ['\"', '\\', '\''] -> c : unesc cs '\\':c:cs | elem c ['\"', '\\', '\''] -> c : unesc cs
'\\':'n':cs -> '\n' : unesc cs '\\':'n':cs -> '\n' : unesc cs
@@ -118,9 +121,9 @@ alexMove (Pn a l c) '\t' = Pn (a+1) l (((c+7) `div` 8)*8+1)
alexMove (Pn a l c) '\n' = Pn (a+1) (l+1) 1 alexMove (Pn a l c) '\n' = Pn (a+1) (l+1) 1
alexMove (Pn a l c) _ = Pn (a+1) l (c+1) alexMove (Pn a l c) _ = Pn (a+1) l (c+1)
type AlexInput = (Posn, -- current position, type AlexInput = (Posn, -- current position,
Char, -- previous char Char, -- previous char
BS.ByteString) -- current input string BS.ByteString) -- current input string
tokens :: BS.ByteString -> [Token] tokens :: BS.ByteString -> [Token]
tokens str = go (alexStartPos, '\n', str) tokens str = go (alexStartPos, '\n', str)
@@ -131,29 +134,31 @@ tokens str = go (alexStartPos, '\n', str)
AlexEOF -> [] AlexEOF -> []
AlexError (pos, _, _) -> [Err pos] AlexError (pos, _, _) -> [Err pos]
AlexSkip inp' len -> go inp' AlexSkip inp' len -> go inp'
AlexToken inp' len act -> act pos (BS.unpack (BS.take len str)) : (go inp') AlexToken inp' len act -> act pos (BS.take len str) : (go inp')
alexGetChar :: AlexInput -> Maybe (Char,AlexInput) alexGetChar :: AlexInput -> Maybe (Char,AlexInput)
alexGetChar (p,_,cs) | BS.null cs = Nothing alexGetChar (p, _, s) =
| otherwise = let c = BS.head cs case BS.uncons s of
cs' = BS.tail cs Nothing -> Nothing
p' = alexMove p c Just (c,s) ->
in p' `seq` cs' `seq` Just (c, (p', c, cs')) let p' = alexMove p c
in p' `seq` Just (c, (p', c, s))
alexInputPrevChar :: AlexInput -> Char alexInputPrevChar :: AlexInput -> Char
alexInputPrevChar (p, c, s) = c alexInputPrevChar (p, c, s) = c
alex_action_3 = tok (\p s -> PT p (TS $ share s)) alex_action_3 = tok (\p s -> PT p (eitherResIdent (T_PIdent . share) s))
alex_action_4 = tok (\p s -> PT p (eitherResIdent (T_LString . share) s)) alex_action_4 = tok (\p s -> PT p (eitherResIdent (T_LString . share) s))
alex_action_5 = tok (\p s -> PT p (eitherResIdent (T_PIdent . share) s)) alex_action_5 = tok (\p s -> PT p (eitherResIdent (T_PIdent . share) s))
alex_action_6 = tok (\p s -> PT p (eitherResIdent (TV . share) s)) alex_action_6 = tok (\p s -> PT p (eitherResIdent (TV . share) s))
alex_action_7 = tok (\p s -> PT p (TL $ share $ unescapeInitTail s)) alex_action_7 = tok (\p s -> PT p (TL $ share $ unescapeInitTail s))
alex_action_8 = tok (\p s -> PT p (TI $ share s)) alex_action_8 = tok (\p s -> PT p (TI $ share s))
alex_action_9 = tok (\p s -> PT p (TD $ share s)) alex_action_9 = tok (\p s -> PT p (TD $ share s))
{-# LINE 1 "GenericTemplate.hs" #-} {-# LINE 1 "templates/GenericTemplate.hs" #-}
{-# LINE 1 "templates/GenericTemplate.hs" #-}
{-# LINE 1 "<built-in>" #-} {-# LINE 1 "<built-in>" #-}
{-# LINE 1 "<command line>" #-} {-# LINE 1 "<command line>" #-}
{-# LINE 1 "GenericTemplate.hs" #-} {-# LINE 1 "templates/GenericTemplate.hs" #-}
-- ----------------------------------------------------------------------------- -- -----------------------------------------------------------------------------
-- ALEX TEMPLATE -- ALEX TEMPLATE
-- --
@@ -163,9 +168,9 @@ alex_action_9 = tok (\p s -> PT p (TD $ share s))
-- ----------------------------------------------------------------------------- -- -----------------------------------------------------------------------------
-- INTERNALS and main scanner engine -- INTERNALS and main scanner engine
{-# LINE 35 "GenericTemplate.hs" #-} {-# LINE 35 "templates/GenericTemplate.hs" #-}
{-# LINE 45 "GenericTemplate.hs" #-} {-# LINE 45 "templates/GenericTemplate.hs" #-}
data AlexAddr = AlexA# Addr# data AlexAddr = AlexA# Addr#

View File

@@ -1,10 +1,11 @@
-- -*- haskell -*- -- -*- haskell -*-
-- This Alex file was machine-generated by the BNF converter -- This Alex file was machine-generated by the BNF converter
{ {
module LexGF where {-# OPTIONS -fno-warn-incomplete-patterns #-}
module GF.Source.LexGF where
import ErrM import GF.Source.SharedString
import SharedString import qualified Data.ByteString.Char8 as BS
} }
@@ -15,16 +16,17 @@ $d = [0-9] -- digit
$i = [$l $d _ '] -- identifier character $i = [$l $d _ '] -- identifier character
$u = [\0-\255] -- universal: any character $u = [\0-\255] -- universal: any character
@rsyms = -- reserved words consisting of special symbols @rsyms = -- symbols and non-identifier-like reserved words
\; | \= | \{ | \} | \( | \) | \: | \- \> | \* \* | \, | \[ | \] | \- | \. | \| | \% | \? | \< | \> | \@ | \! | \* | \+ | \+ \+ | \\ | \= \> | \_ | \$ | \/ \; | \= | \{ | \} | \( | \) | \* \* | \: | \- \> | \, | \[ | \] | \- | \. | \| | \% | \? | \< | \> | \@ | \# | \! | \* | \+ | \+ \+ | \\ | \= \> | \_ | \$ | \/
:- :-
"--" [.]* ; -- Toss single line comments "--" [.]* ; -- Toss single line comments
"{-" ([$u # \-] | \- [$u # \}])* ("-")+ "}" ; "{-" ([$u # \-] | \- [$u # \}])* ("-")+ "}" ;
$white+ ; $white+ ;
@rsyms { tok (\p s -> PT p (TS $ share s)) } @rsyms { tok (\p s -> PT p (eitherResIdent (T_PIdent . share) s)) }
\' ($u # \')* \' { tok (\p s -> PT p (eitherResIdent (T_LString . share) s)) } \' ($u # \')* \' { tok (\p s -> PT p (eitherResIdent (T_LString . share) s)) }
(\_ | $l)($l | $d | \_ | \')* { tok (\p s -> PT p (eitherResIdent (T_PIdent . share) s)) }
$l $i* { tok (\p s -> PT p (eitherResIdent (TV . share) s)) } $l $i* { tok (\p s -> PT p (eitherResIdent (TV . share) s)) }
\" ([$u # [\" \\ \n]] | (\\ (\" | \\ | \' | n | t)))* \"{ tok (\p s -> PT p (TL $ share $ unescapeInitTail s)) } \" ([$u # [\" \\ \n]] | (\\ (\" | \\ | \' | n | t)))* \"{ tok (\p s -> PT p (TL $ share $ unescapeInitTail s)) }
@@ -36,17 +38,18 @@ $d+ \. $d+ (e (\-)? $d+)? { tok (\p s -> PT p (TD $ share s)) }
tok f p s = f p s tok f p s = f p s
share :: String -> String share :: BS.ByteString -> BS.ByteString
share = shareString share = shareString
data Tok = data Tok =
TS !String -- reserved words TS !BS.ByteString !Int -- reserved words and symbols
| TL !String -- string literals | TL !BS.ByteString -- string literals
| TI !String -- integer literals | TI !BS.ByteString -- integer literals
| TV !String -- identifiers | TV !BS.ByteString -- identifiers
| TD !String -- double precision float literals | TD !BS.ByteString -- double precision float literals
| TC !String -- character literals | TC !BS.ByteString -- character literals
| T_LString !String | T_LString !BS.ByteString
| T_PIdent !BS.ByteString
deriving (Eq,Show,Ord) deriving (Eq,Show,Ord)
@@ -63,18 +66,19 @@ posLineCol (Pn _ l c) = (l,c)
mkPosToken t@(PT p _) = (posLineCol p, prToken t) mkPosToken t@(PT p _) = (posLineCol p, prToken t)
prToken t = case t of prToken t = case t of
PT _ (TS s) -> s PT _ (TS s _) -> s
PT _ (TI s) -> s PT _ (TL s) -> s
PT _ (TV s) -> s PT _ (TI s) -> s
PT _ (TD s) -> s PT _ (TV s) -> s
PT _ (TC s) -> s PT _ (TD s) -> s
PT _ (TC s) -> s
PT _ (T_LString s) -> s PT _ (T_LString s) -> s
PT _ (T_PIdent s) -> s
_ -> show t
data BTree = N | B String Tok BTree BTree deriving (Show) data BTree = N | B BS.ByteString Tok BTree BTree deriving (Show)
eitherResIdent :: (String -> Tok) -> String -> Tok eitherResIdent :: (BS.ByteString -> Tok) -> BS.ByteString -> Tok
eitherResIdent tv s = treeFind resWords eitherResIdent tv s = treeFind resWords
where where
treeFind N = tv s treeFind N = tv s
@@ -82,11 +86,12 @@ eitherResIdent tv s = treeFind resWords
| s > a = treeFind right | s > a = treeFind right
| s == a = t | s == a = t
resWords = b "lincat" (b "def" (b "Type" (b "Str" (b "PType" (b "Lin" N N) N) (b "Tok" (b "Strs" N N) N)) (b "cat" (b "case" (b "abstract" N N) N) (b "data" (b "concrete" N N) N))) (b "include" (b "fun" (b "fn" (b "flags" N N) N) (b "in" (b "grammar" N N) N)) (b "interface" (b "instance" (b "incomplete" N N) N) (b "lin" (b "let" N N) N)))) (b "resource" (b "out" (b "of" (b "lintype" (b "lindef" N N) N) (b "oper" (b "open" N N) N)) (b "pattern" (b "param" (b "package" N N) N) (b "printname" (b "pre" N N) N))) (b "union" (b "table" (b "strs" (b "reuse" N N) N) (b "transfer" (b "tokenizer" N N) N)) (b "where" (b "variants" (b "var" N N) N) (b "with" N N)))) resWords = b "def" 39 (b "=>" 20 (b "++" 10 (b "(" 5 (b "$" 3 (b "#" 2 (b "!" 1 N N) N) (b "%" 4 N N)) (b "**" 8 (b "*" 7 (b ")" 6 N N) N) (b "+" 9 N N))) (b "/" 15 (b "->" 13 (b "-" 12 (b "," 11 N N) N) (b "." 14 N N)) (b "<" 18 (b ";" 17 (b ":" 16 N N) N) (b "=" 19 N N)))) (b "[" 30 (b "PType" 25 (b "@" 23 (b "?" 22 (b ">" 21 N N) N) (b "Lin" 24 N N)) (b "Tok" 28 (b "Strs" 27 (b "Str" 26 N N) N) (b "Type" 29 N N))) (b "case" 35 (b "_" 33 (b "]" 32 (b "\\" 31 N N) N) (b "abstract" 34 N N)) (b "concrete" 37 (b "cat" 36 N N) (b "data" 38 N N))))) (b "package" 58 (b "let" 49 (b "in" 44 (b "fun" 42 (b "fn" 41 (b "flags" 40 N N) N) (b "grammar" 43 N N)) (b "instance" 47 (b "incomplete" 46 (b "include" 45 N N) N) (b "interface" 48 N N))) (b "of" 54 (b "lindef" 52 (b "lincat" 51 (b "lin" 50 N N) N) (b "lintype" 53 N N)) (b "oper" 56 (b "open" 55 N N) (b "out" 57 N N)))) (b "transfer" 68 (b "resource" 63 (b "pre" 61 (b "pattern" 60 (b "param" 59 N N) N) (b "printname" 62 N N)) (b "table" 66 (b "strs" 65 (b "reuse" 64 N N) N) (b "tokenizer" 67 N N))) (b "with" 73 (b "variants" 71 (b "var" 70 (b "union" 69 N N) N) (b "where" 72 N N)) (b "|" 75 (b "{" 74 N N) (b "}" 76 N N)))))
where b s = B s (TS s) where b s n = let bs = BS.pack s
in B bs (TS bs n)
unescapeInitTail :: String -> String unescapeInitTail :: BS.ByteString -> BS.ByteString
unescapeInitTail = unesc . tail where unescapeInitTail = BS.pack . unesc . tail . BS.unpack where
unesc s = case s of unesc s = case s of
'\\':c:cs | elem c ['\"', '\\', '\''] -> c : unesc cs '\\':c:cs | elem c ['\"', '\\', '\''] -> c : unesc cs
'\\':'n':cs -> '\n' : unesc cs '\\':'n':cs -> '\n' : unesc cs
@@ -111,26 +116,28 @@ alexMove (Pn a l c) '\t' = Pn (a+1) l (((c+7) `div` 8)*8+1)
alexMove (Pn a l c) '\n' = Pn (a+1) (l+1) 1 alexMove (Pn a l c) '\n' = Pn (a+1) (l+1) 1
alexMove (Pn a l c) _ = Pn (a+1) l (c+1) alexMove (Pn a l c) _ = Pn (a+1) l (c+1)
type AlexInput = (Posn, -- current position, type AlexInput = (Posn, -- current position,
Char, -- previous char Char, -- previous char
String) -- current input string BS.ByteString) -- current input string
tokens :: String -> [Token] tokens :: BS.ByteString -> [Token]
tokens str = go (alexStartPos, '\n', str) tokens str = go (alexStartPos, '\n', str)
where where
go :: (Posn, Char, String) -> [Token] go :: AlexInput -> [Token]
go inp@(pos, _, str) = go inp@(pos, _, str) =
case alexScan inp 0 of case alexScan inp 0 of
AlexEOF -> [] AlexEOF -> []
AlexError (pos, _, _) -> fail $ show pos ++ ": lexical error" AlexError (pos, _, _) -> [Err pos]
AlexSkip inp' len -> go inp' AlexSkip inp' len -> go inp'
AlexToken inp' len act -> act pos (take len str) : (go inp') AlexToken inp' len act -> act pos (BS.take len str) : (go inp')
alexGetChar :: AlexInput -> Maybe (Char,AlexInput) alexGetChar :: AlexInput -> Maybe (Char,AlexInput)
alexGetChar (p, c, []) = Nothing alexGetChar (p, _, s) =
alexGetChar (p, _, (c:s)) = case BS.uncons s of
let p' = alexMove p c Nothing -> Nothing
in p' `seq` Just (c, (p', c, s)) Just (c,s) ->
let p' = alexMove p c
in p' `seq` Just (c, (p', c, s))
alexInputPrevChar :: AlexInput -> Char alexInputPrevChar :: AlexInput -> Char
alexInputPrevChar (p, c, s) = c alexInputPrevChar (p, c, s) = c

File diff suppressed because it is too large Load Diff

View File

@@ -1,100 +1,100 @@
-- This Happy file was machine-generated by the BNF converter -- This Happy file was machine-generated by the BNF converter
{ {
{-# OPTIONS -fno-warn-incomplete-patterns -fno-warn-overlapping-patterns #-} {-# OPTIONS -fno-warn-incomplete-patterns -fno-warn-overlapping-patterns #-}
module GF.Source.ParGF (pGrammar, pModDef, pOldGrammar, pExp, pModHeader, myLexer) where --H module GF.Source.ParGF where
import GF.Source.AbsGF --H import GF.Source.AbsGF
import GF.Source.LexGF --H import GF.Source.LexGF
import GF.Infra.Ident --H import GF.Data.ErrM
import GF.Data.ErrM --H import qualified Data.ByteString.Char8 as BS
} }
%name pGrammar Grammar %name pGrammar Grammar
%name pModDef ModDef %name pModDef ModDef
%name pOldGrammar OldGrammar %name pOldGrammar OldGrammar
%name pExp Exp
%partial pModHeader ModHeader %partial pModHeader ModHeader
%name pExp Exp
-- no lexer declaration -- no lexer declaration
%monad { Err } { thenM } { returnM } %monad { Err } { thenM } { returnM }
%tokentype { Token } %tokentype { Token }
%token %token
';' { PT _ (TS ";") } '!' { PT _ (TS _ 1) }
'=' { PT _ (TS "=") } '#' { PT _ (TS _ 2) }
'{' { PT _ (TS "{") } '$' { PT _ (TS _ 3) }
'}' { PT _ (TS "}") } '%' { PT _ (TS _ 4) }
'(' { PT _ (TS "(") } '(' { PT _ (TS _ 5) }
')' { PT _ (TS ")") } ')' { PT _ (TS _ 6) }
':' { PT _ (TS ":") } '*' { PT _ (TS _ 7) }
'->' { PT _ (TS "->") } '**' { PT _ (TS _ 8) }
'**' { PT _ (TS "**") } '+' { PT _ (TS _ 9) }
',' { PT _ (TS ",") } '++' { PT _ (TS _ 10) }
'[' { PT _ (TS "[") } ',' { PT _ (TS _ 11) }
']' { PT _ (TS "]") } '-' { PT _ (TS _ 12) }
'-' { PT _ (TS "-") } '->' { PT _ (TS _ 13) }
'.' { PT _ (TS ".") } '.' { PT _ (TS _ 14) }
'|' { PT _ (TS "|") } '/' { PT _ (TS _ 15) }
'%' { PT _ (TS "%") } ':' { PT _ (TS _ 16) }
'?' { PT _ (TS "?") } ';' { PT _ (TS _ 17) }
'<' { PT _ (TS "<") } '<' { PT _ (TS _ 18) }
'>' { PT _ (TS ">") } '=' { PT _ (TS _ 19) }
'@' { PT _ (TS "@") } '=>' { PT _ (TS _ 20) }
'#' { PT _ (TS "#") } '>' { PT _ (TS _ 21) }
'!' { PT _ (TS "!") } '?' { PT _ (TS _ 22) }
'*' { PT _ (TS "*") } '@' { PT _ (TS _ 23) }
'+' { PT _ (TS "+") } 'Lin' { PT _ (TS _ 24) }
'++' { PT _ (TS "++") } 'PType' { PT _ (TS _ 25) }
'\\' { PT _ (TS "\\") } 'Str' { PT _ (TS _ 26) }
'=>' { PT _ (TS "=>") } 'Strs' { PT _ (TS _ 27) }
'_' { PT _ (TS "_") } 'Tok' { PT _ (TS _ 28) }
'$' { PT _ (TS "$") } 'Type' { PT _ (TS _ 29) }
'/' { PT _ (TS "/") } '[' { PT _ (TS _ 30) }
'Lin' { PT _ (TS "Lin") } '\\' { PT _ (TS _ 31) }
'PType' { PT _ (TS "PType") } ']' { PT _ (TS _ 32) }
'Str' { PT _ (TS "Str") } '_' { PT _ (TS _ 33) }
'Strs' { PT _ (TS "Strs") } 'abstract' { PT _ (TS _ 34) }
'Tok' { PT _ (TS "Tok") } 'case' { PT _ (TS _ 35) }
'Type' { PT _ (TS "Type") } 'cat' { PT _ (TS _ 36) }
'abstract' { PT _ (TS "abstract") } 'concrete' { PT _ (TS _ 37) }
'case' { PT _ (TS "case") } 'data' { PT _ (TS _ 38) }
'cat' { PT _ (TS "cat") } 'def' { PT _ (TS _ 39) }
'concrete' { PT _ (TS "concrete") } 'flags' { PT _ (TS _ 40) }
'data' { PT _ (TS "data") } 'fn' { PT _ (TS _ 41) }
'def' { PT _ (TS "def") } 'fun' { PT _ (TS _ 42) }
'flags' { PT _ (TS "flags") } 'grammar' { PT _ (TS _ 43) }
'fn' { PT _ (TS "fn") } 'in' { PT _ (TS _ 44) }
'fun' { PT _ (TS "fun") } 'include' { PT _ (TS _ 45) }
'grammar' { PT _ (TS "grammar") } 'incomplete' { PT _ (TS _ 46) }
'in' { PT _ (TS "in") } 'instance' { PT _ (TS _ 47) }
'include' { PT _ (TS "include") } 'interface' { PT _ (TS _ 48) }
'incomplete' { PT _ (TS "incomplete") } 'let' { PT _ (TS _ 49) }
'instance' { PT _ (TS "instance") } 'lin' { PT _ (TS _ 50) }
'interface' { PT _ (TS "interface") } 'lincat' { PT _ (TS _ 51) }
'let' { PT _ (TS "let") } 'lindef' { PT _ (TS _ 52) }
'lin' { PT _ (TS "lin") } 'lintype' { PT _ (TS _ 53) }
'lincat' { PT _ (TS "lincat") } 'of' { PT _ (TS _ 54) }
'lindef' { PT _ (TS "lindef") } 'open' { PT _ (TS _ 55) }
'lintype' { PT _ (TS "lintype") } 'oper' { PT _ (TS _ 56) }
'of' { PT _ (TS "of") } 'out' { PT _ (TS _ 57) }
'open' { PT _ (TS "open") } 'package' { PT _ (TS _ 58) }
'oper' { PT _ (TS "oper") } 'param' { PT _ (TS _ 59) }
'out' { PT _ (TS "out") } 'pattern' { PT _ (TS _ 60) }
'package' { PT _ (TS "package") } 'pre' { PT _ (TS _ 61) }
'param' { PT _ (TS "param") } 'printname' { PT _ (TS _ 62) }
'pattern' { PT _ (TS "pattern") } 'resource' { PT _ (TS _ 63) }
'pre' { PT _ (TS "pre") } 'reuse' { PT _ (TS _ 64) }
'printname' { PT _ (TS "printname") } 'strs' { PT _ (TS _ 65) }
'resource' { PT _ (TS "resource") } 'table' { PT _ (TS _ 66) }
'reuse' { PT _ (TS "reuse") } 'tokenizer' { PT _ (TS _ 67) }
'strs' { PT _ (TS "strs") } 'transfer' { PT _ (TS _ 68) }
'table' { PT _ (TS "table") } 'union' { PT _ (TS _ 69) }
'tokenizer' { PT _ (TS "tokenizer") } 'var' { PT _ (TS _ 70) }
'transfer' { PT _ (TS "transfer") } 'variants' { PT _ (TS _ 71) }
'union' { PT _ (TS "union") } 'where' { PT _ (TS _ 72) }
'var' { PT _ (TS "var") } 'with' { PT _ (TS _ 73) }
'variants' { PT _ (TS "variants") } '{' { PT _ (TS _ 74) }
'where' { PT _ (TS "where") } '|' { PT _ (TS _ 75) }
'with' { PT _ (TS "with") } '}' { PT _ (TS _ 76) }
L_integ { PT _ (TI $$) } L_integ { PT _ (TI $$) }
L_quoted { PT _ (TL $$) } L_quoted { PT _ (TL $$) }
@@ -106,9 +106,9 @@ L_err { _ }
%% %%
Integer :: { Integer } : L_integ { (read $1) :: Integer } Integer :: { Integer } : L_integ { (read (BS.unpack $1)) :: Integer }
String :: { String } : L_quoted { $1 } String :: { String } : L_quoted { BS.unpack $1 }
Double :: { Double } : L_doubl { (read $1) :: Double } Double :: { Double } : L_doubl { (read (BS.unpack $1)) :: Double }
LString :: { LString} : L_LString { LString ($1)} LString :: { LString} : L_LString { LString ($1)}
PIdent :: { PIdent} : L_PIdent { PIdent (mkPosToken $1)} PIdent :: { PIdent} : L_PIdent { PIdent (mkPosToken $1)}
@@ -635,7 +635,7 @@ happyError ts =
case ts of case ts of
[] -> [] [] -> []
[Err _] -> " due to lexer error" [Err _] -> " due to lexer error"
_ -> " before " ++ unwords (map prToken (take 4 ts)) _ -> " before " ++ unwords (map (BS.unpack . prToken) (take 4 ts))
myLexer = tokens myLexer = tokens
} }

View File

@@ -4,7 +4,8 @@ module GF.Source.PrintGF where
-- pretty-printer generated by the BNF converter -- pretty-printer generated by the BNF converter
import GF.Source.AbsGF import GF.Source.AbsGF
import Char import Data.Char
import qualified Data.ByteString.Char8 as BS
-- the top-level printing method -- the top-level printing method
printTree :: Print a => a -> String printTree :: Print a => a -> String
@@ -79,11 +80,11 @@ instance Print Double where
instance Print LString where instance Print LString where
prt _ (LString i) = doc (showString i) prt _ (LString i) = doc (showString (BS.unpack i))
instance Print PIdent where instance Print PIdent where
prt _ (PIdent (_,i)) = doc (showString i) prt _ (PIdent (_,i)) = doc (showString (BS.unpack i))
prtList es = case es of prtList es = case es of
[x] -> (concatD [prt 0 x]) [x] -> (concatD [prt 0 x])
x:xs -> (concatD [prt 0 x , doc (showString ",") , prt 0 xs]) x:xs -> (concatD [prt 0 x , doc (showString ",") , prt 0 xs])
@@ -127,6 +128,7 @@ instance Print Transfer where
[] -> (concatD []) [] -> (concatD [])
x:xs -> (concatD [prt 0 x , prt 0 xs]) x:xs -> (concatD [prt 0 x , prt 0 xs])
instance Print ModType where instance Print ModType where
prt i e = case e of prt i e = case e of
MTAbstract pident -> prPrec i 0 (concatD [doc (showString "abstract") , prt 0 pident]) MTAbstract pident -> prPrec i 0 (concatD [doc (showString "abstract") , prt 0 pident])

View File

@@ -46,6 +46,23 @@ transTransfer x = case x of
TransferOut open -> failure x TransferOut open -> failure x
transModHeader :: ModHeader -> Result
transModHeader x = case x of
MModule2 complmod modtype modheaderbody -> failure x
transModHeaderBody :: ModHeaderBody -> Result
transModHeaderBody x = case x of
MBody2 extend opens -> failure x
MNoBody2 includeds -> failure x
MWith2 included opens -> failure x
MWithBody2 included opens0 opens -> failure x
MWithE2 includeds included opens -> failure x
MWithEBody2 includeds included opens0 opens -> failure x
MReuse2 pident -> failure x
MUnion2 includeds -> failure x
transModType :: ModType -> Result transModType :: ModType -> Result
transModType x = case x of transModType x = case x of
MTAbstract pident -> failure x MTAbstract pident -> failure x

View File

@@ -27,6 +27,7 @@ import qualified GF.Grammar.Macros as M
import qualified GF.Compile.Update as U import qualified GF.Compile.Update as U
import qualified GF.Infra.Option as GO import qualified GF.Infra.Option as GO
import qualified GF.Compile.ModDeps as GD import qualified GF.Compile.ModDeps as GD
import GF.Grammar.Predef
import GF.Infra.Ident import GF.Infra.Ident
import GF.Source.AbsGF import GF.Source.AbsGF
import GF.Source.PrintGF import GF.Source.PrintGF
@@ -37,6 +38,7 @@ import GF.Infra.Option
import Control.Monad import Control.Monad
import Data.Char import Data.Char
import Data.List (genericReplicate) import Data.List (genericReplicate)
import qualified Data.ByteString.Char8 as BS
-- based on the skeleton Haskell module generated by the BNF converter -- based on the skeleton Haskell module generated by the BNF converter
@@ -45,9 +47,6 @@ type Result = Err String
failure :: Show a => a -> Err b failure :: Show a => a -> Err b
failure x = Bad $ "Undefined case: " ++ show x failure x = Bad $ "Undefined case: " ++ show x
prPIdent :: PIdent -> String
prPIdent (PIdent (_,c)) = c
getIdentPos :: PIdent -> Err (Ident,Int) getIdentPos :: PIdent -> Err (Ident,Int)
getIdentPos x = case x of getIdentPos x = case x of
PIdent ((line,_),c) -> return (IC c,line) PIdent ((line,_),c) -> return (IC c,line)
@@ -225,7 +224,7 @@ transAbsDef x = case x of
DefFunData fundefs -> do DefFunData fundefs -> do
fundefs' <- mapM transFunDef fundefs fundefs' <- mapM transFunDef fundefs
returnl $ returnl $
[(cat, G.AbsCat nope (yes [M.cn fun])) | (funs,typ) <- fundefs', [(cat, G.AbsCat nope (yes [G.Cn fun])) | (funs,typ) <- fundefs',
fun <- funs, fun <- funs,
Ok (_,cat) <- [M.valCat typ] Ok (_,cat) <- [M.valCat typ]
] ++ ] ++
@@ -257,6 +256,9 @@ returnl = return . Left
transFlagDef :: FlagDef -> Err GO.Option transFlagDef :: FlagDef -> Err GO.Option
transFlagDef x = case x of transFlagDef x = case x of
FlagDef f x -> return $ GO.Opt (prPIdent f,[prPIdent x]) FlagDef f x -> return $ GO.Opt (prPIdent f,[prPIdent x])
where
prPIdent (PIdent (_,c)) = BS.unpack c
-- | Cat definitions can also return some fun defs -- | Cat definitions can also return some fun defs
-- if it is a list category definition -- if it is a list category definition
@@ -280,7 +282,7 @@ transCatDef x = case x of
consId = mkConsId id' consId = mkConsId id'
catd0@(c,G.AbsCat (Yes cont0) _) <- cat li ddecls catd0@(c,G.AbsCat (Yes cont0) _) <- cat li ddecls
let let
catd = (c,G.AbsCat (Yes cont0) (Yes [M.cn baseId,M.cn consId])) catd = (c,G.AbsCat (Yes cont0) (Yes [G.Cn baseId,G.Cn consId]))
cont = [(mkId x i,ty) | (i,(x,ty)) <- zip [0..] cont0] cont = [(mkId x i,ty) | (i,(x,ty)) <- zip [0..] cont0]
xs = map (G.Vr . fst) cont xs = map (G.Vr . fst) cont
cd = M.mkDecl (M.mkApp (G.Vr id') xs) cd = M.mkDecl (M.mkApp (G.Vr id') xs)
@@ -290,7 +292,7 @@ transCatDef x = case x of
constyp = M.mkProdSimple (cont ++ [cd, M.mkDecl lc]) lc constyp = M.mkProdSimple (cont ++ [cd, M.mkDecl lc]) lc
consfund = (consId, G.AbsFun (yes constyp) (yes G.EData)) consfund = (consId, G.AbsFun (yes constyp) (yes G.EData))
return [catd,nilfund,consfund] return [catd,nilfund,consfund]
mkId x i = if isWildIdent x then (mkIdent "x" i) else x mkId x i = if isWildIdent x then (varX i) else x
transFunDef :: FunDef -> Err ([Ident], G.Type) transFunDef :: FunDef -> Err ([Ident], G.Type)
transFunDef x = case x of transFunDef x = case x of
@@ -434,10 +436,10 @@ transExp x = case x of
EQConstr m c -> liftM2 G.QC (transIdent m) (transIdent c) EQConstr m c -> liftM2 G.QC (transIdent m) (transIdent c)
EQCons m c -> liftM2 G.Q (transIdent m) (transIdent c) EQCons m c -> liftM2 G.Q (transIdent m) (transIdent c)
EString str -> return $ G.K str EString str -> return $ G.K str
ESort sort -> liftM G.Sort $ transSort sort ESort sort -> return $ G.Sort $ transSort sort
EInt n -> return $ G.EInt n EInt n -> return $ G.EInt n
EFloat n -> return $ G.EFloat n EFloat n -> return $ G.EFloat n
EMeta -> return $ M.meta $ M.int2meta 0 EMeta -> return $ G.Meta $ M.int2meta 0
EEmpty -> return G.Empty EEmpty -> return G.Empty
-- [ C x_1 ... x_n ] becomes (ListC x_1 ... x_n) -- [ C x_1 ... x_n ] becomes (ListC x_1 ... x_n)
EList i es -> do EList i es -> do
@@ -499,7 +501,7 @@ transExp x = case x of
EPattType typ -> liftM G.EPattType (transExp typ) EPattType typ -> liftM G.EPattType (transExp typ)
EPatt patt -> liftM G.EPatt (transPatt patt) EPatt patt -> liftM G.EPatt (transPatt patt)
ELString (LString str) -> return $ G.K str ELString (LString str) -> return $ G.K (BS.unpack str) -- use the grammar encoding here
ELin id -> liftM G.LiT $ transIdent id ELin id -> liftM G.LiT $ transIdent id
EEqs eqs -> liftM G.Eqs $ mapM transEquation eqs EEqs eqs -> liftM G.Eqs $ mapM transEquation eqs
@@ -527,10 +529,10 @@ erecord2term ds = do
(lab,(Just ty,Nothing)) -> mapM tryRT fs >>= return . Left (lab,(Just ty,Nothing)) -> mapM tryRT fs >>= return . Left
_ -> mapM tryR fs >>= return . Right _ -> mapM tryR fs >>= return . Right
tryRT f = case f of tryRT f = case f of
(lab,(Just ty,Nothing)) -> return (M.ident2label lab,ty) (lab,(Just ty,Nothing)) -> return (G.ident2label lab,ty)
_ -> Bad $ "illegal record type field" +++ GP.prt (fst f) --- manifest fields ?! _ -> Bad $ "illegal record type field" +++ GP.prt (fst f) --- manifest fields ?!
tryR f = case f of tryR f = case f of
(lab,(mty, Just t)) -> return (M.ident2label lab,(mty,t)) (lab,(mty, Just t)) -> return (G.ident2label lab,(mty,t))
_ -> Bad $ "illegal record field" +++ GP.prt (fst f) _ -> Bad $ "illegal record field" +++ GP.prt (fst f)
@@ -552,16 +554,16 @@ locdef2fields d = case d of
trLabel :: Label -> Err G.Label trLabel :: Label -> Err G.Label
trLabel x = case x of trLabel x = case x of
-- this case is for bward compatibility and should be removed
LIdent (PIdent (_,'v':ds@(_:_))) | all isDigit ds -> return $ G.LVar $ readIntArg ds
LIdent (PIdent (_, s)) -> return $ G.LIdent s LIdent (PIdent (_, s)) -> return $ G.LIdent s
LVar x -> return $ G.LVar $ fromInteger x LVar x -> return $ G.LVar $ fromInteger x
transSort :: Sort -> Ident
transSort Sort_Type = cType
transSort Sort_PType = cPType
transSort Sort_Tok = cTok
transSort Sort_Str = cStr
transSort Sort_Strs = cStrs
transSort :: Sort -> Err String
transSort x = case x of
_ -> return $ printTree x
{- {-
--- no more used 7/1/2006 AR --- no more used 7/1/2006 AR
@@ -703,7 +705,7 @@ transOldGrammar opts name0 x = case x of
resName = identPI $ maybe ("Res" ++ lang) id $ getOptVal opts useResName resName = identPI $ maybe ("Res" ++ lang) id $ getOptVal opts useResName
cncName = identPI $ maybe lang id $ getOptVal opts useCncName cncName = identPI $ maybe lang id $ getOptVal opts useCncName
identPI s = PIdent ((0,0),s) identPI s = PIdent ((0,0),BS.pack s)
(beg,rest) = span (/='.') name (beg,rest) = span (/='.') name
(topic,lang) = case rest of -- to avoid overwriting old files (topic,lang) = case rest of -- to avoid overwriting old files
@@ -725,8 +727,8 @@ transInclude x = case x of
FDot filename -> '.' : trans filename FDot filename -> '.' : trans filename
FMinus filename -> '-' : trans filename FMinus filename -> '-' : trans filename
FAddId (PIdent (_, s)) filename -> modif s ++ trans filename FAddId (PIdent (_, s)) filename -> modif s ++ trans filename
modif s = let s' = init s ++ [toLower (last s)] in modif s = let s' = BS.snoc (BS.init s) (toLower (BS.last s)) in
if elem s' newReservedWords then s' else s BS.unpack (if elem (BS.unpack s') newReservedWords then s' else s)
--- unsafe hack ; cf. GetGrammar.oldLexer --- unsafe hack ; cf. GetGrammar.oldLexer
@@ -740,16 +742,16 @@ termInPattern t = M.mkAbs xx $ G.R [(s, (Nothing, toP body))] where
toP t = case t of toP t = case t of
G.Vr x -> G.P t s G.Vr x -> G.P t s
_ -> M.composSafeOp toP t _ -> M.composSafeOp toP t
s = G.LIdent "s" s = G.LIdent (BS.pack "s")
(xx,body) = abss [] t (xx,body) = abss [] t
abss xs t = case t of abss xs t = case t of
G.Abs x b -> abss (x:xs) b G.Abs x b -> abss (x:xs) b
_ -> (reverse xs,t) _ -> (reverse xs,t)
mkListId,mkConsId,mkBaseId :: Ident -> Ident mkListId,mkConsId,mkBaseId :: Ident -> Ident
mkListId = prefixId "List" mkListId = prefixId (BS.pack "List")
mkConsId = prefixId "Cons" mkConsId = prefixId (BS.pack "Cons")
mkBaseId = prefixId "Base" mkBaseId = prefixId (BS.pack "Base")
prefixId :: String -> Ident -> Ident prefixId :: BS.ByteString -> Ident -> Ident
prefixId pref id = IC (pref ++ prIdent id) prefixId pref id = identC (BS.append pref (ident2bs id))

View File

@@ -20,7 +20,6 @@ import qualified GF.Canon.GFC as GFC
import GF.Grammar.TypeCheck import GF.Grammar.TypeCheck
import GF.Grammar.LookAbs import GF.Grammar.LookAbs
import GF.Grammar.AbsCompute import GF.Grammar.AbsCompute
import GF.Grammar.Macros (errorCat)
import GF.Data.Operations import GF.Data.Operations
import GF.Data.Zipper import GF.Data.Zipper
@@ -51,7 +50,7 @@ actVal :: State -> Val
actVal = valNode . nodeTree . actTree actVal = valNode . nodeTree . actTree
actCat :: State -> Cat actCat :: State -> Cat
actCat = errVal errorCat . val2cat . actVal ---- undef actCat = errVal (cMeta,cMeta) . val2cat . actVal ---- undef
actAtom :: State -> Atom actAtom :: State -> Atom
actAtom = atomTree . actTree actAtom = atomTree . actTree

View File

@@ -4,7 +4,7 @@ include config.mk
GHMAKE=$(GHC) --make GHMAKE=$(GHC) --make
GHCXMAKE=ghcxmake GHCXMAKE=ghcxmake
GHCFLAGS+= -fglasgow-exts GHCFLAGS+= -fglasgow-exts
GHCOPTFLAGS=-O2 GHCOPTFLAGS=-O2 -prof
GHCFUDFLAG= GHCFUDFLAG=
JAVAFLAGS=-target 1.4 -source 1.4 JAVAFLAGS=-target 1.4 -source 1.4
GFEDITOR=JavaGUI2 GFEDITOR=JavaGUI2
@@ -207,7 +207,7 @@ gfc: gf3
gfi: gf3 gfi: gf3
gf3: gf3:
$(GHMAKE) $(GHCOPTFLAGS) $(GHCFLAGS) -o gf3 GF/Devel/GF.hs $(GHMAKE) $(GHCOPTFLAGS) -o gf3 GF/Devel/GF.hs
strip $(GF3_EXE) strip $(GF3_EXE)
mv $(GF3_EXE) ../bin/ mv $(GF3_EXE) ../bin/

View File

@@ -114,9 +114,9 @@ evalCncInfo opts gr cnc abs (c,info) = errIn ("optimizing" +++ prt c) $ case inf
pde' <- case (ptyp,pde) of pde' <- case (ptyp,pde) of
(Yes typ, Yes de) -> (Yes typ, Yes de) ->
liftM yes $ pEval ([(strVar, typeStr)], typ) de liftM yes $ pEval ([(varStr, typeStr)], typ) de
(Yes typ, Nope) -> (Yes typ, Nope) ->
liftM yes $ mkLinDefault gr typ >>= partEval noOptions gr ([(strVar, typeStr)],typ) liftM yes $ mkLinDefault gr typ >>= partEval noOptions gr ([(varStr, typeStr)],typ)
(May b, Nope) -> (May b, Nope) ->
return $ May b return $ May b
_ -> return pde -- indirection _ -> return pde -- indirection
@@ -222,7 +222,7 @@ recordExpand typ trm = case unComputed typ of
mkLinDefault :: SourceGrammar -> Type -> Err Term mkLinDefault :: SourceGrammar -> Type -> Err Term
mkLinDefault gr typ = do mkLinDefault gr typ = do
case unComputed typ of case unComputed typ of
RecType lts -> mapPairsM mkDefField lts >>= (return . Abs strVar . R . mkAssign) RecType lts -> mapPairsM mkDefField lts >>= (return . Abs varStr . R . mkAssign)
_ -> prtBad "linearization type must be a record type, not" typ _ -> prtBad "linearization type must be a record type, not" typ
where where
mkDefField typ = case unComputed typ of mkDefField typ = case unComputed typ of
@@ -230,7 +230,7 @@ mkLinDefault gr typ = do
t' <- mkDefField t t' <- mkDefField t
let T _ cs = mkWildCases t' let T _ cs = mkWildCases t'
return $ T (TWild p) cs return $ T (TWild p) cs
Sort "Str" -> return $ Vr strVar Sort "Str" -> return $ Vr varStr
QC q p -> lookupFirstTag gr q p QC q p -> lookupFirstTag gr q p
RecType r -> do RecType r -> do
let (ls,ts) = unzip r let (ls,ts) = unzip r