foralex / picoc

Automatically exported from code.google.com/p/picoc
0 stars 0 forks source link

Documentation: Simplification / typos in "Adding native C library functions" #138

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. In the document "Adding native C library functions" there is a section 
called "Passing pointers to structures and unions". This section shows how to 
predefine a complex number structure. After some experimentation it looks like 
this process can be greatly simplified. In fact the example code shown can be 
removed entirely.

The whole definition of "struct complex { int i; int j; }" can be done like 
this:

IncludeRegister("win32.h", &win32SetupFunc, &win32Functions[0], "struct complex 
{ int i; int j; };");

The definition of the complex structure can be done by just adding the struct 
definition when calling "IncludeRegister" (when adding the functions that are 
going to use the new structure). Note the trailing ';' in the string defining 
the struct!

2. If one still would like to used the example code, there are a couple of 
changes that are needed to make it compile:

    ...
    char *IntrinsicName = TableStrRegister("complex library");  <== remove 'const'
    const char *StructDefinition = "struct complex { int i; int j; }"; 

    /* define an example structure */ 
    Tokens = LexAnalyse(IntrinsicName, StructDefinition, strlen(StructDefinition), NULL); 
    LexInitParser(&Parser, StructDefinition, Tokens, IntrinsicName, TRUE, FALSE);  <== added 'FALSE' as a last parameter
    TypeParse(&Parser, &ParsedType, &Identifier, &IsStatic);  <== added output parameter 'IsStatic'
    HeapFreeMem(Tokens);  <== changed name of function

What is the expected output? What do you see instead?

What version of the product are you using? On what operating system?
Latest SVN read-only version dated 6th of October 2011.

Please provide any additional information below.

Original issue reported on code.google.com by jjohans...@gmail.com on 9 Oct 2011 at 12:37

GoogleCodeExporter commented 8 years ago
Here's another way of defining structures.

static void lib_parse(const char* definition)
{
    PicocParse("my lib", definition, strlen(definition), TRUE, TRUE, FALSE);
}
void PlatformLibraryInit()
{
    lib_parse("struct tm { int hour; int minute; int second; int year; int month; int day; };");
    lib_parse("struct yuv { int Y; int U; int V; };");
    lib_parse("struct rgb { int R; int G; int B; };");
    ...
    lib_parse("#define M_PI 3.1415");
    lib_parse("#define ABS(a) ((a) < (0) ? -(a) : (a))");
    lib_parse("#define SGN(a) ((a) > 0 ? 1 : (a) < 0 ? -1 : 0)");
}

Original comment by broscuta...@gmail.com on 10 Mar 2013 at 1:14

GoogleCodeExporter commented 8 years ago
I've made these changes to the wiki, thanks for the suggestions.

Original comment by zik.sale...@gmail.com on 16 Mar 2013 at 5:37