Checking if a name is actually defined (and of correct type)
Is it possible to check if a function is defined, and use it as the Just
value of a Maybe type if it is? And use Nothing if it's not defined, of
course.
I'm writing a wrapper around atom for use with the TI MSP430 line. Part of
what I'm doing is making a function that quickly compiles the code in the
right format for MSP430 controllers - for example, compiling an atom to
use in a timer interrupt requires a function definition like so:
#pragma vector=TIMERA0_VECTOR __interrupt
void timerAisr(void) {
...
}
At the moment, I have an object that holds references to the function the
user would like to use for each different ISR. It looks a bit like this:
mspProgram = MSP430Compilation {
setupFn = Nothing,
setupFnName = "setup",
loopFn = Nothing,
loopFnName = "loop",
timerAISR = Nothing,
timerAISRName = "timerAISR",
And so on. Very configurable - you can choose the name of the function to
output in C code, and the Atom to compile for that function. But I've
decided I'd like to take more of a convention-over-configuration approach
and basically assume some sensible function names. So instead of passing
one of these configuration objects, I want the compilation code to check
for definitions of sensibly-named functions.
For example, if the user defines an Atom called timerAISR, then my code
should compile that atom to a C function named the same, with the
appropriate #pragma matter for it to service the timer A interrupt.
So what I need to do is sort of meta-Haskell, checking if the user has
defined a function and using that in my library code. I imagine this might
involve template Haskell, so I'm off to research it.
No comments:
Post a Comment