diff options
| author | Jan Tuomi <jans.tuomi@gmail.com> | 2022-09-26 23:28:51 +0300 |
|---|---|---|
| committer | Jan Tuomi <jans.tuomi@gmail.com> | 2022-12-05 14:21:53 +0200 |
| commit | 18c241ff7a53625c6f3c4e6f20ee8df5bb37ddf3 (patch) | |
| tree | 7b4df772e103b69b98316a2b902e7d34e6cdbfcb /src/Builtins.hs | |
| parent | 5fd62d2d3e74e94a8b225f47af074b249ce7f932 (diff) | |
Add kinds of fixes and stuff
Diffstat (limited to 'src/Builtins.hs')
| -rw-r--r-- | src/Builtins.hs | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/src/Builtins.hs b/src/Builtins.hs index f0b9be2..8dc96b2 100644 --- a/src/Builtins.hs +++ b/src/Builtins.hs @@ -2,6 +2,7 @@ module Builtins where import qualified Data.Map as M +import qualified Data.Text as T import Control.Monad.Except import Utils @@ -16,6 +17,7 @@ builtinEnv = M.fromList [ builtinPrepend, builtinPrint, builtinConcat, + builtinFmt, ("unit", ASTUnit) ] @@ -131,3 +133,17 @@ builtinConcat = (name, ASTFunction outer) where return $ ASTString $ str1 ++ str2 inner _ ast2 = throwL $ argError2 name ast1 ast2 outer _ ast1 = throwL $ argError1 name ast1 + +builtinFmt :: (String, AST) +builtinFmt = (name, ASTFunction outer) where + name = "fmt" + outer _ ast1@(ASTString str) = + return $ ASTFunction $ inner where + inner _ (ASTVector replacements) = + return $ ASTString $ T.unpack $ replaceAll (0 :: Int) replacements (T.pack str) + inner _ ast2 = throwL $ argError2 name ast1 ast2 + outer _ ast1 = throwL $ argError1 name ast1 + replaceAll _ [] text = text + replaceAll n (x:xs) text = + let text' = T.replace (T.pack $ "{" ++ show n ++ "}") (T.pack $ show x) text + in replaceAll (n + 1) xs text' |
