Skip to content

Instantly share code, notes, and snippets.

@ympbyc ympbyc/Smalltalk-72.scm
Last active Mar 4, 2019

Embed
What would you like to do?
Riddle. Emulate Smalltalk-72 with Lisp (Scheme) Macro. Works with Gauche. 2012年のSmalltalkビアバッシュで発表したものです.
;;;; Smalltalk-72.scm ;;;;
;;;; Minori Yamashita 2012 ;;;;
(define Z (lambda (f) ((lambda (p)
(f (lambda (a) ((p p) a))))
(lambda (p)
(f (lambda (a) ((p p) a)))))))
;;Unhygenic anaphoric macro
(define-macro (to class args body)
`(define ,class (lambda ,args
(Z (lambda (SELF) (lambda (⇒) ,body))))))
;;hygenic macro
(define-syntax
(syntax-rules ()
((_ x y a b) (if (equal? x y) a b))))
(define-syntax ☞ (syntax-rules (← ⠄ ፡)
((_ x ← y) (define x y))
((_ x ← ፡ ⠄ body) (lambda (x) body))))
(define-syntax
(syntax-rules ()
((_ x) x)))
;;;Example 1: point
(to point (x y)
(ᗉ 'x ⇒ (⇑ x)
(ᗉ 'y ⇒ (⇑ y)
(ᗉ '+ ⇒ (☞ a ← ፡ ⠄ (⇑ (point (+ x (a 'x)) (+ y (a 'y)))))
(ᗉ '- ⇒ (☞ a ← ፡ ⠄ (⇑ (point (- x (a 'x)) (+ y (a 'y)))))
(ᗉ '= ⇒ (☞ a ← ፡ ⠄ (⇑ (and (= x (a 'x)) (= x (a 'y)))))
'()))))))
(☞ pt1 ← (point 5 6))
(☞ pt2 ← (point 7 8))
(☞ pt3 ← ((pt1 '+) pt2))
(pt3 'x)
(pt3 'y)
;;Example 2: Sammy the Python
(to animal (name)
(ᗉ 'name ⇒ (⇑ name)
(ᗉ 'talk ⇒ (⇑ (string-append (SELF 'name) ": 'Hello'"))
'())))
(☞ sam ← (animal "Sammy the Python"))
(sam 'talk)
;;Appendix A: Unicode Symbols
;ះঃः፡ៈ⟟⦑⦓⦅⦂⦪⦞⦨♙⨟ᗉ⇑
;;Appendix B: ASCII version
;(define-macro (to class args body)
; `(define ,class (lambda ,args
; (Z (lambda (SELF) (lambda (=>) ,body))))))
;(define-syntax <o
; (syntax-rules ()
; ((_ x y a b) (if (equal? x y) a b))))
;(define-syntax o- (syntax-rules (<- -- %)
; ((_ x <- y) (define x y))
; ((_ x <- % -- body) (lambda (x) body))))
;(define-syntax ^
; (syntax-rules ()
; ((_ x) x)))
;;Appendix C: ことえり
;; ← いん
;; ᗉ め
;; ⇒ いふ
;; ፡ ていく
;; ⠄ どっと
;; ⇑ りたーん
;; ☞ くおーと
;;Appendix D: token-eating
;(to point (x y)
; (ᗉ 'x ⇒ (☞ ⇒ ← ፡ ⠄ (ᗉ '<- ⇒ (☞ v ← ፡ ⠄ (set! x v)) x))
; (ᗉ 'y ⇒ (☞ ⇒ ← ፡ ⠄ (ᗉ '<- ⇒ (☞ v ← ፡ ⠄ (set! y v)) y))
; '())))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.