Basic String escape
Some checks failed
Build / build (push) Successful in 1m37s
Test / build (push) Failing after 11m57s

This commit is contained in:
Chuck Smith
2024-03-19 16:41:44 -04:00
parent 97eeae0c1a
commit aa0582ed72
3 changed files with 21 additions and 11 deletions

View File

@@ -17,12 +17,12 @@ printBookName(book);
let fibonacci = fn(x) { let fibonacci = fn(x) {
if (x == 0) { if (x == 0) {
0 return 0
} else { } else {
if (x == 1) { if (x == 1) {
return 1; return 1;
} else { } 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 map = fn(arr, f) {
let iter = fn(arr, accumulated) { let iter = fn(arr, accumulated) {
if (len(arr) == 0) { if (len(arr) == 0) {
accumulated return accumulated
} else { } 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]; let numbers = [1, 1 + 1, 4 - 1, 2 * 2, 2 + 3, 12 / 2];

View File

@@ -1,6 +1,9 @@
package lexer package lexer
import "monkey/token" import (
"monkey/token"
"strings"
)
type Lexer struct { type Lexer struct {
input string input string
@@ -179,14 +182,20 @@ func (l *Lexer) skipWhitespace() {
} }
func (l *Lexer) readString() string { func (l *Lexer) readString() string {
position := l.position + 1 b := &strings.Builder{}
for { for {
l.readChar() l.readChar()
if l.ch == '\\' && l.peekChar() == '"' {
l.readChar()
} else {
if l.ch == '"' || l.ch == 0 { if l.ch == '"' || l.ch == 0 {
break break
} }
} }
return l.input[position:l.position]
b.WriteByte(l.ch)
}
return b.String()
} }
func (l *Lexer) readLine() string { func (l *Lexer) readLine() string {

View File

@@ -32,6 +32,7 @@ func TestNextToken(t *testing.T) {
"foo bar" "foo bar"
[1, 2]; [1, 2];
{"foo": "bar"} {"foo": "bar"}
"foo \"bar\""
` `
tests := []struct { tests := []struct {
@@ -137,7 +138,7 @@ func TestNextToken(t *testing.T) {
{token.COLON, ":"}, {token.COLON, ":"},
{token.STRING, "bar"}, {token.STRING, "bar"},
{token.RBRACE, "}"}, {token.RBRACE, "}"},
{token.STRING, "foo \"bar\""},
{token.EOF, ""}, {token.EOF, ""},
} }