Hi everybody:
I'm intend to write a small game environment using c++, and
I'd like to employ some script tech to enhance its flexibilty. I think
Lisp is a good choice, and I'd like to know how I can use Common Lisp
as a scripting language within the C++ engine.
I know the librep but it seems incomplete, and I just want to imitate
the Emacs' way.
So if there exsit some good solution to this problem, please give me
some suggestions. Thanks!
loiy Shaw
PKU
2008-3-17
From: Sohail Somani
Subject: Re: Howto use Lisp as scripting language within C++?
Date:
Message-ID: <wuoDj.113088$C61.4768@edtnps89>
On Sun, 16 Mar 2008 23:25:49 -0700, blackguester wrote:
> Hi everybody:
> I'm intend to write a small game environment using c++, and
> I'd like to employ some script tech to enhance its flexibilty. I think
> Lisp is a good choice, and I'd like to know how I can use Common Lisp
> as a scripting language within the C++ engine. I know the librep but it
> seems incomplete, and I just want to imitate the Emacs' way.
> So if there exsit some good solution to this problem, please give me
> some suggestions. Thanks!
http://ecls.sourceforge.net/ seems like a start.
--
Sohail Somani
http://uint32t.blogspot.com
On Sun, 16 Mar 2008 23:25:49 -0700, blackguester wrote:
> Hi everybody:
> I'm intend to write a small game environment using c++, and
> I'd like to employ some script tech to enhance its flexibilty. I think
> Lisp is a good choice, and I'd like to know how I can use Common Lisp
> as a scripting language within the C++ engine. I know the librep but it
> seems incomplete, and I just want to imitate the Emacs' way.
> So if there exsit some good solution to this problem, please give me
> some suggestions. Thanks!
Bah .. Do it the other way around. Write everything in Lisp, then
use C (not C++) when/if needed instead.
http://en.wikipedia.org/wiki/Game_Oriented_Assembly_Lisp
http://c2.com/cgi/wiki?LispInJakAndDaxter
https://webmail.iro.umontreal.ca/pipermail/mslug/2008-February/000274.html
https://www.cmpevents.com/GD08/a.asp?option=G&V=3&id=562983
Ignore the silly hybrid C++. It just complicates things. It's almost
impossible to communicate with software written in C++ if the (library)
developers haven't thought hard about FFI'ing etc. which they almost
_never_ do. If you need the high-level features that C++ _try_ to
provide, write them in Lisp instead. Try to optimize, then rewrite
later if needed.
http://thread.gmane.org/gmane.comp.version-control.git/57643/focus=57918
http://groups.google.no/group/comp.lang.lisp/msg/917737b7cc8510e3
Let optimization for performance be the last thing you do:
http://c2.com/cgi/wiki?PrematureOptimization
http://www.extremeprogramming.org/rules/optimize.html
..heck.. SBCL turns out to be faster than C in some cases, even for
some "boring linear" situations where you'd normally expect C to
shine.
http://weitz.de/cl-ppcre/
"Used with a Lisp compiler which compiles to native code it is on par
with Perl's highly optimized regex engine (written in C) which to my
knowledge is faster than most other regex engines around."
http://www.lrde.epita.fr/cgi-bin/twiki/view/Publications/200606-IMECS
http://www.cs.bham.ac.uk/~wbl/biblio/gp-html/1144168.html
--
Lars Rune Nøstdal
http://nostdal.org/
Blackguester <············@gmail.com> writes:
> On Mar 17, 3:46 pm, Lars Rune N��stdal <···········@gmail.com> wrote:
> > On Sun, 16 Mar 2008 23:25:49 -0700, blackguester wrote:
> > > Hi everybody:
> > > I'm intend to write a small game environment using c++, and
> > > I'd like to employ some script tech to enhance its flexibilty. I think
> > > Lisp is a good choice, and I'd like to know how I can use Common Lisp
> > > as a scripting language within the C++ engine. I know the librep but it
> > > seems incomplete, and I just want to imitate the Emacs' way.
> > > So if there exsit some good solution to this problem, please give me
> > > some suggestions. Thanks!
> >
> > Bah .. Do it the other way around. Write everything in Lisp, then
> > use C (not C++) when/if needed instead.
> >
> > http://en.wikipedia.org/wiki/Game_Oriented_Assembly_Lisp
> > http://c2.com/cgi/wiki?LispInJakAndDaxter
> > https://webmail.iro.umontreal.ca/pipermail/mslug/2008-February/000274...
> > https://www.cmpevents.com/GD08/a.asp?option=G&V=3&id=562983
> >
> > Ignore the silly hybrid C++. It just complicates things. It's almost
> > impossible to communicate with software written in C++ if the (library)
> > developers haven't thought hard about FFI'ing etc. which they almost
> > _never_ do. If you need the high-level features that C++ _try_ to
> > provide, write them in Lisp instead. Try to optimize, then rewrite
> > later if needed.
> >
> > http://thread.gmane.org/gmane.comp.version-control.git/57643/focus=57918
> > http://groups.google.no/group/comp.lang.lisp/msg/917737b7cc8510e3
> >
> > Let optimization for performance be the last thing you do:
> >
> > http://c2.com/cgi/wiki?PrematureOptimization
> > http://www.extremeprogramming.org/rules/optimize.html
> >
> > ..heck.. SBCL turns out to be faster than C in some cases, even for
> > some "boring linear" situations where you'd normally expect C to
> > shine.
> >
> > http://weitz.de/cl-ppcre/
> >
> > "Used with a Lisp compiler which compiles to native code it is on par
> > with Perl's highly optimized regex engine (written in C) which to my
> > knowledge is faster than most other regex engines around."
> >
> > http://www.lrde.epita.fr/cgi-bin/twiki/view/Publications/200606-IMECS
> > http://www.cs.bham.ac.uk/~wbl/biblio/gp-html/1144168.html
> >
> > --
> > Lars Rune N��stdalhttp://nostdal.org/
>
> Great guide to me, I'm migrating from c++ to lisp these days, but I'll
> not abandon c++, it's also a great language :) I'm loving its
> metaprogramming feature.
If you are even semi-serious here, you really should reconsider the
advice Lars has given. C++ "metaprogramming" is at best the analog of
"stone knives and bearskins". Even if you had to write the whole
thing first as a learning exercise, and then throw it out and rewrite
it after you are rather comfortable with Lisp, it's quite plausible
that you could still have it done in less time than if you had used
C++ up front. And it's almost certain that it would have more
functionality, be far more flexible for evolving further, and still be
about as fast.
/Jon
--
'j' - a n t h o n y at romeo/charley/november com
P� Mon, 17 Mar 2008 07:25:49 +0100, skrev <············@gmail.com>:
> Hi everybody:
> I'm intend to write a small game environment using c++, and
> I'd like to employ some script tech to enhance its flexibilty. I think
> Lisp is a good choice, and I'd like to know how I can use Common Lisp
> as a scripting language within the C++ engine.
> I know the librep but it seems incomplete, and I just want to imitate
> the Emacs' way.
> So if there exsit some good solution to this problem, please give me
> some suggestions. Thanks!
>
>
>
>
> loiy Shaw
> PKU
> 2008-3-17
You might want to look at ECL
http://ecls.sourceforge.net/
It is a reasonably complete implementation of ANSI CL which is designed to
be embedded with C/C++.
Unlike most Lisp compilers it can compile to C (or interpret code)
It can work with gcc, mingw and also Microsoft Visual C++ under Windows.
--------------
John Thingstad
On Mar 17, 2:57 pm, "John Thingstad" <·······@online.no> wrote:
> På Mon, 17 Mar 2008 07:25:49 +0100, skrev <············@gmail.com>:
>
> > Hi everybody:
> > I'm intend to write a small game environment using c++, and
> > I'd like to employ some script tech to enhance its flexibilty. I think
> > Lisp is a good choice, and I'd like to know how I can use Common Lisp
> > as a scripting language within the C++ engine.
> > I know the librep but it seems incomplete, and I just want to imitate
> > the Emacs' way.
> > So if there exsit some good solution to this problem, please give me
> > some suggestions. Thanks!
>
> > loiy Shaw
> > PKU
> > 2008-3-17
>
> You might want to look at ECL
>
> http://ecls.sourceforge.net/
>
> It is a reasonably complete implementation of ANSI CL which is designed to
> be embedded with C/C++.
> Unlike most Lisp compilers it can compile to C (or interpret code)
> It can work with gcc, mingw and also Microsoft Visual C++ under Windows.
>
> --------------
> John Thingstad
That's a good solution, but I also look the clisp which provided FFI
and External Modules that seems can do the same thing, maybe except
the native code generate. And it's well documented.
BTW, I think the gnu guile is another alternative.
However, Thank u for help.
On Tue, 18 Mar 2008 01:16:00 -0700, Blackguester wrote:
> On Mar 17, 2:57 pm, "John Thingstad" <·······@online.no> wrote:
>> På Mon, 17 Mar 2008 07:25:49 +0100, skrev <············@gmail.com>:
>>
>> > Hi everybody:
>> > I'm intend to write a small game environment using c++,
>> > and
>> > I'd like to employ some script tech to enhance its flexibilty. I
>> > think Lisp is a good choice, and I'd like to know how I can use
>> > Common Lisp as a scripting language within the C++ engine. I know the
>> > librep but it seems incomplete, and I just want to imitate the
>> > Emacs' way.
>> > So if there exsit some good solution to this problem, please give me
>> > some suggestions. Thanks!
>>
>> > loiy Shaw
>> > PKU
>> > 2008-3-17
>>
>> You might want to look at ECL
>>
>> http://ecls.sourceforge.net/
>>
>> It is a reasonably complete implementation of ANSI CL which is designed
>> to be embedded with C/C++.
>> Unlike most Lisp compilers it can compile to C (or interpret code) It
>> can work with gcc, mingw and also Microsoft Visual C++ under Windows.
>>
>> --------------
>> John Thingstad
>
> That's a good solution, but I also look the clisp which provided FFI
..almost all Lisps have FFI APIs. There is work to create a common
interface for them through CFFI:
http://common-lisp.net/project/cffi/
..and here is the documentation for the SBCL FFI API:
http://www.sbcl.org/manual/Foreign-Function-Interface.html
> maybe except the native code generate.
If you need this SBCL provides Lisp --to--> native asm/machine code
compilation.
SBCL is not embeddable like ECL, but -- that does not matter if you turn
things around a bit.
If you have the Lisp side start first then it does not matter whether the
Lisp is embeddable or not. Almost all the Lisps can embed C software - while
C software can not embed all Lisps. Just compile with -fPIC and link
as a shared library instead of a executable.
When you've done this you have what you need:
* Lisp can call C
* C can call Lisp
..and you can do whatever you want.
Here is a quick example. Two files:
call-lisp-from-c.c
------------------
typedef int(*SomeFunction)(int, int);
int doIt(SomeFunction func, int a, int b)
{
return func(a, b) * 2;
}
call-lisp-from-c.lisp
---------------------
(eval-when (:execute :load-toplevel :compile-toplevel)
(require :cffi))
(defpackage :call-lisp-from-c
(:use :cl :cffi))
(in-package :call-lisp-from-c)
(defparameter *gcc-path* "/usr/bin/gcc")
(defmacro awhile (result-symbol test-form &body body)
`(do ((,result-symbol ,test-form ,test-form))
((not ,result-symbol))
,@body))
(defun gcc (args)
(let* ((process (sb-ext:run-program *gcc-path* args :output :stream))
(process-output-string
(with-output-to-string (str)
(awhile line (read-line (sb-ext:process-output process) nil nil)
(write-line line str))
str))
(process-exit-code (sb-ext:process-exit-code process)))
(if (zerop process-exit-code)
process-exit-code
(error process-output-string))))
(defun compile-c-side ()
(gcc '("-g" "-Wall" "-fPIC" "-c" "call-lisp-from-c.c")))
(defun link-c-side ()
(gcc '("-shared" "-o" "libcall-lisp-from-c.so" "call-lisp-from-c.o")))
(defun compile-and-link-c-side ()
(compile-c-side)
(link-c-side))
;;; Lisp side "application" stuff starts here
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; This is a function C will call in this example.
(defcallback a-lisp-function :int ((a :int) (b :int))
(format t "a-lisp-function: ~A + ~A = ~A~%" a b (+ a b))
(+ a b))
(defun main (a b)
(load-foreign-library "libcall-lisp-from-c.so")
(foreign-funcall "doIt"
:pointer (callback a-lisp-function)
:int a
:int b
:int))
..and we try it:
CALL-LISP-FROM-C> (main 2 3)
a-lisp-function: 2 + 3 = 5
10
CALL-LISP-FROM-C> (main 10 50)
a-lisp-function: 10 + 50 = 60
120
CALL-LISP-FROM-C>
..fun.. :)
--
Lars Rune Nøstdal
http://nostdal.org/
note that it would be easy to send C code directly from Lisp to GCC
instead of supplying GCC with a filename for it to compile/link
create a DSL that compile to C if you like .. it's easy in Lisp
--
Lars Rune Nøstdal
http://nostdal.org/
P� Tue, 18 Mar 2008 12:09:21 +0100, skrev Lars Rune N�stdal
<···········@gmail.com>:
> If you have the Lisp side start first then it does not matter whether the
> Lisp is embeddable or not. Almost all the Lisps can embed C software -
> while
> C software can not embed all Lisps. Just compile with -fPIC and link
> as a shared library instead of a executable.
>
> When you've done this you have what you need:
>
> * Lisp can call C
> * C can call Lisp
Not all users, particularly those who write games, use Unix/Linux.
Under Windows SBCL is not production quality (yet).
Under Windows RDNZL (Edi Weitz) allows you access to .NET which allows you
access to DirectX for instance.
Compiling via C could give you some unique capabilities when used with
Microsoft Visual C.
In particular you could let the C compiler compile to CLI rather than
machine code.
I have never tested this. It is just a idea I am playing with.
I just tested a ECL Lisp shell here yesterday. It has a editor and an
shell. It uses .NET to provide a interface via RDNZL. It provides a Visual
Studio like editor with paren match, syntax highlighting, command
completion, display of command arguments and context sensitive help (MS
intellisense style). Fairly basic but it seems functional. Perhaps
something for Windows users that hate EMACS? (Yes, I know of ABLE and the
Eclipse plugin CUSP.)
Anyhow ECL supports ASDF, UFFI and CFFI too.
--------------
John Thingstad
newLISP (newlisp.org) can be embedded in a c/c++ app. It's not common
lisp, but it works very well for that sort of thing; it's fast, small,
and extremely easy to work with.