Skip to content

Commit

Permalink
fix(eo-phi-normalizer): actually support sugar
Browse files Browse the repository at this point in the history
  • Loading branch information
deemp committed Dec 23, 2024
1 parent 4bddea9 commit 38beb9f
Show file tree
Hide file tree
Showing 17 changed files with 289 additions and 14 deletions.
Binary file added eo-phi-normalizer/Setup
Binary file not shown.
2 changes: 1 addition & 1 deletion eo-phi-normalizer/Setup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ main =
, "bnfc --haskell -d -p Language.EO.Phi --generic -o src/ grammar/EO/Phi/Syntax.cf"
, "cd src/Language/EO/Phi/Syntax"
, "alex Lex.x"
, "happy Par.y"
, "happy --info Par.y"
, "true"
]

Expand Down
4 changes: 3 additions & 1 deletion eo-phi-normalizer/app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ import Language.EO.Phi.Rules.Common (ApplicationLimits (ApplicationLimits), Cont
import Language.EO.Phi.Rules.Fast (fastYegorInsideOut, fastYegorInsideOutAsRule)
import Language.EO.Phi.Rules.RunYegor (yegorRuleSet)
import Language.EO.Phi.Rules.Yaml (RuleSet (rules, title), convertRuleNamed, parseRuleSetFromFile)
import Language.EO.Phi.Syntax (desugar, wrapBytesInBytes, wrapTermination)
import Language.EO.Phi.Syntax (desugar, wrapBytesInBytes, wrapTermination, expectedDesugaredObject)
import Language.EO.Phi.ToLaTeX
import Language.EO.Test.YamlSpec (spec)
import Options.Applicative hiding (metavar)
Expand Down Expand Up @@ -570,7 +570,9 @@ wrapRawBytesIn = \case
obj@MetaFunction{} -> obj
obj@ConstString{} -> wrapRawBytesIn (desugar obj)
obj@ConstInt{} -> wrapRawBytesIn (desugar obj)
obj@ConstIntRaw{} -> expectedDesugaredObject obj
obj@ConstFloat{} -> wrapRawBytesIn (desugar obj)
obj@ConstFloatRaw{} -> expectedDesugaredObject obj

-- * Main

Expand Down
2 changes: 1 addition & 1 deletion eo-phi-normalizer/data/0.49.1/org/eolang/number.phi
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@
⟧,
λ ⤍ Package
⟧}
⟧}
75 changes: 75 additions & 0 deletions eo-phi-normalizer/eo-phi-normalizer.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,67 @@ extra-source-files:
data/0.41.2/org/eolang/txt/sscanf.phi
data/0.41.2/org/eolang/txt/text.phi
data/0.41.2/org/eolang/while.phi
data/0.49.1/dependencies.md
data/0.49.1/org/eolang/as-phi.phi
data/0.49.1/org/eolang/bytes.phi
data/0.49.1/org/eolang/cti.phi
data/0.49.1/org/eolang/dataized.phi
data/0.49.1/org/eolang/error.phi
data/0.49.1/org/eolang/false.phi
data/0.49.1/org/eolang/fs/dir.phi
data/0.49.1/org/eolang/fs/file.phi
data/0.49.1/org/eolang/fs/path.phi
data/0.49.1/org/eolang/fs/tmpdir.phi
data/0.49.1/org/eolang/go.phi
data/0.49.1/org/eolang/i16.phi
data/0.49.1/org/eolang/i32.phi
data/0.49.1/org/eolang/i64.phi
data/0.49.1/org/eolang/io/bytes-as-input.phi
data/0.49.1/org/eolang/io/console.phi
data/0.49.1/org/eolang/io/dead-input.phi
data/0.49.1/org/eolang/io/dead-output.phi
data/0.49.1/org/eolang/io/input-length.phi
data/0.49.1/org/eolang/io/malloc-as-output.phi
data/0.49.1/org/eolang/io/stdin.phi
data/0.49.1/org/eolang/io/stdout.phi
data/0.49.1/org/eolang/io/tee-input.phi
data/0.49.1/org/eolang/malloc.phi
data/0.49.1/org/eolang/math/angle.phi
data/0.49.1/org/eolang/math/e.phi
data/0.49.1/org/eolang/math/integral.phi
data/0.49.1/org/eolang/math/numbers.phi
data/0.49.1/org/eolang/math/pi.phi
data/0.49.1/org/eolang/math/random.phi
data/0.49.1/org/eolang/math/real.phi
data/0.49.1/org/eolang/nan.phi
data/0.49.1/org/eolang/negative-infinity.phi
data/0.49.1/org/eolang/net/socket.phi
data/0.49.1/org/eolang/number.phi
data/0.49.1/org/eolang/positive-infinity.phi
data/0.49.1/org/eolang/rust.phi
data/0.49.1/org/eolang/seq.phi
data/0.49.1/org/eolang/string.phi
data/0.49.1/org/eolang/structs/bytes-as-array.phi
data/0.49.1/org/eolang/structs/hash-code-of.phi
data/0.49.1/org/eolang/structs/list.phi
data/0.49.1/org/eolang/structs/map.phi
data/0.49.1/org/eolang/structs/range-of-ints.phi
data/0.49.1/org/eolang/structs/range.phi
data/0.49.1/org/eolang/structs/set.phi
data/0.49.1/org/eolang/switch.phi
data/0.49.1/org/eolang/sys/getenv.phi
data/0.49.1/org/eolang/sys/line-separator.phi
data/0.49.1/org/eolang/sys/os.phi
data/0.49.1/org/eolang/sys/posix.phi
data/0.49.1/org/eolang/sys/win32.phi
data/0.49.1/org/eolang/true.phi
data/0.49.1/org/eolang/try.phi
data/0.49.1/org/eolang/tuple.phi
data/0.49.1/org/eolang/txt/regex.phi
data/0.49.1/org/eolang/txt/sprintf.phi
data/0.49.1/org/eolang/txt/sscanf.phi
data/0.49.1/org/eolang/txt/text.phi
data/0.49.1/org/eolang/while.phi
test/eo/phi/rules/new.yaml
test/eo/phi/rules/streams.yaml

