Commits

Anonymous committed ee89d9d

gen ruby and javascript

  • Participants
  • Parent commits 3961e1e

Comments (0)

Files changed (5)

 	 Lambda Translator
 
 - parsing lambda expression
-- generate Scheme code
+- generate Scheme code (R5RS)
+- generate Ruby code (version 1.9.1 or upper)
+- generate Javascript code
 (** TODO
-- generate Ruby code
-- generate Javascript code
 - generate OCaml code
 - generate Haskell code
 - generate Gallina(Coq) code
 	$ sh compile.sh
 
 USAGE:
-	$ echo 'lambda f. lambda x. f x' | ./run
+	$ ./run < samples/test01.lambda
+
+	$ ./run < samples/test02.lambda
 #!/bin/sh
 ocamllex lexer.mll
-ocamlc -o run util.ml lexer.ml term.ml parserUtil.ml parser.ml gen_scheme.ml main.ml
+ocamlc -o run util.ml lexer.ml term.ml parserUtil.ml parser.ml gen_scheme.ml gen_ruby.ml gen_javascript.ml main.ml
 rm *.cmi *.cmo

File gen_javascript.ml

+open Term
+
+let rec gen = function
+  | Var v -> v
+  | Abs (v, t) -> "(function (" ^ v ^ ") { return (" ^ gen t ^ "); })"
+  | App (t1, t2) -> gen t1 ^ "(" ^ gen t2 ^ ")"
+open Term
+
+let rec gen = function
+  | Var v -> v
+  | Abs (v, t) -> "lambda { |" ^ v ^ "| " ^ gen t ^ "}"
+  | App (t1, t2) -> "(" ^ gen t1 ^ ").call(" ^ gen t2 ^ ")"
     let ts = tokens [] in
     let tm = Parser.parse ts in
     prerr_endline @@ "parse: " ^ sterm tm;
+    prerr_newline ();
+    prerr_endline @@ " -- scheme code";
     prerr_endline @@ Gen_scheme.gen tm;
+
+    prerr_newline ();
+    prerr_endline @@ " -- ruby code";
+    prerr_endline @@ Gen_ruby.gen tm;
+
+    prerr_newline ();
+    prerr_endline @@ " -- javascript code";
+    prerr_endline @@ Gen_javascript.gen tm;
+
   with
   | Parser.Error msg -> prerr_endline @@ "parse error: " ^ msg;
   | e -> prerr_endline ("ERRR: " ^ Printexc.to_string e)