New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added new syntax named 'CBLOCK' . The content in %{ }%
will copied into the generated .c file
#21
Conversation
I like the idea, however, this would sometimes make things harder for the compiler, because there could be unregistered activity the language cannot track, for instance: %{
a = fibonacci(MAX_FACTOR);
}% The variable 'a' is mutated inside a "shadow" block of code, for Zephir, the variable wasn't mutated so the variable will be reported as unused, but the variable was actually used! Our idea of integrate C-functions into Zephir is via optimizers. Check this example: It intercepts calls to 'strlen' and replaces the generated code by a call to a c-function. It also adds the proper header: Returns a 'compiled expression' which has the right data-type Zephir can understand. |
@phalcon I agree. In the most case, Zephir user should use Optimized Function Calls we provided. Zephir is a DSL and Generate to c . For instance
Go back to my example But Zephir with CBLOCK, we can implement simple c-function And the other example is Zephir currently not support So, with Optimizer FunctionCall + CBLOCK will make Zephir more flexible and these two features were complementary not mutex . Finally, You CAN calling c-function, implements c-function in |
I'm not opposed on allowing integrate 3rd party libraries in Zephir or call C-code, I think this is obviously something required. But just embed c-code wherever a developer wants looks ugly and it makes Zephir completely unusable. That would restrict us to make optimizations just because there could be hidden code that Zephir can't understand or analyze. The idea is allow the developer to write his own optimizers having clean interfaces allowing Zephir to know the kind of parameters that must be passed to C-functions and also which data-types are returned. For example, you could have the following file structure. optimizers/
fibonacci.php # this intercepts calls to function "fibonacci" telling Zephir which header include and the c-code
ext/
test/
myclass.c # code generated
kernel/
my-fibonacci.h # fibonacci prototype
my-fibonacci.c # here is implemented the fibonacci in "c"
test/
myclass.zep # here we call a function "fibonnacci" calling the c version fibonacci.php: <?php
class FibonacciOptimizer
{
public function optimize(array $expression, Call $call, CompilationContext $context)
{
$context->headersManager->add('my-fibonnaci.h');
$resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression);
return new CompiledExpression('int', 'fibonacci(' . $resolvedParams[0] . ')', $expression);
}
} my-fibonacci.h: int fibonacci(int n); my-fibonacci.c: int fibonacci(long n) {
if (n < 2) return n;
else return fibonacci(n - 2) + fibonacci(n - 1);
} myclass.zep namespace Test;
class MyClass
{
public function someMethod()
{
int a;
a = fibonacci(40);
return a;
}
} This will be an structured way of integrate C functions in a way that Zephir can understand. Zephir can mutate the variable 'a' in a controlled way. Even if you do the following, Zephir will do the right thing: int a;
a = fibonacci(40);
return a; var a;
a = fibonacci(40);
return a; double a;
a = fibonacci(40);
return a; return fibonacci(40); var a = [];
let a = [fibonnaci(1), fibonnaci(2)];
return a; All this without have to write C-code! |
@phalcon I agree and understand your concerns. Please consider that CBLOCK or (c-code with comments) is part of Zephir features, It make Zephir flexible. The most projects(and Phalcon 2.0) developers SHOULD NOT using CBLOCK to implements or calling c-function. As list before, Google's Thanks. |
Could you please rebase against the current master? |
Signed-off-by: Rack Lin <racklin@gmail.com>
Signed-off-by: Rack Lin <racklin@gmail.com> Conflicts: parser/parser.c parser/parser.out parser/scanner.c
@phalcon Hi, I have rebase and push again. Thanks. |
Added new syntax named 'CBLOCK' . The content in `%{ }%` will copied into the generated .c file
Thanks |
Hi.
I have implement a new syntax named 'CBLOCK'.
The idea is inspired by Mozilla's XPIDL C++ Block . http://www-archive.mozilla.org/scriptable/xpidl/idl-authors-guide/keywords.html
Syntax is
The CBLOCK copied text into generated .c file.
It is useful and help us to call C functions more easier, and have ability to using 3rd libs in zephir.
The CBLOCK before
namespace
class
will copied CONTENT to generated .c file and after headers .The CBLOCK inside method statement, will copied CONTENT to generated .c file
inline
(AT IT's Position)Here is unit-test for example:
AND Generated .c file below: