Unable to use a declaration in assignments and if conditions [message #1744158] |
Thu, 22 September 2016 22:02 |
Edward Mallia Messages: 8 Registered: September 2016 |
Junior Member |
|
|
I have the following grammar (snippets of):
Condition:
'condition' name=ID '(' conditionFormalParameters=FormalParameters ')' '=' conditionExpression=ConditionExpression;
ConditionExpression:
ref=ConditionRefInvocation | block=ConditionBlock;
ConditionBlock:
simple=SimpleCondition | complex=ComplexCondition;
SimpleCondition returns xbase::XBlockExpression:
'{{'
{SimpleCondition}
(expressions+=ConditionBlockStatement ';'?)*
'}}';
ComplexCondition returns xbase::XBlockExpression:
'{'
{ComplexCondition}
(expressions+=ConditionBlockStatement ';'?)*
'}';
ConditionBlockStatement returns xbase::XExpression:
ConditionRefInvocation | XExpressionOrVarDeclaration;
ConditionRefInvocation:
{ConditionRefInvocation} ref=ConditionRef '(' params=ActualParameters ')';
In other words, I can define conditions and conditions are reusable. For example, the following is a snippet from my DSL that works.
condition isEmpty(String s) = {
s.empty
}
condition isAbcEmpty() = {
#isEmpty("abc")
}
However, when I try to us a Condition reference in an assignment (see below) I get a "no viable alternative at input '#'" error.
condition isAbcEmpty() = {
var b = #isEmpty("abc")
return b
}
And when I try to use a Condition reference in an if condition (see below) I get a list of errors:
- mismatched input ')' expecting '}'
- missing EOF at 'else'
- no viable alternative at input '#'
- The method isEmpty(String) is undefined
condition isAbcEmpty() = {
if (#isEmpty("abc")){
return true;
}
else{
return false;
}
}
My TypeComputer and relevant compiler methods are show below together with my compiler
//type computer
def dispatch computeTypes(ConditionRefInvocation literal, ITypeComputationState state) {
for (XExpression ap : literal.params.getParameters()) {
state.withNonVoidExpectation().computeTypes(ap);
}
val booleanLightWeightRef = getTypeForName(Boolean.TYPE, state)
state.withExpectation(booleanLightWeightRef)
state.acceptActualType(booleanLightWeightRef)
}
//compiler
override void _toJavaExpression(XExpression obj, ITreeAppendable appendable) {
switch (obj) {
ConditionRefInvocation: _toJavaExpression(obj as ConditionRefInvocation, appendable)
default: super._toJavaExpression(obj, appendable)
}
}
def _toJavaExpression(ConditionRefInvocation obj, ITreeAppendable appendable) {
appendable.trace(obj)
appendable.newLine
val condition = obj.ref.ref
val conditionParameters = obj.params
val conditionClass = condition.jvmElements.filter(JvmGenericType).filter[t|!t.isInterface].head
appendable.append('''new «conditionClass.fullyQualifiedName»().apply(''')
appendable.newLine
appendArguments(conditionParameters.parameters, appendable)
appendable.newLine
appendable.append(")")
appendable.newLine
appendable.newLine
}
Could you please help?
Thanks
Edward
|
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02584 seconds