Created
April 23, 2018 12:27
-
-
Save johnnyb/211e105882248e892fa485327039cc90 to your computer and use it in GitHub Desktop.
Attempt at object-oriented system
This file contains hidden or 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
| (define-syntax oo-class | |
| (syntax-rules (attr method this) | |
| ( | |
| (oo-class class-name | |
| ((attr attr-name initial-val) ...) | |
| ((method (meth-name meth-arg ...) body ...) ...)) | |
| (define class-name | |
| (lambda () | |
| (letrec | |
| ( | |
| (this #f) | |
| (attr-name initial-val) | |
| ... | |
| (funcmap | |
| (list | |
| (cons (quote meth-name) (cons (lambda (meth-arg ...) body ...) '())) | |
| ... | |
| ) | |
| ) | |
| ) | |
| (set! this (lambda (methname) | |
| (cadr (assoc methname funcmap)) | |
| )) | |
| this | |
| ) | |
| ) | |
| ) | |
| ) | |
| ) | |
| ) | |
| (oo-class Counter | |
| ( | |
| (attr value 0) | |
| (attr skip 1) | |
| ) | |
| ( | |
| (method (next) (set! value (+ value skip)) value) | |
| (method (nextnext) (this 'next) (this 'next)) | |
| (method (set-value newval) (set! value newval)) | |
| (method (set-skip newskip) (set! skip newskip)) | |
| ) | |
| ) | |
| (define c (Counter)) | |
| (display ((c 'next))) | |
| (newline) | |
| (display ((c 'next))) | |
| (newline) | |
| (display ((c 'nextnext)) ) | |
| (newline) | |
| (display ((c 'next))) | |
| (newline) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment