13 import System.CommandLine
16 from Text import class Text(concat,join), instance Text String
23 from yard import :: Error, instance toString Error
35 derive gPrint TokenValue
37 Start :: *World -> *World
39 # (args, w) = parseArgs w
40 # (stdin, w) = stdio w
43 <<< "spl 0.1 (17 march 2016)\n"
44 <<< "Copyright Pim Jager and Mart Lubbers\n"
45 = snd $ fclose stdin w
48 <<< "Usage: " <<< args.program <<< " [OPTION] [FILE]\n"
49 <<< "<spl> ::= <spl> <parser> <lexer>\n"
50 <<< "Lex parse and either FILE or stdin\n"
53 <<< " --help Show this help\n"
54 <<< " --version Show the version\n"
55 <<< " --[no-]lex Lexer output(default: disabled)\n"
56 <<< " --[no-]parse Parser output(default: disabled)\n"
57 <<< " --[no-]sem Semantic analysis output(default: disabled)\n"
58 <<< " --[no-]code Code generation output(default: enabled)\n"
59 = snd $ fclose stdin w
60 # (contents, stdin, w) = readFileOrStdin stdin args.fp w
62 (Left cs) = snd $ fclose (stdin <<< cs) w
63 (Right cs) = case lexer cs of
64 (Left e) = snd $ fclose (stdin <<< toString e <<< "\n") w
66 # stdin = if (not args.lex) stdin (
67 stdin <<< "//LEXER\n" <<< printTokens lexOut <<< "//LEXER\n")
68 = case parser lexOut of
69 (Left e) = snd $ fclose (stdin <<< toString e <<< "\n") w
71 # stdin = if (not args.parse) stdin (
72 stdin <<< "//PARSER\n" <<< toString parseOut <<< "//PARSER\n")
73 = case sem parseOut of
74 (Left e) = snd $ fclose (stdin <<< join "\n" (map toString e) <<< "\n") w
76 # stdin = if (not args.sem) stdin (stdin
77 <<< "//SEM G\n" <<< toString ast <<< "//SEMA\n")
79 (Left e) = snd $ fclose (stdin <<< e) w
81 # stdin = if (not args.gen) stdin (stdin
82 <<< ";CODE GEN\n" <<< asm <<< "\n;CODE GEN\n")
83 = snd $ fclose (stdin <<< "\n") w
85 printConstraints :: Constraints -> String
86 printConstraints [] = ""
87 printConstraints [(t1, t2):ts] = concat [
88 "(", toString t1, ",", toString t2, ")"] +++ printConstraints ts
90 printTokens :: [Token] -> String
91 printTokens ts = concat $ flatten $ map pt ts
93 pt ({line,col},token) = [toString line, ":",
94 toString col, ": ", printToString token, "\n"]
96 parseArgs :: *World -> (Opts, *World)
98 # ([p:args], w) = getCommandLine w
109 pa :: [String] Opts -> Opts
111 pa ["--help":r] o = pa r {o & help=True}
112 pa ["--version":r] o = pa r {o & version=True}
113 pa ["--lex":r] o = pa r {o & lex=True}
114 pa ["--no-lex":r] o = pa r {o & lex=False}
115 pa ["--parse":r] o = pa r {o & parse=True}
116 pa ["--no-parse":r] o = pa r {o & parse=False}
117 pa ["--sem":r] o = pa r {o & sem=True}
118 pa ["--no-sem":r] o = pa r {o & sem=False}
119 pa ["--code":r] o = pa r {o & gen=True}
120 pa ["--no-code":r] o = pa r {o & gen=False}
121 pa [x:r] o = pa r {o & fp=Just x}
123 readFileOrStdin :: *File (Maybe String) *World -> *(Either String [Char], *File, *World)
124 readFileOrStdin stdin Nothing w
125 # (cs, stdin) = readEntireFile stdin
126 = (Right cs, stdin, w)
127 readFileOrStdin stdin (Just fp) w
128 # (b, fin, w) = fopen fp FReadText w
129 | not b = (Left "Unable to open file", stdin, w)
130 # (cs, fin) = readEntireFile fin
131 # (b, w) = fclose fin w
132 | not b = (Left "Unable to close file", stdin, w)
133 = (Right cs, stdin, w)
135 readEntireFile :: *File -> *([Char], *File)
137 # (b, c, f) = freadc f
139 # (cs, f) = readEntireFile f