Grackle: compile KCL bools to EP bools (#1318)
This commit is contained in:
		@ -9,6 +9,7 @@ pub enum KclValueGroup {
 | 
			
		||||
    ObjectExpression(Box<ast::types::ObjectExpression>),
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Debug)]
 | 
			
		||||
pub enum SingleValue {
 | 
			
		||||
    Literal(Box<ast::types::Literal>),
 | 
			
		||||
    Identifier(Box<ast::types::Identifier>),
 | 
			
		||||
 | 
			
		||||
@ -122,7 +122,27 @@ impl Planner {
 | 
			
		||||
                })
 | 
			
		||||
            }
 | 
			
		||||
            SingleValue::Identifier(expr) => {
 | 
			
		||||
                // This is just duplicating a binding.
 | 
			
		||||
                // The KCL parser interprets bools as identifiers.
 | 
			
		||||
                // Consider changing them to be KCL literals instead.
 | 
			
		||||
                let b = if expr.name == "true" {
 | 
			
		||||
                    Some(true)
 | 
			
		||||
                } else if expr.name == "false" {
 | 
			
		||||
                    Some(false)
 | 
			
		||||
                } else {
 | 
			
		||||
                    None
 | 
			
		||||
                };
 | 
			
		||||
                if let Some(b) = b {
 | 
			
		||||
                    let address = self.next_addr.offset_by(1);
 | 
			
		||||
                    return Ok(EvalPlan {
 | 
			
		||||
                        instructions: vec![Instruction::SetPrimitive {
 | 
			
		||||
                            address,
 | 
			
		||||
                            value: ept::Primitive::Bool(b),
 | 
			
		||||
                        }],
 | 
			
		||||
                        binding: EpBinding::Single(address),
 | 
			
		||||
                    });
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                // This identifier is just duplicating a binding.
 | 
			
		||||
                // So, don't emit any instructions, because the value has already been computed.
 | 
			
		||||
                // Just return the address that it was stored at after being computed.
 | 
			
		||||
                let previously_bound_to = self
 | 
			
		||||
 | 
			
		||||
@ -128,6 +128,23 @@ fn name_not_found() {
 | 
			
		||||
    assert_eq!(err, CompileError::Undefined { name: "y".to_owned() });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[test]
 | 
			
		||||
fn assign_bool() {
 | 
			
		||||
    // Check that Grackle properly compiles KCL bools to EP bools.
 | 
			
		||||
    for (str, val) in [("true", true), ("false", false)] {
 | 
			
		||||
        let program = format!("let x = {str}");
 | 
			
		||||
        let (plan, scope) = must_plan(&program);
 | 
			
		||||
        assert_eq!(
 | 
			
		||||
            plan,
 | 
			
		||||
            vec![Instruction::SetPrimitive {
 | 
			
		||||
                address: Address::ZERO,
 | 
			
		||||
                value: val.into(),
 | 
			
		||||
            }]
 | 
			
		||||
        );
 | 
			
		||||
        assert_eq!(scope.get("x"), Some(&EpBinding::Single(Address::ZERO)));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[test]
 | 
			
		||||
fn aliases() {
 | 
			
		||||
    let program = "
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user