Skip to content

Add support for warnings #1735

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

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions src/arturo.nim
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ Commands:

Options:
--no-color Mute all colors from output
--no-warnings Mute all possible warnings

Experimental:
-c, --compile Compile script and write bytecode
Expand Down Expand Up @@ -230,6 +231,8 @@ when isMainModule and not defined(WEB):
# options
of "no-color":
muted = true
of "no-warnings":
ShowWarnings = false

# experimental
of "c","compile":
Expand Down
54 changes: 44 additions & 10 deletions src/vm/errors.nim
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,22 @@ var
ExecStack* : seq[int] = @[]

IsRepl* : bool = false
ShowWarnings* : bool = true

#=======================================
# Helpers
#=======================================

# Check environment

proc getCurrentContext(e: VError): string =
if e.kind == CmdlineErr: return ""
proc getGenericContext(): string =
if IsRepl: return " <repl> "
return " <script> "

proc getCurrentContext(e: VError = nil): string =
if e.kind == CmdlineErr: return ""
return getGenericContext()

proc getMaxWidth(): int =
when not defined(WEB):
terminalWidth()
Expand Down Expand Up @@ -150,24 +154,27 @@ func `~~`*(s: string, inputs: seq[string]): string =
finalS = finalS.replace("$$", "$#")
return finalS % replacements

# Error messages

proc printErrorHeader(e: VError) =
proc printGenericHeader(color: string, label: string, context: string) =
let preHeader =
fg(redColor) & "{HorizLine}{HorizLine}{LeftBracket} ".fmt &
bold(redColor) & (e.kind.label) &
fg(redColor) & " {RightBracket}".fmt
fg(color) & "{HorizLine}{HorizLine}{LeftBracket} ".fmt &
bold(color) & (label) &
fg(color) & " {RightBracket}".fmt

let postHeader =
getCurrentContext(e) &
context &
"{HorizLine}{HorizLine}".fmt &
resetColor()

let middleStretch = getMaxWidth() - preHeader.realLen() - postHeader.realLen()

echo ""
echo preHeader & repeat(HorizLine, middleStretch) & postHeader

# Error messages

proc printErrorHeader(e: VError) =
printGenericHeader(redColor, e.kind.label, getCurrentContext(e))

proc printErrorKindDescription(e: VError) =
if e.kind.description != "":
echo ""
Expand Down Expand Up @@ -234,6 +241,14 @@ proc showError*(e: VError) =
if (not IsRepl) or e.hint=="":
echo ""

proc showWarning*(t: string, w: string) =
printGenericHeader(cyanColor, t, getGenericContext())
echo ""
echo strip(indent(dedent(formatMessage(w)), 2), chars={'\n'})

if not IsRepl:
echo ""

func panic(error: VError) =
raise error

Expand All @@ -244,6 +259,12 @@ proc panic(throw: bool, error: VError) =
showError(error)
quit(1)

proc warn(kind: string, warning: string) =
echo "in WARN"
if ShowWarnings:
echo "and about to show a warning"
showWarning(kind, warning)

#=======================================
# Constructors
#=======================================
Expand Down Expand Up @@ -950,6 +971,19 @@ func ProgramError_panic*(message: string, code: int) =
panic:
toError ProgramErr, message, errCode=code

#------------------------
# Warnings
#------------------------

proc Warning_DeprecatedFunction*(fnc: string, recommended: string) =
warn("Symbol Deprecated"): """
Function has been deprecated:
_$#_

Use instead:
_$#_
""" ~~ @[fnc, recommended]

# TODO Re-establish stack trace debug reports
# labels: vm, error handling

Expand Down