CSC195 2013S, Class 13: Racket Internals (1): Overview Overview: * About Racket * It's Going to Seem Familiar * Extension Basics * Compilation * Loading Admin: * You're probably busy enough this week that there is no homework! * For what we're doing today, it's helpful to have a reference page up. "Inside Racket" * Please attend next week's class for debriefing! About Racket * Racket is a variant of Scheme * Descendent of PLT Scheme * PLT Scheme is the underlying implementation of Media Scheme * We will be transitioning to Racket * And to their IDE, DrRacket * We're going to learn to write new functions (in C) that can be called from Racket (essentially, a Scheme interpreter) It's Going to Seem Familiar * What do I include? * What's the boilerplate? * Functions can take different numbers of parameters. How do we deal with that? (The signature of the functions we write) * Functions have to return something. What does the return value look like * Declaration of variables * How do we tell the system about our new functions? * Once we've created the C code, how do we compile it and install it? * Generic type! Extension Basics * #include #include * Special type: Scheme_Object (generally pointer to) * Three boilerplate functions Scheme_Object *scheme_initialize(Scheme_Environment *env) Scheme_Object *scheme_reload (Scheme_Environment *env) Scheme_Object *scheme_module_name (void) * The new functions you design Scheme_Object *FUNCTION(int argc, Scheme_Object *argv[]) * Adding them to the C code. Two steps! * First, convert the C function to a Scheme function proc = scheme_make_prim_w_arity (CFUNCTION, NAME, MINARITY, MAXARITY)q * Next, tell the Scheme environment about the function scheme_add_global (NAME, proc, env) Compilation * Two steps! * Compile to .o raco ctool --cc * Link to .so raco ctool --ld Loading * The simple answer (load-library "whatever.so") * More sophisticated approach coming in the future On to Real Code * We'll read some * We'll write some Strange issue: There are (at least) four names we worry about * the name from scheme_module_name * the name of your C function * the name used in scheme_make_prim_w_arity * the name associated with the fucntion behind the scenes (for error reporting and such) * the name used in scheme_make_global * The name you use when you call the function