13 import System.CommandLine
15 from Text import class Text(concat), instance Text String
19 from yard import :: Error, instance toString Error
29 derive gPrint TokenValue
31 Start :: *World -> *World
33 # (args, w) = parseArgs w
34 # (stdin, w) = stdio w
37 <<< "main 0.1 (17 march 2016)\n"
38 <<< "Copyright Pim Jager and Mart Lubbers\n"
39 = snd $ fclose stdin w
42 <<< "Usage: " <<< args.program <<< " [OPTION] [FILE]\n"
43 <<< "Lex parse and either FILE or stdin\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
53 (Left cs) = snd $ fclose (stdin <<< cs) w
56 # stdin = if (not args.lex) stdin (case lexOut of
58 stdin <<< "---LEXER\n" <<< printTokens toks <<< "---LEXER\n"
60 # parseOut = parser lexOut
61 # stdin = if (not args.parse) stdin (case parser lexOut of
63 stdin <<< "---PARSER\n" <<< toString ast <<< "---PARSER\n"
64 (Left parse) = stdin <<< toString parse)
65 = snd $ fclose stdin w
67 printTokens :: [Token] -> String
68 printTokens ts = concat $ flatten $ map pt ts
70 pt {line,column,token} = [toString line, ":",
71 toString column, ": ", printToString token, "\n"]
73 parseArgs :: *World -> (Opts, *World)
75 # ([p:args], w) = getCommandLine w
84 pa :: [String] Opts -> Opts
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}
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)
106 readEntireFile :: *File -> *([Char], *File)
108 # (b, c, f) = freadc f
110 # (cs, f) = readEntireFile f