forked from omelkonian/agda-minimal-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RustExpr to separate pretty printing from traverse Agda internals (#18)
* add RustExpr to separate pretty printing and creating expression from Agda internals * rename after refactor to RustExpr * rename after refactor to RustExpr - fix * swap unless to when
- Loading branch information
Showing
8 changed files
with
128 additions
and
108 deletions.
There are no files selected for viewing
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
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
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,63 @@ | ||
module Agda.Compiler.Rust.PrettyPrintRustExpr ( prettyPrintRustExpr, moduleHeader ) where | ||
|
||
import Data.List ( intersperse ) | ||
import Agda.Compiler.Rust.CommonTypes ( CompiledDef ) | ||
import Agda.Compiler.Rust.RustExpr ( RustExpr(..), RustElem(..), FunBody ) | ||
|
||
prettyPrintRustExpr :: CompiledDef -> String | ||
prettyPrintRustExpr def = case def of | ||
(TeEnum name fields) -> | ||
"enum" <> exprSeparator | ||
<> name | ||
<> exprSeparator | ||
<> bracket ( | ||
indent -- TODO this to siplistic indentation | ||
<> concat (intersperse ", " fields)) | ||
(TeFun fName (RustElem aName aType) resType fBody) -> | ||
"pub fn" <> exprSeparator | ||
<> fName | ||
<> argList ( | ||
aName | ||
<> typeSeparator <> exprSeparator | ||
<> aType ) | ||
<> exprSeparator <> funReturnTypeSeparator <> exprSeparator <> resType | ||
<> exprSeparator <> bracket ( | ||
-- TODO proper indentation for every line of function body | ||
-- including nested expressions | ||
indent | ||
<> (prettyPrintFunctionBody fBody)) | ||
<> defsSeparator | ||
(TeMod mName defs) -> | ||
moduleHeader mName | ||
<> bracket (combineLines (map prettyPrintRustExpr defs)) | ||
<> defsSeparator | ||
|
||
bracket :: String -> String | ||
bracket str = "{\n" <> str <> "\n}" | ||
|
||
argList :: String -> String | ||
argList str = "(" <> str <> ")" | ||
|
||
indent :: String | ||
indent = " " | ||
|
||
exprSeparator :: String | ||
exprSeparator = " " | ||
|
||
defsSeparator :: String | ||
defsSeparator = "\n" | ||
|
||
typeSeparator :: String | ||
typeSeparator = ":" | ||
|
||
funReturnTypeSeparator :: String | ||
funReturnTypeSeparator = "->" | ||
|
||
combineLines :: [String] -> String | ||
combineLines xs = unlines (filter (not . null) xs) | ||
|
||
prettyPrintFunctionBody :: FunBody -> String | ||
prettyPrintFunctionBody fBody = "return" <> exprSeparator <> fBody <> ";" | ||
|
||
moduleHeader :: String -> String | ||
moduleHeader mName = "mod" <> exprSeparator <> mName <> exprSeparator |
This file was deleted.
Oops, something went wrong.
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,21 @@ | ||
module Agda.Compiler.Rust.RustExpr ( | ||
RustName, | ||
RustType, | ||
RustExpr(..), | ||
RustElem(..), | ||
FunBody | ||
) where | ||
|
||
type RustName = String | ||
type RustType = String | ||
type FunBody = String | ||
|
||
data RustElem = RustElem RustName RustType | ||
deriving ( Show ) | ||
|
||
data RustExpr | ||
= TeMod RustName [RustExpr] | ||
| TeEnum RustName [RustName] | ||
| TeFun RustName RustElem RustType FunBody | ||
| Unhandled RustName String | ||
deriving ( Show ) |
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