aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <sokolyuk@gmail.com>2023-06-20 09:31:36 +0200
committerDimitri Sokolyuk <sokolyuk@gmail.com>2023-06-20 09:31:36 +0200
commitb58f5b77f5c778cdb8e8133e264567616ce43473 (patch)
tree76f9a41870dad21f953404491632b85019ca1766
parent550aa0a288a00d820927a84b4f54a25f6c544643 (diff)
-rw-r--r--parser/parser.go16
-rw-r--r--repl/face.txt13
-rw-r--r--repl/repl.go28
3 files changed, 29 insertions, 28 deletions
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)
}
}