Skip to content

Instantly share code, notes, and snippets.

View Prince781's full-sized avatar
⚙️
Compiling a compiler

Princeton Ferro Prince781

⚙️
Compiling a compiler
View GitHub Profile
@Prince781
Prince781 / gcd.c
Last active November 1, 2015 05:17
gcd
/* gcd.c
* run `make gcd` */
#include <stdio.h>
#include <stdlib.h>
long gcd (long a, long b) {
ldiv_t qr;
while (b != 0) {
qr = ldiv(a, b);
@Prince781
Prince781 / scale_canvas.js
Last active August 29, 2015 14:22
scale canvas to hidpi
$("canvas").each(function(i,elem) { var ctx = elem.getContext('2d'); var ratio = (window.devicePixelRatio || 1) / (ctx.webkitBackingStorePixelRatio || ctx.mozBackingStorePixelRatio || ctx.msBackingStorePixelRatio || ctx.oBackingStorePixelRatio || ctx.backingStorePixelRatio || 1); elem.width *= ratio; elem.height *= ratio; ctx.scale(ratio,ratio); }); void(0);
@Prince781
Prince781 / stack.c
Created February 6, 2015 16:23
Stack implementation in C
#include "stack.h"
#include <stdio.h>
#include <stdlib.h>
static void stack_grow(struct stack *s, int addition);
struct stack *stack_new(int maxsize)
{
struct stack *s;
@Prince781
Prince781 / llist.c
Created February 6, 2015 16:20
Linked list implementation in C
#include "llist.h"
#include <stdio.h>
#include <stdlib.h>
struct llist *llist_new(void)
{
struct llist *ll;
if ((ll = malloc(sizeof(*ll))) == NULL) {
fprintf(stderr, "%s: could not create list\n", __func__);
@Prince781
Prince781 / validate.c
Created January 13, 2015 05:02
Validate 2sat data
/* validate 2sat input data */
#include <stdio.h>
#include <stdlib.h>
#define abs(x) ((x) < 0 ? -(x) : (x))
int main(int argc, char *argv[])
{
if (argc < 2) {
printf("usage: %s [file]\n", argv[0]);
@Prince781
Prince781 / gendata.c
Last active August 29, 2015 14:13
Generates 2sat data.
/* gendata.c
* generate random 2sat data
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[])
{
if (argc < 4) {
@Prince781
Prince781 / queue.c
Created January 12, 2015 03:44
A queue implementation in C
#include "queue.h"
#include <stdio.h>
#include <stdlib.h>
struct queue *queue_new(void)
{
struct queue *q;
q = malloc(sizeof(struct queue));
q->base = NULL;
@Prince781
Prince781 / threads.c
Created December 27, 2014 21:23
pthreads example
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/sysinfo.h>
#define UBOUND 1000000000
void *tfunc(void *arg);
int main(int argc, char *argv[])
@Prince781
Prince781 / Makefile
Last active August 29, 2015 14:11
GLFW demo
# makefile for glfw
CFLAGS=$(shell pkg-config --cflags glfw3)
LDFLAGS=$(shell pkg-config --libs glfw3) -lGL -lm
all:
$(CC) $(CFLAGS) glfwtest.c -o glfw_test $(LDFLAGS)
clean:
$(RM) glfw_test
@Prince781
Prince781 / fsize.sh
Last active August 29, 2015 14:11
file size function
#!/bin/sh
# put this in .bashrc or /etc/bashrc
function fsize() {
kB=1000
val=$(stat --printf="%s" $1)
if [ $val -lt $kB ]; then
printf "$val B\n"
elif [ $val -lt $(($kB*$kB)) ]; then
printf "%0.2lf KB\n" $(bc -l <<< "$val/$kB")