Skip to content

Commit

Permalink
Add stdlib
Browse files Browse the repository at this point in the history
  • Loading branch information
Saverio976 committed Jan 14, 2024
1 parent 2f27dc8 commit 0b84024
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 10 deletions.
14 changes: 11 additions & 3 deletions lvtc/app/Args.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 =
Expand All @@ -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
Expand All @@ -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
6 changes: 3 additions & 3 deletions lvtc/app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions lvtc/app/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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) =
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lvtc/src/Lexeme.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lvtc/src/ParseLvt.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
30 changes: 30 additions & 0 deletions lvtc/stdlib/Convert.lvt
Original file line number Diff line number Diff line change
@@ -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;
};
40 changes: 40 additions & 0 deletions lvtc/stdlib/Logic.lvt
Original file line number Diff line number Diff line change
@@ -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));
};
57 changes: 57 additions & 0 deletions lvtc/stdlib/Math.lvt
Original file line number Diff line number Diff line change
@@ -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);
};

0 comments on commit 0b84024

Please sign in to comment.