Commit bc7c1f70 authored by pm3g19's avatar pm3g19
Browse files

[COLLAB]

parent 4ffbb4a8
...@@ -33,7 +33,13 @@ evalFinal :: Expr -> Expr -- whnf set to ready set ...@@ -33,7 +33,13 @@ evalFinal :: Expr -> Expr -- whnf set to ready set
evalFinal = evalFull [] evalFinal = evalFull []
showFinal :: Expr -> IO () showFinal :: Expr -> IO ()
showFinal = notImplemented showFinal = (print2DList.sort2DListLex.setTo2DList)
setTo2DList :: Expr -> [[String]]
setTo2DList = notImplemented
sort2DListLex = id -- TODO change this
print2DList = notImplemented
-------------------------------------------- --------------------------------------------
loadInputFile :: SymbolName -> IO Expr loadInputFile :: SymbolName -> IO Expr
...@@ -43,12 +49,13 @@ loadInputFile name = do ...@@ -43,12 +49,13 @@ loadInputFile name = do
return output return output
where where
toRecord stringList = Record $ map ((String).stripLeadingAndTrailingWhitespace) stringList toRecord stringList = Record $ map ((String).stripWhitespace) stringList
stripLeadingAndTrailingWhitespace = stripTrailingWhitespace.dropWhile (==' ') stripWhitespace = stripTrailingWhitespace.dropWhile (==' ')
stripTrailingWhitespace (' ':xs) = let remainder = stripTrailingWhitespace xs in stripTrailingWhitespace (' ':xs) = let remainder = stripTrailingWhitespace xs in
if null remainder then [] else ' ':remainder if null remainder then [] else ' ':remainder
stripTrailingWhitespace (x:xs) = x : stripTrailingWhitespace xs stripTrailingWhitespace (x:xs) = x : stripTrailingWhitespace xs
stripTrailingWhitespace [] = [] stripTrailingWhitespace [] = []
...@@ -5,32 +5,34 @@ module Lexer where ...@@ -5,32 +5,34 @@ module Lexer where
%wrapper "basic" %wrapper "basic"
$digit = 0-9 $digit = 0-9
$alpha = [a-zA-Z] $alpha = [a-zA-Z]
$lower = [a-z]
$upper = [A-Z]
tokens :- tokens :-
$white+ ; $white+ ;
"--".* ; "--".* ;
Filter {\s -> TokenFilter } filter {\s -> TokenFilter }
SetName {\s -> TokenSetName s } $upper[$alpha]* {\s -> TokenSetName s }
Func {\s -> TokenFunc } Func {\s -> TokenFunc }
Nat {\s -> TokenNat (read s) } $digit+ {\s -> TokenNat (read s) }
VarName {\s -> TokenVarName s } $lower [$lower $digit \_ \']* {\s -> TokenVarName s }
Record {\s -> TokenRecord } Record {\s -> TokenRecord }
true {\s -> TokenTrue } true {\s -> TokenTrue }
false {\s -> TokenFalse } false {\s -> TokenFalse }
String {\s -> TokenString s } $[$alpha $digit]* {\s -> TokenString s }
'[' {\s -> TokenLeftSqBracket } '[' {\s -> TokenLeftSqBracket }
']' {\s -> TokenRightSqBracket } ']' {\s -> TokenRightSqBracket }
"->" {\s -> TokenArrow } "->" {\s -> TokenArrow }
"==" {\s -> TokenisEqual } "==" {\s -> TokenisEqual }
"/=" {\s -> TokenisNotEqual } "/=" {\s -> TokenisNotEqual }
'(' {\s -> TokenLeftBracket } '(' {\s -> TokenLeftBracket }
')' {\s -> TokenRightBracket } ')' {\s -> TokenRightBracket }
';' {\s -> TokenApp } ';' {\s -> TokenApp }
'\\' {\s -> TokenLambda } '\\' {\s -> TokenLambda }
',' {\s -> TokenComma } ',' {\s -> TokenComma }
'.' {\s -> TokenFullstop } '.' {\s -> TokenFullstop }
in {\s -> TokenInSet } in {\s -> TokenInSet }
out {\s -> TokenOutSet } out {\s -> TokenOutSet }
{ {
--token type: --token type:
data Token = data Token =
......
{
module Parser where
import Lexer
}
%name parseCalc
%tokentype {Token}
%error {parseError}
Prog : .in SetNames .out SetFuncCalls {($2, $4)} %token
filter { TokenFilter }
in { TokenInSet }
out { TokenOutSet }
Setname { TokenSetName $$ }
function { TokenFunc }
nat { TokenNat $$ }
var { TokenVarName $$ }
Record { TokenRecord }
true { TokenTrue }
false { TokenFalse }
string { TokenString s }
'[' { TokenLeftSqBracket }
']' { TokenRightSqBracket }
"->" { TokenArrow }
"==" { TokenisEqual }
"/=" { TokenisNotEqual }
'(' { TokenLeftBracket }
')' { TokenRightBracket }
';' { TokenSemiCo }
'\\' { TokenLambda }
',' { TokenComma }
'.' { TokenFullstop }
SetNames : SetName {[$1]} %right "->"
| SetNames, SetName %left "/=" "==" ';'
%%
VarNames : VarName
| VarName',' VarNames
Prog : '.' in SetNames '.' out SetFuncCall {($3,$6)}
SetNames : Setname {[$1]}
| SetNames ',' setName { }
VarNames : var {Var $1}
| var ',' VarNames {Var $1 $3}
SetSetFuncCalls : SetFuncCall {[SetFuncCall]} SetSetFuncCalls : SetFuncCall {[SetFuncCall]}
| SetFuncCall; SetFuncCalls {SetFuncCall:SetFuncCalls} | SetFuncCall; SetFuncCalls {SetFuncCall:SetFuncCalls}
SetFuncCall : filter '['SetName']' '('Func')' {FuncCall (PredefFunc Filter) [$3] [$6]} SetFuncCall : filter '['SetName']' '('Func')' {FuncCall (PredefFunc Filter) [$3] [$6]}
--filter[A](\r -> r[2] /= "")
Func : \'('VarNames')' -> Expr {FuncDef [] $3 $6} Func : \'('VarNames')' -> Expr {FuncDef [] $3 $6}
--\(r) -> r[1] == "hello"
Exp : '.' in var '.' out Exp {Prog $3 $6}
| filter '[' Setname ']' Exp {Filter $3 $5}
| '(' '\\'
Expr : Expr == Expr {FuncCall (PredefFunc IsEqual) [] [$1, $3]} Expr : Expr == Expr {FuncCall (PredefFunc IsEqual) [] [$1, $3]}
| Expr'['Nat']' {FuncCall (PredefFunc RecordIndex) [] [$3]} | Expr'['Nat']' {FuncCall (PredefFunc RecordIndex) [] [$3]}
| String {String $1} | String {String $1}
| VarName {Var $1} | VarName {Var $1}
| Record {$1} | Record {$1}
| true {Boolean True} | true {Boolean True}
| false {Boolean False} | false {Boolean False}
Record : '['Exprs']' {Record $2} Record : '['Exprs']' {Record $2}
Exprs : Expr {[$1]} Exprs : Expr {[$1]}
| Expr','Exprs {$1:$2} | Expr','Exprs {$1:$2}
{
}
import Data.List
import Data.Char
import Data.Sequence
-- | Function print2DListLex
-- 1. takes in a list of records
-- 2. prints them in lexicographical order
print2DListLex :: [[String]] -> IO()
print2DListLex (record:records) = sortBy () records
-- | rearran
rearrange :: [[String]] -> [[String]]
rearrange records
\ No newline at end of file
...@@ -9,7 +9,6 @@ readCSV fname = do ...@@ -9,7 +9,6 @@ readCSV fname = do
readCSVString :: String -> [[String]] readCSVString :: String -> [[String]]
readCSVString whole = [splitElem ',' line | line <- splitElem '\n' whole] readCSVString whole = [splitElem ',' line | line <- splitElem '\n' whole]
splitElem :: Eq a => a -> [a] -> [[a]] splitElem :: Eq a => a -> [a] -> [[a]]
splitElem elem = split (/=elem) splitElem elem = split (/=elem)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment