Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kope parsing enhancement #19

Merged
merged 7 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/KopeParser.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module KopeParser (parseFile) where
module KopeParser (parseFile, kopeExpr) where

import KopeParserLib
import Control.Applicative
Expand Down
8 changes: 4 additions & 4 deletions src/KopeParserLib.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module KopeParserLib (KopeVal (..), Parser (..), ws, spanP, notNull, stringP, charP, letterP, oneOfP, removeNewline) where
module KopeParserLib (KopeVal (..), Parser (..), ws, spanP, notNull, stringP, charP, letterP, oneOfP) where

import Control.Applicative
import Data.Char
Expand Down Expand Up @@ -49,7 +49,7 @@ spanP f = Parser $ \input -> do
Just (rest, valid)

ws :: Parser String
ws = spanP isSpace <|> spanP (`elem` "\n")
ws = spanP isSpace

oneOfP :: [String] -> Parser KopeVal
oneOfP tokens = Parser $ \input -> f tokens input
Expand All @@ -72,5 +72,5 @@ stringP = sequenceA . map charP
letterP :: Parser String
letterP = notNull $ spanP (\input -> isLetter input || (== '_') input)

removeNewline :: String -> String
removeNewline xs = [ x | x <- xs, x /= '\n' ]
kopeComment :: Parser String
kopeComment = (++) <$> stringP "//" <*> spanP (/= '\n')
70 changes: 70 additions & 0 deletions test/KopeTests.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
module KopeTests (kopeTests) where
import Test.HUnit
import System.Exit
import KopeParserLib
import KopeParser

testCharP :: Test
testCharP = TestList [
runParser (charP 'a') "abc" ~=? Just ("bc", 'a'),
runParser (charP 'a') "bc" ~=? Nothing
]

testStringP :: Test
testStringP = TestList [
runParser (stringP "ab") "abc" ~=? Just ("c", "ab"),
runParser (stringP "ab") "acb" ~=? Nothing
]

testWs :: Test
testWs = runParser ws " \nd" ~=? Just ("d", " \n")

testNotNull :: Test
testNotNull = TestList [
runParser (notNull ws) "" ~=? Nothing,
runParser (notNull ws) " " ~=? Just ("", " ")
]

testOneOfP :: Test
testOneOfP = TestList [
runParser (oneOfP ["=", "*"]) "*" ~=? Just ("", KopeAtom "*"),
runParser (oneOfP ["=", "*"]) "-" ~=? Nothing
]

testLetterP :: Test
testLetterP = TestList [
runParser letterP "set_value" ~=? Just ("", "set_value"),
runParser letterP "?" ~=? Nothing
]

testKopeExpr :: Test
testKopeExpr = TestList [
runParser kopeExpr "fn set_value(arg, arg) { if (1 == 2) { return 1; }; print(\"Hello\"); }" ~=?
Just ("",KopeFunc {funcName = "set_value", funcParams = ["arg","arg"], funcBody = [KopeArray [KopeAtom "if",KopeArray [KopeAtom "==",KopeNumber 1,KopeNumber 2],KopeArray [KopeArray [KopeAtom "return",KopeNumber 1]]],KopeArray [KopeAtom "print",KopeString "Hello"]]})
]

testShowKopeVal :: Test
testShowKopeVal = TestList [
show (KopeString "test") ~=?
"KopeString \"test\""
]

testEqKopeVal :: Test
testEqKopeVal = TestList [
KopeBool True == KopeBool True ~=? True,
KopeBool True == KopeBool False ~=? False
]

kopeTests :: Test
kopeTests = TestList [
TestLabel "Test charP" testCharP,
TestLabel "Test stringP" testStringP,
TestLabel "Test ws" testWs,
TestLabel "Test notNull" testNotNull,
TestLabel "Test oneOfP" testOneOfP,
TestLabel "Test letterP" testLetterP,
TestLabel "Test kopeExpr" testKopeExpr,
TestLabel "Test show KopeVal" testShowKopeVal,
TestLabel "Test eq KopeVal" testEqKopeVal
]

9 changes: 8 additions & 1 deletion test/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import System.Exit
import Wasm (magic, version, buildSectionHeader, buildWasm, compileOp, compileExpr, buildDataSec, buildDataSegments, buildSegmentHeader, getIdData, compileGetLocalVar)
import WasmNumber (buildNumber, decodeNumber, buildWords, buildString)
import KopeParserLib (KopeVal (..))
import KopeTests (kopeTests)

testMagic :: Test
testMagic = TestCase (assertEqual "wrong magic value" ([0x00, 0x61, 0x73, 0x6d]) magic)
Expand Down Expand Up @@ -111,7 +112,13 @@ wasmTests = TestList [
TestLabel "build data with hello world" testBuildDataSec
]

allTests :: Test
allTests = TestList [
TestLabel "Kope Tests" kopeTests,
TestLabel "Wasm Tests" wasmTests
]

main :: IO ()
main = do
results <- runTestTT wasmTests
results <- runTestTT allTests
if failures results > 0 then System.Exit.exitFailure else System.Exit.exitSuccess