Skip to content

Commit

Permalink
Add typecheck
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumeAbel committed Jan 14, 2024
2 parents a0f771e + 2f27dc8 commit ce1d910
Show file tree
Hide file tree
Showing 57 changed files with 2,230 additions and 87 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/documentation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ jobs:
if: steps.filter.outputs.docs == 'true' || steps.filter.outputs.docs2 == 'true' || steps.filter.outputs.workflow == 'true' || github.ref == 'refs/heads/main'
run: mdbook build

- name: Copy OnlineRunner
if: steps.filter.outputs.docs == 'true' || steps.filter.outputs.docs2 == 'true' || steps.filter.outputs.workflow == 'true' || github.ref == 'refs/heads/main'
run: cp ./lvtext/webrunner/index.html ./book/OnlineVM.html

- name: Setup Pages
if: github.ref == 'refs/heads/main'
uses: actions/configure-pages@v3
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export fn start() -> Int

- **Operators**

```
```python
a + b
a - b
a * b
Expand All @@ -138,3 +138,10 @@ a <= b
a > b
a >= b
```

- **Priority of Operators**

```c
// realy peticuliar buut we use { for ( and } for )
{a + B} * c
```
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
Empty file added docs/OnlineVM.md
Empty file.
1 change: 1 addition & 0 deletions docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ made in Haskell.
[Byte Code Spec Ex](ByteCodeSpecEx.md)
[Syntax Highlighting Extension](SyntaxHighlighting.md)
[BNF](BNF.md)
[Online VM](OnlineVM.md)
6 changes: 6 additions & 0 deletions lvtc/src/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module AST
, VarAssignation
, Condition
, Export
, WhileBlock
) where

import Data.Int (Int32)
Expand Down Expand Up @@ -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
12 changes: 6 additions & 6 deletions lvtc/src/Builtins.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ getBuiltinsFuncOperator =
getBuiltinsFuncComp :: [FuncDeclaration]
getBuiltinsFuncComp =
[
((False, "==", [("x", "Int"), ("y", "Int")], "Int"), []),
((False, "<", [("x", "Int"), ("y", "Int")], "Int"), []),
((False, ">", [("x", "Int"), ("y", "Int")], "Int"), []),
((False, "<=", [("x", "Int"), ("y", "Int")], "Int"), []),
((False, ">=", [("x", "Int"), ("y", "Int")], "Int"), []),
((False, "!=", [("x", "Int"), ("y", "Int")], "Int"), [])
((False, "==", [("x", "Int"), ("y", "Int")], "Bool"), []),
((False, "<", [("x", "Int"), ("y", "Int")], "Bool"), []),
((False, ">", [("x", "Int"), ("y", "Int")], "Bool"), []),
((False, "<=", [("x", "Int"), ("y", "Int")], "Bool"), []),
((False, ">=", [("x", "Int"), ("y", "Int")], "Bool"), []),
((False, "!=", [("x", "Int"), ("y", "Int")], "Bool"), [])
]

getBuiltinsFunc :: [FuncDeclaration]
Expand Down
2 changes: 2 additions & 0 deletions lvtc/src/Lexeme.hs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,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
13 changes: 12 additions & 1 deletion 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 @@ -93,7 +94,7 @@ parseOperatorOp :: Parser Value
parseOperatorOp =
Var
<$> (parseString "+" <|> parseString "-" <|> parseString "*"
<|> parseString "/" <|> parseString "(" <|> parseString ")"
<|> parseString "/" <|> parseString "{" <|> parseString "}"
<|> parseString "==" <|> parseString "!=" <|> parseString "<"
<|> parseString ">" <|> parseString "<=" <|> parseString ">=")

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
12 changes: 6 additions & 6 deletions lvtc/src/ShuntingYard.hs
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ opOnStack (Var op1) (SYS ((Var op2):ops) out)
opOnStack _ sys = sys

shuntingYardOp :: Value -> ShuntingYardState -> ShuntingYardState
shuntingYardOp (Var "(") (SYS ops out) =
SYS (Var "(" : ops) out
shuntingYardOp (Var ")") (SYS [] _) =
shuntingYardOp (Var "{") (SYS ops out) =
SYS (Var "{" : ops) out
shuntingYardOp (Var "}") (SYS [] _) =
SYS [] []
shuntingYardOp (Var ")") (SYS ((Var "("):ops) out) =
shuntingYardOp (Var "}") (SYS ((Var "{"):ops) out) =
SYS ops out
shuntingYardOp (Var ")") (SYS (op:ops) out) =
shuntingYardOp (Var ")") (SYS ops (out ++ [op]))
shuntingYardOp (Var "}") (SYS (op:ops) out) =
shuntingYardOp (Var "}") (SYS ops (out ++ [op]))
shuntingYardOp (Var op) sys =
SYS (Var op:ops') out'
where
Expand Down
14 changes: 13 additions & 1 deletion lvtc/src/TypeCheck.hs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ checkCondition (v, fst_, snd_) env xs
checkInstructions snd_ env = checkInstructions xs env
checkCondition _ _ _ = False

checkWhile :: WhileBlock -> Env -> [Instruction] -> Bool
checkWhile (v, instructions) env _ | assertTypeAndValue "Bool" v env =
checkInstructions instructions env
checkWhile _ _ _ = False

checkInstructions :: [Instruction] -> Env -> Bool
checkInstructions [] _ = True
checkInstructions ((Function func):xs) env = checkCall func env xs
Expand All @@ -147,6 +152,7 @@ checkInstructions ((Declaration declaration):xs) env =
checkInstructions ((Assignation assignation):xs) env =
checkAssignation assignation env xs
checkInstructions ((Cond condition):xs) env = checkCondition condition env xs
checkInstructions ((While while):xs) env = checkWhile while env xs

checkVarTypes :: [Var] -> Bool
checkVarTypes [] = True
Expand Down Expand Up @@ -203,5 +209,11 @@ defaultEnv :: Maybe [FuncPrototype]
defaultEnv = Just (createCalcOp ["+", "-", "*", "%", "/"] ++
createCompOp ["==", "!=", "<", ">", "<=", ">="])

checkStart :: [FuncDeclaration] -> Bool
checkStart (((True, "start", _, _), _):_) = True
checkStart (((_, _, _, _), _):xs) = checkStart xs
checkStart [] = False

typeCheck :: [FuncDeclaration] -> Bool
typeCheck expressions = checkDeclarations expressions defaultEnv
typeCheck expressions | checkStart expressions = checkDeclarations expressions defaultEnv
typeCheck _ = False
15 changes: 12 additions & 3 deletions lvtc/src/WasmUtils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ module WasmUtils
--
opCodeByte,
variableTypeByte,
exportSectionExportTypeByte
exportSectionExportTypeByte,
ifTypeByte
) where

import Wasm
import WatAST (OpCode (..))
import WatAST (OpCode (..), IfType (..))
import Leb128Encode

getDefaultTypeSectionType :: TypeSectionType
Expand Down Expand Up @@ -179,6 +180,9 @@ getSizeOpCode (LocalGet _) = 2
getSizeOpCode (LocalSet _) = 2
getSizeOpCode (I32Const _) = 2
getSizeOpCode (Call _) = 2
getSizeOpCode (If _) = 2
getSizeOpCode (Loop _) = 2
getSizeOpCode (Br _) = 2
getSizeOpCode _ = 1

fillBlankCodeSectionCode :: CodeSectionCode -> CodeSectionCode
Expand Down Expand Up @@ -249,9 +253,14 @@ opCodeByte I32Mul = 0x6c
opCodeByte I32Div = 0x6d
opCodeByte Return = 0x0f
opCodeByte (Call _) = 0x10
opCodeByte If = 0x04
opCodeByte (If EmptyType) = 0x04
opCodeByte Else = 0x05
opCodeByte End = 0x0b
opCodeByte (Loop EmptyType) = 0x03
opCodeByte (Br _) = 0x0c

ifTypeByte :: IfType -> Int
ifTypeByte EmptyType = 0x40

variableTypeByte :: VariableType -> Int
variableTypeByte I32 = 0x7f
Expand Down
9 changes: 8 additions & 1 deletion lvtc/src/WatAST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ module WatAST
OpCode (..)
, Type (..)
, FuncDef (..)
, IfType (..)
) where

import Data.Int (Int32)

data IfType =
EmptyType
deriving (Show, Eq)

-- if opcode added, dont miss to add the right size in ./WasmUtils.hs
data OpCode =
LocalGet Int32
Expand All @@ -33,9 +38,11 @@ data OpCode =
| I32Div
| Return
| Call Int32
| If
| If IfType
| Else
| End
| Loop IfType
| Br Int32
deriving (Show, Eq)

data Type =
Expand Down
28 changes: 21 additions & 7 deletions lvtc/src/WatLike.hs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ modifyAll' x varsIndex funcsIndex = (x, varsIndex, funcsIndex)

---

transformType :: Type -> Type
transformType "Void" = "Int"
transformType "Char" = "Int"
transformType "Bool" = "Int"
transformType x = x

modifyAll :: [Instruction] -> [Index] -> [Index]
-> ([Instruction], [Index], [Index])
modifyAll [] varsIndex funcsIndex = ([], varsIndex, funcsIndex)
Expand Down Expand Up @@ -119,12 +125,13 @@ modifyAll ((Cond (vValue, insIf, insElse)):xs) vsInd fsInd =
(insElse', vsInd''', fsInd''') = modifyAll insElse vsInd'' fsInd''
newCond = Cond (vValue', insIf', insElse')
(ins', vsInd'''', fsInd'''') = modifyAll xs vsInd''' fsInd'''

transformType :: Type -> Type
transformType "Void" = "Int"
transformType "Char" = "Int"
transformType "Bool" = "Int"
transformType x = x
modifyAll ((While (vValue, ins)):xs) vsInd fsInd =
(newWhile:ins', vsInd''', fsInd''')
where
(vValue', vsInd', fsInd') = modifyAll' vValue vsInd fsInd
(insWhile, vsInd'', fsInd'') = modifyAll ins vsInd' fsInd'
newWhile = While (vValue', insWhile)
(ins', vsInd''', fsInd''') = modifyAll xs vsInd'' fsInd''

registerParams :: FuncDeclare -> FuncDeclare
registerParams (((isExp, fName, [], typ), ins), varsIndex, oName) =
Expand Down Expand Up @@ -237,7 +244,7 @@ instructionToWatLike
(varsIndex', ins' ++ [newDeclaration])
where
(varsIndex', ins', vValue') = valueToWatLike vValue oldFuncs varsIndex
newDeclaration = Declaration ((vName, vTyp), vValue')
newDeclaration = Declaration ((vName, transformType vTyp), vValue')
instructionToWatLike
(Assignation (vName, vValue)) oldFuncs varsIndex =
(varsIndex', ins' ++ [newAssignation])
Expand Down Expand Up @@ -266,6 +273,13 @@ instructionToWatLike
(vsInd''', vInsFalse') =
instructionsToWatLike vInsFalse oldFuncs vsInd''
newCond = Cond (vValCond', vInsTrue', vInsFalse')
instructionToWatLike
(While (vValCond, vIns)) oldFuncs vsInd =
(vsInd'', insCond ++ [newWhile])
where
(vsInd', insCond, vValCond') = valueToWatLike vValCond oldFuncs vsInd
(vsInd'', vIns') = instructionsToWatLike vIns oldFuncs vsInd'
newWhile = While (vValCond', vIns' ++ insCond)

instructionsToWatLike :: [Instruction] -> ([FuncDeclare], [Index])
-> [Index] -> ([Index], [Instruction])
Expand Down
Loading

0 comments on commit ce1d910

Please sign in to comment.