boot2

Playing with the boostrap
git clone https://git.ryansepassi.com/git/boot2.git
Log | Files | Refs | README

065-string-symbol.scm (1436B)


      1 ; (string->symbol bv) and (symbol->string sym) -- bridge between the
      2 ; bytevector and symbol spaces. string->symbol interns into the global
      3 ; symtab; symbol->string returns a fresh bytevector copy of the name.
      4 
      5 ; Round-trip a literal.
      6 (define s (string->symbol "hello"))
      7 (if (eq? s 'hello) 0 (sys-exit 1))
      8 
      9 ; Two interns of the same bytes give eq? symbols (interning, not just
     10 ; structural equality).
     11 (if (eq? (string->symbol "abc") (string->symbol "abc")) 0 (sys-exit 2))
     12 
     13 ; Interning a literal matches a quoted symbol.
     14 (if (eq? (string->symbol "foo") 'foo) 0 (sys-exit 3))
     15 
     16 ; symbol->string returns a bytevector with the right bytes.
     17 (define name (symbol->string 'world))
     18 (if (string? name) 0 (sys-exit 4))
     19 (if (bytevector=? name "world") 0 (sys-exit 5))
     20 
     21 ; symbol->string returns a *fresh* bv: mutating it does not bleed back
     22 ; into the symtab (next conversion still gives "world").
     23 (bytevector-u8-set! name 0 88)         ; 'X'
     24 (if (bytevector=? (symbol->string 'world) "world") 0 (sys-exit 6))
     25 
     26 ; Round-trip both directions.
     27 (if (eq? 'banana (string->symbol (symbol->string 'banana))) 0 (sys-exit 7))
     28 (if (bytevector=? "banana"
     29                   (symbol->string (string->symbol "banana"))) 0 (sys-exit 8))
     30 
     31 ; Empty bytevector interns to a single canonical empty-named symbol.
     32 (if (eq? (string->symbol "") (string->symbol "")) 0 (sys-exit 9))
     33 (if (bytevector=? "" (symbol->string (string->symbol ""))) 0 (sys-exit 10))
     34 
     35 (sys-exit 0)