Skip to content

Commit

Permalink
Merge pull request #20 from X-R-G-B/dev
Browse files Browse the repository at this point in the history
dev
  • Loading branch information
Saverio976 authored Jan 14, 2024
2 parents 11ef2f1 + 0bb3f8f commit 920bc41
Show file tree
Hide file tree
Showing 29 changed files with 706 additions and 99 deletions.
8 changes: 4 additions & 4 deletions book.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[book]
title = "Koaky (Yet Another Programming Language)"
title = "Leviator (Yet Another Programming Language)"
authors = ["@Saverio976", "@TTENSHII", "@guillaumeAbel"]
description = "Documentation of Koaky"
description = "Documentation of Leviator"
src = "docs"

[build]
Expand All @@ -10,8 +10,8 @@ create-missing = false
use-default-preprocessors = true

[output.html]
git-repository-url = "https://github.com/X-R-G-B/koak"
edit-url-template = "https://github.com/X-R-G-B/koak/edit/main/{path}"
git-repository-url = "https://github.com/X-R-G-B/Leviator"
edit-url-template = "https://github.com/X-R-G-B/Leviator/edit/main/{path}"

[output.html.search]
enable = true
Expand Down
3 changes: 2 additions & 1 deletion docs/BNF.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<parameter> ::= <identifier> ": " <type>
<type> ::= <upperLetter> <char2>
<instruction> ::= <instructionIns> ";\n"
<instructionIns> ::= <declaration> | <assignment> | <functionCall> | <return> | <condition>
<instructionIns> ::= <declaration> | <assignment> | <functionCall> | <return> | <condition> | <while>
<declaration> ::= "@" <type> " " <identifier> " = " <value>
<assignment> ::= <identifier> " = " <value>
<functionCall> ::= <identifier> "(" <varParamList>* ")"
Expand All @@ -39,6 +39,7 @@
<conditionIfElse> ::= <conditionIf> <conditionElse>
<conditionIf> ::= "if (" <value> ")\n{\n" <instruction>* "}\n"
<conditionElse> ::= "else\n{\n" <instruction>* "}\n"
<while> ::= "while (" <value> ")\n{\n" <instruction>* "}\n"
<character> ::= "'" <char1> "'"
<bool> ::= "True" | "False"
Expand Down
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
18 changes: 13 additions & 5 deletions lvtc/app/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import WatLikeToWat (watsLikeToWat)
import Wasm (Wasm)
import WatToWasm (watToWasm)
import WriteWasm (writeWasm)
import TypeCheck (typeCheck)
import Args

import System.Directory (listDirectory)
Expand Down Expand Up @@ -67,6 +68,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 All @@ -83,9 +91,9 @@ getAllFunc v (x : expressions) = p v >> getAllFunc v expressions
checkAst :: Bool -> IO [FuncDeclaration] -> IO [FuncDeclaration]
checkAst _ funcsIo =
funcsIo
>>= (\funcs -> case Just funcs of
Just f -> return f
Nothing -> fail "Invalid Code")
>>= (\funcs -> case typeCheck funcs of
True -> return funcs
False -> fail "Invalid Code")

transformToWatLike :: Bool -> IO [FuncDeclaration] -> IO [FuncDeclare]
transformToWatLike v funcsIo =
Expand Down Expand Up @@ -119,10 +127,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
2 changes: 2 additions & 0 deletions lvtc/lvtc.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ library
Parser
ParseUtil
ShuntingYard
TypeCheck
Wasm
WasmUtils
WatAST
Expand Down Expand Up @@ -82,6 +83,7 @@ test-suite lvtc-test
UTLexeme
UTParseLvt
UTShuntingYard
UTTypeCheck
UTWasm
UTWat
UTWatLike
Expand Down
16 changes: 11 additions & 5 deletions lvtc/src/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ module AST
( Type
, Value (..)
, Var
, Symbol
, FuncCall
, FuncPrototype
, FuncDeclaration
, Instruction (..)
, VarDeclaration
, VarAssignation
, Condition
, Symbol
, IsFuncExport
) where
, Export
, WhileBlock
) where

