Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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: 2004-05-25 (11:00)
From: skaller <skaller@u...>
Subject: Re: [Caml-list] handling forward references in a compiler
On Tue, 2004-05-25 at 19:52, 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,
voice: 061-2-9660-0850, 
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: