假设我正在为F#中的特定于域的语言构建解析器.
我已经定义了一个区分联合来表示表达式:
type Expression =
| Equality of Expression*Expression
| NonEquality of Expression*Expression
| Or of Expression*Expression
| And of Expression*Expression
| If of Expression*Expression
| IfElse of Expression*Expression*Expression
| Bool of bool
| Variable of string
| StringLiteral of string
Run Code Online (Sandbox Code Playgroud)
现在,我已经建立了一个类型的AST,Expression并希望为它生成代码.我有一个函数,它对表达式进行类型推断和类型检查.
它被定义为
let rec InferType expr =
match expr with
| Equality(e1,e2) -> CheckTypes (InferType e1) (InferType e2)
| Or(e1,e2) -> CheckTypes (InferType e1) (InferType e2)
| And(e1,e2) -> CheckTypes (InferType e1) (InferType e2)
...
Run Code Online (Sandbox Code Playgroud)
我还有另一个函数来生成遵循类似模式的代码:获取表达式,为union中的每个项写入模式匹配语句.
我的问题是:这是用F#做的惯用方式吗? …
f# ×1