-
Notifications
You must be signed in to change notification settings - Fork 6
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
Idris2 pretty printer initial support #208
base: master
Are you sure you want to change the base?
Changes from all commits
e6316bc
05fe03c
ca77e57
6a0c7fb
bbb0087
fc8d7f5
149e7f9
09d73b2
c7cbbc5
851ae3f
be10da9
4a5ae49
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,72 +19,110 @@ import System.Random.Pure.StdGen | |
%default total | ||
|
||
public export | ||
data UniqNames : Funs -> Vars -> Type | ||
data SupportedLanguage = Scala3 | ||
| Idris2 | ||
| Lua5_4 | ||
|
||
public export | ||
data ScalaCondition : FunSig -> (isInfix : Bool) -> (isPure : Bool) -> Type | ||
where | ||
IsNotInfix : ScalaCondition funSig False b | ||
MoreThanOneArg : So (funSig.From.length >= 1) -> ScalaCondition funSig isInfix b | ||
|
||
public export | ||
data NameIsNew : UniqNames funs vars -> String -> Type | ||
|
||
data UniqNames : Funs -> Vars -> Type where | ||
Empty : UniqNames [<] [<] | ||
JustNew : (ss : UniqNames funs vars) => (s : String) -> (0 _ : NameIsNew ss s) => UniqNames funs vars | ||
NewFun : (ss : UniqNames funs vars) => (s : String) -> (0 _ : NameIsNew ss s) => | ||
{default False isInfix : Bool} -> (0 infixCond : So $ not isInfix || fun.From.length >= 1) => | ||
UniqNames (funs:<fun) vars | ||
NewVar : (ss : UniqNames funs vars) => (s : String) -> (0 _ : NameIsNew ss s) => UniqNames funs ((:<) vars var mut) | ||
|
||
data NameIsNew : UniqNames funs vars -> String -> Type where | ||
E : NameIsNew {funs=[<]} {vars=[<]} Empty x | ||
J : (0 _ : So $ x /= s) -> NameIsNew {funs} {vars} ss x -> NameIsNew {funs} {vars} (JustNew @{ss} s @{sub}) x | ||
F : (0 _ : So $ x /= s) -> NameIsNew {funs} {vars} ss x -> NameIsNew {funs=funs:<fun} {vars} (NewFun @{ss} {isInfix} s @{sub} @{infixCond}) x | ||
V : (0 _ : So $ x /= s) -> NameIsNew {funs} {vars} ss x -> NameIsNew {funs} {vars=(:<) vars var mut} (NewVar @{ss} s @{sub}) x | ||
data IdrisCondition : FunSig -> (isInfix : Bool) -> (isPure : Bool) -> Type | ||
where | ||
IsInfix : (a : Ty ** b : Ty ** to : MaybeTy ** funSig === ([<a, b] ==> to)) -> (isPure : Bool) -> IdrisCondition funSig True isPure | ||
NotInfix : (isPure : Bool) -> IdrisCondition funSig False isPure | ||
|
||
public export | ||
data LuaCondition : FunSig -> (isInfix : Bool) -> (isPure : Bool) -> Type | ||
where | ||
TrivialLuaCondition : LuaCondition funSig isInfix isPure | ||
|
||
public export | ||
data LanguageToCondition : (l : SupportedLanguage) -> FunSig -> (isInfix : Bool) -> (isPure : Bool) -> Type | ||
where | ||
[search l] | ||
Scala3Cond : ScalaCondition funSig isInfix isPure -> LanguageToCondition Scala3 funSig isInfix isPure | ||
Idris2Cond : IdrisCondition funSig isInfix isPure -> LanguageToCondition Idris2 funSig isInfix isPure | ||
Lua5_4Cond : LuaCondition funSig isInfix isPure -> LanguageToCondition Lua5_4 funSig isInfix isPure | ||
|
||
public export | ||
data UniqNames : (l : SupportedLanguage) -> (funs : Funs) -> (vars : Vars) -> Type | ||
public export | ||
data NameIsNew : (l : SupportedLanguage) -> (funs : Funs) -> (vars : Vars) -> UniqNames l funs vars -> String -> Type | ||
|
||
data UniqNames : (l : SupportedLanguage) -> (funs : Funs) -> (vars : Vars) -> Type where | ||
[search funs vars] | ||
Empty : UniqNames l [<] [<] | ||
JustNew : (ss : UniqNames l funs vars) => (s : String) -> (0 _ : NameIsNew l funs vars ss s) => UniqNames l funs vars | ||
NewFun : (ss : UniqNames l funs vars) => (s : String) -> (0 _ : NameIsNew l funs vars ss s) => | ||
{default False isInfix : Bool} -> {default False isPure : Bool} -> | ||
(languageCondition : LanguageToCondition l fun isInfix isPure) => | ||
UniqNames l (funs:<fun) vars | ||
NewVar : (ss : UniqNames l funs vars) => (s : String) -> (0 _ : NameIsNew l funs vars ss s) => UniqNames l funs ((:<) vars var mut) | ||
|
||
data NameIsNew : (l : SupportedLanguage) -> (funs : Funs) -> (vars : Vars) -> UniqNames l funs vars -> String -> Type where | ||
E : NameIsNew l [<] [<] Empty x | ||
J : (0 _ : So $ x /= s) -> NameIsNew l funs vars ss x -> NameIsNew l funs vars (JustNew @{ss} s @{sub}) x | ||
F : (0 _ : So $ x /= s) -> NameIsNew l funs vars ss x -> NameIsNew l (funs:<fun) vars (NewFun @{ss} {isInfix} {isPure} s @{sub} @{infixCond}) x | ||
V : (0 _ : So $ x /= s) -> NameIsNew l funs vars ss x -> NameIsNew l funs ((:<) vars var mut) (NewVar @{ss} s @{sub}) x | ||
|
||
public export | ||
interface NamesRestrictions where | ||
reservedKeywords : SortedSet String | ||
|
||
rawNewName : Fuel -> (Fuel -> Gen MaybeEmpty String) => | ||
(vars : Vars) -> (funs : Funs) -> (names : UniqNames funs vars) -> | ||
Gen MaybeEmpty (s ** NameIsNew names s) | ||
rawNewName : Fuel -> (l : SupportedLanguage) -> (Fuel -> Gen MaybeEmpty String) => | ||
(funs : Funs) -> (vars : Vars) -> (names : UniqNames l funs vars) -> | ||
Gen MaybeEmpty (s ** NameIsNew l funs vars names s) | ||
Comment on lines
+76
to
+78
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please put |
||
|
||
export | ||
genNewName : Fuel -> (Fuel -> Gen MaybeEmpty String) => | ||
genNewName : {l : SupportedLanguage} -> Fuel -> Gen MaybeEmpty String -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it so nesessary to make existing Additionally, please, put |
||
NamesRestrictions => | ||
(funs : Funs) -> (vars : Vars) -> (names : UniqNames funs vars) -> | ||
Gen MaybeEmpty (s ** NameIsNew names s) | ||
genNewName fl @{genStr} funs vars names = do | ||
nn@(nm ** _) <- rawNewName fl @{genStr} vars funs names | ||
(funs : Funs) -> (vars : Vars) -> (names : UniqNames l funs vars) -> | ||
Gen MaybeEmpty (s ** NameIsNew l funs vars names s) | ||
genNewName fl genStr funs vars names = do | ||
nn@(nm ** _) <- rawNewName fl l @{const genStr} funs vars names | ||
if reservedKeywords `contains'` nm | ||
then assert_total $ genNewName fl @{genStr} funs vars names -- we could reduce fuel instead of `assert_total` | ||
then assert_total $ genNewName fl genStr funs vars names -- we could reduce fuel instead of `assert_total` | ||
else pure nn | ||
|
||
varName : UniqNames funs vars => IndexIn vars -> String | ||
varName : UniqNames l funs vars => IndexIn vars -> String | ||
varName @{JustNew @{ss} _} i = varName @{ss} i | ||
varName @{NewFun @{ss} _} i = varName @{ss} i | ||
varName @{NewVar s} Here = s | ||
varName @{NewVar @{ss} _} (There i) = varName @{ss} i | ||
|
||
funName : UniqNames funs vars => IndexIn funs -> String | ||
funName : UniqNames l funs vars => IndexIn funs -> String | ||
funName @{JustNew @{ss} _} i = funName @{ss} i | ||
funName @{NewFun s} Here = s | ||
funName @{NewFun @{ss} _} (There i) = funName @{ss} i | ||
funName @{NewVar @{ss} _} i = funName @{ss} i | ||
|
||
isFunInfix : UniqNames funs vars => IndexIn funs -> Bool | ||
isFunInfix : UniqNames l funs vars => IndexIn funs -> Bool | ||
isFunInfix @{JustNew @{ss} _} i = isFunInfix @{ss} i | ||
isFunInfix @{NewFun {isInfix} _} Here = isInfix | ||
isFunInfix @{NewFun @{ss} s} (There i) = isFunInfix @{ss} i | ||
isFunInfix @{NewVar @{ss} s} i = isFunInfix @{ss} i | ||
|
||
isFunPure : UniqNames l funs vars -> IndexIn funs -> Bool | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe, signatures of existing projection functions like |
||
isFunPure (JustNew @{ss} _) i = isFunPure ss i | ||
isFunPure (NewFun {isPure} _) Here = isPure | ||
isFunPure (NewFun @{ss} _) (There i) = isFunPure ss i | ||
isFunPure (NewVar @{ss} _) i = isFunPure ss i | ||
|
||
-- Returned vect has a reverse order; I'd like some `SnocVect` here. | ||
newVars : (newNames : Gen0 String) => | ||
NamesRestrictions => | ||
{funs : _} -> {vars : _} -> | ||
newVars : NamesRestrictions => | ||
{l : _} -> {funs : _} -> {vars : _} -> | ||
Fuel -> | ||
(extraVars : _) -> UniqNames funs vars -> | ||
Gen0 (UniqNames funs (vars ++ extraVars), Vect extraVars.length (String, Ty)) | ||
newVars _ [<] names = pure (names, []) | ||
newVars fl (vs:<v) names = do | ||
(names', vts) <- newVars fl vs names | ||
(nm ** _) <- genNewName fl _ _ names' | ||
(newNames : Gen0 String) -> | ||
(extraVars : _) -> UniqNames l funs vars -> | ||
Gen0 (UniqNames l funs (vars ++ extraVars), Vect extraVars.length (String, Ty)) | ||
newVars _ _ [<] names = pure (names, []) | ||
newVars fl newNames (vs:<v) names = do | ||
(names', vts) <- newVars fl newNames vs names | ||
(nm ** _) <- genNewName {l} fl newNames _ _ names' | ||
pure (NewVar @{names'} nm, (nm, v)::vts) | ||
|
||
isNop : Stmts funs vars retTy -> Bool | ||
|
@@ -104,9 +142,8 @@ wrapBrIf False pre x = pre `vappend` indent' 2 x | |
wrapBrIf True pre x = ifMultiline (pre <++> "{" <++> x <++> "}") (vsep [pre <++> "{", indent' 2 x, "}"]) | ||
|
||
public export | ||
0 PP : Type | ||
PP = {funs : _} -> {vars : _} -> {retTy : _} -> {opts : _} -> | ||
(names : UniqNames funs vars) => | ||
(newNames : Gen0 String) => | ||
Fuel -> | ||
Stmts funs vars retTy -> Gen0 $ Doc opts | ||
0 PP : SupportedLanguage -> Type | ||
PP language = {funs : _} -> {vars : _} -> {retTy : _} -> {opts : _} -> | ||
(names : UniqNames language funs vars) => | ||
Fuel -> | ||
Stmts funs vars retTy -> Gen0 $ Doc opts |
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.
Could you please put
where
in the line withdata
if it fits the line length