Expand Down Expand Up @@ -222,6 +283,7 @@ library
Language.EO.Phi.Pipeline.Dataize.PrintConfigs
Language.EO.Phi.Pipeline.EOTests.Data
Language.EO.Phi.Pipeline.EOTests.PrepareTests
Language.EO.Phi.Preprocess
Language.EO.Phi.Report.Data
Language.EO.Phi.Report.Html
Language.EO.Phi.Rules.Common
Expand Down Expand Up @@ -269,8 +331,11 @@ library
, hspec
, hspec-core
, lens
, megaparsec
, mtl
, parser-combinators
, regex-compat
, replace-megaparsec
, scientific
, template-haskell
, text
Expand Down Expand Up @@ -315,9 +380,12 @@ executable eo-phi-normalizer
, hspec
, hspec-core
, lens
, megaparsec
, mtl
, optparse-applicative
, parser-combinators
, regex-compat
, replace-megaparsec
, scientific
, template-haskell
, text
Expand All @@ -344,6 +412,7 @@ test-suite doctests
Language.EO.Phi.Pipeline.Dataize.PrintConfigs
Language.EO.Phi.Pipeline.EOTests.Data
Language.EO.Phi.Pipeline.EOTests.PrepareTests
Language.EO.Phi.Preprocess
Language.EO.Phi.Report.Data
Language.EO.Phi.Report.Html
Language.EO.Phi.Rules.Common
Expand Down Expand Up @@ -393,8 +462,11 @@ test-suite doctests
, hspec
, hspec-core
, lens
, megaparsec
, mtl
, parser-combinators
, regex-compat
, replace-megaparsec
, scientific
, template-haskell
, text
Expand Down Expand Up @@ -448,8 +520,11 @@ test-suite spec
, hspec-core
, hspec-discover
, lens
, megaparsec
, mtl
, parser-combinators
, regex-compat
, replace-megaparsec
, scientific
, template-haskell
, text
Expand Down
3 changes: 3 additions & 0 deletions eo-phi-normalizer/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ dependencies:
- containers
- code-page
- with-utf8
- replace-megaparsec
- megaparsec
- parser-combinators

default-extensions:
- ImportQualifiedPost
Expand Down
2 changes: 1 addition & 1 deletion eo-phi-normalizer/src/Language/EO/Phi.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ parseProgram = parseWith Phi.pProgram

