虽然人们发现了一些关于如何用f#计算表达式进行组合递归下降解析器的例子,但我试图将它们用于相反的情况。创建易于阅读的代码以从某些XML数据生成(C++)源文件。然而,我被困住了,如果社区能够帮助我发现我的误解,我将非常感激。为了公共利益,我希望很快这篇文章将展示如何通过f#计算表达式,monadic风格以一种很酷的方式来生成代码生成器。f#用于代码生成的计算表达式
这是多远我走到这一步(简化,省略了代输入数据这个问题的目的):
// in my full fledged application, State type also contains the Input data, used for generating code.
type State() =
let builder = new System.Text.StringBuilder()
let mutable indentLevel : int = 0
member this.Result() = builder.ToString()
member this.Emit (s : string) : unit = builder.Append(s)
// ... Methods allowing to do the indenting right, using indentLevel. And adding Output to the builder instance.
member this.Indent() = indentLevel <- indentLevel + 1
member this.Exdent() = indentLevel <- indentLevel - 1
// The return value of the Formatters is State only to allow for |> pipelining.
type Formatter = State -> State
type FormatterBuilder() =
// Q: Bind() Kind of Looks wrong - should it be a generic, taking one generic first Parameter? See Class function below.
member this.Bind (state,formatter) = formatter state
member this.Return state = state // Q: Not sure if this is the way to go. Maybe some Lambda here?!
let format = new FormatterBuilder()
// Q: Now Comes the part I am stuck in!
// I had the idea to have a "Block" function which
// outputs the "{", increases the indent Level,
// invokes the formatters for the Content of the block,
// then reduces the indent Level, then Closes "}".
// But I have no idea how to write this.
// Here my feeble attempt, not even sure which Parameters this function should take.
let rec Block (formatters : Formatter list) (state : State) : State =
format
{
state.EmitLine("{") // do I Need a "do!" here?
state.Indent()
formatters |> List.iter (fun f -> do! f state) // Q: "state" is not really propagated. How to do this better?
state.Exdent()
state.EmitLine "}"
}
// Functions with "Get" prefix are not shown here. They are supposed to get the Information
// from the Input, stored in State class, which is also not shown here.
let rec Namespace (state : State) : State =
format
{
state.EmitLine(GetNameSpace state)
}
let rec Class (classNode : XmlNode) (state : State) : State =
Format
{
do! TemplateDecl classNode state // TemplateDecl function not shown in sample code
do! ClassDecl classNode state
do! Block [ NestedTypes classNode; Variables classNode; // ... ] // just to give the idea. Q: the list seems wrong here - how to do it better?
}
let GenerateCode() : string =
let state = new State()
format
{
do! Namespace state // Q: Is there a way to get rid of the passing of state here?
do! Block
[ // Q: Maybe a Seq is better than a list here?
for c in State.Classes do // Q: requires override of a few functions in Builder class, I guess?!
do! Class c state
]
}
state.Result()
显然上面的代码充其量只能说明什么,我尽量做到。我的研究没有给出如何使用计算表达式的好例子。我发现很多示例都停留在展示构建器是如何声明或稍后显示的,但未能展示如何实际编写最终表达式。因此,如果有人发现有时间发布一个真正的样本来做我上面的乱码代码试图做的事情,这将是最具启发性的,并填补了互联网上可以找到的关于此的一个空白(至少对于我)混淆f#编程方面。
在我上面的代码示例中,我也看不到我从构建器monad中得到的东西。与非一元实现相比,格式化程序代码看起来不够清晰。
这将是伟大的,如果有人添加签名和类型的答案职位的参数;至少对我来说,与“let-the-compiler-find-the-types”风格相比,它更容易理解。
您是否对*不使用计算表达式的功能解决方案感兴趣?我在一些项目中使用了一个非常类似的东西,它非常简单易用。如果您愿意,我很乐意发布。 –
@JackP。这将丰富讨论的肯定,并将非常欢迎!虽然它不会帮助我理解f#中的单点编程如何工作,但它会很好地匹配手头主题。 – user2173833