Skip to content

Commit

Permalink
Merge pull request #58 from nLatt/57-add-function-comments
Browse files Browse the repository at this point in the history
57 add function comments
  • Loading branch information
mindoodoo authored Feb 7, 2023
2 parents bb67fd3 + 564f6bc commit 30b15df
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 22 deletions.
10 changes: 5 additions & 5 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ evalTree (x : xs) reg = do

main :: IO ()
main = do
-- Parsing arguments
(res, fls) <- getArgs >>= parse
-- Parsing arguments
(res, fls) <- getArgs >>= parse

let fileName = getFileName fls (file res)
let fileName = getFileName fls (file res)

if interactive res
if interactive res
then interactiveMode -- interactive
else if fileName == "stdin"
then do
file <- getContents
execute $ tokenize file
else runFile fileName -- normal
return ()
return ()
25 changes: 9 additions & 16 deletions lib/Exec/Eval.hs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ eval (Ast.Symbole s) (v, f) = case lookupVar s v of
Just x -> return $ RetVal (v, f) x
eval x reg = return $ RetVal reg x

-- Dunno what either of these comments are about
-- INPUT SHOULDN'T BE LIST
-- LIST NEEDS TO BE CHECKED IF FUNCTION CALL

-- transforms a list of Symboles into a list of Strings
toString :: [Ast.Expr] -> [String]
toString [Ast.Symbole x] = [x]
toString (Ast.Symbole x: xs) = x : toString xs
Expand All @@ -87,24 +84,20 @@ execFunc funcName argValues reg
| Ast.isValidBuiltin funcName = execBuiltin (Ast.Call funcName argValues) reg
| otherwise = case lookupFunc funcName (snd reg) of
Nothing -> throwIO $ InvalidFunctionCall funcName
Just (Ast.ExprList [Ast.ExprList args, body]) -> evalLambda args body argValues reg
Just (Ast.ExprList [Ast.ExprList args, body]) -> evalLambda args body argValues reg
_ -> throwIO FatalError

makeLambda :: [Ast.Expr] -> Ast.Expr -> [Ast.Expr] -> Ast.Expr
makeLambda args body argValues = Ast.ExprList (Ast.ExprList [Ast.Symbole "lambda", Ast.ExprList args, body] : argValues)
-- Evaluates a lambda expression
-- Accepts: Expression describing the arguments [Symbole "a", Symbole "b"],
-- Expression describing the behaviour Call "+" [Symbole "a", Symbole "b"]],
-- Expression describing the arguments' values [Num 2, Num 3]

-- Argument values are bound to arguments based on order
evalLambda :: [Ast.Expr] -> Ast.Expr -> [Ast.Expr] -> Registry -> IO RetVal
evalLambda args body argValues = evalLambda' (makeLambda args body argValues)

evalLambda' :: Ast.Expr -> Registry -> IO RetVal
evalLambda' (Ast.ExprList (Ast.ExprList [Ast.Symbole "lambda", Ast.ExprList args, body] : argValues)) reg = do
tempReg <- bindArgs (toString args) argValues reg
evalLambda args body val reg = do
tempReg <- bindArgs (toString args) val reg
(RetVal a v) <- eval body tempReg
return (RetVal reg v)
evalLambda' a _ = throwIO $ InvalidFunctionCall "lambda"

-- Ast.Call n a -> bindArgs argNames argValues reg >>= execCall (Ast.Call n a) >>= unbindArgs argNames
-- _ -> throwIO FatalError -- If body isn't an Ast.Call

-- Syntactic sugar to convert Ast.Call to args for execFunc
--
Expand Down
2 changes: 1 addition & 1 deletion lib/Parsing/Args.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ options =
Option ['f'] ["file"] (OptArg ((\ f opts -> opts { file = Just f }) . fromMaybe "stdin") "FILE") "The FILE that is to be executed.",
Option ['h'] ["help"] (NoArg (\ opts -> opts { help = True })) "Display this message.",
Option ['d'] ["debug"] (NoArg (\ opts -> opts { debug = True })) "Enter debug mode",
Option ['i'] ["interactive"] (NoArg (\ opts -> opts { debug = True })) "Enter debug mode"
Option ['i'] ["interactive"] (NoArg (\ opts -> opts { interactive = True })) "Enter interactive REPL"
]

parse :: [String] -> IO (Args, [String])
Expand Down

0 comments on commit 30b15df

Please sign in to comment.