Skip to content

Instantly share code, notes, and snippets.

View swatson555's full-sized avatar

Steven Watson swatson555

View GitHub Profile
/* A tiger program that evaluates a lisp program.
*/
let type tokens = { head: string, tail: tokens }
function stridx (input: string, i: int): string =
substring (input,i,1)
function numeric (s : string): int =
ord (s) >= ord ("0") & ord (s) <= ord ("9") | ord (s) = ord ("-")

Datatypes

integers, reals, and strings

1;     (* => integer *)
1.0;   (* => real *)
"1.0"; (* => string *)
@swatson555
swatson555 / calc.py
Created March 26, 2023 05:47
metacircular python calculator
#!/usr/bin/env python3
""" calc.py
To use,
$ ./calc.py "1+2+3+4"
$ 10
Use `chmod +x calc.py` or run the program with `python3 calc.py <program-string>`.
"""
@swatson555
swatson555 / heap-lisp.c
Created February 17, 2023 12:42
Heap based scheme machine.
/* Heap based virtual machine described in section 3.4 of Three Implementation Models for Scheme, Dybvig
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
char token[128][32];
@swatson555
swatson555 / compile.ss
Created June 22, 2022 13:13
nanopass compiler for r0 language
#!/usr/bin/env scheme --script
(import (nanopass))
(define unique-var
(let ()
(define count 0)
(lambda (name)
(let ([c count])
(set! count (+ count 1))
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
char token[128][32];
int lexer(char* input) {
int ii = 0; // input index
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
// We'll have 128 tokens. Each token can be up to 32 characters long.
char token[128][32];
int lexer(char* input) {
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
// We'll have 128 tokens. Each token can be up to 32 characters long.
char token[128][32];
int lexer(char* input) {
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
// We'll have 128 tokens. Each token can be up to 32 characters long.
char token[128][32];
int lexer(char* input) {