man toegevoegd, mapjes gemaakt, bijna klaar voor inleveren
[cc1516.git] / src / main.icl
1 module main
2
3 import StdFile
4 import StdBool
5 import StdMisc
6 import StdFunc
7 import StdTuple
8 import StdList
9 import StdString
10 import Data.Either
11 import Data.Maybe
12 import Data.Func
13 import System.CommandLine
14 import GenPrint
15 from Text import class Text(concat), instance Text String
16
17 import parse
18 import lex
19 from yard import :: Error, instance toString Error
20
21 :: Opts = {
22 version :: Bool,
23 program :: String,
24 lex :: Bool,
25 parse :: Bool,
26 fp :: Maybe String,
27 help :: Bool}
28
29 derive gPrint TokenValue
30
31 Start :: *World -> *World
32 Start w
33 # (args, w) = parseArgs w
34 # (stdin, w) = stdio w
35 | args.version
36 # stdin = stdin
37 <<< "main 0.1 (17 march 2016)\n"
38 <<< "Copyright Pim Jager and Mart Lubbers\n"
39 = snd $ fclose stdin w
40 | args.help
41 # stdin = stdin
42 <<< "Usage: " <<< args.program <<< " [OPTION] [FILE]\n"
43 <<< "Lex parse and either FILE or stdin\n"
44 <<< "\n"
45 <<< "Options:\n"
46 <<< " --help Show this help\n"
47 <<< " --version Show the version\n"
48 <<< " --[no-]lex Lexer output(default: disabled)\n"
49 <<< " --[no-]parse Parser output(default: enabled)\n"
50 = snd $ fclose stdin w
51 # (contents, stdin, w) = readFileOrStdin stdin args.fp w
52 = case contents of
53 (Left cs) = snd $ fclose (stdin <<< cs) w
54 (Right cs)
55 # lexOut = lexer cs
56 # stdin = if (not args.lex) stdin (case lexOut of
57 (Right toks) =
58 stdin <<< "---LEXER\n" <<< printTokens toks <<< "---LEXER\n"
59 _ = stdin)
60 # parseOut = parser lexOut
61 # stdin = if (not args.parse) stdin (case parser lexOut of
62 (Right ast) =
63 stdin <<< "---PARSER\n" <<< toString ast <<< "---PARSER\n"
64 (Left parse) = stdin <<< toString parse)
65 = snd $ fclose stdin w
66 where
67 printTokens :: [Token] -> String
68 printTokens ts = concat $ flatten $ map pt ts
69 where
70 pt {line,column,token} = [toString line, ":",
71 toString column, ": ", printToString token, "\n"]
72
73 parseArgs :: *World -> (Opts, *World)
74 parseArgs w
75 # ([p:args], w) = getCommandLine w
76 = (pa args {
77 program=p,
78 version=False,
79 lex=False,
80 parse=True,
81 fp=Nothing,
82 help=False}, w)
83 where
84 pa :: [String] Opts -> Opts
85 pa [] o = o
86 pa ["--help":r] o = pa r {o & help=True}
87 pa ["--version":r] o = pa r {o & version=True}
88 pa ["--lex":r] o = pa r {o & lex=True}
89 pa ["--no-lex":r] o = pa r {o & lex=False}
90 pa ["--parse":r] o = pa r {o & parse=True}
91 pa ["--no-parse":r] o = pa r {o & parse=False}
92 pa [x:r] o = pa r {o & fp=Just x}
93
94 readFileOrStdin :: *File (Maybe String) *World -> *(Either String [Char], *File, *World)
95 readFileOrStdin stdin Nothing w
96 # (cs, stdin) = readEntireFile stdin
97 = (Right cs, stdin, w)
98 readFileOrStdin stdin (Just fp) w
99 # (b, fin, w) = fopen fp FReadText w
100 | not b = (Left "Unable to open file", stdin, w)
101 # (cs, fin) = readEntireFile fin
102 # (b, w) = fclose fin w
103 | not b = (Left "Unable to close file", stdin, w)
104 = (Right cs, stdin, w)
105
106 readEntireFile :: *File -> *([Char], *File)
107 readEntireFile f
108 # (b, c, f) = freadc f
109 | not b = ([], f)
110 # (cs, f) = readEntireFile f
111 = ([c:cs], f)