@@ -5,6 +5,10 @@ import mlscript.utils._
5
5
import mlscript .utils .shorthands ._
6
6
import mlscript .compiler .utils ._
7
7
8
+ import scala .language .implicitConversions
9
+
10
+ given Conversion [String , Document ] = raw
11
+
8
12
enum Specifier :
9
13
case Extern
10
14
case Static
@@ -50,16 +54,16 @@ enum Type:
50
54
51
55
def toDocument (extraTypename : Bool = false ): Document =
52
56
def aux (x : Type ): Document = x match
53
- case Prim (name) => name |> raw
54
- case Ptr (inner) => aux(inner) <#> raw( " *" )
55
- case Ref (inner) => aux(inner) <#> raw( " &" )
56
- case Array (inner, size) => aux(inner) <#> raw( " [" ) <#> size.fold(raw(" " ))(x => x.toString |> raw ) <#> raw( " ]" )
57
- case FuncPtr (ret, args) => aux(ret) <#> raw( " (" ) <#> Type .toDocuments(args, sep = raw( " , " )) <#> raw( " )" )
58
- case Struct (name) => raw( s " struct $name" )
59
- case Enum (name) => raw( s " enum $name" )
60
- case Template (name, args) => raw( s " $name" ) <#> raw( " <" ) <#> Type .toDocuments(args, sep = raw( " , " )) <#> raw( " >" )
61
- case Var (name) => name |> raw
62
- case Qualifier (inner, qual) => aux(inner) <:> raw( qual)
57
+ case Prim (name) => name
58
+ case Ptr (inner) => aux(inner) <#> " *"
59
+ case Ref (inner) => aux(inner) <#> " &"
60
+ case Array (inner, size) => aux(inner) <#> " [" <#> size.fold(raw(" " ))(x => x.toString) <#> " ]"
61
+ case FuncPtr (ret, args) => aux(ret) <#> " (" <#> Type .toDocuments(args, sep = " , " ) <#> " )"
62
+ case Struct (name) => s " struct $name"
63
+ case Enum (name) => s " enum $name"
64
+ case Template (name, args) => s " $name" <#> " <" <#> Type .toDocuments(args, sep = " , " ) <#> " >"
65
+ case Var (name) => name
66
+ case Qualifier (inner, qual) => aux(inner) <:> qual
63
67
aux(this )
64
68
65
69
override def toString : Str = toDocument().print
@@ -87,28 +91,29 @@ enum Stmt:
87
91
case AutoBind (lhs, rhs) =>
88
92
lhs match
89
93
case Nil => rhs.toDocument
90
- case x :: Nil => raw( " auto" ) <:> raw(x) <:> raw( " =" ) <:> rhs.toDocument <#> raw( " ;" )
91
- case _ => raw( " auto" ) <:> raw( lhs.mkString(" [" , " ," , " ]" )) <:> raw( " =" ) <:> rhs.toDocument <#> raw( " ;" )
92
- case Assign (lhs, rhs) => raw( lhs) <#> raw( " = " ) <#> rhs.toDocument <#> raw( " ;" )
93
- case Return (expr) => raw( " return " ) <#> expr.toDocument <#> raw( " ;" )
94
+ case x :: Nil => " auto" <:> x <:> " =" <:> rhs.toDocument <#> " ;"
95
+ case _ => " auto" <:> lhs.mkString(" [" , " ," , " ]" ) <:> " =" <:> rhs.toDocument <#> " ;"
96
+ case Assign (lhs, rhs) => lhs <#> " = " <#> rhs.toDocument <#> " ;"
97
+ case Return (expr) => " return " <#> expr.toDocument <#> " ;"
94
98
case If (cond, thenStmt, elseStmt) =>
95
- raw( " if (" ) <#> cond.toDocument <#> raw( " )" ) <#> thenStmt.toDocument <:> elseStmt.fold(raw(" " ))(x => raw( " else" ) <:> x.toDocument)
99
+ " if (" <#> cond.toDocument <#> " )" <#> thenStmt.toDocument <:> elseStmt.fold(raw(" " ))(x => " else" <:> x.toDocument)
96
100
case While (cond, body) =>
97
- raw( " while (" ) <#> cond.toDocument <#> raw( " )" ) <#> body.toDocument
101
+ " while (" <#> cond.toDocument <#> " )" <#> body.toDocument
98
102
case For (init, cond, update, body) =>
99
- raw( " for (" ) <#> init.toDocument <#> raw( " ; " ) <#> cond.toDocument <#> raw( " ; " ) <#> update.toDocument <#> raw( " )" ) <#> body.toDocument
100
- case ExprStmt (expr) => expr.toDocument <#> raw( " ;" )
101
- case Break => raw( " break;" )
102
- case Continue => raw( " continue;" )
103
+ " for (" <#> init.toDocument <#> " ; " <#> cond.toDocument <#> " ; " <#> update.toDocument <#> " )" <#> body.toDocument
104
+ case ExprStmt (expr) => expr.toDocument <#> " ;"
105
+ case Break => " break;"
106
+ case Continue => " continue;"
103
107
case Block (decl, stmts) =>
104
- stack(raw(" {" ),
108
+ stack(
109
+ " {" ,
105
110
Stmt .toDocuments(decl, stmts) |> indent,
106
- raw( " }" ) )
111
+ " }" )
107
112
case Switch (expr, cases) =>
108
- raw( " switch (" ) <#> expr.toDocument <#> raw( " )" ) <#> raw( " {" ) <#> stack_list(cases.map {
109
- case (cond, stmt) => raw( " case " ) <#> cond.toDocument <#> raw( " :" ) <#> stmt.toDocument
110
- }) <#> raw( " }" )
111
- case Raw (stmt) => stmt |> raw
113
+ " switch (" <#> expr.toDocument <#> " )" <#> " {" <#> stack_list(cases.map {
114
+ case (cond, stmt) => " case " <#> cond.toDocument <#> " :" <#> stmt.toDocument
115
+ }) <#> " }"
116
+ case Raw (stmt) => stmt
112
117
aux(this )
113
118
114
119
object Expr :
@@ -135,18 +140,18 @@ enum Expr:
135
140
136
141
def toDocument : Document =
137
142
def aux (x : Expr ): Document = x match
138
- case Var (name) => name |> raw
139
- case IntLit (value) => value.toString |> raw
140
- case DoubleLit (value) => value.toString |> raw
141
- case StrLit (value) => s " \" $value\" " |> raw // need more reliable escape utils
142
- case CharLit (value) => s " ' $ value' " |> raw
143
- case Call (func, args) => aux(func) <#> raw( " (" ) <#> Expr .toDocuments(args, sep = raw( " , " )) <#> raw( " )" )
144
- case Member (expr, member) => aux(expr) <#> raw( " ->" ) <#> raw( member)
145
- case Index (expr, index) => aux(expr) <#> raw( " [" ) <#> aux(index) <#> raw( " ]" )
146
- case Unary (op, expr) => raw( " (" ) <#> raw(op) <#> aux(expr) <#> raw( " )" )
147
- case Binary (op, lhs, rhs) => raw( " (" ) <#> aux(lhs) <#> raw(op) <#> aux(rhs) <#> raw( " )" )
148
- case Initializer (exprs) => raw( " {" ) <#> Expr .toDocuments(exprs, sep = raw( " , " )) <#> raw( " }" )
149
- case Constructor (name, init) => raw( name) <#> init.toDocument
143
+ case Var (name) => name
144
+ case IntLit (value) => value.toString
145
+ case DoubleLit (value) => value.toString
146
+ case StrLit (value) => s " \" $value\" " // need more reliable escape utils
147
+ case CharLit (value) => value.toInt.toString
148
+ case Call (func, args) => aux(func) <#> " (" <#> Expr .toDocuments(args, sep = " , " ) <#> " )"
149
+ case Member (expr, member) => aux(expr) <#> " ->" <#> member
150
+ case Index (expr, index) => aux(expr) <#> " [" <#> aux(index) <#> " ]"
151
+ case Unary (op, expr) => " (" <#> op <#> aux(expr) <#> " )"
152
+ case Binary (op, lhs, rhs) => " (" <#> aux(lhs) <#> op <#> aux(rhs) <#> " )"
153
+ case Initializer (exprs) => " {" <#> Expr .toDocuments(exprs, sep = " , " ) <#> " }"
154
+ case Constructor (name, init) => name <#> init.toDocument
150
155
aux(this )
151
156
152
157
case class CompilationUnit (includes : Ls [Str ], decls : Ls [Decl ], defs : Ls [Def ]):
@@ -161,10 +166,10 @@ enum Decl:
161
166
162
167
def toDocument : Document =
163
168
def aux (x : Decl ): Document = x match
164
- case StructDecl (name) => raw( s " struct $name; " )
165
- case EnumDecl (name) => raw( s " enum $name; " )
166
- case FuncDecl (ret, name, args) => ret.toDocument() <#> raw( s " $name( " ) <#> Type .toDocuments(args, sep = raw( " , " )) <#> raw( " );" )
167
- case VarDecl (name, typ) => typ.toDocument() <#> raw( s " $name; " )
169
+ case StructDecl (name) => s " struct $name; "
170
+ case EnumDecl (name) => s " enum $name; "
171
+ case FuncDecl (ret, name, args) => ret.toDocument() <#> s " $name( " <#> Type .toDocuments(args, sep = " , " ) <#> " );"
172
+ case VarDecl (name, typ) => typ.toDocument() <#> s " $name; "
168
173
aux(this )
169
174
170
175
enum Def :
@@ -178,21 +183,21 @@ enum Def:
178
183
def aux (x : Def ): Document = x match
179
184
case StructDef (name, fields, inherit, defs) =>
180
185
stack(
181
- raw( s " struct $name" ) <#> (if inherit.nonEmpty then raw( " : public" ) <:> raw( inherit.get.mkString(" , " )) else raw( " " ) ) <:> raw( " {" ) ,
186
+ s " struct $name" <#> (if inherit.nonEmpty then " : public" <:> inherit.get.mkString(" , " ) else " " ) <:> " {" ,
182
187
stack_list(fields.map {
183
- case (name, typ) => typ.toDocument() <#> raw( " " ) <#> raw( name) <#> raw( " ;" )
188
+ case (name, typ) => typ.toDocument() <#> " " <#> name <#> " ;"
184
189
}) |> indent,
185
190
stack_list(defs.map(_.toDocument)) |> indent,
186
- raw( " };" )
191
+ " };"
187
192
)
188
193
case EnumDef (name, fields) =>
189
- raw( s " enum $name" ) <:> raw( " {" ) <#> stack_list(fields.map {
190
- case (name, value) => value.fold(raw( s " $name" )) (x => raw( s " $name = $x" ) )
191
- }) <#> raw( " };" )
194
+ s " enum $name" <:> " {" <#> stack_list(fields.map {
195
+ case (name, value) => value.fold(s " $name" )(x => s " $name = $x" )
196
+ }) <#> " };"
192
197
case FuncDef (specret, name, args, body, or, virt) =>
193
- (if virt then raw( " virtual " ) else raw( " " ) )
194
- <#> specret.toDocument() <#> raw( s " $name( " ) <#> Type .toDocuments(args, sep = raw( " , " )) <#> raw( " )" ) <#> (if or then raw( " override" ) else raw( " " ) ) <#> body.toDocument
198
+ (if virt then " virtual " else " " )
199
+ <#> specret.toDocument() <#> s " $name( " <#> Type .toDocuments(args, sep = " , " ) <#> " )" <#> (if or then " override" else " " ) <#> body.toDocument
195
200
case VarDef (typ, name, init) =>
196
- typ.toDocument() <#> raw( s " $name" ) <#> init.fold(raw(" " ))(x => raw( " = " ) <#> x.toDocument) <#> raw(" ;" )
197
- case RawDef (x) => x |> raw
201
+ typ.toDocument() <#> s " $name" <#> init.fold(raw(" " ))(x => " = " <#> x.toDocument) <#> raw(" ;" )
202
+ case RawDef (x) => x
198
203
aux(this )
0 commit comments