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
// no includes | |
typedef typeof(sizeof(0)) size_t; | |
#define NULL 0 | |
int printf ( const char * format, ... ); | |
void* memcpy(void *dest, const void * src, size_t n); | |
void exit(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
func isLeap(y int) bool { | |
if y%4 == 0 { | |
if y%100 == 0 { | |
return y%400 == 0 | |
} | |
return true | |
} | |
return false | |
} |
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 <ctype.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
typedef enum { | |
SUCCESS, // parsed | |
FAILURE, // didn't parse | |
ERROR // bad syntax | |
} status; |
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 <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' : '.'); |
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
(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))))) |
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
// 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 |
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
// 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; |
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 "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 |
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
;; 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) |
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
(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) |
NewerOlder