Skip to content

Commit

Permalink
fixed conditional parsing and adjusted precedence/associativity of so…
Browse files Browse the repository at this point in the history
…me ops, fixed bug with conditionals
  • Loading branch information
osimon8 committed Mar 18, 2022
1 parent 12ed2a3 commit 79704ec
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/ast/ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ let optimize_bexp (b:bexp) : bexp =

| Mul (Neg b1, Neg b2) -> Mul (o b1, o b2)

| Exp (Exp (b1, b2), b3) -> Exp (o b1, Mul (o b2, o b3))

(* BEGIN SECTION N - NECESSARY FOR PROPER COMPILATION *)
| Gt (Lit l1, Lit l2) -> if l1 > l2 then Lit 1l else Lit 0l
| Lt (Lit l1, Lit l2) -> if l1 < l2 then Lit 1l else Lit 0l
Expand Down
52 changes: 48 additions & 4 deletions src/compiler/optimize.ml
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,49 @@ let connect_rand g c1 c2 =
let f = if Random.bool() then connect_primary else connect_secondary in
f g c1 c2

let detect_signal_collision (output_sig:symbol) (b:bexp) : bool =
let rec intern under_conditional b =
let it = intern true in
let i = intern under_conditional in
begin match b with
| Conditional (b1, b2, b3) -> it b1 || it b2 || it b3
| Lit l -> false
| Var v -> v = output_sig && under_conditional
| Plus (b1, b2)
| Minus (b1, b2)
| Div (b1, b2)
| Mul (b1, b2)
| Exp (b1, b2)
| Mod (b1, b2)
| Lshift (b1, b2)
| Rshift (b1, b2)
| AND (b1, b2)
| OR (b1, b2)
| XOR (b1, b2)
| Gt (b1, b2)
| Lt (b1, b2)
| Gte (b1, b2)
| Lte (b1, b2)
| Eq (b1, b2)
| Neq (b1, b2)
| LAND (b1, b2)
| LOR (b1, b2)
| NAND (b1, b2)
| NOR (b1, b2) -> i b1 || i b2
| Not b
| BOOL b
| Neg b -> i b
end in
intern false b

let circuit_of_bexp (output_sig:symbol) (b: bexp) : circuit =
let vars = vars_in_bexp b in
let sigs = output_sig :: vars in

let sig_ctr = create_sig_ctr sigs in
let collision = detect_signal_collision output_sig b in
let original_out = output_sig in
let output_sig = if collision then sig_ctr () else original_out in

let g = CG.create () in

Expand Down Expand Up @@ -214,13 +252,19 @@ let circuit_of_bexp (output_sig:symbol) (b: bexp) : circuit =
| Some l -> l
| None -> []
end in
let oids = List.map id_of_conn o_conns in
let combs, oids =
if collision then
let id = get_entity_id () in
connect o_conns [Ain id];
List.rev (Arithmetic (id, (Symbol o_sig, Add, Const 0l, Symbol original_out)) :: combs),
[id]
else
combs, List.map id_of_conn o_conns
in

let m_id = List.fold_left (fun acc c -> max acc (id_of_combinator c)) Int.min_int combs in
(* List.iter (fun i -> print_endline (string_of_int i)) iids;
print_endline "+++"; *)

(combs, g, (m_id, vars, [o_sig], iids, oids))
(combs, g, (m_id, vars, [original_out], iids, oids))

let lookup (combs:combinator list) (id:id) : combinator =
List.find (fun c -> id_of_combinator c = id) combs
Expand Down
20 changes: 10 additions & 10 deletions src/parser/parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,16 @@ b_assn:
| b=bexp { [("check", b)] }

bexp:
| b=b_o { b }
| b=b_if { b }

b_if:
| IF g=b_if THEN b1=b_if ELSE b2=b_if { Conditional(g, b1, b2) }
| b=b_o { b }

b_o:
| l=b_o LOR r=b_a { LOR(l, r) }
| b=b_a { b }
| l=b_o LOR r=b_a { LOR(l, r) }
| b1=b_o COALESCE b2=b_a { Conditional(b1, b1, b2) }
| b=b_a { b }

b_a:
| l=b_a LAND r=b1 { LAND(l, r) }
Expand Down Expand Up @@ -132,21 +137,16 @@ b8:
| b=b9 { b }

b9:
| l=b9 EXP r=b10 { Exp(l, r) }
| l=b10 EXP r=b9 { Exp(l, r) }
| b=b10 { b }

b10:
| MINUS b=b10 { Neg(b) }
| NOT b=b10 { Not(b) }
| b=b_coalesce { b }

b_coalesce:
| b1=b_coalesce COALESCE b2=b11 { Conditional(b1, b1, b2) }
| b=b11 { b }
| b=b11 { b }

b11:
| LPAREN b=bexp RPAREN { b }
| IF g=bexp THEN b1=b11 ELSE b2=b11 { Conditional(g, b1, b2) }
| b=b12 { b }

b12:
Expand Down

0 comments on commit 79704ec

Please sign in to comment.