parser2: add function definition, integer patterns, line comments

This commit is contained in:
darkf 2013-11-01 20:06:40 -07:00
parent 97cadac9aa
commit 24e52ec524
1 changed files with 16 additions and 3 deletions

View File

@ -1,11 +1,15 @@
{-# Language TemplateHaskell, QuasiQuotes, FlexibleContexts #-}
import Text.Peggy
import Text.Peggy hiding (space)
import AST
[peggy|
top :: [AST] = statements !.
lineComment :: () = '--' (!'\n' .)* '\n' { () }
space :: () = [ \r\n\t] { () } / lineComment
statements :: [AST]
= statement+
@ -17,11 +21,20 @@ args :: AST
/ expr? { case $1 of
Just x -> x
Nothing -> UnitConst }
pattern :: Pattern
= integer { IntP $1 }
funpattern :: Pattern
= pattern ("," pattern)+ { TupleP ($1 : $2) }
/ pattern? { case $1 of
Just x -> x
Nothing -> UnitP }
expr :: AST
= expr "(" args ")" { Call $1 $2 }
/ expr "+" fact { Add $1 $2 }
/ expr "-" fact { Sub $1 $2 }
/ identifier "(" funpattern ")" "->" expr { Defun $1 (Lambda [($2, $3)]) }
/ fact
fact :: AST
@ -31,13 +44,13 @@ fact :: AST
term :: AST
= "(" expr ")"
/ number { IntConst $1 }
/ integer { IntConst $1 }
/ identifier { Var $1 }
identifier ::: String
= [a-zA-Z_] [a-zA-Z0-9_'?!]* { $1 : $2 }
number ::: Integer
integer ::: Integer
= [1-9] [0-9]* { read ($1 : $2) }
|]