Skip to content

Commit

Permalink
Separate out adding parentheses from printing
Browse files Browse the repository at this point in the history
  • Loading branch information
Negabinary committed Apr 30, 2024
1 parent 4fe9d9c commit 4722680
Show file tree
Hide file tree
Showing 9 changed files with 452 additions and 178 deletions.
4 changes: 2 additions & 2 deletions src/haz3lcore/lang/Form.re
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ let atomic_forms: list((string, (string => bool, list(Mold.t)))) = [
let forms: list((string, t)) = [
// INFIX OPERATORS
("typ_plus", mk_infix("+", Typ, P.or_)),
("type-arrow", mk_infix("->", Typ, 6)),
("cell-join", mk_infix(";", Exp, 10)),
("type-arrow", mk_infix("->", Typ, P.plus)),
("cell-join", mk_infix(";", Exp, P.semi)),
("plus", mk_infix("+", Exp, P.plus)),
("minus", mk_infix("-", Exp, P.plus)),
("times", mk_infix("*", Exp, P.mult)),
Expand Down
9 changes: 9 additions & 0 deletions src/haz3lcore/lang/Operators.re
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,12 @@ let string_op_to_string = (op: op_bin_string): string => {
| Equals => "$=="
};
};

let bin_op_to_string = (op: op_bin): string => {
switch (op) {
| Int(op) => int_op_to_string(op)
| Float(op) => float_op_to_string(op)
| Bool(op) => bool_op_to_string(op)
| String(op) => string_op_to_string(op)
};
};
108 changes: 87 additions & 21 deletions src/haz3lcore/lang/Precedence.re
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,63 @@ open Util;

/**
* higher precedence means lower int representation
*
* These precedences are interspersed with examples to help you
* work out the precedence. For each example, if a construct
* requires parentheses when placed in the '_____' space, then
* your new construct's precedence is below the comment with
* the example. (i.e. higher int)
*/
[@deriving (show({with_path: false}), sexp, yojson)]
type t = int;

let max: t = 0;

let unquote = 1;
let ap = 2;
let neg = 3;
let power = 4;
let mult = 5;

let not_ = 5;
let plus = 6;
let cons = 7;
let concat = 8;
let eqs = 9;
let and_ = 10;
let or_ = 11;
let ann = 12;
let if_ = 13;
let fun_ = 14;
let prod = 15;
let semi = 16;
let let_ = 17;
let filter = 18;
let cast = 2;
let ap = 3;
// _____(x)
let neg = 4;
// _____ ** 2
let power = 5;
// 2 ** _____
// 6 / _____
let mult = 6;
let not_ = 6;
// _____ / 6
// 4 - _____
let plus = 7;
// _____ - 4
// _____ :: []
let cons = 8;
// 1 :: _____
// [1,2] @ _____
let concat = 9;
// _____ @ [1,2]
// x == _____
let eqs = 10;
// _____ == x
// _____ && true
let and_ = 11;
// true && _____
// _____ || false
let or_ = 12;
// false || _____
let ann = 13;
let if_ = 14;
let fun_ = 15;
// fun x -> _____
let prod = 16;
// a , _____ , x
// _____ ; ()
let semi = 17;
// () ; _____
let let_ = 18;
let rule_arr = 19;
let rule_pre = 20;
let rule_sep = 21;
let case_ = 22;

let min = 23;
let min = 24;

let compare = (p1: t, p2: t): int =>
(-1) * Int.compare((p1 :> int), (p2 :> int));
Expand All @@ -55,3 +80,44 @@ let associativity_map: IntMap.t(Direction.t) =

let associativity = (p: t): option(Direction.t) =>
IntMap.find_opt(p, associativity_map);

let of_bin_op: Operators.op_bin => t =
fun
| Int(op) =>
switch (op) {
| Plus => plus
| Minus => plus
| Times => mult
| Power => power
| Divide => mult
| LessThan => eqs
| LessThanOrEqual => eqs
| GreaterThan => eqs
| GreaterThanOrEqual => eqs
| Equals => eqs
| NotEquals => eqs
}
| Float(op) =>
switch (op) {
| Plus => plus
| Minus => plus
| Times => mult
| Power => power
| Divide => mult
| LessThan => eqs
| LessThanOrEqual => eqs
| GreaterThan => eqs
| GreaterThanOrEqual => eqs
| Equals => eqs
| NotEquals => eqs
}
| Bool(op) =>
switch (op) {
| And => and_
| Or => or_
}
| String(op) =>
switch (op) {
| Concat => concat
| Equals => eqs
};
Loading

0 comments on commit 4722680

Please sign in to comment.