diff options
author | Marvin Borner | 2024-04-13 20:57:13 +0200 |
---|---|---|
committer | Marvin Borner | 2024-04-13 20:57:13 +0200 |
commit | 32f0891053cb9efe7b2987638a96395d2b9f9722 (patch) | |
tree | 10ed09386fbe39073ae72b2948e607b9da3bd877 | |
parent | 97b55f035d883cb1d0985c6f3f64c9490dd17bb9 (diff) |
Visual improvements
-rw-r--r-- | bruijn.cabal | 3 | ||||
-rw-r--r-- | docs/code.js | 6 | ||||
-rw-r--r-- | src/Helper.hs | 44 |
3 files changed, 41 insertions, 12 deletions
diff --git a/bruijn.cabal b/bruijn.cabal index f7ea587..676ad66 100644 --- a/bruijn.cabal +++ b/bruijn.cabal @@ -39,6 +39,9 @@ data-files: std/Logic/Binary.bruijn std/Logic/Linear.bruijn std/Logic/Ternary.bruijn + std/Math/Complex.bruijn + std/Math/Rational.bruijn + std/Math/Real.bruijn std/Number/Binary.bruijn std/Number/Bruijn.bruijn std/Number/Conversion.bruijn diff --git a/docs/code.js b/docs/code.js index 71ca75d..8fd7158 100644 --- a/docs/code.js +++ b/docs/code.js @@ -5,11 +5,15 @@ const term = (t) => t .replaceAll( - /(?<!\([+-]\d*)(?<![a-z][^&; ]*)(?<!["'])([0-9])/g, + /(?<!\([+-]\d*\.?\d*)(?<![a-z][^&; ]*)(?<!["'])([0-9])/g, "<span class='index'>$1</span>", ) .replaceAll(/'([^\'])'/g, "<span class='string'>'$1'</span>") .replaceAll(/"([^\"]*)"/g, "<span class='string'>\"$1\"</span>") + .replaceAll( + /(\([+-][0-9]+\.[0-9]+[frc]?\))/g, + "<span class='number'>$1</span>", + ) .replaceAll(/(\([+-][0-9]+[ubtd]?\))/g, "<span class='number'>$1</span>") .replaceAll(/(?<!\>)(\()/g, "<span class='left-app'>(</span>") .replaceAll(/(\))(?!\<)/g, "<span class='right-app'>)</span>") diff --git a/src/Helper.hs b/src/Helper.hs index 3617cfc..8ea5600 100644 --- a/src/Helper.hs +++ b/src/Helper.hs @@ -23,6 +23,7 @@ import GHC.Generics ( Generic ) import GHC.Real ( denominator , numerator ) +import Numeric ( showFFloatAlt ) import Text.Megaparsec invalidProgramState :: a @@ -341,8 +342,9 @@ maybeHumanifyExpression :: Expression -> Maybe String maybeHumanifyExpression e = unaryToDecimal e <|> binaryToChar e - <|> binaryToDecimal e - <|> ternaryToDecimal e + <|> binaryToString e + <|> ternaryToString e + <|> rationalToFloat e <|> humanifyString e <|> humanifyList e <|> humanifyPair e @@ -394,7 +396,7 @@ floatToRational f = Abstraction q = denominator f floatToReal :: Rational -> Expression -floatToReal f = Bruijn 0 +floatToReal = Abstraction . floatToRational floatToComplex :: Rational -> Expression floatToComplex f = Bruijn 0 @@ -452,19 +454,19 @@ unaryToDecimal' e = do resolve (Abstraction (Abstraction n)) = resolve' n resolve _ = Nothing -binaryToDecimal :: Expression -> Maybe String -binaryToDecimal e = (<> "b") . show <$> binaryToDecimal' e - binaryToChar :: Expression -> Maybe String binaryToChar e = show <$> binaryToChar' e binaryToChar' :: Expression -> Maybe Char binaryToChar' e = do - n <- binaryToDecimal' e + n <- binaryToDecimal e if n > 31 && n < 127 || n == 10 then Just $ chr $ fromIntegral n else Nothing -binaryToDecimal' :: Expression -> Maybe Integer -binaryToDecimal' e = do +binaryToString :: Expression -> Maybe String +binaryToString e = (<> "b") . show <$> binaryToDecimal e + +binaryToDecimal :: Expression -> Maybe Integer +binaryToDecimal e = do res <- resolve e return (sum $ zipWith (*) res (iterate (* 2) 1) :: Integer) where @@ -480,10 +482,13 @@ binaryToDecimal' e = do resolve (Abstraction (Abstraction (Abstraction n))) = resolve' n resolve _ = Nothing -ternaryToDecimal :: Expression -> Maybe String +ternaryToString :: Expression -> Maybe String +ternaryToString e = (<> "t") . show <$> ternaryToDecimal e + +ternaryToDecimal :: Expression -> Maybe Integer ternaryToDecimal e = do res <- resolve e - return $ show (sum $ zipWith (*) res (iterate (* 3) 1) :: Integer) <> "t" + return $ (sum $ zipWith (*) res (iterate (* 3) 1) :: Integer) where multiplier (Bruijn 0) = Just 0 multiplier (Bruijn 1) = Just 1 @@ -498,3 +503,20 @@ ternaryToDecimal e = do resolve (Abstraction (Abstraction (Abstraction (Abstraction n)))) = resolve' n resolve _ = Nothing + +rationalToFloat :: Expression -> Maybe String +rationalToFloat (Abstraction (Application (Application (Bruijn 0) a) b)) = do + n <- ternaryToDecimal a + d <- ternaryToDecimal b + -- let (h, r) = properFraction (n % (d + 1)) + Just + $ show n + <> "/" + <> show (d + 1) + <> " (approx. " + <> (showFFloatAlt (Just 8) + ((fromIntegral n) / (fromIntegral $ d + 1) :: Double) + "" + ) + <> ")" +rationalToFloat _ = Nothing |