From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id NAA21052; Tue, 25 May 2004 13:00:34 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id NAA20029 for ; Tue, 25 May 2004 13:00:32 +0200 (MET DST) Received: from smtp1.adl2.internode.on.net (smtp1.adl2.internode.on.net [203.16.214.181]) by concorde.inria.fr (8.12.10/8.12.10) with ESMTP id i4PB0SSH021393 for ; Tue, 25 May 2004 13:00:30 +0200 Received: from [192.168.1.200] (ppp114-11.lns1.syd3.internode.on.net [150.101.114.11]) by smtp1.adl2.internode.on.net (8.12.9/8.12.9) with ESMTP id i4PB0LZq078715; Tue, 25 May 2004 20:30:23 +0930 (CST) Subject: Re: [Caml-list] handling forward references in a compiler From: skaller Reply-To: skaller@users.sourceforge.net To: dido@imperium.ph Cc: caml-list In-Reply-To: <20040525095213.GA2655@imperium.ph> References: <20040525095213.GA2655@imperium.ph> Content-Type: text/plain Message-Id: <1085482821.6065.526.camel@pelican.wigram> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.2.2 (1.2.2-4) Date: 25 May 2004 21:00:21 +1000 Content-Transfer-Encoding: 7bit X-Miltered: at concorde with ID 40B3274C.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 sourceforge:01 2004:99 dido:01 undeclared:01 resorting:01 hash:01 hash:01 scopes:01 scopes:01 val:01 val:01 9660:01 glebe:01 compiler:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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