Advertisement
Guest User

Compiler

a guest
Jun 25th, 2015
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.60 KB | None | 0 0
  1. module Compiler
  2.  
  3. open Types
  4. open Microsoft.CSharp
  5. open System.CodeDom.Compiler
  6. open System.IO
  7. open System.Runtime.Serialization
  8. open System.Reflection
  9.  
  10. (* Translate an IMP expression to C# *)
  11. let rec translate_e (e : Expr) : string =
  12.     match e with
  13.     | Const(v) ->
  14.         match v with
  15.         | Value.Int(x) -> string x
  16.         | Value.Bool(true) -> "true"
  17.         | Value.Bool(false) -> "false"
  18.     | Var(x) -> x
  19.     | Plus(e1, e2) -> (translate_e e1) + "+" + (translate_e e2)
  20.     | Times(e1, e2) -> (translate_e e1) + "*" + (translate_e e2)
  21.     | GreaterThan(e1, e2) -> (translate_e e1) + ">" + (translate_e e2)
  22.  
  23. (* Translate an IMP term to C# *)
  24. and translate_s (s : Term) : string =
  25.     match s with
  26.     | VarDecl(x, e) -> "var " + x + " = " + (translate_e e) + "; "
  27.     | VarAssign(x, e) -> x + "=" + (translate_e e) + "; "
  28.     | CodeBlock(x) ->
  29.         match x with
  30.         | x :: xs ->
  31.             let m' = translate_s x
  32.            m' + translate_s (CodeBlock xs)
  33.         | [] -> ""
  34.     | If(c, t, e) -> "if (" + (translate_e c) + ") {" + (translate_s t) + "} else {" + (translate_s e) + "} "
  35.     | While(c, d) -> "while (" + (translate_e c) + ") {" + (translate_s d) + "} "
  36.     | Print(e) -> "Console.WriteLine(" + (translate_e e) + "); "
  37.     | EOF -> ""
  38.  
  39. (* Translate the program *)
  40. let generateSources (l : List<Term>) s =
  41.     let rec generateLines (t : List<Term>) =
  42.         match t with
  43.         | x :: xs -> (translate_s x) + (generateLines xs)
  44.         | [] -> ""
  45.    
  46.     let gen = "using System; namespace Generated {class Program {static void Main() {" + (generateLines l) + "}}}"
  47.     do printfn "\nGENERATED:\n%s\n" gen
  48.     File.WriteAllText(s, gen)
  49.  
  50. (* Generate a C# source (path s) and an executable file *)
  51. let compileAndRun s =
  52.     let generatedPath = s + ".cs" // path containing C# sources to compile
  53.     let code = File.ReadAllLines(generatedPath)
  54.     let dll_name = "Program" // path to C# DLL to generate
  55.     let args = new System.Collections.Generic.Dictionary<string, string>()
  56.     do args.Add("CompilerVersion", "v4.5")
  57.     let provider = CodeDomProvider.CreateProvider("CSharp")
  58.     let parameters = new CompilerParameters()
  59.     do parameters.OutputAssembly <- s + ".dll"
  60.     do parameters.GenerateExecutable <- true
  61.     do parameters.CompilerOptions <- @"/out:"+ s+ ".exe"
  62.     let results = provider.CompileAssemblyFromSource(parameters, code)
  63.     if results.Errors.HasErrors then
  64.         for error in results.Errors do
  65.             if error.IsWarning |> not then do printfn "%s at %d: %s" error.FileName error.Line error.ErrorText
  66.         ()
  67.     else ()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement