13 import System.CommandLine
15 from Text import class Text(concat,join), instance Text String
21 from yard import :: Error, instance toString Error
32 derive gPrint TokenValue
34 Start :: *World -> *World
36 # (args, w) = parseArgs w
37 # (stdin, w) = stdio w
40 <<< "spl 0.1 (17 march 2016)\n"
41 <<< "Copyright Pim Jager and Mart Lubbers\n"
42 = snd $ fclose stdin w
45 <<< "Usage: " <<< args.program <<< " [OPTION] [FILE]\n"
46 <<< "<spl> ::= <spl> <parser> <lexer>\n"
47 <<< "Lex parse and either FILE or stdin\n"
50 <<< " --help Show this help\n"
51 <<< " --version Show the version\n"
52 <<< " --[no-]lex Lexer output(default: disabled)\n"
53 <<< " --[no-]parse Parser output(default: enabled)\n"
54 = snd $ fclose stdin w
55 # (contents, stdin, w) = readFileOrStdin stdin args.fp w
57 (Left cs) = snd $ fclose (stdin <<< cs) w
58 (Right cs) = case lexer cs of
59 (Left e) = snd $ fclose (stdin <<< toString e) w
61 # stdin = if (not args.lex) stdin (
62 stdin <<< "//LEXER\n" <<< printTokens lexOut <<< "//LEXER\n")
63 = case parser lexOut of
64 (Left e) = snd $ fclose (stdin <<< toString e) w
66 # stdin = if (not args.parse) stdin (
67 stdin <<< "//PARSER\n" <<< toString parseOut <<< "//PARSER\n")
68 = case sem parseOut of
69 (Left e) = snd $ fclose (stdin <<< join "\n" (map toString e)) w
71 # stdin = if (not args.sem) stdin (
72 stdin <<< "//SEM\n" <<< toString semOut <<< "//SEM\n")
73 = snd $ fclose stdin w
75 printTokens :: [Token] -> String
76 printTokens ts = concat $ flatten $ map pt ts
78 pt ({line,col},token) = [toString line, ":",
79 toString col, ": ", printToString token, "\n"]
81 parseArgs :: *World -> (Opts, *World)
83 # ([p:args], w) = getCommandLine w
93 pa :: [String] Opts -> Opts
95 pa ["--help":r] o = pa r {o & help=True}
96 pa ["--version":r] o = pa r {o & version=True}
97 pa ["--lex":r] o = pa r {o & lex=True}
98 pa ["--no-lex":r] o = pa r {o & lex=False}
99 pa ["--parse":r] o = pa r {o & parse=True}
100 pa ["--no-parse":r] o = pa r {o & parse=False}
101 pa ["--sem":r] o = pa r {o & sem=True}
102 pa ["--no-sem":r] o = pa r {o & sem=False}
103 pa [x:r] o = pa r {o & fp=Just x}
105 readFileOrStdin :: *File (Maybe String) *World -> *(Either String [Char], *File, *World)
106 readFileOrStdin stdin Nothing w
107 # (cs, stdin) = readEntireFile stdin
108 = (Right cs, stdin, w)
109 readFileOrStdin stdin (Just fp) w
110 # (b, fin, w) = fopen fp FReadText w
111 | not b = (Left "Unable to open file", stdin, w)
112 # (cs, fin) = readEntireFile fin
113 # (b, w) = fclose fin w
114 | not b = (Left "Unable to close file", stdin, w)
115 = (Right cs, stdin, w)
117 readEntireFile :: *File -> *([Char], *File)
119 # (b, c, f) = freadc f
121 # (cs, f) = readEntireFile f