Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
aoh committed May 27, 2017
2 parents 59f5893 + 89f8cbe commit f237f12
Show file tree
Hide file tree
Showing 15 changed files with 433 additions and 256 deletions.
488 changes: 272 additions & 216 deletions c/ovm.c

Large diffs are not rendered by default.

Binary file modified fasl/init.fasl
Binary file not shown.
24 changes: 15 additions & 9 deletions owl/codec.scm
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
(owl proof))

(export
hex-encode ;; str → str
hex-decode) ;; str → str | #false
hex-encode-list ;; (byte ...) → str
hex-encode ;; str → str
hex-decode ;; str → str | #false
hex-decode-list) ;; str → (byte ...) | #false

(begin

Expand Down Expand Up @@ -51,16 +53,20 @@
#false))))
(else #false))))))

(define (hex-encode str)
(define (hex-encode-list lst)
(list->string
(hex-encode-bytes
(string->bytes str))))
(hex-encode-bytes lst)))

(define (hex-encode str)
(hex-encode-list
(string->bytes str)))

(define (hex-decode-list str)
(hex-decode-bytes (string->bytes str)))

(define (hex-decode str)
(let ((bs (hex-decode-bytes (string->bytes str))))
(if bs
(bytes->string bs)
#false)))
(maybe bytes->string
(hex-decode-list str)))

(example
(hex-decode (hex-encode "")) = ""
Expand Down
19 changes: 18 additions & 1 deletion owl/date.scm
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@
leap-year? ;; y → bool
valid-date? ;; d m y → bool
next-date ;; d m y → d' m' y'
next-date-with-week ;; d m y wd wn → d' m' y' wd' wn'
week-info ;; d m y → week weekday
day-names-fi
day-names-en
date-str ;; secs [tz-offset-hours] -> str
date-str-tz ;; secs tz-offset-hours -> str
year-start-day
year-start-week-info
minute hour day week year leap-year)

(begin
Expand Down Expand Up @@ -65,6 +68,7 @@
(values 1 (+ month 1) year)))
(else
(values (+ day 1) month year))))


;; date is valid *and* date computations work for it
(define (valid-date? d m y)
Expand Down Expand Up @@ -136,7 +140,7 @@
;; whole week fits the year
(values week day))
((< (+ md (days-to-thursday day)) 32)
;; partial, but switch happens before thursday
;; partial week, but switch happens before thursday
(values week day))
(else
;; subsequent thursday falls to next year
Expand All @@ -157,6 +161,19 @@
(loop rd rm (+ week 1) day reset?))
(loop rd rm week day reset?)))))))

(define (next-date-with-week day month year week-day week-num)
(lets ((d m y (next-date day month year)))
(if (eq? week-day 7)
(if (< week-num 52)
(values d m y 1 (+ week-num 1))
(lets ((wn wd (week-info d m y)))
(values d m y wd wn)))
(values d m y (+ week-day 1) week-num))))

(example
(next-date-with-week 31 12 1971 5 1) = (values 1 1 1972 6 1)
(next-date-with-week 27 12 1970 7 52) = (values 28 12 1970 1 53))

;;;
;;; UNIXish time
;;;
Expand Down
4 changes: 4 additions & 0 deletions owl/defmac.scm
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
;; k v r, k v l r+ -- type-ff-right
;; k v l, k v l+ r -- type-ff-leftc

maybe
)

(begin
Expand Down Expand Up @@ -569,4 +570,7 @@
((if-lets bindings then)
(if-lets bindings then #false))))

(define (maybe op arg)
(if arg (op arg) arg))

))
14 changes: 12 additions & 2 deletions owl/digest.scm
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,21 @@
(cons (bxor a b)
(list-xor as bs))))))

(define (any->bytes x)
(cond
((string? x)
(string->bytes x))
((vector? x)
(vector->list x))
(else
;; may be a lazy list or list
x)))

