Skip to content
This repository was archived by the owner on Apr 5, 2025. It is now read-only.

Commit 244c6f5

Browse files
Refactoring
1 parent 5e0acf9 commit 244c6f5

File tree

2 files changed

+81
-62
lines changed

2 files changed

+81
-62
lines changed

src/peeble/PhpOutput.fs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
open System
44
open System.IO
55
open PhpAst
6-
6+
/// Context to output the PhpAst as Php text
77
type Writer =
88
{ Writer: TextWriter
99
Indent: int
@@ -19,29 +19,34 @@ type Writer =
1919
EmitDeclNumber = defaultArg emitDeclNumber false
2020
}
2121

22+
/// increment indent
2223
let indent ctx =
2324
{ ctx with Indent = ctx.Indent + 1}
2425

25-
26+
/// write spaces for context indent
2627
let writeIndent ctx =
2728
for _ in 1 .. ctx.Indent do
2829
ctx.Writer.Write(" ")
2930

31+
/// write text to context
3032
let write ctx txt =
3133
ctx.Writer.Write(txt: string)
3234

33-
35+
/// write text followed by a newline
3436
let writeln ctx txt =
3537
ctx.Writer.WriteLine(txt: string)
3638

39+
/// write given text with indent
3740
let writei ctx txt =
3841
writeIndent ctx
3942
write ctx txt
4043

44+
/// write text with index and newline
4145
let writeiln ctx txt =
4246
writeIndent ctx
4347
writeln ctx txt
4448

49+
/// write a list of $ prefixed variable separated by commas
4550
let writeVarList ctx vars =
4651
let mutable first = true
4752
for var in vars do
@@ -51,6 +56,8 @@ let writeVarList ctx vars =
5156
write ctx ", "
5257
write ctx "$"
5358
write ctx var
59+
60+
/// write variables separated by commas in a 'use' specification
5461
let writeUseList ctx vars =
5562
let mutable first = true
5663
for var in vars do
@@ -99,6 +106,7 @@ module Precedence =
99106

100107
let clear ctx = { ctx with Precedence = Int32.MaxValue}
101108

109+
/// write given code using specified precedence. Adds enclosing parens if needed.
102110
let withPrecedence ctx prec f =
103111
let useParens = prec > ctx.Precedence || (prec = 14 && ctx.Precedence = 14)
104112
let subCtx = { ctx with Precedence = prec }
@@ -110,6 +118,7 @@ let withPrecedence ctx prec f =
110118
if useParens then
111119
write subCtx ")"
112120

121+
/// write a PhpExp to context recursively
113122
let rec writeExpr ctx expr =
114123
match expr with
115124
| PhpBinaryOp(op, left, right) ->
@@ -235,6 +244,8 @@ let rec writeExpr ctx expr =
235244
else
236245
write ctx " }"
237246
| PhpMacro(macro, args) ->
247+
// this is used for functions marked with the
248+
// Emit attribute
238249
let regex = System.Text.RegularExpressions.Regex("\$(?<n>\d)(?<s>\.\.\.)?")
239250
let matches = regex.Matches(macro)
240251
let mutable pos = 0
@@ -269,6 +280,7 @@ and writeArgs ctx args =
269280
else
270281
write ctx ", "
271282
writeExpr ctx arg
283+
272284
and writeArrayIndex ctx index =
273285
match index with
274286
| PhpArrayString s ->
@@ -281,7 +293,7 @@ and writeArrayIndex ctx index =
281293
| PhpArrayNoIndex ->
282294
()
283295

284-
296+
/// Writes a Php Statement
285297
and writeStatement ctx st =
286298
match st with
287299
| PhpStatement.Return expr ->
@@ -349,6 +361,7 @@ and writeStatement ctx st =
349361
writeln ctx ";"
350362

351363

364+
/// write a Php function
352365
let writeFunc ctx (f: PhpFun) =
353366
writei ctx ""
354367
if f.Static then
@@ -456,6 +469,7 @@ let writeDecl ctx d =
456469
| PhpDeclValue(n,expr) -> writeAssign ctx n expr
457470
| PhpDeclAction(expr) -> writeAction ctx expr
458471

472+
459473
let writeFile ctx (file: PhpFile) =
460474
if ctx.EmitPhpMark then
461475
writeln ctx "<?php"

src/peeble/Transforms.fs

Lines changed: 63 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -99,66 +99,64 @@ let caseName (case: FSharpUnionCase) =
9999
else
100100
entity.CompiledName + "_" + case.Name
101101

