lci
Stuff related to the LOLCODE interpreter, lci.
4 posts Page 1 of 1
Postby lolfi » Tue Sep 09, 2014 3:49 pm
Dear All.

I've been enjoying the -<future branch of lci recently, and I've been wondering how I can contribute to the project by writing some c modules and integrating them into my dev. version. I'd like to think about a Kitteh way to nom lists to begin.

What would be nice would be a best-practice example of how to integrate my (debugged, validated) c module and enable it to be called in a native lci way. So let's say I've made a very useful module, something like this:

/*C Source*/
int thisisunacceptable(int i){
return i;
}

could I get some advice as to where to go in the lci core to put the equivalent-of-bnf code to invoke this, maybe with a statement like:

1 ISUNACCEPTABLE

and hopefully I can start contributing something useful!

R.
Postby jjm » Tue Sep 09, 2014 10:18 pm
In the future branch there are a couple of ways of doing this: (1) changing lci's core and (2) adding a library. (1) for changing lci's core you could modify the way the language is tokenized, parsed, and interpreted (this is more complex and is best used for implementing changes to the LOLCODE language specification). (2) for adding a library you could bind a function as a library call (this is easier and involves more localized changes but it requires using the library call syntax).

I would recommend trying out option (2) because it will be easier to get up and running and because I think it makes sense to implement features that are not in the LOLCODE spec as library calls. Here's how I would do option (2), note that it only requires making changes to binding.c.
  • Pick a name for your library, let's call it MYLIB.
  • In the loadLibrary function, add a check for your library's name (shown below). This is called the first time the "CAN HAS MYLIB?" statement is reached and it makes all of your functions available to the currently-running code. Note that most of this code is the same regardless of the library being used so if you compare it to the other libraries in binding.c, there are only 3 lines that are different (which I've marked as comments that say "CHANGED").
  • Finally, you need to define the function isunacceptableWrapper using regular C with a bit of extra code to (1) extract the variable that the user called the function with and (2) package the return value of the function (if necessary). Take a look at some of the other *Wrapper functions for different use cases, but I have provided another code snippet example below.
  • Then, you can call it from your code using something like the final code snippet below.
Take a look at bindings.c and https://github.com/justinmeza/httpd.lol ... /httpd.lol for some more complex examples of library calls.

Code: Select all
        } else if (!strcmp(name, "MYLIB")) {  // CHANGED
                lib = createScopeObject(scope);
                if (!lib) goto loadLibraryAbort;

                loadBinding(lib, "ISUNACCEPTABLE", "i", &isunacceptableWrapper); // CHANGED

                id = createIdentifierNode(IT_DIRECT, (void *)copyString("MYLIB"), NULL, NULL, 0); // CHANGED
                if (!id) goto loadLibraryAbort;

                if (!createScopeValue(scope, scope, id)) goto loadLibraryAbort;

                val = createArrayValueObject(lib);
                if (!val) goto loadLibraryAbort;
                lib = NULL;

                if (!updateScopeValue(scope, scope, id, val)) goto loadLibraryAbort;
                deleteIdentifierNode(id);
        }
Code: Select all
ReturnObject *isunacceptableWrapper(struct scopeobject *scope)
{
        ValueObject *arg1 = getArg(scope, "i");
        int i = getInteger(arg1);

        // you can do whatever here

        ValueObject *ret = createIntegerValueObject(i);
        return createReturnObject(RT_RETURN, ret);
}
Code: Select all
HAI 1.4
  CAN HAS MYLIB?
  BTW somewhat verbose syntax, but easy to implement
  VISIBLE I IZ MYLIB'Z ISUNACCEPTABLE YR 1 MKAY
KTHXBYE
Postby lolfi » Wed Sep 10, 2014 8:35 am
Thank you very much!

Yup, it makes perfect sense for things outside the spec to be library call, so method (2) is the way to go.

I think the way to go now is to make my own github repo, stick all of my extensions on there and when I'm sure it's QA-ready, ask teh itteh bitteh kitteh committeh to review and comment. I'll post a reply to this thread once that's done.

Thanks once agin for the speed and quality of your answer!

best wishes
R.
Postby lolfi » Wed Sep 10, 2014 11:00 am
Update:

If this was intended as a gotcha or a Trap For The Lazy, please feel to delete this message ;)

CORRECTION:
VISIBLE I IZ MYLIB'z ISUNACCEPTABLE YR 1 MKAY
SHOULD BE:
VISIBLE I IZ MYLIB'Z ISUNACCEPTABLE YR 1 MKAY
^--------------------------------------case mismatch

xx.R.
4 posts Page 1 of 1

Login

Who is online

Users browsing this forum: No registered users and 1 guest

cron