在为AST节点评估解释器时调用循环函数

时间:2016-06-07 23:35:16

标签: compiler-construction f# functional-programming

我有以下情况:

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调用runIfrunIf调用runStatement,因为if语句是由一些通用语句和一般语句组成的可以是if语句。

我该如何解决这种情况?

PS:我和runWhilerunIfElse等其他函数有类似的情况。

1 个答案:

答案 0 :(得分:8)

使用&#39;和&#39;关键字

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

runx () |> ignore 

打印

runxrunyrunxrunyrunxrunyrunxrunyrunxrunyrunxrunyrunxrunyrunxrunyrunxrunyrunxrunyrunxruny