Subject: instanceof operator
Fellow ChucKers,
I was looking for a way to wrap functionality over different instrument
classes. My strategy for this is to determine the object's class at
runtime, and cast it to an instance of that class. To support this I added
a new operator, that works somewhat like 'instanceof' in the Java
language.
It's not a common use operator, but may be useful for those who want to
build larger OO programs in ChucK.
I have attached three files:
instanceof5.patch - patch from 'diff -ur' on my version vs. v2 sources (* 1 2)
instanceof.ck - basic demonstation of operator functionality
StkWrap.ck - proof of concept ugen wrapper - wraps ugens, plays notes
If this is useful, feel free to modify this for language design concerns.
Or, I can take suggestions for modifications.
Here's what I did:
1. Added an INSTANCEOF token to chuck.lex.
2. Added an instanceof_expression to chuck.y.
3. In chuck_emit.cpp, it emits a new VM instruction. It binds the
right-hand type at compile time.
4. The implementation of the instruction:
a. pops two words off the register stack (disregarding the rhs)
b. compares the actual_type of the static right-hand type
c. with the left-hand type
d. if it doesn't match, try the parent of the left-hand type
e. until parent is NULL
f. pushes a 1 or 0 to the regular stack
* I tried to make the patch using a cvs diff, but something breaks in my
client TortoiseCVS when I try to make the patch vs. anon-chuck. So, I
used 'diff -ur' against a clean copy of v2 and filtered out the
generated files.
In C:\Documents and Settings\Graham\My Documents\projects\chuck_dev:
"C:\Program Files\TortoiseCVS\cvs.exe"
"-q" "diff" "-u" "v2" CVSROOT=:ext:anon-chuck@cvs.cs.princeton.edu:/cvs
cvs.exe diff: warning: unrecognized response `Could not chdir to home
directory /cvs/projects/chuck/homedirs/anon-chuck: Permission denied' from
cvs server
1. chuck_win32.c is omitted from this patch. You can generate it in the
usual way from the flex and bison files. If it would save time, I can send
it.
2. Could someone test this on mac or unix?
much respect to the chuck team,
Graham