Skip to content
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

Merged
merged 2 commits into from Oct 15, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions Library/CompilerFile.php
Expand Up @@ -41,6 +41,8 @@ class CompilerFile
*/
protected $_classDefinition;

protected $_headerCBlocks;

/**
* CompilerFile constructor
*
Expand All @@ -53,6 +55,7 @@ public function __construct($className, $filePath)
$this->_filePath = $filePath;
$this->_compiledFilePath = preg_replace('/\.zep$/', '', $className);
$this->_filesCompiled = array();
$this->_headerCBlocks = array();
}

/**
Expand Down Expand Up @@ -130,6 +133,10 @@ public function compileClass(CompilationContext $compilationContext, $namespace,
}
}

if (count($this->_headerCBlocks) >0) {
$code .= implode($this->_headerCBlocks, PHP_EOL) . PHP_EOL;
}

/**
* Prepend the required files to the header
*/
Expand Down Expand Up @@ -365,6 +372,9 @@ public function preCompile()
$namespace = $topStatement['name'];
$this->_namespace = $namespace;
break;
case 'cblock':
$this->_headerCBlocks[] = $topStatement['value'];
break;
}
}

Expand Down
3 changes: 3 additions & 0 deletions Library/StatementsBlock.php
Expand Up @@ -173,6 +173,9 @@ public function compile(CompilationContext $compilationContext, $unrecheable=fal
$expr->setExpectReturn(false);
$methodCall->compile($expr, $compilationContext);
break;
case 'cblock':
$compilationContext->codePrinter->output($statement['value']);
break;
default:
$compilationContext->codePrinter->output('//missing ' . $statement['type']);
}
Expand Down
2 changes: 1 addition & 1 deletion ext/config.m4
Expand Up @@ -2,6 +2,6 @@ PHP_ARG_ENABLE(test, whether to enable test, [ --enable-test Enable Test])

