gpt4 book ai didi

compiler-construction - 为解释器评估 AST 节点时的循环函数调用

转载 作者:行者123 更新时间:2023-12-04 17:44:59 26 4
gpt4 key购买 nike

我有以下情况:

let private runStatement (vars : Map<identifier, value>) stmt =
match stmt with
| Assignment (id, expr) -> runAssignment vars id expr
| Print exprs -> runPrint vars exprs
| Read id -> runRead vars id
| If (cond, stmts) -> runIf vars cond stmts

let rec private runStatements vars stmts =
match stmts with
| stmt::rest ->
let newVars = runStatement vars stmt
runStatements newVars rest
| [] -> vars

let private runIf vars conditionalValue statements =
match conditionalValue with
| Boolean v when v -> runStatements vars statements
| Boolean v -> vars
| _ -> failwith "Not a boolean expression in if statement"

如您所见,函数 runStatement电话 runIf , 和 runIf电话 runStatement ,因为一个 if 语句是由一些通用语句组成的,而一个通用语句可以是一个 if 语句。

我该如何解决这种情况?

PS。:我有类似的情况与其他功能,如 runWhile , runIfElse等等。

最佳答案

使用“and”关键字

let rec runx () = 
printf "runx"
runy ()
and runy () =
printf "runy"
runx ()

runx () |> ignore

打印
runxrunyrunxrunyrunxrunyrunxrunyrunxrunyrunxrunyrunxrunyrunxrunyrunxrunyrunxrunyrunxruny

关于compiler-construction - 为解释器评估 AST 节点时的循环函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37691097/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com