diff options
| author | Rose Hogenson <rosehogenson@posteo.net> | 2024-05-18 13:08:31 -0700 |
|---|---|---|
| committer | Rose Hogenson <rosehogenson@posteo.net> | 2024-05-18 13:08:31 -0700 |
| commit | 2581ac99323ead839964e9b5f2a799cf61fd5bdc (patch) | |
| tree | a733c96894f4e8fb37dffb964f57fd39d9996d08 /syntax.sml | |
| parent | 92baa4f6bf99efe229abf2ab895cec8fd7a8936d (diff) | |
| download | sml-2581ac99323ead839964e9b5f2a799cf61fd5bdc.tar.zst | |
Allow pattern matching in function definitions.
Diffstat (limited to 'syntax.sml')
| -rw-r--r-- | syntax.sml | 18 |
1 files changed, 11 insertions, 7 deletions
@@ -32,6 +32,7 @@ struct and dec = DVal of pat * expr | DValRec of pat * expr + | DFun of string * (pat list * expr) list | DDatatype of string * (string * etype option) list (* Lambda language *) @@ -145,6 +146,7 @@ struct case x of DVal (p, e) => "DVal (" ^ patToString p ^ ", " ^ exprToStringI indent e ^ ")" | DValRec (p, e) => "DValRec (" ^ patToString p ^ ", " ^ exprToStringI indent e ^ ")" + | DFun (name, cases) => "DFun (" ^ quote name ^ ", " ^ multilineListToString (fn indent => fn (ps, b) => "(" ^ listToString patToString ps ^ ", " ^ exprToStringI indent b ^ ")") indent cases ^ ")" | DDatatype (name, arms) => "DDatatype (" ^ quote name ^ ", " ^ listToString (fn (con, v) => "(" ^ quote con ^ ", " ^ optionToString etypeToString v ^ ")") arms ^ ")" val exprToString : expr -> string = exprToStringI "" @@ -162,18 +164,20 @@ struct | PEq => "PEq" | PIf => "PIf" - fun lexpToString (x : lexp) : string = + fun lexpToStringI (indent : string) (x : lexp) : string = case x of LVar v => "LVar " ^ Int.toString v - | LFn (arg, expr) => "LFun (" ^ Int.toString arg ^ ", " ^ lexpToString expr ^ ")" - | LFix (decls, body) => "LFix (" ^ listToString (fn (arg, var, expr) => "(" ^ Int.toString arg ^ ", " ^ Int.toString var ^ ", " ^ lexpToString expr ^ ")") decls ^ ", " ^ lexpToString body ^ ")" - | LApp (a, b) => "LApp (" ^ lexpToString a ^ ", " ^ lexpToString b ^ ")" + | LFn (arg, expr) => "LFun (" ^ Int.toString arg ^ ",\n" ^ indent ^ "\t" ^ lexpToStringI (indent ^ "\t") expr ^ ")" + | LFix (decls, body) => "LFix (" ^ multilineListToString (fn indent => fn (arg, var, expr) => "(" ^ Int.toString arg ^ ", " ^ Int.toString var ^ ", " ^ lexpToStringI indent expr ^ ")") indent decls ^ ",\n" ^ indent ^ lexpToStringI indent body ^ ")" + | LApp (a, b) => "LApp (" ^ lexpToStringI indent a ^ ",\n" ^ indent ^ "\t" ^ lexpToStringI (indent ^ "\t") b ^ ")" | LInt i => "LInt " ^ Int.toString i | LString s => "LString " ^ quote s - | LRecord l => "LRecord " ^ listToString lexpToString l - | LSelect (i, r) => "LSelect (" ^ Int.toString i ^ ", " ^ lexpToString r ^ ")" + | LRecord l => "LRecord " ^ listToString (lexpToStringI indent) l + | LSelect (i, r) => "LSelect (" ^ Int.toString i ^ ", " ^ lexpToStringI indent r ^ ")" | LPrim p => "LPrim " ^ primopToString p - | LSwitch (e, arms, otherwise) => "LSwitch (" ^ lexpToString e ^ ", " ^ listToString (fn (x, e) => "(" ^ Int.toString x ^ ", " ^ lexpToString e ^ ")") arms ^ ", " ^ optionToString lexpToString otherwise ^ ")" + | LSwitch (e, arms, otherwise) => "LSwitch (" ^ lexpToStringI indent e ^ ",\n" ^ indent ^ "\t" ^ multilineListToString (fn indent => fn (x, e) => "(" ^ Int.toString x ^ ", " ^ lexpToStringI indent e ^ ")") (indent ^ "\t") arms ^ ",\n" ^ indent ^ "\t" ^ optionToString (lexpToStringI (indent ^ "\t")) otherwise ^ ")" + + fun lexpToString (x : lexp) : string = lexpToStringI "" x fun valueToString (x : value) : string = case x of |
