[Bootstrap] Add E0003 and the MakeRuntimeError to cover for MakeError

This commit is contained in:
Sameer Rahmani 2021-01-10 18:30:48 +00:00
parent c30c93442b
commit f2241cc31f
4 changed files with 49 additions and 6 deletions

View File

@ -63,6 +63,7 @@ type IError interface {
IDebuggable
GetErrType() ErrType
GetErrno() errors.Errno
GetDescription() *string
GetStackTrace() *TraceBack
// To wrap Golan rrrrors
@ -116,6 +117,10 @@ func (e *Error) GetStackTrace() *TraceBack {
return e.trace
}
func (e *Error) GetErrno() errors.Errno {
return e.errno
}
func (e *Error) GetDescription() *string {
desc, ok := errors.ErrorsDescription[e.errno]
if ok {
@ -136,8 +141,6 @@ func MakePlainError(msg string) IError {
// the root of the error.
func MakeError(rt *Runtime, e IExpr, msg string) IError {
rt.Stack.Push(e, rt.Stack.GetCurrentFn())
// frame := MakeFrame(e, rt.Stack.GetCurrentFn(), 1)
// trace := append(*rt.Stack.ToTraceBack(), frame)
return &Error{
Node: MakeNodeFromExpr(e),
@ -147,6 +150,19 @@ func MakeError(rt *Runtime, e IExpr, msg string) IError {
}
}
func MakeRuntimeError(rt *Runtime, e IExpr, errno errors.Errno, msg string) IError {
rt.Stack.Push(e, rt.Stack.GetCurrentFn())
return &Error{
Node: MakeNodeFromExpr(e),
errtype: RuntimeError,
msg: msg,
errno: errno,
trace: rt.Stack.ToTraceBack(),
}
}
func MakeSyntaxErrorf(n Node, msg string, a ...interface{}) IError {
return &Error{
Node: n,
@ -156,10 +172,8 @@ func MakeSyntaxErrorf(n Node, msg string, a ...interface{}) IError {
}
func MakeSemanticError(rt *Runtime, e IExpr, errno errors.Errno, msg string) IError {
//currentFn := rt.Stack.GetCurrentFn()
rt.Stack.Push(e, rt.Stack.GetCurrentFn())
frames := &[]*Frame{
//MakeFrame(e, currentFn, 1),
rt.Stack.Pop(),
}

View File

@ -99,9 +99,10 @@ func evalForm(rt *Runtime, scope IScope, form IExpr) (IExpr, IError) {
}
if expr == nil {
return nil, MakeError(
return nil, MakeRuntimeError(
rt,
sym,
errors.E0003,
fmt.Sprintf(
"can't resolve symbol '%s' in ns '%s'",
symbolName,

View File

@ -23,6 +23,7 @@ import (
"strings"
"github.com/gookit/color"
"serene-lang.org/bootstrap/pkg/errors"
)
func toRepresanbleString(ast ...IRepresentable) string {
@ -169,7 +170,17 @@ func printErrorWithTraceBack(rt *Runtime, err IError) {
}
loc := err.GetLocation()
errTag := color.Red.Sprint(err.GetErrType().String())
fmt.Printf("%s: %s\nAt: %d to %d\n", errTag, err.String(), loc.GetStart(), loc.GetEnd())
fmt.Printf(
"%s: %s\nAt: %d to %d\n",
errTag,
err.String(),
loc.GetStart(),
loc.GetEnd(),
)
if err.GetErrno() != errors.E0000 {
fmt.Printf("For more information on this error try: `serene explain %s`\n", err.GetErrno())
}
}
func PrintError(rt *Runtime, err IError) {

View File

@ -26,8 +26,18 @@ const (
E0000 Errno = iota // THE DESCRIPTION IS NOT SET
E0001
E0002
E0003
)
func (e Errno) String() string {
return [...]string{
"E0000",
"E0001",
"E0002",
"E0003",
}[e]
}
var ErrorsDescription map[Errno]string = map[Errno]string{
E0000: `Can't find any description for this error.`,
E0001: `
@ -51,4 +61,11 @@ Functions expect a certain number of argument. The number of arguments
that you're passing to the function doesn't match with it's signature.
To fix the problem double check the function signature and make sure
that you're passing the correct number of arguments to it`,
E0003: `
Do you have a typo ? Or did you forget to define a symbol that you're trying
to evaluate ?
This error happens when the symbol that you're trying to evaluate is not
associated with any value in the current scope tree.
`,
}