Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] handling forward references in a compiler
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: skaller <skaller@u...>
Subject: Re: [Caml-list] handling forward references in a compiler
On Tue, 2004-05-25 at 19:52, dido@imperium.ph wrote:
> I'm writing a compiler for a language that allows undeclared forward
> references in OCaml, and I'm just wondering how one is supposed to
> handle these sorts of things without resorting to imperative features or
> multiple passes.

What's wrong with multiple passes?

Felix uses many passes. It doesn't backpatch though.
I make a hash table keyed by a fresh integer
with data of each definition, and a set of hash tables
representing scopes, keyed by the identifier with
data the integer.

In the next pass, I bind uses by looking up the
name in the scopes to find the integer.

The effect is that scopes are all mutually recursive,
and lookup is always random access within a scope:
forward reference declarations are never needed
because the whole scope is always searched.

For example:

fun f():int { return g(); }
fun g():int { return f(); }

works just fine. You can also write:

val x = y;
val y = 1;

which also binds just fine .. although it doesn't
do anything sensible at run time.

-- 
John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850, 
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net



-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners