Skip to content

Commit

Permalink
cbuilder: ccgexprs sweep part 3 (end) (#24410)
Browse files Browse the repository at this point in the history
This finishes `ccgexprs` minus the sweeping refactors like moving
`NIM_NIL`, `NU8`, `NCSTRING` etc into constants.
  • Loading branch information
metagn authored Nov 6, 2024
1 parent c71de10 commit cfd8f8b
Show file tree
Hide file tree
Showing 5 changed files with 493 additions and 317 deletions.
2 changes: 2 additions & 0 deletions compiler/cbuilderbase.nim
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ template cIntValue*(val: int64): Snippet = $val
template cIntValue*(val: uint64): Snippet = $val
template cIntValue*(val: Int128): Snippet = $val

template cUintValue*(val: uint): Snippet = $val & "U"

import std/formatfloat

proc addFloatValue*(builder: var Builder, val: float) =
Expand Down
6 changes: 6 additions & 0 deletions compiler/cbuilderdecls.nim
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,12 @@ template addAnonUnion(obj: var Builder; body: typed) =
body
obj.add("};\n")

template addUnionType(obj: var Builder; body: typed) =
## adds a union type i.e. `union { ... }` with fields according to `body`
obj.add "union{\n"
body
obj.add("}")

type DeclVisibility = enum
None
Extern
Expand Down
4 changes: 3 additions & 1 deletion compiler/cbuilderexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,12 @@ proc initCallBuilder(builder: var Builder, callee: Snippet): CallBuilder =
builder.add(callee)
builder.add("(")

const cArgumentSeparator = ", "

proc addArgumentSeparator(builder: var Builder) =
# no-op on NIFC
# used by "single argument" builders
builder.add(", ")
builder.add(cArgumentSeparator)

template addArgument(builder: var Builder, call: var CallBuilder, valueBody: typed) =
if call.needsComma:
Expand Down
34 changes: 34 additions & 0 deletions compiler/cbuilderstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,37 @@ template addIncr(builder: var Builder, val: Snippet) =
template addDecr(builder: var Builder, val: Snippet) =
builder.add(val)
builder.add("--;\n")

proc addInPlaceOp(builder: var Builder, binOp: TypedBinaryOp, t: Snippet, a, b: Snippet) =
builder.add(a)
builder.add(' ')
builder.add(typedBinaryOperators[binOp])
builder.add("= ")
builder.add(b)
builder.add(";\n")

proc addInPlaceOp(builder: var Builder, binOp: UntypedBinaryOp, a, b: Snippet) =
builder.add(a)
builder.add(' ')
builder.add(untypedBinaryOperators[binOp])
builder.add("= ")
builder.add(b)
builder.add(";\n")

proc cInPlaceOp(binOp: TypedBinaryOp, t: Snippet, a, b: Snippet): Snippet =
result = ""
result.add(a)
result.add(' ')
result.add(typedBinaryOperators[binOp])
result.add("= ")
result.add(b)
result.add(";\n")

proc cInPlaceOp(binOp: UntypedBinaryOp, a, b: Snippet): Snippet =
result = ""
result.add(a)
result.add(' ')
result.add(untypedBinaryOperators[binOp])
result.add("= ")
result.add(b)
result.add(";\n")
Loading

0 comments on commit cfd8f8b

Please sign in to comment.