Skip to content
This repository has been archived by the owner on Aug 31, 2021. It is now read-only.

fix eq? for strings #98

Merged
merged 3 commits into from
Nov 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions rt/rt.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ function schemeToJS(x, map) {
return x;
case 'object':
if (x instanceof String) {
if (x.charAt(0) === 's')
return x.substring(1);
return x.substring(2);
}
if (x instanceof Pair) {
Expand Down Expand Up @@ -124,15 +126,17 @@ function rt(engine) {
// that's not specified on the Scheme language level. Using JS
// strings for symbols allows us to compare with ===, as is
// required.
'string?': x => typeof(x) === 'string' && x.charAt(0) === 's',
'string?': x => (typeof x === 'string' || x instanceof String)
&& x.charAt(0) === 's',
'%symbol?': x => typeof(x) === 'string' && x.charAt(0) === 'S',
'%string=?' : (x, y) => x.valueOf() === y.valueOf(),

// The code still wants to access chars as a list.
'%list->string': chars => {
let ret = 's';
for (; chars != null; chars = chars.cdr)
ret += String.fromCharCode(chars.car >>> 1);
return ret;
return new String (ret);
},
'%string->list': x => {
let ret = null;
Expand All @@ -146,7 +150,7 @@ function rt(engine) {

// Gensyms are instances of String (objects with identity).
'%make-gensym': str => new String('S' + str),
'%gensym?': x => x instanceof String,
'%gensym?': x => x instanceof String && x.charAt(0) === 'S',

'cons': (car, cdr) => new Pair(car, cdr),
'pair?': x => x instanceof Pair,
Expand Down
3 changes: 1 addition & 2 deletions scheme-lib/rnrs.ss
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,7 @@
(%unreachable))
(%string->list s))
(define (string=? s1 s2)
;; This is ok because strings are currently represented as javascript values.
(eq? s1 s2))
(%string=? s1 s2))
(define (string->symbol s)
(if (string? s)
(%string->symbol s)
Expand Down
Binary file modified schism-stage0.wasm
Binary file not shown.
3 changes: 2 additions & 1 deletion schism/compiler.ss
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@

(bool string? (scm x))
(bool %symbol? (scm x))
(bool %string=? (scm x) (scm y))

(scm %list->string (scm x))
(scm %string->list (scm x))
Expand Down Expand Up @@ -542,7 +543,7 @@
eq? number? char?
%make-number %make-char
%number-value %char-value
string? %symbol?
string? %symbol? %string=?
%list->string %string->list
%string->symbol %symbol->string
%make-gensym %gensym?
Expand Down
25 changes: 25 additions & 0 deletions test/string-eq.ss
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
;; Copyright 2019 Google LLC
;;
;; Licensed under the Apache License, Version 2.0 (the License);
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;; http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an AS IS BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.

(library
(trivial)
(export do-test)
(import (rnrs))

(define (do-test)
(let* ((x (list->string '(#\a #\b #\c)))
(y (string->symbol x)))
(and (not (eq? x (list->string '(#\a #\b #\c))))
(eq? x x)
(eq? (symbol->string y) (symbol->string y))))))