From bc2fef3a2ab81270af207568706647ced919bbe6 Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Wed, 7 Feb 2024 21:08:35 -0800 Subject: Support function arguments. --- syntax.sml | 104 ++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 62 insertions(+), 42 deletions(-) (limited to 'syntax.sml') diff --git a/syntax.sml b/syntax.sml index 95487d7..2a7ee9a 100644 --- a/syntax.sml +++ b/syntax.sml @@ -1,55 +1,69 @@ structure Syntax = struct (* SML syntax *) - datatype etype = Tyvar of string - | Tycon of etype list * string - | TyTuple of etype list - | Tyfun of etype * etype - - datatype expr = EIdent of string list - | EBuiltin of string - | EInt of int - | EStr of string - | ETuple of expr list - | EList of expr list - | EApp of expr * expr - | ETyped of expr * etype - | EAndAlso of expr * expr - | EOrElse of expr * expr - | ELet of dec list * expr - | ELambda of expr + datatype etype = + Tyvar of string + | Tycon of etype list * string + | TyTuple of etype list + | Tyfun of etype * etype + + datatype pat = + PWild + | PVar of string + + datatype expr = + EIdent of string list + | EBuiltin of string + | EInt of int + | EStr of string + | ETuple of expr list + | EList of expr list + | EApp of expr * expr + | ETyped of expr * etype + | EAndAlso of expr * expr + | EOrElse of expr * expr + | ELet of dec list * expr + | ELambda of pat * expr and dec = DVal of expr (* Lambda language *) type var = int + datatype primop = PExit - datatype lexp = LVar of var - | LFn of var * lexp - | LApp of lexp * lexp - | LInt of int - | LString of string - | LRecord of lexp list - | LPrim of primop + + datatype lexp = + LVar of var + | LFn of var * lexp + | LFix of (var * var * lexp) list * lexp + | LApp of lexp * lexp + | LInt of int + | LString of string + | LRecord of lexp list + | LPrim of primop (* CPS *) - datatype value = VVar of var - | VLabel of var - | VInt of int - | VString of string - datatype cexp = CRecord of (value * int list) list * var * cexp - | CSelect of int * value * var * cexp - | CApp of value * value list - | CFix of (var * var list * cexp) list * cexp - | CPrimop of primop * value list * var list * cexp list - - datatype opcode = OAlloc of var * value - | OCall - | OPoke of int * var * value - | OPeek of var * int * value - | OShuf of var * value - | OExit of value - | OLabel of var + datatype value = + VVar of var + | VLabel of var + | VInt of int + | VString of string + + datatype cexp = + CRecord of (value * int list) list * var * cexp + | CSelect of int * value * var * cexp + | CApp of value * value list + | CFix of (var * var list * cexp) list * cexp + | CPrimop of primop * value list * var list * cexp list + + datatype opcode = + OAlloc of var * value + | OCall + | OPoke of int * var * value + | OPeek of var * int * value + | OShuf of var * value + | OExit of value + | OLabel of var fun listToString (show : 'a -> string) (l : 'a list) = "[" ^ String.concatWith ", " (map show l) ^ "]" @@ -72,6 +86,11 @@ struct | TyTuple args => "TyTuple " ^ listToString etypeToString args | Tyfun (a, b) => "Tyfun (" ^ etypeToString a ^ ", " ^ etypeToString b ^ ")" + fun patToString (p : pat) : string = + case p of + PWild => "PWild" + | PVar v => "PVar " ^ quote v + fun exprToStringI (indent : string) (x : expr) : string = let val self = exprToStringI indent in case x of @@ -86,7 +105,7 @@ struct | EAndAlso (a, b) => "EAndAlso (" ^ self a ^ ", " ^ self b ^ ")" | EOrElse (a, b) => "EOrElse (" ^ self a ^ ", " ^ self b ^ ")" | ELet (decs, e) => "ELet (" ^ multilineListToString decToStringI indent decs ^ ", " ^ self e ^ ")" - | ELambda e => "ELambda " ^ exprToStringI indent e + | ELambda (pat, e) => "ELambda (" ^ patToString pat ^ ", " ^ exprToStringI indent e ^ ")" end and decToStringI (indent : string) (x : dec) : string = @@ -105,6 +124,7 @@ struct 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 ^ ")" | LInt i => "LInt " ^ Int.toString i | LString s => "LString " ^ quote s -- cgit v1.3.1