diff options
author | Marvin Borner | 2022-08-23 12:32:38 +0200 |
---|---|---|
committer | Marvin Borner | 2022-08-23 12:33:31 +0200 |
commit | 0a2d18ec27a6ef1dea90e57632834a7eb84bb9cf (patch) | |
tree | 069cd982b0f25c3ea60569f47c335a81887e6bb8 /src/Eval.hs | |
parent | c0ce15a3bacc9fe336f0e536a9b60512e8cc593e (diff) |
Added advanced suggestion AI
blockchain soon
Diffstat (limited to 'src/Eval.hs')
-rw-r--r-- | src/Eval.hs | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/src/Eval.hs b/src/Eval.hs index 2f1bb7e..fe8dbe8 100644 --- a/src/Eval.hs +++ b/src/Eval.hs @@ -8,6 +8,7 @@ import Control.Monad.State import qualified Control.Monad.State.Strict as StrictState import qualified Data.BitString as Bit import qualified Data.ByteString as Byte +import Data.Function ( on ) import Data.List import qualified Data.Map as M import Data.Maybe @@ -67,9 +68,17 @@ evalFun fun (Environment sub) = state $ \env@(Environment e) -> let lookup' name env' = case M.lookup fun env' of Nothing -> Left $ UndefinedIdentifier name Just (EnvDef { _exp = x }) -> Right x + matching n + | length e == 0 = "<no idea>" + | otherwise = snd $ minimumBy (compare `on` fst) $ map + (\f -> (levenshtein (functionName f) n, show f)) + (M.keys e) + suggest (Left u@(UndefinedIdentifier n)) = + Left $ SuggestSolution u (matching $ functionName n) + suggest x = x in case lookup' fun sub of -- search in sub env s@(Right _) -> (s, env) - _ -> (lookup' fun e, env) -- search in global env + _ -> (suggest $ lookup' fun e, env) -- search in global env evalAbs :: Expression -> Environment -> EvalState (Failable Expression) evalAbs e sub = evalExp e sub >>= pure . fmap Abstraction |