Skip to content

Instantly share code, notes, and snippets.

@fredreichbier
Last active December 14, 2015 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fredreichbier/5009297 to your computer and use it in GitHub Desktop.
Save fredreichbier/5009297 to your computer and use it in GitHub Desktop.
g: func <T> (T: Class) -> String { "" }
f: func {
match(t := g(String)) {
case a: String => a println()
case => "n" println()
}
}
void m5__f() {
{
lang_String__String* t = m5__g((lang_types__Class*) (lang_String__String_class()));
lang_String__String* __matchcompare2 = (t); // wrong scope!
}
if (lang_types__Object_instanceOf__quest((lang_types__Object*) (t), (lang_types__Class*) (lang_String__String_class()))){
lang_String__String* a = NULL;
a = ((lang_String__String*) ((t)));
lang_String__String_println(a);
} else {
lang_String__String_println(__strLit215);
};
}
diff --git a/source/rock/middle/Match.ooc b/source/rock/middle/Match.ooc
index 14a69f0..87cf3cf 100644
--- a/source/rock/middle/Match.ooc
+++ b/source/rock/middle/Match.ooc
@@ -10,6 +10,7 @@ Match: class extends Expression {
type: Type = null
expr: Expression = null
+ compareExpr: Expression = null
cases := ArrayList<Case> new()
casesResolved := 0
@@ -78,6 +79,16 @@ Match: class extends Expression {
casesSize = cases getSize()
}
+ if(compareExpr == null && expr != null) {
+ // set a "compare expression".
+ compareDecl := VariableDecl new(type, generateTempName("matchcompare"), expr, token)
+ compareExpr = VariableAccess new(compareDecl, token)
+ if(!trail addBeforeInScope(this, compareDecl)) {
+ res throwError(CouldntAddBeforeInScope new(token, this, compareDecl, trail))
+ }
+ // success! we can now use `compareExpr` later on.
+ }
+
catchAll? := false
if(casesResolved < casesSize) {
@@ -144,7 +155,9 @@ Match: class extends Expression {
if (caze getExpr() instanceOf?(BinaryOp)) {
unwrapBinaryOpCase(caze)
} else {
- caze setExpr(Comparison new(expr, caze getExpr(), CompType equal, caseToken))
+ // use the "compare" expression here to avoid multiple
+ // function calls.
+ caze setExpr(Comparison new(compareExpr, caze getExpr(), CompType equal, caseToken))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment