parser2: add tuples and tuple patterns

This commit is contained in:
darkf 2013-11-01 21:08:38 -07:00
parent 66328dd1b2
commit 1d4d773cec
1 changed files with 14 additions and 2 deletions

View File

@ -30,9 +30,15 @@ patternlist :: Pattern
Just x -> ListP [x] Just x -> ListP [x]
Nothing -> ListP [] } Nothing -> ListP [] }
patterntuple :: Pattern
= "(" "," ")" { TupleP [] }
/ "(" pattern ("," pattern)+ ")" { TupleP ($1 : $2) }
/ "(" pattern "," ")" { TupleP [$1] }
pattern :: Pattern pattern :: Pattern
= pattern "::" pattern { ConsP $1 $2 } = pattern "::" pattern { ConsP $1 $2 }
/ "[" patternlist "]" / "[" patternlist "]"
/ patterntuple
/ identifier { VarP $1 } / identifier { VarP $1 }
/ stringlit { StrP $1 } / stringlit { StrP $1 }
/ integer { IntP $1 } / integer { IntP $1 }
@ -49,6 +55,11 @@ listseq :: AST
Just x -> ListConst [x] Just x -> ListConst [x]
Nothing -> ListConst [] } Nothing -> ListConst [] }
tuple :: AST
= "(" "," ")" { TupleConst [] }
/ "(" expr ("," expr)+ ")" { TupleConst ($1 : $2) }
/ "(" expr "," ")" { TupleConst [$1] }
doblock :: AST doblock :: AST
= "do" semistatements "end" { Block $1 } = "do" semistatements "end" { Block $1 }
@ -57,7 +68,6 @@ expr :: AST
/ expr "::" expr { Cons $1 $2 } / expr "::" expr { Cons $1 $2 }
/ expr "+" fact { Add $1 $2 } / expr "+" fact { Add $1 $2 }
/ expr "-" fact { Sub $1 $2 } / expr "-" fact { Sub $1 $2 }
/ "[" listseq "]"
/ identifier "(" funpattern ")" "->" expr { Defun $1 (Lambda [($2, $3)]) } / identifier "(" funpattern ")" "->" expr { Defun $1 (Lambda [($2, $3)]) }
/ fact / fact
@ -67,7 +77,9 @@ fact :: AST
/ term / term
term :: AST term :: AST
= "(" expr ")" = tuple
/ "(" expr ")"
/ "[" listseq "]"
/ doblock / doblock
/ stringlit { StrConst $1 } / stringlit { StrConst $1 }
/ integer { IntConst $1 } / integer { IntConst $1 }