What I want to do is to impliment a Javascript scripting engine and I was wondering if there was a prebuilt one (EG - a C++ Lexical Analyzer that will tokenize Javascript (is that the right phrase?)). I noticed that Windows VC++ (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnexpvb/html/chapter13addingscriptingsupporttoyourapplication.a sp) had one already built and I thought that there might be the samething for Linux.
The purpose of this is to give me a working example and I'm too lazy to build my own scripting language.
Thanks
bwkaz
08-09-2004, 09:57 PM
You might be able to use part of the Mozilla project... I'm not sure if their Javascript interpreter is easy enough to break away from their DOM, though.
Otherwise, does it have to be Javascript? It would be a lot easier to have a Perl, Python, Lisp, or even Tcl interpreter built into your program (you can use the preinstalled Perl, Python, Lisp, or Tcl interpreters on the system, and just link them into your program).
Cerf
08-10-2004, 12:53 PM
Originally posted by bwkaz
You might be able to use part of the Mozilla project... I'm not sure if their Javascript interpreter is easy enough to break away from their DOM, though.
Otherwise, does it have to be Javascript? It would be a lot easier to have a Perl, Python, Lisp, or even Tcl interpreter built into your program (you can use the preinstalled Perl, Python, Lisp, or Tcl interpreters on the system, and just link them into your program).
No, I'm just looking for a working example of a script that I will recognise. Perl would work fine for me, do you know where I could find the interpreter?
bwkaz
08-10-2004, 06:43 PM
Check out the perlembed manpage. ;)
Cerf
08-10-2004, 07:35 PM
Originally posted by bwkaz
Check out the perlembed manpage. ;)
Thanks, but isnt embeded programming programming for microchips and other such systems?
Cerf
08-10-2004, 10:19 PM
I'm having problems compiling my first perl enabled program
[Cerf@localhost ScriptHost]$ make
make: *** No rule to make target `Embed', needed by `gcc'. Stop.
make is interpreting the :: inside the Makefile to mean "the words to the left of this are targets, and the words to the right of this are other targets that those first targets depend on" -- that's the standard usage of colons inside Makefiles.
The error is there because make isn't trying to execute your gcc command (it doesn't even know that it's a command). It's interpreting it as a dependency statement.
What you need is something like:
interp: interp.c
gcc -o interp interp.c $(shell perl -MExtUtils::Embed -e ccopts -e ldopts) The whitespace before the gcc command MUST be a single tab character (NOT a set of spaces!). The $(shell ...) stuff is make's way of executing another command and substituting its output (run the perl -MExtUtils::Embed ... command from a shell sometime to see what it prints out).
Alternately, you could skip the Makefile entirely, and just run the command that the manpage told you to run (basically, this is equivalent to converting your Makefile into a shell script). However, if you do this, you MUST change the single quotes into backticks (since in the manpage, they were backticks, not single quotes).
Cerf
08-11-2004, 08:08 PM
Originally posted by bwkaz
Your Makefile is wrong. ;)
Alternately, you could skip the Makefile entirely, and just run the command that the manpage told you to run (basically, this is equivalent to converting your Makefile into a shell script). However, if you do this, you MUST change the single quotes into backticks (since in the manpage, they were backticks, not single quotes).
For some reason the command the man page gave me didnot work, but the Makefile did work. THANKS
Cerf
08-12-2004, 07:56 PM
I've got an error with the next script
[Cerf@localhost ScriptHost.v.2]$ make
gcc -o Main Main.cpp -Wl,-E -L/usr/local/lib /usr/local/lib/perl5/5.8.5/i686-linux/auto/DynaLoader/DynaLoader.a -L/usr/local/lib/perl5/5.8.5/i686-linux/CORE -lperl -lnsl -ldl -lm -lcrypt -lutil -lc -fno-strict-aliasing -pipe -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/include/gdbm -I/usr/local/lib/perl5/5.8.5/i686-linux/CORE
Main.cpp: In function `int main(int, char**, char**)':
Main.cpp:13: stray '\342' in program
Main.cpp:13: stray '\224' in program
Main.cpp:13: stray '\202' in program
Main.cpp:17: stray '\342' in program
Main.cpp:17: stray '\224' in program
Main.cpp:17: stray '\202' in program
Main.cpp:17: parse error before numeric constant
make: *** [Main] Error 1
It looks like you have characters in your .cpp file that the C++ compiler doesn't understand? Something like that, maybe. Check out line 13 and 17 in a hex editor or equivalent; maybe you'll come up with something that isn't actually whitespace but looks like it.
Or maybe you can run your program through g++ -E to preprocess it, and see if you can figure out where the nonprintable characters are coming from in the output of that. (Maybe it's coming from your Perl include files? Possible, though not likely...)
Edit: Well... when I copy and paste that program into vim, I get a similar error from g++ too (though I only get 2 stray characters, both \277, which is octal for 191 decimal, which is apparently the upside down question mark character (at least in the ISO-8859-1 character set)). Look at your bitwise-OR characters, and make sure they're really pipes and not something else. That's shift-backslash, on most US keyboards anyway...
Cerf
08-13-2004, 11:37 AM
Originally posted by bwkaz
It looks like you have characters in your .cpp file that the C++ compiler doesn't understand? Something like that, maybe. Check out line 13 and 17 in a hex editor or equivalent; maybe you'll come up with something that isn't actually whitespace but looks like it.
Thats what I thought, but I never thought about checking the pipes. Its working now, thanks for your help
Cerf
08-13-2004, 12:00 PM
I worked but I started messing with it, when trying to recompile I got another error that I dont understand
[Cerf@localhost ScriptHost.v.2]$ make
gcc -o Main Main.cpp -Wl,-E -L/usr/local/lib /usr/local/lib/perl5/5.8.5/i686-linux/auto/DynaLoader/DynaLoader.a -L/usr/local/lib/perl5/5.8.5/i686-linux/CORE -lperl -lnsl -ldl -lm -lcrypt -lutil -lc -fno-strict-aliasing -pipe -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/include/gdbm -I/usr/local/lib/perl5/5.8.5/i686-linux/CORE
/tmp/ccTwsfJf.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
make: *** [Main] Error 1
The file /tmp/ccTwsfJf.o does not exist
So after getting this error I restored the origional file so its now
Where did the error come from, I remember having this error before but I forget how to fix it.
Cerf
08-13-2004, 12:05 PM
I renamed Main.cpp to Main.c and the program is working now, can you explain the error to me?
bwkaz
08-13-2004, 07:14 PM
Originally posted by Cerf
Thats what I thought, but I never thought about checking the pipes. Its working now, thanks for your help I only said "check the pipes" because when I copied your post and pasted it into vim, the pipes in your post turned into upside down question marks in vim. Bit of a dead giveaway... :D
The file /tmp/ccTwsfJf.o does not exist No, not anymore. gcc uses files in /tmp to store the output of each of its stages (the preprocessor, compiling into assembly, then assembling into object code, then linking into a library or executable) before the next stage runs.
That error is coming from the linker.
I renamed Main.cpp to Main.c and the program is working now, can you explain the error to me? Well, you're using a .cpp file extension, which is for C++. You are, however, using the system C compiler, gcc. I'm guessing that that's where the gxx_whatever symbol reference is coming from.
You might also try to change gcc to g++ in the Makefile.
Cerf
08-16-2004, 10:36 PM
OK, I've been working with perl embed for awhile. What I want to do now is call a C++ (from within the script host) with the perl script. All the information I need is in 'man perlapi' (specifically the callback functions) or do I have the wrong idea?
bwkaz
08-17-2004, 06:59 PM
Call a C++ function, right? ;)
The "callbacks" section seems to be calling Perl functions from C++ code, not the other way around...
To call C functions from a Perl script, you need to use the perlxs manpage (or the perlxstut, which might be better). That allows you to create a Perl extension, which when compiled turns into a shared library that Perl scripts can call into. Any function in your program that you would want to make Perl-callable will have to have perlxs wrappers bundled in a Perl extension. And I'm not sure on C++, but I think you'd be able to use g++ instead of gcc with an option in your extension's Makefile.PL file.
I think. I haven't really done Perl, but this is what the perlxstut manpage seems to imply...
Cerf
08-17-2004, 07:02 PM
Originally posted by bwkaz
Call a C++ function, right? ;)
The "callbacks" section seems to be calling Perl functions from C++ code, not the other way around...
Thanks for the clarification, so if callbacks are calling Perl fucntions from C++ code, what is calling a C++ from a perl script called?
And to clarify, I have to make a shared library to do this right? I cant have the perl callable function from the host program?
bwkaz
08-17-2004, 09:43 PM
Originally posted by Cerf
Thanks for the clarification, so if callbacks are calling Perl fucntions from C++ code, what is calling a C++ from a perl script called? Apparently perlxs (or something like that). At least, that's the name of the relevant manpage (but read perlxstut also)...
And to clarify, I have to make a shared library to do this right? I cant have the perl callable function from the host program? That's my understanding of the perlxstut manpage examples (at least the first 2 or 3), yes. Basically, the XSUB file will call your C functions (C++ might be doable, I'm not sure), and it will compile into the Perl functions that the user can call.
Basically, it's like this:
User's Perl code --> function from the .xs file --> your C/C++ function
where the --> are function calls. The .xs file looks like C code, but gets compiled into something that Perl code can call into. That something is going to have to link against your C/C++ functions, though, to be able to call them -- however, that can be solved by compiling your program with -Wl,--export-dynamic, which will make it so that the .xs file's shared object can call functions from your main executable once it's linked into that executable.
Maybe an example would help? The .xs file here almost assuredly isn't syntactically correct, but I think you'll get the idea. First, one function from your main program (this is meant to be Perl-callable):
static int gravity = 10; /* your game's gravity value,
approximately in meters per second per second */
int setGravity(int newGravityValue)
{
if(newGravityValue <= 0) {
return -1; /* signal error -- the value can be anything */
}
gravity = newGravityValue;
return 0; /* success -- again, value can be anything */
} That's a global function, which would have a matching declaration in a header file somewhere. When the executable containing this function is compiled with -Wl,--export-dynamic, this function can be called from any shared library that this executable links to.
Then, the .xs file (sort of):
#include "your-header-file.h"
int
perlSetGravity(newGravityValue)
int newGravityValue
CODE:
RETVAL = setGravity(newGravityValue);
OUTPUT:
RETVAL Use the h2xs script (referenced in the perlxstut manpage) to generate stubs for these files. That will also generate a basic Makefile.PL, a .pm file, etc., etc. Generate a Makefile from Makefile.PL (with a "perl Makefile.PL"), and then run make, and you should have built the XSUB shared library.
Then, you can use your .pm Perl module from any Perl code (e.g., stuff that the user writes), and you can call perlSetGravity() from that. perlSetGravity will return 0 if successful or -1 otherwise (at least the way I have it written here). Link your main program against this shared library, so that the shared lib can actually call the C version of setGravity().
I have not tested this, but based on the examples in the perlxstut manpage, it should work. (Famous last words... ;))
justlinux.com
Copyright Internet.com Inc. All Rights Reserved.