feat!: add Rego Virtual Machine (RVM) implementation (#495)

* feat!: add Rego Virtual Machine (RVM) implementation

This commit introduces a register-based virtual machine for executing Rego
policies with bytecode-style instructions. Unlike the existing tree-walking
interpreter, the RVM compiles policies into instruction sequences that operate
on virtual registers, offering better performance and optimization potential.

Core Components:

Instruction Set Architecture:
- Define instruction types for data operations, control flow, and builtins
- Implement instruction parameter encoding and display formatting
- Add instruction parser with comprehensive test coverage

Virtual Machine Engine:
- Register-based execution model with program counter management
- Loop execution supporting iterators, comprehensions, and quantifiers
- Function call handling with argument evaluation and context management
- Rule evaluation with default value resolution and virtual data support
- Arithmetic and comparison operation implementations

Program Representation:
- Program listing builder with instruction sequencing
- Rule tree construction for organizing policy rules
- Binary and JSON serialization for compiled programs
- Recompilation support for program modification

Testing Infrastructure:
- Extensive YAML test suites covering all VM features
- Rust unit tests for VM execution and instruction parsing
- Test suites for loops, comprehensions, builtins, and control flow

BREAKING CHANGE: Introduces new VM execution path alongside interpreter

Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>

* docs: add detailed RVM architecture references

Introduce architecture.md explaining program artifacts, serialization, and runtime subsystems.
Document the full opcode catalog in instruction-set.md, including operands, parameter tables, and outcomes.
Walk through execution flow, stacks, and operational guidance in vm-runtime.md, tying the runtime to the new architecture docs.

Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>

---------

Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
This commit is contained in:
Anand Krishnamoorthi
2025-11-14 11:43:19 -06:00
committed by GitHub
parent 6dc505c88b
commit 49bd3c22f3
89 changed files with 19158 additions and 313 deletions
@@ -0,0 +1,160 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# Array Comprehension Test Suite
# Tests array comprehensions - collect transformed values based on conditions
# Corresponds to Rego's "[transform | condition]" patterns
cases:
- note: array_simple_transform
description: Simple array comprehension with transformation
example_rego: |
# Transform array elements by doubling them
[x * 2 | x := [1, 2, 3][_]] # [2, 4, 6]
literals:
- 1
- 2
- 3
- 2 # multiplier
instruction_params:
comprehension_begin_params:
- mode: "Array"
collection_reg: 7
key_reg: 4
value_reg: 5
body_start: 8
comprehension_end: 11
loop_params:
- mode: "ForEach"
collection: 0
key_reg: 4
value_reg: 5
result_reg: 8
body_start: 9
loop_end: 12
instructions:
- "ArrayNew { dest: 0 }" # Create input array [1, 2, 3] in register 0
- "Load { dest: 1, literal_idx: 0 }" # Load 1 into register 1
- "ArrayPush { arr: 0, value: 1 }" # Push 1 to array
- "Load { dest: 2, literal_idx: 1 }" # Load 2 into register 2
- "ArrayPush { arr: 0, value: 2 }" # Push 2 to array
- "Load { dest: 3, literal_idx: 2 }" # Load 3 into register 3
- "ArrayPush { arr: 0, value: 3 }" # Push 3 to array
- "ComprehensionBegin { params_index: 0 }" # Start array comprehension and initialize result in register 7
- "LoopStart { params_index: 0 }" # Start loop over array elements
- "Load { dest: 6, literal_idx: 3 }" # Load multiplier 2 into register 6
- "Mul { dest: 9, left: 5, right: 6 }" # Multiply current value by 2, store result in register 9
- "ComprehensionYield { value_reg: 9 }" # Add transformed value to comprehension
- "LoopNext { body_start: 9, loop_end: 12 }" # Continue to next iteration or exit
- "Return { value: 7 }" # Return comprehension collection
want_result: [2, 4, 6]
- note: array_empty_input
description: Array comprehension with empty input
example_rego: |
# Transform empty array
[x + 5 | x := [][_]] # [] (empty array)
literals:
- 5 # addend
instruction_params:
comprehension_begin_params:
- mode: "Array"
collection_reg: 7
key_reg: 4
value_reg: 5
body_start: 2
comprehension_end: 6
loop_params:
- mode: "ForEach"
collection: 0
key_reg: 4
value_reg: 5
result_reg: 8
body_start: 3
loop_end: 6
instructions:
- "ArrayNew { dest: 0 }" # Create empty input array in register 0
- "ComprehensionBegin { params_index: 0 }" # Start array comprehension and initialize result in register 7
- "LoopStart { params_index: 0 }" # Start loop over array elements
- "Load { dest: 6, literal_idx: 0 }" # Load addend 5 into register 6
- "Add { dest: 8, left: 5, right: 6 }" # Add 5 to current value, store result in register 8
- "ComprehensionYield { value_reg: 8 }" # Add transformed value to comprehension
- "LoopNext { body_start: 3, loop_end: 6 }" # Continue to next iteration or exit
- "Return { value: 7 }" # Return comprehension collection
want_result: []
- note: array_single_element
description: Array comprehension with single element
example_rego: |
# Transform single element array
[x - 1 | x := [10][_]] # [9]
literals:
- 10
- 1 # subtrahend
instruction_params:
comprehension_begin_params:
- mode: "Array"
collection_reg: 7
key_reg: 4
value_reg: 5
body_start: 4
comprehension_end: 8
loop_params:
- mode: "ForEach"
collection: 0
key_reg: 4
value_reg: 5
result_reg: 8
body_start: 5
loop_end: 8
instructions:
- "ArrayNew { dest: 0 }" # Create input array [10] in register 0
- "Load { dest: 1, literal_idx: 0 }" # Load 10 into register 1
- "ArrayPush { arr: 0, value: 1 }" # Push 10 to array
- "ComprehensionBegin { params_index: 0 }" # Start array comprehension and initialize result in register 7
- "LoopStart { params_index: 0 }" # Start loop over array elements
- "Load { dest: 6, literal_idx: 1 }" # Load subtrahend 1 into register 6
- "Sub { dest: 9, left: 5, right: 6 }" # Subtract 1 from current value, store result in register 9
- "ComprehensionYield { value_reg: 9 }" # Add transformed value to comprehension
- "LoopNext { body_start: 5, loop_end: 8 }" # Continue to next iteration or exit
- "Return { value: 7 }" # Return comprehension collection
want_result: [9]
- note: array_with_null_values
description: Array comprehension with null value handling
example_rego: |
# Process array with null values - nulls are preserved
[x | x := [1, null, 3][_]] # [1, null, 3]
literals:
- 1
- 3
instruction_params:
comprehension_begin_params:
- mode: "Array"
collection_reg: 7
key_reg: 4
value_reg: 5
body_start: 8
comprehension_end: 11
loop_params:
- mode: "ForEach"
collection: 0
key_reg: 4
value_reg: 5
result_reg: 8
body_start: 9
loop_end: 11
instructions:
- "ArrayNew { dest: 0 }" # Create input array in register 0
- "Load { dest: 1, literal_idx: 0 }" # Load 1
- "ArrayPush { arr: 0, value: 1 }" # Push 1 to array
- "LoadNull { dest: 2 }" # Load null value
- "ArrayPush { arr: 0, value: 2 }" # Push null to array
- "Load { dest: 3, literal_idx: 1 }" # Load 3
- "ArrayPush { arr: 0, value: 3 }" # Push 3 to array
- "ComprehensionBegin { params_index: 0 }" # Start array comprehension and initialize result in register 7
- "LoopStart { params_index: 0 }" # Start loop over array elements
- "ComprehensionYield { value_reg: 5 }" # Add current value (including null) to comprehension
- "LoopNext { body_start: 9, loop_end: 11 }" # Continue to next iteration or exit
- "Return { value: 7 }" # Return comprehension collection
want_result: [1, null, 3]
+295
View File
@@ -0,0 +1,295 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# Empty Collections Loop Test Suite
# Tests behavior of various loop types over empty collections
# 1. Comprehensions should evaluate to their empty versions
# 2. some..in should evaluate to false
# 3. every should evaluate to true
cases:
- note: existential_empty_array
description: Existential quantification (some) on empty array should return false
example_rego: |
# some x in []
# x > 0 # false - no elements to satisfy condition
literals:
- {}
- 0 # comparison value
instruction_params:
loop_params:
- mode: "Any"
collection: 0
key_reg: 4
value_reg: 5
body_start: 2
loop_end: 6
result_reg: 6
instructions:
- "ArrayNew { dest: 0 }" # Create empty array in register 0
- "LoopStart { params_index: 0 }"
- "Load { dest: 7, literal_idx: 1 }" # Load comparison value 0
- "Gt { dest: 8, left: 5, right: 7 }" # Check if current value > 0
- "AssertCondition { condition: 8 }" # Assert the condition
- "LoopNext { body_start: 2, loop_end: 6 }"
- "Return { value: 6 }" # Return false for empty collection
want_result: false
- note: universal_empty_array
description: Universal quantification (every) on empty array should return true
example_rego: |
# every x in []
# x > 0 # true - vacuously true (no elements to violate condition)
literals:
- {}
- 0 # comparison value
instruction_params:
loop_params:
- mode: "Every"
collection: 0
key_reg: 4
value_reg: 5
result_reg: 6
body_start: 2
loop_end: 6
instructions:
- "ArrayNew { dest: 0 }" # Create empty array in register 0
- "LoopStart { params_index: 0 }"
- "Load { dest: 7, literal_idx: 1 }" # Load comparison value 0
- "Gt { dest: 8, left: 5, right: 7 }" # Check if current value > 0
- "AssertCondition { condition: 8 }" # Assert the condition
- "LoopNext { body_start: 2, loop_end: 6 }"
- "Return { value: 6 }" # Return true for empty collection (vacuously true)
want_result: true
- note: array_comprehension_empty
description: Array comprehension on empty collection should return empty array
example_rego: |
# [x + 1 | x = []; true] # []
# Transform each element by adding 1
literals:
- {}
- 1 # value to add
instruction_params:
comprehension_begin_params:
- mode: "Array"
collection_reg: 1
key_reg: 4
value_reg: 5
body_start: 3
comprehension_end: 9
loop_params:
- mode: "ForEach"
collection: 0
key_reg: 4
value_reg: 5
result_reg: 8
body_start: 4
loop_end: 9
instructions:
- "ArrayNew { dest: 0 }" # Create empty input array in register 0
- "ComprehensionBegin { params_index: 0 }" # Start array comprehension
- "LoopStart { params_index: 0 }" # Start loop over array elements
- "Load { dest: 6, literal_idx: 1 }" # Load 1 into register 6
- "Add { dest: 7, left: 5, right: 6 }" # Add 1 to current value
- "ComprehensionYield { value_reg: 7 }" # Add result to comprehension
- "LoadBool { dest: 8, value: true }" # Load true (condition always passes)
- "AssertCondition { condition: 8 }" # Assert true condition
- "LoopNext { body_start: 4, loop_end: 9 }" # Continue to next iteration or exit
- "Return { value: 1 }" # Return the result array (should be empty)
want_result: []
- note: set_comprehension_empty
description: Set comprehension on empty collection should return empty set
example_rego: |
# {x + 1 | x = []; true} # set()
# Transform each element by adding 1 into a set
literals:
- {}
- 1 # value to add
instruction_params:
comprehension_begin_params:
- mode: "Set"
collection_reg: 1
key_reg: 4
value_reg: 5
body_start: 3
comprehension_end: 9
loop_params:
- mode: "ForEach"
collection: 0
key_reg: 4
value_reg: 5
result_reg: 8
body_start: 4
loop_end: 9
instructions:
- "ArrayNew { dest: 0 }" # Create empty input array in register 0
- "ComprehensionBegin { params_index: 0 }" # Start set comprehension
- "LoopStart { params_index: 0 }" # Start loop over array elements
- "Load { dest: 6, literal_idx: 1 }" # Load 1 into register 6
- "Add { dest: 7, left: 5, right: 6 }" # Add 1 to current value
- "ComprehensionYield { value_reg: 7 }" # Add result to comprehension
- "LoadBool { dest: 8, value: true }" # Load true (condition always passes)
- "AssertCondition { condition: 8 }" # Assert true condition
- "LoopNext { body_start: 4, loop_end: 9 }" # Continue to next iteration or exit
- "Return { value: 1 }" # Return the result set (should be empty)
want_result:
set!: [] # Set serializes as empty array
- note: object_comprehension_empty
description: Object comprehension on empty collection should return empty object
example_rego: |
# {k: v + 1 | some k, v in {}; true} # {}
# Transform each key-value pair
literals:
- {}
- 1 # value to add
instruction_params:
object_create_params:
- dest: 0
template_literal_idx: 0
literal_key_fields: []
fields: []
comprehension_begin_params:
- mode: "Object"
collection_reg: 1
key_reg: 4
value_reg: 5
body_start: 3
comprehension_end: 9
loop_params:
- mode: "ForEach"
collection: 0
key_reg: 4
value_reg: 5
result_reg: 8
body_start: 4
loop_end: 9
instructions:
- "ObjectCreate { params_index: 0 }"
- "ComprehensionBegin { params_index: 0 }" # Start object comprehension
- "LoopStart { params_index: 0 }" # Start loop over object elements
- "Load { dest: 6, literal_idx: 1 }" # Load 1 into register 6
- "Add { dest: 7, left: 5, right: 6 }" # Add 1 to current value
- "ComprehensionYield { value_reg: 7 }" # Add result to comprehension (object comprehension needs special handling)
- "LoadBool { dest: 8, value: true }" # Load true (condition always passes)
- "AssertCondition { condition: 8 }" # Assert true condition
- "LoopNext { body_start: 4, loop_end: 9 }" # Continue to next iteration or exit
- "Return { value: 1 }" # Return the result object (should be empty)
want_result: {}
- note: existential_empty_set
description: Existential quantification on empty set should return false
example_rego: |
# some x in set()
# x > 0 # false - no elements in set
literals:
- {}
- 0 # comparison value
instruction_params:
loop_params:
- mode: "Any"
collection: 0
key_reg: 4
value_reg: 5
result_reg: 6
body_start: 2
loop_end: 6
instructions:
- "SetNew { dest: 0 }" # Create empty set in register 0
- "LoopStart { params_index: 0 }"
- "Load { dest: 7, literal_idx: 1 }" # Load comparison value 0
- "Gt { dest: 8, left: 5, right: 7 }" # Check if current value > 0
- "AssertCondition { condition: 8 }" # Assert the condition
- "LoopNext { body_start: 2, loop_end: 6 }"
- "Return { value: 6 }" # Return false for empty set
want_result: false
- note: universal_empty_object
description: Universal quantification on empty object should return true
example_rego: |
# every k, v in {}
# v > 0 # true - vacuously true (no key-value pairs to violate condition)
literals:
- {}
- 0 # comparison value
instruction_params:
object_create_params:
- dest: 0
template_literal_idx: 0
literal_key_fields: []
fields: []
loop_params:
- mode: "Every"
collection: 0
key_reg: 4
value_reg: 5
result_reg: 6
body_start: 2
loop_end: 6
instructions:
- "ObjectCreate { params_index: 0 }"
- "LoopStart { params_index: 0 }"
- "Load { dest: 7, literal_idx: 1 }" # Load comparison value 0
- "Gt { dest: 8, left: 5, right: 7 }" # Check if current value > 0
- "AssertCondition { condition: 8 }" # Assert the condition
- "LoopNext { body_start: 2, loop_end: 6 }"
- "Return { value: 6 }" # Return true for empty object (vacuously true)
want_result: true
- note: nested_empty_comprehensions
description: Nested comprehensions with empty collections
example_rego: |
# [[y | y = []; true] | x = []; true] # []
# Nested array comprehension where both inner and outer collections are empty
literals: []
instruction_params:
comprehension_begin_params:
- mode: "Array"
collection_reg: 0
key_reg: 2
value_reg: 3
body_start: 3
comprehension_end: 16
- mode: "Array"
collection_reg: 6
key_reg: 8
value_reg: 9
body_start: 7
comprehension_end: 12
loop_params:
- mode: "ForEach"
collection: 0
key_reg: 2
value_reg: 3
result_reg: 10
body_start: 4
loop_end: 16
- mode: "ForEach"
collection: 6
key_reg: 8
value_reg: 9
result_reg: 11
body_start: 8
loop_end: 12
instructions:
- "ArrayNew { dest: 0 }" # Create empty outer array in register 0
- "ArrayNew { dest: 1 }" # Create empty result array in register 1
- "ComprehensionBegin { params_index: 0 }"
- "LoopStart { params_index: 0 }" # Start outer loop
# Inner array comprehension (for each x in outer empty array)
- "ArrayNew { dest: 6 }" # Create empty inner array in register 6
- "ArrayNew { dest: 7 }" # Create result for inner comprehension in register 7
- "ComprehensionBegin { params_index: 1 }"
- "LoopStart { params_index: 1 }" # Start inner loop
- "ComprehensionYield { value_reg: 9 }" # Push inner value to inner result (never executes)
- "LoadBool { dest: 10, value: true }" # Load true
- "AssertCondition { condition: 10 }" # Assert true condition for inner loop
- "LoopNext { body_start: 8, loop_end: 12 }" # Continue inner loop
- "ComprehensionYield { value_reg: 7 }" # Push inner result to outer result
- "LoadBool { dest: 11, value: true }" # Load true
- "AssertCondition { condition: 11 }" # Assert true condition for outer loop
- "LoopNext { body_start: 4, loop_end: 16 }" # Continue outer loop
- "Return { value: 1 }" # Return the nested result (should be empty)
want_result: []
+174
View File
@@ -0,0 +1,174 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# Existential Loops Test Suite (some)
# Tests existential quantification loops - succeed if ANY element satisfies the condition
# Corresponds to Rego's "some x in collection; condition" patterns
cases:
- note: existential_basic_some
description: Basic existential quantification - some element satisfies condition
example_rego: |
# Check if any element in array is greater than 2
some x in [1, 2, 3]
x > 2 # true (3 > 2)
literals:
- 1
- 2
- 3
- 2 # comparison value
instruction_params:
loop_params:
- mode: "Existential"
collection: 0
key_reg: 4
value_reg: 5
result_reg: 6
body_start: 8
loop_end: 12
instructions:
- "ArrayNew { dest: 0 }" # Create input array [1, 2, 3] in register 0
- "Load { dest: 1, literal_idx: 0 }" # Load 1 into register 1
- "ArrayPush { arr: 0, value: 1 }" # Push 1 to array
- "Load { dest: 2, literal_idx: 1 }" # Load 2 into register 2
- "ArrayPush { arr: 0, value: 2 }" # Push 2 to array
- "Load { dest: 3, literal_idx: 2 }" # Load 3 into register 3
- "ArrayPush { arr: 0, value: 3 }" # Push 3 to array
- "LoopStart { params_index: 0 }" # Start existential loop using parameter table index 0
- "Load { dest: 7, literal_idx: 3 }" # Load comparison value 2 into register 7
- "Gt { dest: 8, left: 5, right: 7 }" # Check if current value > 2
- "AssertCondition { condition: 8 }" # Assert the condition result for existential logic
- "LoopNext { body_start: 8, loop_end: 12 }" # Continue to next iteration or exit early if condition met
- "Return { value: 6 }" # Return result (true if any element satisfied condition)
want_result: true
- note: existential_none_satisfy
description: Existential quantification where no element satisfies condition
example_rego: |
# Check if any element in array is greater than 5
some x in [1, 2]
x > 5 # false (no element > 5)
literals:
- 1
- 2
- 5 # comparison value
instruction_params:
loop_params:
- mode: "Existential"
collection: 0
key_reg: 4
value_reg: 5
result_reg: 6
body_start: 6
loop_end: 10
instructions:
- "ArrayNew { dest: 0 }" # Create input array [1, 2] in register 0
- "Load { dest: 1, literal_idx: 0 }" # Load 1 into register 1
- "ArrayPush { arr: 0, value: 1 }" # Push 1 to array
- "Load { dest: 2, literal_idx: 1 }" # Load 2 into register 2
- "ArrayPush { arr: 0, value: 2 }" # Push 2 to array
- "LoopStart { params_index: 0 }" # Start existential loop using parameter table index 0
- "Load { dest: 7, literal_idx: 2 }" # Load comparison value 5 into register 7
- "Gt { dest: 8, left: 5, right: 7 }" # Check if current value > 5
- "AssertCondition { condition: 8 }" # Assert the condition result for existential logic
- "LoopNext { body_start: 6, loop_end: 10 }" # Continue to next iteration
- "Return { value: 6 }" # Return result (false since no element satisfied condition)
want_result: false
- note: existential_empty_collection
description: Existential quantification on empty collection
example_rego: |
# Check if any element in empty array satisfies condition
some x in []
x > 0 # false (no elements to check)
literals:
- 0 # comparison value
instruction_params:
loop_params:
- mode: "Existential"
collection: 0
key_reg: 4
value_reg: 5
result_reg: 6
body_start: 2
loop_end: 6
instructions:
- "ArrayNew { dest: 0 }" # Create empty input array in register 0
- "LoopStart { params_index: 0 }" # Start existential loop using parameter table index 0
- "Load { dest: 7, literal_idx: 0 }" # Load comparison value 0 into register 7
- "Gt { dest: 8, left: 5, right: 7 }" # Check if current value > 0
- "AssertCondition { condition: 8 }" # Assert the condition result for existential logic
- "LoopNext { body_start: 2, loop_end: 6 }" # Continue to next iteration
- "Return { value: 6 }" # Return result (false for empty collection)
want_result: false
- note: existential_simplified_arrays
description: Existential quantification with simple array test
example_rego: |
# Check if any element in array is greater than 5
# Simplified version: check if [3, 7, 4] contains element > 5
some x in [3, 7, 4]
x > 5 # true (7 > 5)
literals:
- 3
- 7
- 4
- 5 # comparison value
instruction_params:
loop_params:
- mode: "Existential"
collection: 0
key_reg: 4
value_reg: 5
result_reg: 6
body_start: 8
loop_end: 12
instructions:
- "ArrayNew { dest: 0 }" # Create array [3, 7, 4] in register 0
- "Load { dest: 1, literal_idx: 0 }" # Load 3
- "ArrayPush { arr: 0, value: 1 }" # Push 3 to array
- "Load { dest: 2, literal_idx: 1 }" # Load 7
- "ArrayPush { arr: 0, value: 2 }" # Push 7 to array
- "Load { dest: 3, literal_idx: 2 }" # Load 4
- "ArrayPush { arr: 0, value: 3 }" # Push 4 to array
- "LoopStart { params_index: 0 }" # Start existential loop using parameter table index 0
- "Load { dest: 7, literal_idx: 3 }" # Load comparison value 5
- "Gt { dest: 8, left: 5, right: 7 }" # Check if current value > 5
- "AssertCondition { condition: 8 }" # Assert the condition for existential logic
- "LoopNext { body_start: 8, loop_end: 12 }" # Continue to next iteration
- "Return { value: 6 }" # Return result
want_result: true
- note: some_basic_failure
description: Basic existential loop that fails
example_rego: "some x in [1, 2, 3]; x > 5" # false because no element > 5
literals:
- 1
- 2
- 3
- 5 # comparison value
instruction_params:
loop_params:
- mode: "Existential"
collection: 0
key_reg: 4
value_reg: 5
result_reg: 6
body_start: 8
loop_end: 12
instructions:
- "ArrayNew { dest: 0 }" # Create array [1, 2, 3] in register 0
- "Load { dest: 1, literal_idx: 0 }" # Load 1 into register 1
- "ArrayPush { arr: 0, value: 1 }" # Push 1 to array
- "Load { dest: 2, literal_idx: 1 }" # Load 2 into register 2
- "ArrayPush { arr: 0, value: 2 }" # Push 2 to array
- "Load { dest: 3, literal_idx: 2 }" # Load 3 into register 3
- "ArrayPush { arr: 0, value: 3 }" # Push 3 to array
- "LoopStart { params_index: 0 }" # Start existential loop using parameter table index 0
- "Load { dest: 7, literal_idx: 3 }" # Load comparison value 5 into register 7
- "Gt { dest: 8, left: 5, right: 7 }" # Check if current value > 5, store result in register 8
- "AssertCondition { condition: 8 }" # Assert the condition (fails for all elements)
- "LoopNext { body_start: 8, loop_end: 12 }" # Continue to next iteration or exit
- "Return { value: 6 }" # Return boolean result from loop
want_result: false
@@ -0,0 +1,223 @@
# Advanced Loop and Comprehension Interaction Suite
# Exercises nested quantifiers inside comprehensions and object key/value emission.
cases:
- note: array_comprehension_filters_with_inner_any
description: Array comprehension keeps only members whose nested array passes an Any loop
example_rego: |
[arr |
arr := [[1, 0], [1, 2]][_];
some v in arr; v == 0
]
literals:
- 1
- 0
- 2
instruction_params:
comprehension_begin_params:
- mode: "Array"
collection_reg: 7
result_reg: 7
key_reg: 10
value_reg: 11
body_start: 14
comprehension_end: 23
loop_params:
- mode: "ForEach"
collection: 0
key_reg: 10
value_reg: 11
result_reg: 12
body_start: 15
loop_end: 23
- mode: "Any"
collection: 11
key_reg: 13
value_reg: 14
result_reg: 15
body_start: 16
loop_end: 21
instructions:
- "ArrayNew { dest: 0 }"
- "ArrayNew { dest: 1 }"
- "Load { dest: 2, literal_idx: 0 }"
- "ArrayPush { arr: 1, value: 2 }"
- "Load { dest: 3, literal_idx: 1 }"
- "ArrayPush { arr: 1, value: 3 }"
- "ArrayPush { arr: 0, value: 1 }"
- "ArrayNew { dest: 4 }"
- "Load { dest: 5, literal_idx: 0 }"
- "ArrayPush { arr: 4, value: 5 }"
- "Load { dest: 6, literal_idx: 2 }"
- "ArrayPush { arr: 4, value: 6 }"
- "ArrayPush { arr: 0, value: 4 }"
- "ComprehensionBegin { params_index: 0 }"
- "LoopStart { params_index: 0 }"
- "LoopStart { params_index: 1 }"
- "Load { dest: 16, literal_idx: 1 }"
- "Eq { dest: 17, left: 14, right: 16 }"
- "AssertCondition { condition: 17 }"
- "ComprehensionYield { value_reg: 11 }"
- "LoopNext { body_start: 16, loop_end: 21 }"
- "LoopNext { body_start: 15, loop_end: 23 }"
- "ComprehensionEnd"
- "Return { value: 7 }"
want_result:
- [1, 0]
- note: array_comprehension_requires_inner_every
description: Array comprehension keeps only members whose nested array passes an Every loop
example_rego: |
[arr |
arr := [[1, 1], [-1, 2], [2, 3]][_];
every v in arr; v > 0
]
literals:
- 1
- -1
- 2
- 3
- 0
instruction_params:
comprehension_begin_params:
- mode: "Array"
collection_reg: 9
result_reg: 9
key_reg: 10
value_reg: 11
body_start: 20
comprehension_end: 29
loop_params:
- mode: "ForEach"
collection: 0
key_reg: 10
value_reg: 11
result_reg: 12
body_start: 20
loop_end: 29
- mode: "ForEach"
collection: 11
key_reg: 13
value_reg: 14
result_reg: 18
body_start: 22
loop_end: 26
instructions:
- "ArrayNew { dest: 0 }"
- "ArrayNew { dest: 1 }"
- "Load { dest: 2, literal_idx: 0 }"
- "ArrayPush { arr: 1, value: 2 }"
- "ArrayPush { arr: 1, value: 2 }"
- "ArrayPush { arr: 0, value: 1 }"
- "ArrayNew { dest: 3 }"
- "Load { dest: 4, literal_idx: 1 }"
- "ArrayPush { arr: 3, value: 4 }"
- "Load { dest: 5, literal_idx: 2 }"
- "ArrayPush { arr: 3, value: 5 }"
- "ArrayPush { arr: 0, value: 3 }"
- "ArrayNew { dest: 6 }"
- "Load { dest: 7, literal_idx: 2 }"
- "ArrayPush { arr: 6, value: 7 }"
- "Load { dest: 8, literal_idx: 3 }"
- "ArrayPush { arr: 6, value: 8 }"
- "ArrayPush { arr: 0, value: 6 }"
- "ComprehensionBegin { params_index: 0 }"
- "LoopStart { params_index: 0 }"
- "LoadTrue { dest: 15 }"
- "LoopStart { params_index: 1 }"
- "Load { dest: 16, literal_idx: 4 }"
- "Gt { dest: 17, left: 14, right: 16 }"
- "And { dest: 15, left: 15, right: 17 }"
- "LoopNext { body_start: 22, loop_end: 26 }"
- "AssertCondition { condition: 15 }"
- "ComprehensionYield { value_reg: 11 }"
- "LoopNext { body_start: 20, loop_end: 29 }"
- "ComprehensionEnd"
- "Return { value: 9 }"
want_result:
- [1, 1]
- [2, 3]
- note: object_comprehension_with_nested_any
description: Object comprehension emits keys only when nested Any loop succeeds
example_rego: |
{ entry[0]: true |
entry := [["a", [1, 2]], ["b", [1]]][_];
some v in entry[1]; v % 2 == 0
}
literals:
- {}
- "a"
- 1
- 2
- "b"
- 0
instruction_params:
object_create_params:
- dest: 12
template_literal_idx: 0
literal_key_fields: []
fields: []
comprehension_begin_params:
- mode: "Object"
collection_reg: 12
result_reg: 12
key_reg: 17
value_reg: 22
body_start: 21
comprehension_end: 36
loop_params:
- mode: "ForEach"
collection: 0
key_reg: 13
value_reg: 14
result_reg: 15
body_start: 22
loop_end: 36
- mode: "Any"
collection: 19
key_reg: 20
value_reg: 21
result_reg: 22
body_start: 27
loop_end: 33
instructions:
- "ArrayNew { dest: 0 }"
- "ArrayNew { dest: 1 }"
- "Load { dest: 2, literal_idx: 1 }"
- "ArrayPush { arr: 1, value: 2 }"
- "ArrayNew { dest: 3 }"
- "Load { dest: 4, literal_idx: 2 }"
- "ArrayPush { arr: 3, value: 4 }"
- "Load { dest: 5, literal_idx: 3 }"
- "ArrayPush { arr: 3, value: 5 }"
- "ArrayPush { arr: 1, value: 3 }"
- "ArrayPush { arr: 0, value: 1 }"
- "ArrayNew { dest: 6 }"
- "Load { dest: 7, literal_idx: 4 }"
- "ArrayPush { arr: 6, value: 7 }"
- "ArrayNew { dest: 8 }"
- "Load { dest: 9, literal_idx: 2 }"
- "ArrayPush { arr: 8, value: 9 }"
- "ArrayPush { arr: 6, value: 8 }"
- "ArrayPush { arr: 0, value: 6 }"
- "ObjectCreate { params_index: 0 }"
- "ComprehensionBegin { params_index: 0 }"
- "LoopStart { params_index: 0 }"
- "Load { dest: 16, literal_idx: 5 }"
- "Index { dest: 17, container: 14, key: 16 }"
- "Load { dest: 18, literal_idx: 2 }"
- "Index { dest: 19, container: 14, key: 18 }"
- "LoopStart { params_index: 1 }"
- "Load { dest: 23, literal_idx: 3 }"
- "Mod { dest: 24, left: 21, right: 23 }"
- "Load { dest: 25, literal_idx: 5 }"
- "Eq { dest: 26, left: 24, right: 25 }"
- "AssertCondition { condition: 26 }"
- "LoopNext { body_start: 27, loop_end: 33 }"
- "AssertCondition { condition: 22 }"
- "ComprehensionYield { value_reg: 22, key_reg: 17 }"
- "LoopNext { body_start: 22, loop_end: 36 }"
- "ComprehensionEnd"
- "Return { value: 12 }"
want_result: {"a": true}
+183
View File
@@ -0,0 +1,183 @@
name: "Nested Loops Test Suite"
description: "Test various combinations and levels of nesting for different looping constructs"
cases:
- note: "simple_nested_comprehension"
description: "Test simple nested array comprehension"
example_rego: |
[[x | x := [1, 2][_]] | _ := [1, 2][_]]
literals:
- 1
- 2
instruction_params:
comprehension_begin_params:
- mode: "Array"
collection_reg: 1
key_reg: 2
value_reg: 3
body_start: 6
comprehension_end: 23
- mode: "Array"
collection_reg: 11
key_reg: 12
value_reg: 13
body_start: 12
comprehension_end: 20
loop_params:
- mode: "ForEach"
collection: 6
key_reg: 7
value_reg: 8
result_reg: 9
body_start: 7
loop_end: 22
- mode: "ForEach"
collection: 16
key_reg: 17
value_reg: 18
result_reg: 19
body_start: 15
loop_end: 18
instructions:
- "ComprehensionBegin { params_index: 0 }" # array comprehension in r1, body: 4-21 (P0)
- "Load { dest: 4, literal_idx: 0 }" # Load literal: 1
- "Load { dest: 5, literal_idx: 1 }" # Load literal: 2
- "ArrayNew { dest: 6 }" # Create empty array r6
- "ArrayPush { arr: 6, value: 4 }" # Push r4 to r6
- "ArrayPush { arr: 6, value: 5 }" # Push r5 to r6 -> [1, 2]
- "LoopStart { params_index: 0 }" # foreach loop over r6, body: 8-20 (P0)
# Outer loop body starts here (index 7)
- "Move { dest: 10, src: 8 }" # Copy value from r8 to r10
- "ComprehensionBegin { params_index: 1 }" # array comprehension in r11, body: 10-18 (P1)
- "Load { dest: 14, literal_idx: 0 }" # Load literal: 1
- "Load { dest: 15, literal_idx: 1 }" # Load literal: 2
- "ArrayNew { dest: 16 }" # Create empty array r16
- "ArrayPush { arr: 16, value: 14 }" # Push r14 to r16
- "ArrayPush { arr: 16, value: 15 }" # Push r15 to r16 -> [1, 2]
- "LoopStart { params_index: 1 }" # foreach loop over r16, body: 14-17 (P1)
# Inner loop body starts here (index 16)
- "Move { dest: 20, src: 18 }" # Copy value from r18 to r20
- "ComprehensionYield { value_reg: 20 }" # Yield value to comprehension
- "LoopNext { body_start: 16, loop_end: 19 }" # continue → 16 or exit → 19
# Inner comprehension end (index 19)
- "ComprehensionEnd" # End comprehension block
- "ComprehensionYield { value_reg: 11 }" # Yield value to comprehension
- "LoopNext { body_start: 7, loop_end: 22 }" # continue → 7 or exit → 22
# Outer comprehension end (index 22)
- "ComprehensionEnd" # End comprehension block
- "Move { dest: 0, src: 1 }" # Copy value from r1 to r0
- "Return { value: 0 }" # Return value from r0
want_result: [[1, 2], [1, 2]]
- note: "some_nested_comprehension"
description: "Test some with nested array comprehension"
example_rego: |
[1, 2][_] == [x | x := [1, 2, 3][_]; x > 1][_]
literals:
- 1
- 2
- 3
instruction_params:
comprehension_begin_params:
- mode: "Array"
collection_reg: 12
result_reg: 12
key_reg: 10
value_reg: 11
body_start: 16
comprehension_end: 20
loop_params:
- mode: "Any"
collection: 0
key_reg: 7
value_reg: 8
result_reg: 9
body_start: 14
loop_end: 27
- mode: "ForEach"
collection: 3
key_reg: 10
value_reg: 11
result_reg: 21
body_start: 16
loop_end: 20
- mode: "Any"
collection: 12
key_reg: 13
value_reg: 14
result_reg: 15
body_start: 22
loop_end: 25
instructions:
- "ArrayNew { dest: 0 }" # Index 0: Build left array [1, 2]
- "Load { dest: 1, literal_idx: 0 }" # Index 1: Load literal 1
- "ArrayPush { arr: 0, value: 1 }" # Index 2
- "Load { dest: 2, literal_idx: 1 }" # Index 3: Load literal 2
- "ArrayPush { arr: 0, value: 2 }" # Index 4
- "ArrayNew { dest: 3 }" # Index 5: Build source array [1, 2, 3]
- "Load { dest: 4, literal_idx: 0 }" # Index 6
- "ArrayPush { arr: 3, value: 4 }" # Index 7
- "Load { dest: 5, literal_idx: 1 }" # Index 8
- "ArrayPush { arr: 3, value: 5 }" # Index 9
- "Load { dest: 6, literal_idx: 2 }" # Index 10
- "ArrayPush { arr: 3, value: 6 }" # Index 11
- "Load { dest: 16, literal_idx: 0 }" # Index 12: Load 1 for comparison (stays stable)
- "LoopStart { params_index: 0 }" # Index 13: Start outer some loop over [1, 2]
- "ComprehensionBegin { params_index: 0 }" # Index 14: Build filtered comprehension
- "LoopStart { params_index: 1 }" # Index 15: Iterate source values for comprehension
- "Gt { dest: 17, left: 11, right: 16 }" # Index 16: Check x > 1
- "AssertCondition { condition: 17 }" # Index 17: Skip values <= 1
- "ComprehensionYield { value_reg: 11 }" # Index 18: Include qualifying value
- "LoopNext { body_start: 16, loop_end: 20 }" # Index 19: Continue comprehension loop
- "ComprehensionEnd" # Index 20: Finish comprehension block
- "LoopStart { params_index: 2 }" # Index 21: Iterate comprehension results
- "Eq { dest: 18, left: 8, right: 14 }" # Index 22: Compare outer value with result value
- "AssertCondition { condition: 18 }" # Index 23: Success when values match
- "LoopNext { body_start: 22, loop_end: 25 }" # Index 24: Continue inner any loop
- "AssertCondition { condition: 15 }" # Index 25: Require some match from inner any loop
- "LoopNext { body_start: 14, loop_end: 27 }" # Index 26: Continue outer some loop
- "Return { value: 9 }" # Index 27
want_result: true
- note: "nested_with_condition"
description: "Test nested loops with conditional logic"
example_rego: |
[x | x := [1, 2, 3][_]; x > 1]
literals:
- 1
- 2
- 3
instruction_params:
comprehension_begin_params:
- mode: "Array"
collection_reg: 6
result_reg: 6
key_reg: 4
value_reg: 5
body_start: 9
comprehension_end: 14
loop_params:
- mode: "ForEach"
collection: 0
key_reg: 4
value_reg: 5
result_reg: 7
body_start: 9
loop_end: 14
instructions:
- "ArrayNew { dest: 0 }" # Index 0: Create input array [1, 2, 3]
- "Load { dest: 1, literal_idx: 0 }" # Index 1: Load 1
- "ArrayPush { arr: 0, value: 1 }" # Index 2: Push 1 to array
- "Load { dest: 2, literal_idx: 1 }" # Index 3: Load 2
- "ArrayPush { arr: 0, value: 2 }" # Index 4: Push 2 to array
- "Load { dest: 3, literal_idx: 2 }" # Index 5: Load 3
- "ArrayPush { arr: 0, value: 3 }" # Index 6: Push 3 to array
- "ComprehensionBegin { params_index: 0 }" # Index 7: Start comprehension
- "LoopStart { params_index: 0 }" # Index 8: Start loop
- "Load { dest: 7, literal_idx: 0 }" # Index 9: Load 1 for comparison
- "Gt { dest: 8, left: 5, right: 7 }" # Index 10: x > 1
- "AssertCondition { condition: 8 }" # Index 11: Assert x > 1 (skip if false)
- "ComprehensionYield { value_reg: 5 }" # Index 12: Push x to result if condition true
- "LoopNext { body_start: 9, loop_end: 13 }" # Index 13: Continue loop
- "Return { value: 6 }" # Index 14: Return comprehension result
want_result: [2, 3]
@@ -0,0 +1,7 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# Placeholder for nested fixed loops - currently empty
# TODO: Add test cases for fixed nested loop scenarios
cases: []
@@ -0,0 +1,192 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# Object Comprehension Test Suite
# Tests object comprehensions - construct objects with key-value pairs based on conditions
# Corresponds to Rego's "{key: value | condition}" patterns
cases:
- note: object_simple_key_value
description: Simple object comprehension with computed values
example_rego: |
# Create object mapping each value to its double
{x: x * 2 | x := [1, 2, 3][_]} # {1: 2, 2: 4, 3: 6}
literals:
- {}
- 1
- 2
- 3
- 2 # multiplier
instruction_params:
object_create_params:
- dest: 9
template_literal_idx: 0
literal_key_fields: []
fields: []
comprehension_begin_params:
- mode: "Object"
collection_reg: 9
result_reg: 9
key_reg: 4
value_reg: 5
body_start: 10
comprehension_end: 13
loop_params:
- mode: "ForEach"
collection: 0
key_reg: 4
value_reg: 5
result_reg: 10
body_start: 10
loop_end: 14
instructions:
- "ArrayNew { dest: 0 }" # Index 0: Create input array [1, 2, 3]
- "Load { dest: 1, literal_idx: 1 }" # Index 1: Load 1
- "ArrayPush { arr: 0, value: 1 }" # Index 2
- "Load { dest: 2, literal_idx: 2 }" # Index 3: Load 2
- "ArrayPush { arr: 0, value: 2 }" # Index 4
- "Load { dest: 3, literal_idx: 3 }" # Index 5: Load 3
- "ArrayPush { arr: 0, value: 3 }" # Index 6
- "ObjectCreate { params_index: 0 }" # Index 7: Create empty result object in r9
- "ComprehensionBegin { params_index: 0 }" # Index 8: Start object comprehension
- "LoopStart { params_index: 0 }" # Index 9: Start loop
- "Load { dest: 11, literal_idx: 4 }" # Index 10: Load multiplier 2
- "Mul { dest: 12, left: 5, right: 11 }" # Index 11: Multiply value by 2
- "ComprehensionYield { value_reg: 12, key_reg: 5 }" # Index 12: Add key-value pair
- "LoopNext { body_start: 10, loop_end: 14 }" # Index 13: Continue loop
- "Return { value: 9 }" # Index 14: Return result object
want_result: {1: 2, 2: 4, 3: 6}
- note: object_empty_input
description: Object comprehension with empty input
example_rego: |
# Create object from empty array
{x: x + 5 | x := [][_]} # {} (empty object)
literals:
- {}
- 5 # addend
instruction_params:
object_create_params:
- dest: 9
template_literal_idx: 0
literal_key_fields: []
fields: []
comprehension_begin_params:
- mode: "Object"
collection_reg: 9
result_reg: 9
key_reg: 4
value_reg: 5
body_start: 4
comprehension_end: 7
loop_params:
- mode: "ForEach"
collection: 0
key_reg: 4
value_reg: 5
result_reg: 10
body_start: 4
loop_end: 8
instructions:
- "ArrayNew { dest: 0 }" # Index 0: Create empty input array
- "ObjectCreate { params_index: 0 }" # Index 1: Create empty result object in r9
- "ComprehensionBegin { params_index: 0 }" # Index 2: Start object comprehension
- "LoopStart { params_index: 0 }" # Index 3: Start loop
- "Load { dest: 10, literal_idx: 1 }" # Index 4: Load addend 5
- "Add { dest: 11, left: 5, right: 10 }" # Index 5: Add 5 to value
- "ComprehensionYield { value_reg: 11, key_reg: 5 }" # Index 6: Add key-value pair
- "LoopNext { body_start: 4, loop_end: 8 }" # Index 7: Continue loop
- "Return { value: 9 }" # Index 8: Return result object
want_result: {}
- note: object_single_element
description: Object comprehension with single element
example_rego: |
# Create object with single key-value pair
{x: x - 1 | x := [5][_]} # {5: 4}
literals:
- {}
- 5
- 1 # subtrahend
instruction_params:
object_create_params:
- dest: 9
template_literal_idx: 0
literal_key_fields: []
fields: []
comprehension_begin_params:
- mode: "Object"
collection_reg: 9
result_reg: 9
key_reg: 4
value_reg: 5
body_start: 6
comprehension_end: 9
loop_params:
- mode: "ForEach"
collection: 0
key_reg: 4
value_reg: 5
result_reg: 10
body_start: 6
loop_end: 10
instructions:
- "ArrayNew { dest: 0 }" # Index 0: Create input array [5]
- "Load { dest: 1, literal_idx: 1 }" # Index 1: Load 5
- "ArrayPush { arr: 0, value: 1 }" # Index 2: Push 5 to array
- "ObjectCreate { params_index: 0 }" # Index 3: Create empty result object in r9
- "ComprehensionBegin { params_index: 0 }" # Index 4: Start object comprehension
- "LoopStart { params_index: 0 }" # Index 5: Start loop
- "Load { dest: 10, literal_idx: 2 }" # Index 6: Load subtrahend 1
- "Sub { dest: 11, left: 5, right: 10 }" # Index 7: Subtract 1 from value
- "ComprehensionYield { value_reg: 11, key_reg: 5 }" # Index 8: Add key-value pair
- "LoopNext { body_start: 6, loop_end: 10 }" # Index 9: Continue loop
- "Return { value: 9 }" # Index 10: Return result object
want_result: {5: 4}
- note: object_with_null_keys
description: Object comprehension with null keys and values
example_rego: |
# Create object with null keys/values
{x: x | x := [1, null, 2][_]} # {1: 1, null: null, 2: 2}
literals:
- {}
- 1
- 2
instruction_params:
object_create_params:
- dest: 9
template_literal_idx: 0
literal_key_fields: []
fields: []
comprehension_begin_params:
- mode: "Object"
collection_reg: 9
result_reg: 9
key_reg: 4
value_reg: 5
body_start: 10
comprehension_end: 11
loop_params:
- mode: "ForEach"
collection: 0
key_reg: 4
value_reg: 5
result_reg: 10
body_start: 10
loop_end: 12
instructions:
- "ArrayNew { dest: 0 }" # Index 0: Create input array
- "Load { dest: 1, literal_idx: 1 }" # Index 1: Load 1
- "ArrayPush { arr: 0, value: 1 }" # Index 2: Push 1 to array
- "LoadNull { dest: 2 }" # Index 3: Load null value
- "ArrayPush { arr: 0, value: 2 }" # Index 4: Push null to array
- "Load { dest: 3, literal_idx: 2 }" # Index 5: Load 2
- "ArrayPush { arr: 0, value: 3 }" # Index 6: Push 2 to array
- "ObjectCreate { params_index: 0 }" # Index 7: Create empty result object in r9
- "ComprehensionBegin { params_index: 0 }" # Index 8: Start object comprehension
- "LoopStart { params_index: 0 }" # Index 9: Start loop
- "ComprehensionYield { value_reg: 5, key_reg: 5 }" # Index 10: Use value as both key and value
- "LoopNext { body_start: 10, loop_end: 12 }" # Index 11: Continue loop
- "Return { value: 9 }" # Index 12: Return result object
want_result: {1: 1, null: null, 2: 2}
@@ -0,0 +1,139 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# Set Comprehension Test Suite
# Tests set comprehensions - collect unique transformed values based on conditions
# Corresponds to Rego's "{transform | condition}" patterns
cases:
- note: set_simple_transform
description: Simple set comprehension with transformation
example_rego: |
# Transform array values into set - duplicates removed
{x * 2 | x := [1, 2, 2, 3][_]} # {2, 4, 6} (duplicates removed)
literals:
- 1
- 2
- 3
- 2 # multiplier
instruction_params:
comprehension_start_params:
- mode: "Set"
collection_reg: 0
key_reg: 4
value_reg: 5
result_reg: 7
body_start: 10
comprehension_end: 14
instructions:
- "ArrayNew { dest: 0 }" # Create input array [1, 2, 2, 3] in register 0
- "Load { dest: 1, literal_idx: 0 }" # Load 1 into register 1
- "ArrayPush { arr: 0, value: 1 }" # Push 1 to array
- "Load { dest: 2, literal_idx: 1 }" # Load 2 into register 2
- "ArrayPush { arr: 0, value: 2 }" # Push 2 to array
- "ArrayPush { arr: 0, value: 2 }" # Push 2 again to array (duplicate)
- "Load { dest: 3, literal_idx: 2 }" # Load 3 into register 3
- "ArrayPush { arr: 0, value: 3 }" # Push 3 to array
- "SetNew { dest: 7 }" # Initialize result set in register 7
- "ComprehensionStart { params_index: 0 }" # Start set comprehension
- "Load { dest: 6, literal_idx: 3 }" # Load multiplier 2 into register 6
- "Mul { dest: 10, left: 5, right: 6 }" # Multiply current value by 2, store result in register 10
- "ComprehensionAdd { value_reg: 10 }" # Add transformed value to result set (auto-deduplicates)
- "Halt" # End comprehension
- "Return { value: 7 }" # Return result set
want_result:
set!: [2, 4, 6]
- note: set_empty_input
description: Set comprehension with empty input
example_rego: |
# Transform empty array into set
{x + 5 | x := [][_]} # {} (empty set)
literals:
- 5 # addend
instruction_params:
comprehension_start_params:
- mode: "Set"
collection_reg: 0
key_reg: 4
value_reg: 5
result_reg: 7
body_start: 3
comprehension_end: 7
instructions:
- "ArrayNew { dest: 0 }" # Create empty input array in register 0
- "SetNew { dest: 7 }" # Initialize result set in register 7
- "ComprehensionStart { params_index: 0 }" # Start set comprehension
- "Load { dest: 6, literal_idx: 0 }" # Load addend 5 into register 6
- "Add { dest: 10, left: 5, right: 6 }" # Add 5 to current value, store result in register 10
- "ComprehensionAdd { value_reg: 10 }" # Add transformed value to result set
- "Halt" # End comprehension
- "Return { value: 7 }" # Return result set
want_result:
set!: []
- note: set_single_element
description: Set comprehension with single element
example_rego: |
# Transform single element into set
{x - 1 | x := [10][_]} # {9}
literals:
- 10
- 1 # subtrahend
instruction_params:
comprehension_start_params:
- mode: "Set"
collection_reg: 0
key_reg: 4
value_reg: 5
result_reg: 7
body_start: 5
comprehension_end: 9
instructions:
- "ArrayNew { dest: 0 }" # Create input array [10] in register 0
- "Load { dest: 1, literal_idx: 0 }" # Load 10 into register 1
- "ArrayPush { arr: 0, value: 1 }" # Push 10 to array
- "SetNew { dest: 7 }" # Initialize result set in register 7
- "ComprehensionStart { params_index: 0 }" # Start set comprehension
- "Load { dest: 6, literal_idx: 1 }" # Load subtrahend 1 into register 6
- "Sub { dest: 10, left: 5, right: 6 }" # Subtract 1 from current value, store result in register 10
- "ComprehensionAdd { value_reg: 10 }" # Add transformed value to result set
- "Halt" # End comprehension
- "Return { value: 7 }" # Return result set
want_result:
set!: [9]
- note: set_with_null_deduplication
description: Set comprehension with null values and deduplication
example_rego: |
# Collect unique values including nulls
{x | x := [1, null, 1, null, 2][_]} # {1, null, 2}
literals:
- 1
- 2
instruction_params:
comprehension_start_params:
- mode: "Set"
collection_reg: 0
key_reg: 4
value_reg: 5
result_reg: 7
body_start: 11
comprehension_end: 13
instructions:
- "ArrayNew { dest: 0 }" # Create input array in register 0
- "Load { dest: 1, literal_idx: 0 }" # Load 1
- "ArrayPush { arr: 0, value: 1 }" # Push 1 to array
- "LoadNull { dest: 2 }" # Load null value
- "ArrayPush { arr: 0, value: 2 }" # Push null to array
- "ArrayPush { arr: 0, value: 1 }" # Push 1 again (duplicate)
- "ArrayPush { arr: 0, value: 2 }" # Push null again (duplicate)
- "Load { dest: 3, literal_idx: 1 }" # Load 2
- "ArrayPush { arr: 0, value: 3 }" # Push 2 to array
- "SetNew { dest: 7 }" # Initialize result set in register 7
- "ComprehensionStart { params_index: 0 }" # Start set comprehension
- "ComprehensionAdd { value_reg: 5 }" # Add current value to result set (auto-deduplicates)
- "Halt" # End comprehension
- "Return { value: 7 }" # Return result set
want_result:
set!: [1, null, 2]
+143
View File
@@ -0,0 +1,143 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# Universal Loops Test Suite
# Tests universal quantification - succeed if ALL elements satisfy the condition
# Corresponds to Rego's "every x in collection; condition" patterns
cases:
- note: universal_basic_every
description: Basic universal quantification - every element satisfies condition
example_rego: |
# Check if every element in array is greater than 0
every x in [1, 2, 3] {
x > 0 # true (all elements > 0)
}
literals:
- 1
- 2
- 3
- 0 # comparison value
instruction_params:
loop_params:
- mode: "Every"
collection: 0
key_reg: 4
value_reg: 5
result_reg: 6
body_start: 8
loop_end: 12
instructions:
- "ArrayNew { dest: 0 }" # Create input array [1, 2, 3] in register 0
- "Load { dest: 1, literal_idx: 0 }" # Load 1 into register 1
- "ArrayPush { arr: 0, value: 1 }" # Push 1 to array
- "Load { dest: 2, literal_idx: 1 }" # Load 2 into register 2
- "ArrayPush { arr: 0, value: 2 }" # Push 2 to array
- "Load { dest: 3, literal_idx: 2 }" # Load 3 into register 3
- "ArrayPush { arr: 0, value: 3 }" # Push 3 to array
- "LoopStart { params_index: 0 }" # Start universal loop using parameter table index 0
- "Load { dest: 7, literal_idx: 3 }" # Load comparison value 0 into register 7
- "Gt { dest: 8, left: 5, right: 7 }" # Check if current value > 0
- "AssertCondition { condition: 8 }" # Assert the condition result for universal logic
- "LoopNext { body_start: 8, loop_end: 12 }" # Continue to next iteration or exit early if condition fails
- "Return { value: 6 }" # Return result (true if all elements satisfied condition)
want_result: true
- note: universal_one_fails
description: Universal quantification where one element fails condition
example_rego: |
# Check if every element in array is greater than 1
every x in [1, 2, 3] {
x > 1 # false (1 is not > 1)
}
literals:
- 1
- 2
- 3
- 1 # comparison value
instruction_params:
loop_params:
- mode: "Every"
collection: 0
key_reg: 4
value_reg: 5
result_reg: 6
body_start: 8
loop_end: 12
instructions:
- "ArrayNew { dest: 0 }" # Create input array [1, 2, 3] in register 0
- "Load { dest: 1, literal_idx: 0 }" # Load 1 into register 1
- "ArrayPush { arr: 0, value: 1 }" # Push 1 to array
- "Load { dest: 2, literal_idx: 1 }" # Load 2 into register 2
- "ArrayPush { arr: 0, value: 2 }" # Push 2 to array
- "Load { dest: 3, literal_idx: 2 }" # Load 3 into register 3
- "ArrayPush { arr: 0, value: 3 }" # Push 3 to array
- "LoopStart { params_index: 0 }" # Start universal loop using parameter table index 0
- "Load { dest: 7, literal_idx: 3 }" # Load comparison value 1 into register 7
- "Gt { dest: 8, left: 5, right: 7 }" # Check if current value > 1
- "AssertCondition { condition: 8 }" # Assert the condition result for universal logic
- "LoopNext { body_start: 8, loop_end: 12 }" # Continue to next iteration or exit early on failure
- "Return { value: 6 }" # Return result (false since first element failed condition)
want_result: false
- note: universal_empty_collection
description: Universal quantification on empty collection
example_rego: |
# Check if every element in empty array satisfies condition
every x in [] {
x > 0 # true (vacuously true - all 0 elements satisfy condition)
}
literals:
- 0 # comparison value
instruction_params:
loop_params:
- mode: "Every"
collection: 0
key_reg: 4
value_reg: 5
result_reg: 6
body_start: 2
loop_end: 5
instructions:
- "ArrayNew { dest: 0 }" # Create empty input array in register 0
- "LoopStart { params_index: 0 }" # Start universal loop
- "Load { dest: 7, literal_idx: 0 }" # Load comparison value 0 into register 7
- "Gt { dest: 8, left: 5, right: 7 }" # Check if current value > 0
- "LoopNext { body_start: 2, loop_end: 5 }" # Continue to next iteration
- "Return { value: 6 }" # Return result (true for empty collection - vacuous truth)
want_result: true
- note: universal_null_handling
description: Universal quantification with null values
example_rego: |
# Test behavior with null values - should handle gracefully
every x in [2, null, 4] {
x != null # false (null fails the condition)
}
literals:
- 2
- 4
instruction_params:
loop_params:
- mode: "Every"
collection: 0
key_reg: 4
value_reg: 5
result_reg: 6
body_start: 8
loop_end: 12
instructions:
- "ArrayNew { dest: 0 }" # Create input array in register 0
- "Load { dest: 1, literal_idx: 0 }" # Load 2
- "ArrayPush { arr: 0, value: 1 }" # Push 2 to array
- "LoadNull { dest: 2 }" # Load null value
- "ArrayPush { arr: 0, value: 2 }" # Push null to array
- "Load { dest: 3, literal_idx: 1 }" # Load 4
- "ArrayPush { arr: 0, value: 3 }" # Push 4 to array
- "LoopStart { params_index: 0 }" # Start universal loop
- "LoadNull { dest: 7 }" # Load null for comparison
- "Ne { dest: 8, left: 5, right: 7 }" # Check if current value != null
- "AssertCondition { condition: 8 }" # Assert the condition result for universal logic
- "LoopNext { body_start: 8, loop_end: 12 }" # Continue to next iteration
- "Return { value: 6 }" # Return result
want_result: false