summaryrefslogtreecommitdiffstats
path: root/syntax.sml
diff options
context:
space:
mode:
Diffstat (limited to 'syntax.sml')
-rw-r--r--syntax.sml100
1 files changed, 60 insertions, 40 deletions
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 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 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 value =
+ VVar of var
+ | VLabel of var
+ | VInt of int
+ | VString of string
- 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 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