Skip to content

Commit

Permalink
Type coercions (#684)
Browse files Browse the repository at this point in the history
* Implemented type coercions and java statement builder

* WIP

* Updated tests

* Cleaned

* Fixed bugs

* Added docs
  • Loading branch information
SimonCockx authored Nov 16, 2023
1 parent 24b5cbb commit a1e767f
Show file tree
Hide file tree
Showing 93 changed files with 5,308 additions and 1,302 deletions.
4 changes: 2 additions & 2 deletions rosetta-lang/model/RosettaExpression.xcore
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ class RosettaNumberLiteral extends RosettaLiteral {
}

class RosettaIntLiteral extends RosettaLiteral {
int value
BigInteger value

op Object containedValue() {
return value
}
op String stringValue() {
return Integer.toString(value)
return value.toString
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ BigDecimal returns ecore::EBigDecimal hidden():
('+' | '-')? ('.' INT | INT '.' | INT '.' INT) (('e' | 'E') ('+' | '-') INT | ID)?
;

Integer returns ecore::EInt hidden():
Integer returns ecore::EBigInteger hidden():
('+' | '-')? INT
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,22 @@ public void createKeySynonym(Object key, Object keyWithIdentifier) {

this.keySynonyms.put(key, keyWithIdentifier);
}
/**
* Create another key for a given identifier.
*
* @throws IllegalStateException if this scope is closed.
* @throws IllegalStateException if this scope already contains an identifier for `key`.
*/
public void createSynonym(Object key, GeneratedIdentifier identifier) {
if (isClosed) {
throw new IllegalStateException("Cannot create a new synonym in a closed scope. (" + normalizeKey(key) + " -> " + normalizeKey(identifier) + ")\n" + this);
}
if (this.getIdentifier(key).isPresent()) {
throw new IllegalStateException("There is already a name defined for key `" + normalizeKey(key) + "`.\n" + this);
}

this.identifiers.put(key, identifier);
}

/**
* Mark this scope as closed. New identifiers cannot be added to a closed scope.
Expand Down Expand Up @@ -251,7 +267,7 @@ private void computeActualNames() {
}
private LinkedListMultimap<String, GeneratedIdentifier> localIdentifiersByDesiredName() {
LinkedListMultimap<String, GeneratedIdentifier> result = LinkedListMultimap.create();
identifiers.values().forEach(id -> result.put(id.getDesiredName(), id));
identifiers.values().stream().distinct().forEach(id -> result.put(id.getDesiredName(), id));
return result;
}
protected Set<String> getTakenNames() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public Optional<GeneratedIdentifier> getIdentifier(Object obj) {
JavaType t = JavaType.from(obj);
if (t != null) {
if (t instanceof JavaClass) {
JavaClass clazz = (JavaClass)t;
JavaClass<?> clazz = (JavaClass<?>)t;
String desiredName = clazz.getSimpleName();
if (this.getIdentifiers().stream().anyMatch(id -> id.getDesiredName().equals(desiredName))) {
// Another class with the same name is already imported. Use the canonical name instead.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import static com.regnosys.rosetta.rosetta.simple.SimplePackage.Literals.CONDITI
import javax.inject.Inject
import com.google.inject.ImplementedBy
import com.rosetta.model.lib.validation.ValidationResult.ValidationType
import com.regnosys.rosetta.generator.java.types.JavaTypeUtil

class ConditionGenerator {
@Inject ExpressionGenerator expressionHandler
Expand All @@ -33,6 +34,7 @@ class ConditionGenerator {
@Inject FunctionDependencyProvider funcDependencies
@Inject extension JavaIdentifierRepresentationService
@Inject extension JavaTypeTranslator
@Inject extension JavaTypeUtil

def generate(RootPackage root, IFileSystemAccess2 fsa, Data data, Condition ele, String version) {
val topScope = new JavaScope(root.condition)
Expand Down Expand Up @@ -66,7 +68,6 @@ class ConditionGenerator {
val defaultClassFailureMessageId = defaultClassValidateScope.createUniqueIdentifier("failureMessage")

val defaultClassExecuteScope = defaultClassScope.methodScope("execute")
val defaultClassExecuteResultId = defaultClassExecuteScope.createUniqueIdentifier("result")
val defaultClassExceptionId = defaultClassExecuteScope.createUniqueIdentifier("ex")

val noOpClassScope = classScope.classScope("NoOp")
Expand Down Expand Up @@ -107,10 +108,8 @@ class ConditionGenerator {
}
private «ComparisonResult» executeDataRule(«rosettaClass.name» «defaultClassExecuteScope.createIdentifier(implicitVarRepr, rosettaClass.name.toFirstLower)») {
try {
«ComparisonResult» «defaultClassExecuteResultId» = «expressionHandler.toComparisonResult(rule.expression, defaultClassExecuteScope)»;
return «defaultClassExecuteResultId».get() == null ? ComparisonResult.success() : «defaultClassExecuteResultId»;
}
try «expressionHandler.javaCode(rule.expression, COMPARISON_RESULT, defaultClassExecuteScope)
.completeAsReturn.toBlock»
catch («Exception» «defaultClassExceptionId») {
return «ComparisonResult».failure(«defaultClassExceptionId».getMessage());
}
Expand Down
Loading

0 comments on commit a1e767f

Please sign in to comment.