Skip to content

Commit

Permalink
Allow newlines/tabs/etc inside strings
Browse files Browse the repository at this point in the history
  • Loading branch information
skx committed Feb 15, 2024
1 parent 7468c6d commit 6b653df
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
2 changes: 1 addition & 1 deletion foth/eval/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ func (e *Eval) compileToken(token lexer.Token) error {

}

// output a string, in compiled form
// output a string-print operation, in compiled form
if token.Name == ".\"" {
e.strings = append(e.strings, token.Value)
e.tmp.Words = append(e.tmp.Words, -5)
Expand Down
41 changes: 38 additions & 3 deletions foth/lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,48 @@ func (l *Lexer) Tokens() ([]Token, error) {
closed := false
val := ""
for offset < len(l.input) {
if l.input[offset] == '"' {
c := l.input[offset]

if c == '"' {
closed = true
offset++
break
} else {
val += string(l.input[offset])
}

// Handle \n, etc.
if c == '\\' {

// if there is another character
if offset+1 < len(l.input) {

// look at what it is
offset++
c := l.input[offset]

if c == 'n' {
c = '\n'
}
if c == 'r' {
c = '\r'
}
if c == 't' {
c = '\t'
}
if c == '"' {
c = '"'
}
if c == '\\' {
c = '\\'
}

val += string(c)
offset++
continue
}
}

// default
val += string(l.input[offset])
offset++
}

Expand Down

0 comments on commit 6b653df

Please sign in to comment.