Skip to content

Commit

Permalink
Step 3rd parser lvt (#10)
Browse files Browse the repository at this point in the history
* Rename lib file

* Add parser for one lvt instruction

* Add ;\n for all instruction

* Add AST

* Fix file location

* Add header

* Add exports

* Fix TypeValue

* Fix norm + returnValue

* Fix

* Fix type

* Fix export

* Merge remote-tracking branch 'origin/step4TypeCheck' into step-3rd-parser-lvt

* Fix lvtrun files compil

* Fix condition for parser

* Fix norm

* Fix func call for value

* Add operation priority

* Fix

---------

Co-authored-by: guillaume abel <[email protected]>
  • Loading branch information
Saverio976 and guillaumeAbel authored Jan 6, 2024
1 parent 4e4f570 commit 5328948
Show file tree
Hide file tree
Showing 12 changed files with 729 additions and 11 deletions.
33 changes: 31 additions & 2 deletions lvtc/app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,31 @@

module Main (main) where

import Expression (parseAllExpression)
import Expression (parseExpresion, parseAllExpression)
import Parser (runParser)
import Alias (proceedAlias)
import ParseLvt (parseInstruction, parseInstructions)

test1 :: String
test1 = "if (a)\n{\nb(0);\n};\n"

test2 :: String
test2 = part1 ++ part2
where
part1 = "@Int a = 0;\n @Int c = b(a);\n"
part2 = " if (c)\n {\n d(a);\n };\n"

test3 :: String
test3 = "alias abc def;\n"

test4 :: String
test4 = "// this is a comment\n"

test5 :: String
test5 = "@Int a = 4 + 5;\n"

test6 :: String
test6 = "@Int a = 3 + 4 * 2 / ( 1 - 5 );\n"

text :: String
text = aliasInt ++ aliasRetValue ++ funcMain
Expand All @@ -19,4 +41,11 @@ text = aliasInt ++ aliasRetValue ++ funcMain
funcMain = "fn main () -> int \n{\n <- retValue;\n};\n"

main :: IO ()
main = print $ runParser (proceedAlias <$> parseAllExpression) text
main =
print (runParser parseInstruction test1)
>> print (runParser parseInstructions test2)
>> print (runParser parseExpresion test3)
>> print (runParser parseExpresion test4)
>> print (runParser parseInstruction test5)
>> print (runParser parseInstruction test6)
>> print (runParser (proceedAlias <$> parseAllExpression) text)
5 changes: 5 additions & 0 deletions lvtc/lvtc.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ source-repository head
library
exposed-modules:
Alias
AST
Expression
Lib
ParseLvt
Parser
ParseUtil
ShuntingYard
other-modules:
Paths_lvtc
autogen-modules:
Expand Down Expand Up @@ -59,6 +62,8 @@ test-suite lvtc-test
type: exitcode-stdio-1.0
main-is: Spec.hs
other-modules:
UTParseLvt
UTShuntingYard
Paths_lvtc
autogen-modules:
Paths_lvtc
Expand Down
102 changes: 102 additions & 0 deletions lvtc/src/AST.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
{-
-- EPITECH PROJECT, 2023
-- Leviator Run
-- File description:
-- AST
-}

module AST
( Type
, Value (..)
, Var
, FuncCall
, FuncPrototype
, FuncDeclaration
, Instruction (..)
, VarDeclaration
, VarAssignation
, Condition
, Symbol
) where

import Data.Int (Int32)

type Symbol = String

type Type = String

data Value =
Var String
| FuncValue FuncCall
| Boolean Bool
| Integer Int32
| Character Char
| StringView String
| Void

instance Show Value where
show (Var x) = "V< " ++ show x ++ " >"
show (FuncValue x) = "F< " ++ show x ++ " >"
show (Boolean x) = "B< " ++ show x ++ " >"
show (Integer x) = "I< " ++ show x ++ " >"
show (Character x) = "C< " ++ show x ++ " >"
show (StringView x) = "SV< " ++ show x ++ " >"
show Void = "Void"

instance Eq Value where
(==) (Var x) (Var y) = x == y
(==) (FuncValue x) (FuncValue y) = x == y
(==) (Boolean x) (Boolean y) = x == y
(==) (Integer x) (Integer y) = x == y
(==) (Character x) (Character y) = x == y
(==) (StringView x) (StringView y) = x == y
(==) Void Void = True
(==) _ _ = False

-- Function

type Var = (Symbol, Type)

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

type FuncDeclaration = (FuncPrototype, [Instruction])


-- condition

type Condition = (Value, [Instruction], [Instruction])

-- Instruction

type FuncCall = (Symbol, [Value])

type VarDeclaration = (Var, Value)

type VarAssignation = (Symbol, Value)

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

instance Show Instruction where
show (Function x) =
"Function[< " ++ show x ++ " >]"
show (Return x) =
"Return[< " ++ show x ++ " >]"
show (Declaration x) =
"Declaration[< " ++ show x ++ " >]"
show (Assignation x) =
"Assignation[< " ++ show x ++ " >]"
show (Cond x) =
"Cond[< " ++ show x ++ " >]"

instance Eq Instruction where
(==) (Function x) (Function y) = x == y
(==) (Return x) (Return y) = x == y
(==) (Declaration x) (Declaration y) = x == y
(==) (Assignation x) (Assignation y) = x == y
(==) (Cond x) (Cond y) = x == y
(==) _ _ = False
20 changes: 13 additions & 7 deletions lvtc/src/Expression.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ countBracketsForFunction n ('\\':_:xs) = countBracketsForFunction n xs
countBracketsForFunction n (_:xs) = countBracketsForFunction n xs

parseFunction' :: Parser Expression
parseFunction' = (\x -> Function (x ++ "\n};\n")) <$> ((++) <$>
(parseString "fn " <|> parseString "export fn") <*>
parseAllCharUntil "\n};\n")
parseFunction' =
(\x -> Function (x ++ "\n};\n"))
<$> ((++)
<$> (parseString "fn " <|> parseString "export fn")
<*> parseAllCharUntil "\n};\n")

parseFunction :: Parser Expression
parseFunction = Parser f
Expand All @@ -55,13 +57,17 @@ parseFunction = Parser f

parseAlias :: Parser Expression
parseAlias =
(\x -> Alias (x ++ ";\n")) <$>
((++) <$> parseString "alias " <*> parseAllCharUntil ";\n")
(\x -> Alias (x ++ ";\n"))
<$> ((++)
<$> parseString "alias "
<*> parseAllCharUntil ";\n")

parseComment :: Parser Expression
parseComment =
(\x -> Comment (x ++ "\n")) <$>
((++) <$> parseString "//" <*> parseAllCharUntil "\n")
(\x -> Comment (x ++ "\n"))
<$> ((++)
<$> parseString "//"
<*> parseAllCharUntil "\n")

parseExpresion :: Parser Expression
parseExpresion = parseAlias <|> parseFunction <|> parseComment
Expand Down
Loading

0 comments on commit 5328948

Please sign in to comment.