(define (make-hmac hasher blocksize)
(lambda (key msg)
(lets
((key (string->bytes key)) ;; we want to UTF-8 encode it
(msg (string->bytes msg)) ;; ditto
((key (any->bytes key)) ;; we want to UTF-8 encode it
(msg (any->bytes msg)) ;; ditto
(key (if (> (length key) blocksize) (hasher key) key))
(key
(append key
Expand Down
13 changes: 10 additions & 3 deletions owl/dump.scm
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,9 @@
; dump entry object to path, or stdout if path is "-"

(define (make-compiler extras)
(λ (entry path opts native) ; <- this is the usual compile-owl
(λ (entry path opts native . custom-runtime) ; <- this is the usual compile-owl
(if (null? custom-runtime)
(print-to stderr "custom runtime not provided"))
(lets
((path (get opts 'output "-")) ; <- path argument deprecated
(format
Expand Down Expand Up @@ -383,7 +385,12 @@

(bytes ;; encode the resulting object for saving in some form
(fasl-encode-cooked entry native-cook))


(runtime
(if (and (pair? custom-runtime) (car custom-runtime))
(car custom-runtime)
rts-source))

(port ;; where to save the result
(if (equal? path "-")
stdout
Expand All @@ -410,7 +417,7 @@
;; dump ovm.c and replace /* AUTOGENERATED INSTRUCTIONS */ with new native ops (if any)
(write-bytes port
(string->bytes
(str-replace rts-source
(str-replace runtime
"/* AUTOGENERATED INSTRUCTIONS */"
(render-native-ops native-ops))))

Expand Down
6 changes: 4 additions & 2 deletions owl/env.scm
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,12 @@
or return arity error where first is function))
(else
`(wrong number of returned values ,(tuple->list b))))))
((eq? opcode 0)
`("error: bad call: operator " ,a ", args w/ cont " ,b))
((eq? opcode 52)
`(trying to get car of a non-pair ,a))
`("error: car on non-pair " ,a))
((eq? opcode 53)
`(trying to get cdr of a non-pair ,a))
`("error: cdr on non-pair " ,a))
(else
`("error: instruction" ,(primop-name opcode) "reported error: " ,a " " ,b))))

Expand Down
1 change: 1 addition & 0 deletions owl/io.scm
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

file->vector ;; vector io, may be moved elsewhere later
file->list ;; list io, may be moved elsewhere later
file->byte-stream ;; path → #false | (byte ...)
vector->file
write-vector ;; vec port
port->meta-byte-stream ;; fd → (byte|'io-error|'block ...) | thunk
Expand Down
21 changes: 19 additions & 2 deletions owl/lazy.scm
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
(export
lfold lfoldr lmap lappend ; main usage patterns
lfor liota liter lnums
lzip ltake llast llen
lzip ltake lsplit llast llen
lcar lcdr
lkeep lremove
ldrop llref ledit
Expand All @@ -27,9 +27,11 @@
(owl defmac)
(owl list)
(owl list-extra)
(owl proof)
(only (owl syscall) error))

(begin

;; convert an application to a thunk
(define-syntax delay
(syntax-rules ()
Expand Down Expand Up @@ -61,7 +63,7 @@
(if (null? tl) (car l) (llast tl))))
((null? l) (error "llast: empty list: " l))
(else (llast (l)))))

;; l → hd l' | error
(define (uncons l d)
(cond
Expand Down Expand Up @@ -218,6 +220,21 @@
(else
(λ () (ltake (l) n)))))

(define (lsplit l n)
(let loop ((l l) (o null) (n n))
(cond
((eq? n 0)
(values (reverse o) l))
((pair? l)
(loop (cdr l) (cons (car l) o) (- n 1)))
((null? l)
(loop l o 0))
(else
(loop (l) o n)))))

(example
(lsplit '(1 2 3 4) 2) = (values '(1 2) '(3 4)))

(define (lkeep p l)
(cond
((null? l) l)
Expand Down
35 changes: 28 additions & 7 deletions owl/list-extra.scm
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
(import
(owl math)
(owl list)
(owl proof)
(owl defmac)
(owl syscall))

(begin

(define (lref lst pos)
(cond
((null? lst) (error "lref: out of list" pos))
Expand Down Expand Up @@ -47,7 +49,7 @@
(else
(lets ((hd tl lst))
(cons hd (ledn tl (- pos 1) op))))))

;; list edit - apply op to value at given pos
(define (led lst pos op)
(cond
Expand All @@ -56,7 +58,7 @@
(else
(lets ((hd tl lst))
(cons hd (led tl (- pos 1) op))))))

;; insert value to list at given position
(define (lins lst pos val)
(cond
Expand All @@ -66,27 +68,38 @@
(lets ((hd tl lst))
(cons hd (lins tl (- pos 1) val))))))

(example
(lref '(a b c) 1) = 'b
(lset '(a b c) 1 'x) = '(a x c)
(ldel '(a b c) 1) = '(a c)
(led '(1 2 3) 1 (λ (x) (* x 10))) = '(1 20 3)
(ledn '(1 2 3) 1 (λ (lst) (cons 'x lst))) = '(1 x 2 3)
(lins '(a b c) 1 'x) = '(a x b c))

(define (length lst)
(fold (λ (n v) (+ n 1)) 0 lst))

; take at n (or less) elemts from list l

(define (take l n)
(cond
(cond
((eq? n 0) null)
((null? l) null)
(else (cons (car l) (take (cdr l) (- n 1))))))

; drop n elements (or less) from list l

(define (drop l n)
(cond
((eq? n 0) l)
((null? l) l)
(else (drop (cdr l) (- n 1)))))

; fixme, iotas should be unfolds

(example
(length '(a b c)) = 3
(take '(a b c) 2) = '(a b)
(take '(a) 100) = '(a)
(drop '(a b c) 2) = '(c)
(drop '(a) 100) = '())

(define (iota-up p s e)
(if (< p e)
(cons p (iota-up (+ p s) s e))
Expand All @@ -107,6 +120,10 @@
null)
(else
(error "bad iota: " (list 'iota from step to)))))

(example
(iota 0 1 5) = '(0 1 2 3 4)
(iota 10 -2 0) = '(10 8 6 4 2))

(define (list-tail lst n)
(if (eq? n 0)
Expand All @@ -129,4 +146,8 @@
(else
(loop (cdr l) (cons (car l) o) (- n 1))))))

(example
(list-tail '(a b c) 1) = '(b c)
(repeat 'x 3) = '(x x x)
(split '(a b c d) 2) = (values '(a b) '(c d)))
))
10 changes: 6 additions & 4 deletions owl/list.scm
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,6 @@
(list a)
(ilist a mid (loop (car as) (cdr as)))))))

(example
(interleave 'x '(a b c)) = '(a x b x c)
(interleave 'x '()) = ())

;; lst → a b, a ++ b == lst, length a = length b | length b + 1
(define (halve lst)
(let walk ((t lst) (h lst) (out null))
Expand All @@ -337,5 +333,11 @@
(example
l = (lets ((head tail (halve l))) (append head tail))))

(example
(interleave 'x '(a b c)) = '(a x b x c)
(interleave 'x '()) = ()
(halve '(a b c d)) = (values '(a b) '(c d))
(halve '(a b c d e)) = (values '(a b c) '(d e)))

(define ╯°□°╯ reverse)
))
Loading

0 comments on commit f237f12

Please sign in to comment.