import Data.Int (Int32)

Expand Down Expand Up @@ -58,9 +59,9 @@ instance Eq Value where

type Var = (Symbol, Type)

type IsFuncExport = Bool
type Export = Bool

type FuncPrototype = (IsFuncExport, Symbol, [Var], Type)
type FuncPrototype = (Export, Symbol, [Var], Type)

type FuncDeclaration = (FuncPrototype, [Instruction])

Expand All @@ -77,12 +78,15 @@ type VarDeclaration = (Var, Value)

type VarAssignation = (Symbol, Value)

type WhileBlock = (Value, [Instruction])

data Instruction =
Function FuncCall
| Return Value
| Declaration VarDeclaration
| Assignation VarAssignation
| Cond Condition
| While WhileBlock

instance Show Instruction where
show (Function x) =
Expand All @@ -95,6 +99,8 @@ instance Show Instruction where
"Assignation[< " ++ show x ++ " >]"
show (Cond x) =
"Cond[< " ++ show x ++ " >]"
show (While x) =
"While[< " ++ show x ++ " >]"

instance Eq Instruction where
(==) (Function x) (Function y) = x == y
Expand Down
3 changes: 3 additions & 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 Expand Up @@ -66,6 +67,8 @@ lexeme 0 ('i':'f':' ':xs) = lexeme 0 ('i':'f':xs)
lexeme 0 (' ':'i':'f':xs) = lexeme 0 ('i':'f':xs)
lexeme 0 ('e':'l':'s':'e':' ':xs) = lexeme 0 ('e':'l':'s':'e':xs)
lexeme 0 (' ':'e':'l':'s':'e':xs) = lexeme 0 ('e':'l':'s':'e':xs)
lexeme 0 ('w':'h':'i':'l':'e':' ':xs) = lexeme 0 ('w':'h':'i':'l':'e':xs)
lexeme 0 (' ':'w':'h':'i':'l':'e':xs) = lexeme 0 ('w':'h':'i':'l':'e':xs)
lexeme 0 ('\\':x:xs) = x : lexeme 0 xs
lexeme 1 ('\\':x:xs) = x : lexeme 1 xs
lexeme 0 (' ':' ':xs) = lexeme 0 (' ':xs)
Expand Down
17 changes: 14 additions & 3 deletions lvtc/src/ParseLvt.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module ParseLvt
parseDeclaration,
parseAssignation,
parseCond,
parseWhile,
-- Function
parseFuncDeclaration
) where
Expand Down Expand Up @@ -94,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 Expand Up @@ -269,9 +270,19 @@ parseCond = Parser f
Nothing -> Nothing
Just (ifBlock, ys) -> runParser (parseCond' val ifBlock) ys

parseWhileComp :: Parser Value
parseWhileComp = parseString "while(" *> parseValue <* parseString ")"

parseWhileBlock :: Parser [Instruction]
parseWhileBlock = parseString "{" *> parseInstructions <* parseString "}"

parseWhile :: Parser Instruction
parseWhile = While <$> ((,) <$> parseWhileComp <*> parseWhileBlock)

parseInstruction :: Parser Instruction
parseInstruction =
(parseCond
<|> parseWhile
<|> parseReturn
<|> parseDeclaration
<|> parseAssignation
Expand Down Expand Up @@ -299,7 +310,7 @@ parseFuncVars =
<|> parseFuncVar)
<* parseChar ')'

parseFuncName :: Parser (IsFuncExport, Symbol)
parseFuncName :: Parser (Export, Symbol)
parseFuncName =
((\x -> (True, x)) <$> (parseString "export fn " *> parseVarName))
<|> ((\x -> (False, x)) <$> (parseString "fn " *> parseVarName))
Expand Down
Loading

0 comments on commit 920bc41

Please sign in to comment.