示例#1
0
Variant*
ast_decl_execute(AST_Decl* node,
                 Variant* result)
{
    if (   node
        && result
        && !runtime_is_returning())
    {
        /* retrieve the declaration default value */
        ast_execute(node->value, result);

        /* cast the default value to the declaration type */
        switch (node->type)
        {
            case AST_DECL_BOOLEAN : variant_cast(result, VBOOLEAN); break;
            case AST_DECL_INT32 :   variant_cast(result, VINT32);   break;
            case AST_DECL_FLOAT32 : variant_cast(result, VFLOAT32); break;
            case AST_DECL_TEXT :    variant_cast(result, VTEXT);    break;
            default:                                                break;
        }

        /* allocate memory in the runtime for the declaration */
        Variant* store = runtime_add(node->name, result->type);
        if (store)
        {
            /* assign the calculated value to the allocated memory */
            variant_assign(store, result);
        }
    }

    return result;
} /* end of : Variant*
int main() {

  /* Example of a runtime function. */
  {
    int a = 3, b = 5, c = 7;

    int r1 = runtime_add(a, b);
    int r2 = runtime_add(a, c);

    std::cout << r1 << std::endl;
    std::cout << r2 << std::endl;
  }

  /* Example of a compile-time function for constant values. */
  {
    const int a = 3, b = 5, c = 7;

    const int r1 = compile_time_add<a, b>::value;
    const int r2 = compile_time_add<a, c>::value;

    std::cout << r1 << std::endl;
    std::cout << r2 << std::endl;
  }

  /* Example of a compile-time function for types. */
  {
    using a = int;
    using b = float;

    using r1 = as_ptr<a>::type;
    using r2 = as_ptr<b>::type;

    std::cout << typeid(r1).name() << std::endl;
    std::cout << typeid(r2).name() << std::endl;
  }
}