caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jon Harrop <jon@ffconsultancy.com>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] More Caml
Date: Tue, 23 Dec 2008 10:38:42 +0000	[thread overview]
Message-ID: <200812231038.42835.jon@ffconsultancy.com> (raw)
In-Reply-To: <20081223100454.GA30988@annexia.org>

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


  reply	other threads:[~2008-12-23 10:35 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-12-19 13:04 More cores Mikkel Fahnøe Jørgensen
2008-12-19 14:04 ` [Caml-list] " Dario Teixeira
2008-12-19 15:06   ` Alexy Khrabrov
2008-12-19 15:54     ` The Axis of Eval (was: More cores) Dario Teixeira
2008-12-19 16:26       ` [Caml-list] " Paolo Donadeo
2008-12-19 17:01       ` Dario Teixeira
2008-12-19 18:01         ` Christophe Raffalli
2008-12-19 18:50     ` [Caml-list] More cores Ulf Wiger (TN/EAB)
2008-12-19 19:10   ` Richard Jones
2008-12-19 22:31   ` Jon Harrop
2008-12-19 22:36     ` Erik de Castro Lopo
2008-12-19 22:53       ` Jon Harrop
2008-12-22 17:00         ` [Caml-list] More Caml Jon Harrop
2008-12-22 21:44           ` Richard Jones
2008-12-23  6:07             ` Jon Harrop
2008-12-23  9:59               ` Jon Harrop
2008-12-23 15:32                 ` Ashish Agarwal
2008-12-23 17:33                   ` Jon Harrop
2008-12-24 13:12                 ` Mikkel Fahnøe Jørgensen
2008-12-24 16:47                   ` Jon Harrop
2008-12-23 10:04               ` Richard Jones
2008-12-23 10:38                 ` Jon Harrop [this message]
2008-12-23  9:43           ` Oliver Bandel
2008-12-23 11:53             ` Jon Harrop
2008-12-19 22:42     ` [Caml-list] More cores Richard Jones
2008-12-20 19:33     ` Mikkel Fahnøe Jørgensen
2008-12-20 19:41       ` Mikkel Fahnøe Jørgensen
2008-12-19 20:37 ` Oliver Bandel
2008-12-19 21:27   ` Richard Jones
2008-12-19 22:03     ` Hezekiah M. Carty
2008-12-19 22:47       ` Richard Jones
2008-12-19 23:00         ` Alexy Khrabrov
2008-12-19 23:56         ` prelude.ml as another standard extension to Pervasives? Alexy Khrabrov
2008-12-20  1:40           ` [Caml-list] " Mikkel Fahnøe Jørgensen
2008-12-20  4:50             ` Alexy Khrabrov
2008-12-20 10:53               ` Zheng Li
2008-12-20 12:37         ` [Caml-list] More cores Richard Jones

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200812231038.42835.jon@ffconsultancy.com \
    --to=jon@ffconsultancy.com \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).