From 0740f2da7ad8d5addc156d4f47f4b341de462609 Mon Sep 17 00:00:00 2001 From: Justin Ethier Date: Wed, 24 Jun 2015 22:28:09 -0400 Subject: [PATCH] Added util module --- cgen.scm | 33 --------------------------------- scheme/cyclone/libraries.sld | 2 +- scheme/cyclone/util.sld | 8 ++++++++ util.scm | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 34 deletions(-) create mode 100644 scheme/cyclone/util.sld diff --git a/cgen.scm b/cgen.scm index 58dcfce7..964768eb 100644 --- a/cgen.scm +++ b/cgen.scm @@ -51,39 +51,6 @@ (next (cdr head) (cons (car head) tail))))))) (next (string->list str) '()))) -;; Name-mangling. - -;; We have to "mangle" Scheme identifiers into -;; C-compatible identifiers, because names like -;; foo-bar/baz are not identifiers in C. - -; mangle : symbol -> string -(define (mangle symbol) - (letrec - ((m (lambda (chars) - (if (null? chars) - '() - (if (or (and (char-alphabetic? (car chars)) (not (char=? (car chars) #\_))) - (char-numeric? (car chars))) - (cons (car chars) (m (cdr chars))) - (cons #\_ (append (integer->char-list (char->natural (car chars))) - (m (cdr chars)))))))) - (ident (list->string (m (string->list (symbol->string symbol)))))) - (if (member (string->symbol ident) *c-keywords*) - (string-append "_" ident) - ident))) - -(define (mangle-global symbol) - (string-append "__glo_" (mangle symbol))) - -(define *c-keywords* - '(auto _Bool break case char _Complex const continue default do double else - enum extern float for goto if _Imaginary inline int long register restrict - return short signed sizeof static struct switch typedef union unsigned - void volatile while - list ;; Not a keyword but reserved type - )) - (define *c-main-function* "main(int argc,char **argv) {long stack_size = long_arg(argc,argv,\"-s\",STACK_SIZE); diff --git a/scheme/cyclone/libraries.sld b/scheme/cyclone/libraries.sld index 2a08ccc8..5f1e2a77 100644 --- a/scheme/cyclone/libraries.sld +++ b/scheme/cyclone/libraries.sld @@ -1,7 +1,7 @@ (define-library (scheme cyclone libraries) (import (scheme base) (scheme read) - ; TODO: what else? definitely need trans.scm + (scheme cyclone util) ) (export library? diff --git a/scheme/cyclone/util.sld b/scheme/cyclone/util.sld new file mode 100644 index 00000000..f43cf1df --- /dev/null +++ b/scheme/cyclone/util.sld @@ -0,0 +1,8 @@ +(define-library (scheme cyclone util) + (import (scheme base) + (scheme char)) + (export + tagged-list? + mangle + mangle-global) + (include "../../util.scm")) diff --git a/util.scm b/util.scm index 870e22d5..851cb182 100644 --- a/util.scm +++ b/util.scm @@ -3,3 +3,38 @@ (if (pair? exp) (equal? (car exp) tag) #f)) + +;; Name-mangling. + +;; We have to "mangle" Scheme identifiers into +;; C-compatible identifiers, because names like +;; foo-bar/baz are not identifiers in C. + +; mangle : symbol -> string +(define (mangle symbol) + (letrec + ((m (lambda (chars) + (if (null? chars) + '() + (if (or (and (char-alphabetic? (car chars)) (not (char=? (car chars) #\_))) + (char-numeric? (car chars))) + (cons (car chars) (m (cdr chars))) + (cons #\_ (append (integer->char-list (char->natural (car chars))) + (m (cdr chars)))))))) + (ident (list->string (m (string->list (symbol->string symbol)))))) + (if (member (string->symbol ident) *c-keywords*) + (string-append "_" ident) + ident))) + +(define (mangle-global symbol) + (string-append "__glo_" (mangle symbol))) + +(define *c-keywords* + '(auto _Bool break case char _Complex const continue default do double else + enum extern float for goto if _Imaginary inline int long register restrict + return short signed sizeof static struct switch typedef union unsigned + void volatile while + list ;; Not a keyword but reserved type + )) +;; END name mangling section +