-- | Parse an 'Object' or return a parsing error.
parseObject :: String -> Either String Phi.Object
parseObject = Phi.pObject . Phi.myLexer
parseObject = parseWith Phi.pObject

-- | Parse a 'Program' from a 'String'.
-- May throw an 'error` if input has a syntactical or lexical errors.
Expand Down
2 changes: 2 additions & 0 deletions eo-phi-normalizer/src/Language/EO/Phi/Dataize.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ desugarAsBytes :: Either Object Bytes -> Either Object Bytes
desugarAsBytes (Left obj) = case obj of
ConstString s -> Right (stringToBytes s)
ConstInt n -> Right (intToBytes (fromInteger n))
f@ConstIntRaw{} -> expectedDesugaredObject f
ConstFloat x -> Right (floatToBytes x)
f@ConstFloatRaw{} -> expectedDesugaredObject f
_ -> Left obj
desugarAsBytes (Right bytes) = Right bytes

Expand Down
1 change: 1 addition & 0 deletions eo-phi-normalizer/src/Language/EO/Phi/Dependencies.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ bindingAttr DeltaEmptyBinding = Just (Alpha (AlphaIndex "Δ"))
bindingAttr LambdaBinding{} = Just (Alpha (AlphaIndex "λ"))
bindingAttr MetaBindings{} = Nothing
bindingAttr MetaDeltaBinding{} = Nothing
bindingAttr b@(AlphaBindingSugar{}) = expectedDesugaredBinding b

zipBindings :: [Binding] -> [Binding] -> ([Binding], [(Binding, Binding)])
zipBindings xs ys = (xs' <> ys', collisions)
Expand Down
2 changes: 2 additions & 0 deletions eo-phi-normalizer/src/Language/EO/Phi/Normalize.hs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ peelObject = \case
MetaContextualize{} -> error "impossible"
obj@ConstString{} -> peelObject (desugar obj)
obj@ConstInt{} -> peelObject (desugar obj)
obj@ConstIntRaw{} -> peelObject (desugar obj)
obj@ConstFloat{} -> peelObject (desugar obj)
obj@ConstFloatRaw{} -> peelObject (desugar obj)
where
followedBy (PeeledObject object actions) action = PeeledObject object (actions ++ [action])

