Skip to content

Commit

Permalink
Merge pull request #642 from leiamcalisteryoung/parser
Browse files Browse the repository at this point in the history
Adding absolute value to parser
  • Loading branch information
ozgurakgun authored Feb 5, 2025
2 parents e31a127 + 2838c76 commit 3dd627a
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 10 deletions.
21 changes: 17 additions & 4 deletions conjure_oxide/src/utils/essence_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ fn parse_domain(domain: Node, source_code: &str) -> Domain {
}

fn parse_int_domain(int_domain: Node, source_code: &str) -> Domain {
//TODO implement functionality for non-simple ints
if int_domain.child_count() == 1 {
Domain::IntDomain(vec![Range::Bounded(std::i32::MIN, std::i32::MAX)])
} else {
Expand Down Expand Up @@ -233,9 +232,7 @@ fn parse_constraint(constraint: Node, source_code: &str) -> Expression {

match op_type {
"+" => Expression::Sum(Metadata::new(), vec![expr1, expr2]),
"-" => {
panic!("Subtraction expressions not supported yet")
}
"-" => Expression::Minus(Metadata::new(), Box::new(expr1), Box::new(expr2)),
"*" => Expression::Product(Metadata::new(), vec![expr1, expr2]),
"/" => {
//TODO: add checks for if division is safe or not
Expand Down Expand Up @@ -315,6 +312,22 @@ fn parse_constraint(constraint: Node, source_code: &str) -> Expression {
Atom::Reference(Name::UserName(variable_name)),
)
}
"abs_value" => {
let child = constraint.child(1).expect("Error with absolute value");
Expression::Abs(
Metadata::new(),
Box::new(parse_constraint(child, source_code)),
)
}
"unary_minus_expr" => {
let child = constraint
.child(1)
.expect("Error with unary minus expression");
Expression::Neg(
Metadata::new(),
Box::new(parse_constraint(child, source_code)),
)
}
_ => {
let node_kind = constraint.kind();
panic!("{node_kind} is not a recognized node kind");
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
use_native_parser=false
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
run_solver=true
use_native_parser=false
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
run_solver=true
use_native_parser=false
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
run_solver=true
use_native_parser=false
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
use_native_parser=false
12 changes: 11 additions & 1 deletion crates/tree-sitter-essence/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ module.exports = grammar ({
constraint: $ => seq($.expression, optional(",")),

expression: $ => choice(
$.unary_minus_expr,
$.or_expr,
$.and_expr,
$.comparison,
Expand All @@ -103,8 +104,11 @@ module.exports = grammar ({
$.sum,
$.all_diff,
$.constant,
$.variable
$.variable,
$.abs_value
),

unary_minus_expr: $ => prec.left(seq("-", $.expression)),

or_expr: $ => prec.left(choice(
seq($.expression, "\\/", $.expression),
Expand Down Expand Up @@ -189,6 +193,12 @@ module.exports = grammar ({
optional(",")
)),
"])"
),

abs_value: $ => seq(
"|",
$.expression,
"|"
)
}
})

0 comments on commit 3dd627a

Please sign in to comment.