Skip to content
Snippets Groups Projects
Commit 934a4993 authored by pm3g19's avatar pm3g19
Browse files

Added support for extra functions, untested

parent 87fd77bb
No related branches found
No related tags found
No related merge requests found
...@@ -19,14 +19,43 @@ eval env expr = let (eval', evalFull') = (eval env, evalFull env) in case expr o ...@@ -19,14 +19,43 @@ eval env expr = let (eval', evalFull') = (eval env, evalFull env) in case expr o
Filter -> let (Set inputRecords,predicate) = (eval' $ head inputSets, eval' $ head args) in Filter -> let (Set inputRecords,predicate) = (eval' $ head inputSets, eval' $ head args) in
Set $ filter ((==Boolean True).(\r -> eval' $ FuncCall predicate [] [r])) $ map eval' inputRecords Set $ filter ((==Boolean True).(\r -> eval' $ FuncCall predicate [] [r])) $ map eval' inputRecords
Map -> Set (map (\record -> FuncCall lambda [] [record]) records)
where
(Set records:_) = inputSets
(lambda:_) = args
IsEqual -> let (e1:e2:_) = args in -- TODO not sufficent. IsEqual -> let (e1:e2:_) = args in -- TODO not sufficent.
Boolean (eval' e1 == eval' e2) Boolean (eval' e1 == eval' e2)
XProduct -> let ((Set l1): (Set l2):_) = map eval' inputSets in
Set $ [ x `concatRecord` y | x <- l1, y <- l2]
RecordIndex -> let (Record recordData:Int index:_) = (map eval' args) in RecordIndex -> let (Record recordData:Int index:_) = (map eval' args) in
recordData !! index recordData !! index
XProduct -> debug (show env) $ let ((Set l1): (Set l2):_) = map eval' inputSets in RecordSelect -> Record filteredList
Set $ [ x `concatRecord` y | x <- l1, y <- l2] where
(Record recordData: indexes) = args
indexesRaw = map (\(Int i) -> i) indexes
filteredList = map (recordData!!) indexesRaw
{-numberedElems = zip [1..] recordData :: [(Int, Expr)]
filtered = filter ((`elem` indexesRaw).fst) numberedElems :: [(Int, Expr)]
filteredList = map snd filtered :: [Expr]-}
IsEmpty -> case eval' (head args) of
(String a) -> Boolean $ null a
Contains -> case args of
(mainString:containsWhat:_) -> case (eval' mainString , eval' containsWhat) of
(String a, String b) -> a `beginsWith` b
Plus -> let (e1:e2:_) = args in case (eval' e1, eval' e2) of
(String a, String b) -> String (a ++ b)
(Record a, Record b) -> Record (a ++ b)
_ -> error "eval error"
--implement later --implement later
--(Map) -> Set $ (map (\r -> FuncCall predicate [] [r]) inputRecords) --(Map) -> Set $ (map (\r -> FuncCall predicate [] [r]) inputRecords)
...@@ -37,6 +66,11 @@ eval env expr = let (eval', evalFull') = (eval env, evalFull env) in case expr o ...@@ -37,6 +66,11 @@ eval env expr = let (eval', evalFull') = (eval env, evalFull env) in case expr o
-- TODO FIX -- TODO FIX
(Var name) -> eval' $ findVar env name (Var name) -> eval' $ findVar env name
control@(Control lastResult exprs) -> if null exprs then lastResult else eval' $ evalControl1 env control control@(Control lastResult exprs) -> if null exprs then lastResult else eval' $ evalControl1 env control
_ -> expr _ -> expr
...@@ -52,4 +86,5 @@ evalFull = eval ...@@ -52,4 +86,5 @@ evalFull = eval
--evalFull env (Set xs) = Set (map (eval env) xs) -- evaluates expression fully (not just weak head normal form) --evalFull env (Set xs) = Set (map (eval env) xs) -- evaluates expression fully (not just weak head normal form)
--evalFull _ e = e --evalFull _ e = e
--TODO implement properly --TODO implement properly
concatRecord (Record r1) (Record r2) = Record (r1 ++ r2) concatRecord (Record r1) (Record r2) = Record (r1 ++ r2)
\ No newline at end of file beginsWith = notImplemented
\ No newline at end of file
This diff is collapsed.
...@@ -7,16 +7,17 @@ type Environment = [(SymbolName, Expr)] -- (result of last calculation, other bi ...@@ -7,16 +7,17 @@ type Environment = [(SymbolName, Expr)] -- (result of last calculation, other bi
--data OutputLine = Let SymbolName Expr | Expr -- Expr always a function call for a set function, but not enforced by AST. --data OutputLine = Let SymbolName Expr | Expr -- Expr always a function call for a set function, but not enforced by AST.
type Output = [Expr] type Output = [Expr]
data PredefFunc = XProduct | XXProduct | IsEqual | IsNotEqual | Add --operators data PredefFunc = XProduct | XXProduct | IsEqual | IsNotEqual | Plus --operators
| Map | Filter | Map | Filter
| RecordIndex -- [] operator | RecordIndex -- [] operator
| IsEmpty | NotEmpty -- string functions | RecordSelect
| IsEmpty | NotEmpty | Contains | IsSubString -- string functions
deriving (Show, Eq) deriving (Show, Eq)
--n.b. these definitions do not enforce type checking! The use of any function or operator will be associated with a FuncCall. --n.b. these definitions do not enforce type checking! The use of any function or operator will be associated with a FuncCall.
--Therefore, it is possible to have ASTs with ridiculous things where e.g. a XX product is applied to and int and a String, or a function call has an Int as a function! --Therefore, it is possible to have ASTs with ridiculous things where e.g. a XX product is applied to and int and a String, or a function call has an Int as a function!
--These issues will be checked via the type checker after the AST has been built. --These issues can be checked via the type checker after the AST has been built.
-- filter [A] (\(r) -> r[1] == "hello") -- filter [A] (\(r) -> r[1] == "hello")
-- FuncCall (PredefFunc Filter) [Var "A"] [FuncDef [] ["r"] [(FuncCall (PredefFunc IsEqual) [] (r[1]) ("hello"))]] -- FuncCall (PredefFunc Filter) [Var "A"] [FuncDef [] ["r"] [(FuncCall (PredefFunc IsEqual) [] (r[1]) ("hello"))]]
...@@ -34,6 +35,7 @@ data Expr = Control Expr [Expr] -- result of last computation, sequence of instr ...@@ -34,6 +35,7 @@ data Expr = Control Expr [Expr] -- result of last computation, sequence of instr
| Int Int | Int Int
| String String | String String
| Boolean Bool | Boolean Bool
-- | Nat Int
deriving (Show, Eq) deriving (Show, Eq)
data Parameter = NamedParam SymbolName | TupleMatch [SymbolName] data Parameter = NamedParam SymbolName | TupleMatch [SymbolName]
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment