All pastes #975930 Raw Edit

Untitled

public text v1 · immutable
·published 2008-04-07 21:03 UTC
rendered paste body
{- Copyright 2008 Debris <debris>

This file is part of rcc.
rcc is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

rcc is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with rcc; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

$Id: rcc.hs,v 0.1 2008/07/04 08:16 debris $ -}

-- Import main libraries.
-- Environment, and the parsec monad.
module Main where
import Monad
import System.Environment
import Text.ParserCombinators.Parsec hiding (spaces)

-- main application entry point.
-- parse input expression on args.
main :: IO ()
main = do args <- getArgs
          putStrLn (rccReadExpression (args !! 0))

-- Read input expression.
-- checks for errors using symbols found.
rccReadExpression input = case parse rccParseExpression "" input of
    Left  err -> "Syntax error: Symbol expected at" ++ show err
    Right val -> "Found value" 

-- Symbols definition.
-- oneOf should be working by defining symbols to be any
-- of the instances we mention in the string attached. 
rccSymbols :: Parser Char
rccSymbols = oneOf "!$%&|*+-/:<=>?@^_~#"

-- Spaces definition.
-- spaces should not be taken in consideration, so we use
-- the skipMany1 function to skip them out of the parser.
rccSpaces :: Parser ()
rccSpaces = skipMany1 space

-- Type definitions.
-- types are currently somekind limited but they should 
-- be enough to perform some computation.
data Type = AtomicType   String
          | BooleanType  Bool
          | IntegerType  Integer
          | ListType     [Type]
          | PairType     [Type] Type
          | StringType   String

-- Parse atomic type.
-- Parses an atomic type of the language.
-- This can be either a primitive or a boolean result.
rccParseAtomic :: Parser Type
rccParseAtomic = do 
  x <- letter <|> rccSymbols
  y <- many (letter <|> digit <|> rccSymbols)
  let item = [x] ++ y 
  return $ case item of 
    "#true"   -> BooleanType True
    "#false"  -> BooleanType False
    otherwise -> AtomicType item

-- Parse integer type.
-- Parses an integer number by explicity considering
-- every character as a digit.
rccParseInteger :: Parser Type
rccParseInteger = liftM (IntegerType . read) $ many1 digit

-- Parse list type.
-- Parses a list of types, not delimited 
-- by any characters, but just by spaces.
rccParseList :: Parser Type
rccParseList = liftM ListType $ sepBy rccParseExpression rccSpaces

-- Parses a pair type.
-- As in scheme a pair is composed by two
-- lists separated by punctuation mark.
rccParsePair :: Parser Type
rccParsePair = do
               head <- endBy rccParseExpression rccSpaces
               tail <- char '.' >> rccSpaces >> rccParseExpression
               return $ PairType head tail

-- Parses a string type.
-- A string is just deilimited by double quotes.
-- The drawback is we have to avoid escape characters.
rccParseString :: Parser Type
rccParseString = do char '"'
                    x <- many (noneOf "\"")
                    char '"'
                    return $ StringType x

-- Parses a language construction.
-- The language is made of list or pair lexeme
-- evaluation nodes, we just traverse them.
rccParseConstruct :: Parser Type
rccParseConstruct = do char '('
                       x <- (try rccParseList) <|> rccParsePair
                       char ')'
                       return x

-- Parse a full expression.
-- Expressions are made of lists, that get parsed
-- by trying to get a list or a pair from them.
rccParseExpression :: Parser Type
rccParseExpression = rccParseAtomic
                 <|> rccParseString
                 <|> rccParseInteger      
                 <|> rccParseConstruct