Hi! Pike team.
I hooked Pike's compile_string() and compile_file(), and got a "new" language. I named it "Spear". In Spear, you can embed C code in Pike, and reference the variables each other. see the example:
$ cat hello.spear #! /usr/bin/env spear
void main() { C.INCLUDE("#include <stdio.h>"); C.CC("gcc -shared -fPIC");
string s="pike"; int n=2008; float pi=3.1415926535;
C{ char* str="c string"; int m=514; double e=1.41421;
printf("hello world!\n");
printf("printf %s string\n",STRING{s}); printf("printf pike int: %d\n",INT{n}); printf("printf pike float: %f\n",FLOAT{pi});
P{s="modify";} printf("%s pike var\n",STRING{s});
P{write("write %s\n",STRING{str});} P{write("write c int: %d\n",INT{m});} P{write("write c float: %f\n",FLOAT{e});}
} }
$ pike -x spear hello.spear hello world! printf pike string printf pike int: 2008 printf pike float: 3.141593 modify pike var write c string write c int: 514 write c float: 1.414
How to install:
Extract the attachment in Pike-v7.7-snapshot and make. Please see the README-spear:
$ cat README-spear Install:
tar xzvf snapshot.tar.gz cd Pike-v7.7-snapshot tar xzvf ../Pike-Spear-vN.N.NNNNNNNNN.tar.gz
Note:
* src/Makefile.in is base on Pike-v7.7.44-20080421, do a vimdiff before use with a diffrent version of Pike.
I hooked Pike's compile_string() and compile_file(), and got a "new" language. I named it "Spear". In Spear, you can embed C code in Pike, and reference the variables each other. see the example:
That's cute. Can you please tell more what happens behind the scenes there?
The main code is in lib/modules/Tools.pmod/Standalone.pmod/spear.pike.
precpp() handled the source before pass it to cpp().
precpp() use Parser.Pike to parse the source, and recognize C{},P{},INT{},FLOAT{},STRING{}.
C{} block will be translate to a temporary c source file and mod "C" will be use to compile the it to a .so file, it will be call later.
If you want left the temporary c source for check, delete the follow lines in spear.pike .
Process.system(sprintf("rm -f /tmp/__c__%d_*.c",getpid())); Process.system(sprintf("rm -f /tmp/__c__%d_*.so",getpid()));
P{} block will be translate to a lambda function which will be send to the C{} block as argument.
The INT{},FLOAT{},STRING{} in the C{} block, will be translate to a lambda function wich return the resulte of the expression.
The INT{},FLOAT{},STRING{} in the Pike{} block, will be translate as parameters of P{} block.
pmod "C" use cmod "Dll" to call dlopen() etc.
you can't use cmod "Dll" directly, it is just for pmod "C".
----- Original Message ----- From: "Martin Stjernholm, Roxen IS @ Pike developers forum" 10353@lyskom.lysator.liu.se To: pike-devel@lists.lysator.liu.se Sent: Thursday, May 01, 2008 9:20 PM Subject: Spear !
I hooked Pike's compile_string() and compile_file(), and got a "new" language. I named it "Spear". In Spear, you can embed C code in Pike, and reference the variables each other. see the example:
That's cute. Can you please tell more what happens behind the scenes there?
Ok. I guess there'll be some tricky cases: Modifying variables from the other language, class scope access from C, global storage in C.. What are you going to use it for?
I have used it to wrap iconv and fast cgi in my project. The attachment is a demo of iconv.
----- Original Message ----- From: "Martin Stjernholm, Roxen IS @ Pike developers forum" 10353@lyskom.lysator.liu.se To: pike-devel@lists.lysator.liu.se Sent: Tuesday, May 06, 2008 4:10 AM Subject: Re: Spear !
Ok. I guess there'll be some tricky cases: Modifying variables from the other language, class scope access from C, global storage in C.. What are you going to use it for?
Of course we will meet tricky cases, but I don't think these three be.
What do you means "Modifying variables from the other language"? I think using P{} to modify pike variable and using C{} to modify C variables is enough.
All pike things can be accessed from C through lambda functions. Maybe I should allow pass object point to C side, not sure.
"global storage in C" can be accessed as follow:
C.INCLUDE("extern int some_global_c_variable;"); C.CC("gcc -shared come_sofile_include_the_variable_define.so");
see the attachement.
----- Original Message ----- From: "Martin Stjernholm, Roxen IS @ Pike developers forum" 10353@lyskom.lysator.liu.se To: pike-devel@lists.lysator.liu.se Sent: Tuesday, May 06, 2008 4:10 AM Subject: Re: Spear !
Ok. I guess there'll be some tricky cases: Modifying variables from the other language, class scope access from C, global storage in C.. What are you going to use it for?
pike-devel@lists.lysator.liu.se