From e0b3406a8c0fa27345883adb94fd55e1955febd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Thu, 30 Jun 2022 00:22:46 +0200 Subject: C-parser add strings. --- module/c/parse.scm | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'module/c/parse.scm') diff --git a/module/c/parse.scm b/module/c/parse.scm index d923e5b1..ad716132 100644 --- a/module/c/parse.scm +++ b/module/c/parse.scm @@ -3,6 +3,9 @@ :use-module (srfi srfi-1) :use-module (srfi srfi-71) :use-module (ice-9 match) + :use-module ((rnrs io ports) + :select (string->bytevector make-transcoder utf-8-codec)) + :use-module (rnrs bytevectors) :export (parse-lexeme-tree)) ;;; Rename this @@ -52,6 +55,14 @@ (list else vars) #f))) vars))) +;; Converts string to a null-terminated bytevector +(define* (string->c-string str optional: (transcoder (make-transcoder (utf-8-codec)))) + (let* ((bv* (string->bytevector str transcoder)) + (bv (make-bytevector (1+ (bytevector-length bv*))))) + (bytevector-copy! bv* 0 bv 0 (bytevector-length bv*)) + (bytevector-u8-set! bv (bytevector-length bv*) 0) + bv)) + (define (parse-lexeme-tree tree) (match tree ['() '()] @@ -149,6 +160,14 @@ ,(parse-lexeme-tree b) ,(parse-lexeme-tree c))] + + + + ['string #vu8(0)] + [('string str) (string->c-string str)] + [(('string str) ...) + (string->c-string (string-concatenate str))] + [('infix args ...) (let ((r (resolve-order-of-operations (flatten-infix (cons 'infix args))))) -- cgit v1.2.3