Created
December 22, 2012 21:17
-
-
Save seanedwards/4361216 to your computer and use it in GitHub Desktop.
A somewhat contrived Ides program demonstrating functions as first-class variables.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def main(val argc: int32, val argv: int8**) : int32 { | |
| return call(getsum, 5, 10); | |
| } | |
| def getsum() : (function(int32, int32):int32) { | |
| return sum; | |
| } | |
| def call(val func : function() : function(int32, int32) : int32, val x: int32, val y: int32) : int32 { | |
| return func()(x, y); | |
| } | |
| def sum(val x: int32, val y: int32) = x + y; | |
| define i32 @main(i32 %argc, i8** %argv) gc "refcount" { | |
| entry: | |
| br label %scope | |
| scope: ; preds = %entry | |
| %0 = call i32 @call(i32 (i32, i32)* ()* @getsum, i32 5, i32 10) | |
| ret i32 %0 | |
| } | |
| define i32 (i32, i32)* @getsum() gc "refcount" { | |
| entry: | |
| br label %scope | |
| scope: ; preds = %entry | |
| ret i32 (i32, i32)* @sum | |
| } | |
| define i32 @call(i32 (i32, i32)* ()* %func, i32 %x, i32 %y) gc "refcount" { | |
| entry: | |
| br label %scope | |
| scope: ; preds = %entry | |
| %0 = call i32 (i32, i32)* ()* %func() | |
| %1 = call i32 %0(i32 %x, i32 %y) | |
| ret i32 %1 | |
| } | |
| define i32 @sum(i32 %x, i32 %y) gc "refcount" { | |
| entry: | |
| %0 = add i32 %x, %y | |
| ret i32 %0 | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment