Looping function calls when evaluating AST nodes for an interpreter

I have the following situation:

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" 

As you can see, the runStatement function calls runIf , and runIf calls runStatement , because the if statement is generated by some common statements, and the common statement can be if-statement.

How can I solve this situation?

PS: I have similar situations with other functions like runWhile , runIfElse et cetera.

+5
source share
1 answer

Use the keyword 'and'

 let rec runx () = printf "runx" runy () and runy () = printf "runy" runx () runx () |> ignore 

prints

 runxrunyrunxrunyrunxrunyrunxrunyrunxrunyrunxrunyrunxrunyrunxrunyrunxrunyrunxrunyrunxruny 
+8
source

All Articles