Skip to content

Commit

Permalink
Added integer litteral syntax to the parser.
Browse files Browse the repository at this point in the history
  • Loading branch information
kfl committed Jul 8, 2010
1 parent 8c883ec commit 65fdfc9
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions miniprolog.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Program = Clauses
type Clauses = [Clause]
type Clause = (Term, Terms) -- head and body
data Term = Var Variable
| Val Int
| Comp Ident [Term]
deriving (Eq, Show, Read)
type Terms = [Term]
Expand Down Expand Up @@ -50,7 +51,8 @@ clause = do t <- term
term :: Parser Term
term = variable
<|> literal
<|> list <?> "list term"
<|> (list <?> "list term")
<|> intval

terms :: Parser Terms
terms = sepBy1 term (csymb ',')
Expand All @@ -59,7 +61,7 @@ literal :: Parser Term
literal = do id <- ident
option (Comp id [])
(parens terms >>= return . Comp id)

parens :: Parser p -> Parser p
parens p = between (csymb '(') (csymb ')') p

Expand Down Expand Up @@ -90,6 +92,9 @@ variable = (do c <- upper <|> char '_'
cs <- many (alphaNum <|> char '_')
return $ Var (c:cs)) <?> "variable"

intval :: Parser Term
intval = (do digits <- many1 digit
return $ Val $ read digits) <?> "integer"

----------------------------------------------------------------------
-- Interpreter
Expand Down

0 comments on commit 65fdfc9

Please sign in to comment.