Skip to content

Commit 5c4345a

Browse files
Copilotyegor256
andcommitted
Fix redundant alpha attributes in XMIR output
Co-authored-by: yegor256 <[email protected]>
1 parent 6dc485b commit 5c4345a

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

src/XMIR.hs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,16 @@ expression (ExDispatch expr attr) = do
6969
expression (ExApplication expr (BiTau attr texpr)) = do
7070
(base, children) <- expression expr
7171
(base', children') <- expression texpr
72-
let as = prettyAttribute attr
73-
attrs =
74-
if null base'
75-
then [("as", as)]
76-
else [("as", as), ("base", base')]
72+
let attrs = case attr of
73+
AtAlpha _ -> -- For alpha attributes, omit the "as" attribute since position implies the index
74+
if null base'
75+
then []
76+
else [("base", base')]
77+
_ -> -- For non-alpha attributes, include the "as" attribute as before
78+
let as = prettyAttribute attr
79+
in if null base'
80+
then [("as", as)]
81+
else [("as", as), ("base", base')]
7782
pure (base, children ++ [object attrs children'])
7883
expression (ExApplication (ExFormation bds) tau) = throwIO (UnsupportedExpression (ExApplication (ExFormation bds) tau))
7984
expression expr = throwIO (UnsupportedExpression expr)

test/XMIRSpec.hs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,26 @@ spec = do
2323
it "prints valid xmir" $ do
2424
pending
2525
T.stripEnd (T.pack xmir) `shouldBe` T.stripEnd (T.pack xmir')
26+
27+
it "omits redundant alpha attributes in XMIR" $ do
28+
let phiCode = "Q -> [[org -> [[eolang -> [[version -> Q.org.eolang.number(α0 -> Q.org.eolang.bytes(α0 -> \"test\")), λ ⤍ Package]], λ ⤍ Package]]]]"
29+
program <- parseProgramThrows phiCode
30+
document <- programToXMIR program SALTY
31+
let xmlOutput = printXMIR document
32+
-- Check that alpha attributes are omitted - there should be no 'as="α0"' in the output
33+
xmlOutput `shouldNotContain` "as=\"α0\""
34+
-- Check that the structure is preserved with proper base attributes
35+
xmlOutput `shouldContain` "base=\"Q.org.eolang.number\""
36+
xmlOutput `shouldContain` "base=\"Q.org.eolang.bytes\""
37+
38+
shouldNotContain :: String -> String -> IO ()
39+
shouldNotContain haystack needle =
40+
if T.pack needle `T.isInfixOf` T.pack haystack
41+
then fail $ "Expected not to contain: " ++ needle
42+
else return ()
43+
44+
shouldContain :: String -> String -> IO ()
45+
shouldContain haystack needle =
46+
if T.pack needle `T.isInfixOf` T.pack haystack
47+
then return ()
48+
else fail $ "Expected to contain: " ++ needle

0 commit comments

Comments
 (0)