Expand Down
57 changes: 57 additions & 0 deletions eo-phi-normalizer/src/Language/EO/Phi/Preprocess.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{-# LANGUAGE BlockArguments #-}

module Language.EO.Phi.Preprocess where

import Control.Monad (void)
import Data.Void (Void)
import Replace.Megaparsec (splitCap)
import Text.Megaparsec (MonadParsec (..), Parsec, Stream (..), between, match, sepBy)
import Text.Megaparsec.Byte.Lexer qualified as L
import Text.Megaparsec.Char (lowerChar, space)

symbol :: String -> Parser String
symbol = L.symbol space

lexeme :: Parser a -> Parser a
lexeme = L.lexeme space

type Parser = Parsec Void String

parseLabelId :: Parser ()
parseLabelId = lexeme do
void lowerChar
void $ takeWhileP (Just "LabelId") (`notElem` " \r\n\t,.|':;!?][}{)(⟧⟦")

parseBindingArrow :: Parser ()
parseBindingArrow = void $ symbol ""

parseAlphaAttr :: Parser ()
parseAlphaAttr = do
void parseLabelId
void $ between (symbol "(") (symbol ")") (sepBy parseLabelId (symbol ","))

parseAlphaBindingSugar :: Parser ()
parseAlphaBindingSugar = do
parseAlphaAttr
parseBindingArrow

splitInput :: Parser a -> String -> [Either String (Tokens [Char])]
splitInput sep = splitCap (fst <$> match sep)

addPrefix :: Parser a -> String -> [String]
addPrefix sep = map (either id ("~" <>)) . splitInput sep

preprocess' :: Parser a -> String -> String
preprocess' sep = concat . addPrefix sep

preprocess :: String -> String
preprocess = preprocess' parseAlphaBindingSugar

input1 :: String
input1 = "{⟦ org ↦ ⟦ eolang ↦ ⟦ number( as-bytes, abra ) ↦ ⟦ φ ↦ ξ.as-bytes, neg ↦ ξ.times(-1), ⟧, λ ⤍ Package ⟧, λ ⤍ Package ⟧ ⟧}"

-- >>> addPrefix parseAlphaBindingSugar input1
-- ["{\10214 org \8614 \10214 eolang \8614 \10214 ","~number( as-bytes, abra ) \8614 ","\10214 \966 \8614 \958.as-bytes, neg \8614 \958.times(-1), \10215, \955 \10509 Package \10215, \955 \10509 Package \10215 \10215}"]

-- >>> preprocess input1
-- "{\10214 org \8614 \10214 eolang \8614 \10214 ~number( as-bytes, abra ) \8614 \10214 \966 \8614 \958.as-bytes, neg \8614 \958.times(-1), \10215, \955 \10509 Package \10215, \955 \10509 Package \10215 \10215}"
7 changes: 7 additions & 0 deletions eo-phi-normalizer/src/Language/EO/Phi/Rules/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ withSubObject f ctx root =
MetaContextualize _ _ -> []
ConstString{} -> []
ConstInt{} -> []
ConstIntRaw{} -> []
ConstFloat{} -> []
ConstFloatRaw{} -> []

-- | Given a unary function that operates only on plain objects,
-- converts it to a function that operates on named objects
Expand All @@ -154,6 +156,7 @@ withSubObjectBindings f ctx (b : bs) =
withSubObjectBinding :: (Context -> Object -> [(String, Object)]) -> Context -> Binding -> [(String, Binding)]
withSubObjectBinding f ctx = \case
AlphaBinding a obj -> propagateName1 (AlphaBinding a) <$> withSubObject f (ctx{currentAttr = a}) obj
b@AlphaBindingSugar{} -> expectedDesugaredBinding b
EmptyBinding{} -> []
DeltaBinding{} -> []
DeltaEmptyBinding{} -> []
Expand Down Expand Up @@ -204,7 +207,9 @@ objectSize = \case
obj@MetaTailContext{} -> error ("impossible: expected a desugared object, but got: " <> printTree obj)
obj@ConstString{} -> objectSize (desugar obj)
obj@ConstInt{} -> objectSize (desugar obj)
obj@ConstIntRaw{} -> objectSize (desugar obj)
obj@ConstFloat{} -> objectSize (desugar obj)
obj@ConstFloatRaw{} -> objectSize (desugar obj)

bindingSize :: Binding -> Int
bindingSize = \case
Expand All @@ -215,6 +220,7 @@ bindingSize = \case
LambdaBinding _lam -> 1
obj@MetaDeltaBinding{} -> error ("impossible: expected a desugared object, but got: " <> printTree obj)
obj@MetaBindings{} -> error ("impossible: expected a desugared object, but got: " <> printTree obj)
b@AlphaBindingSugar{} -> expectedDesugaredBinding b

-- | A variant of `applyRules` with a maximum application depth.
applyRulesWith :: ApplicationLimits -> Context -> Object -> [Object]
Expand Down Expand Up @@ -257,6 +263,7 @@ equalBindings bindings1 bindings2 = and (zipWith equalBinding (sortOn attr bindi
attr (MetaDeltaBinding _) = Label (LabelId "Δ")
attr (LambdaBinding _) = Label (LabelId "λ")
attr (MetaBindings (BindingsMetaId metaId)) = MetaAttr (LabelMetaId metaId)
attr b@AlphaBindingSugar{} = expectedDesugaredBinding b

equalBinding :: Binding -> Binding -> Bool
equalBinding (AlphaBinding attr1 obj1) (AlphaBinding attr2 obj2) = attr1 == attr2 && equalObject obj1 obj2
Expand Down
2 changes: 2 additions & 0 deletions eo-phi-normalizer/src/Language/EO/Phi/Rules/Fast.hs
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,6 @@ fastYegorInsideOut ctx = \case
MetaFunction{} -> error "impossible MetaFunction!"
obj@ConstString{} -> obj -- fastYegorInsideOut ctx (desugar obj)
obj@ConstInt{} -> obj -- fastYegorInsideOut ctx (desugar obj)
obj@ConstIntRaw{} -> obj -- fastYegorInsideOut ctx (desugar obj)
obj@ConstFloat{} -> obj -- fastYegorInsideOut ctx (desugar obj)
obj@ConstFloatRaw{} -> obj -- fastYegorInsideOut ctx (desugar obj)
Loading

0 comments on commit 38beb9f

Please sign in to comment.