path: root/parse/parse.h
diff options
authorOri Bernstein <>2017-07-26 22:02:02 -0700
committerOri Bernstein <>2017-07-29 10:16:35 -0700
commit404ed1ddb3faa4fec7ee4fd8fddc5bbdec284bde (patch)
tree49c9399882a1eac7320e02482d809ac096deca21 /parse/parse.h
parent9a2f63d47a96bf522e6c5d39e215b2e6a9e600c7 (diff)
Automatic variables
Hello, I wrote this patch to start a conversation about resource management in Myrddin. ~ Introduction ~~~~~~~~~~~~~~ The patch attached provides a hopefully life-improving and surely non-invasive mechanism to handle resources that have block-limited lifetimes: automatic variables. This resource-management scheme can be found in multiple languages today. For example, C++ calls destructors automatically at the end of the scope of a variable; similarly, Rust automatically calls user-defined code when a variable's scope ends; finally, also related is Go's defer statement which ensures that resource-releasing code is called at the end of a function. ~ Description ~~~~~~~~~~~~~ The idea is that every "binder" of the language (var/const/ fn args/match) offers the possibility to mark the variables it binds as "automatic" using the 'auto' keyword. An automatic variable must be of a type that implements the new builtin 'disposable' trait below. When the scope of a variable 'v' marked as automatic ends, '__dispose__(v)' is called. That's it. trait disposable @a = __dispose__ : (val : @a -> void) ;; ~ Example Programs ~~~~~~~~~~~~~~~~~~ The language modification is shown in action in the program below. use std impl disposable int = __dispose__ = {x std.put("__dispose__({})\n", x) } ;; const g = {auto x -> x++ - 1 } const main = { var auto i, auto j = 42 for i = 1; i < 6; i=i+1 var auto z : int = 2*i if i == 3 std.put("z = {} -- cont\n", z) continue ;; std.put("z = {}\n", z) if i/2 == 2 var auto inner : int = 1234 break ;; ;; i = g(321) } The output of the previous test program is: 1: z = 2 2: __dispose__(2) 3: z = 4 4: __dispose__(4) 5: z = 6 -- cont 6: __dispose__(6) 7: z = 8 8: __dispose__(1234) 9: __dispose__(8) 10: __dispose__(322) 11: __dispose__(42) 12: __dispose__(320) Some important remarks: * Unlike Go, __dispose__ is called as soon as the scope of a variable ends, not at the end of the function. In particular, the variable 'z' in the example is always disposed of before starting the next iteration. (An iteration ends the loop body block.) * __dispose__ is called in reverse order of declaration This allows variables to depend on resources of variables already in scope. * Regardless of how a block is exited (fallthrough, break, continue, return), variables of the blocks left are disposed of in reverse order and exactly once. * As line 10 of the output shows, the __dispose__ calls happen "after" the return statement of the function. (It happens after the post-increment, so x's value when it is disposed of is 322.) The following example shows that, using an ad hoc type, it is possible to execute arbitrary code at the end of a scope. type defer = (-> void) impl disposable defer = __dispose__ = {f: defer; f()} ;; const foobar = {... const auto _dummy = ({ std.put("Bye!\n") }: defer) ... } ~ Discussion ~~~~~~~~~~~~ Multiple alternatives exist for resource management, and hopefully this mail starts an interesting debate. According to me, here are the pros and cons of the current proposal: - PROS - * Opt-in * Backward compatible * Simple * The spirit of C's original auto * It has an implementation - CONS - * No safety guarantees/compiler checks whatsoever * Syntactic pollution: 'auto' keyword, 'disposable' trait Finally, note that the current patch does not implement auto support for variables bound in match statements. This will come in a followup patch if there is sufficient interest. Also, the patch does not provide proper support (or proper errors) for gotos and labels.
Diffstat (limited to 'parse/parse.h')
1 files changed, 13 insertions, 0 deletions
diff --git a/parse/parse.h b/parse/parse.h
index 3dc79ba..24c273a 100644
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -85,6 +85,13 @@ struct Tysubst {
size_t nsubst;
+typedef enum {
+ Xcnt,
+ Xbrk,
+ Xret,
+ Nexits
+} Exit;
struct Stab {
Stab *super;
char *name;
@@ -100,6 +107,11 @@ struct Stab {
Htab *env; /* the syms closed over, if this is a function stab */
Htab *lbl; /* labels */
Htab *impl; /* trait implementations: really a set of implemented traits. */
+ Node **autodcl; /* declarations in dcl marked 'auto' */
+ size_t nautodcl;
+ Node *exit[Nexits];
struct Tyenv {
@@ -306,6 +318,7 @@ struct Node {
char isnoret;
char isexportinit;
char isinit;
+ char isauto;
} decl;
struct {