1
- package hkmc2 .codegen .cpp
1
+ package hkmc2
2
+ package codegen .cpp
2
3
3
4
import mlscript ._
4
5
import mlscript .utils ._
5
6
import mlscript .utils .shorthands ._
6
-
7
- import hkmc2 .Message .MessageContext
8
- import hkmc2 .document ._
9
-
10
7
import scala .language .implicitConversions
11
8
9
+ import document ._
10
+
12
11
private def raw (x : String ): Document = doc " $x"
13
12
given Conversion [String , Document ] = x => doc " $x"
14
13
@@ -130,14 +129,13 @@ enum Expr:
130
129
case Unary (op : Str , expr : Expr )
131
130
case Binary (op : Str , lhs : Expr , rhs : Expr )
132
131
case Initializer (exprs : Ls [Expr ])
133
- case Constructor (name : Str , init : Expr )
134
132
135
133
def toDocument : Document =
136
134
def aux (x : Expr ): Document = x match
137
135
case Var (name) => name
138
136
case IntLit (value) => value.toString
139
137
case DoubleLit (value) => value.toString
140
- case StrLit (value) => s " \" $ value\" " // need more reliable escape utils
138
+ case StrLit (value) => value.escaped
141
139
case CharLit (value) => value.toInt.toString
142
140
case Call (func, args) =>
143
141
doc " ${func.toDocument}( ${Expr .toDocuments(args, sep = " , " )}) "
@@ -151,8 +149,6 @@ enum Expr:
151
149
doc " ( ${lhs.toDocument} $op ${rhs.toDocument}) "
152
150
case Initializer (exprs) =>
153
151
doc " { ${Expr .toDocuments(exprs, sep = " , " )}} "
154
- case Constructor (name, init) =>
155
- doc " $name( ${init.toDocument}) "
156
152
aux(this )
157
153
158
154
case class CompilationUnit (includes : Ls [Str ], decls : Ls [Decl ], defs : Ls [Def ]):
@@ -161,57 +157,63 @@ case class CompilationUnit(includes: Ls[Str], decls: Ls[Decl], defs: Ls[Def]):
161
157
def toDocumentWithoutHidden : Document =
162
158
val hiddenNames : Set [Str ] = Set ()
163
159
defs.filterNot {
164
- case Def .StructDef (name, _, _, _) => hiddenNames.contains(name.stripPrefix(" _mls_" ))
160
+ case Def .StructDef (name, _, _, _, _ ) => hiddenNames.contains(name.stripPrefix(" _mls_" ))
165
161
case _ => false
166
162
}.map(_.toDocument).mkDocument(doc " # " )
167
163
168
164
enum Decl :
169
165
case StructDecl (name : Str )
170
166
case EnumDecl (name : Str )
171
- case FuncDecl (ret : Type , name : Str , args : Ls [Type ])
167
+ case FuncDecl (ret : Type , name : Str , args : Ls [Type ], isOverride : Bool , isVirtual : Bool )
172
168
case VarDecl (name : Str , typ : Type )
173
169
174
170
def toDocument : Document =
175
171
def aux (x : Decl ): Document = x match
176
172
case StructDecl (name) => doc " struct $name; "
177
173
case EnumDecl (name) => doc " enum $name; "
178
- case FuncDecl (ret, name, args) =>
179
- doc " ${ret.toDocument()} $name( ${Type .toDocuments(args, sep = " , " )}); "
174
+ case FuncDecl (ret, name, args, or, virt) =>
175
+ val docVirt = (if virt then doc " virtual " else doc " " )
176
+ val docSpecRet = ret.toDocument()
177
+ val docArgs = Type .toDocuments(args, sep = " , " )
178
+ val docOverride = if or then doc " override " else doc " "
179
+ doc " $docVirt$docSpecRet $name( $docArgs) $docOverride; "
180
180
case VarDecl (name, typ) =>
181
181
doc " ${typ.toDocument()} $name; "
182
182
aux(this )
183
183
184
184
enum Def :
185
- case StructDef (name : Str , fields : Ls [(Str , Type )], inherit : Opt [Ls [Str ]], methods : Ls [Def ] = Ls .empty )
185
+ case StructDef (name : Str , fields : Ls [(Str , Type )], inherit : Opt [Ls [Str ]], methods : Ls [Def ], methodsDecl : Ls [ Decl ] )
186
186
case EnumDef (name : Str , fields : Ls [(Str , Opt [Int ])])
187
- case FuncDef (specret : Type , name : Str , args : Ls [(Str , Type )], body : Stmt .Block , or : Bool = false , virt : Bool = false )
187
+ case FuncDef (specret : Type , name : Str , args : Ls [(Str , Type )], body : Stmt .Block , isOverride : Bool , isVirtual : Bool , in_scope : Opt [ Str ] )
188
188
case VarDef (typ : Type , name : Str , init : Opt [Expr ])
189
189
case RawDef (raw : Str )
190
190
191
191
def toDocument : Document =
192
192
def aux (x : Def ): Document = x match
193
- case StructDef (name, fields, inherit, defs) =>
193
+ case StructDef (name, fields, inherit, defs, decls ) =>
194
194
val docFirst = doc " struct $name${inherit.fold(doc " " )(x => doc " : public ${x.mkDocument(doc " , " )}" )} { "
195
195
val docFields = fields.map {
196
196
case (name, typ) => doc " ${typ.toDocument()} $name; "
197
197
}.mkDocument(doc " # " )
198
198
val docDefs = defs.map(_.toDocument).mkDocument(doc " # " )
199
+ val docDecls = decls.map(_.toDocument).mkDocument(doc " # " )
199
200
val docLast = " };"
200
- doc " $docFirst #{ # $docFields # $docDefs #} # $docLast"
201
+ doc " $docFirst #{ # $docFields # $docDefs # $docDecls # } # $docLast"
201
202
case EnumDef (name, fields) =>
202
203
val docFirst = doc " enum $name { "
203
204
val docFields = fields.map {
204
205
case (name, value) => value.fold(s " $name" )(x => s " $name = $x" )
205
206
}.mkDocument(doc " # " )
206
207
val docLast = " };"
207
208
doc " $docFirst #{ # $docFields #} # $docLast"
208
- case FuncDef (specret, name, args, body, or, virt) =>
209
+ case FuncDef (specret, name, args, body, or, virt, scope ) =>
209
210
val docVirt = (if virt then doc " virtual " else doc " " )
210
211
val docSpecRet = specret.toDocument()
211
212
val docArgs = Type .toDocuments(args, sep = " , " )
212
213
val docOverride = if or then doc " override " else doc " "
213
214
val docBody = body.toDocument
214
- doc " $docVirt$docSpecRet $name( $docArgs) $docOverride ${body.toDocument}"
215
+ val docScope = scope.fold(doc " " )(x => doc " $x:: " )
216
+ doc " $docVirt$docSpecRet $docScope$name( $docArgs) $docOverride ${body.toDocument}"
215
217
case VarDef (typ, name, init) =>
216
218
val docTyp = typ.toDocument()
217
219
val docInit = init.fold(raw(" " ))(x => doc " = ${x.toDocument}" )
0 commit comments