jon at ffconsultancy.com
Sat Nov 24 21:31:28 PST 2007
On Sunday 25 November 2007 05:28, Aaron Gray wrote:
> > On Sunday 25 November 2007 03:42, Christopher Lamb wrote:
> >> Try this google query. I know there's been some discussion/work on
> >> OCaml and LLVM.
> >> site:lists.cs.uiuc.edu/pipermail/llvmdev OCaml interface
> > I just rediscovered the OCaml bindings in bindings/ocaml (rather than the
> > ones
> > in test/Bindings/OCaml!). They do indeed look quite complete but I can't
> > find
> > any examples using them. I think a translation of the tutorial would be
> > most
> > welcome and about 10x shorter. ;-)
> Lexing is the one issue though.
How do you mean?
I'm just fiddling around with it now. The lexer, parser and AST written using
camlp4 might look something like this in OCaml:
type ast =
| Num of float
| Var of string
| BinOp of [ `Add | `Sub | `Mul | `Less ] * ast * ast
| Call of string * ast list
| Function of string * string list * ast
let expr = Gram.Entry.mk "expr" ;;
[ [ e1 = expr; "+"; e2 = expr -> BinOp(`Add, e1, e2)
| e1 = expr; "-"; e2 = expr -> BinOp(`Sub, e1, e2) ]
| [ e1 = expr; "*"; e2 = expr -> BinOp(`Mul, e1, e2) ]
| [ e1 = expr; "<"; e2 = expr -> BinOp(`Less, e1, e2) ]
| [ "("; e = expr; ")" -> e ]
| [ f = STRING; "("; args = LIST0 expr; ")" -> Call(f, args) ]
| [ "def"; f = STRING; "("; vars = LIST0 [ s = STRING -> s ]; ")"; body =
Function(f, vars, body) ]
| [ x = FLOAT -> Num(float_of_string x) ]
| [ v = LIDENT -> Var v ]
Probably better to use conventional lex and yacc though...
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
More information about the llvm-dev