mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-23 03:32:51 -06:00
added a command for tree operations and started a module for defining them
This commit is contained in:
@@ -27,6 +27,8 @@ import GF.Command.Messages
|
|||||||
import GF.Text.Lexing
|
import GF.Text.Lexing
|
||||||
import GF.Text.Transliterations
|
import GF.Text.Transliterations
|
||||||
|
|
||||||
|
import GF.Command.TreeOperations ---- temporary place for typecheck and compute
|
||||||
|
|
||||||
import GF.Data.Operations
|
import GF.Data.Operations
|
||||||
import GF.Text.Coding
|
import GF.Text.Coding
|
||||||
|
|
||||||
@@ -367,6 +369,23 @@ allCommands cod pgf = Map.fromList [
|
|||||||
exec = \opts -> return . fromString . stringOps (map prOpt opts) . toString,
|
exec = \opts -> return . fromString . stringOps (map prOpt opts) . toString,
|
||||||
options = stringOpOptions
|
options = stringOpOptions
|
||||||
}),
|
}),
|
||||||
|
("pt", emptyCommandInfo {
|
||||||
|
longname = "put_tree",
|
||||||
|
syntax = "ps OPT? TREE",
|
||||||
|
synopsis = "return a tree, possibly processed with a function",
|
||||||
|
explanation = unlines [
|
||||||
|
"Returns a tree obtained from its argument tree by applying",
|
||||||
|
"tree processing functions in the order given in the command line",
|
||||||
|
"option list. Thus 'pt -f -g s' returns g (f s). Typical tree processors",
|
||||||
|
"are type checking and semantic computation."
|
||||||
|
],
|
||||||
|
examples = [
|
||||||
|
"pt -compute (plus one two) -- compute value",
|
||||||
|
"p \"foo\" | pt -typecheck -- type check parse results"
|
||||||
|
],
|
||||||
|
exec = \opts -> return . fromTrees . treeOps (map prOpt opts),
|
||||||
|
options = treeOpOptions
|
||||||
|
}),
|
||||||
("q", emptyCommandInfo {
|
("q", emptyCommandInfo {
|
||||||
longname = "quit",
|
longname = "quit",
|
||||||
synopsis = "exit GF interpreter"
|
synopsis = "exit GF interpreter"
|
||||||
@@ -588,6 +607,9 @@ allCommands cod pgf = Map.fromList [
|
|||||||
stringOps opts s = foldr app s (reverse opts) where
|
stringOps opts s = foldr app s (reverse opts) where
|
||||||
app f = maybe id id (stringOp f)
|
app f = maybe id id (stringOp f)
|
||||||
|
|
||||||
|
treeOps opts s = foldr app s (reverse opts) where
|
||||||
|
app f = maybe id id (treeOp f)
|
||||||
|
|
||||||
showAsString t = case t of
|
showAsString t = case t of
|
||||||
Lit (LStr s) -> s
|
Lit (LStr s) -> s
|
||||||
_ -> "\n" ++ showTree t --- newline needed in other cases than the first
|
_ -> "\n" ++ showTree t --- newline needed in other cases than the first
|
||||||
@@ -619,6 +641,8 @@ stringOpOptions = [
|
|||||||
("words","lexer that assumes tokens separated by spaces (default)")
|
("words","lexer that assumes tokens separated by spaces (default)")
|
||||||
]
|
]
|
||||||
|
|
||||||
|
treeOpOptions = [(op,expl) | (op,(expl,_)) <- allTreeOps]
|
||||||
|
|
||||||
translationQuiz :: String -> PGF -> Language -> Language -> Category -> IO ()
|
translationQuiz :: String -> PGF -> Language -> Language -> Category -> IO ()
|
||||||
translationQuiz cod pgf ig og cat = do
|
translationQuiz cod pgf ig og cat = do
|
||||||
tts <- translationList pgf ig og cat infinity
|
tts <- translationList pgf ig og cat infinity
|
||||||
|
|||||||
58
src/GF/Command/TreeOperations.hs
Normal file
58
src/GF/Command/TreeOperations.hs
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
module GF.Command.TreeOperations (
|
||||||
|
treeOp,
|
||||||
|
allTreeOps
|
||||||
|
--typeCheck,
|
||||||
|
--compute
|
||||||
|
) where
|
||||||
|
|
||||||
|
import GF.Compile.TypeCheck
|
||||||
|
import GF.Compile.AbsCompute
|
||||||
|
|
||||||
|
-- for conversions
|
||||||
|
import PGF.Data
|
||||||
|
--import GF.Compile.GrammarToGFCC (mkType,mkExp)
|
||||||
|
import GF.Grammar.Grammar
|
||||||
|
|
||||||
|
|
||||||
|
type TreeOp = [Tree] -> [Tree]
|
||||||
|
|
||||||
|
treeOp :: String -> Maybe TreeOp
|
||||||
|
treeOp f = fmap snd $ lookup f allTreeOps
|
||||||
|
|
||||||
|
allTreeOps :: [(String,(String,TreeOp))]
|
||||||
|
allTreeOps = [
|
||||||
|
("compute",("compute by using semantic definitions (def)",
|
||||||
|
id)),
|
||||||
|
("smallest",("sort trees from smallest to largest, in number of nodes",
|
||||||
|
id)),
|
||||||
|
("typecheck",("type check and solve metavariables; reject if incorrect",
|
||||||
|
id))
|
||||||
|
]
|
||||||
|
|
||||||
|
typeCheck :: PGF -> Tree -> (Tree,(Bool,[String]))
|
||||||
|
typeCheck pgf t = (t,(True,[]))
|
||||||
|
|
||||||
|
compute :: PGF -> Tree -> Tree
|
||||||
|
compute pgf t = t
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
{-
|
||||||
|
data Tree =
|
||||||
|
Abs [CId] Tree -- ^ lambda abstraction. The list of variables is non-empty
|
||||||
|
| Var CId -- ^ variable
|
||||||
|
| Fun CId [Tree] -- ^ function application
|
||||||
|
| Lit Literal -- ^ literal
|
||||||
|
| Meta Int -- ^ meta variable
|
||||||
|
|
||||||
|
data Literal =
|
||||||
|
LStr String -- ^ string constant
|
||||||
|
| LInt Integer -- ^ integer constant
|
||||||
|
| LFlt Double -- ^ floating point constant
|
||||||
|
|
||||||
|
mkType :: A.Type -> C.Type
|
||||||
|
mkType t = case GM.typeForm t of
|
||||||
|
Ok (hyps,(_,cat),args) -> C.DTyp (mkContext hyps) (i2i cat) (map mkExp args)
|
||||||
|
|
||||||
|
mkExp :: A.Term -> C.Expr
|
||||||
|
-}
|
||||||
Reference in New Issue
Block a user