cleaner build system and string module

This commit is contained in:
2022-01-05 17:51:48 +11:00
committed by aaron-jack-manning
parent de837bbf3b
commit 0fb7333add
10 changed files with 169 additions and 51 deletions

View File

@ -1,4 +1,4 @@
open FromStdlib
open FromStdlib open Exposed
let ( + ) a b = stdlib_plus_float a b
let ( - ) a b = stdlib_minus_float a b

View File

@ -16,6 +16,22 @@ external stdlib_minus_float : float -> float -> float = "%subfloat"
external stdlib_multiply_float : float -> float -> float = "%mulfloat"
external stdlib_divide_float : float -> float -> float = "%divfloat"
external stdlib_string_length : string -> int = "%string_length"
external bytes_create : int -> bytes = "caml_create_bytes"
external string_blit : string -> int -> bytes -> int -> int -> unit
= "caml_blit_string" [@@noalloc]
external bytes_blit : bytes -> int -> bytes -> int -> int -> unit
= "caml_blit_bytes" [@@noalloc]
external bytes_unsafe_to_string : bytes -> string = "%bytes_to_string"
let stdlib_string_concat s1 s2 =
let l1 = stdlib_string_length s1 and l2 = stdlib_string_length s2 in
let s = bytes_create (l1 + l2) in
string_blit s1 0 s 0 l1;
string_blit s2 0 s l1 l2;
bytes_unsafe_to_string s
external ( = ) : 'a -> 'a -> bool = "%equal"
external ( <> ) : 'a -> 'a -> bool = "%notequal"

23
lib/fromStdlib.mli Normal file
View File

@ -0,0 +1,23 @@
val failwith : string -> 'a
val printf : ('a, out_channel, unit) format -> 'a
external ( |> ) : 'a -> ('a -> 'b) -> 'b = "%revapply"
external stdlib_plus_int : int -> int -> int = "%addint"
external stdlib_minus_int : int -> int -> int = "%subint"
external stdlib_multiply_int : int -> int -> int = "%mulint"
external stdlib_divide_int : int -> int -> int = "%divint"
external stdlib_mod_int : int -> int -> int = "%modint"
external stdlib_plus_float : float -> float -> float = "%addfloat"
external stdlib_minus_float : float -> float -> float = "%subfloat"
external stdlib_multiply_float : float -> float -> float = "%mulfloat"
external stdlib_divide_float : float -> float -> float = "%divfloat"
external stdlib_string_length : string -> int = "%string_length"
val stdlib_string_concat : string -> string -> string
external ( = ) : 'a -> 'a -> bool = "%equal"
external ( <> ) : 'a -> 'a -> bool = "%notequal"
external ( < ) : 'a -> 'a -> bool = "%lessthan"
external ( > ) : 'a -> 'a -> bool = "%greaterthan"
external ( <= ) : 'a -> 'a -> bool = "%lessequal"
external ( >= ) : 'a -> 'a -> bool = "%greaterequal"
external not : bool -> bool = "%boolnot"
external ( or ) : bool -> bool -> bool = "%sequor"
external ( & ) : bool -> bool -> bool = "%sequand"

View File

@ -1,3 +0,0 @@
open FromStdlib open Exposed
let _ = printf "Hello, World\n"

View File

@ -1,45 +1,26 @@
# build recompiles and links all files (no incemental builds)
build:
STANDARD_FLAGS = -S -O3
STANDARD_COMPILE = ocamlopt $(STANDARD_FLAGS) -nopervasives -c
# compiles the entire custom standard library
compile:
# fromStdlib manages things that need to be exposed from the standard library
ocamlopt -i fromStdlib.ml > fromStdlib.mli
ocamlopt -S -O3 -c fromStdlib.mli fromStdlib.ml
ocamlopt $(STANDARD_FLAGS) -c fromStdlib.mli fromStdlib.ml
# exposed types and functions, that can be opened module wide
ocamlopt -S -O3 -nopervasives -c exposed.mli exposed.ml
ocamlopt $(STANDARD_FLAGS) -nopervasives -c exposed.mli exposed.ml
# the following files make up the core custom standard library code
ocamlopt -S -O3 -nopervasives -c int.mli int.ml
ocamlopt -S -O3 -nopervasives -c float.mli float.ml
ocamlopt -S -O3 -nopervasives -c option.mli option.ml
ocamlopt -S -O3 -nopervasives -c stack.mli stack.ml
ocamlopt -S -O3 -nopervasives -c list.mli list.ml
ocamlopt -S -O3 -nopervasives -c map.mli map.ml
ocamlopt -S -O3 -nopervasives -c queue.mli queue.ml
ocamlopt -S -O3 -nopervasives -c set.mli set.ml
ocamlopt -S -O3 -nopervasives -c tree.mli tree.ml
# main is the main file to run code in
ocamlopt -i main.ml > main.mli
ocamlopt -S -O3 -nopervasives -c main.mli main.ml
# after all files are individually compiled with -nopervasives, this is compiled with it so that fromStdlib has the necessary linking
ocamlopt -S -O3 fromStdlib.cmx exposed.cmx int.cmx float.cmx option.cmx stack.cmx list.cmx map.cmx queue.cmx set.cmx tree.cmx main.cmx -o program
$(STANDARD_COMPILE) int.mli int.ml
$(STANDARD_COMPILE) float.mli float.ml
$(STANDARD_COMPILE) option.mli option.ml
$(STANDARD_COMPILE) stack.mli stack.ml
$(STANDARD_COMPILE) list.mli list.ml
$(STANDARD_COMPILE) map.mli map.ml
$(STANDARD_COMPILE) queue.mli queue.ml
$(STANDARD_COMPILE) set.mli set.ml
$(STANDARD_COMPILE) tree.mli tree.ml
$(STANDARD_COMPILE) string.mli string.ml
# clean removes all except source files. autogenerated mli files are also removed.
clean:
rm -f *.o *.a *.s *.cmi *.cmx *.cmxa *.cmo *.cma fromStdlib.mli main.mli program
# mostlyclean removes all except source files and the final executable. autogenerated mli files are also removed.
mostlyclean:
rm -f *.o *.a *.s *.cmi *.cmx *.cmxa *.cmo *.cma fromStdlib.mli main.mli
# install builds, mostlycleans and runs
install:
make build
make mostlyclean
./program
# just runs the executable if it has been produced by make build
make run:
./program
rm -f *.o *.a *.s *.cmi *.cmx *.cmxa *.cmo *.cma

View File

@ -1,5 +1,4 @@
open FromStdlib
open Exposed
open FromStdlib open Exposed
let pop (st : 'a stack) : 'a option * 'a stack =
match st with

5
lib/string.ml Normal file
View File

@ -0,0 +1,5 @@
open FromStdlib open Exposed
let ( + ) = stdlib_string_concat
let length = stdlib_string_length

5
lib/string.mli Normal file
View File

@ -0,0 +1,5 @@
(** Concatenates two strings together in the provided order. *)
val ( + ) : string -> string -> string
(** Calculates the length of the provided string. *)
val length : string -> int