fb39ca4 / picoc

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

Allow intrinsic functions to take variant types [Feature] #95

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
This would allow functions like:

  int X = 1;
  char* Y = "Hello";
  printMsg("%1%1%1: %2-%2\n", &X, Y);

This can be achieved with function ExpressionParseFunctionCall:

    /* parse arguments */
    ArgCount = 0;
    do {
        if (Parser->Mode == RunModeRun && ArgCount < FuncValue->Val->FuncDef.NumParams)
            ParamArray[ArgCount] = VariableAllocValueFromType(Parser, FuncValue->Val->FuncDef.ParamType[ArgCount], FALSE, NULL, FALSE);

        if (ExpressionParse(Parser, &Param))
        {
            if (Parser->Mode == RunModeRun)
            {
                if (ArgCount < FuncValue->Val->FuncDef.NumParams)
                {
                    ExpressionAssign(Parser, ParamArray[ArgCount], Param, TRUE, FuncName, ArgCount+1, FALSE);

                    if(FuncValue->Val->FuncDef.Intrinsic)
                    {
                        if((Param->Val->Typ != VoidPtrType) && (ParamArray[ArgCount]->Typ == VoidPtrType) && (Param->Typ->Base == TypePointer))
                        {
                            // Copy the from type to allow variant behaviour in intrinsic functions
                            ParamArray[ArgCount]->Typ = Param->Typ;
                        }
                    }

                    VariableStackPop(Parser, Param);

I have add a bit between ExpressionAssign and VariableStackPop.
The reason it doesn't work now is the intrinsic function sees the data type 
from function declaration, not the passed in type.
This allows the intrinsic function to obtain the data type of passed in 
arguments.
In my example the variant arguments must be pointers.

Original issue reported on code.google.com by duncan.f...@gmail.com on 27 Jul 2010 at 4:41

GoogleCodeExporter commented 8 years ago
Thanks for the idea.

I've changed the type from "defect" to "enhancement". I'll add it to the 
feature request list for post 2.0 release.

Original comment by zik.sale...@gmail.com on 27 Jul 2010 at 8:33

GoogleCodeExporter commented 8 years ago
I think this already works. See the definition of printf() for an example.

Original comment by zik.sale...@gmail.com on 15 Feb 2011 at 4:41