Skip to content

Commit

Permalink
A closure sample to test a recent bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
Amos Wenger committed Jun 8, 2012
1 parent 4d54877 commit bb9ed43
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
4 changes: 1 addition & 3 deletions report.md
Expand Up @@ -333,13 +333,11 @@ by callsite. In theory, this might lead to combinatorial explosion (as seen
above), but in practice, module-level functions are rare enough in typical ooc
code that such an implementation is still relevant.

Since the ooc AST[^ast] is mutable, the first step to specializing a function is to
Since the ooc AST is mutable, the first step to specializing a function is to
keep a copy of it before any AST mutation can transform it into a full-blown
generic function. In our implementation, we simply added an `inline` member to
the `FunctionDecl` AST node.

[^ast]: Abstract Syntax Tree.

The second step is to modify the function call resolution process in order to
intercept functions that are marked as specializable. This is done by adding a
condition in the `resolveCall` function of the `FunctionCall`, that calls the
Expand Down
28 changes: 28 additions & 0 deletions samples/closure.ooc
@@ -0,0 +1,28 @@


Jar: class <Xaneon> {
_cookie: Xaneon

cookie: func ~set (=_cookie)

withCookie: func (falafel: Func (Xaneon)) {
falafel(_cookie)
}
}

#specialize Jar<Char> // binks

main: func {
j := Jar<Int> new()
j cookie(99)
j withCookie(|c|
printf("j's cookie = %d\n", c)
)

k := Jar<Char> new()
k cookie('c')
k withCookie(|c|
printf("k's cookie = %d\n", c)
)
}

2 changes: 1 addition & 1 deletion samples/sample.use
@@ -1 +1 @@
Main: sorting.ooc
Main: closure.ooc

0 comments on commit bb9ed43

Please sign in to comment.