This repository has been archived by the owner on Jan 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Add control flow AST #2
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Submodule libsnark
updated
61 files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
{-# LANGUAGE GADTs #-} | ||
{-# LANGUAGE DeriveGeneric #-} | ||
{-# LANGUAGE KindSignatures #-} | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE QuantifiedConstraints #-} | ||
{-# LANGUAGE StandaloneDeriving #-} | ||
{-# LANGUAGE QuasiQuotes #-} | ||
{-# LANGUAGE DeriveFunctor #-} | ||
{-# LANGUAGE DeriveAnyClass #-} | ||
module IR.Circify.Control where | ||
|
||
import Data.Natural | ||
import Data.String.Interpolate (i) | ||
import Control.Monad.State.Lazy | ||
|
||
-- Range of a for loop | ||
data Range = Range { start :: Int, end :: Int, step :: Int } | ||
|
||
-- Convenience function that errors on bad ranges | ||
mkRange :: Int -> Int -> Int -> Range | ||
mkRange start end step | ||
| abs (end - start) >= step && step > 0 = Range start end step | ||
| abs (end - start) >= (- step) && step < 0 = Range start end step | ||
| otherwise = error [i| Bad loop bounds for(i = #{start}; i < #{end}; i = i + #{step})|] | ||
|
||
-- Control flow syntax | ||
data Control (term :: * -> *) (a :: *) where | ||
For :: Range -> term Int -> Control term a -> Control term a | ||
While :: term Bool -> Natural -> Control term a -> Control term a | ||
If :: term Bool -> Control term a -> Control term a -> Control term a | ||
One :: term a -> Control term a | ||
Seq :: term b -> Control term a -> Control term a | ||
|
||
deriving instance Functor term => Functor (Control term) | ||
deriving instance Applicative term => Applicative (Control term) | ||
deriving instance Monad term => Monad (Control term) | ||
|
||
-- Convenience constructors | ||
forloop :: Range -> term Int -> Control term a -> Control term a | ||
forloop range iterator body = For range iterator body | ||
|
||
whileloop :: term Bool -> Natural -> Control term a -> Control term a | ||
whileloop check maxIterations body = While check maxIterations body | ||
|
||
ifstmt :: term Bool -> Control term a -> Control term a -> Control term a | ||
ifstmt check left right = If check left right | ||
|
||
-- Analysis state | ||
data LoopAnalysis a term = LoopAnalysis { stack :: [Control term a] } | ||
|
||
-- TODO: Implement analysis and loop flattening transformation | ||
loopAnalysis :: Control term a -> State (LoopAnalysis a term) (Control term a) | ||
loopAnalysis = undefined | ||
|
||
loopFlatten :: Control term a -> Control term a | ||
loopFlatten top = undefined | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the body be
Control term a
? Maybe we should do the loop flattening during Codegen and have nested loops be unrepresentable