From b58f5b77f5c778cdb8e8133e264567616ce43473 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Tue, 20 Jun 2023 09:31:36 +0200 Subject: errors --- parser/parser.go | 16 ++++++++-------- repl/face.txt | 13 +++++++++++++ repl/repl.go | 28 ++++++++-------------------- 3 files changed, 29 insertions(+), 28 deletions(-) create mode 100644 repl/face.txt diff --git a/parser/parser.go b/parser/parser.go index 725b0c6..b8c27d9 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -40,7 +40,7 @@ type ( type Parser struct { l *lexer.Lexer - errors []string + errors []error curToken token.Token peekToken token.Token @@ -111,18 +111,18 @@ func (p *Parser) expectPeek(t token.TokenType) bool { } } -func (p *Parser) Errors() []string { +func (p *Parser) Errors() []error { return p.errors } func (p *Parser) peekError(t token.TokenType) { - msg := fmt.Sprintf("expected next token to be %s, got %s instead", t, p.peekToken.Type) - p.errors = append(p.errors, msg) + err := fmt.Errorf("expected next token to be %s, got %s instead", t, p.peekToken.Type) + p.errors = append(p.errors, err) } func (p *Parser) noPrefixParseFnError(t token.TokenType) { - msg := fmt.Sprintf("no prefix parse function for %s found", t) - p.errors = append(p.errors, msg) + err := fmt.Errorf("no prefix parse function for %s found", t) + p.errors = append(p.errors, err) } func (p *Parser) ParseProgram() *ast.Program { @@ -247,8 +247,8 @@ func (p *Parser) parseIntegerLiteral() ast.Expression { value, err := strconv.ParseInt(p.curToken.Literal, 0, 64) if err != nil { - msg := fmt.Sprintf("could not parse %q as integer", p.curToken.Literal) - p.errors = append(p.errors, msg) + err = fmt.Errorf("could not parse %q as integer: %w", p.curToken.Literal, err) + p.errors = append(p.errors, err) return nil } diff --git a/repl/face.txt b/repl/face.txt new file mode 100644 index 0000000..14fdeb6 --- /dev/null +++ b/repl/face.txt @@ -0,0 +1,13 @@ + __,__ + .--. .-" "-. .--. + / .. \/ .-. .-. \/ .. \ + | | '| / Y \ |' | | + | \ \ \ 0 | 0 / / / | + \ '- ,\.-"""""""-./, -' / + ''-' /_ ^ ^ _\ '-'' + | \._ _./ | + \ \ '~' / / + '._ '-=-' _.' + '-----' + +Woops! We ran into some monkey business here! diff --git a/repl/repl.go b/repl/repl.go index 41bfe79..43a65f3 100644 --- a/repl/repl.go +++ b/repl/repl.go @@ -2,6 +2,7 @@ package repl import ( "bufio" + _ "embed" "fmt" "io" @@ -20,8 +21,7 @@ func Start(in io.Reader, out io.Writer) { for { fmt.Print(prompt) - scanned := scanner.Scan() - if !scanned { + if !scanner.Scan() { return } @@ -46,24 +46,12 @@ func Start(in io.Reader, out io.Writer) { } } -const monkeyFace = ` __,__ - .--. .-" "-. .--. - / .. \/ .-. .-. \/ .. \ - | | '| / Y \ |' | | - | \ \ \ 0 | 0 / / / | - \ '- ,\.-"""""""-./, -' / - ''-' /_ ^ ^ _\ '-'' - | \._ _./ | - \ \ '~' / / - '._ '-=-' _.' - '-----' -` +//go:embed face.txt +var monkeyFace string -func printParserErrors(out io.Writer, errors []string) { - io.WriteString(out, monkeyFace) - io.WriteString(out, "Woops! We ran into some monkey business here!\n") - io.WriteString(out, " parser errors:\n") - for _, msg := range errors { - io.WriteString(out, "\t"+msg+"\n") +func printParserErrors(out io.Writer, errors []error) { + fmt.Fprintln(out, monkeyFace) + for _, err := range errors { + fmt.Fprintln(out, err) } } -- cgit v1.2.3