Created
September 10, 2012 21:43
-
-
Save hodzanassredin/3694117 to your computer and use it in GitHub Desktop.
lisp dsl in fsharp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Expr = | ListOfExpr of list<Expr> | |
| | Func of (list<Expr> -> Expr) | |
| | Macro of (list<Expr> -> list<Expr>) | |
| | Int of int | |
| | Void;; | |
| let rec evalArgs l = | |
| match l with | |
| | h :: tail -> (eval h) :: evalArgs tail | |
| | [] -> [] | |
| | _ -> failwith "fuck" | |
| and eval l = | |
| match l with | |
| | ListOfExpr ((Func f) :: tail) -> f (evalArgs tail) | |
| | ListOfExpr ((Macro f) :: tail) -> eval (ListOfExpr (f tail)) | |
| | ListOfExpr ((ListOfExpr f) :: tail) -> eval (ListOfExpr f) | |
| | Int i -> Int i | |
| | Void -> Void | |
| | _ -> failwith "fuck";; | |
| let rec print l = | |
| match l with | |
| | ListOfExpr ((Func f) :: tail) -> "f(" + (ListOfExpr tail |> print) + ")" | |
| | ListOfExpr ((Macro f) :: tail) -> "m(" + (ListOfExpr tail |> print) + ")" | |
| | ListOfExpr [] -> "" | |
| | ListOfExpr (h :: tail) -> print h + "," + (ListOfExpr tail |> print) | |
| | Int i -> i.ToString() | |
| | Void -> "Void" | |
| | _ -> failwith "fuck";; | |
| let plus args= | |
| let rec plusInt arguments = | |
| match arguments with | |
| | (Int h) :: t -> h + plusInt t | |
| | (ListOfExpr h) :: t -> plusInt h + plusInt t | |
| | [] -> 0 | |
| | _ -> failwith "fuck" in Int (plusInt args);; | |
| let rec doubleIntExp args: list<Expr>= | |
| match args with | |
| | Int h :: tail -> Int h :: Int h :: (doubleIntExp tail) | |
| | ListOfExpr h :: tail -> (doubleIntExp h) @ (doubleIntExp tail) | |
| | h :: tail -> h :: (doubleIntExp tail) | |
| | [] -> [] | |
| | _ -> failwith "fuck";; | |
| let script = ListOfExpr [Macro doubleIntExp; ListOfExpr [Func plus;Int 2;Int 3; Int 4]];; | |
| [<EntryPoint>] | |
| let main argv = | |
| script|> eval |> print |> printfn "Res %s" | |
| System.Console.ReadKey() |> ignore | |
| 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment