diff options
| author | Jan Tuomi <jan.tuomi@valuemotive.com> | 2022-09-22 22:29:38 +0300 |
|---|---|---|
| committer | Jan Tuomi <jans.tuomi@gmail.com> | 2022-12-05 14:21:51 +0200 |
| commit | 12ac2354ffca7a221cb4535354eb86907bfceea7 (patch) | |
| tree | e91d72e94313b3efb36e08cfa1b8eaff77a4ba86 /src | |
| parent | 1ce7af69533658ec88971a1c53eb4a158bb66e48 (diff) | |
Initial commit
Diffstat (limited to 'src')
| -rw-r--r-- | src/Lib.hs | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/Lib.hs b/src/Lib.hs new file mode 100644 index 0000000..b9ad7bb --- /dev/null +++ b/src/Lib.hs @@ -0,0 +1,56 @@ +module Lib + ( runScriptFile + ) where + +import qualified Data.Map as M +import qualified Data.Bifunctor as B +import Debug.Trace + +data AST + = ASTNumber Double + | ASTSymbol String + | ASTBoolean Bool + | ASTString String + | ASTVector [AST] + | ASTHashMap (M.Map AST AST) + | ASTFunction (AST -> AST) + +_tokenize :: [String] -> String -> String -> [String] +_tokenize acc current src = case src of + "" -> reverse current : acc + (x:xs) + | x == ';' -> + let commentDropped = dropWhile (\c -> c /= '\n') xs + in _tokenize (reverse current : acc) "" commentDropped + | x == '"' -> + let consume :: String -> (String, Int) + consume str = case str of + ('\\':'"':rest) -> B.bimap ('\"' :) (+ 2) (consume rest) + ('\\':'n':rest) -> B.bimap ('\n' :) (+ 2) (consume rest) + ('\\':'t':rest) -> B.bimap ('\t' :) (+ 2) (consume rest) + ('"':_) -> ("", 1) + (c:rest) -> B.bimap (c :) (+ 1) (consume rest) + [] -> error "Unbalanced string literal" + (string, stringLength) = consume xs + stringDropped = drop (stringLength) xs + in _tokenize (string : acc) "" stringDropped + | x `elem` [' ', '\n', '\t', '\r'] -> + _tokenize (reverse current : acc) "" xs + | x `elem` ['(', ')', '[', ']', '{', '}', '\\'] -> + _tokenize ([x] : acc) "" xs + | otherwise -> + _tokenize acc (x : current) xs + +tokenize :: String -> [String] +tokenize = filter (\t -> length t > 0) . reverse . _tokenize [] "" + +parse :: [String] -> [AST] +parse src = [] + +runScriptFile :: String -> IO () +runScriptFile fileName = do + src <- readFile fileName + let tokenized = tokenize src + putStrLn $ "tokenized:\t\t" ++ show tokenized + let parsed = parse tokenized + return () |
