Closed rpcope1 closed 10 years ago
I think you should be in the clear. If you notice any bugs with basic arithmetic, file an issue and I'll fix that. I'm guessing you will mainly be working with floats, so use the TypeFloat cast to convert values. TypeInt is defined too, but it's missing a declaration in the corresponding header file. In general, VALUEs work like unions, except each field has its own memory location. The type field defines what type a value is. I'll give you an example of sqrt() to start off, though I see you already have started.
/* mathlib.c */
#include <math.h>
#include "interpreter.h"
int MathSqrt(int arg_count)
{
VALUE numf = GetRecord("numf", gCurrentContext);
float num = TypeFloat(numf); /* from interpreter.h, in top of duck.c */
num = sqrtf(num);
/* return value is set in gLastExpression */
/* this convention may change in the future */
gLastExpression.type = VAL_FLOATING_POINT; /* could return VAL_NIL with error */
gLastExpression.floatp = num;
return 0; // no error, error halts program
}
/* called in library.c */
void BindMathLibrary() // could be Math.func() or duck.math.func() or both
{
VALUE mathlib = LinkNamespace("Math");
VALUE mathsqrt = CreateFunction(MathSqrt);
AddParameter(mathsqrt, "numf");
LinkFunction(mathlib, "sqrt", mathsqrt);
}
If you get a good, consistent library defined/implemented then I would gladly merge it into the main branch.
Cheers, Greg
Awesome, thanks! Yeah I can see a couple of things that should shorten my code a little bit (like TypeFloat()).
One question, though, in your example you don't throw an error ever, however if the user throws something like a string like "lol", which doesn't represent a valid float/int, to the function, would it be appropriate to throw a fault somehow? I see that you use atof to try and get the float out a string, which is decent, but it's slightly worrisome that atof does undefined things when I throw a string down into it like atof("hello"). You could always just say user beware, but maybe utilizing strtof() as in http://stackoverflow.com/a/5581058 to check that whatever was thrown down into the TypeFloat() was well behaved, would be cool. I am not sure how you would return an error from a type conversion, but perhaps you have some ideas?
Thanks again for your input and help, I really appreciate it.
Best Regards, Robert
That would probably be better. It would also make a lot of sense to add exception handling to duck.
I think we can close this now, I guess the Math library has been added (maybe could add a few other things, like hyperbolic trig functions).
I probably said this already, but this is a pretty slick project, and I enjoy the hands on C coding especially with a language this new (lot more fun than writing C libraries for Python is any more). Your TODO list includes building up a standard library, and a few other things. Is there any other tasks that aren't high on your priority list that you wanted to get done (add another library, maybe)? I'm just interested in continuing contributing if you'd like the help.
Hello! I saw your post about duck-lang on Hacker News today, and decided to fork and probe around on this project, and it looks pretty exciting. :) I am interested in getting my hands dirty with this language, but noticed you're lacking a basic math library (you know like trig functions, sqrt, etc.). I have started to add a "math" library to my local duck repo, and am interested to see if you might not take a pull request on it once I finish the implementation? Really, I want to start by providing most of the major functions from math.h; I have already added and started to test sin/cos/tan. Also, is there anything I should be wary of?