diff --git a/lvtc/app/Args.hs b/lvtc/app/Args.hs index 5d40246..56ae89a 100644 --- a/lvtc/app/Args.hs +++ b/lvtc/app/Args.hs @@ -22,7 +22,8 @@ data Args = Args { action :: Action, folderPath :: String, outFile :: String, - verbose :: Bool + verbose :: Bool, + folders :: [String] } parseArgs' :: [String] -> Args -> Either Args String @@ -42,6 +43,10 @@ parseArgs' ["-o"] _ = Right "Missing argument for -o" parseArgs' ("--verbose":xs) args = parseArgs' xs (args {verbose = True}) +parseArgs' ("--lib":x:xs) args = + parseArgs' xs (args {folders = x : folders args}) +parseArgs' ("-l":x:xs) args = + parseArgs' xs (args {folders = x : folders args}) parseArgs' (('-':xs):_) _ = Right ("Unknown option: " ++ xs) parseArgs' (x:xs) args = @@ -51,7 +56,8 @@ parseArgs :: [String] -> IO (Either Args String) parseArgs args = getCurrentDirectory >>= \path -> return (parseArgs' args (Args { - action = Run, folderPath = path, outFile = "out.wasm", verbose = False + action = Run, folderPath = path, outFile = "out.wasm", + verbose = False, folders = [] })) hLine1 :: String @@ -77,9 +83,11 @@ hLine9 = part1 ++ part2 part2 = " source code recursively from FOLDER\n" hLine10 :: String hLine10 = "\t--verbose\n\t\tVerbose mode\n" +hLine11 :: String +hLine11 = "\t-l, --lib\n\t\tAdd folder to compiled Leviator source code\n" printHelp :: IO () printHelp = putStr hLine1 >> putStr hLine2 >> putStr hLine3 >> putStr hLine4 >> putStr hLine5 >> putStr hLine6 >> putStr hLine7 >> putStr hLine8 - >> putStr hLine9 >> putStr hLine10 + >> putStr hLine9 >> putStr hLine10 >> putStr hLine11 diff --git a/lvtc/app/Main.hs b/lvtc/app/Main.hs index cb24862..0f41aca 100644 --- a/lvtc/app/Main.hs +++ b/lvtc/app/Main.hs @@ -14,9 +14,9 @@ import Run (run) import Version (printVersion) dispatchArgs :: Args -> IO () -dispatchArgs (Args Run fPath oFile v) = run (Args Run fPath oFile v) -dispatchArgs (Args ShowHelp _ _ _) = printHelp -dispatchArgs (Args ShowVersion _ _ _) = printVersion +dispatchArgs (Args Run fPath oFile v fs) = run (Args Run fPath oFile v fs) +dispatchArgs (Args ShowHelp _ _ _ _) = printHelp +dispatchArgs (Args ShowVersion _ _ _ _) = printVersion dispatchIfOk :: Either Args String -> IO () dispatchIfOk (Left args) = dispatchArgs args diff --git a/lvtc/app/Run.hs b/lvtc/app/Run.hs index 1a454fe..768ccd4 100644 --- a/lvtc/app/Run.hs +++ b/lvtc/app/Run.hs @@ -67,6 +67,13 @@ listAllFiles v path = p True = putStrLn ("Compiling Folder: " ++ show path) p False = return () +listsAllFiles :: Bool -> [FilePath] -> IO [FilePath] +listsAllFiles _ [] = return [] +listsAllFiles v (f:fs) = + listAllFiles v f + >>= (\files -> listsAllFiles v fs + >>= (\others -> return (files ++ others))) + getAllFunc :: Bool -> [Expression] -> IO [FuncDeclaration] getAllFunc _ [] = return [] getAllFunc v ((Expression.Function str):expressions) = @@ -119,10 +126,10 @@ showDebug True wasm = print wasm showDebug False _ = return () run :: Args -> IO () -run (Args Run fPath oFile v) = +run (Args Run fPath oFile v fPaths) = transformedWasm >>= \wasm -> (showDebug v wasm >> writeWasm wasm oFile) where - expressions = listAllFiles v fPath >>= getFilesExpression v + expressions = listsAllFiles v (fPath:fPaths) >>= getFilesExpression v funcs = expressions >>= getAllFunc v transformedWatLike = transformToWatLike v (checkAst v funcs) transformedWat = transformToWat v transformedWatLike diff --git a/lvtc/src/Lexeme.hs b/lvtc/src/Lexeme.hs index 083d301..fe8a845 100644 --- a/lvtc/src/Lexeme.hs +++ b/lvtc/src/Lexeme.hs @@ -13,6 +13,7 @@ module Lexeme replaceN :: Int -> String -> String replaceN _ [] = [] replaceN 0 ('"':xs) = '"' : replaceN 1 xs +replaceN 1 ('\\':'0':xs) = '\0' : replaceN 1 xs replaceN 1 ('\\':'n':xs) = '\n' : replaceN 1 xs replaceN 1 ('\\':'t':xs) = '\t' : replaceN 1 xs replaceN 1 ('\\':'v':xs) = '\v' : replaceN 1 xs diff --git a/lvtc/src/ParseLvt.hs b/lvtc/src/ParseLvt.hs index 74e6667..67a6a7b 100644 --- a/lvtc/src/ParseLvt.hs +++ b/lvtc/src/ParseLvt.hs @@ -95,8 +95,8 @@ parseOperatorOp = Var <$> (parseString "+" <|> parseString "-" <|> parseString "*" <|> parseString "/" <|> parseString "{" <|> parseString "}" - <|> parseString "==" <|> parseString "!=" <|> parseString "<" - <|> parseString ">" <|> parseString "<=" <|> parseString ">=") + <|> parseString "==" <|> parseString "!=" <|> parseString "<=" + <|> parseString ">=" <|> parseString "<" <|> parseString ">") parseOperator' :: ShuntingYardState -> Parser ShuntingYardState parseOperator' sys = diff --git a/lvtc/stdlib/Convert.lvt b/lvtc/stdlib/Convert.lvt new file mode 100644 index 0000000..b60fbf6 --- /dev/null +++ b/lvtc/stdlib/Convert.lvt @@ -0,0 +1,30 @@ +fn intToBool(a: Int) -> Bool +{ + if (a == 1) + { + <- True; + }; + <- False; +}; + +fn boolToInt(a: Bool) -> Int +{ + if (a) + { + <- 1; + }; + <- 0; +}; + +fn charToInt(a: Char) -> Int +{ + @Char i = '\0'; + @Int res = 0; + + while (i < a) + { + res = res + 1; + i = i + 1; + }; + <- res; +}; diff --git a/lvtc/stdlib/Logic.lvt b/lvtc/stdlib/Logic.lvt new file mode 100644 index 0000000..66289a3 --- /dev/null +++ b/lvtc/stdlib/Logic.lvt @@ -0,0 +1,40 @@ +fn or(a: Bool, b: Bool) -> Bool +{ + if (a) + { + <- True; + }; + <- b; +}; + +fn and(a: Bool, b: Bool) -> Bool +{ + if (a) + { + <- b; + }; + <- False; +}; + +fn not(a: Bool) -> Bool +{ + if (a) + { + <- False; + }; + <- True; +}; + +fn xor(a: Bool, b: Bool) -> Bool +{ + if (a) + { + <- not(b); + }; + <- b; +}; + +fn nand(a: Bool, b: Bool) -> Bool +{ + <- not(and(a, b)); +}; diff --git a/lvtc/stdlib/Math.lvt b/lvtc/stdlib/Math.lvt new file mode 100644 index 0000000..7b734b4 --- /dev/null +++ b/lvtc/stdlib/Math.lvt @@ -0,0 +1,57 @@ +fn mod(a: Int, b: Int) -> Int +{ + @Int res = a; + @Int q = a / b; + @Int r = q * b; + res = res - r; + <- res; +}; + +fn square(a: Int) -> Int +{ + <- a * a; +}; + +fn cube(a: Int) -> Int +{ + <- a * a * a; +}; + +fn pow(a: Int, b: Int) -> Int +{ + @Int res = 1; + @Int i = 0; + + while (i < b) + { + res = res * a; + i = i + 1; + }; + <- res; +}; + +fn factorial(a: Int) -> Int +{ + @Int res = 1; + @Int i = 2; + + while (i <= a) + { + res = res * i; + i = i + 1; + }; + <- res; +}; + +fn factorialRec(a: Int) -> Int +{ + if (a == 0) + { + <- 1; + }; + if (a == 1) + { + <- 1; + }; + <- a * factorialRec(a - 1); +};