Skip to content

Commit

Permalink
style: remove coding style error in Compiler.hs
Browse files Browse the repository at this point in the history
  • Loading branch information
AxelHumeau committed Feb 11, 2024
1 parent 9815508 commit f8c9811
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 111 deletions.
49 changes: 15 additions & 34 deletions LobsterLang/src/AstOptimizer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

module AstOptimizer
( optimizeAst,
fromOptimised,
fromOpti,
AstError (..),
AstOptimised (..),
)
Expand Down Expand Up @@ -51,23 +51,15 @@ optimizeAst stack ((List asts) : xs) inF =
case sequence (optimizeAst stack asts inF) of
Left err -> Left err : optimizeAst stack xs inF
Right opAst ->
Right (Result (List (map fromOptimised opAst)))
Right (Result (List (map fromOpti opAst)))
: optimizeAst stack xs inF
optimizeAst stack ((Define n ast) : xs) inF =
checkOptiAfterDef stack (optimizeAst stack [ast] inF) n ast xs inF
optimizeAst stack ((Symbol s Nothing) : xs) inF
| inF =
Right (Result (Symbol s Nothing)) : optimizeAst stack xs inF
| inF = Right (Result (Symbol s Nothing)) : optimizeAst stack xs inF
| otherwise = case getVarInScope stack s of
Nothing ->
Left
( Error
( "Symbol '"
++ s
++ "' doesn't exist in the current or global scope"
)
(Symbol s Nothing)
)
Nothing -> Left (Error ("Symbol '" ++ s ++
"' doesn't exist in the current or global scope") (Symbol s Nothing))
: optimizeAst stack xs inF
Just _ -> Right (Result (Symbol s Nothing)) : optimizeAst stack xs inF
optimizeAst stack ((Symbol s (Just asts)) : xs) inF
Expand All @@ -76,11 +68,8 @@ optimizeAst stack ((Symbol s (Just asts)) : xs) inF
(evalAst stack (Symbol s (Just asts))) inF
| otherwise = case sequence (optimizeAst stack asts inF) of
Left err -> Left err : optimizeAst stack xs inF
Right opAst ->
optimizeAst
stack
(Symbol s (Just (map fromOptimised opAst)) : xs)
inF
Right opAst -> optimizeAst stack
(Symbol s (Just (map fromOpti opAst)) : xs) inF
optimizeAst stack ((Call op asts) : xs) inF
| foldr ((&&) . isUnoptimizable) True asts
&& foldr ((&&) . isValue) True asts =
Expand All @@ -90,11 +79,7 @@ optimizeAst stack ((Call op asts) : xs) inF
(evalAst stack (Call op asts)) inF
| otherwise = case sequence (optimizeAst stack asts inF) of
Left err -> Left err : optimizeAst stack xs inF
Right asts' ->
optimizeAst
stack
(Call op (map fromOptimised asts') : xs)
inF
Right asts' -> optimizeAst stack (Call op (map fromOpti asts') : xs) inF
optimizeAst stack ((Cond condAst trueAst mFalseAst) : xs) inF
| not (isUnoptimizable condAst) = case optimizeAst stack [condAst] inF of
[Left err] -> Left err : optimizeAst stack xs inF
Expand Down Expand Up @@ -123,11 +108,8 @@ optimizeAst stack ((Cond condAst trueAst mFalseAst) : xs) inF
Right (Warning "Condition is always true" trueAst)
: optimizeAst stack xs inF
Boolean False ->
Right
( Warning
"Condition is always false"
(fromMaybe (Cond condAst trueAst mFalseAst) mFalseAst)
)
Right (Warning "Condition is always false"
(fromMaybe (Cond condAst trueAst mFalseAst) mFalseAst))
: optimizeAst stack xs inF
_ ->
Right (Result (Cond condAst trueAst mFalseAst))
Expand All @@ -139,8 +121,7 @@ optimizeAst stack (FunctionValue params ast Nothing : xs) inF =
Right (Result (FunctionValue params ast' Nothing))
: optimizeAst stack xs inF
[Right (Warning mes ast')] ->
Right
(Warning mes (FunctionValue params ast' Nothing))
Right (Warning mes (FunctionValue params ast' Nothing))
: optimizeAst stack xs inF
_ -> shouldntHappen stack (FunctionValue params ast Nothing : xs) inF
optimizeAst stack (FunctionValue params ast (Just asts) : xs) inF
Expand All @@ -161,7 +142,7 @@ optimizeAst stack (FunctionValue params ast (Just asts) : xs) inF
Right asts' ->
optimizeAst
stack
(FunctionValue params ast (Just (map fromOptimised asts')) : xs)
(FunctionValue params ast (Just (map fromOpti asts')) : xs)
inF
| length params > length asts =
case evalAst stack (FunctionValue params ast (Just asts)) of
Expand Down Expand Up @@ -221,9 +202,9 @@ isValue (FunctionValue _ _ Nothing) = True
isValue _ = False

-- | Get the `Ast` contained in a `AstOptimised`
fromOptimised :: AstOptimised -> Ast
fromOptimised (Warning _ ast) = ast
fromOptimised (Result ast) = ast
fromOpti :: AstOptimised -> Ast
fromOpti (Warning _ ast) = ast
fromOpti (Result ast) = ast

-- | Handle cases where the optimization depends on
-- the result of a evaluation of the `Ast` and it have to return evaluated
Expand Down
110 changes: 51 additions & 59 deletions LobsterLang/src/Compiler.hs
Original file line number Diff line number Diff line change
Expand Up @@ -233,25 +233,15 @@ astToInstructions (Define symbolName value) =
let symbolValue = astToInstructions value
in [Def symbolName 1 symbolValue]
astToInstructions (FunctionValue argsNames funcBody Nothing) =
[ Fnv
(length argsNames)
argsNames
nbFuncBodyInstructions
funcBodyInstructions
[]
Nothing ]
[Fnv (length argsNames) argsNames nbFuncBodyInstructions
funcBodyInstructions [] Nothing]
where
nbFuncBodyInstructions = _findAstInstrSize [funcBody]
funcBodyInstructions =
_resolveFunctionPushArgs (astToInstructions funcBody ++ [Ret]) argsNames
astToInstructions (FunctionValue argsNames funcBody (Just argsValues)) =
[ Fnv
(length argsNames)
argsNames
nbFuncBodyInstructions
funcBodyInstructions
nbArgsValuesInstructions
argsValuesInstructions ]
[Fnv (length argsNames) argsNames nbFuncBodyInstructions
funcBodyInstructions nbArgsValuesInstructions argsValuesInstructions]
where
nbFuncBodyInstructions = _findAstInstrSize [funcBody]
funcBodyInstructions =
Expand All @@ -260,23 +250,17 @@ astToInstructions (FunctionValue argsNames funcBody (Just argsValues)) =
Just (map astToInstructions argsValues)
nbArgsValuesInstructions = _instructionListLengths argsValuesInstructions
astToInstructions (AST.Cond cond trueBlock (Just falseBlock)) =
[ Compiler.Cond
condInstructions
nbTrueBlockInstructions
trueBlockInstructions
(Just falseBlockInstructions) ]
[Compiler.Cond condInstructions nbTrueBlockInstructions
trueBlockInstructions (Just falseBlockInstructions)]
where
condInstructions = astToInstructions cond
falseBlockInstructions = astToInstructions falseBlock
trueBlockInstructions =
astToInstructions trueBlock ++ [Jump (_findAstInstrSize [falseBlock] + 1)]
trueBlockInstructions = astToInstructions trueBlock ++
[Jump (_findAstInstrSize [falseBlock] + 1)]
nbTrueBlockInstructions = _findAstInstrSize [trueBlock] + 1
astToInstructions (AST.Cond cond trueBlock Nothing) =
[ Compiler.Cond
condInstructions
nbTrueBlockInstructions
trueBlockInstructions
Nothing ]
[Compiler.Cond condInstructions nbTrueBlockInstructions
trueBlockInstructions Nothing]
where
condInstructions = astToInstructions cond
trueBlockInstructions =
Expand Down Expand Up @@ -363,15 +347,11 @@ _showInstruction (Def symbolName nbInstruction instructions) depth =
_showInstruction (Fnv nbArgsNames argsNames nbFuncBodyInstructions
funcBodyInstructions nbArgsValuesInstructions
(Just argsValuesInstructions)) depth =
concat (replicate depth "\t") ++
"FNV " ++
"(" ++ show nbArgsNames ++ ")" ++
show argsNames ++
" (" ++ show nbArgsValuesInstructions ++ ")" ++
concat (replicate depth "\t") ++ "FNV " ++ "(" ++ show nbArgsNames ++ ")"
++ show argsNames ++ " (" ++ show nbArgsValuesInstructions ++ ")" ++
"(\n" ++ _showInstructionList argsValuesInstructions (depth + 1) ++ ")" ++
" = (" ++ show nbFuncBodyInstructions ++
"){\n" ++ _showInstructions funcBodyInstructions (depth + 1) ++ "}\n"

_showInstruction (Fnv nbArgsNames argsNames nbFuncBodyInstructions
funcBodyInstructions _ Nothing) depth =
concat (replicate depth "\t") ++
Expand All @@ -382,10 +362,8 @@ _showInstruction (Fnv nbArgsNames argsNames nbFuncBodyInstructions
"){\n" ++ _showInstructions funcBodyInstructions (depth + 1) ++ "}\n"
_showInstruction (Compiler.Cond condInstructions
nbTrueBlockInstructions trueBlockInstructions
(Just falseBlockInstructions)) depth =
concat (replicate depth "\t") ++
"COND " ++
"(" ++ show (length condInstructions) ++ ")" ++
(Just falseBlockInstructions)) depth = concat (replicate depth "\t") ++
"COND " ++ "(" ++ show (length condInstructions) ++ ")" ++
"(\n" ++ _showInstructions condInstructions (depth + 1) ++
_showInstruction (JumpIfFalse nbTrueBlockInstructions) 0 ++ ")" ++
" true: (" ++ show nbTrueBlockInstructions ++
Expand Down Expand Up @@ -420,7 +398,8 @@ _resolveFunctionPushArgs [PushSym symbolName Nothing] argsNames =
_resolveFunctionPushArgs [PushSym symbolName (Just args)] argsNames =
case Data.List.elemIndex symbolName argsNames of
Just value -> [PushArg value]
Nothing -> [PushSym symbolName (Just (fmap (`_resolveFunctionPushArgs` argsNames) args))]
Nothing -> [PushSym symbolName
(Just (fmap (`_resolveFunctionPushArgs` argsNames) args))]
_resolveFunctionPushArgs [Compiler.Cond condInstructions
nbTrueBlockInstructions trueBlockInstructions
(Just falseBlockInstructions)] argsNames =
Expand All @@ -447,18 +426,33 @@ _resolveFunctionPushArgs (instruction:instructions) argsNames

_findAstInstrSize :: [Ast] -> Int
_findAstInstrSize [] = 0
_findAstInstrSize (Value _:xs) = 1 + _findAstInstrSize xs
_findAstInstrSize (Boolean _:xs) = 1 + _findAstInstrSize xs
_findAstInstrSize (String _:xs) = 1 + _findAstInstrSize xs
_findAstInstrSize (Define _ ast:xs) = 1 + _findAstInstrSize [ast] + _findAstInstrSize xs
_findAstInstrSize (List asts:xs) = 1 + _findAstInstrSize asts + _findAstInstrSize xs
_findAstInstrSize (Symbol _ Nothing:xs) = 1 + _findAstInstrSize xs
_findAstInstrSize (Symbol _ (Just asts):xs) = _findAstInstrSize asts + 4 + _findAstInstrSize xs-- push nbGivenArgs, pushSym, Call
_findAstInstrSize (AST.Call _ asts:xs) = _findAstInstrSize asts + 1 + _findAstInstrSize xs
_findAstInstrSize (FunctionValue _ ast Nothing:xs) = _findAstInstrSize [ast] + 2 + _findAstInstrSize xs
_findAstInstrSize (FunctionValue _ ast (Just asts):xs) = _findAstInstrSize asts + 1 + _findAstInstrSize [ast] + 3 + _findAstInstrSize xs
_findAstInstrSize (AST.Cond astCond astTrue Nothing:xs) = _findAstInstrSize [astCond] + 1 + _findAstInstrSize [astTrue] + _findAstInstrSize xs
_findAstInstrSize (AST.Cond astCond astTrue (Just astFalse):xs) = _findAstInstrSize [astCond] + 1 + _findAstInstrSize [astTrue] + 1 + _findAstInstrSize [astFalse] + _findAstInstrSize xs
_findAstInstrSize (Value _:xs) =
1 + _findAstInstrSize xs
_findAstInstrSize (Boolean _:xs) =
1 + _findAstInstrSize xs
_findAstInstrSize (String _:xs) =
1 + _findAstInstrSize xs
_findAstInstrSize (Define _ ast:xs) =
1 + _findAstInstrSize [ast] + _findAstInstrSize xs
_findAstInstrSize (List asts:xs) =
1 + _findAstInstrSize asts + _findAstInstrSize xs
_findAstInstrSize (Symbol _ Nothing:xs) =
1 + _findAstInstrSize xs
_findAstInstrSize (Symbol _ (Just asts):xs) =
_findAstInstrSize asts + 4 + _findAstInstrSize xs-- push nbGivenArgs, pushSym, Call
_findAstInstrSize (AST.Call _ asts:xs) =
_findAstInstrSize asts + 1 + _findAstInstrSize xs
_findAstInstrSize (FunctionValue _ ast Nothing:xs) =
_findAstInstrSize [ast] + 2 + _findAstInstrSize xs
_findAstInstrSize (FunctionValue _ ast (Just asts):xs) =
_findAstInstrSize asts + 1 + _findAstInstrSize [ast] + 3 +
_findAstInstrSize xs
_findAstInstrSize (AST.Cond astCond astTrue Nothing:xs) =
_findAstInstrSize [astCond] + 1 + _findAstInstrSize [astTrue] +
_findAstInstrSize xs
_findAstInstrSize (AST.Cond astCond astTrue (Just astFalse):xs) =
_findAstInstrSize [astCond] + 1 + _findAstInstrSize [astTrue] + 1 +
_findAstInstrSize [astFalse] + _findAstInstrSize xs

_instructionListLengths :: Maybe [[Instruction]] -> [Int]
_instructionListLengths (Just []) = [0]
Expand Down Expand Up @@ -589,17 +583,14 @@ _compileInstruction (Def symbolName nbInstruction instructions)
>> _putInt32 nbInstruction
>> compileInstructions instructions
-- Fnv
_compileInstruction (Fnv nbArgsNames argsNames nbFuncBodyInstructions
_compileInstruction (Fnv nbArgsNames argsNames nbFnBodyInsts
funcBodyInstructions nbArgsValuesInstructions
(Just argsValuesInstructions)) =
_fputList compileInstructions argsValuesInstructions
>> _putOpCodeFromInstruction (PushI (length argsValuesInstructions))
>> _putInt32 (length argsValuesInstructions)
(Just argsValuesInsts)) = _fputList compileInstructions argsValuesInsts
>> _putOpCodeFromInstruction (PushI (length argsValuesInsts))
>> _putInt32 (length argsValuesInsts)
>> _putOpCodeFromInstruction (Fnv nbArgsNames argsNames
nbFuncBodyInstructions funcBodyInstructions nbArgsValuesInstructions
(Just argsValuesInstructions))
>> _putInt32 nbArgsNames
>> _putInt32 nbFuncBodyInstructions
nbFnBodyInsts funcBodyInstructions nbArgsValuesInstructions
(Just argsValuesInsts)) >> _putInt32 nbArgsNames >> _putInt32 nbFnBodyInsts
>> _fputList _compileInstruction funcBodyInstructions
>> _putOpCodeFromInstruction Compiler.Call
_compileInstruction (Fnv nbArgsNames argsNames nbFuncBodyInstructions
Expand Down Expand Up @@ -645,4 +636,5 @@ compile ast filepath showInst = if showInst
else writeCompiledInstructionsToFile filepath compiledInstructions
where
instructions = concatMap astToInstructions ast ++ [Ret]
compiledInstructions = _putInt32 (fromEnum MagicNumber) >> _fputList _compileInstruction instructions
compiledInstructions = _putInt32 (fromEnum MagicNumber) >>
_fputList _compileInstruction instructions
12 changes: 6 additions & 6 deletions extension/language-configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@
},
// symbols used as brackets
"brackets": [
["{", "}"],
["[", "]"],
["{|", "|}"],
["[|", "|]"],
["(|", "|)"]
],
// symbols that are auto closed when typing
"autoClosingPairs": [
["{", "}"],
["[", "]"],
["{|", "|}"],
["[|", "|]"],
["(|", "|)"],
["\"", "\""],
["'", "'"]
],
// symbols that can be used to surround a selection
"surroundingPairs": [
["{", "}"],
["[", "]"],
["{|", "|}"],
["[|", "|]"],
["(|", "|)"],
["\"", "\""],
["'", "'"]
Expand Down
24 changes: 12 additions & 12 deletions extension/snippets/lobsterlang.code-snippets.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,43 @@
"function": {
"prefix": ["fn", "function"],
"body": [
"fn $1(| $2 |) {\n\t$3\n};"
"fn $1(| $2 |) {|\n\t$3\n|};"
],
"description": "Create a lobster function (fn ...(| ... |) {...};)"
"description": "Create a lobster function (fn ...(| ... |) {|...|};)"
},
"lambda": {
"prefix": ["lambda"],
"body": [
"$1 = lambda (| $2 |) {$3};"
"$1 = lambda (| $2 |) {|$3|};"
],
"description": "Create a lobster lambda (... = lambda (| ... |) {...};)"
"description": "Create a lobster lambda (... = lambda (| ... |) {|...|};)"
},
"lambda (λ)": {
"prefix": ["lambda", "λ"],
"body": [
"$1 = λ (| $2 |) {$3};"
"$1 = λ (| $2 |) {|$3|};"
],
"description": "Create a lobster lambda (... = λ (| ... |) {...};)"
"description": "Create a lobster lambda (... = λ (| ... |) {|...|};)"
},
"if": {
"prefix": ["if"],
"body": [
"if $1 {\n\t$2\n}"
"if $1 {|\n\t$2\n|}"
],
"description": "Create a if statement block (if ... { ... })"
"description": "Create a if statement block (if ... {| ... |})"
},
"if else": {
"prefix": ["if else"],
"body": [
"if $1 {\n\t$2\n} else $3"
"if $1 {|\n\t$2\n|} else $3"
],
"description": "Create a if-else statement block (if ... { ... } else ...)"
"description": "Create a if-else statement block (if ... {| ... |} else ...)"
},
"while": {
"prefix": ["while"],
"body": [
"while $1 {\n\t$2\n}"
"while $1 {|\n\t$2\n|}"
],
"description": "Create a while loop (while ... { ... })"
"description": "Create a while loop (while ... {| ... |})"
}
}

0 comments on commit f8c9811

Please sign in to comment.