File: Generate.lhs

package info (click to toggle)
frown 0.6.2.3-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 8,440 kB
  • ctags: 247
  • sloc: haskell: 35,175; makefile: 173; yacc: 23
file content (153 lines) | stat: -rw-r--r-- 7,047 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%   Frown --- An LALR(k) parser generator for Haskell 98                      %
%   Copyright (C) 2001-2005 Ralf Hinze                                        %
%                                                                             %
%   This program is free software; you can redistribute it and/or modify      %
%   it under the terms of the GNU General Public License (version 2) as       %
%   published by the Free Software Foundation.                                %
%                                                                             %
%   This program is distributed in the hope that it will be useful,           %
%   but WITHOUT ANY WARRANTY; without even the implied warranty of            %
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             %
%   GNU General Public License for more details.                              %
%                                                                             %
%   You should have received a copy of the GNU General Public License         %
%   along with this program; see the file COPYING.  If not, write to          %
%   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,          %
%   Boston, MA 02111-1307, USA.                                               %
%                                                                             %
%   Contact information                                                       %
%   Email:      Ralf Hinze <ralf@cs.uni-bonn.de>                              %
%   Homepage:   http://www.informatik.uni-bonn.de/~ralf/                      %
%   Paper mail: Dr. Ralf Hinze                                                %
%               Institut für Informatik III                                   %
%               Universität Bonn                                              %
%               Römerstraße 164                                               %
%               53117 Bonn, Germany                                           %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%-------------------------------=  --------------------------------------------
\section{|Generate.lhs|}
%-------------------------------=  --------------------------------------------

> module Generate
> where
> import Atom
> import Lexer2
> import Haskell
> import Grammar
> import LR0
> import qualified OrdUniqListSet as Set
> import OrdUniqListSet         (  Set  )
> import Base
> --import Char                   (  toLower, toUpper  )
> import Data.List                   (  intersperse  )

Symbols.

> argsOf                        :: Symbol -> [Expr]
>-- argsOf                        =  map fst . quotesOf . pattern
> argsOf                        =  attributes
>
> typesOf                       :: Symbol -> [Type]
>-- typesOf                       =  map snd . quotesOf . pattern
> typesOf                       =  types

> fresh, anonymous, bottoms     :: Symbol -> Pat
> fresh v                       =  combine (pattern v) [ v_i i | i <- [1 ..] ]
> anonymous v                   =  replace anon (pattern v)
> bottoms v                     =  replace hsUndefined (pattern v)

> genVars                       :: Symbol -> [Expr]
> genVars v                     =  [ v_i i | i <- [1 .. length (typesOf  v)] ]

Monadic actions.

> --eval args e                   =  foldr eval e (zip [1 ..] args)
> --    where eval (i, ts) x      =  ts <>>=> Fun [v_i i] x

> evaluate                      :: [Pat] -> ([Pat] -> Expr) -> Expr
> evaluate args cont            =  eval ([ (v_i i, arg) | (i, arg) <- zip [1 ..] args]) []
>   where eval [] es            =  cont es
>         eval ((v, ts) : as) es=  case monadic ts of
>                                      Nothing  -> eval as (es ++ [ts])
>                                      Just ts' -> ts' <>>=> Fun [v] (eval as (es ++ [v]))

> monadic                       :: Expr -> Maybe Expr
> monadic (TypeOf (Varsym "%" : ts) us)
>                               =  Just (TypeOf ts us)
> monadic (Quoted (Varsym "%" : ts))
>                               =  Just (Quoted ts)
> monadic _                     =  Nothing

Expected tokens.

> expected                      :: Set Symbol -> Expr
> expected lookahead            =  List [ Literal s | t <- Set.toList lookahead, Just s <- [shorthand t] ]
> --expected lookahead            =  List [ bottoms t | t <- Set.toList lookahead ]

Helper functions.

> unCon, unVar                  :: Expr -> Ident
> unCon (Con x)                 =  x
> unCon _                       =  impossible "Generate.unCon"
> unVar (Var x)                 =  x
> unVar _                       =  impossible "Generate.unVar"

> mangle                        :: Symbol -> String
> mangle v                      =  show (number v)     -- number
> smangle                       :: State -> String
> smangle s                     =  show (snumber s)
> imangle                       :: Item -> String
> imangle i                     =  show (inumber i)
> pmangle                       :: Action -> String
> pmangle p                     =  show (pnumber p)

> vmangle                       :: Int -> Symbol -> String
> vmangle i v                   =  concat (glue (safeName v : map (vmangle (i + 1)) (safeArguments v)))
>   where
>   glue                        =  intersperse (replicate i '_')
>
>   safeName                    :: Symbol -> String
>   safeName (Terminal { number = n })
>                               =  show n
>   safeName (Nonterminal { name = s, types = ts })
>                               =  string s ++ "'" ++ show (length ts)
>
>   safeArguments               :: Symbol -> [Symbol]
>   safeArguments (Terminal {}) =  []
>   safeArguments (Nonterminal { arguments = vs })
>                               =  vs

> asPat                         :: Expr -> Pat -> Expr
> asPat (Var x) p               =  As x p
> asPat _ _                     =  impossible "Generate.asPat"

> {-
> consOf                        :: Expr -> Ident
> consOf (Var s)                =  s
> consOf (App p _p')            =  consOf p
> consOf _                      =  impossible "Generate.consOf"

> hasHash                       :: Expr -> Bool
> hasHash e                     =  head (consOf e) == '#'

> rmHash                        :: Expr -> Ident
> rmHash e                      =  tail (consOf e)
> -}

Names. NB. |v_i| should only be used if there is \emph{no} danger of
name capture.

> v_i                           :: Int -> Expr
> v_i i                         =  var ("v" ++ show i)

User supplied functions.

> hsFrown, result_tcon, terminal_tcon
>                               :: Expr
> hsFrown                       =  var "frown"
> result_tcon                   =  con "Result"
> terminal_tcon                 =  con "Terminal"