Skip to content

Commit

Permalink
Make it possible to read multiple program files
Browse files Browse the repository at this point in the history
  • Loading branch information
ushitora-anqou committed Jan 29, 2019
1 parent fdb5c7a commit f4f1bf8
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 127 deletions.
24 changes: 12 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
aqaml: main.ml
SRC=hashmap.ml hashtbl.ml main.ml

aqaml: $(SRC)
ocamlopt $^ -o $@

test: aqaml test.sh utility.o _test.ml
test: aqaml test.sh utility.o
./test.sh

utility.o: utility.c
gcc -Wall -std=c11 -c -o $@ $^

_test.ml: stdlib.ml test.ml
cat stdlib.ml test.ml > $@

_self_aqaml: stdlib.ml main.ml utility.o aqaml
cat stdlib.ml main.ml | ./aqaml > _self_aqaml.s
_self_aqaml: stdlib.ml $(SRC) utility.o aqaml
./aqaml stdlib.ml $(SRC) > _self_aqaml.s
gcc _self_aqaml.s utility.o -o $@
strip $@

_self_test.sh: test.sh
cat test.sh | sed "s#./aqaml#./_self_aqaml#g" > _self_test.sh
chmod +x _self_test.sh

self_test: _self_aqaml _test.ml _self_test.sh utility.o
self_test: _self_aqaml _self_test.sh utility.o
./_self_test.sh

_selfself_aqaml: stdlib.ml main.ml utility.o _self_aqaml
cat stdlib.ml main.ml | ./_self_aqaml > _selfself_aqaml.s
_selfself_aqaml: stdlib.ml $(SRC) utility.o _self_aqaml
./_self_aqaml stdlib.ml $(SRC) > _selfself_aqaml.s
gcc _selfself_aqaml.s utility.o -o $@
strip $@

_selfself_test.sh: test.sh
cat test.sh | sed "s#./aqaml#./_selfself_aqaml#g" > _selfself_test.sh
chmod +x _selfself_test.sh

selfself_test: _selfself_aqaml _test.ml _selfself_test.sh utility.o
selfself_test: _selfself_aqaml _selfself_test.sh utility.o
./_selfself_test.sh
cmp _self_aqaml _selfself_aqaml

clean:
rm -f _test.ml _self_test.sh _self_aqaml _self_aqaml.s _test.o _test.s aqaml utility.o _selfself_aqaml _selfself_aqaml.s _selfself_test.sh
rm -f _self_test.sh _self_aqaml _self_aqaml.s _test.o _test.s aqaml utility.o _selfself_aqaml _selfself_aqaml.s _selfself_test.sh
rm -f $(SRC:.ml=.cmi) $(SRC:.ml=.cmx) $(SRC:.ml=.o) $(SRC:.ml=.cmo)

.PHONY: test self_test clean selfself_test
48 changes: 48 additions & 0 deletions hashmap.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
type ('a, 'b) t = ('a * 'b) list

let empty = []

let add k v m = (k, v) :: m

let rec find k = function
| (k', v') :: xs -> if k = k' then v' else find k xs
| [] -> raise Not_found

let mem k m =
try
ignore (find k m) ;
true
with Not_found -> false

let merge f m1 m2 =
let src = ref empty in
let rec iter_m1 = function
| (k, v) :: xs ->
( try src := add k (Some v, Some (find k m2)) !src with Not_found ->
src := add k (Some v, None) !src ) ;
iter_m1 xs
| [] -> ()
in
let rec iter_m2 = function
| (k, v) :: xs ->
if not (mem k m1) then src := add k (None, Some v) !src ;
iter_m2 xs
| [] -> ()
in
iter_m1 m1 ;
iter_m2 m2 ;
List.fold_left
(fun m (k, (l, r)) -> match f k l r with None -> m | Some v -> add k v m)
empty !src

let union f m1 m2 =
merge
(fun k l r ->
match (l, r) with
| None, None -> None
| Some v, None -> l
| None, Some v -> r
| Some v1, Some v2 -> f k v1 v2 )
m1 m2

let cardinal m = List.length m
11 changes: 11 additions & 0 deletions hashtbl.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
type ('a, 'b) t = ('a, 'b) Hashmap.t ref

let create size_hint = ref Hashmap.empty

let add tbl k v = tbl := Hashmap.add k v !tbl

let mem tbl k = Hashmap.mem k !tbl

let find tbl k = Hashmap.find k !tbl

let length tbl = Hashmap.cardinal !tbl
Loading

0 comments on commit f4f1bf8

Please sign in to comment.