Skip to content

Instantly share code, notes, and snippets.

View michaelballantyne's full-sized avatar

Michael Ballantyne michaelballantyne

View GitHub Profile
@michaelballantyne
michaelballantyne / linklet-primitives.rkt
Created February 1, 2022 04:15
Compute the set of primitives used in an s-expression linklet
#lang racket/base
(require racket/list racket/set racket/string racket/match)
(define (local? sym)
(define str (symbol->string sym))
(define trimmed (string-trim str #rx"_[0-9]+" #:left? #f))
(not (equal? str trimmed)))
(define (get-primitives l)
@michaelballantyne
michaelballantyne / magic-macro.rkt
Last active January 31, 2022 19:49
Macro that prints the names and values of local lexical variables
#lang racket/base
; Hastily adapted from debug-repl at https://github.com/AlexKnauth/debug/blob/master/debug/repl.rkt
; Note that this code relies on the output of `syntax-debug-info`, which the Racket documentation
; does not provide strong promises about. It may break with a future release of Racket.
; MIT License
;
; Copyright (c) 2015-2022 Alex Knauth, Suzanne Soy, Matthew Butterick, Sorawee Porncharoenwase,
@michaelballantyne
michaelballantyne / quasi.rkt
Created February 22, 2021 19:30
quasiquote interpreter
#lang racket
(define (eval e env)
(match e
[(? symbol?) (hash-ref env e)]
[`(let ([,(? symbol? x) ,e]) ,b)
(eval b (hash-set env x (eval e env)))]
[(? number?)
e]
[`(+ ,e1 ,e2)
@michaelballantyne
michaelballantyne / serializer-test1.rkt
Last active September 1, 2020 04:14
syntax serializer
#lang racket
(require (for-syntax racket/syntax "syntax-serializer.rkt"))
(provide Foo (for-syntax bar) check)
(define Foo #f)
(define-syntax (def stx)
(syntax-case stx ()
;;; Copyright (c) 2000-2008 Dan Friedman, Erik Hilsdale, and Kent Dybvig
;;;
;;; Permission is hereby granted, free of charge, to any person
;;; obtaining a copy of this software and associated documentation files
;;; (the "Software"), to deal in the Software without restriction,
;;; including without limitation the rights to use, copy, modify, merge,
;;; publish, distribute, sublicense, and/or sell copies of the Software,
;;; and to permit persons to whom the Software is furnished to do so,
;;; subject to the following conditions:
;;;
@michaelballantyne
michaelballantyne / subproctest.rkt
Last active September 30, 2019 16:46
close-output-port errors
#lang racket
(match-define (list pout pin id perr f) (process "exit"))
(with-handlers ([exn:fail? (lambda (e) (printf "error in write: \n~a\n" e) (void))])
(write-bytes #"foo" pin)
(flush-output pin)
(sleep 0.1)
(write-bytes #"bar" pin)
(flush-output pin))
#lang racket
(require
(for-syntax
ee-lib
syntax/parse))
(begin-for-syntax
(define lift-binds! (make-parameter #f))
(define lift-syntaxes! (make-parameter #f))
@michaelballantyne
michaelballantyne / submod.rkt
Created April 14, 2019 20:18
submodule problem
#lang racket
(module A racket
(provide m)
(require (for-syntax syntax/parse syntax/location))
(module empty racket
(provide #%module-begin))
(define-syntax (m stx)
#lang racket
; Interpreter for MetaScheme, the core staged language.
(struct lit [v] #:transparent)
(struct lam [xs e] #:transparent)
(struct ref [x] #:transparent)
(struct app [e1 e2] #:transparent)
(struct lif [c t e] #:transparent)
(struct quot [e] #:transparent)
#lang racket
(struct lit [v] #:transparent)
(struct lam [xs e] #:transparent)
(struct ref [x] #:transparent)
(struct app [e1 e2] #:transparent)
(struct quot [e] #:transparent)
(struct unquot [e] #:transparent)
(struct lif [c t e] #:transparent)
(struct run [e])