Parsley is a pattern matching and parsing tool for Python programmers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static BOOL | |
inside_segment(int v, int v1, int v2) | |
{ | |
if (v1 < 0 && v2 < 0) /* line */ | |
return TRUE; | |
else if (v1 < 0) /* ray */ | |
return v <= v2; | |
else if (v2 < 0) /* ray */ | |
return v >= v1; | |
else /* line segment */ |
Let's parse it up with Parsley. Following the standard example of other parsing frameworks, we're going to build a standard mathematical expression parser. Unlike other frameworks like PyParsing, Parsley grammars are defined in a giant string, rather than a special DSL.
Let's start with something that can interpret a collection of digits, and spit out a number:
import parsley
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* gcc -o listmode listmode.c $(pkg-config --cflags --libs glib-2.0 libdrm) */ | |
#include <glib.h> | |
#include <fcntl.h> | |
#include <stdlib.h> | |
#include <xf86drm.h> | |
#include <xf86drmMode.h> | |
int | |
main (int argc, char **argv) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* gcc -o displaybuffer displaybuffer.c $(pkg-config --cflags --libs glib-2.0 libdrm) */ | |
#include <glib.h> | |
#include <fcntl.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <xf86drm.h> | |
#include <xf86drmMode.h> | |
int |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* gcc -o listconnectors listconnectors.c $(pkg-config --cflags --libs glib-2.0 libdrm) */ | |
#include <glib.h> | |
#include <fcntl.h> | |
#include <stdlib.h> | |
#include <xf86drm.h> | |
#include <xf86drmMode.h> | |
int | |
main (int argc, char **argv) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* gcc -o listconnectors listconnectors.c $(pkg-config --cflags --libs glib-2.0 libdrm) */ | |
#include <glib.h> | |
#include <fcntl.h> | |
#include <stdlib.h> | |
#include <xf86drm.h> | |
#include <xf86drmMode.h> | |
int | |
main (int argc, char **argv) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Lang = imports.lang; | |
const Signals = imports.signals; | |
const Tweener = imports.tweener.tweener; | |
const Clutter = imports.gi.Clutter; | |
Clutter.init(null); | |
let white = Clutter.Color.get_static(Clutter.StaticColor.WHITE); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sdl.h> | |
struct _EventFuncs { | |
void (*on_keydown) (char key, void *user_data); | |
void (*on_loop) (void *user_data); | |
}; | |
typedef struct _EventFuncs EventFucs; | |
void event_loop(EventFuncs *event_funcs, void *user_data) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cairo/cairo.h> | |
#include <math.h> | |
static void | |
draw_region (cairo_t *cr, | |
cairo_region_t *region) | |
{ | |
cairo_rectangle_int_t box; | |
int n_boxes, i; |