(* name := namespace * symbol *)
type qname = (string * string) Node.t
-type ident = string Node.t
+type sname = string Node.t
(* expression has no side-effect. *)
type expr =
| `Bool of bool Node.t
| `Float of float Node.t
| `Var of qname
- | `Lambda of ident list * expr
+ | `Lambda of sname list * expr
| `Call of expr list
| `If of expr * expr * expr
- | `Let of (ident*expr) list * expr
- | `LetRec of (ident*expr) list * expr
+ | `Let of (sname*expr) list * expr
+ | `LetRec of (sname*expr) list * expr
| `Block of expr list
| `New of qname * expr list
- | `Invoke of expr * ident * expr list
- | `SlotRef of expr * ident
- | `SlotSet of expr * ident * expr ]
+ | `Invoke of expr * sname * expr list
+ | `SlotRef of expr * sname
+ | `SlotSet of expr * sname * expr ]
(* statement has side-effect *)
-type attr = ident
-type method_ = ident * ident list * expr
+type attr = sname
+type method_ = sname * sname list * expr
type stmt =
[ `Define of qname * expr
let string_of_qname {Node.value=(ns,name)} =
ns ^ "." ^ name
-let string_of_ident {Node.value=name} =
+let string_of_sname {Node.value=name} =
name
string_of_qname n
| `Lambda (args,expr') ->
Printf.sprintf "Lambda (%s,%s)"
- (string_of_list_by string_of_ident args)
+ (string_of_list_by string_of_sname args)
(to_string expr')
| `Call exprs ->
Printf.sprintf "Call %s" @@
string_of_list_by
(fun (a,b)->
Printf.sprintf "(%s,%s)"
- (string_of_ident a)
+ (string_of_sname a)
(to_string b)) decl in
let body' =
to_string body in
string_of_list_by
(fun (a,b)->
Printf.sprintf "(%s,%s)"
- (string_of_ident a)
+ (string_of_sname a)
(to_string b)) decl in
let body' =
to_string body in
| `Invoke (obj,name,args) ->
Printf.sprintf "Invoke (%s,%s,%s)"
(to_string obj)
- (string_of_ident name) @@
+ (string_of_sname name) @@
string_of_list_by to_string args
| `SlotRef (obj,name) ->
Printf.sprintf "SlotRef (%s,%s)"
- (to_string obj) @@ string_of_ident name
+ (to_string obj) @@ string_of_sname name
| `SlotSet (obj,name,value) ->
Printf.sprintf "SlotSet (%s,%s,%s)"
(to_string obj)
- (string_of_ident name)
+ (string_of_sname name)
(to_string value)
let to_string_stmt =
Printf.sprintf "Class (%s,%s,%s,%s)"
(string_of_qname klass)
(string_of_qname super)
- (string_of_list_by string_of_ident attrs)
+ (string_of_list_by string_of_sname attrs)
@@ String.concat "\n"
@@ List.map (fun (name,args,expr) ->
Printf.sprintf "((%s %s) %s)"
- (string_of_ident name)
+ (string_of_sname name)
(String.concat " " @@
- List.map string_of_ident args)
+ List.map string_of_sname args)
(to_string expr))
body
(** name := namespace * symbol *)
type qname = (string * string) Node.t
-type ident = string Node.t
+type sname = string Node.t
(** expression has no side-effect. *)
type expr =
| `Bool of bool Node.t
| `Float of float Node.t
| `Var of qname
- | `Lambda of ident list * expr
+ | `Lambda of sname list * expr
| `Call of expr list
| `If of expr * expr * expr
- | `Let of (ident*expr) list * expr
- | `LetRec of (ident*expr) list * expr
+ | `Let of (sname*expr) list * expr
+ | `LetRec of (sname*expr) list * expr
| `Block of expr list
| `New of qname * expr list
- | `Invoke of expr * ident * expr list (** (invoke <object> <method-name> <arg1> <arg2>...)*)
- | `SlotRef of expr * ident
- | `SlotSet of expr * ident * expr ]
+ | `Invoke of expr * sname * expr list (** (invoke <object> <method-name> <arg1> <arg2>...)*)
+ | `SlotRef of expr * sname
+ | `SlotSet of expr * sname * expr ]
-type attr = ident
-type method_ = ident * ident list * expr
+type attr = sname
+type method_ = sname * sname list * expr
(** statement has side-effect *)
type stmt =
exception Unbound_class of (string*string) Node.t
exception Unbound_method of string Node.t
-type method_ = Ast.ident
-
type stmt =
- [ `ExternalClass of Ast.qname * method_ list
+ [ `ExternalClass of Ast.qname * Ast.sname list
| `External of Ast.qname
| Ast.stmt]
type program = stmt list
exception Unbound_class of (string*string) Node.t
exception Unbound_method of string Node.t
-type method_ = Ast.ident
-
type stmt =
- [ `ExternalClass of Ast.qname * method_ list
+ [ `ExternalClass of Ast.qname * Ast.sname list
| `External of Ast.qname
| Ast.stmt]
+open Base
(*
Example:
(package A :export '(f g h))
*)
type stmt =
- [ `Class of Ast.ident * Ast.qname * Ast.attr list * Ast.method_ list
- | `Define of Ast.ident * Ast.expr
+ [ `Class of Ast.sname * Ast.qname * Ast.attr list * Ast.method_ list
+ | `Define of Ast.sname * Ast.expr
| `Expr of Ast.expr
- | `Module of Ast.ident * Ast.ident list * stmt ]
-
-
-let trans x = x
+ | `Module of Ast.sname * (Ast.sname list) * stmt list ]
+
+let to_qname ({Node.value = ns} as loc) ({Node.value=name;end_pos=pos}) =
+ {loc with
+ Node.value = (ns,name);
+ end_pos = pos}
+
+let rec trans_stmt ns : stmt -> Ast.stmt list =
+ function
+ `Class (klass,super,attrs,methods) ->
+ [`Class (to_qname ns klass,super,attrs,methods)]
+ | `Define (name,body) ->
+ [`Define (to_qname ns name,body)]
+ | `Expr _ as expr ->
+ [expr]
+ | `Module (ns,_,stmts) ->
+ HList.concat_map (trans_stmt ns) stmts
+
+let trans =
+ HList.concat_map (trans_stmt (Node.empty ""))
| a,b ->
eq_bind a b
-(* random node *)
-let count =
- ref 0
-
let node x =
- let pos ()=
- incr count;
- !count in
- {(Node.empty x) with
- Node.filename = "<string>";
- Node.lineno = 0;
- start_pos = pos ();
- end_pos = pos ()}
+ {(Node.empty x) with
+ Node.filename = "<string>";
+ Node.lineno = 0;
+ start_pos = 0;
+ end_pos = 0}
let qname x =
node ("",x)
+let full_qname ns name =
+ node (ns,name)
+
+let sname =
+ node
+
let string x =
`String (node x)
OUnit.assert_equal
x @@ trans y
+let sname =
+ node
+
let define x y =
`Define (x,y)
"define trans" >::
(fun () ->
ok
- [define (node ("foo","bar")) @@ `Block []]
- [`Module (node "foo",[
- define (node ("","bar")) @@ `Block []])]);
+ [define (node ("foo","bar")) (`Block [])]
+ [`Module (sname "foo",[],[
+ define (sname "bar") (`Block []) ] ) ]);
"class trans" >::
(fun () ->
ok
[`Class (node ("foo","bar"),qname "Object",[],[])]
- [`Module (node "foo",[
- `Class (node ("","bar"),qname "Object",[],[])])])
+ [`Module (sname "foo",[],[
+ `Class (sname "bar",qname "Object",[],[])])])
]) +> run_test_tt