From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.6 required=5.0 tests=AWL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id A97C4BB84 for ; Tue, 23 Dec 2008 11:35:39 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ak0BAOVNUEnUnw6TlGdsb2JhbACCO5EzAQEBAQkLCAkRBKpmWIUACIwAhQsIgTA X-IronPort-AV: E=Sophos;i="4.36,270,1228086000"; d="scan'208";a="21663404" Received: from ptb-relay03.plus.net ([212.159.14.147]) by mail1-smtp-roc.national.inria.fr with ESMTP/TLS/AES256-SHA; 23 Dec 2008 11:35:39 +0100 Received: from [87.114.38.62] (helo=leper.local) by ptb-relay03.plus.net with esmtp (Exim) id 1LF4by-00022K-Cn for caml-list@yquem.inria.fr; Tue, 23 Dec 2008 10:35:38 +0000 From: Jon Harrop Organization: Flying Frog Consultancy Ltd. To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] More Caml Date: Tue, 23 Dec 2008 10:38:42 +0000 User-Agent: KMail/1.9.9 References: <157727.93194.qm@web111508.mail.gq1.yahoo.com> <200812230607.37399.jon@ffconsultancy.com> <20081223100454.GA30988@annexia.org> In-Reply-To: <20081223100454.GA30988@annexia.org> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200812231038.42835.jon@ffconsultancy.com> X-Plusnet-Relay: 833f8f3191d5fe47b037cbdcec6b2c80 X-Spam: no; 0.00; compiler:01 ocaml's:01 bytecode:01 syntax:01 camlp:01 ocaml:01 camlp:01 tuareg:01 printf:01 printf:01 parser:01 datatypes:01 ocamllex:01 ocamlyacc:01 lexer:01 On Tuesday 23 December 2008 10:04:55 Richard Jones wrote: > On Tue, Dec 23, 2008 at 06:07:37AM +0000, Jon Harrop wrote: > > another interesting project and a JIT compiler for OCaml's existing > > bytecode would also be nice. > > Probably easier to start with the abstract syntax tree that camlp4 > writes out. http://brion.inria.fr/gallium/index.php/AST Actually I was previously compiling quoted OCaml code via Camlp4. However, lack of support for camlp4 macros in Tuareg and lack of documentation about Camlp4 itself (how do you get a tuple "out"!?) made this sufficiently painful that I ditched it and opted for writing out the AST in variant types by hand instead, e.g. my mandelbrot benchmark: let mandelbrot n = [Defn.Function("lerp", ["i", Type.Int; "n", Type.Int], [Type.Float], Float 2.0 *:. FloatOfInt(Var "i") /:. FloatOfInt(Var "n") -:. Float 1.0); Defn.Function("zsqr", ["r", Type.Float; "i", Type.Float], [Type.Float; Type.Float], Values[Var "r" *:. Var "r" -:. Var "i" *:. Var "i"; Float 2.0 *:. Var "r" *:. Var "i"]); Defn.Function("znorm2", ["r", Type.Float; "i", Type.Float], [Type.Float], Var "r" *:. Var "r" +:. Var "i" *:. Var "i"); Defn.Function ("pixel2", ["n", Type.Int; "zr", Type.Float; "zi", Type.Float; "cr", Type.Float; "ci", Type.Float], [Type.String], If(Var "n" =: Int 65536, String " ", If(apply(Var "znorm2", [Var "zr"; Var "zi"], Type.Float) >=:. Float 4.0, String ".", Call(["zr", Type.Float; "zi", Type.Float], Var "zsqr", [Var "zr"; Var "zi"], apply(Var "pixel2", [Var "n" +: Int 1; Var "zr" +:. Var "cr"; Var "zi" +:. Var "ci"; Var "cr"; Var "ci"], Type.Unit))))); Defn.Function ("pixel", ["n", Type.Int; "zr", Type.Float; "zi", Type.Float; "cr", Type.Float; "ci", Type.Float], [Type.String], If(Var "n" =: Int 65536, String " ", If(Var "zr" *:. Var "zr" +:. Var "zi" *:. Var "zi" >=:. Float 4.0, String ".", apply(Var "pixel", [Var "n" +: Int 1; Var "zr" *:. Var "zr" -:. Var "zi" *:. Var "zi" +:. Var "cr"; Float 2.0 *:. Var "zr" *:. Var "zi" +:. Var "ci"; Var "cr"; Var "ci"], Type.Unit)))); Defn.Function ("row", ["i", Type.Int; "j", Type.Int; "n", Type.Int], [], If(Var "i" >: Var "n", Unit, Compound [ Printf[apply(Var "pixel", [Int 0; Float 0.0; Float 0.0; apply(Var "lerp", [Var "i"; Var "n"], Type.Float); apply(Var "lerp", [Var "j"; Var "n"], Type.Float)], Type.Unit)]; apply(Var "row", [Var "i" +: Int 1; Var "j"; Var "n"], Type.Unit)])); Defn.Function ("col", ["j", Type.Int; "n", Type.Int], [], If(Var "j" >: Var "n", Unit, Compound [ apply(Var "row", [Int 0; Var "j"; Var "n"], Type.Unit); Printf[String "\n"]; apply(Var "col", [Var "j" +: Int 1; Var "n"], Type.Unit)])); Defn.Function ("main", [], [], apply(Var "col", [Int 0; Int n], Type.Unit))] I'll probably just write a "real" parser and implement a REPL for it instead, probably after I've got algebraic datatypes and generic printing working. Hmm, maybe I should use ocamllex and ocamlyacc instead of Camlp4 because I'd like a better lexer as well (e.g. complex literals). I had been thinking about using parser combinators to make bootstrapping easier but they're a world of pain and I don't actually see any point in bootstrapping anyway. Are there any usable OCaml frameworks that can parse and pretty print (i.e. remove superfluous parentheses by taking associativity, precedence and fixity into account) from the same grammar definition? -- Dr Jon Harrop, Flying Frog Consultancy Ltd. http://www.ffconsultancy.com/?e