add cons and list expressions/patterns

This commit is contained in:
darkf 2013-11-01 20:30:46 -07:00
parent c9dcb3b66b
commit 24688ba9de
1 changed files with 18 additions and 1 deletions

View File

@ -19,8 +19,17 @@ args :: AST
/ expr? { case $1 of / expr? { case $1 of
Just x -> x Just x -> x
Nothing -> UnitConst } Nothing -> UnitConst }
patternlist :: Pattern
= pattern ("," pattern)+ { ListP ($1 : $2) }
/ pattern? { case $1 of
Just x -> ListP [x]
Nothing -> ListP [] }
pattern :: Pattern pattern :: Pattern
= integer { IntP $1 } = pattern "::" pattern { ConsP $1 $2 }
/ "[" patternlist "]"
/ integer { IntP $1 }
/ stringlit { StrP $1 } / stringlit { StrP $1 }
funpattern :: Pattern funpattern :: Pattern
@ -29,10 +38,18 @@ funpattern :: Pattern
Just x -> x Just x -> x
Nothing -> UnitP } Nothing -> UnitP }
listseq :: AST
= expr ("," expr)+ { ListConst ($1 : $2) }
/ expr? { case $1 of
Just x -> ListConst [x]
Nothing -> ListConst [] }
expr :: AST expr :: AST
= expr "(" args ")" { Call $1 $2 } = expr "(" args ")" { Call $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