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