From: ············@gmail.com
Subject: Howto use Lisp as scripting language within C++?
Date: 
Message-ID: <8e38bb0c-fc2c-4a25-963c-f5fbb83dcdd4@h11g2000prf.googlegroups.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

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
From: Lars Rune Nøstdal
Subject: Re: Howto use Lisp as scripting language within C++?
Date: 
Message-ID: <47de21e1$0$28885$c83e3ef6@nn1-read.tele2.net>
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/
From: Blackguester
Subject: Re: Howto use Lisp as scripting language within C++?
Date: 
Message-ID: <3722a1ff-b81d-4213-a6f4-a72b3d627696@e23g2000prf.googlegroups.com>
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.
I think Lisp is good at structure, modularized and logical etc some
"pure" ascpect of promgram, in comparison c++ can do more duty work in
related to system , gui etc.
Maybe i'm newbie, I think its funny to write intelligent part of
game.
Anyway, Thank u a lot£»£©
From: jayessay
Subject: Re: Howto use Lisp as scripting language within C++?
Date: 
Message-ID: <m3y78g6wrs.fsf@sirius.goldenthreadtech.com>
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
From: John Thingstad
Subject: Re: Howto use Lisp as scripting language within C++?
Date: 
Message-ID: <op.t75ex60dut4oq5@pandora.alfanett.no>
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
From: Blackguester
Subject: Re: Howto use Lisp as scripting language within C++?
Date: 
Message-ID: <a35cbc08-99b3-4529-8454-6258eee4b18d@i7g2000prf.googlegroups.com>
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.
From: Lars Rune Nøstdal
Subject: Re: Howto use Lisp as scripting language within C++?
Date: 
Message-ID: <47dfa2e1$0$28888$c83e3ef6@nn1-read.tele2.net>
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/
From: Lars Rune Nøstdal
Subject: Re: Howto use Lisp as scripting language within C++?
Date: 
Message-ID: <47dfa479$0$28888$c83e3ef6@nn1-read.tele2.net>
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/
From: John Thingstad
Subject: Re: Howto use Lisp as scripting language within C++?
Date: 
Message-ID: <op.t77mymswut4oq5@pandora.alfanett.no>
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
From: Jeff
Subject: Re: Howto use Lisp as scripting language within C++?
Date: 
Message-ID: <46d9df6a-1797-4130-bc81-c3df3bc608fa@d62g2000hsf.googlegroups.com>
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.