[英]pretty printing in haskell with data structure
I am the beginner of Haskell, and I want to make a pretty printing with data structures in Haskell. 我是Haskell的初学者,我想用Haskell中的数据结构进行漂亮的打印。
I have the following data structure: 我有以下数据结构:
https://codeshare.io/5zl9WD https://codeshare.io/5zl9WD
I want to print the following value: 我要打印以下值:
int a(int b, int* c){
return b + *c;
}
by using the following function types: 通过使用以下函数类型:
pFunc :: f -> document
putStrLn $ pretty 40 $ pFunc fun
fun =("a", TInt, [(TInt, "b"),(TPtr TInt, "c")]
, [SReturn (EBinOp "+" (EIdent "b") (EUnOpPre "*" (EIdent "c")))])
document
data structure is as the following: document
数据结构如下:
https://codeshare.io/G64gyg https://codeshare.io/G64gyg
But honestly I don't have any idea. 但老实说,我没有任何想法。 I have many similar simple printings.
我有许多类似的简单印刷品。 If I can have an idea about this printing, the remaining one won't be a problem.
如果我对这种打印有一个想法,那么剩下的就不会有问题了。
But might be the shortest way to print the value above by using document
data type? 但是,这可能是使用
document
数据类型打印上述值的最短方法吗?
Write a pretty-printer function for each of the types in the abstract-syntax-tree. 为abstract-syntax-tree中的每种类型编写一个漂亮的打印机函数。 Each function will have type:
每个函数将具有以下类型:
pretty<Foo> :: <Foo> -> Document
where you replace <Foo>
with the type name. 在其中将
<Foo>
替换为类型名称。 Work bottom up, so you can test each function as you write it. 自下而上,因此您可以在编写每个功能时对其进行测试。 ie implement the pretty-printer for
Type
first, since it is needed by the pretty-printer for Stmt
and f
(which should be called Func
or something). 即,首先为
Type
实现pretty-printer,因为Stmt
和f
(应该称为Func
)的漂亮打印机需要它。
For instance: 例如:
prettyType :: Type -> Document
prettyType TVoid = text "void"
prettyType TInt = text "int"
prettyType TFloat = text "float"
prettyType TChar = text "char"
prettyType TPtr t = prettyType t <> text "*"
prettyType TStruct s = text "struct" <+> text s
Follow the same pattern for each of Exp
, Stmt
, Case
, and Func
. 对
Exp
, Stmt
, Case
和Func
遵循相同的模式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.