Merge branch 'master' of https://github.com/dopefishh/cc1516
[cc1516.git] / spl.icl
diff --git a/spl.icl b/spl.icl
index cf3d5f3..383b84e 100644 (file)
--- a/spl.icl
+++ b/spl.icl
@@ -19,6 +19,7 @@ import parse
 import lex
 import sem
 import AST
+import gen
 from yard import :: Error, instance toString Error
 
 :: Opts = {
@@ -27,11 +28,22 @@ from yard import :: Error, instance toString Error
        lex :: Bool,
        parse :: Bool,
        sem :: Bool,
+    gen :: Bool,
        fp :: Maybe String,
        help :: Bool}
 
 derive gPrint TokenValue
 
+preamble :: AST -> AST
+preamble (AST fd) = AST (pre ++ fd)
+       where
+               pre = [
+                       FunDecl zero "1printstr" ["x"] Nothing [] [
+                               IfStmt (FunExpr zero "isEmpty" [VarExpr zero (VarDef "x" [])] [])
+                                       []
+                                       [FunStmt "1printchar" [VarExpr zero (VarDef "x" [FieldHd])] []
+                                       ,FunStmt "1printstr" [VarExpr zero (VarDef "x" [FieldTl])] []]]]
+
 Start :: *World -> *World
 Start w
 # (args, w) = parseArgs w
@@ -52,27 +64,33 @@ Start w
                <<< "  --version          Show the version\n"
                <<< "  --[no-]lex         Lexer output(default: disabled)\n"
                <<< "  --[no-]parse       Parser output(default: disabled)\n"
-               <<< "  --[no-]sem         Semantic analysis output(default: enabled)\n"
+               <<< "  --[no-]sem         Semantic analysis output(default: disabled)\n"
+               <<< "  --[no-]code        Code generation output(default: enabled)\n"
        = snd $ fclose stdin w
 # (contents, stdin, w) = readFileOrStdin stdin args.fp w
 = case contents of
        (Left cs) = snd $ fclose (stdin <<< cs) w
        (Right cs) = case lexer cs of
-               (Left e) = snd $ fclose (stdin <<< toString e) w
+               (Left e) = snd $ fclose (stdin <<< toString e <<< "\n") w
                (Right lexOut)
                # stdin = if (not args.lex) stdin (
                        stdin <<< "//LEXER\n" <<< printTokens lexOut <<< "//LEXER\n")
                = case parser lexOut of
-                       (Left e) = snd $ fclose (stdin <<< toString e) w
+                       (Left e) = snd $ fclose (stdin <<< toString e <<< "\n") w
                        (Right parseOut)
                        # stdin = if (not args.parse) stdin (
                                stdin <<<  "//PARSER\n" <<< toString parseOut <<< "//PARSER\n")
-                       = case sem parseOut of
-                               (Left e) = snd $ fclose (stdin <<< join "\n" (map toString e)) w
+                       = case sem (preamble parseOut) of
+                               (Left e) = snd $ fclose (stdin <<< join "\n" (map toString e) <<< "\n") w
                                (Right ast)
                                # stdin = if (not args.sem) stdin (stdin
                                        <<< "//SEM G\n" <<< toString ast <<< "//SEMA\n")
-                               = snd $ fclose (stdin <<< "\n") w
+                               = case gen ast of
+                                       (Left e) = snd $ fclose (stdin <<< e) w
+                                       (Right asm)
+                                       # stdin = if (not args.gen) stdin (stdin
+                       <<< ";CODE GEN\n" <<< asm <<< "\n;CODE GEN\n")
+                                       = snd $ fclose (stdin <<< "\n") w
                where
                        printConstraints :: Constraints -> String
                        printConstraints [] = ""
@@ -93,7 +111,8 @@ parseArgs w
        version=False,
        lex=False,
        parse=False,
-       sem=True,
+       sem=False,
+    gen=True,
        fp=Nothing,
        help=False}, w)
 where
@@ -107,6 +126,8 @@ where
        pa ["--no-parse":r] o = pa r {o & parse=False}
        pa ["--sem":r] o = pa r {o & sem=True}
        pa ["--no-sem":r] o = pa r {o & sem=False}
+    pa ["--code":r] o = pa r {o & gen=True}
+    pa ["--no-code":r] o = pa r {o & gen=False}
        pa [x:r] o = pa r {o & fp=Just x}
 
 readFileOrStdin :: *File (Maybe String) *World -> *(Either String [Char], *File, *World)