summaryrefslogtreecommitdiff
path: root/testsuite/tests/programs/joao-circular/Funcs_Lexer.hs
blob: c9f5c07b16fc629a9d72ff39a85896fa9d2cdddb (plain)
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
--
-- Lexer for MicroC
--
-- Joćo Saraiva
-- 2002
--


module Funcs_Lexer where
import Data.Char

--
-- Tokens passed to the parser
--

data Token
       = Tsemicolon
       | TopenB
       | TcloseB
       | TopenSB
       | TcloseSB
       | TopenCB
       | TcloseCB
       | Ttowdots
       | Tcomma
       | Tequal
       | Tand
       | Tor
       | Tassing
       | Tadd
       | Tmul
       | Tsub
       | Tdiv
       | Tgt
       | Tlt 
       | Tnot
       | Ttrue
       | Tfalse
       | Tint
       | Treal
       | Tbool
       | Tchar
       | Tif
       | Tthen
       | Telse
       | Twhile
       | Tinput
       | Tprint
       | TIdent String
       | TintVal Int
       | TrealVal Float
       deriving Show

lexer []       = []
lexer (';':cs) =  Tsemicolon : lexer cs
lexer ('(':cs) =  TopenB : lexer cs
lexer (')':cs) =  TcloseB : lexer cs
lexer ('[':cs) =  TopenSB : lexer cs
lexer (']':cs) =  TcloseSB : lexer cs
lexer ('{':cs) =  TopenCB : lexer cs
lexer ('}':cs) =  TcloseCB : lexer cs
lexer (':':cs) =  Ttowdots : lexer cs
lexer (',':cs) =  Tcomma : lexer cs

lexer ('=':'=':cs) = Tequal : lexer cs
lexer ('&':'&':cs) = Tor : lexer cs
lexer ('|':'|':cs) = Tand : lexer cs

lexer ('=':cs) =  Tassing : lexer cs
lexer ('+':cs) =  Tadd : lexer cs
lexer ('*':cs) =  Tmul : lexer cs
lexer ('-':cs) =  Tsub : lexer cs
lexer ('/':cs) =  Tdiv : lexer cs
lexer ('>':cs) =  Tgt : lexer cs
lexer ('<':cs) =  Tlt : lexer cs
lexer ('!':cs) =  Tnot : lexer cs

lexer (c:cs) 
      | isSpace c = lexer cs
      | isAlpha c = lexSTR (c:cs)
      | isDigit c = lexINT (c:cs)

lexINT cs =
   case span isDigit cs of
      (var,rest) -> TintVal ((read var)::Int) : lexer rest


lexSTR cs = 
   case span isLegalChar cs of
      ("true",rest)  -> Ttrue : lexer rest
      ("false",rest) -> Tfalse : lexer rest
      ("int",rest)   -> Tint : lexer rest
      ("real",rest)  -> Treal : lexer rest
      ("bool",rest)  -> Tbool : lexer rest
      ("char",rest)  -> Tchar : lexer rest
      ("if",rest)    -> Tif : lexer rest
      ("then",rest)  -> Tthen : lexer rest
      ("else",rest)  -> Telse : lexer rest
      ("while",rest) -> Twhile : lexer rest
      ("input",rest) -> Tinput : lexer rest
      ("print",rest) -> Tprint : lexer rest
      (var,rest)     -> TIdent var : lexer rest

isLegalChar c = let x = ord c
                in (isAlpha c) || (isDigit c) || (c == '_') 

runScan = do s <- readFile "input"
             let t = lexer s
             putStr (show t)
             return () -- (scanner inp))