Skip to content

Commit

Permalink
add indents
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Nov 7, 2024
1 parent cec6d0e commit bfd4f99
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 57 deletions.
25 changes: 25 additions & 0 deletions compiler/cbuilderbase.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ type
Snippet* = string
Builder* = object
buf*: string
indents*: int

template newBuilder*(s: string): Builder =
Builder(buf: s)
Expand All @@ -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)

Expand Down
29 changes: 15 additions & 14 deletions compiler/cbuilderdecls.nim
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ 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
builder.addVarHeader(kind)
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) =
Expand All @@ -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) =
Expand All @@ -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
Expand All @@ -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`
Expand All @@ -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
Expand All @@ -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 ")
Expand All @@ -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`,
Expand All @@ -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
Expand Down Expand Up @@ -521,12 +521,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) =
Expand All @@ -552,7 +553,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) =
Expand All @@ -567,4 +568,4 @@ proc addProcVar(builder: var Builder, callConv: TCallingConvention,
builder.add(")")
builder.add(params)
# ensure we are just adding a variable:
builder.add(";\n")
builder.addLineEnd(";")
Loading

0 comments on commit bfd4f99

Please sign in to comment.