OSDN Git Service

Add let-expression
authorMIZUNO Hiroki <mzpppp@gmail.com>
Tue, 20 May 2008 14:56:35 +0000 (23:56 +0900)
committerMIZUNO Hiroki <mzpppp@gmail.com>
Tue, 20 May 2008 14:56:35 +0000 (23:56 +0900)
- 'let' is worable,but slow

example/let.scm
src/ast.ml
util/instruction.txt

index 7426ca2..2b2687b 100644 (file)
@@ -1,2 +1,2 @@
-(let ((x 42))
-  (print 42))
\ No newline at end of file
+(let ((x 42) (y 10))
+  (print (+ x y)))
index 3f85d95..c61e254 100644 (file)
@@ -19,20 +19,9 @@ type ast =
   | Let of (string*ast) list * ast
   | Var of string
 
-let find name table = 
-  let rec sub i = function
-      [] -> 
-       failwith @@ "no name: " ^ name
-    | x::xs ->
-       try
-         i,List.assoc name x
-       with Not_found ->
-         sub (i+1) xs in
-    sub 0 table
-
 let scope_depth = function
     [] -> 0
-  | (_,(scope,_))::_ -> scope
+  | (_,scope)::_ -> scope
 
 let rec generate_expr ast table = 
   let expr ast =
@@ -64,24 +53,23 @@ let rec generate_expr ast table =
                  traits=[];
                  instructions=inst}]
     | Var name ->
-       let scope,slot = 
+       let scope = 
          List.assoc name table in
          Right [GetScopeObject scope;
-                GetSlot slot]
+                GetProperty (Cpool.QName ((Cpool.Namespace ""),name))]
     | Let (vars,body) ->
        let depth = 
          scope_depth table + 1 in
        let table' =
-         (ExtList.List.mapi (fun i (name,_) -> name,(depth,i+1)) vars)@table in
+         (ExtList.List.mapi (fun i (name,_) -> name,depth) vars)@table in
        let inits =
          concatMap (fun (name,init)-> 
-                      let scope,slot = 
-                        List.assoc name table' in
-                        List.concat [ expr init;
-                                     [GetScopeObject scope;Swap;SetSlot slot]]) vars in
-       Right (List.concat [[NewObject 0; PushScope];
-                           inits;
-                           right @@ generate_expr body table'])
+                      List.concat [[PushString name;];
+                                   expr init]) vars in
+       Right (List.concat [inits;
+                           [NewObject (List.length vars);PushScope];
+                           right @@ generate_expr body table';
+                           [PopScope]])
     | Call (name,args) ->
        let mname =
          Cpool.QName ((Cpool.Namespace ""),name) in
index c38b579..daed551 100644 (file)
@@ -55,11 +55,15 @@ SetSlot of int:op=0x6d; stack= ~2; args=const [u30 arg0]
 GetGlobalSlot of int:op=0x6e; stack=1; args=const [u30 arg0]
 SetGlobalSlot of int:op=0x6f; stack= ~-1; args=const [u30 arg0]
 
+GetProperty of Cpool.multiname: op=0x66; const=multiname arg0; args=fun cmap ->[multiname_get arg0 cmap]
+
 ReturnVoid: op=0x47
 FindPropStrict of Cpool.multiname: op=0x5D; stack=1; const=multiname arg0; args=fun cmap ->[multiname_get arg0 cmap]
 PushUInt of int: op=0x2E; stack=1; const=uint arg0; args=fun cmap -> [uint_get arg0 cmap]
 CallPropLex of Cpool.multiname * int: op=0x4c; stack= ~-arg1; args=fun cmap->[multiname_get arg0 cmap;Bytes.u30 arg1]
 Pop: op=0x29; stack= ~-1
 Swap:op=0x2b
+PopScope:op=0x1d; scope= ~-1
 
-NewObject of int:op=0x55; args=const [u30 arg0]
\ No newline at end of file
+NewObject of int:op=0x55; args=const [u30 arg0]
+NewArray of int:op=0x56; args=const [u30 arg0]