add print and read preamble
authorMart Lubbers <mart@martlubbers.net>
Fri, 20 May 2016 11:24:37 +0000 (13:24 +0200)
committerMart Lubbers <mart@martlubbers.net>
Fri, 20 May 2016 11:24:37 +0000 (13:24 +0200)
gen.icl
spl.icl

diff --git a/gen.icl b/gen.icl
index 76085ab..acfdd61 100644 (file)
--- a/gen.icl
+++ b/gen.icl
@@ -57,14 +57,27 @@ gen (AST fds) = case evalRWST prog () (defaultAddressBook, labelStream) of
             >>| mapM_ g fds
 
 programContext :: SSMProgram
-programContext =    [Lab "print" //there is no actual IO in SSM
+programContext =    [Lab "1printint"
                     ,Instr "link" [Lit 0] ""
+                    ,Instr "ldl" [Lit -2] "load first argument"
+                                       ,Instr "trap" [Lit 0] "print int"
                     ,Instr "unlink" [] ""
                     ,Instr "ret" [] ""
-                    ,Lab "read" //there is no actual IO in SSM
+                                       ,Lab "1printchar"
                     ,Instr "link" [Lit 0] ""
-                    ,Instr "ldc" [Lit 0] ""
-                    ,Instr "sth" [] ""
+                    ,Instr "ldl" [Lit -2] "load first argument"
+                                       ,Instr "trap" [Lit 1] "print char"
+                    ,Instr "unlink" [] ""
+                    ,Instr "ret" [] ""
+                    ,Lab "1readint"
+                    ,Instr "link" [Lit 0] ""
+                                       ,Instr "trap" [Lit 10] "read int"
+                    ,Instr "str" [Raw "RR"] ""
+                    ,Instr "unlink" [] ""
+                    ,Instr "ret" [] ""
+                    ,Lab "1readchar"
+                    ,Instr "link" [Lit 0] ""
+                                       ,Instr "trap" [Lit 11] "read char"
                     ,Instr "str" [Raw "RR"] ""
                     ,Instr "unlink" [] ""
                     ,Instr "ret" [] ""
diff --git a/spl.icl b/spl.icl
index b5738ee..383b84e 100644 (file)
--- a/spl.icl
+++ b/spl.icl
@@ -34,6 +34,16 @@ from yard import :: Error, instance toString Error
 
 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
@@ -70,7 +80,7 @@ Start w
                        (Right parseOut)
                        # stdin = if (not args.parse) stdin (
                                stdin <<<  "//PARSER\n" <<< toString parseOut <<< "//PARSER\n")
-                       = case sem parseOut of
+                       = 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