-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinfix.hs
executable file
·66 lines (54 loc) · 1.63 KB
/
infix.hs
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
#!/usr/bin/env koshu-env.sh runhaskell
{-# OPTIONS_GHC -Wall #-}
-- ------------------------------------------------------------------
--
-- DESCRIPTION
-- Display result of infix-to-prefix conversion
--
-- EXAMPLE
-- ./infix.hs "/a * /b + /c"
--
-- ------------------------------------------------------------------
import qualified Koshucode.Baala.Base as B
import qualified System.Environment as Sys
import qualified Text.PrettyPrint as P
main :: IO ()
main = do args <- Sys.getArgs
mapM_ display args
display :: String -> IO ()
display code =
do putStrLn $ "** " ++ code
case B.infixToPrefix ht $ B.tt1 code of
Right tree -> B.putShowLn $ B.ttDoc [tree]
Left xs -> B.putShowLn $ P.hang ambLabel
2 (B.docv $ map ambContent xs)
ambLabel :: B.Doc
ambLabel = B.doc "Ambiguous (same height, different direction)"
ambContent :: (Show a) => (a, B.Token) -> B.Doc
ambContent (spec, tok) = B.doch [B.doc tok, B.doc ":", B.doc (show spec)]
text :: B.Token -> Maybe String
text (B.TWord _ 0 name) = Just name
text _ = Nothing
-- height function
ht :: B.Token -> B.InfixHeight
ht = B.infixHeight text
[ ("=" , Left 8)
, ("=/=" , Left 8)
, ("<>" , Left 8)
, ("<" , Left 8)
, ("<=" , Left 8)
, (">" , Left 8)
, (">=" , Left 8)
, ("or" , Left 7)
, ("and" , Left 6)
, ("not" , Left 5)
, ("+" , Left 4)
, ("-" , Left 4)
, ("*" , Left 1)
, ("div" , Left 2)
, ("quo" , Left 2)
, ("rem" , Left 2)
, ("mod" , Left 2)
, ("L" , Left 1)
, ("R" , Right 1)
]