-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cbuilder: ccgexprs sweep part 1, basic if stmts (#24381)
Most of what ccgexprs uses is now ported to cbuilder, so this PR makes around ~25% of ccgexprs use it, along with adding `if` stmts (no `while`/`switch` and `for` which is only used as `for (tmp = a; tmp < b; tmp++)`). The `if` builder does not add indents for blocks since we can't make `Builder` an object yet rather than an alias to `string`, this will likely be one of the last refactors. Somewhat unrelated but `ccgtypes` is not ready yet because proc signatures are not implemented.
- Loading branch information
Showing
6 changed files
with
483 additions
and
322 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,94 @@ | ||
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) = | ||
builder.add("if (") | ||
builder.add(cond) | ||
builder.add(") {\n") | ||
body | ||
builder.add("}\n") | ||
|
||
template addSingleIfStmtWithCond(builder: var Builder, condBody: typed, body: typed) = | ||
builder.add("if (") | ||
condBody | ||
builder.add(") {\n") | ||
body | ||
builder.add("}\n") | ||
|
||
type IfStmt = object | ||
needsElse: bool | ||
|
||
template addIfStmt(builder: var Builder, stmt: out IfStmt, body: typed) = | ||
stmt = IfStmt(needsElse: false) | ||
body | ||
builder.add("\n") | ||
|
||
template addElifBranch(builder: var Builder, stmt: var IfStmt, cond: Snippet, body: typed) = | ||
if stmt.needsElse: | ||
builder.add(" else ") | ||
else: | ||
stmt.needsElse = true | ||
builder.add("if (") | ||
builder.add(cond) | ||
builder.add(") {\n") | ||
body | ||
builder.add("}") | ||
|
||
template addElseBranch(builder: var Builder, stmt: var IfStmt, body: typed) = | ||
assert stmt.needsElse | ||
builder.add(" else {\n") | ||
body | ||
builder.add("}") |
Oops, something went wrong.