2 people like it.
Like the snippet!
Simple F# Expression to Java compiler
Compiles simple F# quoted expressions (literals, values, operators, ifs and for loops) to Java code.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
|
open Microsoft.FSharp.Quotations
open Microsoft.FSharp.Quotations.Patterns
open Microsoft.FSharp.Quotations.DerivedPatterns
let notImplemented () = raise (System.NotImplementedException())
let toTypeName t =
if t = typeof<int> then "int"
elif t = typeof<double> then "double"
elif t = typeof<bool> then "boolean"
else notImplemented()
let rec toJava = function
| Int32 x -> sprintf "%i" x
| Double x -> sprintf "%gd" x
| Bool true -> "true"
| Bool false -> "false"
| SpecificCall <@@ (+) @@> (None, _, [lhs;rhs]) -> toOp "+" lhs rhs
| SpecificCall <@@ (-) @@> (None, _, [lhs;rhs]) -> toOp "-" lhs rhs
| SpecificCall <@@ (*) @@> (None, _, [lhs;rhs]) -> toOp "*" lhs rhs
| SpecificCall <@@ (/) @@> (None, _, [lhs;rhs]) -> toOp "/" lhs rhs
| SpecificCall <@@ (=) @@> (None, _, [lhs;rhs]) -> toOp "==" lhs rhs
| SpecificCall <@@ (<>) @@> (None, _, [lhs;rhs]) -> toOp "!=" lhs rhs
| IfThenElse(condition, t, f) -> toIfThenElse condition t f
| _ -> notImplemented()
and toOp op lhs rhs =
sprintf "(%s %s %s)" (toJava lhs) op (toJava rhs)
and toIfThenElse condition t f =
sprintf "%s ? %s : %s" (toJava condition) (toJava t) (toJava f)
let toClass (expr:Expr<'TRet>) =
let returnType = toTypeName typeof<'TRet>
let body = toJava expr
sprintf """
public class Generated {
public static %s fun(){
return %s;
}
public static void main(String []args){
System.out.println(fun());
}
}""" returnType body
toClass <@ if 1 + 1 = 2 then 1 else -1 @>
(* Returns
public class Generated {
public static int fun(){
return ((1 + 1) == 2) ? 1 : -1;
}
public static void main(String []args){
System.out.println(fun());
}
}
*)
|
namespace Microsoft
namespace Microsoft.FSharp
namespace Microsoft.FSharp.Quotations
module Patterns
from Microsoft.FSharp.Quotations
module DerivedPatterns
from Microsoft.FSharp.Quotations
val notImplemented : unit -> 'a
Full name: Script.notImplemented
val raise : exn:System.Exception -> 'T
Full name: Microsoft.FSharp.Core.Operators.raise
namespace System
Multiple items
type NotImplementedException =
inherit SystemException
new : unit -> NotImplementedException + 2 overloads
Full name: System.NotImplementedException
--------------------
System.NotImplementedException() : unit
System.NotImplementedException(message: string) : unit
System.NotImplementedException(message: string, inner: exn) : unit
val toTypeName : t:System.Type -> string
Full name: Script.toTypeName
val t : System.Type
val typeof<'T> : System.Type
Full name: Microsoft.FSharp.Core.Operators.typeof
Multiple items
val int : value:'T -> int (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.int
--------------------
type int = int32
Full name: Microsoft.FSharp.Core.int
--------------------
type int<'Measure> = int
Full name: Microsoft.FSharp.Core.int<_>
Multiple items
val double : value:'T -> float (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.double
--------------------
type double = System.Double
Full name: Microsoft.FSharp.Core.double
type bool = System.Boolean
Full name: Microsoft.FSharp.Core.bool
val toJava : _arg1:Expr -> string
Full name: Script.toJava
active recognizer Int32: Expr -> int32 option
Full name: Microsoft.FSharp.Quotations.DerivedPatterns.( |Int32|_| )
val x : int32
val sprintf : format:Printf.StringFormat<'T> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
active recognizer Double: Expr -> float option
Full name: Microsoft.FSharp.Quotations.DerivedPatterns.( |Double|_| )
val x : float
active recognizer Bool: Expr -> bool option
Full name: Microsoft.FSharp.Quotations.DerivedPatterns.( |Bool|_| )
active recognizer SpecificCall: Expr -> Expr -> (Expr option * System.Type list * Expr list) option
Full name: Microsoft.FSharp.Quotations.DerivedPatterns.( |SpecificCall|_| )
union case Option.None: Option<'T>
val lhs : Expr
val rhs : Expr
val toOp : op:string -> lhs:Expr -> rhs:Expr -> string
Full name: Script.toOp
active recognizer IfThenElse: Expr -> (Expr * Expr * Expr) option
Full name: Microsoft.FSharp.Quotations.Patterns.( |IfThenElse|_| )
val condition : Expr
val t : Expr
val f : Expr
val toIfThenElse : condition:Expr -> t:Expr -> f:Expr -> string
Full name: Script.toIfThenElse
val op : string
val toClass : expr:Expr<'TRet> -> string
Full name: Script.toClass
val expr : Expr<'TRet>
Multiple items
type Expr =
override Equals : obj:obj -> bool
member GetFreeVars : unit -> seq<Var>
member Substitute : substitution:(Var -> Expr option) -> Expr
member ToString : full:bool -> string
member CustomAttributes : Expr list
member Type : Type
static member AddressOf : target:Expr -> Expr
static member AddressSet : target:Expr * value:Expr -> Expr
static member Application : functionExpr:Expr * argument:Expr -> Expr
static member Applications : functionExpr:Expr * arguments:Expr list list -> Expr
...
Full name: Microsoft.FSharp.Quotations.Expr
--------------------
type Expr<'T> =
inherit Expr
member Raw : Expr
Full name: Microsoft.FSharp.Quotations.Expr<_>
val returnType : string
val body : string
More information