Skip to content

Commit c1b11bc

Browse files
KuarniKakadu
authored andcommitted
feat(everything-at-once): github force me to do it
1 parent 00828d7 commit c1b11bc

File tree

109 files changed

+9408
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+9408
-0
lines changed

BDSML/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
_build
2+
.idea

BDSML/.ocamlformat

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
version=0.26.2
2+
profile=janestreet

BDSML/BDSML.opam

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# This file is generated by dune, edit dune-project instead
2+
opam-version: "2.0"
3+
synopsis: "Based Development Sadistic Meta Language"
4+
description: "A compiler for BDSML"
5+
maintainer: ["Arseniy Kuznetsov" "Leonid Elkin"]
6+
authors: ["Arseniy Kuznetsov" "Leonid Elkin"]
7+
license: "LGPL-2.1"
8+
homepage: "https://github.com/Kuarni/BDSML"
9+
bug-reports: "https://github.com/Kuarni/BDSML/issues"
10+
depends: [
11+
"ocaml"
12+
"dune" {>= "3.16"}
13+
"ppx_deriving"
14+
"ppx_inline_test" {with-test}
15+
"odoc" {with-doc}
16+
"ocamlformat" {build}
17+
"qcheck"
18+
]
19+
build: [
20+
["dune" "subst"] {dev}
21+
[
22+
"dune"
23+
"build"
24+
"-p"
25+
name
26+
"-j"
27+
jobs
28+
"@install"
29+
"@runtest" {with-test}
30+
"@doc" {with-doc}
31+
]
32+
]
33+
dev-repo: "git+https://github.com/Kuarni/BDSML.git"

BDSML/bin/compiler.ml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
open Parser
2+
open Typing
3+
open Middleend
4+
open My_llvm
5+
6+
let () =
7+
let str = Stdio.In_channel.input_all Stdlib.stdin in
8+
match Main_parser.parse str with
9+
| Result.Error err -> Format.printf "Parsing error%s\n" err
10+
| Result.Ok ast ->
11+
(match Inference.infer_program ast with
12+
| Result.Error e -> Format.printf "Type inference error: %s\n" e
13+
| Result.Ok _ ->
14+
let ( >>= ) = Result.bind in
15+
let res =
16+
Pattern_remover.remove_patterns ast
17+
>>= Alpha_conversion.alpha_conversion
18+
|> Result.map Closure_conversion.closure_convert
19+
>>= Alpha_conversion.alpha_conversion
20+
>>= Lambda_lifting.ll
21+
|> Result.map_error Middleend_utils.exp_to_string
22+
in
23+
(match res with
24+
| Result.Error e -> Format.printf "Middleend error: %s\n" e
25+
| Result.Ok res ->
26+
(match Anf.rast_to_anf res with
27+
| Result.Error _ -> Format.printf "Converting to anf error\n"
28+
| Result.Ok res -> Codegen.compile_program res)))
29+
;;