102-
103-
let convertUnion (ctx: PhpCompiler) (info: Fable.UnionConstructorInfo) =
104-
if info.Entity.UnionCases.Count = 1 then
105-
let case = info.Entity.UnionCases.[0]
106-
[ let t =
107-
{ Name = case.Name
108-
Fields = [ for e in case.UnionCaseFields do
109-
{ Name = e.Name
110-
Type = convertType e.FieldType } ]
111-
Methods = [
112-
{ PhpFun.Name = "get_FSharpCase"
113-
PhpFun.Args = []
114-
PhpFun.Matchings = []
115-
PhpFun.Static = false
116-
PhpFun.Body =
117-
[ PhpStatement.Return(PhpConst(PhpConstString(case.Name)))] }
118-
{ PhpFun.Name = "CompareTo"
119-
PhpFun.Args = ["other"]
120-
PhpFun.Matchings = []
121-
PhpFun.Static = false
122-
PhpFun.Body =
123-
[ for e in case.UnionCaseFields do
124-
let cmp = PhpVar(ctx.MakeUniqueVar "cmp",None)
125-
match e.FieldType.TypeDefinition.CompiledName with
126-
| "int" ->
127-
Assign(cmp,
128-
PhpTernary( PhpBinaryOp(">",
129-
PhpProp(PhpVar("this",None), Prop.Field { Name = e.Name; Type = convertType e.FieldType }, None),
130-
PhpProp(PhpVar("other", None), Prop.Field { Name = e.Name; Type = convertType e.FieldType }, None) ),
131-
PhpConst(PhpConstNumber 1.),
132-
PhpTernary(
133-
PhpBinaryOp("<",
134-
PhpProp(PhpVar("this",None), Prop.Field { Name = e.Name; Type = convertType e.FieldType }, None),
135-
PhpProp(PhpVar("other", None), Prop.Field { Name = e.Name; Type = convertType e.FieldType }, None)),
136-
PhpConst(PhpConstNumber -1.),
137-
PhpConst(PhpConstNumber 0.)
138-
139-
140-
) ) )
141-
| _ ->
142-
Assign(cmp,
143-
PhpMethod(PhpProp(PhpVar("this",None), Prop.Field { Name = e.Name; Type = convertType e.FieldType }, None),
144-
"CompareTo",
145-
[PhpProp(PhpVar("other", None), Prop.Field { Name = e.Name; Type = convertType e.FieldType }, None) ])
102+
let convertSingleCaseUnion (ctx: PhpCompiler) (case: FSharpUnionCase) =
103+
[ let t =
104+
{ Name = case.Name
105+
Fields = [ for e in case.UnionCaseFields do
106+
{ Name = e.Name
107+
Type = convertType e.FieldType } ]
108+
Methods = [
109+
{ PhpFun.Name = "get_FSharpCase"
110+
PhpFun.Args = []
111+
PhpFun.Matchings = []
112+
PhpFun.Static = false
113+
PhpFun.Body =
114+
[ PhpStatement.Return(PhpConst(PhpConstString(case.Name)))] }
115+
{ PhpFun.Name = "CompareTo"
116+
PhpFun.Args = ["other"]
117+
PhpFun.Matchings = []
118+
PhpFun.Static = false
119+
PhpFun.Body =
120+
[ for e in case.UnionCaseFields do
121+
let cmp = PhpVar(ctx.MakeUniqueVar "cmp",None)
122+
match e.FieldType.TypeDefinition.CompiledName with
123+
| "int" ->
124+
Assign(cmp,
125+
PhpTernary( PhpBinaryOp(">",
126+
PhpProp(PhpVar("this",None), Prop.Field { Name = e.Name; Type = convertType e.FieldType }, None),
127+
PhpProp(PhpVar("other", None), Prop.Field { Name = e.Name; Type = convertType e.FieldType }, None) ),
128+
PhpConst(PhpConstNumber 1.),
129+
PhpTernary(
130+
PhpBinaryOp("<",
131+
PhpProp(PhpVar("this",None), Prop.Field { Name = e.Name; Type = convertType e.FieldType }, None),
132+
PhpProp(PhpVar("other", None), Prop.Field { Name = e.Name; Type = convertType e.FieldType }, None)),
133+
PhpConst(PhpConstNumber -1.),
134+
PhpConst(PhpConstNumber 0.)
135+
146136

147-
)
148-
If(PhpBinaryOp("!=", cmp, PhpConst(PhpConstNumber 0.) ),
149-
[PhpStatement.Return cmp],
150-
[]
137+
) ) )
138+
| _ ->
139+
Assign(cmp,
140+
PhpMethod(PhpProp(PhpVar("this",None), Prop.Field { Name = e.Name; Type = convertType e.FieldType }, None),
141+
"CompareTo",
142+
[PhpProp(PhpVar("other", None), Prop.Field { Name = e.Name; Type = convertType e.FieldType }, None) ])
143+
151144
)
152-
PhpStatement.Return (PhpConst (PhpConstNumber 0.))
153-
]
154-
}
155-
]
156-
Abstract = false
157-
BaseType = None
158-
Interfaces = [ PhpUnion.fSharpUnion; Core.icomparable ]
159-
}
160-
ctx.AddType(t) |> PhpType ]
161-
else
145+
If(PhpBinaryOp("!=", cmp, PhpConst(PhpConstNumber 0.) ),
146+
[PhpStatement.Return cmp],
147+
[]
148+
)
149+
PhpStatement.Return (PhpConst (PhpConstNumber 0.))
150+
]
151+
}
152+
]
153+
Abstract = false
154+
BaseType = None
155+
Interfaces = [ PhpUnion.fSharpUnion; Core.icomparable ]
156+
}
157+
ctx.AddType(t) |> PhpType ]
158+
159+
let convertMultiCaseUnion (ctx: PhpCompiler) (info: Fable.UnionConstructorInfo) =
162160
[ let baseType =
163161
{ Name = info.Entity.CompiledName
164162
Fields = []
@@ -259,6 +257,13 @@ let convertUnion (ctx: PhpCompiler) (info: Fable.UnionConstructorInfo) =
259257
Interfaces = [ Core.icomparable ] }
260258
ctx.AddType(t) |> PhpType ]
261259

260+
261+
let convertUnion (ctx: PhpCompiler) (info: Fable.UnionConstructorInfo) =
262+
if info.Entity.UnionCases.Count = 1 then
263+
convertSingleCaseUnion ctx info.Entity.UnionCases.[0]
264+
else
265+
convertMultiCaseUnion ctx info
266+
262267
let convertRecord (ctx: PhpCompiler) (info: Fable.CompilerGeneratedConstructorInfo) =
263268
[ let t =
264269
{ Name = info.Entity.CompiledName

0 commit comments

Comments
 (0)