From c39c43e144733973ebf9cd6842df75b5a5ceee0b Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Fri, 17 May 2024 12:36:56 -0700 Subject: Add datatypes. --- syntax.sml | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'syntax.sml') diff --git a/syntax.sml b/syntax.sml index fbb4dfd..767851d 100644 --- a/syntax.sml +++ b/syntax.sml @@ -12,6 +12,7 @@ struct | PVar of string | PInt of int | PTuple of pat list + | PCon of string * pat datatype expr = EIdent of string list @@ -31,6 +32,7 @@ struct and dec = DVal of pat * expr | DValRec of pat * expr + | DDatatype of string * (string * etype option) list (* Lambda language *) type var = int @@ -101,6 +103,11 @@ struct fun quote (s : string) : string = "\"" ^ String.toString s ^ "\"" + fun optionToString (show : 'a -> string) (x : 'a option) = + case x of + NONE => "NONE" + | SOME x => "SOME " ^ show x + fun etypeToString (x : etype) : string = case x of Tyvar s => "Tyvar " ^ quote s @@ -114,6 +121,7 @@ struct | PVar v => "PVar " ^ quote v | PInt i => "PInt " ^ Int.toString i | PTuple pats => "PTuple " ^ listToString patToString pats + | PCon (con, v) => "PCon " ^ "(" ^ quote con ^ ", " ^ patToString v ^ ")" fun exprToStringI (indent : string) (x : expr) : string = let val self = exprToStringI indent @@ -137,6 +145,7 @@ struct case x of DVal (p, e) => "DVal (" ^ patToString p ^ ", " ^ exprToStringI indent e ^ ")" | DValRec (p, e) => "DValRec (" ^ patToString p ^ ", " ^ exprToStringI indent e ^ ")" + | DDatatype (name, arms) => "DDatatype (" ^ quote name ^ ", " ^ listToString (fn (con, v) => "(" ^ quote con ^ ", " ^ optionToString etypeToString v ^ ")") arms ^ ")" val exprToString : expr -> string = exprToStringI "" -- cgit v1.3.1