BDSML/bin/compiler_tests.t

Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
1+
2+
$ clang-16 -std=c++20 -c runtime/runtime.cpp -o runtime.o
3+
$ for f in manytests/typed/*.ml; do
4+
> echo "\n----------------------------------- $f"
5+
> ./compiler.exe < $f
6+
> clang-16 -lstdc++ -std=c++20 -lffi out.ll runtime.o -o a.out
7+
> cat $f
8+
> echo "\nAnswer:"
9+
> ./a.out
10+
> rm a.out
11+
> done
12+
13+
----------------------------------- manytests/typed/001fac.ml
14+
let rec fac n = if n <= 1 then 1 else n * fac (n - 1)
15+
16+
let main =
17+
let () = print_int (fac 4) in
18+
0
19+
;;
20+
21+
Answer:
22+
24
23+
24+
----------------------------------- manytests/typed/002fac.ml
25+
let rec fac_cps n k =
26+
if n=1 then k 1 else
27+
fac_cps (n-1) (fun p -> k (p*n))
28+
29+
let main =
30+
let () = print_int (fac_cps 4 (fun print_int -> print_int)) in
31+
0
32+
33+
34+
Answer:
35+
24
36+
37+
----------------------------------- manytests/typed/003fib.ml
38+
let rec fib_acc a b n =
39+
if n=1 then b
40+
else
41+
let n1 = n-1 in
42+
let ab = a+b in
43+
fib_acc b ab n1
44+
45+
let rec fib n =
46+
if n<2
47+
then n
48+
else fib (n - 1) + fib (n - 2)
49+
50+
let main =
51+
let () = print_int (fib_acc 0 1 4) in
52+
let () = print_int (fib 4) in
53+
0
54+
55+
56+
Answer:
57+
3
58+
3
59+
60+
----------------------------------- manytests/typed/004manyargs.ml
61+
let wrap f = if 1 = 1 then f else f
62+
63+
let test3 a b c =
64+
let a = print_int a in
65+
let b = print_int b in
66+
let c = print_int c in
67+
0
68+
69+
let test10 a b c d e f g h i j = a + b + c + d + e + f + g + h + i + j
70+
71+
let main =
72+
let rez =
73+
(wrap test10 1 10 100 1000 10000 100000 1000000 10000000 100000000
74+
1000000000)
75+
in
76+
let () = print_int rez in
77+
let temp2 = wrap test3 1 10 100 in
78+
0
79+
80+
81+
Answer:
82+
1111111111
83+
1
84+
10
85+
100
86+
87+
----------------------------------- manytests/typed/005fix.ml
88+
let rec fix f x = f (fix f) x
89+
90+
let fac self n = if n<=1 then 1 else n * self (n-1)
91+
92+
let main =
93+
let () = print_int (fix fac 6) in
94+
0
95+
96+
97+
Answer:
98+
720
99+
100+
----------------------------------- manytests/typed/006partial.ml
101+
let foo b = if b then (fun foo -> foo+2) else (fun foo -> foo*10)
102+
103+
let foo x = foo true (foo false (foo true (foo false x)))
104+
let main =
105+
let () = print_int (foo 11) in
106+
0
107+
Answer:
108+
1122
109+
110+
----------------------------------- manytests/typed/006partial2.ml
111+
let foo a b c =
112+
let () = print_int a in
113+
let () = print_int b in
114+
let () = print_int c in
115+
a + b * c
116+
117+
let main =
118+
let foo = foo 1 in
119+
let foo = foo 2 in
120+
let foo = foo 3 in
121+
let () = print_int foo in
122+
0
123+
Answer:
124+
1
125+
2
126+
3
127+
7
128+
129+
----------------------------------- manytests/typed/006partial3.ml
130+
let foo a =
131+
let () = print_int a in fun b ->
132+
let () = print_int b in fun c ->
133+
print_int c
134+
135+
let main =
136+
let () = foo 4 8 9 in
137+
0
138+
Answer:
139+
4
140+
8
141+
9
142+
143+
----------------------------------- manytests/typed/007order.ml
144+
let _start () () a () b _c () d __ =
145+
let () = print_int (a+b) in
146+
let () = print_int __ in
147+
a*b / _c + d
148+
149+
150+
let main =
151+
print_int (_start (print_int 1) (print_int 2) 3 (print_int 4) 100 1000 (print_int (-1)) 10000 (-555555))
152+
Answer:
153+
1
154+
2
155+
4
156+
-1
157+
103
158+
-555555
159+
10000
160+
161+
----------------------------------- manytests/typed/008ascription.ml
162+
let addi = fun f g x -> (f x (g x: bool) : int)
163+
164+
let main =
165+
let () = print_int (addi (fun x b -> if b then x+1 else x*2) (fun _start -> _start/2 = 0) 4) in
166+
0
167+
Answer:
168+
8
169+
170+
----------------------------------- manytests/typed/009let_poly.ml
171+
let temp =
172+
let f = fun x -> x in
173+
(f 1, f true)
174+
Answer:
175+
176+
----------------------------------- manytests/typed/015tuples.ml
177+
let rec fix f x = f (fix f) x
178+
let map f p = let (a,b) = p in (f a, f b)
179+
let fixpoly l =
180+
fix (fun self l -> map (fun li x -> li (self l) x) l) l
181+
let feven p n =
182+
let (e, o) = p in
183+
if n = 0 then 1 else o (n - 1)
184+
let fodd p n =
185+
let (e, o) = p in
186+
if n = 0 then 0 else e (n - 1)
187+
let tie = fixpoly (feven, fodd)
188+
189+
let rec meven n = if n = 0 then 1 else modd (n - 1)
190+
and modd n = if n = 0 then 1 else meven (n - 1)
191+
let main =
192+
let () = print_int (modd 1) in
193+
let () = print_int (meven 2) in
194+
let (even,odd) = tie in
195+
let () = print_int (odd 3) in
196+
let () = print_int (even 4) in
197+
0
198+
199+
200+
Answer:
201+
1
202+
1
203+
1
204+
1
205+
206+
----------------------------------- manytests/typed/016lists.ml
207+
let rec length xs =
208+
match xs with
209+
| [] -> 0
210+
| h::tl -> 1 + length tl
211+
212+
let length_tail =
213+
let rec helper acc xs =
214+
match xs with
215+
| [] -> acc
216+
| h::tl -> helper (acc + 1) tl
217+
in
218+
helper 0
219+
220+
let rec map f xs =
221+
match xs with
222+
| [] -> []
223+
| a::[] -> [f a]
224+
| a::b::[] -> [f a; f b]
225+
| a::b::c::[] -> [f a; f b; f c]
226+
| a::b::c::d::tl -> f a :: f b :: f c :: f d :: map f tl
227+
228+
let rec append xs ys = match xs with [] -> ys | x::xs -> x::(append xs ys)
229+
230+
let concat =
231+
let rec helper xs =
232+
match xs with
233+
| [] -> []
234+
| h::tl -> append h (helper tl)
235+
in helper
236+
237+
let rec iter f xs = match xs with [] -> () | h::tl -> let () = f h in iter f tl
238+
239+
let rec cartesian xs ys =
240+
match xs with
241+
| [] -> []
242+
| h::tl -> append (map (fun a -> (h,a)) ys) (cartesian tl ys)
243+
244+
let main =
245+
let () = iter print_int [1;2;3] in
246+
let () = print_int (length (cartesian [1;2] [1;2;3;4])) in
247+
0
248+
249+
Answer:
250+
1
251+
2
252+
3
253+
8

BDSML/bin/dune

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
(executable
2+
(name compiler)
3+
(modules Compiler)
4+
(public_name compiler)
5+
(libraries BDSML.Parser BDSML.middleend BDSML.Typing BDSML.my_llvm stdio))
6+
7+
(cram
8+
(applies_to compiler_tests)
9+
(deps
10+
./compiler.exe
11+
runtime/runtime.cpp
12+
manytests/typed/001fac.ml
13+
manytests/typed/002fac.ml
14+
manytests/typed/003fib.ml
15+
manytests/typed/004manyargs.ml
16+
manytests/typed/005fix.ml
17+
manytests/typed/006partial.ml
18+
manytests/typed/006partial2.ml
19+
manytests/typed/006partial3.ml
20+
manytests/typed/007order.ml
21+
manytests/typed/008ascription.ml
22+
manytests/typed/009let_poly.ml
23+
manytests/typed/015tuples.ml
24+
manytests/typed/016lists.ml))

BDSML/bin/manytests

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../manytests

BDSML/bin/runtime

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../lib/runtime

BDSML/dune

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
(env
2+
(dev
3+
(flags
4+
(:standard -warn-error -A -w -3-9-32-34-58)))
5+
(release
6+
(flags
7+
(:standard -warn-error -A -w -58))))

BDSML/dune-project

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
(lang dune 3.16)
2+
3+
(name BDSML)
4+
5+
(generate_opam_files true)
6+
7+
(source
8+
(github Kuarni/BDSML))
9+
10+
(authors "Arseniy Kuznetsov" "Leonid Elkin")
11+
12+
(maintainers "Arseniy Kuznetsov" "Leonid Elkin")
13+
14+
(license LGPL-2.1)
15+
16+
(package
17+
(name BDSML)
18+
(synopsis "Based Development Sadistic Meta Language")
19+
(description "A compiler for BDSML")
20+
(depends
21+
ocaml
22+
dune
23+
ppx_deriving
24+
(ppx_inline_test :with-test)
25+
(odoc :with-doc)
26+
(ocamlformat :build)
27+
qcheck))

0 commit comments

Comments
 (0)