Skip to content

Commit

Permalink
Implement more builtins for bytestring and integer
Browse files Browse the repository at this point in the history
  • Loading branch information
MicroProofs committed Aug 10, 2022
1 parent 7f1ffd8 commit ee3f640
Show file tree
Hide file tree
Showing 5 changed files with 246 additions and 131 deletions.
2 changes: 1 addition & 1 deletion add_integers.uplc
@@ -1,5 +1,5 @@
(program
1.0.0
[ (builtin decodeUtf8) [ (builtin encodeUtf8) (con string "hello world") ] ]
[ (builtin divideInteger) (con integer 5) (con integer 2) ]
)

17 changes: 14 additions & 3 deletions crates/cli/src/main.rs
Expand Up @@ -2,6 +2,7 @@ use std::{fmt::Write as _, fs};

use uplc::{
ast::{DeBruijn, FakeNamedDeBruijn, Name, NamedDeBruijn, Program, Term},
machine::cost_model::ExBudget,
parser,
};

Expand Down Expand Up @@ -102,14 +103,24 @@ fn main() -> anyhow::Result<()> {
Ok(term) => {
let term: Term<Name> = term.try_into()?;

println!("{}", term.to_pretty());
println!("\nResult\n------\n\n{}\n", term.to_pretty());
}
Err(err) => {
eprintln!("{}", err);
eprintln!("\nError\n-----\n\n{}\n", err);
}
}

println!("\nCosts - memory: {} & cpu: {}", cost.mem, cost.cpu);
let budget = ExBudget::default();

println!(
"\nCosts\n-----\ncpu: {}\nmemory: {}",
budget.cpu - cost.cpu,
budget.mem - cost.mem
);
println!(
"\nBudget\n------\ncpu: {}\nmemory: {}\n",
cost.cpu, cost.mem
);
}
},
}
Expand Down
75 changes: 67 additions & 8 deletions crates/uplc/src/machine/cost_model.rs
Expand Up @@ -562,7 +562,16 @@ impl BuiltinCosts {
.cpu
.cost(args[0].to_ex_mem(), args[1].to_ex_mem()),
},
DefaultFunction::DivideInteger => todo!(),
DefaultFunction::DivideInteger => ExBudget {
mem: self
.divide_integer
.mem
.cost(args[0].to_ex_mem(), args[1].to_ex_mem()),
cpu: self
.divide_integer
.cpu
.cost(args[0].to_ex_mem(), args[1].to_ex_mem()),
},
DefaultFunction::QuotientInteger => todo!(),
DefaultFunction::RemainderInteger => todo!(),
DefaultFunction::ModInteger => todo!(),
Expand Down Expand Up @@ -606,10 +615,42 @@ impl BuiltinCosts {
.cpu
.cost(args[0].to_ex_mem(), args[1].to_ex_mem()),
},
DefaultFunction::ConsByteString => todo!(),
DefaultFunction::SliceByteString => todo!(),
DefaultFunction::LengthOfByteString => todo!(),
DefaultFunction::IndexByteString => todo!(),
DefaultFunction::ConsByteString => ExBudget {
mem: self
.cons_byte_string
.mem
.cost(args[0].to_ex_mem(), args[1].to_ex_mem()),
cpu: self
.cons_byte_string
.cpu
.cost(args[0].to_ex_mem(), args[1].to_ex_mem()),
},
DefaultFunction::SliceByteString => ExBudget {
mem: self.slice_byte_string.mem.cost(
args[0].to_ex_mem(),
args[1].to_ex_mem(),
args[2].to_ex_mem(),
),
cpu: self.slice_byte_string.cpu.cost(
args[0].to_ex_mem(),
args[1].to_ex_mem(),
args[2].to_ex_mem(),
),
},
DefaultFunction::LengthOfByteString => ExBudget {
mem: self.length_of_byte_string.mem.cost(args[0].to_ex_mem()),
cpu: self.length_of_byte_string.cpu.cost(args[0].to_ex_mem()),
},
DefaultFunction::IndexByteString => ExBudget {
mem: self
.index_byte_string
.mem
.cost(args[0].to_ex_mem(), args[1].to_ex_mem()),
cpu: self
.index_byte_string
.cpu
.cost(args[0].to_ex_mem(), args[1].to_ex_mem()),
},
DefaultFunction::EqualsByteString => ExBudget {
mem: self
.equals_byte_string
Expand All @@ -620,8 +661,26 @@ impl BuiltinCosts {
.cpu
.cost(args[0].to_ex_mem(), args[1].to_ex_mem()),
},
DefaultFunction::LessThanByteString => todo!(),
DefaultFunction::LessThanEqualsByteString => todo!(),
DefaultFunction::LessThanByteString => ExBudget {
mem: self
.less_than_byte_string
.mem
.cost(args[0].to_ex_mem(), args[1].to_ex_mem()),
cpu: self
.less_than_byte_string
.cpu
.cost(args[0].to_ex_mem(), args[1].to_ex_mem()),
},
DefaultFunction::LessThanEqualsByteString => ExBudget {
mem: self
.less_than_equals_byte_string
.mem
.cost(args[0].to_ex_mem(), args[1].to_ex_mem()),
cpu: self
.less_than_equals_byte_string
.cpu
.cost(args[0].to_ex_mem(), args[1].to_ex_mem()),
},
DefaultFunction::Sha2_256 => ExBudget {
mem: self.sha2_256.mem.cost(args[0].to_ex_mem()),
cpu: self.sha2_256.cpu.cost(args[0].to_ex_mem()),
Expand Down Expand Up @@ -763,7 +822,7 @@ impl TwoArguments {
TwoArguments::LinearInX(l) => l.slope * x + l.intercept,
TwoArguments::LinearInY(l) => l.slope * y + l.intercept,
TwoArguments::AddedSizes(s) => s.slope * (x + y) + s.intercept,
TwoArguments::SubtractedSizes(s) => s.slope * s.minimum.min(x - y) + s.intercept,
TwoArguments::SubtractedSizes(s) => s.slope * s.minimum.max(x - y) + s.intercept,
TwoArguments::MultipliedSizes(s) => s.slope * (x * y) + s.intercept,
TwoArguments::MinSize(s) => s.slope * x.min(y) + s.intercept,
TwoArguments::MaxSize(s) => s.slope * x.max(y) + s.intercept,
Expand Down
4 changes: 4 additions & 0 deletions crates/uplc/src/machine/error.rs
Expand Up @@ -32,4 +32,8 @@ pub enum Error {
MachineNeverReachedDone,
#[error("Decoding utf8")]
Utf8(#[from] FromUtf8Error),
#[error("Out of Bounds\n\nindex: {}\nbytestring: {}\npossible: 0 - {}", .0, hex::encode(.1), .1.len() - 1)]
ByteStringOutOfBounds(isize, Vec<u8>),
#[error("Divide By Zero\n\n{0} / {1}")]
DivideByZero(isize, isize),
}

0 comments on commit ee3f640

Please sign in to comment.