Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated integer types from 32-bit to 64-bit #16

Merged
merged 2 commits into from
Aug 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ library aims to be very simple to use, while still being fast, safe, and customi
fn main() {
let program = Program::compile("add(2, 3) == 5").unwrap();
let mut context = Context::default();
context.add_function("add", |a: i32, b: i32| a + b);
context.add_function("add", |a: i64, b: i64| a + b);
let value = program.execute(&context).unwrap();
assert_eq!(value, true.into());
}
Expand Down
6 changes: 3 additions & 3 deletions example/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn main() {
let mut context = Context::default();

// Add functions using closures
context.add_function("add", |a: i32, b: i32| a + b);
context.add_function("add", |a: i64, b: i64| a + b);

// Add methods to a string type
context.add_function("isEmpty", is_empty);
Expand Down Expand Up @@ -51,8 +51,8 @@ fn fail(ftx: &FunctionContext) -> ResolveResult {
/// as arguments to a function, as well as the fact that any of these types can also
/// be returned from a function.
fn primitives(
_a: i32,
_b: u32,
_a: i64,
_b: u64,
_c: f64,
_d: bool,
_e: Rc<String>,
Expand Down
2 changes: 1 addition & 1 deletion interpreter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn main() {

// Add any variables or functions that the program will need
let mut context = Context::default();
context.add_function("add", |a: i32, b: i32| a + b);
context.add_function("add", |a: i64, b: i64| a + b);

// Run the program
let value = program.execute(&context).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions interpreter/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ impl<'context> FunctionContext<'context> {
/// ```skip
/// size([1, 2, 3]) == 3
/// ```
pub fn size(ftx: &FunctionContext, value: Value) -> Result<i32> {
pub fn size(ftx: &FunctionContext, value: Value) -> Result<i64> {
let size = match value {
Value::List(l) => l.len(),
Value::Map(m) => m.map.len(),
Value::String(s) => s.len(),
Value::Bytes(b) => b.len(),
value => return Err(ftx.error(&format!("cannot determine the size of {:?}", value))),
};
Ok(size as i32)
Ok(size as i64)
}

/// Returns true if the target contains the provided argument. The actual behavior
Expand Down
4 changes: 2 additions & 2 deletions interpreter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ mod tests {
fn variables() {
fn assert_output(script: &str, expected: ResolveResult) {
let mut ctx = Context::default();
ctx.add_variable("foo", HashMap::from([("bar", 1)]));
ctx.add_variable("arr", vec![1i32, 2, 3]);
ctx.add_variable("foo", HashMap::from([("bar", 1i64)]));
ctx.add_variable("arr", vec![1i64, 2, 3]);
ctx.add_variable("str", "foobar".to_string());
assert_eq!(test_script(script, Some(ctx)), expected);
}
Expand Down
10 changes: 8 additions & 2 deletions interpreter/src/magic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use std::marker::PhantomData;
use std::rc::Rc;

impl_conversions!(
i32 => Value::Int,
u32 => Value::UInt,
i64 => Value::Int,
u64 => Value::UInt,
f64 => Value::Float,
Rc<String> => Value::String,
Rc<Vec<u8>> => Value::Bytes,
Expand All @@ -19,6 +19,12 @@ impl_conversions!(
Rc<Vec<Value>> => Value::List
);

impl From<i32> for Value {
fn from(value: i32) -> Self {
Value::Int(value as i64)
}
}

/// Describes any type that can be converted from a [`Value`] into itself.
/// This is commonly used to convert from [`Value`] into primitive types,
/// e.g. from `Value::Bool(true) -> true`. This trait is auto-implemented
Expand Down
16 changes: 8 additions & 8 deletions interpreter/src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ impl PartialOrd for Map {

#[derive(Debug, Eq, PartialEq, Hash, Ord, Clone, PartialOrd)]
pub enum Key {
Int(i32),
Uint(u32),
Int(i64),
Uint(u64),
Bool(bool),
String(Rc<String>),
}
Expand Down Expand Up @@ -56,14 +56,14 @@ impl From<bool> for Key {
}
}

impl From<i32> for Key {
fn from(v: i32) -> Self {
impl From<i64> for Key {
fn from(v: i64) -> Self {
Key::Int(v)
}
}

impl From<u32> for Key {
fn from(v: u32) -> Self {
impl From<u64> for Key {
fn from(v: u64) -> Self {
Key::Uint(v)
}
}
Expand Down Expand Up @@ -106,8 +106,8 @@ pub enum Value {
Function(Rc<String>, Option<Box<Value>>),

// Atoms
Int(i32),
UInt(u32),
Int(i64),
UInt(u64),
Float(f64),
String(Rc<String>),
Bytes(Rc<Vec<u8>>),
Expand Down
4 changes: 2 additions & 2 deletions parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ pub enum Member {

#[derive(Debug, PartialEq, Clone)]
pub enum Atom {
Int(i32),
UInt(u32),
Int(i64),
UInt(u64),
Float(f64),
String(Rc<String>),
Bytes(Rc<Vec<u8>>),
Expand Down
4 changes: 2 additions & 2 deletions parser/src/cel.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ RelationOp: RelationOp = {
Atom: Atom = {
// Integer literals. Annoying to parse :/
r"-?[0-9]+" => Atom::Int(<>.parse().unwrap()),
r"-?0[xX]([0-9a-fA-F]+)" => Atom::Int(i32::from_str_radix(<>, 16).unwrap()),
r"-?0[xX]([0-9a-fA-F]+)" => Atom::Int(i64::from_str_radix(<>, 16).unwrap()),
r"-?[0-9]+ [uU]" => Atom::UInt(<>.parse().unwrap()),
r"-?0[xX]([0-9a-fA-F]+) [uU]" => Atom::UInt(u32::from_str_radix(<>, 16).unwrap()),
r"-?0[xX]([0-9a-fA-F]+) [uU]" => Atom::UInt(u64::from_str_radix(<>, 16).unwrap()),

// Float with decimals and optional exponent
r"([-+]?[0-9]*\.[0-9]+([eE][-+]?[0-9]+)?)" => Atom::Float(<>.parse().unwrap()),
Expand Down