Skip to content

Commit

Permalink
Code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
alavrik committed Oct 8, 2012
1 parent 7ec8a43 commit 2ab9095
Show file tree
Hide file tree
Showing 26 changed files with 174 additions and 202 deletions.
2 changes: 1 addition & 1 deletion piqi-tools/piqi_call.ml
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ let get_http_piqi path =


let find_function piqi name =
trace "piqi_call: find function %s\n" (quote name);
trace "piqi_call: find function %s\n" (U.quote name);
try List.find (fun x -> x.T.Func.name = name) piqi.P#resolved_func
with Not_found ->
piqi_error ("server doesn't implement function: " ^ name)
Expand Down
6 changes: 3 additions & 3 deletions piqi-tools/piqi_convert_cmd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,15 @@ let rec get_piqi_deps piqi ~only_imports =
let includes = if only_imports then [piqi] else piqi.P#included_piqi in
(* get all dependencies from imports *)
let import_deps =
flatmap (fun x ->
U.flatmap (fun x ->
let piqi = some_of x.T.Import#piqi in
flatmap (get_piqi_deps ~only_imports) piqi.P#included_piqi)
U.flatmap (get_piqi_deps ~only_imports) piqi.P#included_piqi)
piqi.P#resolved_import
in
(* NOTE: imports go first in the list of dependencies *)
let l = import_deps @ includes in
(* remove duplicate entries *)
C.uniqq l
U.uniqq l


let get_parent_piqi (t: T.piqtype) =
Expand Down
2 changes: 1 addition & 1 deletion piqi-tools/piqi_server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ let do_run name f x =
try f x
with exn ->
let response = return_rpc_error
(`internal_error ("error while running function " ^ quote name ^ ": " ^ string_of_exn exn))
(`internal_error ("error while running function " ^ U.quote name ^ ": " ^ string_of_exn exn))
in
raise (Break response)

Expand Down
4 changes: 2 additions & 2 deletions piqi-tools/piqi_to_proto.ml
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ and gen_alias ?name ?parent ?(is_func_param=false) a =


let gen_defs (defs:T.typedef list) =
let defs = flatmap gen_def defs in
let defs = U.flatmap gen_def defs in
iod "\n" defs


Expand Down Expand Up @@ -424,7 +424,7 @@ let gen_imports l =
let modnames = List.map (fun x -> x.Import#modname) l in
(* using C.uniq to prevent importing a module more than once, otherwise protoc
* will fail to compile *)
let l = List.map gen_import (C.uniq modnames) in
let l = List.map gen_import (U.uniq modnames) in
iol l


Expand Down
14 changes: 7 additions & 7 deletions piqic/piqic_common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ let get_builtin_defs piqi seen_defs def =
| None -> []
in
match def with
| `record x -> flatmap (fun x -> get_builtin_def_opt x.F#piqtype) x.R#field
| `variant x -> flatmap (fun x -> get_builtin_def_opt x.O#piqtype) x.V#option
| `record x -> U.flatmap (fun x -> get_builtin_def_opt x.F#piqtype) x.R#field
| `variant x -> U.flatmap (fun x -> get_builtin_def_opt x.O#piqtype) x.V#option
| `list x -> get_builtin_def (some_of x.L#piqtype)
| `enum _ -> []
| `alias a -> get_builtin_def (some_of a.A#piqtype)
Expand All @@ -84,11 +84,11 @@ let get_builtin_defs piqi seen_defs def =
* definitions *)
let get_builtin_dependencies piqi =
let rec aux accu root_defs =
let new_builtin_defs = flatmap (get_builtin_defs piqi accu) root_defs in
let new_builtin_defs = U.flatmap (get_builtin_defs piqi accu) root_defs in
if new_builtin_defs = []
then accu
else
let accu = uniqq (new_builtin_defs @ accu) in
let accu = U.uniqq (new_builtin_defs @ accu) in
aux accu new_builtin_defs
in
aux [] piqi.P#resolved_typedef
Expand Down Expand Up @@ -117,12 +117,12 @@ let rec get_piqi_deps piqi =
in
(* get all imports' dependencies recursively *)
let import_deps =
flatmap (fun piqi ->
flatmap get_piqi_deps piqi.P#included_piqi
U.flatmap (fun piqi ->
U.flatmap get_piqi_deps piqi.P#included_piqi
) imports
in
(* remove duplicate entries *)
let deps = C.uniqq (import_deps @ imports) in
let deps = U.uniqq (import_deps @ imports) in
deps @ [piqi]


Expand Down
6 changes: 3 additions & 3 deletions piqic/piqic_erlang_out.ml
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ let rec gen_option outer_option o =
(* recursively generate cases from "included" variants *)
let gen_options options =
if outer_option <> None
then flatmap (gen_option outer_option) options
else flatmap (gen_option (Some o)) options
then U.flatmap (gen_option outer_option) options
else U.flatmap (gen_option (Some o)) options
in
match o.erlang_name, o.piqtype with
| Some ename, None -> (* gen true *)
Expand Down Expand Up @@ -247,7 +247,7 @@ let rec gen_option outer_option o =

let gen_variant v =
let open Variant in
let options = flatmap (gen_option None) v.option in
let options = U.flatmap (gen_option None) v.option in
iol
[
ios "gen_" ^^ ios (some_of v.erlang_name);
Expand Down
4 changes: 2 additions & 2 deletions piqic/piqic_erlang_types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,9 @@ let gen_def x =


let gen_defs (defs:T.typedef list) =
let records = flatmap (function `record x -> [x] | _ -> []) defs in
let records = U.flatmap (function `record x -> [x] | _ -> []) defs in
let record_types = List.map gen_record_type records in
let defs = flatmap gen_def defs in
let defs = U.flatmap gen_def defs in
let code = iol [
iol defs; eol;
iol record_types; eol;
Expand Down
6 changes: 3 additions & 3 deletions piqic/piqic_ocaml_types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,8 @@ let gen_mod_def = function


let gen_defs (defs:T.typedef list) =
let mod_defs = flatmap gen_mod_def defs in
let odefs = flatmap gen_def defs in
let mod_defs = U.flatmap gen_mod_def defs in
let odefs = U.flatmap gen_def defs in
let odef =
let odef =
if odefs = []
Expand Down Expand Up @@ -367,7 +367,7 @@ let order_variant_defs variants =
let get_adjacent_vertixes = function
| `variant v ->
(* get the list of included variants *)
flatmap (fun o ->
U.flatmap (fun o ->
let open O in
match o.ocaml_name, o.piqtype with
| None, Some ((`variant _) as def)
Expand Down
2 changes: 1 addition & 1 deletion piqilib/Makefile.piqi_common
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ all:

PIQI_SOURCES_HEAD = \
piqloc.ml \
piqi_util.ml \
piq_ast.ml \


Expand All @@ -19,7 +20,6 @@ PIQI_SOURCES_TAIL = \
piqi_config.ml \
piqi_iolist.ml \
piqi_graph.ml \
piqi_util.ml \
piqi_name.ml \
piqi_common.ml \
piqi_file.ml \
Expand Down
2 changes: 1 addition & 1 deletion piqilib/piq.ml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ let default_piqtype = ref None

let check_piqtype n =
if not (Piqi_name.is_valid_typename n)
then error n ("invalid type name: " ^ quote n)
then error n ("invalid type name: " ^ U.quote n)
else ()


Expand Down
24 changes: 24 additions & 0 deletions piqilib/piq_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,27 @@ include Piq_ast
let ast_of_bool x = `bool x
let ast_to_bool _ = true


(* apply function f to the node identified by its path in the tree *)
let transform_ast path f (ast:ast) =
let rec aux p = function
| `list l when p = [] -> (* leaf node *)
(* f replaces, removes element, or splices elements of the list *)
let res = Piqi_util.flatmap f l in
`list res
| x when p = [] -> (* leaf node *)
(* expecting f to replace the existing value, no other modifications
* such as removal or splicing is allowed in this context *)
(match f x with [res] -> res | _ -> assert false)
| `list l ->
(* haven't reached the leaf node => continue tree traversal *)
let res = List.map (aux p) l in
`list res
| `named {Named.name = n; value = v} when List.hd p = n ->
(* found path element => continue tree traversal *)
let res = {Named.name = n; value = aux (List.tl p) v} in
`named res
| x -> x
in
aux path ast

16 changes: 8 additions & 8 deletions piqilib/piq_parser.ml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ let tokenize s ?(start=0) sep =
let tokenize_name ?start s =
let l = tokenize s '.' ?start in
match l with
| h::t -> h :: (flatmap (fun x -> ["."; x]) t)
| h::t -> h :: (U.flatmap (fun x -> ["."; x]) t)
| _ -> assert false


Expand All @@ -70,11 +70,11 @@ let tokenize_typename s =
let tokenize_name first_c s =
let parts = tokenize s ':' in
match first_c with
| ":" -> flatmap tokenize_typename parts
| ":" -> U.flatmap tokenize_typename parts
| "." ->
(match parts with
| h::t ->
"." :: (tokenize_name h) @ (flatmap tokenize_typename t)
"." :: (tokenize_name h) @ (U.flatmap tokenize_typename t)
| _ -> assert false)
| _ -> assert false

Expand All @@ -83,13 +83,13 @@ let check_name n =
(* XXX: this should refer to piq rather than piqi name *)
if Piqi_name.is_valid_name n ~allow:"."
then ()
else error n ("invalid name: " ^ quote n)
else error n ("invalid name: " ^ U.quote n)


let check_typename n =
if Piqi_name.is_valid_typename n ~allow:"."
then ()
else error n ("invalid type name: " ^ quote n)
else error n ("invalid type name: " ^ U.quote n)


let piq_addrefret dst (src :piq_ast) =
Expand Down Expand Up @@ -285,7 +285,7 @@ let expand_control (x: piq_ast) :piq_ast =
if List.exists (function `control _ -> true | _ -> false) l
then
(* expand and splice the results of control expansion *)
`list (flatmap expand_list_elem l)
`list (U.flatmap expand_list_elem l)
else
(* process inner elements *)
`list (List.map aux l)
Expand Down Expand Up @@ -359,7 +359,7 @@ let parse_int s =
Piqloc.addref s i;
`uint i
with Failure _ ->
failwith ("invalid integer literal: " ^ quote s)
failwith ("invalid integer literal: " ^ U.quote s)


let parse_float s =
Expand All @@ -376,7 +376,7 @@ let parse_float s =
Piqloc.addref s f;
`float f
with Failure _ ->
failwith ("invalid floating point literal: " ^ quote s)
failwith ("invalid floating point literal: " ^ U.quote s)


let parse_number s =
Expand Down
Loading

0 comments on commit 2ab9095

Please sign in to comment.