Merge pull request #1393 from ZackerySpytz/OCaml-examples
[OCaml] Fix some of the OCaml examples
This commit is contained in:
commit
4e4bb54fe7
21 changed files with 83 additions and 86 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
|
@ -164,8 +164,8 @@ Examples/test-suite/ocaml/*.cm*
|
|||
Examples/test-suite/ocaml/runme
|
||||
!Examples/test-suite/ocaml/*runme.ml
|
||||
Examples/ocaml/**/example.ml*
|
||||
Examples/ocaml/**/example
|
||||
Examples/ocaml/**/example_top
|
||||
Examples/ocaml/**/runme
|
||||
Examples/ocaml/**/runme_top
|
||||
Examples/ocaml/**/*.cm*
|
||||
Examples/ocaml/**/swig.ml*
|
||||
Examples/ocaml/**/swigp4.ml
|
||||
|
|
|
|||
|
|
@ -919,7 +919,7 @@ ocaml_static: $(SRCDIR_SRCS)
|
|||
$(OCC) -g -c $(INTERFACE:%.i=%.mli)
|
||||
$(OCC) -w -U -g -c $(INTERFACE:%.i=%.ml)
|
||||
test -z "$(PROGFILE)" || $(OCC) -o $(PROGFILE:%.ml=%) $(OCAMLPP) -c $(SRCDIR)$(PROGFILE)
|
||||
$(NOLINK) || $(OCC) -g -ccopt -g -cclib -g -custom -o $(TARGET) swig.cmo $(INTERFACE:%.i=%.cmo) $(PROGFILE:%.ml=%.cmo) $(INTERFACE:%.i=%_wrap.@OBJEXT@) $(OBJS) -cclib "$(LIBS)"
|
||||
$(NOLINK) || $(OCC) -g -ccopt -g -cclib -g -custom -o $(RUNME) swig.cmo $(INTERFACE:%.i=%.cmo) $(PROGFILE:%.ml=%.cmo) $(INTERFACE:%.i=%_wrap.@OBJEXT@) $(OBJS) -cclib "$(LIBS)"
|
||||
|
||||
ocaml_dynamic: $(SRCDIR_SRCS)
|
||||
$(OCAMLCORE)
|
||||
|
|
@ -931,7 +931,7 @@ ocaml_dynamic: $(SRCDIR_SRCS)
|
|||
rm $(INTERFACE:%.i=%.mli)
|
||||
$(OCAMLFIND) $(OCC) -g -c -package dl $(INTERFACE:%.i=%.ml)
|
||||
test -z "$(PROGFILE)" || $(OCC) -o $(PROGFILE:%.ml=%) $(OCAMLPP) -c $(SRCDIR)$(PROGFILE)
|
||||
$(NOLINK) || $(OCAMLFIND) $(OCC) -g -ccopt -g -cclib -g -custom -o $(TARGET) swig.cmo -package dl -linkpkg $(INTERFACE:%.i=%.cmo) $(PROGFILE:%.ml=%.cmo)
|
||||
$(NOLINK) || $(OCAMLFIND) $(OCC) -g -ccopt -g -cclib -g -custom -o $(RUNME) swig.cmo -package dl -linkpkg $(INTERFACE:%.i=%.cmo) $(PROGFILE:%.ml=%.cmo)
|
||||
|
||||
ocaml_static_toplevel: $(SRCDIR_SRCS)
|
||||
$(OCAMLCORE)
|
||||
|
|
@ -940,7 +940,7 @@ ocaml_static_toplevel: $(SRCDIR_SRCS)
|
|||
$(OCC) -g -c $(INTERFACE:%.i=%.mli)
|
||||
$(OCC) -w -U -g -c $(INTERFACE:%.i=%.ml)
|
||||
test -z "$(PROGFILE)" || $(OCC) -o $(PROGFILE:%.ml=%) $(OCAMLPP) -c $(SRCDIR)$(PROGFILE)
|
||||
$(NOLINK) || $(OCAMLMKTOP) swig.cmo -I $(OCAMLP4WHERE) camlp4o.cma swigp4.cmo -g -ccopt -g -cclib -g -custom -o $(TARGET)_top $(INTERFACE:%.i=%.cmo) $(INTERFACE:%.i=%_wrap.@OBJEXT@) $(OBJS) -cclib "$(LIBS)"
|
||||
$(NOLINK) || $(OCAMLMKTOP) swig.cmo -I $(OCAMLP4WHERE) camlp4o.cma swigp4.cmo -g -ccopt -g -cclib -g -custom -o $(RUNME)_top $(INTERFACE:%.i=%.cmo) $(INTERFACE:%.i=%_wrap.@OBJEXT@) $(OBJS) -cclib "$(LIBS)"
|
||||
|
||||
ocaml_static_cpp: $(SRCDIR_SRCS)
|
||||
$(OCAMLCORE)
|
||||
|
|
@ -950,7 +950,7 @@ ocaml_static_cpp: $(SRCDIR_SRCS)
|
|||
$(OCC) -g -c $(INTERFACE:%.i=%.mli)
|
||||
$(OCC) -w -U -g -c $(INTERFACE:%.i=%.ml)
|
||||
test -z "$(PROGFILE)" || $(OCC) -o $(PROGFILE:%.ml=%) $(OCAMLPP) -c $(SRCDIR)$(PROGFILE)
|
||||
$(NOLINK) || $(OCC) -g -ccopt -g -cclib -g -custom -o $(TARGET) swig.cmo $(INTERFACE:%.i=%.cmo) $(PROGFILE:%.ml=%.cmo) $(INTERFACE:%.i=%_wrap.@OBJEXT@) $(OBJS) -cclib "$(LIBS)" -cc '$(CXX) -Wno-write-strings'
|
||||
$(NOLINK) || $(OCC) -g -ccopt -g -cclib -g -custom -o $(RUNME) swig.cmo $(INTERFACE:%.i=%.cmo) $(PROGFILE:%.ml=%.cmo) $(INTERFACE:%.i=%_wrap.@OBJEXT@) $(OBJS) -cclib "$(LIBS)" -cc '$(CXX) -Wno-write-strings'
|
||||
|
||||
ocaml_static_cpp_toplevel: $(SRCDIR_SRCS)
|
||||
$(OCAMLCORE)
|
||||
|
|
@ -960,7 +960,7 @@ ocaml_static_cpp_toplevel: $(SRCDIR_SRCS)
|
|||
$(OCC) -g -c $(INTERFACE:%.i=%.mli)
|
||||
$(OCC) -w -U -g -c $(INTERFACE:%.i=%.ml)
|
||||
test -z "$(PROGFILE)" || $(OCC) -o $(PROGFILE:%.ml=%) $(OCAMLPP) -c $(SRCDIR)$(PROGFILE)
|
||||
$(NOLINK) || $(OCAMLMKTOP) swig.cmo -I $(OCAMLP4WHERE) dynlink.cma camlp4o.cma swigp4.cmo -g -ccopt -g -cclib -g -custom -o $(TARGET)_top $(INTERFACE:%.i=%.cmo) $(INTERFACE:%.i=%_wrap.@OBJEXT@) $(OBJS) -cclib "$(LIBS)" -cc '$(CXX) -Wno-write-strings'
|
||||
$(NOLINK) || $(OCAMLMKTOP) swig.cmo -I $(OCAMLP4WHERE) dynlink.cma camlp4o.cma swigp4.cmo -g -ccopt -g -cclib -g -custom -o $(RUNME)_top $(INTERFACE:%.i=%.cmo) $(INTERFACE:%.i=%_wrap.@OBJEXT@) $(OBJS) -cclib "$(LIBS)" -cc '$(CXX) -Wno-write-strings'
|
||||
|
||||
ocaml_dynamic_cpp: $(SRCDIR_SRCS)
|
||||
$(OCAMLCORE)
|
||||
|
|
@ -973,14 +973,14 @@ ocaml_dynamic_cpp: $(SRCDIR_SRCS)
|
|||
rm $(INTERFACE:%.i=%.mli)
|
||||
$(OCAMLFIND) $(OCC) -g -c -package dl $(INTERFACE:%.i=%.ml)
|
||||
test -z "$(PROGFILE)" || $(OCC) -o $(PROGFILE:%.ml=%) $(OCAMLPP) -c $(SRCDIR)$(PROGFILE)
|
||||
$(NOLINK) || $(OCAMLFIND) swig.cmo $(OCC) -cclib -export-dynamic -g -ccopt -g -cclib -g -custom -o $(TARGET) -package dl -linkpkg $(INTERFACE:%.i=%.cmo) $(PROGFILE:%.ml=%.cmo) -cc '$(CXX) -Wno-write-strings'
|
||||
$(NOLINK) || $(OCAMLFIND) swig.cmo $(OCC) -cclib -export-dynamic -g -ccopt -g -cclib -g -custom -o $(RUNME) -package dl -linkpkg $(INTERFACE:%.i=%.cmo) $(PROGFILE:%.ml=%.cmo) -cc '$(CXX) -Wno-write-strings'
|
||||
|
||||
# -----------------------------------------------------------------
|
||||
# Run ocaml example
|
||||
# -----------------------------------------------------------------
|
||||
|
||||
ocaml_run:
|
||||
$(RUNTOOL) ./$(TARGET) $(RUNPIPE)
|
||||
$(RUNTOOL) ./$(RUNME) $(RUNPIPE)
|
||||
|
||||
# -----------------------------------------------------------------
|
||||
# Version display
|
||||
|
|
@ -994,7 +994,7 @@ ocaml_version:
|
|||
# -----------------------------------------------------------------
|
||||
|
||||
ocaml_clean:
|
||||
rm -f *_wrap* *~ .~* *.cmo *.cmi $(MLFILE) $(MLFILE)i swig.mli swig.cmi swig.ml swig.cmo swigp4.ml swigp4.cmo
|
||||
rm -f *_wrap* *~ .~* *.cmo *.cmi *.mli $(TARGET).ml $(RUNME) $(RUNME)_top swig.ml swigp4.ml
|
||||
rm -f core @EXTRA_CLEAN@
|
||||
rm -f *.@OBJEXT@ *@SO@
|
||||
|
||||
|
|
|
|||
|
|
@ -4,8 +4,7 @@ SWIG_LIB_DIR = $(TOP)/../$(TOP_BUILDDIR_TO_TOP_SRCDIR)Lib
|
|||
SRCS = example.c
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
MLFILE = example.ml
|
||||
PROGFILE = example_prog.ml
|
||||
PROGFILE = runme.ml
|
||||
OBJS = example.o
|
||||
|
||||
check: build
|
||||
|
|
@ -16,16 +15,16 @@ build: static
|
|||
static:
|
||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
|
||||
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
|
||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MLFILE='$(MLFILE)' \
|
||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
|
||||
PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
|
||||
ocaml_static_cpp
|
||||
|
||||
dynamic:
|
||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
|
||||
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
|
||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MLFILE='$(MLFILE)' \
|
||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
|
||||
PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
|
||||
ocaml_dynamic_cpp
|
||||
|
||||
clean:
|
||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' MLFILE='$(MLFILE)' ocaml_clean
|
||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' TARGET='$(TARGET)' ocaml_clean
|
||||
|
|
|
|||
|
|
@ -1,26 +0,0 @@
|
|||
(* example_prog.ml *)
|
||||
|
||||
open Swig
|
||||
open Example
|
||||
|
||||
exception BadReturn
|
||||
|
||||
let _ = if Array.length Sys.argv < 3 then
|
||||
begin
|
||||
print_endline
|
||||
("Usage: " ^ Sys.argv.(0) ^ " n1 n2\n" ^
|
||||
" Displays the least factors of the numbers that have the same\n" ^
|
||||
" relationship, 16 12 -> 4 3\n") ;
|
||||
exit 0
|
||||
end
|
||||
|
||||
let x = int_of_string Sys.argv.(1)
|
||||
let y = int_of_string Sys.argv.(2)
|
||||
let (xf,yf) = match _factor '((x to int),(y to int)) with
|
||||
C_list [ C_int a ; C_int b ] -> a,b
|
||||
| _ -> raise BadReturn
|
||||
let _ = print_endline
|
||||
("Factorization of " ^ (string_of_int x) ^
|
||||
" and " ^ (string_of_int y) ^
|
||||
" is " ^ (string_of_int xf) ^
|
||||
" and " ^ (string_of_int yf))
|
||||
17
Examples/ocaml/argout_ref/runme.ml
Normal file
17
Examples/ocaml/argout_ref/runme.ml
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
(* runme.ml *)
|
||||
|
||||
open Swig
|
||||
open Example
|
||||
|
||||
exception BadReturn
|
||||
|
||||
let x = if Array.length Sys.argv > 1 then int_of_string Sys.argv.(1) else 16
|
||||
let y = if Array.length Sys.argv > 2 then int_of_string Sys.argv.(2) else 12
|
||||
let (xf,yf) = match _factor '((x to int),(y to int)) with
|
||||
C_list [ C_int a ; C_int b ] -> a,b
|
||||
| _ -> raise BadReturn
|
||||
let _ = print_endline
|
||||
("Factorization of " ^ (string_of_int x) ^
|
||||
" and " ^ (string_of_int y) ^
|
||||
" is " ^ (string_of_int xf) ^
|
||||
" and " ^ (string_of_int yf))
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
# see top-level Makefile.in
|
||||
simple
|
||||
std_string
|
||||
#std_string
|
||||
std_vector
|
||||
stl
|
||||
argout_ref
|
||||
|
|
@ -8,3 +8,4 @@ shapes
|
|||
contract
|
||||
scoped_enum
|
||||
string_from_ptr
|
||||
strings_test
|
||||
|
|
|
|||
|
|
@ -4,8 +4,7 @@ SWIG_LIB_DIR = $(TOP)/../$(TOP_BUILDDIR_TO_TOP_SRCDIR)Lib
|
|||
SRCS =
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
MLFILE = example.ml
|
||||
PROGFILE = example_prog.ml
|
||||
PROGFILE = runme.ml
|
||||
OBJS =
|
||||
|
||||
check: build
|
||||
|
|
@ -16,23 +15,23 @@ build: static
|
|||
dynamic:
|
||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
|
||||
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
|
||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MLFILE='$(MLFILE)' \
|
||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
|
||||
PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
|
||||
ocaml_dynamic
|
||||
|
||||
static:
|
||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
|
||||
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
|
||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MLFILE='$(MLFILE)' \
|
||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
|
||||
PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
|
||||
ocaml_static
|
||||
|
||||
toplevel:
|
||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
|
||||
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
|
||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MLFILE='$(MLFILE)' \
|
||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
|
||||
PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
|
||||
ocaml_static_toplevel
|
||||
|
||||
clean:
|
||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' MLFILE='$(MLFILE)' ocaml_clean
|
||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' TARGET='$(TARGET)' ocaml_clean
|
||||
|
|
|
|||
12
Examples/ocaml/contract/runme.ml
Normal file
12
Examples/ocaml/contract/runme.ml
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
open Swig
|
||||
open Example
|
||||
|
||||
let _ = print_endline "This won't throw."
|
||||
let _ = Printf.printf "Cos 1.0 is %f\n" (_cos '(1.0) as float)
|
||||
let _ = print_endline "This will throw."
|
||||
let _ =
|
||||
try
|
||||
Printf.printf "Cos 5.0 is %f\n" (_cos '(5.0) as float)
|
||||
with Failure s ->
|
||||
print_endline s ;
|
||||
print_endline "Exception thrown."
|
||||
|
|
@ -4,8 +4,7 @@ SWIG_LIB_DIR = $(TOP)/../$(TOP_BUILDDIR_TO_TOP_SRCDIR)Lib
|
|||
SRCS =
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
MLFILE = example.ml
|
||||
PROGFILE = example_prog.ml
|
||||
PROGFILE = runme.ml
|
||||
OBJS =
|
||||
|
||||
check: build
|
||||
|
|
@ -16,23 +15,23 @@ build: static
|
|||
dynamic:
|
||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
|
||||
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
|
||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MLFILE='$(MLFILE)' \
|
||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
|
||||
PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
|
||||
ocaml_dynamic_cpp
|
||||
|
||||
static:
|
||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
|
||||
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
|
||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MLFILE='$(MLFILE)' \
|
||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
|
||||
PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
|
||||
ocaml_static_cpp
|
||||
|
||||
toplevel:
|
||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
|
||||
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
|
||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MLFILE='$(MLFILE)' \
|
||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
|
||||
PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
|
||||
ocaml_static_cpp_toplevel
|
||||
|
||||
clean:
|
||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' MLFILE='$(MLFILE)' ocaml_clean
|
||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' TARGET='$(TARGET)' ocaml_clean
|
||||
|
|
|
|||
|
|
@ -1,4 +0,0 @@
|
|||
open Swig
|
||||
open Example
|
||||
|
||||
let _ = _f (match Sys.argv.(1) with "Tag1" -> ``Tag1 | _ -> ``Tag2)
|
||||
5
Examples/ocaml/scoped_enum/runme.ml
Normal file
5
Examples/ocaml/scoped_enum/runme.ml
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
open Swig
|
||||
open Example
|
||||
|
||||
let arg = if Array.length Sys.argv > 1 then Sys.argv.(1) else "Tag1"
|
||||
let _ = _f (match arg with "Tag1" -> ``Tag1 | _ -> ``Tag2)
|
||||
|
|
@ -5,8 +5,7 @@ SWIGOPT =
|
|||
SRCS = example.c
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
MLFILE = example.ml
|
||||
PROGFILE = example_prog.ml
|
||||
PROGFILE = runme.ml
|
||||
OBJS = example.o
|
||||
|
||||
check: build
|
||||
|
|
@ -18,22 +17,22 @@ static:
|
|||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
|
||||
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
|
||||
MLFILE='$(MLFILE)' PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
|
||||
PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
|
||||
ocaml_static_cpp
|
||||
|
||||
static_top:
|
||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
|
||||
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
|
||||
MLFILE='$(MLFILE)' PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
|
||||
PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
|
||||
ocaml_static_cpp_toplevel
|
||||
|
||||
dynamic:
|
||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
|
||||
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)'
|
||||
MLFILE='$(MLFILE)' PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
|
||||
PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
|
||||
ocaml_dynamic_cpp
|
||||
|
||||
clean:
|
||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' MLFILE='$(MLFILE)' ocaml_clean
|
||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' TARGET='$(TARGET)' ocaml_clean
|
||||
|
|
|
|||
76
Examples/ocaml/shapes/runme.ml
Normal file
76
Examples/ocaml/shapes/runme.ml
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
(* runme.ml *)
|
||||
|
||||
open Swig ;;
|
||||
open Example ;;
|
||||
|
||||
let side_length (ax,ay) (bx,by) =
|
||||
sqrt (((bx -. ax) ** 2.0) +. ((by -. ay) ** 2.0)) ;;
|
||||
|
||||
let triangle_area a_pt b_pt c_pt =
|
||||
let a = (side_length a_pt b_pt)
|
||||
and b = (side_length b_pt c_pt)
|
||||
and c = (side_length c_pt a_pt) in
|
||||
let s = (a +. b +. c) /. 2.0 in
|
||||
sqrt (s *. (s -. a) *. (s -. b) *. (s -. c)) ;;
|
||||
|
||||
let point_in_triangle (pta,ptb,ptc) x y =
|
||||
let delta = 0.0000001 in (* Error *)
|
||||
let ptx = (x,y) in
|
||||
begin
|
||||
let a_area = triangle_area pta ptb ptx
|
||||
and b_area = triangle_area ptb ptc ptx
|
||||
and c_area = triangle_area ptc pta ptx
|
||||
and x_area = triangle_area pta ptb ptc in
|
||||
let result = (abs_float (a_area +. b_area +. c_area -. x_area)) < delta
|
||||
in
|
||||
result
|
||||
end ;;
|
||||
|
||||
let triangle_class pts ob meth args =
|
||||
match meth with
|
||||
"cover" ->
|
||||
(match args with
|
||||
C_list [ x_arg ; y_arg ] ->
|
||||
let xa = x_arg as float
|
||||
and ya = y_arg as float in
|
||||
(point_in_triangle pts xa ya) to bool
|
||||
| _ -> raise (Failure "cover needs two double arguments."))
|
||||
| _ -> (invoke ob) meth args ;;
|
||||
|
||||
let dist (ax,ay) (bx,by) =
|
||||
let dx = ax -. bx and dy = ay -. by in
|
||||
sqrt ((dx *. dx) +. (dy *. dy))
|
||||
|
||||
let waveplot_depth events distance pt =
|
||||
(List.fold_left (+.) 0.0
|
||||
(List.map
|
||||
(fun (x,y,d) ->
|
||||
let t = dist pt (x,y) in
|
||||
((sin t) /. t) *. d)
|
||||
events)) +. distance
|
||||
|
||||
let waveplot_class events distance ob meth args =
|
||||
match meth with
|
||||
"depth" ->
|
||||
(match args with
|
||||
C_list [ x_arg ; y_arg ] ->
|
||||
let xa = x_arg as float
|
||||
and ya = y_arg as float in
|
||||
(waveplot_depth events distance (xa,ya)) to float
|
||||
| _ -> raise (Failure "cover needs two double arguments."))
|
||||
| _ -> (invoke ob) meth args ;;
|
||||
|
||||
let triangle =
|
||||
new_derived_object
|
||||
new_shape
|
||||
(triangle_class ((0.0,0.0),(0.5,1.0),(1.0,0.6)))
|
||||
'() ;;
|
||||
|
||||
let waveplot =
|
||||
new_derived_object
|
||||
new_volume
|
||||
(waveplot_class [ 0.01,0.01,3.0 ; 1.01,-2.01,1.5 ] 5.0)
|
||||
'() ;;
|
||||
|
||||
let _ = _draw_shape_coverage '(triangle, 60, 20) ;;
|
||||
let _ = _draw_depth_map '(waveplot, 60, 20) ;;
|
||||
|
|
@ -4,8 +4,7 @@ SWIG_LIB_DIR = $(TOP)/../$(TOP_BUILDDIR_TO_TOP_SRCDIR)Lib
|
|||
SRCS = example.c
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
MLFILE = example.ml
|
||||
PROGFILE = example_prog.ml
|
||||
PROGFILE = runme.ml
|
||||
OBJS = example.o
|
||||
|
||||
check: build
|
||||
|
|
@ -16,23 +15,23 @@ build: static
|
|||
dynamic:
|
||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
|
||||
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
|
||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MLFILE='$(MLFILE)' \
|
||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
|
||||
PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
|
||||
ocaml_dynamic
|
||||
|
||||
static:
|
||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
|
||||
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
|
||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MLFILE='$(MLFILE)' \
|
||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
|
||||
PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
|
||||
ocaml_static
|
||||
|
||||
toplevel:
|
||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
|
||||
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
|
||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MLFILE='$(MLFILE)' \
|
||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
|
||||
PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
|
||||
ocaml_static_toplevel
|
||||
|
||||
clean:
|
||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' MLFILE='$(MLFILE)' ocaml_clean
|
||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' TARGET='$(TARGET)' ocaml_clean
|
||||
|
|
|
|||
33
Examples/ocaml/simple/runme.ml
Normal file
33
Examples/ocaml/simple/runme.ml
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
(* runme.ml *)
|
||||
|
||||
open Swig
|
||||
open Example
|
||||
|
||||
(* Call our gcd() function *)
|
||||
|
||||
exception NoReturn
|
||||
|
||||
let x = 42 to int
|
||||
let y = 105 to int
|
||||
let g = _gcd '(x,y) as int
|
||||
let _ = Printf.printf "The gcd of %d and %d is %d\n" (x as int) (y as int) g
|
||||
|
||||
(* Manipulate the Foo global variable *)
|
||||
|
||||
(* Output its current value *)
|
||||
let _ = Printf.printf "Foo = %f\n" (_Foo '() as float)
|
||||
|
||||
(* Change its value *)
|
||||
let _ = _Foo '(3.1415926)
|
||||
|
||||
(* See if the change took effect *)
|
||||
let _ = Printf.printf "Foo = %f\n" (_Foo '() as float)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -14,7 +14,7 @@ let print_DoubleVector v =
|
|||
(fun v i ->
|
||||
print_float ((v '[i to int]) as float) ;
|
||||
print_string " ") ;
|
||||
print_endline
|
||||
print_endline ""
|
||||
end
|
||||
|
||||
(* Call average with a Ocaml array... *)
|
||||
|
|
@ -27,7 +27,7 @@ let rec fill_dv v x =
|
|||
fill_dv v (x *. x)
|
||||
end
|
||||
let _ = fill_dv v 0.999
|
||||
let _ = print_DoubleVector v ; print_endline ""
|
||||
let _ = print_DoubleVector v
|
||||
let u = new_IntVector '()
|
||||
let _ = for i = 1 to 4 do
|
||||
u -> push_back ((i to int))
|
||||
|
|
|
|||
|
|
@ -4,9 +4,8 @@ SWIG_LIB_DIR = $(TOP)/../$(TOP_BUILDDIR_TO_TOP_SRCDIR)Lib
|
|||
SWIGOPT = -c++
|
||||
SRCS =
|
||||
TARGET = example
|
||||
INTERFACE = foolib.i
|
||||
MLFILE = foolib.ml
|
||||
PROGFILE = example_prog.ml
|
||||
INTERFACE = example.i
|
||||
PROGFILE = runme.ml
|
||||
OBJS =
|
||||
|
||||
check: build
|
||||
|
|
@ -18,22 +17,22 @@ static:
|
|||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
|
||||
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
|
||||
MLFILE='$(MLFILE)' PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
|
||||
PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
|
||||
ocaml_static_cpp
|
||||
|
||||
static_top:
|
||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
|
||||
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
|
||||
MLFILE='$(MLFILE)' PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
|
||||
PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
|
||||
ocaml_static_cpp_toplevel
|
||||
|
||||
dynamic:
|
||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
|
||||
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)'
|
||||
MLFILE='$(MLFILE)' PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
|
||||
PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
|
||||
ocaml_dynamic_cpp
|
||||
|
||||
clean:
|
||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' MLFILE='$(MLFILE)' ocaml_clean
|
||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' TARGET='$(TARGET)' ocaml_clean
|
||||
|
|
|
|||
16
Examples/ocaml/string_from_ptr/example.i
Normal file
16
Examples/ocaml/string_from_ptr/example.i
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
%module example
|
||||
%{
|
||||
static int foo( char **buf ) {
|
||||
*buf = "string from c";
|
||||
return 0;
|
||||
}
|
||||
%}
|
||||
|
||||
%typemap(in,numinputs=0) char **buf (char *temp) {
|
||||
$1 = &temp;
|
||||
}
|
||||
%typemap(argout) char **buf {
|
||||
swig_result = caml_list_append(swig_result,caml_val_string((char *)*$1));
|
||||
}
|
||||
|
||||
int foo( char **buf );
|
||||
21
Examples/ocaml/string_from_ptr/runme.ml
Normal file
21
Examples/ocaml/string_from_ptr/runme.ml
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
open Swig (* Give access to the swig library *)
|
||||
open Example (* This is the name of your swig output *)
|
||||
|
||||
let results = _foo '() (* Function names are prefixed with _ in order to make
|
||||
them lex as identifiers in ocaml. Consider that
|
||||
uppercase identifiers are module names in ocaml.
|
||||
NOTE: the '() syntax is part of swigp4. You can do:
|
||||
let results = _foo C_void *)
|
||||
|
||||
(* Since your function has a return value in addition to the string output,
|
||||
you'll need to match them as a list *)
|
||||
|
||||
let result_string =
|
||||
match results with
|
||||
C_list [ C_string result_string ; C_int 0 ] -> (* The return value is
|
||||
last when out arguments appear, but this too can be customized.
|
||||
We're also checking that the function succeeded. *)
|
||||
result_string
|
||||
| _ -> raise (Failure "Expected string, int reply from _foo")
|
||||
|
||||
let _ = print_endline result_string
|
||||
|
|
@ -11,4 +11,5 @@ using std::string;
|
|||
#include "example.h"
|
||||
%}
|
||||
|
||||
%include "std_string.i"
|
||||
%include example.h
|
||||
|
|
|
|||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue