Step towards parsing the initial generation state from cmdline.
Chris Pressey
1 year, 11 months ago
21 | 21 |
-> shell command "bin/fountain generate %(test-body-file)"
|
22 | 22 |
|
23 | 23 |
### 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.
|
24 | 27 |
|
25 | 28 |
-> Tests for functionality "Load Fountain Grammar"
|
26 | 29 |
|
|
50 | 53 |
|
51 | 54 |
### Preprocessing
|
52 | 55 |
|
|
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 |
|
53 | 59 |
-> Tests for functionality "Preprocess Fountain Grammar"
|
54 | 60 |
|
55 | 61 |
Sequence.
|
|
75 | 81 |
===> 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")]]])]
|
76 | 82 |
|
77 | 83 |
### Parsing
|
|
84 |
|
|
85 |
OK _now_ we get into testing the language.
|
78 | 86 |
|
79 | 87 |
-> Tests for functionality "Parse using Fountain Grammar"
|
80 | 88 |
|
0 | |
module Language.Fountain.Generator (generateFrom, obtainResult) where
|
|
0 |
module Language.Fountain.Generator (constructState, generateFrom, obtainResult) where
|
1 | 1 |
|
2 | 2 |
import Language.Fountain.Grammar
|
3 | 3 |
import Language.Fountain.Constraint
|
|
92 | 92 |
Just $ update (\i -> Just (i - 1)) v st
|
93 | 93 |
|
94 | 94 |
|
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)
|
97 | 100 |
where
|
98 | 101 |
revgen (Generating s a) = Generating (reverse s) a
|
99 | 102 |
revgen other = other
|
6 | 6 |
| NT String
|
7 | 7 |
deriving (Show, Ord, Eq)
|
8 | 8 |
|
|
9 |
|
9 | 10 |
data Expr = Seq [Expr]
|
10 | 11 |
| 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]
|
12 | 18 |
| Term Term
|
13 | 19 |
| Constraint Constraint
|
14 | 20 |
deriving (Show, Ord, Eq)
|
15 | 21 |
|
|
22 |
|
16 | 23 |
data Grammar = Grammar [(Term, Expr)]
|
17 | 24 |
deriving (Show, Ord, Eq)
|
18 | |
|
19 | 25 |
|
20 | 26 |
startSymbol (Grammar ((term, _) : _)) = term
|
21 | 27 |
|
22 | 28 |
production nt (Grammar ((term, expr) : rest)) =
|
23 | 29 |
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.
|
37 | 37 |
let finalState = Parser.parseFrom grammar text
|
38 | 38 |
putStrLn $ if (dumpState flags) then show finalState else formatParseResult $ Parser.obtainResult finalState
|
39 | 39 |
exitWith $ either (\msg -> ExitFailure 1) (\remaining -> ExitSuccess) $ Parser.obtainResult finalState
|
40 | |
["generate", grammarFileName] -> do
|
|
40 |
("generate":grammarFileName:initialParams) -> do
|
41 | 41 |
grammar <- loadSource grammarFileName
|
42 | 42 |
let grammar' = Preprocessor.preprocessGrammar grammar
|
43 | |
let finalState = Generator.generateFrom grammar'
|
|
43 |
let initialState = Generator.constructState initialParams
|
|
44 |
let finalState = Generator.generateFrom grammar' initialState
|
44 | 45 |
putStrLn $ if (dumpState flags) then show finalState else formatGenerateResult $ Generator.obtainResult finalState
|
45 | 46 |
exitWith $ either (\msg -> ExitFailure 1) (\remaining -> ExitSuccess) $ Generator.obtainResult finalState
|
46 | 47 |
_ -> do
|