Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow function calls to partially fill RHS (fixes #52) #58

Merged
merged 1 commit into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 12 additions & 18 deletions src/phast.nim
Original file line number Diff line number Diff line change
Expand Up @@ -672,9 +672,8 @@ type
TSymKinds* = set[TSymKind]

const
routineKinds* = {
skProc, skFunc, skMethod, skIterator, skConverter, skMacro, skTemplate
}
routineKinds* =
{skProc, skFunc, skMethod, skIterator, skConverter, skMacro, skTemplate}
ExportableSymKinds* =
{skVar, skLet, skConst, skType, skEnumField, skStub} + routineKinds
tfUnion* = tfNoSideEffect
Expand All @@ -683,9 +682,8 @@ const
tfReturnsNew* = tfInheritable
skError* = skUnknown

var eqTypeFlags* = {
tfIterator, tfNotNil, tfVarIsPtr, tfGcSafe, tfNoSideEffect, tfIsOutParam, tfSendable
}
var eqTypeFlags* =
{tfIterator, tfNotNil, tfVarIsPtr, tfGcSafe, tfNoSideEffect, tfIsOutParam, tfSendable}
## type flags that are essential for type equality.
## This is now a variable because for emulation of version:1.0 we
## might exclude {tfGcSafe, tfNoSideEffect}.
Expand Down Expand Up @@ -1353,9 +1351,8 @@ const
resultPos* = 7
dispatcherPos* = 8
nfAllFieldsSet* = nfBase2
nkCallKinds* = {
nkCall, nkInfix, nkPrefix, nkPostfix, nkCommand, nkCallStrLit, nkHiddenCallConv
}
nkCallKinds* =
{nkCall, nkInfix, nkPrefix, nkPostfix, nkCommand, nkCallStrLit, nkHiddenCallConv}
nkIdentKinds* = {nkIdent, nkSym, nkAccQuoted, nkOpenSymChoice, nkClosedSymChoice}
nkPragmaCallKinds* = {nkExprColonExpr, nkCall, nkCallStrLit}
nkLiterals* = {nkCharLit .. nkTripleStrLit}
Expand All @@ -1368,9 +1365,8 @@ const
nkSymChoices* = {nkClosedSymChoice, nkOpenSymChoice}
nkStrKinds* = {nkStrLit .. nkTripleStrLit}
skLocalVars* = {skVar, skLet, skForVar, skParam, skResult}
skProcKinds* = {
skProc, skFunc, skTemplate, skMacro, skIterator, skMethod, skConverter
}
skProcKinds* =
{skProc, skFunc, skTemplate, skMacro, skIterator, skMethod, skConverter}
defaultSize = -1
defaultAlignment = -1
defaultOffset* = -1
Expand Down Expand Up @@ -1849,9 +1845,8 @@ proc newProcNode*(
result = newNodeI(kind, info)
result.sons = @[name, pattern, genericParams, params, pragmas, exceptions, body]

const AttachedOpToStr*: array[TTypeAttachedOp, string] = [
"=wasMoved", "=destroy", "=copy", "=dup", "=sink", "=trace", "=deepcopy"
]
const AttachedOpToStr*: array[TTypeAttachedOp, string] =
["=wasMoved", "=destroy", "=copy", "=dup", "=sink", "=trace", "=deepcopy"]

proc `$`*(s: PSym): string =
if s != nil:
Expand Down Expand Up @@ -2444,9 +2439,8 @@ proc addParam*(procType: PType, param: PSym) =

rawAddSon(procType, param.typ)

const magicsThatCanRaise = {
mNone, mSlurp, mStaticExec, mParseExprToAst, mParseStmtToAst, mEcho
}
const magicsThatCanRaise =
{mNone, mSlurp, mStaticExec, mParseExprToAst, mParseStmtToAst, mEcho}

proc canRaiseConservative*(fn: PNode): bool =
if fn.kind == nkSym and fn.sym.magic notin magicsThatCanRaise:
Expand Down
15 changes: 6 additions & 9 deletions src/phoptions.nim
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,10 @@ type
# old unused: cmdInterpret, cmdDef: def feature (find definition for IDEs)

const
cmdBackends* = {
cmdCompileToC, cmdCompileToCpp, cmdCompileToOC, cmdCompileToJS, cmdCrun
}
cmdDocLike* = {
cmdDoc0, cmdDoc, cmdDoc2tex, cmdJsondoc0, cmdJsondoc, cmdCtags, cmdBuildindex
}
cmdBackends* =
{cmdCompileToC, cmdCompileToCpp, cmdCompileToOC, cmdCompileToJS, cmdCrun}
cmdDocLike* =
{cmdDoc0, cmdDoc, cmdDoc2tex, cmdJsondoc0, cmdJsondoc, cmdCtags, cmdBuildindex}

type
NimVer* = tuple[major: int, minor: int, patch: int]
Expand Down Expand Up @@ -602,9 +600,8 @@ template newPackageCache*(): untyped =
proc newProfileData(): ProfileData =
ProfileData(data: newTable[TLineInfo, ProfileInfo]())

const foreignPackageNotesDefault* = {
hintProcessing, warnUnknownMagic, hintQuitCalled, hintExecuting, hintUser, warnUser
}
const foreignPackageNotesDefault* =
{hintProcessing, warnUnknownMagic, hintQuitCalled, hintExecuting, hintUser, warnUser}

proc isDefined*(conf: ConfigRef, symbol: string): bool

Expand Down
77 changes: 51 additions & 26 deletions src/phrenderer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -394,11 +394,18 @@ proc hasIndent(n: PNode): bool =
nkObjectTy, nkEnumTy, nkBlockStmt, nkBlockExpr
}

const postExprBlocks = {
nkStmtList, nkStmtListExpr, nkOfBranch, nkElifBranch, nkElse, nkExceptBranch,
nkFinally, nkDo
}

proc isStackedCall(n: PNode, inCall: bool): bool =
# At least two calls to enable "stacking" mode
case n.kind
of nkCall:
if inCall:
if n.len > 1 and n.lastSon.kind in postExprBlocks:
isStackedCall(n[0], inCall)
elif inCall:
true
else:
isStackedCall(n[0], true)
Expand Down Expand Up @@ -671,16 +678,28 @@ proc lstmts(g: var TOutput, n: PNode, flags: SubFlags = {}, doIndent = true): Li
withSrcLen(g):
gstmts(sl, n, flags, doIndent)

proc nlsub(g: TOutput, n: PNode, flags: SubFlags = {}): LineLen =
proc nlsubImpl(g: TOutput, n: PNode, flags: SubFlags): (bool, LineLen) =
## How many characters until the next early line break
let ll = lsub(g, n, flags)
case n.kind
of nkPar, nkClosure, nkCurly, nkBracket, nkTableConstr, nkStmtListExpr, nkTupleConstr:
(1, ll[1])
of nkPragma, nkPragmaExpr:
(2, ll[1])
if n.kind == nkDotExpr:
let (a, l) = nlsubImpl(g, n[0], flags)
if a:
(a, l)
else:
(false, l + lsub(g, n[1]) + 1)
else:
ll
let ll = lsub(g, n, flags)
case n.kind
of nkPar, nkClosure, nkCurly, nkBracket, nkTableConstr, nkStmtListExpr,
nkTupleConstr:
(true, (1, ll[1]))
of nkPragma, nkPragmaExpr:
(true, (2, ll[1]))
else:
(false, ll)

proc nlsub(g: TOutput, n: PNode, flags: SubFlags = {}): LineLen =
## How many characters until the next early line break
nlsubImpl(g, n, flags)[1]

proc fits(g: TSrcLen, x: LineLen): bool =
# Line lengths are computed assuming no extra line breaks
Expand Down Expand Up @@ -1330,6 +1349,15 @@ proc doParamsAux(g: var TOutput, params: PNode) =
putWithSpace(g, tkOpr, "->")
gsub(g, params[0])

proc isDotCall(n: PNode): bool =
case n.kind
of nkDotExpr:
isDotCall(n[0])
of nkCall:
true
else:
false

proc gsubOptNL(g: var TOutput, n: PNode, indentNL = IndentWidth, flags: SubFlags = {}) =
# Output n on the same line if it fits, else continue on next - indentation is
# always set up in case a comment linebreaks the statement
Expand All @@ -1344,19 +1372,22 @@ proc gsubOptNL(g: var TOutput, n: PNode, indentNL = IndentWidth, flags: SubFlags
const suboptAvoidNL = {
nkCall, nkConv, nkPattern, nkObjConstr, nkCast, nkStaticExpr, nkBracketExpr,
nkCurlyExpr, nkPragmaExpr, nkCommand, nkExprEqExpr, nkAsgn, nkFastAsgn, nkClosure,
nkTupleConstr, nkCurly, nkArgList, nkTableConstr, nkBracket, nkDotExpr, nkBind,
nkDo, nkIdentDefs, nkConstDef, nkVarTuple, nkExprColonExpr, nkTypeOfExpr,
nkDistinctTy, nkTypeDef, nkBlockStmt, nkBlockExpr, nkLambda, nkProcTy
nkTupleConstr, nkCurly, nkArgList, nkTableConstr, nkBracket, nkBind, nkDo,
nkIdentDefs, nkConstDef, nkVarTuple, nkExprColonExpr, nkTypeOfExpr, nkDistinctTy,
nkTypeDef, nkBlockStmt, nkBlockExpr, nkLambda, nkProcTy
}

let
sublen = nlsub(g, n, flags = flags)
sublen = lsub(g, n, flags = flags)
nl =
overflows(g, sublen) and (
n.kind notin suboptAvoidNL or fits(g, sublen + g.indent + indentNL) or
isStackedCall(n, false)
)
ind = condIndent(g, nl or g.pendingNL >= 0 or n.prefix.len > 0, indentNL)
(
((n.kind notin suboptAvoidNL and not isDotCall(n))) and
overflows(g, nlsub(g, n, flags = flags))
) or (overflows(g, sublen) and fits(g, sublen + g.indent + indentNL))

ind = condIndent(
g, isStackedCall(n, false) or nl or g.pendingNL >= 0 or n.prefix.len > 0, indentNL
)
flags =
if ind > 0:
{sfNoIndent} + flags
Expand Down Expand Up @@ -1395,11 +1426,6 @@ proc infixArgument(g: var TOutput, n: PNode, i: int, flags: SubFlags) =
if needsParenthesis:
put(g, tkParRi, ")")

const postExprBlocks = {
nkStmtList, nkStmtListExpr, nkOfBranch, nkElifBranch, nkElse, nkExceptBranch,
nkFinally, nkDo
}

proc postStatements(
g: var TOutput, n: PNode, start: int, skipDo: bool, skipColon = false
) =
Expand Down Expand Up @@ -1516,7 +1542,7 @@ proc gsub(g: var TOutput, n: PNode, flags: SubFlags, extra: int) =
false
ind = condIndent(g, doPars)

accentedName(g, n[0], flags = (flags * {sfStackDot}) + {sfStackDotInCall})
accentedName(g, n[0])

var i = 1
while i < n.len and n[i].kind notin postExprBlocks:
Expand Down Expand Up @@ -2194,8 +2220,7 @@ proc gsub(g: var TOutput, n: PNode, flags: SubFlags, extra: int) =
# see hasSignature - it would be nicer to remove them when unncessary
if n.len >= 1:
let retExtra =
extra +
(
extra + (
if n.len > 0 and n[0].kind != nkEmpty:
lsub(g, n[0]) + len(": ")
else:
Expand Down
18 changes: 16 additions & 2 deletions tests/after/exprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,7 @@ aaaaaaa.bbbbbbbbb
.longdotcall().ccccccccc.dddddddddd.eeeeeeeee
.sdcsd(a0000000, b000000, c000000).fffffff.ggggggg.hhhhhhhh.csdcsdcs.sdcsdcsd.csdcsdcsdcsd.csdcsdcs.dcsdcsdcsdcs.sdc

mynums =
myNums
mynums = myNums
.replace1("one", "o1ne")
.replace2("two", "t2wo")
.replace3("three", "t3hree")
Expand All @@ -170,3 +169,18 @@ discard
(cccccccccccccccccccccc and ddddddddddddddddddd)
)
)

# Dot expressions
let test2 = someSimpleResult.fff(v).valueOr:
3

let test2 = someSimpleResult(
aaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbb, ccccccccccccccccccccc, ddddddddddddd
).valueOr:
3

var aaaaaaaaaaaaaaaaaaaaaaaa =
bbbbbbbbbbbbbbbbbbbb.cccccccccccccccccccc[].ddddddddddddddddddddd

aaaaaaaaaaaaaaaaaaaaaaaa =
bbbbbbbbbbbbbbbbbbbb.cccccccccccccccccccc[].ddddddddddddddddddddd
93 changes: 93 additions & 0 deletions tests/after/exprs.nim.nph.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1885,3 +1885,96 @@ sons:
ident: "cccccccccccccccccccccc"
- kind: "nkIdent"
ident: "ddddddddddddddddddd"
- kind: "nkCommentStmt"
"comment": "# Dot expressions"
- kind: "nkLetSection"
sons:
- kind: "nkIdentDefs"
sons:
- kind: "nkIdent"
ident: "test2"
- kind: "nkEmpty"
- kind: "nkCall"
sons:
- kind: "nkDotExpr"
sons:
- kind: "nkCall"
sons:
- kind: "nkDotExpr"
sons:
- kind: "nkIdent"
ident: "someSimpleResult"
- kind: "nkIdent"
ident: "fff"
- kind: "nkIdent"
ident: "v"
- kind: "nkIdent"
ident: "valueOr"
- kind: "nkStmtList"
sons:
- kind: "nkIntLit"
intVal: 3
- kind: "nkLetSection"
sons:
- kind: "nkIdentDefs"
sons:
- kind: "nkIdent"
ident: "test2"
- kind: "nkEmpty"
- kind: "nkCall"
sons:
- kind: "nkDotExpr"
sons:
- kind: "nkCall"
sons:
- kind: "nkIdent"
ident: "someSimpleResult"
- kind: "nkIdent"
ident: "aaaaaaaaaaaaaaaaaaa"
- kind: "nkIdent"
ident: "bbbbbbbbbbbbb"
- kind: "nkIdent"
ident: "ccccccccccccccccccccc"
- kind: "nkIdent"
ident: "ddddddddddddd"
- kind: "nkIdent"
ident: "valueOr"
- kind: "nkStmtList"
sons:
- kind: "nkIntLit"
intVal: 3
- kind: "nkVarSection"
sons:
- kind: "nkIdentDefs"
sons:
- kind: "nkIdent"
ident: "aaaaaaaaaaaaaaaaaaaaaaaa"
- kind: "nkEmpty"
- kind: "nkDotExpr"
sons:
- kind: "nkBracketExpr"
sons:
- kind: "nkDotExpr"
sons:
- kind: "nkIdent"
ident: "bbbbbbbbbbbbbbbbbbbb"
- kind: "nkIdent"
ident: "cccccccccccccccccccc"
- kind: "nkIdent"
ident: "ddddddddddddddddddddd"
- kind: "nkAsgn"
sons:
- kind: "nkIdent"
ident: "aaaaaaaaaaaaaaaaaaaaaaaa"
- kind: "nkDotExpr"
sons:
- kind: "nkBracketExpr"
sons:
- kind: "nkDotExpr"
sons:
- kind: "nkIdent"
ident: "bbbbbbbbbbbbbbbbbbbb"
- kind: "nkIdent"
ident: "cccccccccccccccccccc"
- kind: "nkIdent"
ident: "ddddddddddddddddddddd"
11 changes: 4 additions & 7 deletions tests/after/postexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,7 @@ of a:
else:
discard

discard
(
aaa.bbb
.exec do(res: int64):
size = res
)
.ccc()
discard (
aaa.bbb.exec do(res: int64):
size = res
).ccc()
9 changes: 9 additions & 0 deletions tests/before/exprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,12 @@ let xxxxxxxxx = block:
let yyyyyyyyyy = aaaaaaaaaaaaaaaaaaaaaaaaa.ffffffffffffff(aaaaaaaaa, bbbbbbbbbbbbbbbbbb, cccccccccccccccccccc)

discard aaaaaaaaa and (aaaaaaaaaaaaaaaaaaaaaaaa and (aaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbb and (cccccccccccccccccccccc and ddddddddddddddddddd)))

# Dot expressions
let test2 = someSimpleResult.fff(v).valueOr: 3

let test2 = someSimpleResult(aaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbb, ccccccccccccccccccccc, ddddddddddddd).valueOr: 3

var aaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb.cccccccccccccccccccc[].ddddddddddddddddddddd

aaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb.cccccccccccccccccccc[].ddddddddddddddddddddd
Loading
Loading