Skip to content

Commit

Permalink
reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
jake-87 committed Aug 20, 2023
1 parent b09664c commit f496ef9
Show file tree
Hide file tree
Showing 13 changed files with 192 additions and 63 deletions.
2 changes: 1 addition & 1 deletion .ocamlformat
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
profile = default

if-then-else = vertical
40 changes: 28 additions & 12 deletions lib/backend/emit_c.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ open KhasmUTF

let mangler id =
let asint = String.get_uint8 id 0 in
if asint >= 65 && asint < 65 + 26 then id
else if asint >= 97 && asint < 97 + 26 then id
else if asint >= 48 && asint < 48 + 10 then id
else "_" ^ string_of_int asint ^ "_"
if asint >= 65 && asint < 65 + 26 then
id
else if asint >= 97 && asint < 97 + 26 then
id
else if asint >= 48 && asint < 48 + 10 then
id
else
"_" ^ string_of_int asint ^ "_"

let mangle_top nms id =
let nm = List.assoc id nms in
Expand All @@ -30,7 +34,12 @@ let function_name name argnum =
(List.mapi (fun i x -> x ^ "_" ^ string_of_int i)
@@ n_of_i argnum "kha_obj * a")
in
let args = if args = "" then "void" else args in
let args =
if args = "" then
"void"
else
args
in
"extern kha_obj * " ^ name ^ "(" ^ args ^ ");\n" ^ "KHASM_ENTRY(" ^ name
^ ", " ^ string_of_int argnum ^ ", " ^ args ^ ") {\n"

Expand Down Expand Up @@ -63,7 +72,8 @@ and gen_new s =
n'

let rec emit_tuple tbl x =
if List.length x = 0 then gen_new "make_tuple(0)"
if List.length x = 0 then
gen_new "make_tuple(0)"
else
let codes = List.map (fun x -> codegen_func x tbl) x in
gen_new
Expand All @@ -89,8 +99,10 @@ and add_to_emi s = emission := s :: !emission
and codegen_func code tbl =
match code with
| Val v ->
if is_toplevel v tbl then gen_new (emit_ptr tbl v)
else gen_new (emit_ref (mangle v))
if is_toplevel v tbl then
gen_new (emit_ptr tbl v)
else
gen_new (emit_ref (mangle v))
| Unboxed v -> gen_new (emit_unboxed v)
| Tuple t -> emit_tuple tbl t
| Call (e1, e2) -> emit_call tbl e1 e2
Expand All @@ -105,12 +117,16 @@ and codegen_func code tbl =
let n1 = codegen_func c tbl in
add_to_emi ("if (" ^ n1 ^ "->data.i) {\n");
let t1 = codegen_func e1 tbl in
if t1 = "IFELSETEMP" then ()
else add_to_emi (";\n IFELSETEMP = ref(" ^ t1 ^ ");");
if t1 = "IFELSETEMP" then
()
else
add_to_emi (";\n IFELSETEMP = ref(" ^ t1 ^ ");");
add_to_emi "} else {";
let t2 = codegen_func e2 tbl in
if t2 = "IFELSETEMP" then ()
else add_to_emi (";\n IFELSETEMP = ref(" ^ t2 ^ ");");
if t2 = "IFELSETEMP" then
()
else
add_to_emi (";\n IFELSETEMP = ref(" ^ t2 ^ ");");
add_to_emi "}\n";
"IFELSETEMP"

Expand Down
6 changes: 5 additions & 1 deletion lib/backend/native.ml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ let prelude () =

