Weghalen unresolved labels en update examples
authorMart Lubbers <mart@martlubbers.net>
Tue, 31 May 2016 09:17:45 +0000 (11:17 +0200)
committerMart Lubbers <mart@martlubbers.net>
Tue, 31 May 2016 09:17:45 +0000 (11:17 +0200)
examples/higher.spl
examples/printparse.spl
gen.icl
grammar/grammar.txt
lex.icl

index 5e3429f..2f91e2e 100644 (file)
@@ -40,5 +40,6 @@ main(){
        print("faculty of 5 is: ", foldr(\x y->x*y, 1, intList(5)));
        print("sum of 1..5 is: ", foldr(\x y->x+y, 0, intList(5)));
        print("filter evens from 0..12 is: ");
-       print("sum of 0..12 but only the evens: ", foldr(\x y->x+y, 0, filter(\x->x%2 == 0, intList(12))));
+       print("sum of 0..12 but only the evens: ",
+               foldr(\x y->x+y, 0, filter(\x->x%2 == 0, intList(12))));
 }
index 41f1988..6ac6199 100644 (file)
@@ -49,7 +49,7 @@ strToInt(x) :: [Char] -> Int {
 }
 
 //printList(p, l) :: (a -> Void) -> [a] -> Void {
-printList(p, l) :: (a -> Void) -> [a] -> Void {
+printList(p, l) {
     print('[');
        if(!isEmpty(l)){
                p(l.hd);
@@ -65,6 +65,10 @@ printList(p, l) :: (a -> Void) -> [a] -> Void {
 
 printInt(x) {
        [Char] l = [];
+       if(x < 0){
+               print('-');
+               x = -x;
+       }
        while(x > 0){
                l = toChar(x % 10) : l;
                x = x / 10;
@@ -75,5 +79,5 @@ printInt(x) {
 main(){
        var num = "-1234";
 
-       printList(printInt, 32 : 4 : []);
+       printList(printInt, 32 : 4 : strToInt(num) : []);
 }
diff --git a/gen.icl b/gen.icl
index eba5a1d..9f515c9 100644 (file)
--- a/gen.icl
+++ b/gen.icl
@@ -43,9 +43,8 @@ defaultAddressBook :: [FunDecl] -> Addressbook
 defaultAddressBook fd = extend "1printint" (LAB "1printint" 1 0)
        $ extend "1printchar" (LAB "1printchar" 1 1)
        $ extend "read" (LAB "read" 0 2)
-       $ extend "1readint" (LAB "1readint" 0 3)
-       $ extend "isEmpty" (LAB "isempty" 1 4)
-       $ addFuncs fd 5
+       $ extend "isEmpty" (LAB "isEmpty" 1 3)
+       $ addFuncs fd 4
        where
                addFuncs [] _ = 'Map'.newMap
                addFuncs [(FunDecl _ k args _ _ _):xs] n =
@@ -64,8 +63,7 @@ gen (AST fds) = case evalRWST prog () (defaultAddressBook fds, labelStream) of
 
 programContext :: [FunDecl] -> SSMProgram
 programContext x = [Lab "1func"
-                               :fS ["1printint" ,"1printchar"
-                                       ,"read" ,"1readint"
+                               :fS ["1printint" ,"1printchar","read"
                                        ,"isEmpty":map (\(FunDecl _ k _ _ _ _)->k) x] 0] ++ context
        where
 
@@ -76,7 +74,9 @@ programContext x = [Lab "1func"
                        ,Instr "ldr" [Raw "R5"] ""
                        ,Instr "ldc" [Lit n] $ "branch to: " +++ k
                        ,Instr "eq" [] ""
-                       ,Instr "brf" [L $ "1next" +++ (toString $ n + 1)] ""
+                       ,if (isEmpty xs)
+                               (Instr "nop" [] "")
+                               (Instr "brf" [L $ "1next" +++ (toString $ n + 1)] "")
                        ,Instr "bra" [L k] ""
                        :fS xs $ n+1]
                context :: SSMProgram
@@ -98,7 +98,7 @@ programContext x = [Lab "1func"
                     ,Instr "str" [Raw "RR"] ""
                     ,Instr "unlink" [] ""
                     ,Instr "ret" [] ""
-                    ,Lab "isempty"
+                    ,Lab "isEmpty"
                     ,Instr "link" [Lit 0] ""
                     ,Instr "ldl" [Lit -2] "load prt to list"
                     ,Instr "lda" [Lit 0] "derefrence ptr"
index 06250b1..08ddedf 100644 (file)
@@ -1,6 +1,6 @@
 <Prog>         ::= <LetDecl>*
                     <FunDecl>+
-<LetDecl>      ::= 'Let' <type> <id> '=' <Expr> ';'
+<LetDecl>      ::= 'let' <type> <id> '=' <Expr> ';'
 <FunDecl>      ::= <id> '(' <Type>* ')' ['::' <FunType] '{' <VarDecl>* <Stmt>+ '}'
 <FunType>      ::= <VoidType> ['->' <FunType>]
                  | '(' <FunType> ')'
@@ -27,6 +27,7 @@
                  | '[]' <Expr>
                  | '(' <Expr> ',' <Expr> ')'
                  | '"' <char> '"'
+<Op1>          ::= '~' | '!'
 <LamdaExpr>    ::= '\'<id>* '->' <Expr>
 <FieldSels>    ::= ('.' ('hd'|'tl'|'fst'|'snd))*
 <FunCall>      ::= <id> ['(' <ActArgs>+ ')']
diff --git a/lex.icl b/lex.icl
index 6cb2e92..15ae4f1 100644 (file)
--- a/lex.icl
+++ b/lex.icl
@@ -48,7 +48,7 @@ lexToken =
        lexKw "True" TrueToken <|> lexKw "False" FalseToken <|>
        lexKw "Int" IntTypeToken <|> lexKw "Bool" BoolTypeToken <|>
        lexKw "Char" CharTypeToken <|>
-    lexKw "Let" LetToken <|>
+    lexKw "let" LetToken <|>
        //Character tokens
        lexEscape <|> lexCharacter <|>
        //Two char ops tokens