feat: new Error not a constant

This commit is contained in:
rvcas 2022-07-20 23:53:57 -04:00 committed by Kasey White
parent 77a7b11467
commit 9e62181caa
7 changed files with 36 additions and 50 deletions

View File

@ -1,7 +1,5 @@
(program (program
1.0.0 1.0.0
[ [ (force (builtin ifThenElse)) (con string "yo") (con integer 3) (con integer 4) ]
[ [ (force (builtin ifThenElse)) (con integer 2) ] (con integer 1) ]
(con string "yo")
]
) )

View File

@ -25,12 +25,6 @@ where
/// We use this to apply the validator to Datum, /// We use this to apply the validator to Datum,
/// then redeemer, then ScriptContext. If datum is /// then redeemer, then ScriptContext. If datum is
/// even necessary (i.e. minting policy). /// even necessary (i.e. minting policy).
///
/// ```rust
/// program.apply(&datum)
/// .apply(&redeemer)
/// .apply(&script_context);
/// ```
pub fn apply(&self, program: &Self) -> Self { pub fn apply(&self, program: &Self) -> Self {
let applied_term = Term::Apply { let applied_term = Term::Apply {
function: Box::new(self.term.clone()), function: Box::new(self.term.clone()),
@ -86,10 +80,8 @@ pub enum Constant {
// tag: 2 // tag: 2
String(String), String(String),
// tag: 3 // tag: 3
Char(char),
// tag: 4
Unit, Unit,
// tag: 5 // tag: 4
Bool(bool), Bool(bool),
} }

View File

@ -170,14 +170,6 @@ impl Encode for &Constant {
encode_constant(2, e)?; encode_constant(2, e)?;
s.encode(e)?; s.encode(e)?;
} }
// there is no char constant tag
Constant::Char(c) => {
let mut b = [0; 4];
let s = c.encode_utf8(&mut b);
s.as_bytes().encode(e)?;
}
Constant::Unit => encode_constant(3, e)?, Constant::Unit => encode_constant(3, e)?,
Constant::Bool(b) => { Constant::Bool(b) => {
encode_constant(4, e)?; encode_constant(4, e)?;

View File

@ -377,22 +377,31 @@ impl Value {
Value::Builtin { .. } => 1, Value::Builtin { .. } => 1,
} }
} }
pub fn expect_type(&self, r#type: Type) -> Result<(), Error> {
match self {
Value::Con(constant) => {
let constant_type = Type::from(constant);
if constant_type == r#type {
Ok(())
} else {
Err(Error::TypeMismatch(r#type, constant_type))
}
}
rest => Err(Error::NotAConstant(rest.clone())),
}
}
} }
impl From<&Value> for Type { impl From<&Constant> for Type {
fn from(value: &Value) -> Self { fn from(constant: &Constant) -> Self {
match value { match constant {
Value::Con(constant) => match constant {
Constant::Integer(_) => Type::Integer, Constant::Integer(_) => Type::Integer,
Constant::ByteString(_) => Type::ByteString, Constant::ByteString(_) => Type::ByteString,
Constant::String(_) => Type::String, Constant::String(_) => Type::String,
Constant::Char(_) => todo!(),
Constant::Unit => Type::Unit, Constant::Unit => Type::Unit,
Constant::Bool(_) => Type::Bool, Constant::Bool(_) => Type::Bool,
},
Value::Delay(_) => todo!(),
Value::Lambda { .. } => todo!(),
Value::Builtin { .. } => todo!(),
} }
} }
} }

View File

@ -12,13 +12,13 @@ pub enum Error {
OutOfExError(ExBudget), OutOfExError(ExBudget),
#[error("Invalid Stepkind: {0}")] #[error("Invalid Stepkind: {0}")]
InvalidStepKind(u8), InvalidStepKind(u8),
#[error("Cannot evaluate an open term: {0:#?}")] #[error("Cannot evaluate an open term:\n\n{0:#?}")]
OpenTermEvaluated(Term<NamedDeBruijn>), OpenTermEvaluated(Term<NamedDeBruijn>),
#[error("The provided Plutus code called 'error'.")] #[error("The provided Plutus code called 'error'.")]
EvaluationFailure, EvaluationFailure,
#[error("Attempted to instantiate a non-polymorphic term: {0:#?}")] #[error("Attempted to instantiate a non-polymorphic term:\n\n{0:#?}")]
NonPolymorphicInstantiation(Value), NonPolymorphicInstantiation(Value),
#[error("Attempted to apply a non-function: {0:#?}")] #[error("Attempted to apply a non-function:\n\n{0:#?}")]
NonFunctionalApplication(Value), NonFunctionalApplication(Value),
#[error("Type mismatch expected '{0}' got '{1}'")] #[error("Type mismatch expected '{0}' got '{1}'")]
TypeMismatch(Type, Type), TypeMismatch(Type, Type),
@ -26,9 +26,11 @@ pub enum Error {
UnexpectedBuiltinTermArgument(Term<NamedDeBruijn>), UnexpectedBuiltinTermArgument(Term<NamedDeBruijn>),
#[error("A builtin expected a term argument, but something else was received:\n\n{}\n\nYou probably have an extra force wrapped around a builtin", .0.to_pretty())] #[error("A builtin expected a term argument, but something else was received:\n\n{}\n\nYou probably have an extra force wrapped around a builtin", .0.to_pretty())]
BuiltinTermArgumentExpected(Term<NamedDeBruijn>), BuiltinTermArgumentExpected(Term<NamedDeBruijn>),
#[error("Unable to unlift value because it is not a constant:\n\n{0:#?}")]
NotAConstant(Value),
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone, PartialEq)]
pub enum Type { pub enum Type {
Bool, Bool,
Integer, Integer,

View File

@ -188,13 +188,7 @@ impl DefaultFunction {
pub fn check_type(&self, arg: &Value, args: &[Value]) -> Result<(), Error> { pub fn check_type(&self, arg: &Value, args: &[Value]) -> Result<(), Error> {
match self { match self {
DefaultFunction::AddInteger => { DefaultFunction::AddInteger => arg.expect_type(Type::Integer),
if arg.is_integer() {
Ok(())
} else {
Err(Error::TypeMismatch(Type::Integer, arg.into()))
}
}
DefaultFunction::SubtractInteger => todo!(), DefaultFunction::SubtractInteger => todo!(),
DefaultFunction::MultiplyInteger => todo!(), DefaultFunction::MultiplyInteger => todo!(),
DefaultFunction::DivideInteger => todo!(), DefaultFunction::DivideInteger => todo!(),
@ -224,7 +218,7 @@ impl DefaultFunction {
DefaultFunction::DecodeUtf8 => todo!(), DefaultFunction::DecodeUtf8 => todo!(),
DefaultFunction::IfThenElse => { DefaultFunction::IfThenElse => {
if args.is_empty() && !arg.is_bool() { if args.is_empty() && !arg.is_bool() {
Err(Error::TypeMismatch(Type::Bool, arg.into())) arg.expect_type(Type::Bool)
} else { } else {
Ok(()) Ok(())
} }

View File

@ -164,7 +164,6 @@ impl Constant {
.append(RcDoc::text("\"")) .append(RcDoc::text("\""))
.append(RcDoc::text(s)) .append(RcDoc::text(s))
.append(RcDoc::text("\"")), .append(RcDoc::text("\"")),
Constant::Char(c) => unimplemented!("char: {}", c),
Constant::Unit => RcDoc::text("unit") Constant::Unit => RcDoc::text("unit")
.append(RcDoc::line()) .append(RcDoc::line())
.append(RcDoc::text("()")), .append(RcDoc::text("()")),