diff --git a/crates/uplc/src/machine.rs b/crates/uplc/src/machine.rs index 81cd44ae8..f6598d9a7 100644 --- a/crates/uplc/src/machine.rs +++ b/crates/uplc/src/machine.rs @@ -240,9 +240,12 @@ impl Machine { env, t.clone(), )), - None => todo!(), + None => Err(Error::MissingCaseBranch( + branches, + Value::Constr { tag, fields }, + )), }, - _ => todo!("return a proper evaluation error"), + v => Err(Error::NonConstrScrutinized(v)), }, } } diff --git a/crates/uplc/src/machine/cost_model.rs b/crates/uplc/src/machine/cost_model.rs index 6b9002580..2701fc0c4 100644 --- a/crates/uplc/src/machine/cost_model.rs +++ b/crates/uplc/src/machine/cost_model.rs @@ -186,12 +186,12 @@ impl Default for MachineCosts { }, // Placeholder values constr: ExBudget { - mem: 30000000000, - cpu: 30000000000, + mem: 100, + cpu: 23000, }, case: ExBudget { - mem: 30000000000, - cpu: 30000000000, + mem: 100, + cpu: 23000, }, } } diff --git a/crates/uplc/src/machine/error.rs b/crates/uplc/src/machine/error.rs index 468cb5189..bac398ca1 100644 --- a/crates/uplc/src/machine/error.rs +++ b/crates/uplc/src/machine/error.rs @@ -20,6 +20,10 @@ pub enum Error { NonPolymorphicInstantiation(Value), #[error("Attempted to apply a non-function:\n\n{0:#?} to argument:\n\n{1:#?}")] NonFunctionalApplication(Value, Value), + #[error("Attempted to case a non-const:\n\n{0:#?}")] + NonConstrScrutinized(Value), + #[error("Cases: {0:#?}\n\n are missing branch for constr:\n\n{1:#?}")] + MissingCaseBranch(Vec>, Value), #[error("Type mismatch expected '{0}' got '{1}'")] TypeMismatch(Type, Type), #[error("Type mismatch expected '(list a)' got '{0}'")] @@ -36,6 +40,7 @@ pub enum Error { NotAConstant(Value), #[error("The evaluation never reached a final state")] MachineNeverReachedDone, + #[error("Decoding utf8")] Utf8(#[from] FromUtf8Error), #[error("Out of Bounds\n\nindex: {}\nbytestring: {}\npossible: 0 - {}", .0, hex::encode(.1), .1.len() - 1)] diff --git a/crates/uplc/src/parser.rs b/crates/uplc/src/parser.rs index 794518378..29adc8d71 100644 --- a/crates/uplc/src/parser.rs +++ b/crates/uplc/src/parser.rs @@ -145,7 +145,7 @@ peg::parser! { } rule case(interner: &mut Interner) -> Term - = "(" _* "case" _+ constr:term(interner) _* branches:(t:term(interner) _* { t })+ _* ")" { + = "(" _* "case" _+ constr:term(interner) _* branches:(t:term(interner) _* { t })* _* ")" { Term::Case { constr: constr.into(), branches } } diff --git a/crates/uplc/test_data/conformance/evaluation/term/case/case-1/case-1.uplc b/crates/uplc/test_data/conformance/evaluation/term/case/case-1/case-1.uplc new file mode 100644 index 000000000..a08de31c4 --- /dev/null +++ b/crates/uplc/test_data/conformance/evaluation/term/case/case-1/case-1.uplc @@ -0,0 +1,4 @@ +-- select first branch +(program 1.1.0 + (case (constr 0 (con integer 0)) (lam x (con integer 1)) (lam x (con integer 2))) +) diff --git a/crates/uplc/test_data/conformance/evaluation/term/case/case-1/case-1.uplc.expected b/crates/uplc/test_data/conformance/evaluation/term/case/case-1/case-1.uplc.expected new file mode 100644 index 000000000..c897a619d --- /dev/null +++ b/crates/uplc/test_data/conformance/evaluation/term/case/case-1/case-1.uplc.expected @@ -0,0 +1 @@ +(program 1.1.0 (con integer 1)) \ No newline at end of file diff --git a/crates/uplc/test_data/conformance/evaluation/term/case/case-2/case-2.uplc b/crates/uplc/test_data/conformance/evaluation/term/case/case-2/case-2.uplc new file mode 100644 index 000000000..b9861165c --- /dev/null +++ b/crates/uplc/test_data/conformance/evaluation/term/case/case-2/case-2.uplc @@ -0,0 +1,4 @@ +-- select second branch +(program 1.1.0 + (case (constr 1 (con integer 0)) (lam x (con integer 1)) (lam x (con integer 2))) +) diff --git a/crates/uplc/test_data/conformance/evaluation/term/case/case-2/case-2.uplc.expected b/crates/uplc/test_data/conformance/evaluation/term/case/case-2/case-2.uplc.expected new file mode 100644 index 000000000..2b513f80b --- /dev/null +++ b/crates/uplc/test_data/conformance/evaluation/term/case/case-2/case-2.uplc.expected @@ -0,0 +1 @@ +(program 1.1.0 (con integer 2)) \ No newline at end of file diff --git a/crates/uplc/test_data/conformance/evaluation/term/case/case-3/case-3.uplc b/crates/uplc/test_data/conformance/evaluation/term/case/case-3/case-3.uplc new file mode 100644 index 000000000..a4399f953 --- /dev/null +++ b/crates/uplc/test_data/conformance/evaluation/term/case/case-3/case-3.uplc @@ -0,0 +1,4 @@ +-- select first branch and do computation with the args +(program 1.1.0 + (case (constr 0 (con integer 3) (con integer 2)) (lam x (lam y [(builtin addInteger) x y])) (lam x (lam y [(builtin subtractInteger) x y]))) +) diff --git a/crates/uplc/test_data/conformance/evaluation/term/case/case-3/case-3.uplc.expected b/crates/uplc/test_data/conformance/evaluation/term/case/case-3/case-3.uplc.expected new file mode 100644 index 000000000..307ccca40 --- /dev/null +++ b/crates/uplc/test_data/conformance/evaluation/term/case/case-3/case-3.uplc.expected @@ -0,0 +1 @@ +(program 1.1.0 (con integer 5)) \ No newline at end of file diff --git a/crates/uplc/test_data/conformance/evaluation/term/case/case-4/case-4.uplc b/crates/uplc/test_data/conformance/evaluation/term/case/case-4/case-4.uplc new file mode 100644 index 000000000..e471f4ec4 --- /dev/null +++ b/crates/uplc/test_data/conformance/evaluation/term/case/case-4/case-4.uplc @@ -0,0 +1,4 @@ +-- select second branch and do computation with the args +(program 1.1.0 + (case (constr 1 (con integer 3) (con integer 2)) (lam x (lam y [(builtin addInteger) x y])) (lam x (lam y [(builtin subtractInteger) x y]))) +) diff --git a/crates/uplc/test_data/conformance/evaluation/term/case/case-4/case-4.uplc.expected b/crates/uplc/test_data/conformance/evaluation/term/case/case-4/case-4.uplc.expected new file mode 100644 index 000000000..c897a619d --- /dev/null +++ b/crates/uplc/test_data/conformance/evaluation/term/case/case-4/case-4.uplc.expected @@ -0,0 +1 @@ +(program 1.1.0 (con integer 1)) \ No newline at end of file diff --git a/crates/uplc/test_data/conformance/evaluation/term/case/case-5/case-5.uplc b/crates/uplc/test_data/conformance/evaluation/term/case/case-5/case-5.uplc new file mode 100644 index 000000000..5478b9182 --- /dev/null +++ b/crates/uplc/test_data/conformance/evaluation/term/case/case-5/case-5.uplc @@ -0,0 +1,4 @@ +-- case of non-constr +(program 1.1.0 + (case (con integer 1) (lam x x) (lam x x)) +) diff --git a/crates/uplc/test_data/conformance/evaluation/term/case/case-5/case-5.uplc.expected b/crates/uplc/test_data/conformance/evaluation/term/case/case-5/case-5.uplc.expected new file mode 100644 index 000000000..ccc477ffe --- /dev/null +++ b/crates/uplc/test_data/conformance/evaluation/term/case/case-5/case-5.uplc.expected @@ -0,0 +1 @@ +evaluation failure \ No newline at end of file diff --git a/crates/uplc/test_data/conformance/evaluation/term/case/case-7/case-7.uplc b/crates/uplc/test_data/conformance/evaluation/term/case/case-7/case-7.uplc deleted file mode 100644 index 94153d582..000000000 --- a/crates/uplc/test_data/conformance/evaluation/term/case/case-7/case-7.uplc +++ /dev/null @@ -1,4 +0,0 @@ --- case can't be used before 1.1.0 -(program 1.0.0 - (case (con integer 1)) -) diff --git a/crates/uplc/test_data/conformance/evaluation/term/case/case-7/case-7.uplc.expected b/crates/uplc/test_data/conformance/evaluation/term/case/case-7/case-7.uplc.expected deleted file mode 100644 index fd5694899..000000000 --- a/crates/uplc/test_data/conformance/evaluation/term/case/case-7/case-7.uplc.expected +++ /dev/null @@ -1 +0,0 @@ -parse error \ No newline at end of file diff --git a/crates/uplc/test_data/conformance/evaluation/term/case/case-8/case-8.uplc b/crates/uplc/test_data/conformance/evaluation/term/case/case-8/case-8.uplc new file mode 100644 index 000000000..5aae469be --- /dev/null +++ b/crates/uplc/test_data/conformance/evaluation/term/case/case-8/case-8.uplc @@ -0,0 +1,4 @@ +-- nullary case +(program 1.1.0 + (case (constr 0) (con integer 1) (con integer 2)) +) diff --git a/crates/uplc/test_data/conformance/evaluation/term/case/case-8/case-8.uplc.expected b/crates/uplc/test_data/conformance/evaluation/term/case/case-8/case-8.uplc.expected new file mode 100644 index 000000000..c897a619d --- /dev/null +++ b/crates/uplc/test_data/conformance/evaluation/term/case/case-8/case-8.uplc.expected @@ -0,0 +1 @@ +(program 1.1.0 (con integer 1)) \ No newline at end of file diff --git a/crates/uplc/test_data/conformance/evaluation/term/case/case-9/case-9.uplc b/crates/uplc/test_data/conformance/evaluation/term/case/case-9/case-9.uplc new file mode 100644 index 000000000..73b0719b1 --- /dev/null +++ b/crates/uplc/test_data/conformance/evaluation/term/case/case-9/case-9.uplc @@ -0,0 +1,4 @@ +-- empty case, aka -XEmptyCase +(program 1.1.0 + (case (constr 0)) +) diff --git a/crates/uplc/test_data/conformance/evaluation/term/case/case-9/case-9.uplc.expected b/crates/uplc/test_data/conformance/evaluation/term/case/case-9/case-9.uplc.expected new file mode 100644 index 000000000..ccc477ffe --- /dev/null +++ b/crates/uplc/test_data/conformance/evaluation/term/case/case-9/case-9.uplc.expected @@ -0,0 +1 @@ +evaluation failure \ No newline at end of file diff --git a/crates/uplc/test_data/conformance/evaluation/term/constr/constr-1/constr-1.uplc b/crates/uplc/test_data/conformance/evaluation/term/constr/constr-1/constr-1.uplc new file mode 100644 index 000000000..9c413fa7f --- /dev/null +++ b/crates/uplc/test_data/conformance/evaluation/term/constr/constr-1/constr-1.uplc @@ -0,0 +1,4 @@ +-- empty constr +(program 1.1.0 + (constr 0 ) +) diff --git a/crates/uplc/test_data/conformance/evaluation/term/constr/constr-1/constr-1.uplc.expected b/crates/uplc/test_data/conformance/evaluation/term/constr/constr-1/constr-1.uplc.expected new file mode 100644 index 000000000..a65a6aca2 --- /dev/null +++ b/crates/uplc/test_data/conformance/evaluation/term/constr/constr-1/constr-1.uplc.expected @@ -0,0 +1 @@ +(program 1.1.0 (constr 0)) \ No newline at end of file diff --git a/crates/uplc/test_data/conformance/evaluation/term/constr/constr-2/constr-2.uplc b/crates/uplc/test_data/conformance/evaluation/term/constr/constr-2/constr-2.uplc new file mode 100644 index 000000000..a27c1c666 --- /dev/null +++ b/crates/uplc/test_data/conformance/evaluation/term/constr/constr-2/constr-2.uplc @@ -0,0 +1,4 @@ +-- constr with an argument +(program 1.1.0 + (constr 0 (con integer 1)) +) diff --git a/crates/uplc/test_data/conformance/evaluation/term/constr/constr-2/constr-2.uplc.expected b/crates/uplc/test_data/conformance/evaluation/term/constr/constr-2/constr-2.uplc.expected new file mode 100644 index 000000000..fafd655db --- /dev/null +++ b/crates/uplc/test_data/conformance/evaluation/term/constr/constr-2/constr-2.uplc.expected @@ -0,0 +1 @@ +(program 1.1.0 (constr 0 (con integer 1))) \ No newline at end of file