Skip to content

Instantly share code, notes, and snippets.

Erik Haliewicz ehaliewicz

Block or report user

Report or block ehaliewicz

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View l.c
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef enum {
SUCCESS, // parsed
FAILURE, // didn't parse
ERROR // bad syntax
} status;
View 8queens.c
#include <stdio.h>
#include <stdint.h>
int columns[8] = {-1,-1,-1,-1,-1,-1,-1,-1};
int num_solutions = 0;
void print_queens() {
for(int y = 0; y < 8; y++) {
for(int x = 0; x < 8; x++) {
putchar(columns[x] == y ? 'Q' : '.');
@ehaliewicz
ehaliewicz / bf.lisp
Created Jul 30, 2018
brainfuck compiler to low-level lisp code
View bf.lisp
(defun compile-bf (program)
(let ((loop-stack (list)))
(let ((translated (loop for char across program appending
(case char
(#\> '((incf pointer)))
(#\< '((decf pointer)))
(#\+ '((incf (aref memory (wrap-pointer pointer)))))
(#\- '((decf (aref memory (wrap-pointer pointer)))))
(#\. '((format t "~a" (code-char (aref memory (wrap-pointer pointer))))))
(#\, '((setf (aref memory (wrap-pointer pointer)) (char-code (read-char)))))
@ehaliewicz
ehaliewicz / lambda-comp.c
Created May 31, 2017
lambda calculus compiler in 511 lines of C
View lambda-comp.c
// compile lambda calculus in a few lines (^:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <setjmp.h>
// syntax tree structures and utility functions
@ehaliewicz
ehaliewicz / lambda.c
Created May 26, 2017
lambda calculus in 255 lines of C
View lambda.c
// interpret lambda calculus in a few lines (^:
#include "stdio.h"
#include "stdlib.h"
#include "ctype.h"
#include "string.h"
#include "setjmp.h"
typedef enum {
APPLICATION, LAMBDA, SYMBOL
} type;
View gist:10608f47dc710ff423eb
#include "stdio.h"
#include "stdlib.h"
typedef struct {
void* apply_func; // pointer to function apply this closure w/ correct number of args and free-vars
void* func; // pointer to lifted function that implements this closure
int num_freevars; // number of bound variables (for GC use)
char* str; // string representation of this function
void* forwarded; // forward pointer (for GC use)
void* args[]; // bound variables
@ehaliewicz
ehaliewicz / lc->c.lisp
Last active Aug 29, 2015
lambda calculus -> c compiler
View lc->c.lisp
;; compiles untyped lambda calculus to portable C
;; syntax
;; (lambda x x) - lambda abstraction
;; (x y) - lambda combination (assuming x and y are bound)
;; map of function names to declarations
;; kept separate from the rest of the C code because they need to be forward-declared
(defvar *lambda-map* nil)
@ehaliewicz
ehaliewicz / lambda.lisp
Last active Aug 29, 2015
Lambda calculus interpreter in common lisp
View lambda.lisp
(defun interpret-lambda (expr &optional env)
(etypecase expr
(symbol (let ((res (assoc expr env))) (if res (cdr res) (error "Unbound symbol"))))
(list (case (car expr)
(lambda (list (car (second expr)) (third expr) env))
(otherwise
(let ((rand (interpret-lambda (second expr) env))
(rator (interpret-lambda (first expr) env)))
(interpret-lambda
(cadr rator)
@ehaliewicz
ehaliewicz / fast-gol.lisp
Last active Jan 2, 2016
A quick game of life algorithm.
View fast-gol.lisp
(ql:quickload "lispbuilder-sdl")
(ql:quickload "lispbuilder-sdl-gfx")
(deftype triplet () '(unsigned-byte 16))
(defmacro pixel-to-cell (val) `(/ ,val *cell-size*))
(defmacro cell-to-col (val) `(floor ,val 3))
(defmacro pixel-to-col (val) `(cell-to-col (pixel-to-cell ,val)))
@ehaliewicz
ehaliewicz / defenum.lisp
Last active Jan 1, 2016
A macro that generates enumeration types and functions to parse strings into enumerations.
View defenum.lisp
;; load a regex library
(ql:quickload "cl-ppcre")
;; utility functions
;; concatenate any number of any type of object into a string
(defun mkstr (&rest args)
(with-output-to-string (s)
(dolist (a args) (princ a s))))
You can’t perform that action at this time.