From 41ddeebe7410df9f0eda937fa432eae32e79b03d Mon Sep 17 00:00:00 2001 From: metagn Date: Fri, 8 Nov 2024 01:29:31 +0300 Subject: [PATCH] add indents --- compiler/cbuilderbase.nim | 25 ++++++++++ compiler/cbuilderdecls.nim | 35 +++++++------- compiler/cbuilderstmts.nim | 94 ++++++++++++++++++++------------------ compiler/ccgexprs.nim | 2 +- 4 files changed, 93 insertions(+), 63 deletions(-) diff --git a/compiler/cbuilderbase.nim b/compiler/cbuilderbase.nim index 6be24495c648..8cd302a793a8 100644 --- a/compiler/cbuilderbase.nim +++ b/compiler/cbuilderbase.nim @@ -4,6 +4,7 @@ type Snippet* = string Builder* = object buf*: string + indents*: int template newBuilder*(s: string): Builder = Builder(buf: s) @@ -17,6 +18,30 @@ proc add*(builder: var Builder, s: string) = proc add*(builder: var Builder, s: char) = builder.buf.add(s) +proc addNewline*(builder: var Builder) = + builder.add('\n') + for i in 0 ..< builder.indents: + builder.add('\t') + +proc addLineEnd*(builder: var Builder, s: string) = + builder.add(s) + builder.addNewline() + +proc addLineEndIndent*(builder: var Builder, s: string) = + inc builder.indents + builder.add(s) + builder.addNewline() + +proc addDedent*(builder: var Builder, s: string) = + if builder.buf.len > 0 and builder.buf[^1] == '\t': + builder.buf.setLen(builder.buf.len - 1) + builder.add(s) + dec builder.indents + +proc addLineEndDedent*(builder: var Builder, s: string) = + builder.addDedent(s) + builder.addNewline() + proc addIntValue*(builder: var Builder, val: int) = builder.buf.addInt(val) diff --git a/compiler/cbuilderdecls.nim b/compiler/cbuilderdecls.nim index 4ddd2ead790d..28d28579a4ca 100644 --- a/compiler/cbuilderdecls.nim +++ b/compiler/cbuilderdecls.nim @@ -32,7 +32,7 @@ proc addVar(builder: var Builder, kind: VarKind = Local, name: string, typ: Snip if initializer.len != 0: builder.add(" = ") builder.add(initializer) - builder.add(";\n") + builder.addLineEnd(";") template addVarWithType(builder: var Builder, kind: VarKind = Local, name: string, body: typed) = ## adds a variable declaration to the builder, with the `body` building the type @@ -40,7 +40,7 @@ template addVarWithType(builder: var Builder, kind: VarKind = Local, name: strin body builder.add(" ") builder.add(name) - builder.add(";\n") + builder.addLineEnd(";") template addVarWithInitializer(builder: var Builder, kind: VarKind = Local, name: string, typ: Snippet, initializerBody: typed) = @@ -52,7 +52,7 @@ template addVarWithInitializer(builder: var Builder, kind: VarKind = Local, name builder.add(name) builder.add(" = ") initializerBody - builder.add(";\n") + builder.addLineEnd(";") template addVarWithTypeAndInitializer(builder: var Builder, kind: VarKind = Local, name: string, typeBody, initializerBody: typed) = @@ -64,7 +64,7 @@ template addVarWithTypeAndInitializer(builder: var Builder, kind: VarKind = Loca builder.add(name) builder.add(" = ") initializerBody - builder.add(";\n") + builder.addLineEnd(";") proc addArrayVar(builder: var Builder, kind: VarKind = Local, name: string, elementType: Snippet, len: int, initializer: Snippet = "") = ## adds an array variable declaration to the builder @@ -78,7 +78,7 @@ proc addArrayVar(builder: var Builder, kind: VarKind = Local, name: string, elem if initializer.len != 0: builder.add(" = ") builder.add(initializer) - builder.add(";\n") + builder.addLineEnd(";") template addArrayVarWithInitializer(builder: var Builder, kind: VarKind = Local, name: string, elementType: Snippet, len: int, body: typed) = ## adds an array variable declaration to the builder with the initializer built according to `body` @@ -90,7 +90,7 @@ template addArrayVarWithInitializer(builder: var Builder, kind: VarKind = Local, builder.addIntValue(len) builder.add("] = ") body - builder.add(";\n") + builder.addLineEnd(";") template addTypedef(builder: var Builder, name: string, typeBody: typed) = ## adds a typedef declaration to the builder with name `name` and type as @@ -99,7 +99,7 @@ template addTypedef(builder: var Builder, name: string, typeBody: typed) = typeBody builder.add(" ") builder.add(name) - builder.add(";\n") + builder.addLineEnd(";") proc addProcTypedef(builder: var Builder, callConv: TCallingConvention, name: string, rettype, params: Snippet) = builder.add("typedef ") @@ -110,7 +110,7 @@ proc addProcTypedef(builder: var Builder, callConv: TCallingConvention, name: st builder.add(name) builder.add(")") builder.add(params) - builder.add(";\n") + builder.addLineEnd(";") template addArrayTypedef(builder: var Builder, name: string, len: BiggestInt, typeBody: typed) = ## adds an array typedef declaration to the builder with name `name`, @@ -121,7 +121,7 @@ template addArrayTypedef(builder: var Builder, name: string, len: BiggestInt, ty builder.add(name) builder.add("[") builder.addIntValue(len) - builder.add("];\n") + builder.addLineEnd("];") type StructInitializerKind = enum @@ -525,12 +525,13 @@ proc addProcHeader(builder: var Builder, m: BModule, prc: PSym, name: string, pa builder.add(" __attribute__((noreturn))") proc finishProcHeaderAsProto(builder: var Builder) = - builder.add(";\n") + builder.addLineEnd(";") template finishProcHeaderWithBody(builder: var Builder, body: typed) = - builder.add(" {\n") + builder.addLineEndIndent(" {") body - builder.add("}\n\n") + builder.addLineEndDedent("}") + builder.addNewline proc addProcVar(builder: var Builder, m: BModule, prc: PSym, name: string, params, rettype: Snippet, isStatic = false, ignoreAttributes = false) = @@ -556,7 +557,7 @@ proc addProcVar(builder: var Builder, m: BModule, prc: PSym, name: string, param if noreturn and hasAttribute in extccomp.CC[m.config.cCompiler].props: builder.add(" __attribute__((noreturn))") # ensure we are just adding a variable: - builder.add(";\n") + builder.addLineEnd(";") proc addProcVar(builder: var Builder, callConv: TCallingConvention, name: string, params, rettype: Snippet, isStatic = false) = @@ -571,7 +572,7 @@ proc addProcVar(builder: var Builder, callConv: TCallingConvention, builder.add(")") builder.add(params) # ensure we are just adding a variable: - builder.add(";\n") + builder.addLineEnd(";") type VarInitializerKind = enum Assignment, CppConstructor @@ -583,7 +584,7 @@ proc addVar(builder: var Builder, m: BModule, s: PSym, name: string, typ: Snippe if initializerKind == Assignment: builder.add(" = ") builder.add(initializer) - builder.add(";\n") + builder.addLineEnd(";") return if s.kind in {skLet, skVar, skField, skForVar} and s.alignment > 0: builder.add("NIM_ALIGN(" & $s.alignment & ") ") @@ -606,7 +607,7 @@ proc addVar(builder: var Builder, m: BModule, s: PSym, name: string, typ: Snippe if initializerKind == Assignment: builder.add(" = ") builder.add(initializer) - builder.add(";\n") + builder.addLineEnd(";") proc addInclude(builder: var Builder, value: Snippet) = - builder.add("#include " & value & "\n") + builder.addLineEnd("#include " & value) diff --git a/compiler/cbuilderstmts.nim b/compiler/cbuilderstmts.nim index ac74dbfa37f7..f01851c6e7ea 100644 --- a/compiler/cbuilderstmts.nim +++ b/compiler/cbuilderstmts.nim @@ -2,13 +2,13 @@ template addAssignmentWithValue(builder: var Builder, lhs: Snippet, valueBody: t builder.add(lhs) builder.add(" = ") valueBody - builder.add(";\n") + builder.addLineEnd(";") template addFieldAssignmentWithValue(builder: var Builder, lhs: Snippet, name: string, valueBody: typed) = builder.add(lhs) builder.add("." & name & " = ") valueBody - builder.add(";\n") + builder.addLineEnd(";") template addAssignment(builder: var Builder, lhs, rhs: Snippet) = builder.addAssignmentWithValue(lhs): @@ -35,18 +35,18 @@ template addDerefFieldAssignment(builder: var Builder, lhs: Snippet, name: strin builder.add(lhs) builder.add("->" & name & " = ") builder.add(rhs) - builder.add(";\n") + builder.addLineEnd(";") template addSubscriptAssignment(builder: var Builder, lhs: Snippet, index: Snippet, rhs: Snippet) = builder.add(lhs) builder.add("[" & index & "] = ") builder.add(rhs) - builder.add(";\n") + builder.addLineEnd(";") template addStmt(builder: var Builder, stmtBody: typed) = ## makes an expression built by `stmtBody` into a statement stmtBody - builder.add(";\n") + builder.addLineEnd(";") proc addCallStmt(builder: var Builder, callee: Snippet, args: varargs[Snippet]) = builder.addStmt(): @@ -57,23 +57,23 @@ proc addCallStmt(builder: var Builder, callee: Snippet, args: varargs[Snippet]) template addSingleIfStmt(builder: var Builder, cond: Snippet, body: typed) = builder.add("if (") builder.add(cond) - builder.add(") {\n") + builder.addLineEndIndent(") {") body - builder.add("}\n") + builder.addLineEndDedent("}") template addSingleIfStmtWithCond(builder: var Builder, condBody: typed, body: typed) = builder.add("if (") condBody - builder.add(") {\n") + builder.addLineEndIndent(") {") body - builder.add("}\n") + builder.addLineEndDedent("}") proc initIfStmt(builder: var Builder): IfBuilder = IfBuilder(state: WaitingIf) proc finishIfStmt(builder: var Builder, stmt: IfBuilder) = assert stmt.state != InBlock - builder.add("\n") + builder.addNewline() template addIfStmt(builder: var Builder, stmt: out IfBuilder, body: typed) = stmt = initIfStmt(builder) @@ -88,16 +88,16 @@ proc initElifBranch(builder: var Builder, stmt: var IfBuilder, cond: Snippet) = builder.add(" else if (") else: assert false, $stmt.state builder.add(cond) - builder.add(") {\n") + builder.addLineEndIndent(") {") stmt.state = InBlock proc initElseBranch(builder: var Builder, stmt: var IfBuilder) = assert stmt.state == WaitingElseIf, $stmt.state - builder.add(" else {\n") + builder.addLineEndIndent(" else {") stmt.state = InBlock proc finishBranch(builder: var Builder, stmt: var IfBuilder) = - builder.add("}") + builder.addDedent("}") stmt.state = WaitingElseIf template addElifBranch(builder: var Builder, stmt: var IfBuilder, cond: Snippet, body: typed) = @@ -124,7 +124,7 @@ proc initForRange(builder: var Builder, i, start, bound: Snippet, inclusive: boo builder.add(bound) builder.add("; ") builder.add(i) - builder.add("++) {\n") + builder.addLineEndIndent("++) {") proc initForStep(builder: var Builder, i, start, bound, step: Snippet, inclusive: bool = false) = builder.add("for (") @@ -142,10 +142,10 @@ proc initForStep(builder: var Builder, i, start, bound, step: Snippet, inclusive builder.add(i) builder.add(" += ") builder.add(step) - builder.add(") {\n") + builder.addLineEndIndent(") {") proc finishFor(builder: var Builder) {.inline.} = - builder.add("}\n") + builder.addLineEndDedent("}") template addForRangeExclusive(builder: var Builder, i, start, bound: Snippet, body: typed) = initForRange(builder, i, start, bound, false) @@ -160,15 +160,16 @@ template addForRangeInclusive(builder: var Builder, i, start, bound: Snippet, bo template addSwitchStmt(builder: var Builder, val: Snippet, body: typed) = builder.add("switch (") builder.add(val) - builder.add(") {\n") + builder.addLineEnd(") {") # no indent body - builder.add("}\n") + builder.addLineEnd("}") template addSingleSwitchCase(builder: var Builder, val: Snippet, body: typed) = builder.add("case ") builder.add(val) - builder.add(":\n") + builder.addLineEndIndent(":") body + builder.addLineEndDedent("") type SwitchCaseState = enum @@ -182,7 +183,7 @@ proc addCase(builder: var Builder, info: var SwitchCaseBuilder, val: Snippet) = info.state = Of builder.add("case ") builder.add(val) - builder.add(":\n") + builder.addLineEndIndent(":") proc addCaseRange(builder: var Builder, info: var SwitchCaseBuilder, first, last: Snippet) = if info.state != Of: @@ -192,42 +193,44 @@ proc addCaseRange(builder: var Builder, info: var SwitchCaseBuilder, first, last builder.add(first) builder.add(" ... ") builder.add(last) - builder.add(":\n") + builder.addLineEndIndent(":") proc addCaseElse(builder: var Builder, info: var SwitchCaseBuilder) = assert info.state == None info.state = Else - builder.add("default:\n") + builder.addLineEndIndent("default:") template addSwitchCase(builder: var Builder, info: out SwitchCaseBuilder, caseBody, body: typed) = info = SwitchCaseBuilder(state: None) caseBody info.state = Finished body + builder.addLineEndDedent("") template addSwitchElse(builder: var Builder, body: typed) = - builder.add("default:\n") + builder.addLineEndIndent("default:") body + builder.addLineEndDedent("") proc addBreak(builder: var Builder) = - builder.add("break;\n") + builder.addLineEnd("break;") type ScopeBuilder = object inside: bool proc initScope(builder: var Builder): ScopeBuilder = - builder.add("{\n") + builder.addLineEndIndent("{") result = ScopeBuilder(inside: true) proc finishScope(builder: var Builder, scope: var ScopeBuilder) = assert scope.inside, "scope not inited" - builder.add("}\n") + builder.addLineEndDedent("}") scope.inside = false template addScope(builder: var Builder, body: typed) = - builder.add("{\n") + builder.addLineEndIndent("{") body - builder.add("}\n") + builder.addLineEndDedent("}") type WhileBuilder = object inside: bool @@ -235,50 +238,50 @@ type WhileBuilder = object proc initWhileStmt(builder: var Builder, cond: Snippet): WhileBuilder = builder.add("while (") builder.add(cond) - builder.add(") {\n") + builder.addLineEndIndent(") {") result = WhileBuilder(inside: true) proc finishWhileStmt(builder: var Builder, stmt: var WhileBuilder) = assert stmt.inside, "while stmt not inited" - builder.add("}\n") + builder.addLineEndDedent("}") stmt.inside = false template addWhileStmt(builder: var Builder, cond: Snippet, body: typed) = builder.add("while (") builder.add(cond) - builder.add(") {\n") + builder.addLineEndIndent(") {") body - builder.add("}\n") + builder.addLineEndDedent("}") proc addLabel(builder: var Builder, name: TLabel) = builder.add(name) - builder.add(": ;\n") + builder.addLineEnd(": ;") proc addReturn(builder: var Builder) = - builder.add("return;\n") + builder.addLineEnd("return;") proc addReturn(builder: var Builder, value: Snippet) = builder.add("return ") builder.add(value) - builder.add(";\n") + builder.addLineEnd(";") proc addGoto(builder: var Builder, label: TLabel) = builder.add("goto ") builder.add(label) - builder.add(";\n") + builder.addLineEnd(";") proc addComputedGoto(builder: var Builder, value: Snippet) = builder.add("goto *") builder.add(value) - builder.add(";\n") + builder.addLineEnd(";") proc addIncr(builder: var Builder, val: Snippet) = builder.add(val) - builder.add("++;\n") + builder.addLineEnd("++;") proc addDecr(builder: var Builder, val: Snippet) = builder.add(val) - builder.add("--;\n") + builder.addLineEnd("--;") proc addInPlaceOp(builder: var Builder, binOp: TypedBinaryOp, t: Snippet, a, b: Snippet) = builder.add(a) @@ -286,7 +289,7 @@ proc addInPlaceOp(builder: var Builder, binOp: TypedBinaryOp, t: Snippet, a, b: builder.add(typedBinaryOperators[binOp]) builder.add("= ") builder.add(b) - builder.add(";\n") + builder.addLineEnd(";") proc addInPlaceOp(builder: var Builder, binOp: UntypedBinaryOp, a, b: Snippet) = builder.add(a) @@ -294,7 +297,7 @@ proc addInPlaceOp(builder: var Builder, binOp: UntypedBinaryOp, a, b: Snippet) = builder.add(untypedBinaryOperators[binOp]) builder.add("= ") builder.add(b) - builder.add(";\n") + builder.addLineEnd(";") proc cInPlaceOp(binOp: TypedBinaryOp, t: Snippet, a, b: Snippet): Snippet = result = "" @@ -303,7 +306,7 @@ proc cInPlaceOp(binOp: TypedBinaryOp, t: Snippet, a, b: Snippet): Snippet = result.add(typedBinaryOperators[binOp]) result.add("= ") result.add(b) - result.add(";\n") + result.addLineEnd(";") proc cInPlaceOp(binOp: UntypedBinaryOp, a, b: Snippet): Snippet = result = "" @@ -312,9 +315,10 @@ proc cInPlaceOp(binOp: UntypedBinaryOp, a, b: Snippet): Snippet = result.add(untypedBinaryOperators[binOp]) result.add("= ") result.add(b) - result.add(";\n") + result.addLineEnd(";") template addCPragma(builder: var Builder, val: Snippet) = - builder.add("\n#pragma ") + builder.addNewline() + builder.add("#pragma ") builder.add(val) - builder.add("\n") + builder.addNewline() diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim index 4bf35dbe1c5f..1c4bbd885d17 100644 --- a/compiler/ccgexprs.nim +++ b/compiler/ccgexprs.nim @@ -98,7 +98,7 @@ proc genRawSetData(cs: TBitSet, size: int; result: var Builder) = result.addStructInitializer(setInit, kind = siArray): for i in 0..