Skip to content

Commit

Permalink
Writing strings to a file.
Browse files Browse the repository at this point in the history
  • Loading branch information
arlaneenalra committed May 30, 2019
1 parent cd99bac commit 862ae31
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 9 deletions.
3 changes: 2 additions & 1 deletion insomniac_as
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ idx! ( index obj vector -- ) Takes an index, object, and vector/record/byte vect
vec-len ( vector -- size) Takes a vector as an argument and returns
the vectors length.

str->slice (string -- slice) Convert a string into bytevector slice.
str->slice ( string -- slice ) Convert a string into bytevector slice.
u8->str ( u8 -- string ) Convert a bytevector into a string.

slice ( start end vector -- slice ) Given a vector or bytevector, return a vector of the same
type that is backed by the same data store as the original.
Expand Down
4 changes: 2 additions & 2 deletions lib/ports/core.scm
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@
(define write-bytevector
(writer-factory bytevector-length slice))

;(define write-string
; (writer-factory string-length (lambda (str) str)))
(define write-string
(writer-factory string-length substring))

(define (read-bytevector k . args)
(define port
Expand Down
2 changes: 1 addition & 1 deletion lib/ports/textual.scm
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
(begin
(define (text-write str num fd)
(write-bytevector
(string->utf8 string 0 num)))
(slice (string->u8 str) 0 num) fd))

(define (text-read num fd)
"")
Expand Down
22 changes: 17 additions & 5 deletions lib/strings.scm
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,27 @@
;;; Some basic string functions
;;;

;; Directly convert a string into a bytevector
;; Directly convert a string into a bytevector.
(define (string->u8 str)
(asm (str) str->slice))

;; Directly convert a byte vector into a string.
(define (u8->string u8)
(asm (u8) u8->str))

;; Determine the length of a string.
(define (string-length str)
(bytevector-length (string->u8 str)))

;; Slice a string.
(define (substring str start end)
(u8->string
(slice (string->u8 str) start end)))

;; Convert a String into a utf8
(define (utf8->string bytevector . args)
"")
;;(define (utf8->string bytevector . args)
;; "")

(define (string->utf8 string . args)
(bytevector 0))
;;(define (string->utf8 string . args)
;; (bytevector 0))

1 change: 1 addition & 0 deletions src/include/ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ typedef enum op {
OP_VECTOR_LENGTH, /* return the length of a vector */
OP_SLICE, /* returns a slice of a vector */
OP_STRING_SLICE, /* returns a slice of a string */
OP_BYTE_VECTOR_STRING, /* returns a string from a bytevector */

/* jump operations Jumps are relative */
OP_CALL, /* call the given target and leave return on stack */
Expand Down
1 change: 1 addition & 0 deletions src/libinsomniac_asm/lexer.l
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ CHAR_VAL (newline|space|eof|[^{WS}])
"vec-len" { return OP_VECTOR_LENGTH; }
"slice" { return OP_SLICE; }
"str->slice" { return OP_STRING_SLICE; }
"u8->str" { return OP_BYTE_VECTOR_STRING; }

"out" { return OP_OUTPUT; }
"read" { return OP_FD_READ; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ void op_index_ref(vm_internal_type *vm);
void op_vector_length(vm_internal_type *vm);
void op_slice(vm_internal_type *vm);
void op_string_slice(vm_internal_type *vm);
void op_byte_vector_string(vm_internal_type *vm);

/* list operations */
void op_cons(vm_internal_type *vm);
Expand Down
1 change: 1 addition & 0 deletions src/libinsomniac_vm/libinsomniac_vm_instructions/vm_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ void setup_instructions(vm_internal_type *vm) {
vm->ops[OP_VECTOR_LENGTH] = &op_vector_length;
vm->ops[OP_SLICE] = &op_slice;
vm->ops[OP_STRING_SLICE] = &op_string_slice;
vm->ops[OP_BYTE_VECTOR_STRING] = &op_byte_vector_string;

vm->ops[OP_CONS] = &op_cons;
vm->ops[OP_CAR] = &op_car;
Expand Down
17 changes: 17 additions & 0 deletions src/libinsomniac_vm/libinsomniac_vm_instructions/vm_vector.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,20 @@ void op_string_slice(vm_internal_type *vm) {
vm_push(vm, vm->reg2);
}

/* Return a string from a bytevector. */
void op_byte_vector_string(vm_internal_type *vm) {
object_type *vector = 0;

vm->reg1 = vector = vm_pop(vm);

if (vector->type != BYTE_VECTOR) {
throw(vm, "u8->str requires a bytevector argument.", 1, vector);
return;
}

vm->reg2 = vm_make_string(
vm, (char *)vector->value.byte_vector.vector, vector->value.byte_vector.length);

vm_push(vm, vm->reg2);

}

0 comments on commit 862ae31

Please sign in to comment.