Skip to content

Commit

Permalink
Merge pull request #453 from shamanas/strInterp
Browse files Browse the repository at this point in the history
String interpolation fix: Use toString methods of parent classes
  • Loading branch information
alexnask committed Jun 25, 2012
2 parents ed5246c + e9d50b3 commit f390d0e
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions source/rock/middle/StringLiteral.ooc
Original file line number Diff line number Diff line change
Expand Up @@ -104,33 +104,36 @@ StringLiteral: class extends Literal {
interpolatedExpressions each(|pos, expr|
type := expr getType()
ref := type getRef()
fail := false
fail := true
if(!type isNumericType() && !(type instanceOf?(BaseType) && (type equals?(objectType) || type as BaseType subclassOf?(objectType)))) {
// If this is not a number or a String, we look for a toString method
if(ref) {
if(!ref instanceOf?(TypeDecl)) fail = true
else {
while(ref) {
if(!ref instanceOf?(TypeDecl)) {
ref = null
break
} else {
if(!ref as TypeDecl isMeta) ref = ref as TypeDecl getMeta()
fDecl := ref as TypeDecl lookupFunction("toString", null)
if(!fDecl) fail = true
else {
if(fDecl) {
returnType := fDecl returnType
if(!fDecl args empty?() || \
!(returnType instanceOf?(BaseType) && (returnType equals?(objectType) ||
returnType as BaseType subclassOf?(objectType)))) fail = true
else {
if(fDecl args empty?() && \
returnType instanceOf?(BaseType) && \
(returnType equals?(objectType) ||
returnType as BaseType subclassOf?(objectType))) {
// We have a toString method, so our argument is a call to it
toStringCall := FunctionCall new(expr, "toString", expr token)
args add(toStringCall)
fail = false
break
}
}
}
} else {
fail = true
ref = ref as TypeDecl getSuperType() getRef()
}
} else {
// If the expression is a number or a String, add it as is to the arguments
args add(expr)
fail = false
}
if(fail) {
res throwError(InvalidInterpolatedExpressionError new(token, "Expression %s of type %s cannot be interpolated in this string as it is neither a number nor a String type and it has no valid toString method." format(expr toString(), type toString())))
Expand Down

0 comments on commit f390d0e

Please sign in to comment.