Class ToJsonGenerator
java.lang.Object
org.ek9lang.compiler.phase7.synthesis.AbstractSyntheticGenerator
org.ek9lang.compiler.phase7.synthesis.ToJsonGenerator
Generates synthetic IR for the _json ($$) operator.
The generated code follows this pattern:
JSON _json():
// Guard: if object is unset, return UNSET JSON
if !this._isSet() -> return unset JSON
// has_data: at least one field is set
jsonTemp = JSON()
result = jsonTemp.object()
// For each field:
fieldValue = LOAD this.field
fieldJson = fieldValue._json() // Recursive JSON conversion
nameString = "fieldName" (literal)
pair = JSON(nameString, fieldJson) // Create name/value pair
result._merge(pair) // Merge into result
return result
return_unset:
return new JSON() // Unset JSON
Key semantic requirements:
- Returns UNSET JSON if this._isSet() is false (empty objects serialize to null)
- Returns set JSON if any field is set
- Format: {"field1": value1, "field2": value2}
- Each field value is converted via $$ (_json) recursively
- Unset field values become JSON null (handled by JSON constructor)
- Lists produce JSON arrays, Dicts produce nested objects
The ? guard ensures consistency: operations on empty objects propagate uncertainty.
-
Nested Class Summary
Nested classes/interfaces inherited from class AbstractSyntheticGenerator
AbstractSyntheticGenerator.ScopeSetup -
Field Summary
Fields inherited from class AbstractSyntheticGenerator
stackContext -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiongenerate(MethodSymbol operatorSymbol, AggregateSymbol aggregateSymbol) Generate the _json operator IR for the given aggregate.Methods inherited from class AbstractSyntheticGenerator
aggregateHasOperator, createDebugInfo, createSynthesisScope, generateAnyFieldSetGuard, generateBinaryOperatorGuards, generateBooleanBranch, generateBooleanReturnBlock, generateConstructorCall, generateConstructorCall, generateConstructorCall, generateConstructorCallWithArgs, generateFieldLoad, generateFieldLoad, generateFieldSetStatusCheck, generateIsSetGuard, generateIsSetGuard, generateLabelName, generateMethodCall, generateMethodCall, generateMethodCall, generateMethodCall, generateMethodCall, generateResultReturnBlock, generateResultReturnBlock, generateStringLiteralLoad, generateTempName, generateThisIsSetGuard, generateThisIsSetGuard, generateUnsetReturnBlock, generateUnsetReturnBlockWithLabel, generateUnsetReturnBlockWithLabel, getBitsTypeName, getBooleanTypeName, getIntegerTypeName, getReturnBlockHelper, getStringTypeName, getSuperTypeName, getSyntheticFields, getTypeName, getVoidTypeName, initializeScope, isAnyType, superHasOperator
-
Constructor Details
-
ToJsonGenerator
ToJsonGenerator(IRGenerationContext stackContext)
-
-
Method Details
-
generate
Generate the _json operator IR for the given aggregate.- Parameters:
operatorSymbol- The _json operator symbolaggregateSymbol- The aggregate containing the operator- Returns:
- List of IR instructions implementing the operator
-