Skip to content

Commit

Permalink
add prebuilt call & assignments to reduce code size
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Oct 30, 2024
1 parent 10524eb commit 3d6e0a9
Show file tree
Hide file tree
Showing 4 changed files with 243 additions and 428 deletions.
22 changes: 16 additions & 6 deletions compiler/cbuilderexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,26 @@ template addCall(builder: var Builder, call: out CallBuilder, callee: Snippet, b
body
finishCallBuilder(builder, call)

proc addNullaryCall(builder: var Builder, callee: Snippet) =
builder.add(callee)
builder.add("()")

proc addUnaryCall(builder: var Builder, callee: Snippet, arg: Snippet) =
proc addCall(builder: var Builder, callee: Snippet, args: varargs[Snippet]) =
builder.add(callee)
builder.add("(")
builder.add(arg)
if args.len != 0:
builder.add(args[0])
for i in 1 ..< args.len:
builder.add(", ")
builder.add(args[i])
builder.add(")")

proc cCall(callee: Snippet, args: varargs[Snippet]): Snippet =
result = callee
result.add("(")
if args.len != 0:
result.add(args[0])
for i in 1 ..< args.len:
result.add(", ")
result.add(args[i])
result.add(")")

proc addSizeof(builder: var Builder, val: Snippet) =
builder.add("sizeof(")
builder.add(val)
Expand Down
37 changes: 31 additions & 6 deletions compiler/cbuilderstmts.nim
Original file line number Diff line number Diff line change
@@ -1,32 +1,57 @@
template addAssignment(builder: var Builder, lhs: Snippet, valueBody: typed) =
template addAssignmentWithValue(builder: var Builder, lhs: Snippet, valueBody: typed) =
builder.add(lhs)
builder.add(" = ")
valueBody
builder.add(";\n")

template addFieldAssignment(builder: var Builder, lhs: Snippet, name: string, valueBody: typed) =
template addFieldAssignmentWithValue(builder: var Builder, lhs: Snippet, name: string, valueBody: typed) =
builder.add(lhs)
builder.add("." & name & " = ")
valueBody
builder.add(";\n")

template addDerefFieldAssignment(builder: var Builder, lhs: Snippet, name: string, valueBody: typed) =
template addAssignment(builder: var Builder, lhs, rhs: Snippet) =
builder.addAssignmentWithValue(lhs):
builder.add(rhs)

template addFieldAssignment(builder: var Builder, lhs: Snippet, name: string, rhs: Snippet) =
builder.addFieldAssignmentWithValue(lhs, name):
builder.add(rhs)

template addMutualFieldAssignment(builder: var Builder, lhs, rhs: Snippet, name: string) =
builder.addFieldAssignmentWithValue(lhs, name):
builder.add(rhs)
builder.add("." & name)

template addAssignment(builder: var Builder, lhs: Snippet, rhs: int | int64 | uint64 | Int128) =
builder.addAssignmentWithValue(lhs):
builder.addIntValue(rhs)

template addFieldAssignment(builder: var Builder, lhs: Snippet, name: string, rhs: int | int64 | uint64 | Int128) =
builder.addFieldAssignmentWithValue(lhs, name):
builder.addIntValue(rhs)

template addDerefFieldAssignment(builder: var Builder, lhs: Snippet, name: string, rhs: Snippet) =
builder.add(lhs)
builder.add("->" & name & " = ")
valueBody
builder.add(rhs)
builder.add(";\n")

template addSubscriptAssignment(builder: var Builder, lhs: Snippet, index: Snippet, valueBody: typed) =
template addSubscriptAssignment(builder: var Builder, lhs: Snippet, index: Snippet, rhs: Snippet) =
builder.add(lhs)
builder.add("[" & index & "] = ")
valueBody
builder.add(rhs)
builder.add(";\n")

template addStmt(builder: var Builder, stmtBody: typed) =
## makes an expression built by `stmtBody` into a statement
stmtBody
builder.add(";\n")

proc addCallStmt(builder: var Builder, callee: Snippet, args: varargs[Snippet]) =
builder.addStmt():
builder.addCall(callee, args)

# XXX blocks need indent tracker in `Builder` object

template addSingleIfStmt(builder: var Builder, cond: Snippet, body: typed) =
Expand Down
Loading

0 comments on commit 3d6e0a9

Please sign in to comment.