From aa0582ed7232a0eedb9f57f708815e49fe5c73cc Mon Sep 17 00:00:00 2001 From: Chuck Smith Date: Tue, 19 Mar 2024 16:41:44 -0400 Subject: [PATCH] Basic String escape --- examples/demo.monkey | 10 +++++----- lexer/lexer.go | 19 ++++++++++++++----- lexer/lexer_test.go | 3 ++- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/examples/demo.monkey b/examples/demo.monkey index 4a06e8d..7c1d635 100644 --- a/examples/demo.monkey +++ b/examples/demo.monkey @@ -17,12 +17,12 @@ printBookName(book); let fibonacci = fn(x) { if (x == 0) { - 0 + return 0 } else { if (x == 1) { return 1; } else { - fibonacci(x - 1) + fibonacci(x - 2); + return fibonacci(x - 1) + fibonacci(x - 2); } } }; @@ -30,13 +30,13 @@ let fibonacci = fn(x) { let map = fn(arr, f) { let iter = fn(arr, accumulated) { if (len(arr) == 0) { - accumulated + return accumulated } else { - iter(rest(arr), push(accumulated, f(first(arr)))); + return iter(rest(arr), push(accumulated, f(first(arr)))); } }; - iter(arr, []); + return iter(arr, []); }; let numbers = [1, 1 + 1, 4 - 1, 2 * 2, 2 + 3, 12 / 2]; diff --git a/lexer/lexer.go b/lexer/lexer.go index c31d041..e123bfd 100644 --- a/lexer/lexer.go +++ b/lexer/lexer.go @@ -1,6 +1,9 @@ package lexer -import "monkey/token" +import ( + "monkey/token" + "strings" +) type Lexer struct { input string @@ -179,14 +182,20 @@ func (l *Lexer) skipWhitespace() { } func (l *Lexer) readString() string { - position := l.position + 1 + b := &strings.Builder{} for { l.readChar() - if l.ch == '"' || l.ch == 0 { - break + if l.ch == '\\' && l.peekChar() == '"' { + l.readChar() + } else { + if l.ch == '"' || l.ch == 0 { + break + } } + + b.WriteByte(l.ch) } - return l.input[position:l.position] + return b.String() } func (l *Lexer) readLine() string { diff --git a/lexer/lexer_test.go b/lexer/lexer_test.go index 7bf104c..cb4cf3f 100644 --- a/lexer/lexer_test.go +++ b/lexer/lexer_test.go @@ -32,6 +32,7 @@ func TestNextToken(t *testing.T) { "foo bar" [1, 2]; {"foo": "bar"} + "foo \"bar\"" ` tests := []struct { @@ -137,7 +138,7 @@ func TestNextToken(t *testing.T) { {token.COLON, ":"}, {token.STRING, "bar"}, {token.RBRACE, "}"}, - + {token.STRING, "foo \"bar\""}, {token.EOF, ""}, }