diff --git a/Interpreter.hs b/Interpreter.hs index d23a45bc0fb3441152edb9936cfa06d4e2e71f79..ad557b3597a4cf9406649c61e0ab61950f9df0ab 100644 --- a/Interpreter.hs +++ b/Interpreter.hs @@ -3,18 +3,24 @@ import Debug import Eval import System.IO import ReadCSV +import Lexer +import Parser parse :: String -> Program -parse = notImplemented +parse = parseSource.alexScanTokens --outline +main = interpret "sampleprogram.txt" + interpret :: FilePath -> IO () -- the main function, takes in file name, prints out result interpret sourceFName = do source <- readFile sourceFName let program = parse source -- main abstract syntax tree (env, mainExpr) <- prepare program + print $ "Main expression " ++ show mainExpr let output = eval env mainExpr + print $ "Output " ++ show output let finalOutput = evalFinal output showFinal finalOutput @@ -24,7 +30,7 @@ prepare (inputSets,instructions) = do let env = zip inputSets csvData :: Environment let mainExpr = Control (head csvData) instructions return (env, mainExpr) - + where loadInputFiles = mapM loadInputFile diff --git a/Lexer.hs b/Lexer.hs new file mode 100644 index 0000000000000000000000000000000000000000..7b219d60bdc737444b240d256a1a8ac07eb845c7 --- /dev/null +++ b/Lexer.hs @@ -0,0 +1,3592 @@ +{-# OPTIONS_GHC -fno-warn-unused-binds -fno-warn-missing-signatures #-} +{-# LANGUAGE CPP #-} +{-# LINE 1 "Lexer.x" #-} + +module Lexer where +import Data.List + +#if __GLASGOW_HASKELL__ >= 603 +#include "ghcconfig.h" +#elif defined(__GLASGOW_HASKELL__) +#include "config.h" +#endif +#if __GLASGOW_HASKELL__ >= 503 +import Data.Array +#else +import Array +#endif +{-# LINE 1 "templates/wrappers.hs" #-} +-- ----------------------------------------------------------------------------- +-- Alex wrapper code. +-- +-- This code is in the PUBLIC DOMAIN; you may copy it freely and use +-- it for any purpose whatsoever. + + + + + +import Data.Word (Word8) + + + + + + + + + + + + + + + + +import Data.Char (ord) +import qualified Data.Bits + +-- | Encode a Haskell String to a list of Word8 values, in UTF8 format. +utf8Encode :: Char -> [Word8] +utf8Encode = uncurry (:) . utf8Encode' + +utf8Encode' :: Char -> (Word8, [Word8]) +utf8Encode' c = case go (ord c) of + (x, xs) -> (fromIntegral x, map fromIntegral xs) + where + go oc + | oc <= 0x7f = ( oc + , [ + ]) + + | oc <= 0x7ff = ( 0xc0 + (oc `Data.Bits.shiftR` 6) + , [0x80 + oc Data.Bits..&. 0x3f + ]) + + | oc <= 0xffff = ( 0xe0 + (oc `Data.Bits.shiftR` 12) + , [0x80 + ((oc `Data.Bits.shiftR` 6) Data.Bits..&. 0x3f) + , 0x80 + oc Data.Bits..&. 0x3f + ]) + | otherwise = ( 0xf0 + (oc `Data.Bits.shiftR` 18) + , [0x80 + ((oc `Data.Bits.shiftR` 12) Data.Bits..&. 0x3f) + , 0x80 + ((oc `Data.Bits.shiftR` 6) Data.Bits..&. 0x3f) + , 0x80 + oc Data.Bits..&. 0x3f + ]) + + + +type Byte = Word8 + +-- ----------------------------------------------------------------------------- +-- The input type + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +-- ----------------------------------------------------------------------------- +-- Token positions + +-- `Posn' records the location of a token in the input text. It has three +-- fields: the address (number of chacaters preceding the token), line number +-- and column of a token within the file. `start_pos' gives the position of the +-- start of the file and `eof_pos' a standard encoding for the end of file. +-- `move_pos' calculates the new position after traversing a given character, +-- assuming the usual eight character tab stops. + + + + + + + + + + + + + + +-- ----------------------------------------------------------------------------- +-- Monad (default and with ByteString input) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +-- ----------------------------------------------------------------------------- +-- Basic wrapper + + +type AlexInput = (Char,[Byte],String) + +alexInputPrevChar :: AlexInput -> Char +alexInputPrevChar (c,_,_) = c + +-- alexScanTokens :: String -> [token] +alexScanTokens str = go ('\n',[],str) + where go inp__@(_,_bs,s) = + case alexScan inp__ 0 of + AlexEOF -> [] + AlexError _ -> error "lexical error" + AlexSkip inp__' _ln -> go inp__' + AlexToken inp__' len act -> act (take len s) : go inp__' + +alexGetByte :: AlexInput -> Maybe (Byte,AlexInput) +alexGetByte (c,(b:bs),s) = Just (b,(c,bs,s)) +alexGetByte (_,[],[]) = Nothing +alexGetByte (_,[],(c:s)) = case utf8Encode' c of + (b, bs) -> Just (b, (c, bs, s)) + + + +-- ----------------------------------------------------------------------------- +-- Basic wrapper, ByteString version + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +-- ----------------------------------------------------------------------------- +-- Posn wrapper + +-- Adds text positions to the basic model. + + + + + + + + + + + + + +-- ----------------------------------------------------------------------------- +-- Posn wrapper, ByteString version + + + + + + + + + + + + + + +-- ----------------------------------------------------------------------------- +-- GScan wrapper + +-- For compatibility with previous versions of Alex, and because we can. + + + + + + + + + + + + + + +alex_tab_size :: Int +alex_tab_size = 8 +alex_base :: Array Int Int +alex_base = listArray (0 :: Int, 39) + [ -8 + , -55 + , -54 + , -109 + , 67 + , 156 + , -53 + , -106 + , -99 + , 3 + , 240 + , 268 + , 352 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , -88 + , 380 + , 464 + , 492 + , 576 + , 604 + , 688 + , 716 + , 800 + , 828 + , 912 + , 940 + , 1024 + , 1082 + , 143 + , 0 + ] + +alex_table :: Array Int Int +alex_table = listArray (0 :: Int, 1337) + [ 0 + , 9 + , 9 + , 9 + , 9 + , 9 + , 18 + , 19 + , 7 + , 17 + , 14 + , 13 + , 9 + , 9 + , 9 + , 9 + , 9 + , 8 + , 0 + , 0 + , 0 + , 0 + , 0 + , 3 + , 9 + , 0 + , 4 + , 0 + , 0 + , 0 + , 0 + , 0 + , 20 + , 21 + , 0 + , 9 + , 0 + , 6 + , 24 + , 2 + , 38 + , 38 + , 38 + , 38 + , 38 + , 38 + , 38 + , 38 + , 38 + , 38 + , 0 + , 22 + , 0 + , 1 + , 0 + , 0 + , 0 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 15 + , 23 + , 16 + , 0 + , 0 + , 0 + , 31 + , 31 + , 31 + , 31 + , 31 + , 30 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 34 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 39 + , 38 + , 38 + , 38 + , 38 + , 38 + , 38 + , 38 + , 38 + , 38 + , 38 + , 0 + , 0 + , 0 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 5 + , 31 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 31 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 31 + , 0 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 0 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 31 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 31 + , 0 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 0 + , 31 + , 31 + , 31 + , 31 + , 11 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 31 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 31 + , 0 + , 31 + , 31 + , 31 + , 31 + , 12 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 0 + , 31 + , 31 + , 31 + , 31 + , 36 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 31 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 31 + , 0 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 25 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 0 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 26 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 31 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 31 + , 0 + , 33 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 32 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 0 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 31 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 31 + , 0 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 35 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 0 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 29 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 31 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 31 + , 0 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 28 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 0 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 27 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 31 + , 0 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 10 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 31 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 37 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + , 0 + ] + +alex_check :: Array Int Int +alex_check = listArray (0 :: Int, 1337) + [ -1 + , 9 + , 10 + , 11 + , 12 + , 13 + , 61 + , 61 + , 117 + , 62 + , 116 + , 110 + , 9 + , 10 + , 11 + , 12 + , 13 + , 105 + , -1 + , -1 + , -1 + , -1 + , -1 + , 111 + , 32 + , -1 + , 34 + , -1 + , -1 + , -1 + , -1 + , -1 + , 40 + , 41 + , -1 + , 32 + , -1 + , 45 + , 46 + , 47 + , 48 + , 49 + , 50 + , 51 + , 52 + , 53 + , 54 + , 55 + , 56 + , 57 + , -1 + , 59 + , -1 + , 61 + , -1 + , -1 + , -1 + , 65 + , 66 + , 67 + , 68 + , 69 + , 70 + , 71 + , 72 + , 73 + , 74 + , 75 + , 76 + , 77 + , 78 + , 79 + , 80 + , 81 + , 82 + , 83 + , 84 + , 85 + , 86 + , 87 + , 88 + , 89 + , 90 + , 91 + , 92 + , 93 + , -1 + , -1 + , -1 + , 97 + , 98 + , 99 + , 100 + , 101 + , 102 + , 103 + , 104 + , 105 + , 106 + , 107 + , 108 + , 109 + , 110 + , 111 + , 112 + , 113 + , 114 + , 115 + , 116 + , 117 + , 118 + , 119 + , 120 + , 121 + , 122 + , 48 + , 49 + , 50 + , 51 + , 52 + , 53 + , 54 + , 55 + , 56 + , 57 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , 65 + , 66 + , 67 + , 68 + , 69 + , 70 + , 71 + , 72 + , 73 + , 74 + , 75 + , 76 + , 77 + , 78 + , 79 + , 80 + , 81 + , 82 + , 83 + , 84 + , 85 + , 86 + , 87 + , 88 + , 89 + , 90 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , 97 + , 98 + , 99 + , 100 + , 101 + , 102 + , 103 + , 104 + , 105 + , 106 + , 107 + , 108 + , 109 + , 110 + , 111 + , 112 + , 113 + , 114 + , 115 + , 116 + , 117 + , 118 + , 119 + , 120 + , 121 + , 122 + , 34 + , 48 + , 49 + , 50 + , 51 + , 52 + , 53 + , 54 + , 55 + , 56 + , 57 + , -1 + , -1 + , -1 + , 48 + , 49 + , 50 + , 51 + , 52 + , 53 + , 54 + , 55 + , 56 + , 57 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , 65 + , 66 + , 67 + , 68 + , 69 + , 70 + , 71 + , 72 + , 73 + , 74 + , 75 + , 76 + , 77 + , 78 + , 79 + , 80 + , 81 + , 82 + , 83 + , 84 + , 85 + , 86 + , 87 + , 88 + , 89 + , 90 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , 97 + , 98 + , 99 + , 100 + , 101 + , 102 + , 103 + , 104 + , 105 + , 106 + , 107 + , 108 + , 109 + , 110 + , 111 + , 112 + , 113 + , 114 + , 115 + , 116 + , 117 + , 118 + , 119 + , 120 + , 121 + , 122 + , 39 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , 48 + , 49 + , 50 + , 51 + , 52 + , 53 + , 54 + , 55 + , 56 + , 57 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , 39 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , 48 + , 49 + , 50 + , 51 + , 52 + , 53 + , 54 + , 55 + , 56 + , 57 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , 95 + , -1 + , 97 + , 98 + , 99 + , 100 + , 101 + , 102 + , 103 + , 104 + , 105 + , 106 + , 107 + , 108 + , 109 + , 110 + , 111 + , 112 + , 113 + , 114 + , 115 + , 116 + , 117 + , 118 + , 119 + , 120 + , 121 + , 122 + , 95 + , -1 + , 97 + , 98 + , 99 + , 100 + , 101 + , 102 + , 103 + , 104 + , 105 + , 106 + , 107 + , 108 + , 109 + , 110 + , 111 + , 112 + , 113 + , 114 + , 115 + , 116 + , 117 + , 118 + , 119 + , 120 + , 121 + , 122 + , 39 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , 48 + , 49 + , 50 + , 51 + , 52 + , 53 + , 54 + , 55 + , 56 + , 57 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , 39 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , 48 + , 49 + , 50 + , 51 + , 52 + , 53 + , 54 + , 55 + , 56 + , 57 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , 95 + , -1 + , 97 + , 98 + , 99 + , 100 + , 101 + , 102 + , 103 + , 104 + , 105 + , 106 + , 107 + , 108 + , 109 + , 110 + , 111 + , 112 + , 113 + , 114 + , 115 + , 116 + , 117 + , 118 + , 119 + , 120 + , 121 + , 122 + , 95 + , -1 + , 97 + , 98 + , 99 + , 100 + , 101 + , 102 + , 103 + , 104 + , 105 + , 106 + , 107 + , 108 + , 109 + , 110 + , 111 + , 112 + , 113 + , 114 + , 115 + , 116 + , 117 + , 118 + , 119 + , 120 + , 121 + , 122 + , 39 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , 48 + , 49 + , 50 + , 51 + , 52 + , 53 + , 54 + , 55 + , 56 + , 57 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , 39 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , 48 + , 49 + , 50 + , 51 + , 52 + , 53 + , 54 + , 55 + , 56 + , 57 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , 95 + , -1 + , 97 + , 98 + , 99 + , 100 + , 101 + , 102 + , 103 + , 104 + , 105 + , 106 + , 107 + , 108 + , 109 + , 110 + , 111 + , 112 + , 113 + , 114 + , 115 + , 116 + , 117 + , 118 + , 119 + , 120 + , 121 + , 122 + , 95 + , -1 + , 97 + , 98 + , 99 + , 100 + , 101 + , 102 + , 103 + , 104 + , 105 + , 106 + , 107 + , 108 + , 109 + , 110 + , 111 + , 112 + , 113 + , 114 + , 115 + , 116 + , 117 + , 118 + , 119 + , 120 + , 121 + , 122 + , 39 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , 48 + , 49 + , 50 + , 51 + , 52 + , 53 + , 54 + , 55 + , 56 + , 57 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , 39 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , 48 + , 49 + , 50 + , 51 + , 52 + , 53 + , 54 + , 55 + , 56 + , 57 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , 95 + , -1 + , 97 + , 98 + , 99 + , 100 + , 101 + , 102 + , 103 + , 104 + , 105 + , 106 + , 107 + , 108 + , 109 + , 110 + , 111 + , 112 + , 113 + , 114 + , 115 + , 116 + , 117 + , 118 + , 119 + , 120 + , 121 + , 122 + , 95 + , -1 + , 97 + , 98 + , 99 + , 100 + , 101 + , 102 + , 103 + , 104 + , 105 + , 106 + , 107 + , 108 + , 109 + , 110 + , 111 + , 112 + , 113 + , 114 + , 115 + , 116 + , 117 + , 118 + , 119 + , 120 + , 121 + , 122 + , 39 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , 48 + , 49 + , 50 + , 51 + , 52 + , 53 + , 54 + , 55 + , 56 + , 57 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , 39 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , 48 + , 49 + , 50 + , 51 + , 52 + , 53 + , 54 + , 55 + , 56 + , 57 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , 95 + , -1 + , 97 + , 98 + , 99 + , 100 + , 101 + , 102 + , 103 + , 104 + , 105 + , 106 + , 107 + , 108 + , 109 + , 110 + , 111 + , 112 + , 113 + , 114 + , 115 + , 116 + , 117 + , 118 + , 119 + , 120 + , 121 + , 122 + , 95 + , -1 + , 97 + , 98 + , 99 + , 100 + , 101 + , 102 + , 103 + , 104 + , 105 + , 106 + , 107 + , 108 + , 109 + , 110 + , 111 + , 112 + , 113 + , 114 + , 115 + , 116 + , 117 + , 118 + , 119 + , 120 + , 121 + , 122 + , 39 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , 48 + , 49 + , 50 + , 51 + , 52 + , 53 + , 54 + , 55 + , 56 + , 57 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , 39 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , 48 + , 49 + , 50 + , 51 + , 52 + , 53 + , 54 + , 55 + , 56 + , 57 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , 95 + , -1 + , 97 + , 98 + , 99 + , 100 + , 101 + , 102 + , 103 + , 104 + , 105 + , 106 + , 107 + , 108 + , 109 + , 110 + , 111 + , 112 + , 113 + , 114 + , 115 + , 116 + , 117 + , 118 + , 119 + , 120 + , 121 + , 122 + , 95 + , -1 + , 97 + , 98 + , 99 + , 100 + , 101 + , 102 + , 103 + , 104 + , 105 + , 106 + , 107 + , 108 + , 109 + , 110 + , 111 + , 112 + , 113 + , 114 + , 115 + , 116 + , 117 + , 118 + , 119 + , 120 + , 121 + , 122 + , 39 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , 48 + , 49 + , 50 + , 51 + , 52 + , 53 + , 54 + , 55 + , 56 + , 57 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , 39 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , 48 + , 49 + , 50 + , 51 + , 52 + , 53 + , 54 + , 55 + , 56 + , 57 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , 95 + , -1 + , 97 + , 98 + , 99 + , 100 + , 101 + , 102 + , 103 + , 104 + , 105 + , 106 + , 107 + , 108 + , 109 + , 110 + , 111 + , 112 + , 113 + , 114 + , 115 + , 116 + , 117 + , 118 + , 119 + , 120 + , 121 + , 122 + , 95 + , -1 + , 97 + , 98 + , 99 + , 100 + , 101 + , 102 + , 103 + , 104 + , 105 + , 106 + , 107 + , 108 + , 109 + , 110 + , 111 + , 112 + , 113 + , 114 + , 115 + , 116 + , 117 + , 118 + , 119 + , 120 + , 121 + , 122 + , 39 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , 48 + , 49 + , 50 + , 51 + , 52 + , 53 + , 54 + , 55 + , 56 + , 57 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , 95 + , -1 + , 97 + , 98 + , 99 + , 100 + , 101 + , 102 + , 103 + , 104 + , 105 + , 106 + , 107 + , 108 + , 109 + , 110 + , 111 + , 112 + , 113 + , 114 + , 115 + , 116 + , 117 + , 118 + , 119 + , 120 + , 121 + , 122 + , 65 + , 66 + , 67 + , 68 + , 69 + , 70 + , 71 + , 72 + , 73 + , 74 + , 75 + , 76 + , 77 + , 78 + , 79 + , 80 + , 81 + , 82 + , 83 + , 84 + , 85 + , 86 + , 87 + , 88 + , 89 + , 90 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , 97 + , 98 + , 99 + , 100 + , 101 + , 102 + , 103 + , 104 + , 105 + , 106 + , 107 + , 108 + , 109 + , 110 + , 111 + , 112 + , 113 + , 114 + , 115 + , 116 + , 117 + , 118 + , 119 + , 120 + , 121 + , 122 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + ] + +alex_deflt :: Array Int Int +alex_deflt = listArray (0 :: Int, 39) + [ -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + , -1 + ] + +alex_accept = listArray (0 :: Int, 39) + [ AlexAccNone + , AlexAccNone + , AlexAccNone + , AlexAccNone + , AlexAccNone + , AlexAccNone + , AlexAccNone + , AlexAccNone + , AlexAccNone + , AlexAccSkip + , AlexAcc 29 + , AlexAcc 28 + , AlexAcc 27 + , AlexAcc 26 + , AlexAcc 25 + , AlexAcc 24 + , AlexAcc 23 + , AlexAcc 22 + , AlexAcc 21 + , AlexAcc 20 + , AlexAcc 19 + , AlexAcc 18 + , AlexAcc 17 + , AlexAcc 16 + , AlexAcc 15 + , AlexAcc 14 + , AlexAcc 13 + , AlexAcc 12 + , AlexAcc 11 + , AlexAcc 10 + , AlexAcc 9 + , AlexAcc 8 + , AlexAcc 7 + , AlexAcc 6 + , AlexAcc 5 + , AlexAcc 4 + , AlexAcc 3 + , AlexAcc 2 + , AlexAcc 1 + , AlexAcc 0 + ] + +alex_actions = array (0 :: Int, 30) + [ (29,alex_action_1) + , (28,alex_action_2) + , (27,alex_action_3) + , (26,alex_action_4) + , (25,alex_action_5) + , (24,alex_action_6) + , (23,alex_action_7) + , (22,alex_action_8) + , (21,alex_action_9) + , (20,alex_action_10) + , (19,alex_action_11) + , (18,alex_action_12) + , (17,alex_action_13) + , (16,alex_action_14) + , (15,alex_action_15) + , (14,alex_action_16) + , (13,alex_action_16) + , (12,alex_action_16) + , (11,alex_action_16) + , (10,alex_action_16) + , (9,alex_action_16) + , (8,alex_action_16) + , (7,alex_action_16) + , (6,alex_action_16) + , (5,alex_action_16) + , (4,alex_action_16) + , (3,alex_action_16) + , (2,alex_action_17) + , (1,alex_action_18) + , (0,alex_action_19) + ] + +{-# LINE 36 "Lexer.x" #-} + +--token type: +data Token = + TokenFilter | + TokenSetName String | + TokenFunc | + TokenNat Int | + TokenVarName String | + TokenTrue | + TokenFalse | + TokenString String | + TokenLeftSqBracket | + TokenRightSqBracket | + TokenArrow | + TokenisEqual | + TokenisNotEqual | + TokenLeftBracket | + TokenRightBracket | + TokenSemiCol | + TokenLambda | + TokenComma | + TokenFullStop | + TokenInSet | + TokenOutSet + deriving (Eq, Show) + +alex_action_1 = \s -> TokenFilter +alex_action_2 = \s -> TokenTrue +alex_action_3 = \s -> TokenFalse +alex_action_4 = \s -> TokenInSet +alex_action_5 = \s -> TokenOutSet +alex_action_6 = \s -> TokenLeftSqBracket +alex_action_7 = \s -> TokenRightSqBracket +alex_action_8 = \s -> TokenArrow +alex_action_9 = \s -> TokenisEqual +alex_action_10 = \s -> TokenisNotEqual +alex_action_11 = \s -> TokenLeftBracket +alex_action_12 = \s -> TokenRightBracket +alex_action_13 = \s -> TokenSemiCol +alex_action_14 = \s -> TokenLambda +alex_action_15 = \s -> TokenFullStop +alex_action_16 = \s -> TokenVarName s +alex_action_17 = \s -> TokenSetName s +alex_action_18 = \s -> TokenNat (read s) +alex_action_19 = \s -> ((TokenString).init.tail) s +{-# LINE 1 "templates/GenericTemplate.hs" #-} +-- ----------------------------------------------------------------------------- +-- ALEX TEMPLATE +-- +-- This code is in the PUBLIC DOMAIN; you may copy it freely and use +-- it for any purpose whatsoever. + +-- ----------------------------------------------------------------------------- +-- INTERNALS and main scanner engine + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +alexIndexInt16OffAddr arr off = arr ! off + + + + + + + + + + + + + + + + + + + + + + + + +alexIndexInt32OffAddr arr off = arr ! off + + + + + + + + + + + +quickIndex arr i = arr ! i + + +-- ----------------------------------------------------------------------------- +-- Main lexing routines + +data AlexReturn a + = AlexEOF + | AlexError !AlexInput + | AlexSkip !AlexInput !Int + | AlexToken !AlexInput !Int a + +-- alexScan :: AlexInput -> StartCode -> AlexReturn a +alexScan input__ (sc) + = alexScanUser undefined input__ (sc) + +alexScanUser user__ input__ (sc) + = case alex_scan_tkn user__ input__ (0) input__ sc AlexNone of + (AlexNone, input__') -> + case alexGetByte input__ of + Nothing -> + + + + AlexEOF + Just _ -> + + + + AlexError input__' + + (AlexLastSkip input__'' len, _) -> + + + + AlexSkip input__'' len + + (AlexLastAcc k input__''' len, _) -> + + + + AlexToken input__''' len (alex_actions ! k) + + +-- Push the input through the DFA, remembering the most recent accepting +-- state it encountered. + +alex_scan_tkn user__ orig_input len input__ s last_acc = + input__ `seq` -- strict in the input + let + new_acc = (check_accs (alex_accept `quickIndex` (s))) + in + new_acc `seq` + case alexGetByte input__ of + Nothing -> (new_acc, input__) + Just (c, new_input) -> + + + + case fromIntegral c of { (ord_c) -> + let + base = alexIndexInt32OffAddr alex_base s + offset = (base + ord_c) + check = alexIndexInt16OffAddr alex_check offset + + new_s = if (offset >= (0)) && (check == ord_c) + then alexIndexInt16OffAddr alex_table offset + else alexIndexInt16OffAddr alex_deflt s + in + case new_s of + (-1) -> (new_acc, input__) + -- on an error, we want to keep the input *before* the + -- character that failed, not after. + _ -> alex_scan_tkn user__ orig_input (if c < 0x80 || c >= 0xC0 then (len + (1)) else len) + -- note that the length is increased ONLY if this is the 1st byte in a char encoding) + new_input new_s new_acc + } + where + check_accs (AlexAccNone) = last_acc + check_accs (AlexAcc a ) = AlexLastAcc a input__ (len) + check_accs (AlexAccSkip) = AlexLastSkip input__ (len) + + check_accs (AlexAccPred a predx rest) + | predx user__ orig_input (len) input__ + = AlexLastAcc a input__ (len) + | otherwise + = check_accs rest + check_accs (AlexAccSkipPred predx rest) + | predx user__ orig_input (len) input__ + = AlexLastSkip input__ (len) + | otherwise + = check_accs rest + + +data AlexLastAcc + = AlexNone + | AlexLastAcc !Int !AlexInput !Int + | AlexLastSkip !AlexInput !Int + +data AlexAcc user + = AlexAccNone + | AlexAcc Int + | AlexAccSkip + + | AlexAccPred Int (AlexAccPred user) (AlexAcc user) + | AlexAccSkipPred (AlexAccPred user) (AlexAcc user) + +type AlexAccPred user = user -> AlexInput -> Int -> AlexInput -> Bool + +-- ----------------------------------------------------------------------------- +-- Predicates on a rule + +alexAndPred p1 p2 user__ in1 len in2 + = p1 user__ in1 len in2 && p2 user__ in1 len in2 + +--alexPrevCharIsPred :: Char -> AlexAccPred _ +alexPrevCharIs c _ input__ _ _ = c == alexInputPrevChar input__ + +alexPrevCharMatches f _ input__ _ _ = f (alexInputPrevChar input__) + +--alexPrevCharIsOneOfPred :: Array Char Bool -> AlexAccPred _ +alexPrevCharIsOneOf arr _ input__ _ _ = arr ! alexInputPrevChar input__ + +--alexRightContext :: Int -> AlexAccPred _ +alexRightContext (sc) user__ _ _ input__ = + case alex_scan_tkn user__ input__ (0) input__ sc AlexNone of + (AlexNone, _) -> False + _ -> True + -- TODO: there's no need to find the longest + -- match when checking the right context, just + -- the first match will do. + diff --git a/Lexer.x b/Lexer.x index 000d86cd66118c0bb0c107ce3c6aa817bc3fef4e..8f37a6a8f5c0202cba4d7cdd140b964920805d73 100644 --- a/Lexer.x +++ b/Lexer.x @@ -1,5 +1,6 @@ { module Lexer where +import Data.List } %wrapper "basic" @@ -9,30 +10,29 @@ $lower = [a-z] $upper = [A-Z] tokens :- - $white+ ; - "--".* ; - filter {\s -> TokenFilter } - $upper[$alpha]* {\s -> TokenSetName s } - Func {\s -> TokenFunc } - $digit+ {\s -> TokenNat (read s) } - $lower [$lower $digit \_ \']* {\s -> TokenVarName s } - Record {\s -> TokenRecord } - true {\s -> TokenTrue } - false {\s -> TokenFalse } - $[$alpha $digit]* {\s -> TokenString s } - '[' {\s -> TokenLeftSqBracket } - ']' {\s -> TokenRightSqBracket } - "->" {\s -> TokenArrow } - "==" {\s -> TokenisEqual } - "/=" {\s -> TokenisNotEqual } - '(' {\s -> TokenLeftBracket } - ')' {\s -> TokenRightBracket } - ';' {\s -> TokenApp } - '\\' {\s -> TokenLambda } - ',' {\s -> TokenComma } - '.' {\s -> TokenFullstop } - in {\s -> TokenInSet } - out {\s -> TokenOutSet } + +$white+ ; +filter {\s -> TokenFilter } +true {\s -> TokenTrue } +false {\s -> TokenFalse } +\.in {\s -> TokenInSet } +\.out {\s -> TokenOutSet } +\[ {\s -> TokenLeftSqBracket } +\] {\s -> TokenRightSqBracket } +"->" {\s -> TokenArrow } +"==" {\s -> TokenisEqual } +"/=" {\s -> TokenisNotEqual } +\( {\s -> TokenLeftBracket } +\) {\s -> TokenRightBracket } +\; {\s -> TokenSemiCol } +\\ {\s -> TokenLambda } +--\, {\s -> TokenComma } +\. {\s -> TokenFullStop } +$lower [$lower $digit \_ \']* {\s -> TokenVarName s } +$upper[$alpha]* {\s -> TokenSetName s } +$digit+ {\s -> TokenNat (read s) } +\"[$alpha $digit]+\" {\s -> ((TokenString).init.tail) s } + { --token type: data Token = @@ -41,7 +41,6 @@ data Token = TokenFunc | TokenNat Int | TokenVarName String | - TokenRecord | TokenTrue | TokenFalse | TokenString String | @@ -52,7 +51,7 @@ data Token = TokenisNotEqual | TokenLeftBracket | TokenRightBracket | - TokenApp | + TokenSemiCol | TokenLambda | TokenComma | TokenFullStop | diff --git a/Parser.hs b/Parser.hs new file mode 100644 index 0000000000000000000000000000000000000000..785d1c518cb920036975ef814da7e524098e9c50 --- /dev/null +++ b/Parser.hs @@ -0,0 +1,769 @@ +{-# OPTIONS_GHC -w #-} +module Parser where +import Lexer +import Types +import qualified Data.Array as Happy_Data_Array +import qualified Data.Bits as Bits +import Control.Applicative(Applicative(..)) +import Control.Monad (ap) + +-- parser produced by Happy Version 1.20.0 + +data HappyAbsSyn t4 t5 t6 t7 t8 t9 t10 t11 t12 + = HappyTerminal (Token) + | HappyErrorToken Prelude.Int + | HappyAbsSyn4 t4 + | HappyAbsSyn5 t5 + | HappyAbsSyn6 t6 + | HappyAbsSyn7 t7 + | HappyAbsSyn8 t8 + | HappyAbsSyn9 t9 + | HappyAbsSyn10 t10 + | HappyAbsSyn11 t11 + | HappyAbsSyn12 t12 + +happyExpList :: Happy_Data_Array.Array Prelude.Int Prelude.Int +happyExpList = Happy_Data_Array.listArray (0,75) ([8192,0,16384,0,0,2,0,0,0,4,0,0,8,32,0,8,0,0,0,0,32,32768,0,0,0,2048,0,512,0,0,0,0,32,0,1024,0,16384,0,8192,0,8192,0,32,0,0,0,0,2,0,32,512,0,0,1,63488,0,0,0,0,18,0,0,0,0,0,0,0,0,0,0,0,248,0,2304,2,1024,0,32,0,3968,0,0,0,16384,0,0,0,63488,0,0,0,0,0,0 + ]) + +{-# NOINLINE happyExpListPerState #-} +happyExpListPerState st = + token_strs_expected + where token_strs = ["error","%dummy","%start_parseSource","Prog","SetNames","VarNames","SetFuncCalls","SetFuncCall","Func","Expr","Record","Exprs","filter","in","out","SetName","Nat","VarName","true","false","Str","'['","']'","\"->\"","\"==\"","\"/=\"","'('","')'","';'","'\\\\'","','","'.'","%eof"] + bit_start = st Prelude.* 33 + bit_end = (st Prelude.+ 1) Prelude.* 33 + read_bit = readArrayBit happyExpList + bits = Prelude.map read_bit [bit_start..bit_end Prelude.- 1] + bits_indexed = Prelude.zip bits [0..32] + token_strs_expected = Prelude.concatMap f bits_indexed + f (Prelude.False, _) = [] + f (Prelude.True, nr) = [token_strs Prelude.!! nr] + +action_0 (14) = happyShift action_2 +action_0 (4) = happyGoto action_3 +action_0 _ = happyFail (happyExpListPerState 0) + +action_1 (14) = happyShift action_2 +action_1 _ = happyFail (happyExpListPerState 1) + +action_2 (16) = happyShift action_5 +action_2 (5) = happyGoto action_4 +action_2 _ = happyFail (happyExpListPerState 2) + +action_3 (33) = happyAccept +action_3 _ = happyFail (happyExpListPerState 3) + +action_4 (15) = happyShift action_7 +action_4 _ = happyFail (happyExpListPerState 4) + +action_5 (31) = happyShift action_6 +action_5 _ = happyReduce_2 + +action_6 (16) = happyShift action_5 +action_6 (5) = happyGoto action_11 +action_6 _ = happyFail (happyExpListPerState 6) + +action_7 (13) = happyShift action_10 +action_7 (7) = happyGoto action_8 +action_7 (8) = happyGoto action_9 +action_7 _ = happyFail (happyExpListPerState 7) + +action_8 _ = happyReduce_1 + +action_9 (29) = happyShift action_13 +action_9 _ = happyReduce_6 + +action_10 (22) = happyShift action_12 +action_10 _ = happyFail (happyExpListPerState 10) + +action_11 _ = happyReduce_3 + +action_12 (16) = happyShift action_15 +action_12 _ = happyFail (happyExpListPerState 12) + +action_13 (13) = happyShift action_10 +action_13 (7) = happyGoto action_14 +action_13 (8) = happyGoto action_9 +action_13 _ = happyFail (happyExpListPerState 13) + +action_14 _ = happyReduce_7 + +action_15 (23) = happyShift action_16 +action_15 _ = happyFail (happyExpListPerState 15) + +action_16 (27) = happyShift action_17 +action_16 _ = happyFail (happyExpListPerState 16) + +action_17 (30) = happyShift action_19 +action_17 (9) = happyGoto action_18 +action_17 _ = happyFail (happyExpListPerState 17) + +action_18 (28) = happyShift action_21 +action_18 _ = happyFail (happyExpListPerState 18) + +action_19 (27) = happyShift action_20 +action_19 _ = happyFail (happyExpListPerState 19) + +action_20 (18) = happyShift action_23 +action_20 (6) = happyGoto action_22 +action_20 _ = happyFail (happyExpListPerState 20) + +action_21 _ = happyReduce_8 + +action_22 (28) = happyShift action_25 +action_22 _ = happyFail (happyExpListPerState 22) + +action_23 (31) = happyShift action_24 +action_23 _ = happyReduce_4 + +action_24 (18) = happyShift action_23 +action_24 (6) = happyGoto action_27 +action_24 _ = happyFail (happyExpListPerState 24) + +action_25 (24) = happyShift action_26 +action_25 _ = happyFail (happyExpListPerState 25) + +action_26 (18) = happyShift action_30 +action_26 (19) = happyShift action_31 +action_26 (20) = happyShift action_32 +action_26 (21) = happyShift action_33 +action_26 (22) = happyShift action_34 +action_26 (10) = happyGoto action_28 +action_26 (11) = happyGoto action_29 +action_26 _ = happyFail (happyExpListPerState 26) + +action_27 _ = happyReduce_5 + +action_28 (22) = happyShift action_37 +action_28 (25) = happyShift action_38 +action_28 _ = happyReduce_9 + +action_29 _ = happyReduce_14 + +action_30 _ = happyReduce_13 + +action_31 _ = happyReduce_15 + +action_32 _ = happyReduce_16 + +action_33 _ = happyReduce_12 + +action_34 (18) = happyShift action_30 +action_34 (19) = happyShift action_31 +action_34 (20) = happyShift action_32 +action_34 (21) = happyShift action_33 +action_34 (22) = happyShift action_34 +action_34 (10) = happyGoto action_35 +action_34 (11) = happyGoto action_29 +action_34 (12) = happyGoto action_36 +action_34 _ = happyFail (happyExpListPerState 34) + +action_35 (22) = happyShift action_37 +action_35 (25) = happyShift action_38 +action_35 (31) = happyShift action_42 +action_35 _ = happyReduce_18 + +action_36 (23) = happyShift action_41 +action_36 _ = happyFail (happyExpListPerState 36) + +action_37 (17) = happyShift action_40 +action_37 _ = happyFail (happyExpListPerState 37) + +action_38 (18) = happyShift action_30 +action_38 (19) = happyShift action_31 +action_38 (20) = happyShift action_32 +action_38 (21) = happyShift action_33 +action_38 (22) = happyShift action_34 +action_38 (10) = happyGoto action_39 +action_38 (11) = happyGoto action_29 +action_38 _ = happyFail (happyExpListPerState 38) + +action_39 (22) = happyShift action_37 +action_39 _ = happyReduce_10 + +action_40 (23) = happyShift action_44 +action_40 _ = happyFail (happyExpListPerState 40) + +action_41 _ = happyReduce_17 + +action_42 (18) = happyShift action_30 +action_42 (19) = happyShift action_31 +action_42 (20) = happyShift action_32 +action_42 (21) = happyShift action_33 +action_42 (22) = happyShift action_34 +action_42 (10) = happyGoto action_35 +action_42 (11) = happyGoto action_29 +action_42 (12) = happyGoto action_43 +action_42 _ = happyFail (happyExpListPerState 42) + +action_43 _ = happyReduce_19 + +action_44 _ = happyReduce_11 + +happyReduce_1 = happyReduce 4 4 happyReduction_1 +happyReduction_1 ((HappyAbsSyn7 happy_var_4) `HappyStk` + _ `HappyStk` + (HappyAbsSyn5 happy_var_2) `HappyStk` + _ `HappyStk` + happyRest) + = HappyAbsSyn4 + ((happy_var_2,happy_var_4) + ) `HappyStk` happyRest + +happyReduce_2 = happySpecReduce_1 5 happyReduction_2 +happyReduction_2 (HappyTerminal (TokenSetName happy_var_1)) + = HappyAbsSyn5 + ([happy_var_1] + ) +happyReduction_2 _ = notHappyAtAll + +happyReduce_3 = happySpecReduce_3 5 happyReduction_3 +happyReduction_3 (HappyAbsSyn5 happy_var_3) + _ + (HappyTerminal (TokenSetName happy_var_1)) + = HappyAbsSyn5 + (happy_var_1:happy_var_3 + ) +happyReduction_3 _ _ _ = notHappyAtAll + +happyReduce_4 = happySpecReduce_1 6 happyReduction_4 +happyReduction_4 (HappyTerminal (TokenVarName happy_var_1)) + = HappyAbsSyn6 + ([happy_var_1] + ) +happyReduction_4 _ = notHappyAtAll + +happyReduce_5 = happySpecReduce_3 6 happyReduction_5 +happyReduction_5 (HappyAbsSyn6 happy_var_3) + _ + (HappyTerminal (TokenVarName happy_var_1)) + = HappyAbsSyn6 + (happy_var_1:happy_var_3 + ) +happyReduction_5 _ _ _ = notHappyAtAll + +happyReduce_6 = happySpecReduce_1 7 happyReduction_6 +happyReduction_6 (HappyAbsSyn8 happy_var_1) + = HappyAbsSyn7 + ([happy_var_1] + ) +happyReduction_6 _ = notHappyAtAll + +happyReduce_7 = happySpecReduce_3 7 happyReduction_7 +happyReduction_7 (HappyAbsSyn7 happy_var_3) + _ + (HappyAbsSyn8 happy_var_1) + = HappyAbsSyn7 + (happy_var_1:happy_var_3 + ) +happyReduction_7 _ _ _ = notHappyAtAll + +happyReduce_8 = happyReduce 7 8 happyReduction_8 +happyReduction_8 (_ `HappyStk` + (HappyAbsSyn9 happy_var_6) `HappyStk` + _ `HappyStk` + _ `HappyStk` + (HappyTerminal (TokenSetName happy_var_3)) `HappyStk` + _ `HappyStk` + _ `HappyStk` + happyRest) + = HappyAbsSyn8 + (FuncCall (PredefFunc Filter) [Var happy_var_3] [happy_var_6] + ) `HappyStk` happyRest + +happyReduce_9 = happyReduce 6 9 happyReduction_9 +happyReduction_9 ((HappyAbsSyn10 happy_var_6) `HappyStk` + _ `HappyStk` + _ `HappyStk` + (HappyAbsSyn6 happy_var_3) `HappyStk` + _ `HappyStk` + _ `HappyStk` + happyRest) + = HappyAbsSyn9 + (FuncDef [] happy_var_3 happy_var_6 + ) `HappyStk` happyRest + +happyReduce_10 = happySpecReduce_3 10 happyReduction_10 +happyReduction_10 (HappyAbsSyn10 happy_var_3) + _ + (HappyAbsSyn10 happy_var_1) + = HappyAbsSyn10 + (FuncCall (PredefFunc IsEqual) [] [happy_var_1, happy_var_3] + ) +happyReduction_10 _ _ _ = notHappyAtAll + +happyReduce_11 = happyReduce 4 10 happyReduction_11 +happyReduction_11 (_ `HappyStk` + (HappyTerminal (TokenNat happy_var_3)) `HappyStk` + _ `HappyStk` + _ `HappyStk` + happyRest) + = HappyAbsSyn10 + (FuncCall (PredefFunc RecordIndex) [] [Types.Int happy_var_3] + ) `HappyStk` happyRest + +happyReduce_12 = happySpecReduce_1 10 happyReduction_12 +happyReduction_12 (HappyTerminal (TokenString happy_var_1)) + = HappyAbsSyn10 + (Types.String happy_var_1 + ) +happyReduction_12 _ = notHappyAtAll + +happyReduce_13 = happySpecReduce_1 10 happyReduction_13 +happyReduction_13 (HappyTerminal (TokenVarName happy_var_1)) + = HappyAbsSyn10 + (Var happy_var_1 + ) +happyReduction_13 _ = notHappyAtAll + +happyReduce_14 = happySpecReduce_1 10 happyReduction_14 +happyReduction_14 (HappyAbsSyn11 happy_var_1) + = HappyAbsSyn10 + (happy_var_1 + ) +happyReduction_14 _ = notHappyAtAll + +happyReduce_15 = happySpecReduce_1 10 happyReduction_15 +happyReduction_15 _ + = HappyAbsSyn10 + (Boolean True + ) + +happyReduce_16 = happySpecReduce_1 10 happyReduction_16 +happyReduction_16 _ + = HappyAbsSyn10 + (Boolean False + ) + +happyReduce_17 = happySpecReduce_3 11 happyReduction_17 +happyReduction_17 _ + (HappyAbsSyn12 happy_var_2) + _ + = HappyAbsSyn11 + (Record happy_var_2 + ) +happyReduction_17 _ _ _ = notHappyAtAll + +happyReduce_18 = happySpecReduce_1 12 happyReduction_18 +happyReduction_18 (HappyAbsSyn10 happy_var_1) + = HappyAbsSyn12 + ([happy_var_1] + ) +happyReduction_18 _ = notHappyAtAll + +happyReduce_19 = happySpecReduce_3 12 happyReduction_19 +happyReduction_19 (HappyAbsSyn12 happy_var_3) + _ + (HappyAbsSyn10 happy_var_1) + = HappyAbsSyn12 + (happy_var_1:happy_var_3 + ) +happyReduction_19 _ _ _ = notHappyAtAll + +happyNewToken action sts stk [] = + action 33 33 notHappyAtAll (HappyState action) sts stk [] + +happyNewToken action sts stk (tk:tks) = + let cont i = action i i tk (HappyState action) sts stk tks in + case tk of { + TokenFilter -> cont 13; + TokenInSet -> cont 14; + TokenOutSet -> cont 15; + TokenSetName happy_dollar_dollar -> cont 16; + TokenNat happy_dollar_dollar -> cont 17; + TokenVarName happy_dollar_dollar -> cont 18; + TokenTrue -> cont 19; + TokenFalse -> cont 20; + TokenString happy_dollar_dollar -> cont 21; + TokenLeftSqBracket -> cont 22; + TokenRightSqBracket -> cont 23; + TokenArrow -> cont 24; + TokenisEqual -> cont 25; + TokenisNotEqual -> cont 26; + TokenLeftBracket -> cont 27; + TokenRightBracket -> cont 28; + TokenSemiCol -> cont 29; + TokenLambda -> cont 30; + TokenComma -> cont 31; + TokenFullStop -> cont 32; + _ -> happyError' ((tk:tks), []) + } + +happyError_ explist 33 tk tks = happyError' (tks, explist) +happyError_ explist _ tk tks = happyError' ((tk:tks), explist) + +newtype HappyIdentity a = HappyIdentity a +happyIdentity = HappyIdentity +happyRunIdentity (HappyIdentity a) = a + +instance Prelude.Functor HappyIdentity where + fmap f (HappyIdentity a) = HappyIdentity (f a) + +instance Applicative HappyIdentity where + pure = HappyIdentity + (<*>) = ap +instance Prelude.Monad HappyIdentity where + return = pure + (HappyIdentity p) >>= q = q p + +happyThen :: () => HappyIdentity a -> (a -> HappyIdentity b) -> HappyIdentity b +happyThen = (Prelude.>>=) +happyReturn :: () => a -> HappyIdentity a +happyReturn = (Prelude.return) +happyThen1 m k tks = (Prelude.>>=) m (\a -> k a tks) +happyReturn1 :: () => a -> b -> HappyIdentity a +happyReturn1 = \a tks -> (Prelude.return) a +happyError' :: () => ([(Token)], [Prelude.String]) -> HappyIdentity a +happyError' = HappyIdentity Prelude.. (\(tokens, _) -> parseError tokens) +parseSource tks = happyRunIdentity happySomeParser where + happySomeParser = happyThen (happyParse action_0 tks) (\x -> case x of {HappyAbsSyn4 z -> happyReturn z; _other -> notHappyAtAll }) + +happySeq = happyDontSeq + + +parseError :: [Token] -> a +parseError _ = error "Parse error" +{-# LINE 1 "templates/GenericTemplate.hs" #-} +-- $Id: GenericTemplate.hs,v 1.26 2005/01/14 14:47:22 simonmar Exp $ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +data Happy_IntList = HappyCons Prelude.Int Happy_IntList + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +infixr 9 `HappyStk` +data HappyStk a = HappyStk a (HappyStk a) + +----------------------------------------------------------------------------- +-- starting the parse + +happyParse start_state = happyNewToken start_state notHappyAtAll notHappyAtAll + +----------------------------------------------------------------------------- +-- Accepting the parse + +-- If the current token is ERROR_TOK, it means we've just accepted a partial +-- parse (a %partial parser). We must ignore the saved token on the top of +-- the stack in this case. +happyAccept (1) tk st sts (_ `HappyStk` ans `HappyStk` _) = + happyReturn1 ans +happyAccept j tk st sts (HappyStk ans _) = + (happyReturn1 ans) + +----------------------------------------------------------------------------- +-- Arrays only: do the next action + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +indexShortOffAddr arr off = arr Happy_Data_Array.! off + + +{-# INLINE happyLt #-} +happyLt x y = (x Prelude.< y) + + + + + + +readArrayBit arr bit = + Bits.testBit (indexShortOffAddr arr (bit `Prelude.div` 16)) (bit `Prelude.mod` 16) + + + + + + +----------------------------------------------------------------------------- +-- HappyState data type (not arrays) + + + +newtype HappyState b c = HappyState + (Prelude.Int -> -- token number + Prelude.Int -> -- token number (yes, again) + b -> -- token semantic value + HappyState b c -> -- current state + [HappyState b c] -> -- state stack + c) + + + +----------------------------------------------------------------------------- +-- Shifting a token + +happyShift new_state (1) tk st sts stk@(x `HappyStk` _) = + let i = (case x of { HappyErrorToken (i) -> i }) in +-- trace "shifting the error token" $ + new_state i i tk (HappyState (new_state)) ((st):(sts)) (stk) + +happyShift new_state i tk st sts stk = + happyNewToken new_state ((st):(sts)) ((HappyTerminal (tk))`HappyStk`stk) + +-- happyReduce is specialised for the common cases. + +happySpecReduce_0 i fn (1) tk st sts stk + = happyFail [] (1) tk st sts stk +happySpecReduce_0 nt fn j tk st@((HappyState (action))) sts stk + = action nt j tk st ((st):(sts)) (fn `HappyStk` stk) + +happySpecReduce_1 i fn (1) tk st sts stk + = happyFail [] (1) tk st sts stk +happySpecReduce_1 nt fn j tk _ sts@(((st@(HappyState (action))):(_))) (v1`HappyStk`stk') + = let r = fn v1 in + happySeq r (action nt j tk st sts (r `HappyStk` stk')) + +happySpecReduce_2 i fn (1) tk st sts stk + = happyFail [] (1) tk st sts stk +happySpecReduce_2 nt fn j tk _ ((_):(sts@(((st@(HappyState (action))):(_))))) (v1`HappyStk`v2`HappyStk`stk') + = let r = fn v1 v2 in + happySeq r (action nt j tk st sts (r `HappyStk` stk')) + +happySpecReduce_3 i fn (1) tk st sts stk + = happyFail [] (1) tk st sts stk +happySpecReduce_3 nt fn j tk _ ((_):(((_):(sts@(((st@(HappyState (action))):(_))))))) (v1`HappyStk`v2`HappyStk`v3`HappyStk`stk') + = let r = fn v1 v2 v3 in + happySeq r (action nt j tk st sts (r `HappyStk` stk')) + +happyReduce k i fn (1) tk st sts stk + = happyFail [] (1) tk st sts stk +happyReduce k nt fn j tk st sts stk + = case happyDrop (k Prelude.- ((1) :: Prelude.Int)) sts of + sts1@(((st1@(HappyState (action))):(_))) -> + let r = fn stk in -- it doesn't hurt to always seq here... + happyDoSeq r (action nt j tk st1 sts1 r) + +happyMonadReduce k nt fn (1) tk st sts stk + = happyFail [] (1) tk st sts stk +happyMonadReduce k nt fn j tk st sts stk = + case happyDrop k ((st):(sts)) of + sts1@(((st1@(HappyState (action))):(_))) -> + let drop_stk = happyDropStk k stk in + happyThen1 (fn stk tk) (\r -> action nt j tk st1 sts1 (r `HappyStk` drop_stk)) + +happyMonad2Reduce k nt fn (1) tk st sts stk + = happyFail [] (1) tk st sts stk +happyMonad2Reduce k nt fn j tk st sts stk = + case happyDrop k ((st):(sts)) of + sts1@(((st1@(HappyState (action))):(_))) -> + let drop_stk = happyDropStk k stk + + + + + + _ = nt :: Prelude.Int + new_state = action + + in + happyThen1 (fn stk tk) (\r -> happyNewToken new_state sts1 (r `HappyStk` drop_stk)) + +happyDrop (0) l = l +happyDrop n ((_):(t)) = happyDrop (n Prelude.- ((1) :: Prelude.Int)) t + +happyDropStk (0) l = l +happyDropStk n (x `HappyStk` xs) = happyDropStk (n Prelude.- ((1)::Prelude.Int)) xs + +----------------------------------------------------------------------------- +-- Moving to a new state after a reduction + + + + + + + + + +happyGoto action j tk st = action j j tk (HappyState action) + + +----------------------------------------------------------------------------- +-- Error recovery (ERROR_TOK is the error token) + +-- parse error if we are in recovery and we fail again +happyFail explist (1) tk old_st _ stk@(x `HappyStk` _) = + let i = (case x of { HappyErrorToken (i) -> i }) in +-- trace "failing" $ + happyError_ explist i tk + +{- We don't need state discarding for our restricted implementation of + "error". In fact, it can cause some bogus parses, so I've disabled it + for now --SDM + +-- discard a state +happyFail ERROR_TOK tk old_st CONS(HAPPYSTATE(action),sts) + (saved_tok `HappyStk` _ `HappyStk` stk) = +-- trace ("discarding state, depth " ++ show (length stk)) $ + DO_ACTION(action,ERROR_TOK,tk,sts,(saved_tok`HappyStk`stk)) +-} + +-- Enter error recovery: generate an error token, +-- save the old token and carry on. +happyFail explist i tk (HappyState (action)) sts stk = +-- trace "entering error recovery" $ + action (1) (1) tk (HappyState (action)) sts ((HappyErrorToken (i)) `HappyStk` stk) + +-- Internal happy errors: + +notHappyAtAll :: a +notHappyAtAll = Prelude.error "Internal Happy error\n" + +----------------------------------------------------------------------------- +-- Hack to get the typechecker to accept our action functions + + + + + + + +----------------------------------------------------------------------------- +-- Seq-ing. If the --strict flag is given, then Happy emits +-- happySeq = happyDoSeq +-- otherwise it emits +-- happySeq = happyDontSeq + +happyDoSeq, happyDontSeq :: a -> b -> b +happyDoSeq a b = a `Prelude.seq` b +happyDontSeq a b = b + +----------------------------------------------------------------------------- +-- Don't inline any functions from the template. GHC has a nasty habit +-- of deciding to inline happyGoto everywhere, which increases the size of +-- the generated parser quite a bit. + + + + + + + + + +{-# NOINLINE happyShift #-} +{-# NOINLINE happySpecReduce_0 #-} +{-# NOINLINE happySpecReduce_1 #-} +{-# NOINLINE happySpecReduce_2 #-} +{-# NOINLINE happySpecReduce_3 #-} +{-# NOINLINE happyReduce #-} +{-# NOINLINE happyMonadReduce #-} +{-# NOINLINE happyGoto #-} +{-# NOINLINE happyFail #-} + +-- end of Happy Template. diff --git a/Parser.y b/Parser.y index e994c6b94d2171f02b3340ce54f8db0a071d359e..5bcbb6773ffd95952e7f9fa3eaaefb672c6a5870 100644 --- a/Parser.y +++ b/Parser.y @@ -1,9 +1,10 @@ { module Parser where import Lexer +import Types } -%name parseCalc +%name parseSource %tokentype {Token} %error {parseError} @@ -11,14 +12,12 @@ import Lexer filter { TokenFilter } in { TokenInSet } out { TokenOutSet } - Setname { TokenSetName $$ } - function { TokenFunc } - nat { TokenNat $$ } - var { TokenVarName $$ } - Record { TokenRecord } + SetName { TokenSetName $$ } + Nat { TokenNat $$ } + VarName { TokenVarName $$ } true { TokenTrue } false { TokenFalse } - string { TokenString s } + Str { TokenString $$ } '[' { TokenLeftSqBracket } ']' { TokenRightSqBracket } "->" { TokenArrow } @@ -26,49 +25,46 @@ import Lexer "/=" { TokenisNotEqual } '(' { TokenLeftBracket } ')' { TokenRightBracket } - ';' { TokenSemiCo } + ';' { TokenSemiCol } '\\' { TokenLambda } ',' { TokenComma } - '.' { TokenFullstop } + '.' { TokenFullStop } %right "->" %left "/=" "==" ';' %% +Prog : in SetNames out SetFuncCalls {($2,$4)} -Prog : '.' in SetNames '.' out SetFuncCall {($3,$6)} +SetNames : SetName {[$1]} + | SetName ',' SetNames { $1:$3} -SetNames : Setname {[$1]} - | SetNames ',' setName { } +VarNames : VarName {[$1]} + | VarName ',' VarNames {$1:$3} -VarNames : var {Var $1} - | var ',' VarNames {Var $1 $3} +SetFuncCalls : SetFuncCall {[$1]} + | SetFuncCall';' SetFuncCalls {$1:$3} -SetSetFuncCalls : SetFuncCall {[SetFuncCall]} - | SetFuncCall; SetFuncCalls {SetFuncCall:SetFuncCalls} +SetFuncCall : filter '['SetName']' '('Func')' {FuncCall (PredefFunc Filter) [Var $3] [$6]} -SetFuncCall : filter '['SetName']' '('Func')' {FuncCall (PredefFunc Filter) [$3] [$6]} +Func : '\\' '(' VarNames ')' "->" Expr {FuncDef [] $3 $6} -Func : \'('VarNames')' -> Expr {FuncDef [] $3 $6} - -Exp : '.' in var '.' out Exp {Prog $3 $6} - | filter '[' Setname ']' Exp {Filter $3 $5} - | '(' '\\' - -Expr : Expr == Expr {FuncCall (PredefFunc IsEqual) [] [$1, $3]} - | Expr'['Nat']' {FuncCall (PredefFunc RecordIndex) [] [$3]} - | String {String $1} +Expr : Expr "==" Expr {FuncCall (PredefFunc IsEqual) [] [$1, $3]} + | Expr'['Nat']' {FuncCall (PredefFunc RecordIndex) [] [Types.Int $3]} + | Str {Types.String $1} | VarName {Var $1} | Record {$1} | true {Boolean True} | false {Boolean False} Record : '['Exprs']' {Record $2} + Exprs : Expr {[$1]} - | Expr','Exprs {$1:$2} + | Expr','Exprs {$1:$3} { - +parseError :: [Token] -> a +parseError _ = error "Parse error" } diff --git a/Print2DListLex.hs b/Print2DListLex.hs index 1572773ae35d8d665dc7e80d53ce56a35a88b4e6..aa895ae1a61e0480d364f17eb3221cd2c8347f89 100644 --- a/Print2DListLex.hs +++ b/Print2DListLex.hs @@ -2,7 +2,7 @@ import Data.List import Data.Char import Data.Sequence --- | Function print2DListLex +-- | Function print2aaaDListLex -- 1. takes in a list of records -- 2. prints them in lexicographical order print2DListLex :: [[String]] -> IO() @@ -10,4 +10,4 @@ print2DListLex (record:records) = sortBy () records -- | rearran rearrange :: [[String]] -> [[String]] -rearrange records \ No newline at end of file +rearrange record \ No newline at end of file diff --git a/SampleSet.csv b/SampleSet.csv new file mode 100644 index 0000000000000000000000000000000000000000..f912eeba142cff35ae3a045abe37418111daa8fd --- /dev/null +++ b/SampleSet.csv @@ -0,0 +1,5 @@ + hello ,tree +big ,apple +hello,world +he,good +hello,good bye diff --git a/sampleprogram.txt b/sampleprogram.txt new file mode 100644 index 0000000000000000000000000000000000000000..8c5dc4adf60d70127ca1ebc601f41fb2532ebbf2 --- /dev/null +++ b/sampleprogram.txt @@ -0,0 +1,5 @@ +.in +SampleSet + +.out +filter[A](\(r) -> r[1] == "hello") \ No newline at end of file