git @ Cat's Eye Technologies Fountain / 4afe229
Step towards parsing the initial generation state from cmdline. Chris Pressey 1 year, 11 months ago
4 changed file(s) with 25 addition(s) and 12 deletion(s). Raw diff Collapse all Expand all
2121 -> shell command "bin/fountain generate %(test-body-file)"
2222
2323 ### Loading
24
25 Note, these tests are testing the implementation, not the language.
26 Ideally they should be moved out of here, into their own test suite.
2427
2528 -> Tests for functionality "Load Fountain Grammar"
2629
5053
5154 ### Preprocessing
5255
56 Note, these tests are testing the implementation, not the language.
57 Ideally they should be moved out of here, into their own test suite.
58
5359 -> Tests for functionality "Preprocess Fountain Grammar"
5460
5561 Sequence.
7581 ===> Grammar [(NT "Goal",Alt [Seq [Constraint (Arb (Var "n")),Constraint (UnifyConst (Var "a") 0),Loop (Alt [Seq [Term (T 'a'),Constraint (Inc (Var "a") 1)]]) [UnifyVar (Var "a") (Var "n"),UnifyConst (Var "b") 0],Loop (Alt [Seq [Term (T 'b'),Constraint (Inc (Var "b") 1)]]) [UnifyVar (Var "b") (Var "n"),UnifyConst (Var "c") 0],Loop (Alt [Seq [Term (T 'c'),Constraint (Inc (Var "c") 1)]]) [UnifyVar (Var "c") (Var "n")]]])]
7682
7783 ### Parsing
84
85 OK _now_ we get into testing the language.
7886
7987 -> Tests for functionality "Parse using Fountain Grammar"
8088
0 module Language.Fountain.Generator (generateFrom, obtainResult) where
0 module Language.Fountain.Generator (constructState, generateFrom, obtainResult) where
11
22 import Language.Fountain.Grammar
33 import Language.Fountain.Constraint
9292 Just $ update (\i -> Just (i - 1)) v st
9393
9494
95 generateFrom :: Grammar -> GenState
96 generateFrom g = revgen $ gen g (Generating "" empty) (production (startSymbol g) g)
95 constructState :: [String] -> GenState
96 constructState _ = (Generating "" empty) -- FIXME
97
98 generateFrom :: Grammar -> GenState -> GenState
99 generateFrom g state = revgen $ gen g state (production (startSymbol g) g)
97100 where
98101 revgen (Generating s a) = Generating (reverse s) a
99102 revgen other = other
66 | NT String
77 deriving (Show, Ord, Eq)
88
9
910 data Expr = Seq [Expr]
1011 | Alt [Expr]
11 | Loop Expr [Constraint] -- see below
12 --
13 -- In the case of a Loop, there is a post-processing step
14 -- that copies any constraints following the Loop, into the Loop
15 -- itself. This is to make the generator's job easier.
16 --
17 | Loop Expr [Constraint]
1218 | Term Term
1319 | Constraint Constraint
1420 deriving (Show, Ord, Eq)
1521
22
1623 data Grammar = Grammar [(Term, Expr)]
1724 deriving (Show, Ord, Eq)
18
1925
2026 startSymbol (Grammar ((term, _) : _)) = term
2127
2228 production nt (Grammar ((term, expr) : rest)) =
2329 if term == nt then expr else production nt (Grammar rest)
24
25
26 -- In the case of a Loop, there will be a post-processing step
27 -- that copies any constraints following the Loop, into the Loop
28 -- itself. This is to make the generator's life easier.
3737 let finalState = Parser.parseFrom grammar text
3838 putStrLn $ if (dumpState flags) then show finalState else formatParseResult $ Parser.obtainResult finalState
3939 exitWith $ either (\msg -> ExitFailure 1) (\remaining -> ExitSuccess) $ Parser.obtainResult finalState
40 ["generate", grammarFileName] -> do
40 ("generate":grammarFileName:initialParams) -> do
4141 grammar <- loadSource grammarFileName
4242 let grammar' = Preprocessor.preprocessGrammar grammar
43 let finalState = Generator.generateFrom grammar'
43 let initialState = Generator.constructState initialParams
44 let finalState = Generator.generateFrom grammar' initialState
4445 putStrLn $ if (dumpState flags) then show finalState else formatGenerateResult $ Generator.obtainResult finalState
4546 exitWith $ either (\msg -> ExitFailure 1) (\remaining -> ExitSuccess) $ Generator.obtainResult finalState
4647 _ -> do