Expressions
Advanced expression handling in the HCL parser.
Use case: Parsing HCL files with complex expressions, conditionals, and transformations.
Arithmetic Expressions
Section titled “Arithmetic Expressions”use Cline\Hcl\Hcl;
$hcl = <<<'HCL'a = 10b = 3
sum = 10 + 3 # 13diff = 10 - 3 # 7product = 10 * 3 # 30quotient = 10 / 3 # 3.333...remainder = 10 % 3 # 1HCL;
$data = Hcl::parse($hcl);
// Expressions are evaluated at parse time$data['sum']; // 13.0$data['product']; // 30.0$data['remainder']; // 1.0Comparison Expressions
Section titled “Comparison Expressions”$hcl = <<<'HCL'x = 5y = 10
equal = 5 == 5 # truenot_equal = 5 != 10 # trueless = 5 < 10 # truegreater = 10 > 5 # trueless_or_eq = 5 <= 5 # truegreater_or_eq = 10 >= 5 # trueHCL;
$data = Hcl::parse($hcl);$data['less']; // trueLogical Expressions
Section titled “Logical Expressions”$hcl = <<<'HCL'a = trueb = false
and_result = true && false # falseor_result = true || false # truenot_result = !false # true
# Complex conditionscomplex = (5 > 3) && (10 < 20) # trueHCL;
$data = Hcl::parse($hcl);$data['complex']; // trueConditional (Ternary) Expressions
Section titled “Conditional (Ternary) Expressions”$hcl = <<<'HCL'production = true
port = production ? 443 : 8080message = production ? "Production mode" : "Development mode"
# Nested conditionalsenv = "staging"color = env == "prod" ? "red" : env == "staging" ? "yellow" : "green"HCL;
$data = Hcl::parse($hcl);$data['port']; // 443$data['message']; // "Production mode"String Interpolation
Section titled “String Interpolation”$hcl = <<<'HCL'name = "World"greeting = "Hello, ${name}!"
host = "localhost"port = 8080url = "http://${host}:${port}/api"
# Expressions in interpolationcount = 5status = "Found ${count * 2} items"HCL;
$data = Hcl::parse($hcl);$data['greeting']; // "Hello, World!"$data['url']; // "http://localhost:8080/api"Collection Access
Section titled “Collection Access”Index Access
Section titled “Index Access”$hcl = <<<'HCL'ports = [80, 443, 8080]
first = ports[0] # 80second = ports[1] # 443last = ports[2] # 8080HCL;
$data = Hcl::parse($hcl);$data['first']; // 80Attribute Access
Section titled “Attribute Access”$hcl = <<<'HCL'server = { host = "localhost" port = 8080 config = { timeout = 30 }}
hostname = server.hosttimeout = server.config.timeoutHCL;
$data = Hcl::parse($hcl);$data['hostname']; // "localhost"$data['timeout']; // 30Splat Expressions
Section titled “Splat Expressions”Extract values from lists of objects:
$hcl = <<<'HCL'users = [ { name = "alice", age = 30 }, { name = "bob", age = 25 }, { name = "charlie", age = 35 },]
# Bracket splat syntaxnames = users[*].name
# Dot splat syntax (alternative)ages = users.*.ageHCL;
$data = Hcl::parse($hcl);// Note: Splat expressions return references when not fully resolvable// They work best with static data in the same fileFor Expressions
Section titled “For Expressions”List Transformation
Section titled “List Transformation”$hcl = <<<'HCL'numbers = [1, 2, 3, 4, 5]
# Double each numberdoubled = [for n in numbers : n * 2]# Result: [2, 4, 6, 8, 10]
# Square each numbersquared = [for n in numbers : n * n]# Result: [1, 4, 9, 16, 25]HCL;
$data = Hcl::parse($hcl);$data['doubled']; // [2.0, 4.0, 6.0, 8.0, 10.0]Filtering with If
Section titled “Filtering with If”$hcl = <<<'HCL'numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Filter even numbersevens = [for n in numbers : n if n % 2 == 0]# Result: [2, 4, 6, 8, 10]
# Filter and transformeven_doubled = [for n in numbers : n * 2 if n % 2 == 0]# Result: [4, 8, 12, 16, 20]HCL;
$data = Hcl::parse($hcl);Object (Map) Expressions
Section titled “Object (Map) Expressions”$hcl = <<<'HCL'names = ["alice", "bob", "charlie"]
# Create object from listname_lengths = { for name in names : name => length(name) }# Result: { "alice" = 5, "bob" = 3, "charlie" = 7 }HCL;Key-Value Iteration
Section titled “Key-Value Iteration”$hcl = <<<'HCL'source = { a = 1 b = 2 c = 3}
# Swap keys and valuesswapped = { for k, v in source : v => k }# Result: { 1 = "a", 2 = "b", 3 = "c" }
# Transform valuesincremented = { for k, v in source : k => v + 1 }# Result: { "a" = 2, "b" = 3, "c" = 4 }HCL;Unary Operators
Section titled “Unary Operators”$hcl = <<<'HCL'positive = 5negative = -5double_neg = --5 # 5
flag = trueinverted = !flag # falseHCL;
$data = Hcl::parse($hcl);$data['negative']; // -5$data['double_neg']; // 5.0$data['inverted']; // falseParentheses for Grouping
Section titled “Parentheses for Grouping”$hcl = <<<'HCL'# Without parentheses: 2 + 3 * 4 = 14result1 = 2 + 3 * 4
# With parentheses: (2 + 3) * 4 = 20result2 = (2 + 3) * 4
# Complex groupingcomplex = (10 + 5) * (3 - 1) / 2HCL;
$data = Hcl::parse($hcl);$data['result1']; // 14.0$data['result2']; // 20.0Function Calls
Section titled “Function Calls”Functions are parsed but not evaluated unless built-in:
$hcl = <<<'HCL'# Function calls are preserved as structuressecret = sensitive("password123")content = file("config.json")encoded = base64encode("hello")HCL;
$data = Hcl::parse($hcl);
// Functions are represented as:$data['secret'];// [// '__function__' => 'sensitive',// '__args__' => ['password123'],// ]
// You can evaluate them in your application:if (isset($data['secret']['__function__'])) { $func = $data['secret']['__function__']; $args = $data['secret']['__args__']; // Handle based on function name}Operator Precedence
Section titled “Operator Precedence”From highest to lowest:
()- Parentheses!,-(unary) - Logical NOT, negation*,/,%- Multiplication, division, modulo+,-- Addition, subtraction<,<=,>,>=- Comparison==,!=- Equality&&- Logical AND||- Logical OR? :- Conditional (ternary)
$hcl = <<<'HCL'# Precedence exampleresult = 2 + 3 * 4 > 10 && true || false# Evaluates as: ((2 + (3 * 4)) > 10) && true || false# = (2 + 12 > 10) && true || false# = (14 > 10) && true || false# = true && true || false# = true || false# = trueHCL;