-
-
Save teo-tsirpanis/5876f041e3e127b067e56a5b62ec2edf to your computer and use it in GitHub Desktop.
Generating F# code using its AST
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
| [<AutoOpen>] | |
| module ConsoleApp.CreateAst | |
| open Fantomas | |
| open Microsoft.FSharp.Compiler.Ast | |
| open Microsoft.FSharp.Compiler.Range | |
| let createAst() = | |
| // create member | |
| let memberFlags : MemberFlags = {IsInstance = true; IsDispatchSlot = false; IsOverrideOrExplicitImpl = false; IsFinal = false; MemberKind = MemberKind.Member} | |
| let b : SynBindingRcd = | |
| { Access = None | |
| Kind = SynBindingKind.NormalBinding | |
| IsInline = false | |
| IsMutable = false | |
| Attribs = SynAttributes.Empty | |
| XmlDoc = PreXmlDoc.Empty | |
| ValData = SynValData(Some memberFlags, SynValInfo([], SynArgInfo(SynAttributes.Empty, false, None)), None) | |
| Pat = SynPat.LongIdent(LongIdentWithDots([mkId "x"; mkId "Points"], [range.Zero]), None, None, SynConstructorArgs.Pats[], None, range.Zero) | |
| ReturnInfo = None | |
| Expr = SynExpr.Const(SynConst.Int32 3, range.Zero) | |
| Range = range.Zero | |
| Bind = SequencePointInfoForBinding.NoSequencePointAtInvisibleBinding | |
| } | |
| // create Type | |
| let ti : SynComponentInfoRcd = | |
| { Attribs = SynAttributes.Empty | |
| TyParams = [] | |
| Constraints = [] | |
| Id = [mkId "Triangle"] | |
| XmlDoc = PreXmlDoc.Empty | |
| PreferPostfix = false | |
| Access = None | |
| Range = range.Zero | |
| } | |
| let ms : SynMemberDefns = | |
| [ | |
| SynMemberDefn.ImplicitCtor(None, SynAttributes.Empty, [], None, range.Zero) | |
| SynMemberDefn.Member(b.FromRcd, range.Zero) | |
| ] | |
| let r : SynTypeDefnReprObjectModelRcd = | |
| { //Kind = SynTypeDefnKind.TyconClass | |
| Kind = SynTypeDefnKind.TyconUnspecified | |
| Members = ms | |
| Range = range.Zero | |
| } | |
| let t : SynTypeDefnRcd = | |
| { Info = ti.FromRcd | |
| Repr = r.FromRcd | |
| Members = [] | |
| Range = range.Zero | |
| } | |
| // create module | |
| let m : SynModuleOrNamespaceRcd = | |
| { Id = [mkId "Hello"] | |
| IsModule = true | |
| Decls = [SynModuleDecl.Types([t.FromRcd], range.Zero)] | |
| XmlDoc = PreXmlDoc.Empty | |
| Attribs = SynAttributes.Empty | |
| Access = None | |
| Range = range.Zero | |
| } | |
| // create file | |
| let pi : ParsedImplFileInputRcd = | |
| { File = "Hello.fs" | |
| IsScript = false | |
| QualName = QualifiedNameOfFile(mkId "Hello") | |
| Pragmas = [] | |
| HashDirectives = [] | |
| Modules = [m.FromRcd] | |
| IsLastCompiland = true | |
| } | |
| let txt = formatAst (ParsedInput.ImplFile pi.FromRcd) | |
| printfn "%s" txt |
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
| [<AutoOpen>] | |
| module ConsoleApp.FormatFs | |
| open System.IO | |
| open Fantomas | |
| let formatAst ast = | |
| let cfg = { FormatConfig.FormatConfig.Default with StrictMode = true } // no comments | |
| let noOriginalSourceCode = "//" | |
| CodeFormatter.formatAST ast noOriginalSourceCode cfg | |
| let formatFs() = | |
| let s = File.ReadAllText @"C:\Projects\fantomas\src\ConsoleApplication1\Hello.fs" | |
| let isFsi = false | |
| let ast = CodeFormatter.parse isFsi s | |
| let txt = formatAst ast | |
| printfn "%s" txt |
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
| module Hello | |
| type Triangle () = | |
| member x.Points = 3 |
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
| module Hello | |
| type Triangle() = | |
| member x.Points = 3 |
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
| [<AutoOpen>] | |
| module ConsoleApp.PrintAstInfo | |
| open System.IO | |
| open Fantomas | |
| open Microsoft.FSharp.Compiler.Ast | |
| let printAstInfo() = | |
| let s = File.ReadAllText @"C:\Projects\fantomas\src\ConsoleApplication1\Hello.fs" | |
| let isFsi = false | |
| let ast = CodeFormatter.parse isFsi s | |
| match ast with | |
| | ParsedInput.ImplFile f -> | |
| let fr = f.ToRcd | |
| let m = fr.Modules.Head.ToRcd | |
| printfn "module: %s" m.Id.Head.idText | |
| match m.Decls.Head with | |
| | SynModuleDecl.Types(types, _) -> | |
| let t = types.Head.ToRcd | |
| printfn "type: %s" t.Info.ToRcd.Id.Head.idText | |
| let om = t.Repr.ToObjectModelRcd | |
| let m1 = om.Members.[1] // [[0] is ImplicitCtor | |
| match om.Members.[1] with | |
| | SynMemberDefn.Member(b, _) -> | |
| let br = b.ToRcd | |
| match br.Pat with | |
| | SynPat.LongIdent(id,_,_,_,_,_) -> | |
| printfn "member: %s %s" id.Lid.[0].idText id.Lid.[1].idText | |
| match br.Expr with | |
| | SynExpr.Const(c,_) -> | |
| match c with | |
| | SynConst.Int32 i -> printfn "member value: %d" i |
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
| module: Hello | |
| type: Triangle | |
| member: x Points | |
| member value: 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment