Skip to content

Instantly share code, notes, and snippets.

View NalaGinrut's full-sized avatar

NalaGinrut

View GitHub Profile
@NalaGinrut
NalaGinrut / naive-aot.scm
Last active December 21, 2015 04:19
very naive AOT half-baked compiler for unfinished Guile brand new Register VM. It's only a toy since the RTL branch is still is very experimental.
(use-modules (ice-9 match))
(define test
'((begin-program self1180 ())
(label kentry1181)
(begin-kw-arity (x y) () #f () #f 3 #f)
(label kargs1182)
(add 1 1 2)
(label ktail1189)
(return 1)
@NalaGinrut
NalaGinrut / time_test.c
Created August 12, 2013 09:39
times function tested under GNU/Hurd, it always returns all zero in tms struct. PS: it's OK under GNU/Linux
#include <sys/times.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
struct tms buf;
if (times(&buf) < 0)
exit(115);
@NalaGinrut
NalaGinrut / recv.scm
Created August 12, 2013 07:34
recv.scm of termite port to GNU Guile. NOTE: this may not be the best port, but it seems logical for the expanding of hygienic macro.
;; All hail the RECV form
(define-syntax-rule (recv . clauses)
(let ((msg (gensym "msg")) ;; the current mailbox message
(loop (gensym "loop"))) ;; the mailbox seeking loop
;; check the last clause to see if it's a timeout
(let ((sesualc (reverse clauses)))
(if (and (pair? (car sesualc))
(eq? (caar sesualc) 'after))
@NalaGinrut
NalaGinrut / lexer.scm
Last active December 20, 2015 19:19
a lexer for fun
(define sm '((0 ("A" 1)) (1 ("A" 3) ("B" 2)) (2 (end 2) ("C" 7)) (3 ("B" 4))
(4 ("B" 6) (end 4)) (6 (end 6)) (7 ("C" 8)) (8 ("D" 9)) (9 (end 9))))
(define ll '((2 . "AB") (4 . "AAB") (6 . "AABBB") (9 . "ABCCD")))
(define (my-lexer str)
(call-with-input-string
str
(lambda (port)
(let lp((stat 0) (c (read-char port)) (ret '()))
(if (eof-object? c)
(let* ((now (assoc-ref sm stat)) (w (assoc-ref now 'end)))
@NalaGinrut
NalaGinrut / bytevector-slice.scm
Created July 19, 2013 07:21
efficient bytevector-slice for GNU Guile
(use-modules (system foreign) (rnrs bytevector))
;; TODO:
;; 1. (> hi (bytevector-length bv))
;; 2. (< lo 0) wrap reference
(define (%bytevector-slice bv lo hi)
(and (< hi lo) (error %bytevector-slice "wrong range" lo hi))
(let* ((ptr (bytevector->pointer bv))
(addr (pointer-address ptr))
(la (+ addr lo))
@NalaGinrut
NalaGinrut / kmp.py
Created July 4, 2013 08:37
KMP string matching algorithm in Python
from sets import Set
def gen_xfix_set(string, generator):
ret,n = Set([]),len(string)-1
while True:
if n==0: return ret
else:
ret.add(generator(string, n))
n -= 1
@NalaGinrut
NalaGinrut / kmp.scm
Last active December 19, 2015 08:29
A KMP string match algorithm implementation
(use-modules (srfi srfi-1) (ice-9 receiver))
(define (gen-xfix-list str generator)
(let lp((ret '()) (n (1- (string-length str))))
(if (zero? n) ret (lp (cons (generator str n) ret) (1- n)))))
(define (gen-prefix-list str)
(gen-xfix-list str (lambda (str n) (substring str 0 n))))
(define (gen-suffix-list str)
@NalaGinrut
NalaGinrut / png2gdh.py
Last active December 19, 2015 05:09
convert png file to gameduino graphic header. You may need gameduino module of Python
#! /usr/bin/env python
# Be sure you have installed:
# easy_install PIL
# easy_install PySerial
# easy_install gameduino
import Image
import gameduino.prep as gdprep
from sys import argv
@NalaGinrut
NalaGinrut / cat.scm
Last active December 14, 2015 21:28
A Guile version 'cat' for Guile-100 project
#! /usr/bin/env guile
!#
(use-modules (ice-9 getopt-long))
(setlocale LC_ALL "")
(define cur-out (current-output-port))
(define cur-in (current-input-port))
(define option-spec
@NalaGinrut
NalaGinrut / b2s.scm
Created October 20, 2012 08:41
binary->string
(use-modules (rnrs))
(define* (binary->string bin #:key (base 16) (endiannes 'little))
(let ((bl (bytevector->uint-list bin endiannes 1))
(n (car (assoc-ref '((2 "~b") (8 "~o") (16 "~x")) base))))
(call-with-output-string
(lambda (port)
(for-each (lambda (i) (format port n i)) bl)))))