added some type checking stuff. vardecl now works for basictypes and unary operators
[cc1516.git] / spl.icl
1 module spl
2
3 import StdFile
4 import StdBool
5 import StdMisc
6 import StdFunc
7 import StdTuple
8 import StdList
9 import StdString
10 import Data.Either
11 import Data.Maybe
12 import Data.Func
13 import System.CommandLine
14 import GenPrint
15 from Text import class Text(concat,join), instance Text String
16
17 import parse
18 import lex
19 import sem
20 import AST
21 from yard import :: Error, instance toString Error
22
23 :: Opts = {
24 version :: Bool,
25 program :: String,
26 lex :: Bool,
27 parse :: Bool,
28 sem :: Bool,
29 fp :: Maybe String,
30 help :: Bool}
31
32 derive gPrint TokenValue
33
34 Start :: *World -> *World
35 Start w
36 # (args, w) = parseArgs w
37 # (stdin, w) = stdio w
38 | args.version
39 # stdin = stdin
40 <<< "spl 0.1 (17 march 2016)\n"
41 <<< "Copyright Pim Jager and Mart Lubbers\n"
42 = snd $ fclose stdin w
43 | args.help
44 # stdin = stdin
45 <<< "Usage: " <<< args.program <<< " [OPTION] [FILE]\n"
46 <<< "<spl> ::= <spl> <parser> <lexer>\n"
47 <<< "Lex parse and either FILE or stdin\n"
48 <<< "\n"
49 <<< "Options:\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
56 = case contents of
57 (Left cs) = snd $ fclose (stdin <<< cs) w
58 (Right cs) = case lexer cs of
59 (Left e) = snd $ fclose (stdin <<< toString e) w
60 (Right lexOut)
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
65 (Right parseOut)
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
70 (Right semOut)
71 # stdin = if (not args.sem) stdin (
72 stdin <<< "//SEM\n" <<< toString semOut <<< "//SEM\n")
73 = snd $ fclose stdin w
74 where
75 printTokens :: [Token] -> String
76 printTokens ts = concat $ flatten $ map pt ts
77 where
78 pt ({line,col},token) = [toString line, ":",
79 toString col, ": ", printToString token, "\n"]
80
81 parseArgs :: *World -> (Opts, *World)
82 parseArgs w
83 # ([p:args], w) = getCommandLine w
84 = (pa args {
85 program=p,
86 version=False,
87 lex=False,
88 parse=False,
89 sem=True,
90 fp=Nothing,
91 help=False}, w)
92 where
93 pa :: [String] Opts -> Opts
94 pa [] o = o
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}
104
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)
116
117 readEntireFile :: *File -> *([Char], *File)
118 readEntireFile f
119 # (b, c, f) = freadc f
120 | not b = ([], f)
121 # (cs, f) = readEntireFile f
122 = ([c:cs], f)