diff options
| author | Rose Hogenson <rosehogenson@posteo.net> | 2025-01-22 19:45:12 -0800 |
|---|---|---|
| committer | Rose Hogenson <rosehogenson@posteo.net> | 2025-05-18 08:11:25 -0700 |
| commit | e5034d3a668fbf0a4367bb582d759ba82b5576a3 (patch) | |
| tree | b0b658d254fe6a710183e60e792bb068263e77df /Parser.sml | |
| parent | 06624f3183f4d773cf1023cabe2e370ebba5dee1 (diff) | |
| download | sml-e5034d3a668fbf0a4367bb582d759ba82b5576a3.tar.zst | |
Support struct expressions
Diffstat (limited to 'Parser.sml')
| -rw-r--r-- | Parser.sml | 27 |
1 files changed, 16 insertions, 11 deletions
@@ -606,11 +606,15 @@ struct ((reserved "structure" >> bind identifier (fn strID => reserved "=" >> - reserved "struct" >> - bind (many strdec) (fn bindings => - reserved "end" >> - const (SOME (Syntax.DStruct (strID, List.mapPartial (fn x => x) bindings)))))) + bind structExpr (fn str => + const (SOME (Syntax.DStruct (strID, str)))))) <|> dec) st + and structExpr : Syntax.structExpr parser = fn st => + ((reserved "struct" >> + bind (many strdec) (fn bindings => + reserved "end" >> + const (Syntax.SStruct (List.mapPartial (fn x => x) bindings)))) + <|> Syntax.SIdent <$> longIdentifier) st (* There's ambiguity between pattern variables and constructors that can only * be resolved by checking for constructors in scope *) @@ -637,17 +641,18 @@ struct Syntax.DFun (f, map (fn (args, body) => (map (fixPatConstructors constructors) args, fixConstructors constructors body)) arms) | fixDecConstructors _ (decl as Syntax.DDatatype _) = decl | fixDecConstructors _ (decl as Syntax.DType _) = decl - | fixDecConstructors constructors (Syntax.DStruct (name, decs)) = - let - val constructors = ref constructors - val decs : Syntax.dec list = - map + | fixDecConstructors constructors (Syntax.DStruct (name, str)) = Syntax.DStruct (name, fixStructExprConstructors constructors str) + + and fixStructExprConstructors (constructors : unit StringMap.map) (Syntax.SStruct decls) : Syntax.structExpr = + let val constructors = ref constructors in + Syntax.SStruct + (map (fn dec => (constructors := foldl (fn (x, acc) => StringMap.insert x () acc) (!constructors) (findConstructors dec) ; fixDecConstructors (!constructors) dec)) - decs - in Syntax.DStruct (name, decs) + decls) end + | fixStructExprConstructors _ expr = expr and fixConstructors (constructors : unit StringMap.map) (Syntax.ETuple exprs) : Syntax.expr = Syntax.ETuple (map (fixConstructors constructors) exprs) |