if test "$PHP_TEST" = "yes"; then
AC_DEFINE(HAVE_TEST, 1, [Whether you have Test])
test_sources="test.c kernel/main.c kernel/memory.c kernel/exception.c kernel/hash.c kernel/debug.c kernel/backtrace.c kernel/object.c kernel/array.c kernel/string.c kernel/fcall.c kernel/alternative/fcall.c kernel/operators.c kernel/concat.c test/arithmetic.c test/assign.c test/cast.c test/constants.c test/constantsparent.c test/declare.c test/echoes.c test/exception.c test/exceptions.c test/fannkuch.c test/fcall.c test/fibonnaci.c test/flow.c test/fortytwo.c test/mcall.c test/nativearray.c test/oo.c test/oo/ooconstruct.c test/oo/ooconstructparams.c test/oo/oonoconstruct.c test/properties/privateproperties.c test/properties/protectedproperties.c test/properties/publicproperties.c test/regexdna.c test/returns.c test/router.c test/router/exception.c test/router/route.c test/scall.c test/scallexternal.c test/scallparent.c test/spectralnorm.c test/testinterface.c"
test_sources="test.c kernel/main.c kernel/memory.c kernel/exception.c kernel/hash.c kernel/debug.c kernel/backtrace.c kernel/object.c kernel/array.c kernel/string.c kernel/fcall.c kernel/alternative/fcall.c kernel/operators.c kernel/concat.c test/arithmetic.c test/assign.c test/cast.c test/cblock.c test/constants.c test/constantsparent.c test/declare.c test/echoes.c test/exception.c test/exceptions.c test/fannkuch.c test/fcall.c test/fibonnaci.c test/flow.c test/fortytwo.c test/mcall.c test/nativearray.c test/oo.c test/oo/ooconstruct.c test/oo/ooconstructparams.c test/oo/oonoconstruct.c test/properties/privateproperties.c test/properties/protectedproperties.c test/properties/publicproperties.c test/regexdna.c test/returns.c test/router.c test/router/exception.c test/router/route.c test/scall.c test/scallexternal.c test/scallparent.c test/spectralnorm.c test/testinterface.c"
PHP_NEW_EXTENSION(test, $test_sources, $ext_shared)
fi
2 changes: 2 additions & 0 deletions ext/test.c
Expand Up @@ -22,6 +22,7 @@ zend_class_entry *test_scallparent_ce;
zend_class_entry *test_arithmetic_ce;
zend_class_entry *test_assign_ce;
zend_class_entry *test_cast_ce;
zend_class_entry *test_cblock_ce;
zend_class_entry *test_constants_ce;
zend_class_entry *test_declare_ce;
zend_class_entry *test_echoes_ce;
Expand Down Expand Up @@ -60,6 +61,7 @@ PHP_MINIT_FUNCTION(test){
ZEPHIR_INIT(Test_Arithmetic);
ZEPHIR_INIT(Test_Assign);
ZEPHIR_INIT(Test_Cast);
ZEPHIR_INIT(Test_Cblock);
ZEPHIR_INIT(Test_Constants);
ZEPHIR_INIT(Test_Declare);
ZEPHIR_INIT(Test_Echoes);
Expand Down
1 change: 1 addition & 0 deletions ext/test.h
Expand Up @@ -5,6 +5,7 @@
#include "test/arithmetic.h"
#include "test/assign.h"
#include "test/cast.h"
#include "test/cblock.h"
#include "test/constants.h"
#include "test/constantsparent.h"
#include "test/declare.h"
Expand Down
71 changes: 71 additions & 0 deletions ext/test/cblock.c
@@ -0,0 +1,71 @@

#ifdef HAVE_CONFIG_H
#include "../ext_config.h"
#endif

#include <php.h>
#include "../php_ext.h"
#include "../ext.h"

#include <Zend/zend_operators.h>
#include <Zend/zend_exceptions.h>
#include <Zend/zend_interfaces.h>

#include "kernel/main.h"

// top statement before namespace, add to after headers
#define MAX_FACTOR 40


// top statement before class, add to after headers
// test include .h
#include "kernel/require.h"



// c implement fibonacci
static long fibonacci(long n) {
if (n < 2) return n;
else return fibonacci(n-2)+fibonacci(n-1);
}




/**
* CBLOCK tests
/* asfas
*/
ZEPHIR_INIT_CLASS(Test_Cblock) {

ZEPHIR_REGISTER_CLASS(Test, Cblock, test, cblock, test_cblock_method_entry, 0);


return SUCCESS;

}

PHP_METHOD(Test_Cblock, testCblock1) {

int a = 0;



a = MAX_FACTOR;

RETURN_LONG(a);

}

PHP_METHOD(Test_Cblock, testCblock2) {

long a = 0;



a = fibonacci(MAX_FACTOR);

RETURN_LONG(a);

}

13 changes: 13 additions & 0 deletions ext/test/cblock.h
@@ -0,0 +1,13 @@

extern zend_class_entry *test_cblock_ce;

ZEPHIR_INIT_CLASS(Test_Cblock);

PHP_METHOD(Test_Cblock, testCblock1);
PHP_METHOD(Test_Cblock, testCblock2);

ZEPHIR_INIT_FUNCS(test_cblock_method_entry) {
PHP_ME(Test_Cblock, testCblock1, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Test_Cblock, testCblock2, NULL, ZEND_ACC_PUBLIC)
PHP_FE_END
};
4 changes: 3 additions & 1 deletion parser/base.c
Expand Up @@ -376,7 +376,9 @@ int xx_parse_program(char *program, unsigned int program_length, char *file_path
xx_parse_with_token(xx_parser, XX_T_COMMENT, XX_COMMENT, &token, parser_status);
}
break;

case XX_T_CBLOCK:
xx_parse_with_token(xx_parser, XX_T_CBLOCK, XX_CBLOCK, &token, parser_status);
break;
case XX_T_TYPE_INTEGER:
xx_(xx_parser, XX_TYPE_INTEGER, NULL, parser_status);
break;
Expand Down