% $Id: Unparse.oz,v 1.5 2010/08/10 22:19:07 leavens Exp $ % Unparser for a subset of Oz % AUTHOR: Gary T. Leavens \insert 'SeparatedBy.oz' declare fun {UnparseDeclare fDeclare(Body ...)} "declare" # {Unparse Body} end fun {Unparse Stmt} %% ENSURES: Result is a virtual string representing Stmt case Stmt of skipStmt(...) then "skip" [] seqStmt(StmtList ...) then {BlankSeparate {Map StmtList Unparse}} [] localStmt(V Body ...) then {BlankSeparate ["local" V "in" {Unparse Body} "end"]} [] assignStmt(V Exp ...) then V # " = " # {UnparseExp Exp} [] ifStmt(TestExp S1 S2 ...) then {BlankSeparate ["if" {UnparseExp TestExp} "then" {Unparse S1} "else" {Unparse S2} "end"]} [] caseStmt(Exp Pattern S1 S2 ...) then {BlankSeparate ["case" {UnparseExp Exp} "of" {UnparsePattern Pattern} "then" {Unparse S1} "else" {Unparse S2} "end"]} [] applyStmt(ProcExp ArgExpList ...) then {UnparseApply ProcExp ArgExpList} [] threadStmt(S ...) then {BlankSeparate ["thread" {Unparse S} "end"]} [] namedFunStmt(Name Formals Body ...) then {UnparseF "fun" Name Formals {UnparseExp Body}} [] inStmt(Pattern Exp Body ...) then {BlankSeparate [{UnparsePattern Pattern} "=" {UnparseExp Exp} "in" {Unparse Body}]} [] parseError then "Parse-Error!" end end fun {UnparseExp Exp} %% ENSURES: Result is a virtual string representing Exp case Exp of varIdExp(V ...) then V [] atomExp(A ...) then {AtomToString A} [] boolExp(B ...) then if B then "true" else "false" end [] numExp(N ...) then {Value.toVirtualString N 3 3} [] recordExp(LabelExp FieldList ...) then {UnparseRecord {UnparseExp LabelExp} FieldList} [] procExp(Formals Body ...) then {UnparseF "proc" "$" Formals {Unparse Body}} [] ifExp(TestExp E1 E2 ...) then {BlankSeparate ["if" {UnparseExp TestExp} "then" {UnparseExp E1} "else" {UnparseExp E2} "end"]} [] caseExp(Exp Pattern E1 E2 ...) then {BlankSeparate ["case" {UnparseExp Exp} "of" {UnparsePattern Pattern} "then" {UnparseExp E1} "else" {UnparseExp E2} "end"]} [] threadExp(E ...) then {BlankSeparate ["thread" {UnparseExp E} "end"]} [] applyExp(FunExp ArgExpList ...) then {UnparseApply FunExp ArgExpList} end end fun {UnparsePattern Pat} %% ENSURES: Result is a virtual string representing Pat case Pat of varIdPat(V ...) then V [] atomPat(A ...) then {AtomToString A} [] boolPat(B ...) then if B then "true" else "false" end [] recordPat(Atom FieldList ...) then {UnparseRecord {AtomToString Atom} FieldList} end end fun {UnparseField Field} %% ENSURES: Result is a virtual string representing Field case Field of colonFld(Name Exp ...) then {Value.toVirtualString Name 3 3} # ":" # {UnparseExp Exp} [] posFld(Exp ...) then {UnparseExp Exp} end end fun {BlankSeparate LoVS} %% ENSURES: Result is a virtual string corresponding %% to the list of virtual strings LoStr, with elements separated by blanks case LoVS of nil then "" [] S|Rest then S # {FoldR Rest fun {$ Str VSforRest} " " # Str # VSforRest end ""} end end % Common code for unparsing functions and procedures. fun {UnparseF Type Name Formals UnparsedBody} %% ENSURES: Result is a virtual string representing a fun or proc local Nm in Nm = if {IsAtom Name} then {AtomToString Name} % (hack) else Name end Type # " {" # Nm # (if Formals == nil then "" else " " end) # {BlankSeparate {Map Formals UnparsePattern}} # "} " # UnparsedBody # " end" end end % Common code for unparsing apply statements and expressions fun {UnparseApply FE ArgExpList} %% ENSURES: Result is a virtual string representing FE applied to ArgExpList "{" # {UnparseExp FE} # (if ArgExpList == nil then "" else " " end) # {BlankSeparate {Map ArgExpList UnparseExp}} # "}" end % Common code for unparsing record expressions and patterns fun {UnparseRecord Label FieldList} %% ENSURES: Result is a virtual string representing a record expression if (Label == "|" orelse Label == "#") andthen {Length FieldList} == 2 andthen {All FieldList fun {$ F} case F of posFld(_) then true else false end end} then {UnparseField FieldList.1} # Label # {UnparseField FieldList.2.1} else Label # "(" # {BlankSeparate {Map FieldList UnparseField}} # ")" end end