Commit d03edc1a authored by pm3g19's avatar pm3g19
Browse files

Lexer, parser and pre-evaluation now work

parent bc7c1f70
......@@ -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
......
This diff is collapsed.
{
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 |
......
{-# 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