Skip to content

Commit

Permalink
Merge pull request #40 from AxelHumeau/refactor/remove-SExpr-to-AST
Browse files Browse the repository at this point in the history
Refactor/remove s expr to ast
  • Loading branch information
tzhengtek authored Jan 11, 2024
2 parents 785f990 + 6d02a58 commit 807c1de
Show file tree
Hide file tree
Showing 9 changed files with 480 additions and 228 deletions.
50 changes: 29 additions & 21 deletions LobsterLang/app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,58 @@ import Scope
import System.IO (isEOF)
import System.Exit (exitWith, ExitCode (ExitFailure))
import System.Environment (getArgs)
import qualified AstEval
import Control.Exception
import SExpr (SExpr)
import qualified AST
-- import Compiler

-- | Return a Result that contain the evaluation of our Lisp String
-- Takes as parameter the string that need to be evaluated and the Stack (Environment)
interpretateLisp :: AST.Ast -> [Scope.ScopeMb] -> Either String (Maybe AST.Ast, [Scope.ScopeMb])
interpretateLisp value stack = case AstEval.evalAst stack value of
(Left err, _) -> Left err
(Right res', stack') -> Right (res', stack')

-- | Infinite loop until EOF from the user
inputLoop :: [Scope.ScopeMb] -> IO ()
-- inputLoop = print
inputLoop stack = isEOF >>= \end -> if end then print "End of Interpretation GLaDOS" else
getLine >>= \line -> case runParser parseLisp (0, 0) line of
Left err -> print err >> exitWith (ExitFailure 84)
Right (res, _, _) -> interpretateInfo res stack
getLine >>= \line -> case runParser parseLobster (0, 0) line of
Left err -> putStrLn ("\ESC[34m\ESC[1mThe lobster is angry: " ++ err ++ "\ESC[0m") >> inputLoop stack
Right (res, [], _) -> interpretateInfo res stack
Right (_, _, pos) -> putStrLn ("\ESC[34m\ESC[1mThe lobster is angry: " ++ errorParsing pos ++ "\ESC[0m") >> inputLoop stack

interpretateInfo :: [SExpr] -> [Scope.ScopeMb] -> IO ()
interpretateInfo [] _ = putStr ""
interpretateInfo :: [AST.Ast] -> [Scope.ScopeMb] -> IO ()
interpretateInfo [] stack = inputLoop stack
interpretateInfo (x:xs) stack = case interpretateLisp x stack of
Left err -> print err
Left err -> putStrLn ("\ESC[31m\ESC[1mThe lobster is angry: " ++ err ++ "\ESC[0m") >> inputLoop stack
Right (res, stack') -> case res of
Nothing -> interpretateInfo xs stack'
Just value -> print value >> print stack' >> interpretateInfo xs stack'
Just value -> print value >> interpretateInfo xs stack'

compileInfo :: [AST.Ast] -> [Scope.ScopeMb] -> IO ()
compileInfo [] _ = putStr ""
compileInfo (x:xs) stack = case interpretateLisp x stack of
Left err -> putStrLn ("\ESC[31m\ESC[1mThe lobster is angry: " ++ err ++ "\ESC[0m") >> exitWith (ExitFailure 84)
Right (res, stack') -> case res of
Nothing -> compileInfo xs stack'
Just value -> print value >> compileInfo xs stack'

compileFile :: String -> IO ()
compileFile s = case runParser parseLisp (0, 0) s of
compileFile s = case runParser parseLobster (0, 0) s of
Left err -> print err >> exitWith (ExitFailure 84)
Right (res, _, _) -> interpretateInfo res []
Right (res, [], _) -> print res >> compileInfo res []
Right (_, _, (row, col)) -> print ("Error on parsing on '" ++ show row ++ "' '" ++ show col)
-- (Right (Just res), stack') -> let instructions = (astToInstructions (AST.Cond (Boolean True) (Value 1) (Just (AST.Call "CallHere" [(Value 0)])))) in showInstructions instructions >> writeCompiledInstructionsToFile "output" (compileInstructions instructions)


checkArgs :: [String] -> IO ()
checkArgs ("-i": _) = print "Launch Interpreter" >> inputLoop []
checkArgs [] = print "Launch Interpreter" >> inputLoop []
checkArgs (file:_) = either
(\_ -> print "File doesn't exist" >> exitWith (ExitFailure 84))
compileFile
=<< (try (readFile file) :: IO (Either SomeException String))
checkArgs _ = exitWith (ExitFailure 84)

-- | Main
main :: IO ()
main = getArgs >>= \argv -> checkArgs argv
-- inputLoop new = isEOF >>= \end -> if end then putStrLn "End of Interpretation GLaDOS" else
-- getLine >>= \line -> case parseLisp line new of
-- (Left err, _) -> putStrLn ("\ESC[31m\ESC[1mThe lobster is angry: " ++ err ++ "\ESC[0m") >> inputLoop new
-- (Right Nothing, stack) -> inputLoop stack
-- (Right (Just res), stack') -> print res >> inputLoop stack'

-- -- | Main
-- main :: IO ()
-- main = putStrLn "Start of Interpretation Lisp" >> inputLoop []
17 changes: 1 addition & 16 deletions LobsterLang/src/AstEval.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
-}

module AstEval
( sexprToAst,
evalAst,
( evalAst,
evalBiValOp,
evalBiBoolOp,
evalBiCompValOp,
Expand All @@ -16,22 +15,8 @@ where

import AST
import Data.Bifunctor
import SExpr
import Scope

-- | Convert a S-expression into an 'Ast',
-- return Nothing if the expression is invalid or Just the Ast
sexprToAst :: SExpr -> Maybe Ast
sexprToAst (SExpr.List [SExpr.Symbol "define", SExpr.Symbol s, t]) =
Define s <$> sexprToAst t
sexprToAst (SExpr.List (SExpr.Symbol f : xs)) =
Call f <$> mapM sexprToAst xs
sexprToAst (SExpr.List _) = Nothing
sexprToAst (SExpr.Value i) = Just (AST.Value i)
sexprToAst (SExpr.Symbol "true") = Just (Boolean True)
sexprToAst (SExpr.Symbol "false") = Just (Boolean False)
sexprToAst (SExpr.Symbol s) = Just (AST.Symbol s Nothing)

noEvaluationError :: String -> String
noEvaluationError s = "No evaluation in one or more parameters of " ++ s

Expand Down
Loading

0 comments on commit 807c1de

Please sign in to comment.