text
stringlengths
0
601k
let record_only_pairs_expected loc sexp = let msg = loc ^ " _of_sexp : record conversion : only pairs expected , \ their first element must be an atom " in of_sexp_error msg sexp
let record_superfluous_fields ~ what ~ loc rev_fld_names sexp = let fld_names_str = String . concat ( List . rev rev_fld_names ) ~ sep " : " in let msg = sprintf " % s_of_sexp : % s : % s " loc what fld_names_str in of_sexp_error msg sexp
let record_duplicate_fields loc rev_fld_names sexp = record_superfluous_fields ~ what " : duplicate fields " ~ loc rev_fld_names sexp
let record_extra_fields loc rev_fld_names sexp = record_superfluous_fields ~ what " : extra fields " ~ loc rev_fld_names sexp
let rec record_get_undefined_loop fields = function | [ ] -> String . concat ( List . rev fields ) ~ sep " : " | ( true , field ) :: rest -> record_get_undefined_loop ( field :: fields ) rest | _ :: rest -> record_get_undefined_loop fields rest
let record_undefined_elements loc sexp lst = let undefined = record_get_undefined_loop [ ] lst in let msg = sprintf " % s_of_sexp : the following record elements were undefined : % s " loc undefined in of_sexp_error msg sexp
let record_list_instead_atom loc sexp = let msg = loc ^ " _of_sexp : list instead of atom for record expected " in of_sexp_error msg sexp
let record_poly_field_value loc sexp = let msg = loc ^ " _of_sexp : cannot convert values of types resulting from polymorphic \ record fields " in of_sexp_error msg sexp
let no_variant_match ( ) = raise No_variant_match
let no_matching_variant_found loc sexp = of_sexp_error ( loc ^ " _of_sexp : no matching variant found " ) sexp
let ptag_no_args loc sexp = of_sexp_error ( loc ^ " _of_sexp : polymorphic variant does not take arguments " ) sexp
let ptag_incorrect_n_args loc cnstr sexp = let msg = sprintf " % s_of_sexp : polymorphic variant tag % S has incorrect number of arguments " loc cnstr in of_sexp_error msg sexp
let ptag_takes_args loc sexp = of_sexp_error ( loc ^ " _of_sexp : polymorphic variant tag takes an argument " ) sexp
let nested_list_invalid_poly_var loc sexp = of_sexp_error ( loc ^ " _of_sexp : a nested list is an invalid polymorphic variant " ) sexp
let empty_list_invalid_poly_var loc sexp = of_sexp_error ( loc ^ " _of_sexp : the empty list is an invalid polymorphic variant " ) sexp
let empty_type loc sexp = of_sexp_error ( loc ^ " _of_sexp : trying to convert an empty type " ) sexp
type env = Sexp_lexer . t String_hashtbl . t
let rec print fmt ( x : Sexp_lexer . t ) t = match x with | Atom v -> Format . pp_print_string fmt v | Lit s -> Format . fprintf fmt " % S " s | List vs -> Format . fprintf fmt [ ( " @@ % a @ ) ] " @ ( Format . pp_print_list ~ pp_sep ( : Format . pp_print_space ) pp_print_space print ) vs | Data vs -> Format . fprintf ...
let print_env fmt env = String_hashtbl . iter ( fun k v -> Format . fprintf fmt [ " @% s @ ->@ % a ] . " @@ k print v ) env
let nil : Sexp_lexer . t = List [ ]
let rec eval env ( x : Sexp_lexer . t ) t : Sexp_lexer . t = match x with | Atom x -> begin match String_hashtbl . find_opt env x with | None -> raise ( Unbound_value x ) x | Some x -> x end | Lit _ -> x | Data xs -> List xs | List [ ] -> nil | List ( command :: rest ) rest -> begin match command with | Atom " setq " -...
let eval_file s = let sexps = Sexp_lexer . from_file s in let env : Sexp_lexer . t String_hashtbl . t = String_hashtbl . create 64 in List . iter ( fun x -> ignore ( eval env x ) ) sexps ; env
let eval_string s = let sexps = Sexp_lexer . token ( Lexing . from_string s ) s in let env : Sexp_lexer . t String_hashtbl . t = String_hashtbl . create 64 in List . iter ( fun x -> ignore ( eval env x ) ) sexps ; env
type ty = | Any | String | List of ty
let error ( x , ty ) ty = raise ( Expect ( x , ty ) ty )
let expect_string ( key , default ) default ( global_data : env ) env = match String_hashtbl . find_exn global_data key with | exception Not_found -> default | Atom s | Lit s -> s | List _ | Data _ -> error ( key , String ) String
let expect_string_opt key ( global_data : env ) env = match String_hashtbl . find_exn global_data key with | exception Not_found -> None | Atom s | Lit s -> Some s | List _ | Data _ -> error ( key , String ) String
let expect_string_list key ( global_data : env ) env = match String_hashtbl . find_exn global_data key with | exception Not_found -> [ ] | Atom _ | Lit _ | Data _ -> error ( errorkey , List String ) String | List xs -> Ext_list . filter_map ( fun x -> match x with | Sexp_lexer . Atom x | Lit x -> Some x | _ -> None ) x...
let expect_string_list_unordered key ( global_data : env ) env init update = match String_hashtbl . find_exn global_data key with | exception Not_found -> init | Atom _ | Lit _ | Data _ -> error ( errorkey , List String ) String | List files -> List . fold_left ( fun acc s -> match s with | Sexp_lexer . Atom s | Lit s ...
module Atom = struct type t = Sexp . Private . Raw_grammar . Atom . t = | String | Bool | Char | Float | Int | This of { ignore_capitalization : bool ; string : string } [ @@ deriving traverse_lift ] let lifter ~ loc = object inherit [ expression ] lift inherit Ppxlib_metaquot_lifters . expression_lifters loc end ; ; e...
let lift_string ~ loc s = pexp_constant ~ loc ( Pconst_string ( s , loc , None ) )
type ' t type_ = ' t Sexp . Private . Raw_grammar . type_ = | Any | Apply of ' t type_ * ' t type_ list | Atom of atom | Explicit_bind of var_name list * ' t type_ | Explicit_var of int | Grammar of ' t | Implicit_var of int | List of ' t sequence_type | Option of ' t type_ | Record of ' t record_type | Recursive of ty...
let lifter ~ loc = let atom_lifter = Atom . lifter ~ loc in object inherit [ expression ] lift inherit Ppxlib_metaquot_lifters . expression_lifters loc method atom atom = atom_lifter # t atom method label = lift_string ~ loc method var_name = lift_var_name ~ loc method type_name = lift_type_name ~ loc end ; ;
module Make ( M : sig val am_running_test : bool end ) = struct type ' a t = ' a [ @@ deriving bin_io , compare , sexp ] let sexp_of_t sexp_of_a a = if M . am_running_test then Sexp . Atom " < hidden_in_test " > else sexp_of_a a ; ; module With_non_roundtripping_in_test_of_sexp = struct type nonrec ' a t = ' a t [ @@ d...
module type S = sig module Raw_grammar = Sexplib0 . Sexp . Private . Raw_grammar type t = Type . t = | Atom of string | List of t list type bigstring = ( char , int8_unsigned_elt , c_layout ) Array1 . t val compare : t -> t -> int val equal : t -> t -> bool val default_indent : int ref val size : t -> int * int val sca...
module Grammar = struct type t = entry list and entry = | Keyword of string | Atom | List of t | Repeat of t [ @@ deriving sexp ] end
module Parser : sig type ' a t include Selective . S with type ' a t := ' a t val keyword : string -> unit t val atom : string t val enter : ' a t -> ' a t val repeat : ' a t -> ' a list t val is_list : bool t val parse : ' a t -> Sexp . t list -> ' a val grammar : _ t -> Grammar . t module Let_syntax : sig val return ...
module Test = struct open Parser type hello_to = | These of string list | World [ @@ deriving sexp ] let expr = enter ( let % map ( ) = keyword " hello " and y = ifS is_list ( let % map x = enter ( repeat atom ) in These x ) ( let % map ( ) = keyword " world " ! in World ) in y ) let % expect_test _ = let v = parse exp...
module Format = struct include Caml . Format let pp_listi sep ( ? offset = 0 ) ? singleton pp fmt list = match list with | [ ] -> ( ) | hd :: tl -> ( match singleton , tl with | Some pp , [ ] -> pp offset fmt hd | _ -> pp offset fmt hd ) ; List . iteri tl ~ f ( : fun i el -> Caml . Format . fprintf fmt sep ; pp ( i + o...
type state = { is_comment : bool }
let start_state = { is_comment = false }
let split = Re . Str . regexp " [ \ t ] " +
let color_to_code = function | Black -> 30 | Red -> 31 | Green -> 32 | Yellow -> 33 | Blue -> 34 | Magenta -> 35 | Cyan -> 36 | White -> 37 | Default -> 39 ; ;
let rainbow_open_tag conf tag = let args = Re . Str . split split tag in let color_count = Array . length conf . color_scheme in match args with | [ " d " ; n ] -> let i = Int . of_string n in " [ "  ^ Int . to_string ( color_to_code ( if i < 0 || color_count < 1 then Default else conf . color_scheme . ( i % color_cou...
let rainbow_tags conf = { Format . mark_open_tag = rainbow_open_tag conf ; Format . mark_close_tag = ( fun _ -> match conf . comments with | Print ( _ , Some _clr , _ ) -> " " | _ -> " " ) ; Format . print_open_tag = ignore ; Format . print_close_tag = ignore } ; ;
let open_parens conf state ~ depth fmt n = match conf . paren_coloring , state . is_comment , conf . comments with | _ , true , Print ( _ , Some _ , _ ) -> for i = depth to depth + n - 1 do Format . fprintf fmt " { @< c % d ( } " >@ i done | true , false , _ -> for i = depth to depth + n - 1 do Format . fprintf fmt " {...
let close_parens conf state ~ depth fmt n = match conf . paren_coloring , state . is_comment , conf . comments with | _ , true , Print ( _ , Some _ , _ ) -> for i = depth + ( n - 1 ) downto depth do Format . fprintf fmt " { @< c % d ) } " >@ i done | true , false , _ -> for i = depth + ( n - 1 ) downto depth do Format ...
let must_escape = function | " " \\ -> false | string -> Sexplib . Pre_sexp . must_escape string ; ;
let minimal_escaping at = let body = String . concat_map at ~ f ( : fun char -> match char with | ' " ' | ' ' \\ -> String . of_char ' ' \\ ^ String . of_char char | ' ' | ' \ t ' | ' \ n ' -> String . of_char char | _ -> if Char . is_print char then String . of_char char else Char . escaped char ) in String . concat [...
let atom_escape conf at = match conf . atom_printing with | Escaped | Interpreted -> Sexplib . Pre_sexp . mach_maybe_esc_str at | Minimal_escaping -> if Sexplib . Pre_sexp . must_escape at then minimal_escaping at else at ; ;
let atom_printing_len conf at = let s = atom_escape conf at in if String . for_all s ~ f : Char . is_print then Some ( String . length s ) else None ; ;
let atom_printing_len_exn conf at = match atom_printing_len conf at with | Some len -> len | None -> raise_s ( Sexp . List [ Atom " Sexp_pretty . atom_printing_len_exn : invalid input " ; Atom at ] ) ; ;
let pp_atom conf state ~ depth ~ len index fmt at = let at = if state . is_comment then at else if must_escape at then ( match conf . atom_printing with | Escaped | Interpreted -> Sexplib . Pre_sexp . esc_str at | Minimal_escaping -> minimal_escaping at ) else at in let should_be_colored = match conf . atom_coloring wi...
let pp_associated_comments conf ~ depth fmt associated_comments = if not ( List . is_empty associated_comments ) then ( Format . fprintf fmt " " ; Format . pp_open_vbox fmt 0 ; List . iteri associated_comments ~ f ( : fun i comment -> if i > 0 then Format . pp_print_break fmt 0 0 ; pp_atom conf { is_comment = true } ~ ...
module Normalize = struct type t = | Sexp of sexp * string list | Comment of comment and comment = | Line_comment of string | Block_comment of int * string list | Sexp_comment of comment list * sexp and sexp = | Atom of string | List of t list let parse_sexps = Sexp . With_layout . Parser . sexps Sexp . With_layout . L...
module Print = struct module N = Normalize type associated_comments = string list type forces_breakline = bool type opened = | Opened | Closed type ' a tree = | Node of ' a tree list | Leaf of ' a type shape = ( int * string ) tree type t = | Sexp of sexp * associated_comments | Comment of comment and comment = | Line_...
let setup conf fmt = ( Format . pp_set_formatter_tag_functions fmt ( rainbow_tags conf ) [ @ ocaml . warning " - 3 " ] ) ; Format . pp_set_tags fmt true ; ;
let run ~ next conf fmt = setup conf fmt ; let rec loop prints_newline = match next ( ) with | None -> ( ) | Some t_or_comment -> ( match conf . comments , t_or_comment with | Drop , W . Comment _ -> loop prints_newline | Print _ , W . Comment _ -> ( match prints_newline , conf . separator with | true , Empty_line -> F...
let dummy_pos = { Sexplib . Src_pos . Relative . row = 0 ; col = 0 }
let rec sexp_to_sexp_or_comment = function | Sexp . Atom at -> let fmt_at = Some ( Sexplib . Pre_sexp . mach_maybe_esc_str at ) in W . Sexp ( W . Atom ( dummy_pos , at , fmt_at ) ) | Sexp . List list -> W . Sexp ( W . List ( dummy_pos , List . map list ~ f : sexp_to_sexp_or_comment , dummy_pos ) ) ; ;
module type S = sig type sexp type ' a writer = Config . t -> ' a -> sexp -> unit val pp_formatter : Format . formatter writer val pp_formatter ' : next ( : unit -> sexp option ) -> Config . t -> Caml . Format . formatter -> unit val pp_buffer : Buffer . t writer val pp_out_channel : Caml . out_channel writer val pp_bl...
module Make ( M : sig type t val to_sexp_or_comment : t -> Sexp . With_layout . t_or_comment end ) : S with type sexp := M . t = struct type ' a writer = Config . t -> ' a -> M . t -> unit let pp_formatter conf fmt sexp = let t_or_comment = M . to_sexp_or_comment sexp in let next = let stop = ref false in fun ( ) -> if...
module Sexp_with_layout = Make ( struct type t = W . t_or_comment let to_sexp_or_comment = Fn . id end )
module Lexer = struct open P1 let chr c = tok ( Asp . Utilities . Unstaged . Chr c ) let charset s = let len = String . length s in let rec loop i acc = if i < len then loop ( i + 1 ) ( acc <|> chr s . [ i ] ) else acc in loop 0 bot let lower = charset " abcdefghijklmnopqrstuvwxyz " let upper = charset " ABCDEFGHIJKLMN...
module Parser = struct open P2 let parse = let open Sexp_tokens_base in fix @@ fun sexp -> ( ( tok LPAREN >>> star sexp >>> tok RPAREN ) $ fun x -> . < List . fold_left ( ) + 0 ( snd ( fst . ~ x ) ) . ) > <|> ( tok ATOM $ fun _ -> . < 1 . ) > let parsecode = let module R = P2 . Parser ( Asp_streamcode . Streamcode ( st...
let ( ) = Printexc . record_backtrace true
let ( ) = for len = 0 to 3 do let s = Bytes . create len in for i = 0 to ( 1 lsl ( len * 8 ) ) - 1 do if len > 0 then Bytes . set s 0 ( Char . chr ( i land 0xff ) ) ; if len > 1 then Bytes . set s 1 ( Char . chr ( ( i lsr 4 ) land 0xff ) ) ; if len > 2 then Bytes . set s 2 ( Char . chr ( ( i lsr 8 ) land 0xff ) ) ; let...
type _ tag = ATOM : char list tag
module Tag = struct type ' a t = ' a tag let print : type a . Format . formatter -> a t -> unit = fun fmt -> function | ATOM -> Format . fprintf fmt " ATOM " | LPAREN -> Format . fprintf fmt " LPAREN " | RPAREN -> Format . fprintf fmt " RPAREN " let compare : type a b . a t -> b t -> ( a , b ) Asp . Types . cmp = fun x...
type utag = U : _ tag -> utag [ @@ unboxed ]
type t = T : ' a tag * ' a -> t
let to_int : utag -> int = Obj . magic
let tag : t -> utag = function ( T ( tag , _ ) ) -> U tag
module Ord = struct type t = utag let compare l r = Pervasives . compare ( to_int l ) ( to_int r ) end
let all = TagSet . of_list [ U ATOM ; U LPAREN ; U RPAREN ]
let inj t = U t
let match_ : type a b . t -> a tag -> ( a -> b ) -> ( unit -> b ) -> b = fun ( T ( tag , v ) ) tag ' yes no -> if U tag = U tag ' then yes ( Obj . magic v ) else no ( )
let matchlist_ : type a b . t -> a tag list -> ( a -> b ) -> ( unit -> b ) -> b = fun ( T ( tag , v ) ) tags yes no -> if List . mem ( Obj . magic tag ) tags then yes ( Obj . magic v ) else no ( )
module Lexer = struct open P1 let chr c = tok ( Asp . Utilities . Unstaged . Chr c ) $ fun _ -> c let charset s = let len = String . length s in let rec loop i acc = if i < len then loop ( i + 1 ) ( acc <|> chr s . [ i ] ) else acc in loop 0 bot let lower = charset " abcdefghijklmnopqrstuvwxyz " let upper = charset " A...
module Parser = struct open P2 let parse = let open Sexp_tokens_base in fix @@ fun sexp -> ( ( tok LPAREN >>> star sexp >>> tok RPAREN ) $ fun x -> List . fold_left ( ) + 0 ( snd ( fst x ) ) ) <|> ( tok ATOM $ fun _ -> 1 ) let parser : P2 . token Stream . t -> int = P2 . parse ( P2 . type_check parse ) let unstaged_com...
module List = struct let iter t ~ f = List . iter f t let map t ~ f = List . rev ( List . rev_map f t ) end
type pos = Src_pos . Relative . t = { row : int ; col : int }
module Lexer = struct let main = Lexer . main_with_layout end
module Render = struct module Rel_pos = Src_pos . Relative module Abs_pos = Src_pos . Absolute type last_atom = { immed_after : Abs_pos . t ; unescaped : bool } type state = { mutable row_shift : Rel_pos . t ; mutable current : Abs_pos . t ; mutable last_atom : last_atom option ; mutable last_comment_row : int } type '...
module Forget = struct module Cps : sig val forget_t : t -> ( Type . t -> ' r ) -> ' r val forget_toc : t_or_comment -> ( Type . t option -> ' r ) -> ' r val forget_tocs : t_or_comment list -> ( Type . t list -> ' r ) -> ' r end = struct let rec forget_t t k = match t with | Atom ( _ , x , _ ) -> k ( Type . Atom x ) | ...
module Data = struct type t = ( int * string ) list [ @@ deriving sexp ] end
type t = Data . t Sexp . With_text . t String . Map . t [ @@ deriving sexp ]
let edit_file ( type a ) ( module A : Sexpable with type t = a ) filename = let editor = match Sys . getenv " EDITOR " with | Some x -> x | None -> " emacs " in let rec loop ( ) = Unix . system_exn ( String . concat [ editor ; " " ; filename ] ) >>= fun ( ) -> Reader . file_contents filename >>= fun text -> match Sexp ...
let rec edit_loop t = printf " key to edit : " ; Reader . read_line ( force Reader . stdin ) >>= function | ` Eof -> Deferred . unit | ` Ok key -> let current = match Map . find t key with | Some x -> x | None -> Sexp . With_text . of_value [ % sexp_of : ( int * string ) list ] [ ] in let filename = Filename . temp_fil...
let ( ) = don ' t_wait_for ( edit_loop String . Map . empty ) ; never_returns ( Scheduler . go ( ) ) ; ;
let ( $ ) f x = f x
module C = struct let size = 20 . 0 end
let font = new font ( ` File " resources / sansation . ttf " )
let pause_message = new text ~ font ~ character_size : 40 ~ position ( : 170 . 0 , 150 . 0 ) ~ color : Color . white ~ string " : Paused : Press p to unpause " ~ style [ : Bold ] ( )
let dead_message = new text ~ font ~ character_size : 40 ~ position ( : 170 . 0 , 150 . 0 ) ~ color : Color . white ~ string " : Dead : Press space to start the game " ~ style [ : Bold ] ( )
let top_left row col = ( C . size . * ( float_of_int col ) , C . size . * ( float_of_int row ) )
let color_of_stone color = let s x = int_of_float ( x . * 255 . 0 ) in let ( r , g , b ) = Stone . rgb color in Color . rgb ( s r ) ( s g ) ( s b )
let rect_of row col color = let position = top_left row col in let size = ( C . size , C . size ) in let fill_color = color_of_stone color in new rectangle_shape ~ size ~ position ~ outline_thickness : 1 . 0 ~ outline_color : Color . white ~ fill_color ( )
let draw_board ( display : render_window ) w = let open World in let open Board in let bgcolor = Color . rgb 0 0 0 in display # clear ~ color : bgcolor ( ) ; let b = w . board in for r = 0 to b . rows - 1 do for c = 0 to b . cols - 1 do match b . grid . ( r ) . ( c ) with | Some color -> begin let rect = rect_of r c co...
let test_tetris ( ) = let open World in Random . self_init ( ) ; let game_width = 600 in let game_height = 800 in let vm = VideoMode . create ~ w : game_width ~ h : game_height ( ) in let app = new render_window vm " Ocsfml - Tetris " in let global_clock = new clock in let world = World . make default_configuration 0 i...