diff options
| author | Jan Tuomi <jan.tuomi@valuemotive.com> | 2022-09-23 15:44:05 +0300 |
|---|---|---|
| committer | Jan Tuomi <jans.tuomi@gmail.com> | 2022-12-05 14:21:53 +0200 |
| commit | f1617276517dafbfbd855541bf61dbe15165dbe5 (patch) | |
| tree | 826baa98d88d6c8ee1cf0a77ec9d213c04216980 | |
| parent | c02d29e8e119d12748c90b3965f6b98633efbec2 (diff) | |
Add some evaluation logic
| -rw-r--r-- | examples/test.lisp | 16 | ||||
| -rw-r--r-- | src/Lib.hs | 30 |
2 files changed, 36 insertions, 10 deletions
diff --git a/examples/test.lisp b/examples/test.lisp index b753827..39949ab 100644 --- a/examples/test.lisp +++ b/examples/test.lisp @@ -1,8 +1,8 @@ -foo ; test1 -(sum2 ; test2 - 1 - 2 ) -"string with space" -"another \n\"string\"" -(let id - (\[a] a)) +(sum2 1 2) ; test1 +;; (sum2 ; test2 +;; 1 +;; 2 ) +;; "string with space" +;; "another \n\"string\"" +;; (let id +;; (\[a] a)) @@ -115,6 +115,31 @@ _parse acc (token:rest) = parse :: [String] -> LContext [AST] parse = _parse [] +isFunctionAST :: AST -> Bool +isFunctionAST ast = case ast of + (ASTFunction _) -> True + _ -> False + +curryCall :: [AST] -> (AST -> AST) -> LContext AST +curryCall [] f = return $ ASTFunction f +curryCall (arg:[]) f = return $ f arg +curryCall (arg:rest) f = do + g <- curryCall rest f + case g of + ASTFunction f' -> return $ f' arg + other -> throwError $ LException $ "Cannot call value " ++ show other ++ " as a function" + +evaluate :: AST -> LContext AST +evaluate (ASTFunctionCall (first:args)) = do + fnEvaled <- evaluate first + when (not $ isFunctionAST fnEvaled) + $ throwError $ LException $ "Cannot call value " ++ show fnEvaled ++ " as a function" + let (ASTFunction fn) = fnEvaled + evaledArgs <- mapM evaluate args + result <- curryCall evaledArgs fn + return result +evaluate ast = return ast + runScriptFile :: String -> LContext () runScriptFile fileName = do src <- liftIO $ readFile fileName @@ -126,5 +151,6 @@ runInlineScript src = do config <- ask when (configVerboseMode config) $ liftIO $ putStrLn $ "tokenized:\t\t" ++ show tokenized parsed <- parse tokenized - when (configVerboseMode config) $ liftIO $ putStrLn $ "parsed:\t\t\t" ++ show parsed - liftIO $ mapM_ putStrLn (map show parsed) + when (configVerboseMode config) $ liftIO $ mapM_ putStrLn ("parsed:" : map show parsed) + evaluated <- mapM evaluate parsed + liftIO $ mapM_ putStrLn (map show evaluated) |