let flags =
KhasmUTF.utf8_map
(fun x -> if x = "\n" then "" else x)
(fun x ->
if x = "\n" then
""
else
x)
{| -O0
-Wall
-Wextra
Expand Down
32 changes: 26 additions & 6 deletions lib/frontend/ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -86,26 +86,46 @@ and program = Program of toplevel list [@@deriving show { with_path = false }]
(* The following two are basically just α-renaming *)
let base_subs i b x y =
match b with
| Ident (i', b') -> if b' = x then Base (i, Ident (i', y)) else Base (i, b)
| Ident (i', b') ->
if b' = x then
Base (i, Ident (i', y))
else
Base (i, b)
| _ -> Base (i, b)

let rec esubs expr x y =
match expr with
| Base (i, b) -> base_subs i b x y
| FCall (i, f, x') -> FCall (i, esubs f x y, esubs x' x y)
| LetIn (i, id, e1, e2) ->
if id <> x then LetIn (i, id, esubs e1 x y, esubs e2 x y) else expr
if id <> x then
LetIn (i, id, esubs e1 x y, esubs e2 x y)
else
expr
| AnnotLet (i, id, ts, e1, e2) ->
if id <> x then AnnotLet (i, id, ts, esubs e1 x y, esubs e2 x y) else expr
if id <> x then
AnnotLet (i, id, ts, esubs e1 x y, esubs e2 x y)
else
expr
| LetRecIn (i, ts, id, e1, e2) ->
if id <> x then LetRecIn (i, ts, id, esubs e1 x y, esubs e2 x y) else expr
if id <> x then
LetRecIn (i, ts, id, esubs e1 x y, esubs e2 x y)
else
expr
| IfElse (i, c, e1, e2) -> IfElse (i, esubs c x y, esubs e1 x y, esubs e2 x y)
| Join (info, e1, e2) -> Join (info, esubs e1 x y, esubs e2 x y)
| Lam (i, x', e) -> if x' <> x then Lam (i, x', esubs e x y) else expr
| Lam (i, x', e) ->
if x' <> x then
Lam (i, x', esubs e x y)
else
expr
| TypeLam (i, t, e) -> TypeLam (i, t, esubs e x y)
| TupAccess (i, e, i') -> TupAccess (i, esubs e x y, i')
| AnnotLam (i, x', ts, e) ->
if x' <> x then AnnotLam (i, x', ts, esubs e x y) else expr
if x' <> x then
AnnotLam (i, x', ts, esubs e x y)
else
expr
| ModAccess _ -> expr
| Inst _ -> expr

Expand Down
7 changes: 6 additions & 1 deletion lib/frontend/debug.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
let debug_s = ref ""
let debug str = debug_s := !debug_s ^ "\n" ^ str
let log_debug_stdout b = if b then print_endline !debug_s else ()

let log_debug_stdout b =
if b then
print_endline !debug_s
else
()

let log_debug_destruct () =
print_endline !debug_s;
Expand Down
18 changes: 12 additions & 6 deletions lib/frontend/elim_modules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ type module_ctx = {
[@@deriving show { with_path = false }]

let rec open_if_valid id orig ctx =
if ctx.name = id then { ctx with is_open = true }
else open_deep_module_h ctx id orig
if ctx.name = id then
{ ctx with is_open = true }
else
open_deep_module_h ctx id orig

and open_deep_module_h ctx id orig =
{
Expand Down Expand Up @@ -89,7 +91,8 @@ let rec get_parent_list ctx =
| Some x -> get_parent_list !x ^ "." ^ ctx.name

let rec get_mang ctx id =
if id = "main" then "main"
if id = "main" then
"main"
else
match List.assoc_opt id ctx.idents with
| Some true -> id
Expand Down Expand Up @@ -148,8 +151,10 @@ let elim_ts ctx ts = ts
let rec elim_base mctx lctx i k : kexpr =
match k with
| Ident (i', k) ->
if is_local lctx k then Base (i, Ident (i', k))
else Base (i, Ident (i', get_full_id_mod mctx [] k))
if is_local lctx k then
Base (i, Ident (i', k))
else
Base (i, Ident (i', get_full_id_mod mctx [] k))
| Int _ | Float _ | Str _ -> Base (i, k)
| Tuple l -> Base (i, Tuple (List.map (elim_expr mctx lctx) l))
| True | False -> Base (i, k)
Expand Down Expand Up @@ -205,7 +210,8 @@ let rec elim_toplevel ctx t =
let id' = get_mang ctx id_e in
(add_ident ctx id_e, IntExtern (id_i, id', i, ts) :: [])
| Bind (id, ids, nm) ->
if ids <> [] then todo "bind with module args"
if ids <> [] then
todo "bind with module args"
else
let id' = get_mang ctx id in
let nm' = get_mang ctx nm in
Expand Down
27 changes: 20 additions & 7 deletions lib/frontend/parser.ml
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,10 @@ module ParserState = struct

let expect state tok =
let t = pop state in
if t = tok then () else error state t [ tok ]
if t = tok then
()
else
error state t [ tok ]
end

open ParserState
Expand Down Expand Up @@ -343,10 +346,17 @@ and infix_bind_pow tok =
let first = String.get tok 0 in
let mab =
if first = '*' then
try if String.get tok 1 = '*' then (16, 17) else (0, 0) with _ -> (0, 0)
else (0, 0)
try
if String.get tok 1 = '*' then
(16, 17)
else
(0, 0)
with _ -> (0, 0)
else
(0, 0)
in
if mab <> (0, 0) then mab
if mab <> (0, 0) then
mab
else
match first with
| ';' -> (1, 0)
Expand Down Expand Up @@ -506,7 +516,8 @@ and parse_expr_h state res prec =
toss state;
let next_prec = pr in
parse_expr_h state (Join (mkinfo (), res, parse_expr state next_prec)) 0)
else res
else
res
| Some s ->
let pl, pr = infix_bind_pow s in
if pl >= prec then (
Expand All @@ -518,7 +529,8 @@ and parse_expr_h state res prec =
FCall (mkinfo (), Base (mkinfo (), Ident (mkinfo (), s)), res),
parse_expr state next_prec ))
0)
else res
else
res

and parse_expr state prec =
let lhs = parse_compound state in
Expand Down Expand Up @@ -644,4 +656,5 @@ and program token lexbuf file =
if tmp = [] then (
print_endline "EMPTY FILE";
raise ParseError)
else Program tmp
else
Program tmp
Loading

0 comments on commit f496ef9

Please sign in to comment.