diff --git a/test.sh b/test.sh index 4b3fa0e..3a7a556 100755 --- a/test.sh +++ b/test.sh @@ -1,17 +1,29 @@ #!/usr/bin/env zsh -for fic in tests/*.in; do - input="${fic:r}" - arg="$(cat $input.in)" - print -n -- "$input: " - runghc y.hs "$arg" > $input.res - if diff $input.res $input.out > /dev/null; then - print -- "OK" - else - print -- "ERROR" - print -- " expected: '$(cat $input.out)'" - print -- " got: '$(cat $input.res)'" - print -- "" - fi - \rm -f $input.res +tmpfic=tests/tmp +for input in tests/*; do + sed 's/\\/\\\\/g' $input > $tmpfic + # set 3 as the file descriptor for the file $tmpfic + exec 3< $tmpfic + done=0 + num=1 + until ((done == 1)); do + read <&3 program + (($?!=0)) && {done=1;continue} + read <&3 expected + (($?!=0)) && {done=1;continue} + result="$( runghc y.hs "$program")" + printf "%18s (line %3d): " ${input:t} $num + if [[ $expected == $result ]]; then + print -- "OK" + else + print -- "ERROR" + print -- " program: '$program'" + print -- " expected: '$expected'" + print -- " got: '$result'" + print -- "" + fi + ((num+=2)) + done done +\rm -f $tmpfic diff --git a/tests/binarynumber.out b/tests/binarynumber similarity index 52% rename from tests/binarynumber.out rename to tests/binarynumber index 9f500df..a9ffca6 100644 --- a/tests/binarynumber.out +++ b/tests/binarynumber @@ -1 +1,2 @@ +#b001010 Number 10 diff --git a/tests/binarynumber.in b/tests/binarynumber.in deleted file mode 100644 index a3b9bf9..0000000 --- a/tests/binarynumber.in +++ /dev/null @@ -1 +0,0 @@ -#b001010 diff --git a/tests/char.out b/tests/char similarity index 77% rename from tests/char.out rename to tests/char index 5915776..56f7a70 100644 --- a/tests/char.out +++ b/tests/char @@ -1 +1,2 @@ +#\a Character 'a' diff --git a/tests/char.in b/tests/char.in deleted file mode 100644 index e78f028..0000000 --- a/tests/char.in +++ /dev/null @@ -1 +0,0 @@ -#\a diff --git a/tests/float.out b/tests/float similarity index 68% rename from tests/float.out rename to tests/float index c13b93f..25be416 100644 --- a/tests/float.out +++ b/tests/float @@ -1 +1,2 @@ +3.14 Float 3.14 diff --git a/tests/float.in b/tests/float.in deleted file mode 100644 index 6324d40..0000000 --- a/tests/float.in +++ /dev/null @@ -1 +0,0 @@ -3.14 diff --git a/tests/number.out b/tests/number similarity index 76% rename from tests/number.out rename to tests/number index 64ed153..5b34a3d 100644 --- a/tests/number.out +++ b/tests/number @@ -1 +1,2 @@ +32 Number 32 diff --git a/tests/number.in b/tests/number.in deleted file mode 100644 index f5c8955..0000000 --- a/tests/number.in +++ /dev/null @@ -1 +0,0 @@ -32 diff --git a/tests/string b/tests/string new file mode 100644 index 0000000..341906f --- /dev/null +++ b/tests/string @@ -0,0 +1,6 @@ +"This is a simple string" +String "This is a simple string" +"This is a string with a return ->\n<- Here" +String "This is a string with a return ->\n<- Here" +"More difficult string \n, \r, \t, \\, \x, \A" +String "More difficult string \n, \r, \t, \\, x, A" diff --git a/tests/string-2.in b/tests/string-2.in deleted file mode 100644 index 8d0a559..0000000 --- a/tests/string-2.in +++ /dev/null @@ -1 +0,0 @@ -"This is a string with a return ->\n<- Here" diff --git a/tests/string-2.out b/tests/string-2.out deleted file mode 100644 index 5a16424..0000000 --- a/tests/string-2.out +++ /dev/null @@ -1 +0,0 @@ -String "This is a string with a return ->\n<- Here" diff --git a/tests/string-3.in b/tests/string-3.in deleted file mode 100644 index eb22e25..0000000 --- a/tests/string-3.in +++ /dev/null @@ -1 +0,0 @@ -"More difficult string \n, \r, \t, \\, \x, \A" diff --git a/tests/string-3.out b/tests/string-3.out deleted file mode 100644 index 5ee6306..0000000 --- a/tests/string-3.out +++ /dev/null @@ -1 +0,0 @@ -String "More difficult string \n, \r, \t, \\, x, A" diff --git a/tests/string.in b/tests/string.in deleted file mode 100644 index 4c0cf18..0000000 --- a/tests/string.in +++ /dev/null @@ -1 +0,0 @@ -"This is a simple string" diff --git a/tests/string.out b/tests/string.out deleted file mode 100644 index 1513617..0000000 --- a/tests/string.out +++ /dev/null @@ -1 +0,0 @@ -String "This is a simple string" diff --git a/y.hs b/y.hs index bb4cbc4..5b98663 100644 --- a/y.hs +++ b/y.hs @@ -1,5 +1,6 @@ module Main where +import Control.Monad (liftM) import Data.List (foldl') import Text.ParserCombinators.Parsec hiding (spaces) import System.Environment (getArgs) @@ -36,6 +37,12 @@ parseExpr = parseString <|> try parseFloat -- 3.1415 <|> parseNumber -- 3, #b011001, #o070, #d930, #xFF3 <|> parseAtom -- symbol-323 + <|> parseQuoted + <|> do + char '(' + x <- try parseList <|> parseDottedList + char ')' + return x symbol :: Parser Char symbol = oneOf "!#$%&|*+-/:<=>?@^_~" @@ -138,3 +145,19 @@ parseNumber = do <|> parseBaseSpecifiedNumber return (Number number) +-- Recursive Parsers + +parseList :: Parser LispVal +parseList = liftM List $ sepBy parseExpr spaces + +parseDottedList :: Parser LispVal +parseDottedList = do + head <- endBy parseExpr spaces + tail <- char '.' >> spaces >> parseExpr + return $ DottedList head tail + +parseQuoted :: Parser LispVal +parseQuoted = do + char '\'' + x <- parseExpr + return $ List [Atom "quote", x]