Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial support for pre-defined rules #9

Merged
merged 26 commits into from
Jan 13, 2024
Merged
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
9f4c9ac
Fix bytes token rule in grammar
aabounegm Dec 25, 2023
c3d3cb8
Add an explicit export list
aabounegm Dec 25, 2023
e20b57a
Add a draft implementation for the first rule
aabounegm Dec 25, 2023
329d782
Fix the implementation of the first rules
aabounegm Dec 28, 2023
70d57e4
Stop normalizing objects that are values of Nu
aabounegm Dec 28, 2023
802360a
Treat program as a formation when normalizing
aabounegm Dec 28, 2023
ac64050
Update tests
aabounegm Dec 28, 2023
59d4e42
Fix the hex representation of data bytes
aabounegm Dec 28, 2023
b296cdd
Change the test Yaml format to an array of tests
aabounegm Dec 28, 2023
5511306
Merge branch 'master' into rule-1
aabounegm Jan 10, 2024
c4f6a16
Update syntax in phi unit tests
aabounegm Jan 10, 2024
17f46ee
Revert "Fix bytes token rule in grammar"
deemp Jan 10, 2024
03ce7c1
Merge branch '21-fix-grammar' into rule-1
deemp Jan 10, 2024
fdc66c9
feat: explain \nu
deemp Jan 10, 2024
1b05862
Merge branch 'master' into rule-1
aabounegm Jan 11, 2024
13f019b
Make test groups objects, not arrays
fizruk Jan 11, 2024
ece7fbd
Update Syntax.cf
fizruk Jan 11, 2024
cb9d91b
Implement applyRules
fizruk Jan 11, 2024
c5d45f5
Fix imports/exports for the build
fizruk Jan 11, 2024
c5bf9c5
add: string-interpolate package
deemp Jan 12, 2024
2cac3f2
refactor: move rules from phi-paper
deemp Jan 12, 2024
06cd141
upd: tests
deemp Jan 12, 2024
655019e
upd: cabal file
deemp Jan 12, 2024
fcd7bb9
refactor: code to run rules unit tests
deemp Jan 12, 2024
8b0ab4e
fix: rm redundant input
deemp Jan 12, 2024
2494f11
fix: add -Werror flag to match CI
deemp Jan 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add a draft implementation for the first rule
aabounegm committed Dec 28, 2023
commit e20b57ab027fc66f23e4db8bd15440233bfb1baf
27 changes: 26 additions & 1 deletion eo-phi-normalizer/src/Language/EO/Phi/Normalize.hs
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ module Language.EO.Phi.Normalize (

import Data.Maybe (fromMaybe)
import Language.EO.Phi.Syntax.Abs
import Numeric (showHex)

data Context = Context
{ globalObject :: [Binding]
@@ -38,9 +39,33 @@ normalizeBindingWith context = \case
AlphaBinding a object -> AlphaBinding a (normalizeObjectWith context object)
binding -> binding

count :: (a -> Bool) -> [a] -> Int
count = (length .) . filter

normalizeObjectWith :: Context -> Object -> Object
normalizeObjectWith Context{..} object =
normalizeObjectWith ctx@Context{..} object =
case object of
-- Rule 1
Formation bindings -> Formation bindings'
where
bindings'
| not $ any isNu bindings = AlphaBinding VTX (dataObject nu) : normalizedBindings
| otherwise = normalizedBindings
normalizedBindings = map (normalizeBindingWith ctx) bindings
nuCount binds = count isNu binds + sum (map (sum . map (nuCount . objectBindings) . values) binds)
dataObject n = Formation [DeltaBinding $ Bytes $ showHex n ""]

values (AlphaBinding _ obj) = [obj]
values _ = []

objectBindings (Formation bs) = bs
objectBindings _ = []

isNu (AlphaBinding VTX _) = True
isNu (EmptyBinding VTX) = True
isNu _ = False

nu = nuCount normalizedBindings
ThisDispatch a ->
fromMaybe object (lookupBinding a thisObject)
_ -> object