% $Id: Conversions.oz,v 1.3 2012/01/07 23:20:35 leavens Exp $ % Conversions between expressions and pattern ASTs % AUTHOR: Gary T. Leavens declare fun {Pattern2Exp Pat} %% ENSURES: Result is an expression AST that represents the pattern Pat case Pat of varIdPat(V ...) then varId(V) [] atomPat(A ...) then atomExp(A) [] boolPat(B ...) then boolExp(B) [] recordPat(atomExp(A) FL ...) then recordExp(atomExp(A) {Map FL PatField2ExpField}) end end fun {Exp2Pattern Expr} %% ENSURES: Result is an pattern AST that represents the expression Exp case Expr of varId(V pos:POS) then varIdPat(V pos:POS) [] varId(V ...) then varIdPat(V) [] atomExp(A pos:POS) then atomPat(A pos:POS) [] atomExp(A ...) then atomPat(A) [] boolExp(B pos:POS) then boolPat(B pos:POS) [] boolExp(B ...) then boolPat(B) [] recordExp(atomExp(A) FL pos:POS) then recordPat(A {Map FL ExpField2PatField} pos:POS) [] recordExp(atomExp(A) FL ...) then recordPat(A {Map FL ExpField2PatField}) else {Exception.raiseError wrongExpressionForPattern(Expr)} raise Expr end end end fun {PatField2ExpField F} case F of colonFld(N P pos:POS) then colonFld(N {Pattern2Exp P} pos:POS) [] colonFld(N P ...) then colonFld(N {Pattern2Exp P}) [] posFld(P pos:POS) then posFld({Pattern2Exp P} pos:POS) [] posFld(P ...) then posFld({Pattern2Exp P}) end end fun {ExpField2PatField F} case F of colonFld(N E pos:POS) then colonFld(N {Exp2Pattern E} pos:POS) [] colonFld(N E ...) then colonFld(N {Exp2Pattern E}) [] posFld(E pos:POS) then posFld({Exp2Pattern E} pos:POS) [] posFld(E ...) then posFld({Exp2Pattern E}) end end