summaryrefslogtreecommitdiff
path: root/modules/language
diff options
context:
space:
mode:
authorStefan Israelsson Tampe <stefan.itampe@gmail.com>2018-02-19 18:12:48 +0100
committerStefan Israelsson Tampe <stefan.itampe@gmail.com>2018-02-19 18:12:48 +0100
commit0a6079db826621c32cdfc89c3daea217582f0bb7 (patch)
tree0caa325c4466978aa6015158a1cb7988286045ec /modules/language
parent304b82e4bb9c5f85e293a7e0706e3448aec04574 (diff)
os
Diffstat (limited to 'modules/language')
-rw-r--r--modules/language/python/compile.scm5
-rw-r--r--modules/language/python/for.scm5
-rw-r--r--modules/language/python/list.scm7
-rw-r--r--modules/language/python/module/os.scm63
-rw-r--r--modules/language/python/module/python.scm1
-rw-r--r--modules/language/python/module/string.scm116
-rw-r--r--modules/language/python/string.scm30
7 files changed, 206 insertions, 21 deletions
diff --git a/modules/language/python/compile.scm b/modules/language/python/compile.scm
index 75ceba1..d0e1ca5 100644
--- a/modules/language/python/compile.scm
+++ b/modules/language/python/compile.scm
@@ -323,7 +323,8 @@
((__setitem__) (L 'pylist-set!))
;; String
- ((format) (S 'py-format ))
+ ((format) (S 'py-strformat))
+ ((format_map) (S 'py-format-map))
((capitalize) (S 'py-capitalize))
((center) (S 'py-center ))
((endswith) (S 'py-endswith))
@@ -344,7 +345,7 @@
((upper) (S 'py-upper ))
((lstrip) (S 'py-lstrip ))
((rstrip) (S 'py-rstrip ))
- ((partition) (S 'py-partiti))
+ ((partition) (S 'py-partition))
((replace) (S 'py-replace))
((strip) (S 'py-strip ))
((title) (S 'py-title ))
diff --git a/modules/language/python/for.scm b/modules/language/python/for.scm
index bd9fa41..ec245df 100644
--- a/modules/language/python/for.scm
+++ b/modules/language/python/for.scm
@@ -110,6 +110,11 @@
(define-method (wrap-in (o <yield>))
o)
+(define-method (wrap-in (o <p>))
+ (aif it (ref o '__iter__)
+ (it)
+ (next-method)))
+
(define-method (next (l <p>))
((ref l '__next__)))
diff --git a/modules/language/python/list.scm b/modules/language/python/list.scm
index 0131328..5f4fbb7 100644
--- a/modules/language/python/list.scm
+++ b/modules/language/python/list.scm
@@ -174,16 +174,15 @@
(to-pylist l)))
(define-method (pylist-slice (o <string>) n1 n2 n3)
- (define N (slot-ref o 'n))
+ (define N (string-length o))
(define (f n) (if (< n 0) (+ N n) n))
(let* ((n1 (f (if (eq? n1 None) 0 n1)))
(n2 (f (if (eq? n2 None) (slot-ref o 'n) n2)))
(n3 (f (if (eq? n3 None) 1 n3))))
(list->string
- (map (lambda (x) (string-ref x 0))
- (to-list
- (pylist-slice (to-pylist o) n1 n2 n3))))))
+ (to-list
+ (pylist-slice (to-pylist o) n1 n2 n3)))))
(defpair (pylist-slice o n1 n2 n3)
diff --git a/modules/language/python/module/os.scm b/modules/language/python/module/os.scm
new file mode 100644
index 0000000..b5b8a18
--- /dev/null
+++ b/modules/language/python/module/os.scm
@@ -0,0 +1,63 @@
+(define-module (language python module os)
+ #:use-module (system foreign)
+ #:use-module (oop pf-objects)
+ #:use-module (oop goops)
+ #:use-module (language python for)
+ #:use-module (language python yield)
+ #:use-module (language python string)
+ #:export (error name ctermid environ))
+
+(define error 'OSError)
+(define name "posix")
+(define ctermid
+ (@ (guile) ctermid))
+
+
+(define environ
+ (let ()
+ (define e (dereference-pointer (dynamic-pointer "environ" (dynamic-link))))
+ (define (get-envs)
+ (let lp ((e e))
+ (let ((*e (dereference-pointer e)))
+ (if (null-pointer? *e)
+ '()
+ (cons
+ (pointer->string *e)
+ (lp (make-pointer (+ (pointer-address e) 8))))))))
+
+ (define (getkw)
+ (let lp ((es (get-envs)))
+ (if (pair? es)
+ (let ((x (string-split (car es) #\=)))
+ (let ((k (car x))
+ (v (string-join (cdr x) "=")))
+ (cons (cons k v) (lp (cdr es)))))
+ '())))
+
+ (define-python-class Env ()
+ (define __init__
+ (lambda (self) (values)))
+
+ (define __getitem__
+ (lambda (self k)
+ (getenv (slot-ref (pystring k) 'str))))
+
+ (define __setitem__
+ (lambda (self k v)
+ (putenv (slot-ref (pystring (+ k "=" v)) 'str))))
+
+ (define __delitem__
+ (lambda (self k)
+ (putenv (slot-ref (pystring k) 'str))))
+
+ (define __iter__
+ (lambda (self)
+ ((make-generator ()
+ (lambda (yield)
+ (for ((x : (getkw))) ()
+ (yield (car x) (cdr x)))))))))
+
+
+ (Env)))
+
+(for ((k v : environ)) () (pk k))
diff --git a/modules/language/python/module/python.scm b/modules/language/python/module/python.scm
index 69e02b0..2ea57eb 100644
--- a/modules/language/python/module/python.scm
+++ b/modules/language/python/module/python.scm
@@ -6,6 +6,7 @@
(<p> <property> class-method static-method ref
py-super-mac type object pylist-ref))
#:use-module (language python exceptions )
+ #:use-module ((language python module string ) #:select ())
#:use-module (language python def )
#:use-module (language python for )
#:use-module (language python try )
diff --git a/modules/language/python/module/string.scm b/modules/language/python/module/string.scm
index 6ece062..08dabcd 100644
--- a/modules/language/python/module/string.scm
+++ b/modules/language/python/module/string.scm
@@ -3,7 +3,6 @@
#:use-module (oop goops)
#:use-module (ice-9 match)
#:use-module (language python number)
- #:use-module ((language python module python) #:select (repr))
#:use-module (language python exceptions)
#:use-module (language python yield)
#:use-module (language python list)
@@ -13,6 +12,7 @@
#:use-module (parser stis-parser)
#:export (Formatter))
+(define (repr x) ((@ (guile) format) #f "~a" x))
(define int (mk-token (f+ (f-reg! "[0-9]")) string->number))
(define id (mk-token (f-seq (f-reg! "[_a-zA-Z]")
(f* (f-reg! "[_0-9a-zA-Z]")))))
@@ -78,6 +78,100 @@
(values (- s) "-")
(values s " "))))))
+(define (convert-float s format-str)
+ (match (pk 'conv-float
+ (with-fluids ((*whitespace* f-true))
+ (stis-parse format-str (f-seq formatSpec f-eof))))
+ ((align sign sharp zero width comma prec type)
+ (call-with-values (lambda () (gen-sign s sign))
+ (lambda (s s-sign)
+ (let* ((prec (if prec prec 6))
+ (s (let lp ((type type))
+ (match type
+ (#f (lp "g"))
+
+ ("f"
+ (format #f (+ "~," (number->string prec) "f") s))
+
+ ("F"
+ (let ((s (format #f (+ "~," (number->string prec)
+ "f")
+ s)))
+ (py-replace
+ (py-replace s "nan" "NAN")
+ "inf" "INF")))
+
+ ("e"
+ (py-replace
+ (format #f (+ "~," (number->string prec) "e") s)
+ "E" "e"))
+
+ ("E"
+ (format #f (+ "~," (number->string prec) "e") s))
+
+ ("g"
+ (let ((exp (log10 (abs s))))
+ (if (and (<= -4 exp)
+ (<= exp (max 1 prec)))
+ (lp "f")
+ (lp "e"))))
+ ("G"
+ (let ((exp (log10 (abs s))))
+ (if (and (<= -4 exp)
+ (<= exp (max 1 prec)))
+ (lp "F")
+ (lp "E"))))
+ ("n"
+ (let ((exp (log10 (abs s))))
+ (if (and (<= -4 exp)
+ (<= exp (max 1 prec)))
+ (lp "f")
+ (format #f (+ "~," (number->string prec) "h")
+ s))))
+
+ ("%"
+ (set s (* s 100))
+ (+ (lp "f") "%"))))))
+
+ (if width
+ (if zero
+ (get-align s '(#:align "0" "=") width
+ s-sign)
+ (get-align s align width
+ s-sign))
+
+ (+ s-sign s))))))))
+
+(define (convert-complex s format-str)
+ (match (pk 'conv-complex
+ (with-fluids ((*whitespace* f-true))
+ (stis-parse format-str (f-seq formatSpec f-eof))))
+ ((align sign sharp zero width comma prec type)
+ (let* ((prec (if prec prec 6))
+ (s (let lp ((type type))
+ (match type
+ (#f (lp "f"))
+ ("f"
+ (format #f (+ "~," (number->string prec) "i") s))))))
+ (if width
+ (get-align s align width "")
+ s)))))
+
+
+(define-method (py-format (s <real>) f)
+ (convert-float s f))
+(define-method (py-format (s <py-float>) f)
+ (convert-float s f))
+
+(define-method (py-format (s <complex>) f)
+ (convert-complex s f))
+(define-method (py-format (s <py-complex>) f)
+ (convert-complex s f))
+
+
+
+
+
(define (convert-integer s format-str)
(match (pk 'conv-int
(with-fluids ((*whitespace* f-true))
@@ -97,13 +191,21 @@
(s (let lp ((type type))
(match type
("b"
- (format #f "~b" s))
+ (if comma
+ (format #f "~:b" s)
+ (format #f "~b" s)))
("x"
- (format #f "~x" s))
+ (if comma
+ (format #f "~:x" s)
+ (format #f "~x" s)))
("X"
- (format #f "~:@(~x~)" s))
+ (if comma
+ (format #f "~:@(~:x~)" s)
+ (format #f "~:@(~x~)" s)))
("o"
- (format #f "~o" s))
+ (if comma
+ (format #f "~:o" s)
+ (format #f "~o" s)))
("d"
(if comma
(format #f "~:d" s)
@@ -117,7 +219,7 @@
(get-align (+ prefix s) align width
s-sign))
- (+ sign prefix s))))))))
+ (+ s-sign prefix s))))))))
(define-method (py-format (s <integer>) f)
(convert-integer s f))
@@ -254,7 +356,6 @@
(lp (+ i 1))
(warn "unused arg" i)))))
(for ((k v : kwargs)) ()
- (pk 'key k)
(if (not (member k used_args))
(warn "unused arg" k)))))
@@ -276,3 +377,4 @@
(else
(throw TypeError "conversion" conversion))))))
+(set! (@@ (language python string) formatter) (Formatter))
diff --git a/modules/language/python/string.scm b/modules/language/python/string.scm
index 1b9883d..8d6d642 100644
--- a/modules/language/python/string.scm
+++ b/modules/language/python/string.scm
@@ -11,7 +11,7 @@
py-expandtabs py-find py-rfind
py-isalnum py-isalpha py-isdigit py-islower
py-isspace py-isupper py-istitle py-join py-ljust
- py-rjust
+ py-rjust py-format-map
py-rljust py-lower py-upper py-lstrip py-rstrip
py-partition py-replace py-strip py-title
py-rpartitio py-rindex py-split py-rsplit py-splitlines
@@ -133,6 +133,13 @@
(- n it (len sub))
-1)))))
+(define formatter #f)
+(define-py (py-strformat format s . l)
+ (apply (ref formatter 'format) s l))
+
+(define-py (py-format-map format_map s map)
+ (apply (ref formatter 'vformat) s '() map))
+
(define format (lambda (a b) a))
(define-py (py-format format s format-string)
(format s format-string))
@@ -194,7 +201,7 @@
(()
#\space))))
(if (< width n)
- (pylist-slice s 0 width)
+ (pylist-slice s 0 width 1)
(let ((ret (make-string width ch)))
(let lp ((i 0))
(if (< i n)
@@ -213,7 +220,7 @@
(()
#\space))))
(if (< width n)
- (pylist-slice s (- width) (len s))
+ (pylist-slice s (- width) (len s) 1)
(let ((ret (make-string width ch)))
(let lp ((i 0) (j (- width n)))
(if (< i n)
@@ -259,10 +266,14 @@
(let lp ((i 0))
(if (< i n)
(if (test i)
- (list (pylist-slice s 0 i) sep (pylist-slice s (+ i m) n))
+ (list (pylist-slice s 0 i 1) sep (pylist-slice s (+ i m) n 1))
(lp (+ i 1)))
(list s "" "")))))
+(define-py (py-partition partition s (sep <py-string>))
+ (py-partition s (slot-ref sep 'str)))
+
+
(define-py (py-rpartition rpartition ss (ssep <string>))
(let* ((s (string-reverse ss))
(sep (string-reverse ssep))
@@ -281,13 +292,16 @@
(if (< i n)
(if (test i)
(list (string-reverse
- (pylist-slice s (+ i m) n))
+ (pylist-slice s (+ i m) n 1))
ssep
(string-reverse
- (pylist-slice s 0 i)))
+ (pylist-slice s 0 i 1)))
(lp (+ i 1)))
(list "" "" s)))))
-
+
+(define-py (py-rpartition rpartition s (sep <py-string>))
+ (py-rpartition s (slot-ref sep 'str)))
+
(define-py (py-replace replace s old new . l)
(let ((n (match l (() #f) ((n . _) n))))
(string-join
@@ -549,7 +563,7 @@
isdigit islower isspace istitle isupper join
ljust lower lstrip partition replace rfind rindex
rjust rpartition rsplit rstrip split splitlines
- startswith strip swapcase
+ startswith strip swapcase format_map
title translate upper zfill)))))
(pylist-sort! l)
l))