OSDN Git Service

support return-value
authorMIZUNO Hiroki <mzpppp@gmail.com>
Mon, 26 May 2008 00:13:00 +0000 (09:13 +0900)
committerMIZUNO Hiroki <mzpppp@gmail.com>
Mon, 26 May 2008 00:13:00 +0000 (09:13 +0900)
example/lambda.scm
src/ast.ml
src/base.ml
test/test_ast.ml

index 0d4eb52..8feb129 100644 (file)
@@ -1,2 +1,2 @@
-(let ((f (lambda () (print 42))))
-  (f))
\ No newline at end of file
+(let ((f (lambda () 42)))
+  (print (f)))
\ No newline at end of file
index 186c2f0..7681cc6 100644 (file)
@@ -28,7 +28,7 @@ let make_meth name body =
   let inst =
     [GetLocal_0;PushScope] @
       body @
-      [ReturnVoid] in
+      [ReturnValue] in
   { name  = name;
     params=[];
     return=0;
@@ -63,7 +63,7 @@ let rec generate_expr ast table =
          make_meth name @@ expr body in
          [NewFunction m]
     | Block xs ->
-       (concatMap expr xs)
+       List.concat @@ interperse [Pop] @@ (List.map expr xs)
     | Var name ->
        let scope,index = 
          List.assoc name table in
@@ -91,14 +91,14 @@ let rec generate_expr ast table =
            load;
            [GetLocal_0];
            concatMap expr args;
-           [Asm.Call (List.length args);Pop]]
+           [Asm.Call (List.length args)]]
     | Call (name,args) ->
        let mname =
          Cpool.QName ((Cpool.Namespace ""),name) in
-         [FindPropStrict mname]
-         @ (concatMap expr args)
-         @ [CallPropLex (mname,List.length args);
-            Pop;]
+         List.concat [
+           [FindPropStrict mname];
+           concatMap expr args;
+           [CallPropLex (mname,List.length args)]]
     | If (cond,cons,alt) ->
        let l_alt =
          Label.make () in
index 2f3ad32..656e20f 100644 (file)
@@ -17,6 +17,12 @@ let rec range a b =
   else
     a::range (a+1) b
 
+let rec interperse delim =
+  function
+      []  -> []
+    | [x] -> [x]
+    | x::xs -> x::delim::interperse delim xs
+
 type ('a,'b) either = Left of 'a | Right of 'b
 let left = 
   function 
index b8b2890..6bd2b97 100644 (file)
@@ -9,7 +9,7 @@ let result inst = {
   params=[];
   return=0;
   flags=0;
-  instructions=[GetLocal_0;PushScope;]@inst@[ReturnVoid];
+  instructions=[GetLocal_0;PushScope;]@inst@[ReturnValue];
   traits=[];
   exceptions=[]}
 
@@ -20,8 +20,7 @@ test call =
     assert_equal 
       (result [FindPropStrict (QName ((Namespace ""),"print"));
             PushString "Hello";
-            CallPropLex (QName ((Namespace ""),"print"),1);
-            Pop])
+            CallPropLex (QName ((Namespace ""),"print"),1)])
       (compile (Call ("print",[String "Hello"])))
 
 test int = 
@@ -42,7 +41,7 @@ test boolean =
 
 test block =
   assert_equal
-    (result [PushInt 1;PushInt 2])
+    (result [PushInt 1;Pop;PushInt 2])
     (compile (Block [Int 1;Int 2]))
 
 test if_ =
@@ -61,6 +60,7 @@ test let_ =
             PushScope;
             GetScopeObject 1;
             GetProperty (QName ((Namespace ""),"0"));
+            Pop;
             GetScopeObject 1;
             GetProperty (QName ((Namespace ""),"1"));
             PopScope])