diff options
author | Marvin Borner | 2022-08-10 18:53:34 +0200 |
---|---|---|
committer | Marvin Borner | 2022-08-10 18:53:34 +0200 |
commit | 792534b3888bc1b9c33047f1c312c4e17a720885 (patch) | |
tree | b0401f5c1ca156858fb7de94a5488151be075c89 | |
parent | cba3d7d21241f8db913e6e2733a8edc3a522ee62 (diff) |
Added syntactic sugar for strings & chars
-rw-r--r-- | bruijn.cabal | 1 | ||||
-rw-r--r-- | editors/vim/syntax/bruijn.vim | 8 | ||||
-rw-r--r-- | src/Eval.hs | 4 | ||||
-rw-r--r-- | src/Helper.hs | 12 | ||||
-rw-r--r-- | src/Parser.hs | 12 | ||||
-rw-r--r-- | std/Byte.bruijn | 14 | ||||
-rw-r--r-- | std/List.bruijn | 1 |
7 files changed, 46 insertions, 6 deletions
diff --git a/bruijn.cabal b/bruijn.cabal index 6979a61..f866112 100644 --- a/bruijn.cabal +++ b/bruijn.cabal @@ -19,6 +19,7 @@ extra-source-files: README.md data-files: config + std/Byte.bruijn std/Church.bruijn std/Combinator.bruijn std/List.bruijn diff --git a/editors/vim/syntax/bruijn.vim b/editors/vim/syntax/bruijn.vim index 45e75d9..8bd194c 100644 --- a/editors/vim/syntax/bruijn.vim +++ b/editors/vim/syntax/bruijn.vim @@ -14,16 +14,20 @@ syn match bruijnKeyword /:test\|:import\|:print/ syn match bruijnNamespace /[A-Z][a-z]*\(\.\)\@=/ syn match bruijnNamespaceDelim /\([A-Z][a-z]*\)\@<=\./ -syn region bruijnCommentLine start="^# " end="$" +syn region bruijnCommentLine start="^# " end="$" oneline +syn region bruijnString start=+"+ end=+"+ oneline +syn region bruijnChar start=+'+ end=+'+ oneline hi def link bruijnIndex Special hi def link bruijnNumber Number +hi def link bruijnString String +hi def link bruijnChar String hi def link bruijnDefinition Define hi def link bruijnKeyword Macro hi def link bruijnNamespace Type hi def link bruijnNamespaceDelim Special hi def link bruijnAbstraction Function -hi def link bruijnApplication String +hi def link bruijnApplication Statement hi def link bruijnCommentLine Comment let b:current_syntax = "bruijn" diff --git a/src/Eval.hs b/src/Eval.hs index 7cf598b..3a1b0c1 100644 --- a/src/Eval.hs +++ b/src/Eval.hs @@ -137,7 +137,7 @@ evalInstruction (ContextualInstruction instr inp) s@(EnvState env) rec conf = ca <> (show e') <> "\n*> " <> (show reduced) - <> (if likeTernary reduced + <> (if likeTernary reduced -- TODO: Also sugar string/char then "\t(" <> (show $ ternaryToDecimal reduced) <> ")" else "" ) @@ -255,5 +255,5 @@ evalMain = do exec path (try . Byte.readFile) (fromBitString . Bit.bitString) ["-E", path] -> exec path (try . readFile) id ['-' : _] -> usage - [path ] -> evalFile path putStrLn decodeStdout + [path ] -> evalFile path putStr decodeStdout _ -> usage diff --git a/src/Helper.hs b/src/Helper.hs index 160ca9a..d653319 100644 --- a/src/Helper.hs +++ b/src/Helper.hs @@ -8,6 +8,7 @@ module Helper where import qualified Control.Monad.State as S import qualified Data.BitString as Bit import qualified Data.ByteString as Byte +import qualified Data.ByteString.Char8 as C import Data.List import Text.Megaparsec @@ -136,8 +137,15 @@ encodeBytes bytes = listify $ map (encodeByte . Bit.toList . Bit.bitString . Byte.pack . (: [])) (Byte.unpack bytes) +stringToExpression :: String -> Expression +stringToExpression = encodeBytes . C.pack + +charToExpression :: Char -> Expression +charToExpression ch = encodeByte $ Bit.toList $ Bit.bitString $ C.pack [ch] + encodeStdin :: IO Expression encodeStdin = do + putStrLn "Waiting for stdin eof" bytes <- Byte.getContents pure $ encodeBytes bytes @@ -153,10 +161,10 @@ decodeByte (Abstraction (Application (Application (Bruijn 0) (Abstraction (Abstr = False : (decodeByte es) decodeByte (Abstraction (Application (Application (Bruijn 0) (Abstraction (Abstraction (Bruijn 1)))) es)) = True : (decodeByte es) -decodeByte _ = error "invalid" +decodeByte _ = error "invalid" -- TODO: Better errors using Maybe decodeStdout :: Expression -> String -decodeStdout e = show $ Byte.concat $ map +decodeStdout e = C.unpack $ Byte.concat $ map (Bit.realizeBitStringStrict . Bit.fromList . decodeByte) (unlistify e) diff --git a/src/Parser.hs b/src/Parser.hs index 5d62ab0..1d889d3 100644 --- a/src/Parser.hs +++ b/src/Parser.hs @@ -73,6 +73,16 @@ parseNumeral = do number :: Parser Integer number = ap sign nat +parseString :: Parser Expression +parseString = do + str <- between (char '\"') (char '\"') (some $ satisfy (`notElem` "\"\\")) + pure (stringToExpression str) <?> "string" + +parseChar :: Parser Expression +parseChar = do + ch <- between (char '\'') (char '\'') (satisfy (`notElem` "\"\\")) + pure (charToExpression ch) <?> "char" + parseVariable :: Parser Expression parseVariable = do var <- identifier @@ -82,6 +92,8 @@ parseSingleton :: Parser Expression parseSingleton = parseBruijn <|> parseNumeral + <|> parseString + <|> parseChar <|> parseAbstraction <|> (parens parseApplication <?> "enclosed application") <|> parseVariable diff --git a/std/Byte.bruijn b/std/Byte.bruijn new file mode 100644 index 0000000..72e2c6e --- /dev/null +++ b/std/Byte.bruijn @@ -0,0 +1,14 @@ +# MIT License, Copyright (c) 2022 Marvin Borner + +:import std/Combinator . + +:import std/List . + +# bit 0 +b0 F + +# bit 1 +b1 T + +# generates a byte with correct endianness +byte [[[[[[[[cons 0 (cons 1 (cons 2 (cons 3 (cons 4 (cons 5 (cons 6 (cons 7 empty)))))))]]]]]]]] diff --git a/std/List.bruijn b/std/List.bruijn index eba39bb..ac9b574 100644 --- a/std/List.bruijn +++ b/std/List.bruijn @@ -21,6 +21,7 @@ empty? [0 [[[F]]] T] cons P.pair :test cons +1 (cons +2 empty) = P.pair +1 (P.pair +2 empty) +:test cons 'a' (cons 'b' (cons 'c' empty)) = "abc" # returns the head of a list or empty head P.fst |