regex-split for Guile

2011-03-07 Thread William James
Tested under Guile 1.8.7.


(define (regex-split regexp str . options)
  (let ((keep #f) (trim #f))
(if (member 'keep options)
  (begin (set! options (delete 'keep options))
 (set! keep #t)))
(if (member 'trim options)
  (begin (set! options (delete 'trim options))
 (set! trim #t)))
(let* ((matches (apply list-matches regexp str options))
   (indices
 (append '(0)
   (fold-right
 (lambda (m acc) (cons (match:start m)
   (cons (match:end m) acc))) '()
 matches)
   (list (string-length str
   (substrings
  (pair-fold-right
(lambda (lst accum)
  (if (or (even? (length lst))
  (and keep (> (length lst) 1)))
(cons (apply substring str (take lst 2)) accum)
accum))
'()
indices)))
  (if trim
(reverse! (drop-while string-null?
  (reverse! (drop-while string-null? substrings
substrings



guile> (regex-split "[-x]+" "foo--x--bar---what--")

("foo" "bar" "what" "")

guile> (regex-split "[-x]+" "foo--x--bar---what--"  'trim)

("foo" "bar" "what")

guile> (regex-split "[-x]+" "foo--x--bar---what"  'keep)

("foo" "--x--" "bar" "---" "what")



  



Parsing a struct within a struct

2011-03-07 Thread Aidan Gauland
Hello,

I'm trying to write a game in Guile Scheme using SDL, and I'm having
trouble parsing a C struct within a struct.  (The files for my program
are in the attached tarball, for those with the time to examine it
that closely.)

This is how the struct is parsed...

(parse-c-struct event-pointer
(list uint8 uint8 uint8
  (list uint8 int int uint16 )))

...which is called only when the event (an SDL event) is a key-down
event.

These are the relevant SDL structs...

/** General event structure */
typedef union SDL_Event {
Uint8 type;
SDL_ActiveEvent active;
SDL_KeyboardEvent key;
SDL_MouseMotionEvent motion;
SDL_MouseButtonEvent button;
SDL_JoyAxisEvent jaxis;
SDL_JoyBallEvent jball;
SDL_JoyHatEvent jhat;
SDL_JoyButtonEvent jbutton;
SDL_ResizeEvent resize;
SDL_ExposeEvent expose;
SDL_QuitEvent quit;
SDL_UserEvent user;
SDL_SysWMEvent syswm;
} SDL_Event;

typedef struct SDL_KeyboardEvent {
Uint8 type; /**< SDL_KEYDOWN or SDL_KEYUP */
Uint8 which;/**< The keyboard device index */
Uint8 state;/**< SDL_PRESSED or SDL_RELEASED */
SDL_keysym keysym;
} SDL_KeyboardEvent;

typedef struct SDL_keysym {
Uint8 scancode; /**< hardware specific scancode */
SDLKey sym; /**< SDL virtual keysym */
SDLMod mod; /**< current key modifiers */
Uint16 unicode; /**< translated character */
} SDL_keysym;

SDLKey and SDLMod are declared as typedef enums.
i.e.
typedef enum {
  ...
} SDLKey or SDLMod;

In my program, I print the result of the above call to
`parse-c-struct'.  In the program's output, the value of the sym field
is always a high-magnitude, negative number, but the value is unique
for each key, and consistent for each key (i.e. a key-press of, say, A
is always the same value).  But the defined values of SDLKey range
from 0 to 322, so the parsed value is obviously incorrect.  (I think
the parsed value of the mod field is also incorrect.)

Am I doing something wrong?  Are the arguments to `parse-c-struct'
incorrect?  Am I neglecting some important detail of how structs are
dealt with in C (such as alignment)?  Or (heaven forbid) is it a bug
in the FFI?

Regards,
Aidan Gauland


fruitflats.tar.gz
Description: Binary data


signature.asc
Description: Digital signature