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
73 # stdin = if (not args.sem) stdin (stdin
74 <<< "//SEM G\n" <<< toString ast <<< "//SEMA\n")
75 = snd $ fclose (stdin <<< "\n") w
77 printConstraints :: Constraints -> String
78 printConstraints [] = ""
79 printConstraints [(t1, t2):ts] = concat [
80 "(", toString t1, ",", toString t2, ")"] +++ printConstraints ts
82 printTokens :: [Token] -> String
83 printTokens ts = concat $ flatten $ map pt ts
85 pt ({line,col},token) = [toString line, ":",
86 toString col, ": ", printToString token, "\n"]
88 parseArgs :: *World -> (Opts, *World)
90 # ([p:args], w) = getCommandLine w
100 pa :: [String] Opts -> Opts
102 pa ["--help":r] o = pa r {o & help=True}
103 pa ["--version":r] o = pa r {o & version=True}
104 pa ["--lex":r] o = pa r {o & lex=True}
105 pa ["--no-lex":r] o = pa r {o & lex=False}
106 pa ["--parse":r] o = pa r {o & parse=True}
107 pa ["--no-parse":r] o = pa r {o & parse=False}
108 pa ["--sem":r] o = pa r {o & sem=True}
109 pa ["--no-sem":r] o = pa r {o & sem=False}
110 pa [x:r] o = pa r {o & fp=Just x}
112 readFileOrStdin :: *File (Maybe String) *World -> *(Either String [Char], *File, *World)
113 readFileOrStdin stdin Nothing w
114 # (cs, stdin) = readEntireFile stdin
115 = (Right cs, stdin, w)
116 readFileOrStdin stdin (Just fp) w
117 # (b, fin, w) = fopen fp FReadText w
118 | not b = (Left "Unable to open file", stdin, w)
119 # (cs, fin) = readEntireFile fin
120 # (b, w) = fclose fin w
121 | not b = (Left "Unable to close file", stdin, w)
122 = (Right cs, stdin, w)
124 readEntireFile :: *File -> *([Char], *File)
126 # (b, c, f) = freadc f
128 # (cs, f) = readEntireFile f