Skip to content

Commit

Permalink
nil safe
Browse files Browse the repository at this point in the history
Signed-off-by: George Lemon <[email protected]>
  • Loading branch information
georgelemon committed Aug 8, 2023
1 parent 1afbc9e commit 3d9b64f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
10 changes: 8 additions & 2 deletions src/bropkg/engine/handlers/pCommand.nim
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,11 @@ newPrefixProc "parseAssert":
walk p
let exp = p.getPrefixOrInfix(includeOnly = {tkInteger, tkFloat, tkBool,
tkString, tkVarCall, tkIdentifier, tkFnCall}, scope = scope)
if exp.nt == ntInfix:
return newAssert(exp, tk)
if likely(exp != nil):
case exp.nt:
of ntInfix:
result = newAssert(exp, tk)
of ntCall:
if exp.getNodeType in {ntBool, ntInt, ntFloat, ntString}:
result = newAssert(exp, tk)
else: discard
4 changes: 2 additions & 2 deletions src/bropkg/engine/logging.nim
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ type
UnknownPseudoClass = "Unknown pseudo-class"
VariableRedefinition = "Compile-time variables are immutable"
UndefinedPropertyAccessor = "Undefined property accessor $ for object $"
InvalidInfixMissingValue = "Invalid infix missing assignable token"
InvalidInfixOperator = "Invalid infix operator $ for $"
invalidInfixMissingValue = "Invalid infix missing assignable token"
invalidInfixOperator = "Invalid infix operator $ for $"
declaredNotUsed = "Declared and not used $"
TryingAccessNonObject = "Trying to get property $ on a non-object variable $"

Expand Down
19 changes: 17 additions & 2 deletions src/bropkg/engine/parser.nim
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ const
# }
tkAssignable = {tkString, tkInteger, tkBool, tkColor, tkAccQuoted} + tkVars + tkNamedColors
tkComparable = tkAssignable
tkLogicalComp = {tkInteger, tkFloat, tkBool, tkString, tkVarCall, tkIdentifier, tkFnCall}
tkAssignableFn = {tkJSON}
tkTypedLiterals = {
tkArrayLit, tkBoolLit, tkColorLit, tkFloatLit, tkFunctionLit,
Expand Down Expand Up @@ -393,7 +394,20 @@ proc parseCompExp(p: var Parser, lht: Node, scope: var seq[ScopeTable]): Node =
# parse logical expressions with symbols (==, !=, >, >=, <, <=)
let op = getInfixOp(p.curr.kind, false)
walk p
let rht = p.parsePrefix(includeOnly= {tkInteger, tkFloat, tkVarCall, tkIdentifier, tkFnCall}, scope = scope)
let rhtToken = p.curr
let rht = p.parsePrefix(includeOnly=tkLogicalComp, scope = scope)
if likely(rht != nil):
case op
of LT, LTE, GT, GTE:
let
lhtNodeType = lht.getNodeType
rhtNodeType = rht.getNodeType
if lhtNodeType notin {ntInt, ntFloat}:
errorWithArgs(invalidInfixOperator, rhtToken, [$op, $lhtNodeType])
elif rhtNodeType notin {ntInt, ntFloat}:
errorWithArgs(invalidInfixOperator, rhtToken, [$op, $rhtNodeType])
else: discard

if likely(rht != nil):
result = newInfix(lht)
result.infixOp = op
Expand Down Expand Up @@ -575,7 +589,7 @@ proc getPrefixFn(p: var Parser, excludeOnly, includeOnly: set[TokenKind] = {}):
of tkThis: parseThis
of tkAccQuoted: parseAccQuoted
of tkNamedColors: parseNamedColor
# of tkAssert: parseAssert
of tkAssert: parseAssert
else:
if p.next isnot tkColon:
parseSelectorTag
Expand Down Expand Up @@ -603,6 +617,7 @@ proc parseRoot(p: var Parser, scope: var seq[ScopeTable], excludeOnly, includeOn
of tkIf: p.parseCond(scope, excludeOnly, includeOnly)
of tkCase: p.parseCase(scope, excludeOnly, includeOnly)
of tkEcho: p.parseEchoCommand(scope)
of tkAssert: p.parseAssert(scope)
of tkFor: p.parseFor(scope, excludeOnly, includeOnly)
of tkImport: p.parseImport(scope, excludeOnly, includeOnly)
of tkIdentifier:
Expand Down

0 comments on commit 3d9b64f

Please sign in to comment.