Skip to content

Commit

Permalink
Added ExpToSegment
Browse files Browse the repository at this point in the history
Co-authored-by: Alexander Bandukwala <[email protected]>
  • Loading branch information
Negabinary and 7h3kk1d committed Oct 7, 2024
1 parent c1c0eba commit 4c9520c
Show file tree
Hide file tree
Showing 10 changed files with 1,501 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/haz3lcore/lang/Form.re
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ let forms: list((string, t)) = [
("list_concat", mk_infix("@", Exp, P.plus)),
("cons_exp", mk_infix("::", Exp, P.cons)),
("cons_pat", mk_infix("::", Pat, P.cons)),
("typeann", mk(ss, [":"], mk_bin'(P.ann, Pat, Pat, [], Typ))),
("typeann", mk(ss, [":"], mk_bin'(P.cast, Pat, Pat, [], Typ))),
// UNARY PREFIX OPERATORS
("not", mk(ii, ["!"], mk_pre(P.not_, Exp, []))),
("typ_sum_single", mk(ss, ["+"], mk_pre(P.or_, Typ, []))),
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)
};
};
169 changes: 127 additions & 42 deletions src/haz3lcore/lang/Precedence.re
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,145 @@ 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 associativity_map: ref(list((t, Direction.t))) = ref([]);
let left_associative = (level: t) => {
associativity_map := [(level, Direction.Left), ...associativity_map^];
level;
};
let right_associative = (level: t) => {
associativity_map := [(level, Direction.Right), ...associativity_map^];
level;
};

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 semi = 16;
let let_ = 17;
let filter = 18;
let rule_arr = 19;
let rule_pre = 20;
let rule_sep = 21;
let case_ = 22;

let comma = 15;

let type_plus = 4;
let type_arrow = 5;
let type_prod = comma;

let min = 26;
// ========== TYPES ==========
let type_sum_ap = 22;
// _____ (Int)
// + T1 + _____
let type_plus = 25;
// _____ -> Int
let type_arrow = 28 |> right_associative;
// Int -> _____
// String , _____ , String
let type_prod = 45;
let type_binder = 38;
// forall t -> _____
// rec t -> _____

// ======== PATTERNS =========
// ======= EXPRESSIONS =======

let unquote = 21;
// $_____
let ap = 22;
// _____(x)
// 5 : _____
let cast = 23 |> left_associative;
// _____ : T
// - _____
let neg = 24;
// _____ ** 2
let power = 25 |> right_associative;
// 2 ** _____
// 6 / _____
let mult = 26 |> left_associative;
let not_ = 26;
// _____ / 6
// 4 - _____
let plus = 27 |> left_associative;
// _____ - 4
// _____ :: []
let cons = 28 |> right_associative;
// 1 :: _____
// [1,2] @ _____
let concat = 29 |> right_associative;
// _____ @ [1,2]
// x == _____
let eqs = 30 |> left_associative;
// _____ == x
// _____ && true
let and_ = 31;
// true && _____
// _____ || false
let or_ = 32;
// false || _____
let if_ = 34;
let fun_ = 35;
// fun x -> _____
let prod = 36;
// a , _____ , x
// _____ ; ()
let semi = 37 |> right_associative;
// () ; _____
let let_ = 38;
// let x = 3 in _____
let rule_arr = 39;
let rule_pre = 40;
let rule_sep = 41;
let case_ = 42;

let comma = 45;

let min = 46;

let compare = (p1: t, p2: t): int =>
(-1) * Int.compare((p1 :> int), (p2 :> int));
// let min = (p1: t, p2: t): t => max(p1, p2);

let associativity_map: IntMap.t(Direction.t) =
[
(mult, Direction.Left),
(plus, Left),
(power, Right),
(cons, Right),
(concat, Right),
(ann, Left),
(eqs, Left),
(type_arrow, Right),
]
|> List.to_seq
|> IntMap.of_seq;
associativity_map^ |> List.to_seq |> IntMap.of_seq;

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 4c9520c

Please sign in to comment.