[med-svn] [cnrun] 23/25: WIP
andrei zavada
hmmr-guest at moszumanska.debian.org
Thu Nov 6 22:08:32 UTC 2014
This is an automated email from the git hooks/post-receive script.
hmmr-guest pushed a commit to branch WIP
in repository cnrun.
commit 9cb1548053ab70876ee688321e1f98f79d450a68
Author: Andrei Zavada <johnhommer at gmail.com>
Date: Sun Oct 19 20:28:13 2014 +0300
WIP
---
debian/control | 2 +-
upstream/configure.ac | 18 ++-
upstream/doc/Makefile.am | 3 +-
upstream/doc/examples/example1.lua | 2 +
upstream/src/Makefile.am | 2 +-
upstream/src/cnrun/Makefile.am | 32 ------
upstream/src/cnrun/cnrun.hh | 162 ---------------------------
upstream/src/cnrun/commands.cc | 1 +
upstream/src/cnrun/interpreter.cc | 85 ++++++++-------
upstream/src/{cnrun => libcnlua}/.gitignore | 0
upstream/src/libcnlua/Makefile.am | 36 ++++++
upstream/src/libcnlua/cnhost.hh | 154 ++++++++++++++++++++++++++
upstream/src/libcnlua/lua-iface.cc | 163 ++++++++++++++++++++++++++++
13 files changed, 415 insertions(+), 245 deletions(-)
diff --git a/debian/control b/debian/control
index 4a4da22..758c177 100644
--- a/debian/control
+++ b/debian/control
@@ -5,7 +5,7 @@ Maintainer: Andrei Zavada <johnhommer at gmail.com>
Build-Depends: debhelper (>= 9), dh-autoreconf, autoconf-archive, g++,
libgomp1, libreadline6-dev, pkg-config, libgsl0-dev, libxml2-dev,
liblua5.2-dev
-Standards-Version: 3.9.5
+Standards-Version: 3.9.6
Homepage: http://johnhommer.com/academic/code/cnrun
Vcs-Git: git://git.debian.org/git/debian-med/cnrun.git
Vcs-Browser: http://anonscm.debian.org/gitweb/?p=debian-med/cnrun.git;a=summary
diff --git a/upstream/configure.ac b/upstream/configure.ac
index e6c2515..bbc859d 100644
--- a/upstream/configure.ac
+++ b/upstream/configure.ac
@@ -1,6 +1,6 @@
AC_COPYRIGHT([Copyright (c) 2008-14 Andrei Zavada <johnhommer at gmail.com>])
-AC_INIT([cnrun], [1.2_rc], [johnhommer at gmail.com])
+AC_INIT([cnrun], [2.0.0], [johnhommer at gmail.com])
AC_CONFIG_SRCDIR([src/cnrun/main.cc])
AC_CONFIG_MACRO_DIR([m4])
AC_PREREQ(2.61)
@@ -47,7 +47,10 @@ ac_cv_cxx_cpp11_features,
])
AC_CXX_STDCPP11_FEATURES()
test $ac_cv_cxx_cpp11_features = no && \
- AC_MSG_ERROR([g++ >= 4.7 is required to build $PACKAGE as we must use -std=c++11 features your compiler doesn't seem to support], 1)
+ AC_MSG_ERROR([
+Your C++ compiler seems to not support some c++11 features\
+that we would rather like to have. Please check config.log for details.
+], 1)
cxx_version=`$CXX --version | head -n1`
AC_OPENMP()
@@ -60,10 +63,15 @@ fi
PKG_CHECK_MODULES([LIBCN], [gsl libxml-2.0])
-AX_PROG_LUA([5.1])
+AX_PROG_LUA([5.1], [5.3],)
AX_LUA_LIBS
AX_LUA_HEADERS
-
+dnl we cannot do strcmp in cpp, so here's bash to the rescue
+if test x"$LUA_VERSION" = x"5.1"; then
+ AC_DEFINE([HAVE_LUA_51], [], ["Do we have lua 5.1?"])
+else
+ AC_DEFINE([HAVE_LUA_52], [], ["Do we have lua 5.2?"])
+fi
AC_ARG_ENABLE(
[tools],
@@ -92,7 +100,7 @@ AC_OUTPUT([
src/Makefile
src/libstilton/Makefile
src/libcn/Makefile
- src/cnrun/Makefile
+ src/libcnlua/Makefile
data/Makefile
doc/Makefile
man/cnrun.1
diff --git a/upstream/doc/Makefile.am b/upstream/doc/Makefile.am
index 312471a..a8393f7 100644
--- a/upstream/doc/Makefile.am
+++ b/upstream/doc/Makefile.am
@@ -1,9 +1,8 @@
-ACLOCAL_AMFLAGS = -I m4
-
doc_DATA = \
README
examples_DATA = \
+ examples/example1.lua \
examples/ratiocoding/ORNa.x1000.in \
examples/ratiocoding/ORNb.x1000.in \
examples/ratiocoding/PN.0.sxf.target \
diff --git a/upstream/doc/examples/example1.lua b/upstream/doc/examples/example1.lua
new file mode 100644
index 0000000..8b28bce
--- /dev/null
+++ b/upstream/doc/examples/example1.lua
@@ -0,0 +1,2 @@
+libcn = require("libcn")
+
diff --git a/upstream/src/Makefile.am b/upstream/src/Makefile.am
index 884db3b..092a045 100644
--- a/upstream/src/Makefile.am
+++ b/upstream/src/Makefile.am
@@ -1,6 +1,6 @@
include $(top_srcdir)/src/Common.mk
-SUBDIRS = libstilton libcn cnrun
+SUBDIRS = libstilton libcn libcnlua
if DO_TOOLS
SUBDIRS += tools
endif
diff --git a/upstream/src/cnrun/Makefile.am b/upstream/src/cnrun/Makefile.am
deleted file mode 100644
index e08045f..0000000
--- a/upstream/src/cnrun/Makefile.am
+++ /dev/null
@@ -1,32 +0,0 @@
-include $(top_srcdir)/src/Common.mk
-AM_CXXFLAGS += $(LUA_INCLUDE)
-
-if DO_PCH
-BUILT_SOURCES := \
- cnrun.hh.gch
-
-CLEANFILES := $(BUILT_SOURCES)
-endif
-
-bin_PROGRAMS := \
- cnrun
-cnrun_SOURCES := \
- interpreter.cc commands.cc cnrun.hh main.cc print_version.cc
-cnrun_LDADD := \
- ../libcn/libcn.la \
- ../libstilton/libstilton.la \
- $(LIBCN_LIBS) \
- $(LUA_LIB)
-
-print_version.o: CXXFLAGS = $(AM_CXXFLAGS) -DGIT_DESCRIBE_TAGS=\"$(shell ../../make_version)\"
-print_version.o: FORCE
-FORCE:
-
-cnrun_LDFLAGS := \
- -shared
-
-install-exec-hook:
- rm -f $(DESTDIR)/$(pkglibdir)/*.la
-uninstall-hook:
- rm -f $(DESTDIR)/$(pkglibdir)/*.so
-
diff --git a/upstream/src/cnrun/cnrun.hh b/upstream/src/cnrun/cnrun.hh
deleted file mode 100644
index 2bf3aaf..0000000
--- a/upstream/src/cnrun/cnrun.hh
+++ /dev/null
@@ -1,162 +0,0 @@
-/*
- * File name: cnrun/cnrun.hh
- * Project: cnrun
- * Author: Andrei Zavada <johnhommer at gmail.com>
- * Initial version: 2008-11-04
- *
- * Purpose: interpreter
- *
- * License: GPL
- */
-
-#ifndef CNRUN_CNRUN_CNRUN_H_
-#define CNRUN_CNRUN_CNRUN_H_
-
-#if HAVE_CONFIG_H && !defined(VERSION)
-# include "config.h"
-#endif
-
-#include <list>
-#include <string>
-extern "C" {
-#include <lua.h>
-}
-
-#include "libstilton/misc.hh"
-#include "libcn/model.hh"
-
-namespace cnrun {
-
-struct SInterpOptions
- : public cnrun::SModelOptions {
- bool list_units:1;
- string working_dir;
-
- list<string>
- scripts;
-
- SInterpOptions ()
- : list_units (false),
- working_dir (".")
- {}
- SInterpOptions (const SInterpOptions& rv)
- : cnrun::SModelOptions (rv),
- list_units (rv.list_units),
- working_dir (rv.working_dir),
- scripts (rv.scripts)
- {}
-};
-
-
-class CInterpreterShell
- : public cnrun::stilton::C_verprintf {
-
- DELETE_DEFAULT_METHODS (CInterpreterShell)
-
- public:
- CInterpreterShell (const SInterpOptions&);
- ~CInterpreterShell ();
-
- SInterpOptions
- options;
-
- enum class TScriptExecResult {
- ok, file_error, compile_error, stack_error, call_error
- };
- TScriptExecResult exec_script( const string& script_fname);
- int run();
- // individual commands
- struct SArg {
- char type;
- double vg; int vd; string vs;
- explicit SArg (const double& v) : type ('g'), vg (v) {}
- explicit SArg (const int& v) : type ('d'), vd (v) {}
- explicit SArg (const string& v) : type ('s'), vs (v) {}
- };
- enum TCmdResult {
- ok = 0,
- no_function, bad_arity,
- bad_id, bad_value, bad_param, logic_error,
- no_file, system_error, exit,
- };
- struct SCmdResult {
- TCmdResult result;
- string error_message;
- vector<SArg> values;
- SCmdResult ()
- : result (TCmdResult::ok)
- {}
- SCmdResult (SCmdResult&& rv)
- : result (rv.result), error_message (move(rv.error_message)),
- values (move(rv.values))
- {}
- };
- using TArgs = vector<SArg>;
- SCmdResult cmd_new_model( const TArgs&);
- SCmdResult cmd_delete_model( const TArgs&);
- SCmdResult cmd_import_nml( const TArgs&);
- SCmdResult cmd_export_nml( const TArgs&);
- SCmdResult cmd_reset_model( const TArgs&);
- SCmdResult cmd_cull_deaf_synapses( const TArgs&);
- SCmdResult cmd_describe_model( const TArgs&);
- SCmdResult cmd_get_model_parameter( const TArgs&);
- SCmdResult cmd_set_model_parameter( const TArgs&);
- SCmdResult cmd_advance( const TArgs&);
- SCmdResult cmd_advance_until( const TArgs&);
-
- SCmdResult cmd_new_neuron( const TArgs&);
- SCmdResult cmd_new_synapse( const TArgs&);
- SCmdResult cmd_get_unit_properties( const TArgs&);
- SCmdResult cmd_get_unit_parameter( const TArgs&);
- SCmdResult cmd_set_unit_parameter( const TArgs&);
- SCmdResult cmd_get_unit_vars( const TArgs&);
- SCmdResult cmd_reset_unit( const TArgs&);
-
- SCmdResult cmd_get_units_matching( const TArgs&);
- SCmdResult cmd_get_units_of_type( const TArgs&);
- SCmdResult cmd_set_matching_neuron_parameter( const TArgs&);
- SCmdResult cmd_set_matching_synapse_parameter( const TArgs&);
- SCmdResult cmd_revert_matching_unit_parameters( const TArgs&);
- SCmdResult cmd_decimate( const TArgs&);
- SCmdResult cmd_putout( const TArgs&);
-
- SCmdResult cmd_new_tape_source( const TArgs&);
- SCmdResult cmd_new_periodic_source( const TArgs&);
- SCmdResult cmd_new_noise_source( const TArgs&);
- SCmdResult cmd_get_sources( const TArgs&);
- SCmdResult cmd_connect_source( const TArgs&);
- SCmdResult cmd_disconnect_source( const TArgs&);
-
- SCmdResult cmd_start_listen( const TArgs&);
- SCmdResult cmd_stop_listen( const TArgs&);
- SCmdResult cmd_start_log_spikes( const TArgs&);
- SCmdResult cmd_stop_log_spikes( const TArgs&);
-
- // vp
- int verbose_threshold() const
- { return options.verbosely; }
- private:
- map<string, CModel*>
- models;
-
- lua_State
- *lua_state;
-
- // enum class TIssueType { warning, syntax_error, system_error };
- // static const char* _issue_type_s(TIssueType);
- // void _report_script_issue( TIssueType, const char* fmt, ...) const
- // __attribute__ ((format (printf, 3, 4)));
- public:
- static list<string> list_commands();
-};
-
-}
-
-#endif
-
-// Local Variables:
-// Mode: c++
-// indent-tabs-mode: nil
-// tab-width: 8
-// c-basic-offset: 8
-// End:
diff --git a/upstream/src/cnrun/commands.cc b/upstream/src/cnrun/commands.cc
index 2da9c5a..ae6bb85 100644
--- a/upstream/src/cnrun/commands.cc
+++ b/upstream/src/cnrun/commands.cc
@@ -90,6 +90,7 @@ cmd_delete_model( const TArgs& aa)
CMD_PROLOG (1, "delete_model");
delete &M;
+
models.erase( model);
return move(R);
diff --git a/upstream/src/cnrun/interpreter.cc b/upstream/src/cnrun/interpreter.cc
index 9d8a5cf..0e623b2 100644
--- a/upstream/src/cnrun/interpreter.cc
+++ b/upstream/src/cnrun/interpreter.cc
@@ -133,54 +133,55 @@ host_fun( lua_State* L) // -> nargsout
const char* opcode = lua_tostring( L, 2);
- for ( auto& C : Commands )
- if ( strcmp( opcode, C.id) == 0 ) {
- if ( nargsin != strlen(C.arg_sig) ) {
- reperr( sasprintf( "Bad arity in call to %s (expecting %zu arg(s), got %zu",
- opcode, strlen(C.arg_sig), nargsin).c_str());
- return 2;
+ for ( auto& C : Commands ) {
+ if ( strcmp( opcode, C.id) != 0 )
+ continue;
+ if ( nargsin != strlen(C.arg_sig) ) {
+ reperr( sasprintf( "Bad arity in call to %s (expecting %zu arg(s), got %zu",
+ opcode, strlen(C.arg_sig), nargsin).c_str());
+ return 2;
+ }
+
+ // we don't accept arrays from lua yet
+ CInterpreterShell::TArgs args;
+ size_t argth = 0;
+ while ( ++argth <= nargsin ) {
+ CInterpreterShell::SArg A (0);
+ A.type = C.arg_sig[argth-1];
+ switch ( A.type ) {
+ case 's': A.vs = lua_tostring( L, 2 + argth); break;
+ case 'd': A.vd = lua_tointeger( L, 2 + argth); break;
+ case 'b': A.vd = lua_tointeger( L, 2 + argth); break;
+ case 'g': A.vg = lua_tonumber( L, 2 + argth); break;
+ default:
+ throw "Fix type literals in SCmdDesc?";
}
+ args.push_back(A);
+ }
- // we don't accept arrays from lua yet
- CInterpreterShell::TArgs args;
- size_t argth = 0;
- while ( ++argth <= nargsin ) {
- CInterpreterShell::SArg A (0);
- A.type = C.arg_sig[argth-1];
- switch ( A.type ) {
- case 's': A.vs = lua_tostring( L, 2 + argth); break;
- case 'd': A.vd = lua_tointeger( L, 2 + argth); break;
- case 'b': A.vd = lua_tointeger( L, 2 + argth); break;
- case 'g': A.vg = lua_tonumber( L, 2 + argth); break;
+ // return: ok result code, # of values pushed, value0, value1, ...; o
+ // non-ok result code, error string
+ this_p->vp( 5, "fun %s/%zu\n", C.id, args.size());
+ auto R = (this_p ->* C.fun)( args);
+ lua_settop( L, 0);
+ lua_pushboolean( L, true);
+ lua_pushinteger( L, R.result);
+ if ( R.result == CInterpreterShell::TCmdResult::ok ) {
+ lua_pushinteger( L, R.values.size());
+ for ( auto& V : R.values )
+ switch (V.type) {
+ case 's': lua_pushstring( L, V.vs.c_str()); break;
+ case 'd': lua_pushinteger( L, V.vd); break;
+ case 'g': lua_pushnumber( L, V.vg); break;
default:
throw "Fix type literals in SCmdDesc?";
}
- args.push_back(A);
- }
-
- // return: ok result code, # of values pushed, value0, value1, ...; o
- // non-ok result code, error string
- this_p->vp( 5, "fun %s/%zu\n", C.id, args.size());
- auto R = (this_p ->* C.fun)( args);
- lua_settop( L, 0);
- lua_pushboolean( L, true);
- lua_pushinteger( L, R.result);
- if ( R.result == CInterpreterShell::TCmdResult::ok ) {
- lua_pushinteger( L, R.values.size());
- for ( auto& V : R.values )
- switch (V.type) {
- case 's': lua_pushstring( L, V.vs.c_str()); break;
- case 'd': lua_pushinteger( L, V.vd); break;
- case 'g': lua_pushnumber( L, V.vg); break;
- default:
- throw "Fix type literals in SCmdDesc?";
- }
- return 1 + 1 + 1 + R.values.size();
- } else {
- lua_pushstring( L, R.error_message.c_str());
- return 1 + 1 + 1;
- }
+ return 1 + 1 + 1 + R.values.size();
+ } else {
+ lua_pushstring( L, R.error_message.c_str());
+ return 1 + 1 + 1;
}
+ }
reperr( sasprintf( "Unrecognized function \"%s\"/%zu",
opcode, nargsin - 2).c_str());
return 2;
diff --git a/upstream/src/cnrun/.gitignore b/upstream/src/libcnlua/.gitignore
similarity index 100%
rename from upstream/src/cnrun/.gitignore
rename to upstream/src/libcnlua/.gitignore
diff --git a/upstream/src/libcnlua/Makefile.am b/upstream/src/libcnlua/Makefile.am
new file mode 100644
index 0000000..2996281
--- /dev/null
+++ b/upstream/src/libcnlua/Makefile.am
@@ -0,0 +1,36 @@
+include $(top_srcdir)/src/Common.mk
+AM_CXXFLAGS += $(LUA_INCLUDE)
+
+if DO_PCH
+BUILT_SOURCES := \
+ cnhost.hh.gch
+
+CLEANFILES := $(BUILT_SOURCES)
+endif
+
+
+lib_LTLIBRARIES = \
+ libcnlua.la
+libcnlua_la_SOURCES = \
+ lua-iface.cc cnhost.hh
+libcnlua_la_LIBADD := \
+ ../libcn/libcn.la \
+ ../libstilton/libstilton.la \
+ $(LIBCN_LIBS) \
+ $(LUA_LIB)
+libcnlua_la_LDFLAGS := \
+ -shared -version-info $(subst .,:,$(PACKAGE_VERSION))
+
+
+#print_version.o: CXXFLAGS = $(AM_CXXFLAGS) -DGIT_DESCRIBE_TAGS=\"$(shell ../../make_version)\"
+#print_version.o: FORCE
+#FORCE:
+
+lua_libdir := $(DESTDIR)/$(libdir)/lua/$(LUA_VERSION)
+install-exec-hook:
+ rm -f "$(DESTDIR)/$(pkglibdir)/*.la"
+ $(MKDIR_P) "$(lua_libdir)"
+ $(LN_S) -f "$(DESTDIR)/$(libdir)/libcnlua.so.$(PACKAGE_VERSION)" \
+ "$(lua_libdir)/libcn.so"
+uninstall-hook:
+ rm "$(lua_libdir)/libcn.so"
diff --git a/upstream/src/libcnlua/cnhost.hh b/upstream/src/libcnlua/cnhost.hh
new file mode 100644
index 0000000..7c21eff
--- /dev/null
+++ b/upstream/src/libcnlua/cnhost.hh
@@ -0,0 +1,154 @@
+/*
+ * File name: cnrun/cnhost.hh
+ * Project: cnrun
+ * Author: Andrei Zavada <johnhommer at gmail.com>
+ * Initial version: 2008-11-04
+ *
+ * Purpose: C host side for cn lua library
+ *
+ * License: GPL
+ */
+
+#ifndef CNRUN_CNRUN_CNHOST_H_
+#define CNRUN_CNRUN_CNHOST_H_
+
+#if HAVE_CONFIG_H && !defined(VERSION)
+# include "config.h"
+#endif
+
+#include <list>
+#include <string>
+extern "C" {
+#include <lua.h>
+}
+
+#include "libcn/model.hh"
+
+namespace cnrun {
+
+struct SHostOptions
+ : public cnrun::SModelOptions {
+ string working_dir;
+
+ SHostOptions ()
+ : working_dir (".")
+ {}
+ SHostOptions (const SHostOptions& rv)
+ : cnrun::SModelOptions (rv),
+ working_dir (rv.working_dir)
+ {}
+};
+
+
+class CHost
+ : public cnrun::stilton::C_verprintf {
+
+ DELETE_DEFAULT_METHODS (CHost)
+
+ public:
+ CHost (const SHostOptions& rv)
+ : options (rv)
+ {}
+ ~CHost ()
+ {
+ for ( auto& m : models )
+ delete m.second;
+ }
+
+ SHostOptions
+ options;
+
+ bool have_model( const string& name) const
+ {
+ return models.find(name) != models.end();
+ }
+ list<const char*> list_models() const
+ {
+ list<const char*> L;
+ for ( auto& x : models )
+ L.push_back(x.first.c_str());
+ return move(L);
+ }
+ CModel* get_model( const string& name)
+ {
+ return models.at(name);
+ }
+ bool new_model( CModel& M)
+ {
+ if ( models.find(M.name) == models.end() ) {
+ models[M.name] = &M;
+ return 0;
+ } else
+ return -1;
+ }
+ void del_model( const string& name)
+ {
+ if ( models.find(name) != models.end() )
+ delete models[name];
+ models.erase( name);
+ }
+ // SCmdResult cmd_new_model( const TArgs&);
+ // SCmdResult cmd_delete_model( const TArgs&);
+ // SCmdResult cmd_import_nml( const TArgs&);
+ // SCmdResult cmd_export_nml( const TArgs&);
+ // SCmdResult cmd_reset_model( const TArgs&);
+ // SCmdResult cmd_cull_deaf_synapses( const TArgs&);
+ // SCmdResult cmd_describe_model( const TArgs&);
+ // SCmdResult cmd_get_model_parameter( const TArgs&);
+ // SCmdResult cmd_set_model_parameter( const TArgs&);
+ // SCmdResult cmd_advance( const TArgs&);
+ // SCmdResult cmd_advance_until( const TArgs&);
+
+ // SCmdResult cmd_new_neuron( const TArgs&);
+ // SCmdResult cmd_new_synapse( const TArgs&);
+ // SCmdResult cmd_get_unit_properties( const TArgs&);
+ // SCmdResult cmd_get_unit_parameter( const TArgs&);
+ // SCmdResult cmd_set_unit_parameter( const TArgs&);
+ // SCmdResult cmd_get_unit_vars( const TArgs&);
+ // SCmdResult cmd_reset_unit( const TArgs&);
+
+ // SCmdResult cmd_get_units_matching( const TArgs&);
+ // SCmdResult cmd_get_units_of_type( const TArgs&);
+ // SCmdResult cmd_set_matching_neuron_parameter( const TArgs&);
+ // SCmdResult cmd_set_matching_synapse_parameter( const TArgs&);
+ // SCmdResult cmd_revert_matching_unit_parameters( const TArgs&);
+ // SCmdResult cmd_decimate( const TArgs&);
+ // SCmdResult cmd_putout( const TArgs&);
+
+ // SCmdResult cmd_new_tape_source( const TArgs&);
+ // SCmdResult cmd_new_periodic_source( const TArgs&);
+ // SCmdResult cmd_new_noise_source( const TArgs&);
+ // SCmdResult cmd_get_sources( const TArgs&);
+ // SCmdResult cmd_connect_source( const TArgs&);
+ // SCmdResult cmd_disconnect_source( const TArgs&);
+
+ // SCmdResult cmd_start_listen( const TArgs&);
+ // SCmdResult cmd_stop_listen( const TArgs&);
+ // SCmdResult cmd_start_log_spikes( const TArgs&);
+ // SCmdResult cmd_stop_log_spikes( const TArgs&);
+
+ // vp
+ int verbose_threshold() const
+ { return options.verbosely; }
+ private:
+ map<string, CModel*>
+ models;
+
+ // enum class TIssueType { warning, syntax_error, system_error };
+ // static const char* _issue_type_s(TIssueType);
+ // void _report_script_issue( TIssueType, const char* fmt, ...) const
+ // __attribute__ ((format (printf, 3, 4)));
+ public:
+ static list<string> list_commands();
+};
+
+}
+
+#endif
+
+// Local Variables:
+// Mode: c++
+// indent-tabs-mode: nil
+// tab-width: 8
+// c-basic-offset: 8
+// End:
diff --git a/upstream/src/libcnlua/lua-iface.cc b/upstream/src/libcnlua/lua-iface.cc
new file mode 100644
index 0000000..8bba8b3
--- /dev/null
+++ b/upstream/src/libcnlua/lua-iface.cc
@@ -0,0 +1,163 @@
+/*
+ * File name: cnrun/lua-iface.cc
+ * Project: cnrun
+ * Author: Andrei Zavada <johnhommer at gmail.com>
+ * building on original work by Thomas Nowotny <tnowotny at ucsd.edu>
+ * Initial version: 2014-10-09
+ *
+ * Purpose: libcn and some state, exported for use in your lua code.
+ *
+ * License: GPL
+ */
+
+#if HAVE_CONFIG_H && !defined(VERSION)
+# include "config.h"
+#endif
+
+#include <list>
+
+extern "C" {
+#include <lua.h>
+#include <lualib.h>
+#include <lauxlib.h>
+}
+
+#include "libstilton/string.hh"
+#include "cnhost.hh"
+
+using namespace std;
+using namespace cnrun;
+
+namespace {
+
+int check_signature( lua_State* L, const char* fun, const char* sig)
+{
+ using cnrun::stilton::str::sasprintf;
+
+ size_t siglen = strlen(sig),
+ nargsin = lua_gettop( L);
+ if ( nargsin != siglen ) {
+ lua_pushnil(L);
+ lua_pushstring(L, sasprintf("%s: Expected %zu arg(s), got %zu", fun, siglen, nargsin).c_str());
+ return -1;
+ }
+
+ for ( size_t i = 0; i < siglen; ++i )
+ switch ( sig[i] ) {
+ case 's':
+ if ( !lua_isstring( L, i) ) {
+ lua_pushnil(L);
+ lua_pushstring( L, sasprintf( "%s(\"%s\"): Expected a string arg at position %zu", fun, sig, i).c_str());
+ return i + 1;
+ }
+ case 'g':
+ case 'd':
+ if ( !lua_isnumber( L, i) ) {
+ lua_pushnil(L);
+ lua_pushstring( L, sasprintf( "%s(\"%s\"): Expected a numeric arg at position %zu", fun, sig, i).c_str());
+ return i + 1;
+ }
+ case 'p':
+ if ( !lua_islightuserdata( L, i) ) {
+ lua_pushnil(L);
+ lua_pushstring( L, sasprintf( "%s(\"%s\"): Expected a light user data arg at position %zu", fun, sig, i).c_str());
+ return i + 1;
+ }
+ }
+ return 0;
+}
+
+const int TWO_ARGS_FOR_ERROR = 2;
+
+int cn_get_context( lua_State *L)
+{
+ if ( check_signature( L, "cn_get_context", "") )
+ return TWO_ARGS_FOR_ERROR;
+
+ auto ctx = new CHost (SHostOptions ());
+ lua_pushinteger( L, 1);
+ lua_pushlightuserdata( L, ctx);
+ return 2;
+}
+
+int cn_new_model( lua_State *L)
+{
+ if ( check_signature( L, "cn_new_model", "ps") )
+ return TWO_ARGS_FOR_ERROR;
+
+ auto& C = *(CHost*)lua_topointer( L, 1);
+ const char* model_name = lua_tostring( L, 2);
+
+ if ( C.have_model( model_name) )
+ return lua_pushnil(L),
+ lua_pushstring(L, sasprintf( "cn_new_model(): Model named %s already exists", model_name).c_str()),
+ TWO_ARGS_FOR_ERROR;
+
+ auto M = new CModel(
+ model_name,
+ new CIntegrateRK65(
+ C.options.integration_dt_min,
+ C.options.integration_dt_max,
+ C.options.integration_dt_cap),
+ C.options);
+ if ( !M )
+ return lua_pushnil(L),
+ lua_pushstring(L, sasprintf( "cn_new_model(): Failed to create a model (%s)", model_name).c_str()),
+ TWO_ARGS_FOR_ERROR;
+
+ C.new_model(*M);
+
+ return lua_pushinteger( L, 1),
+ lua_pushlightuserdata( L, M),
+ 2;
+}
+
+
+int cn_list_models( lua_State *L)
+{
+ if ( check_signature( L, "cn_list_models", "p") )
+ return TWO_ARGS_FOR_ERROR;
+
+ auto& C = *(CHost*)lua_topointer( L, 1);
+
+ lua_pushinteger( L, 1);
+ auto MM = C.list_models();
+ for ( auto& M : MM )
+ lua_pushstring( L, M);
+ return MM.size() + 1;
+}
+
+
+const struct luaL_Reg cnlib [] = {
+ {"cn_get_context", cn_get_context},
+ {"cn_new_model", cn_new_model},
+ {"cn_list_models", cn_list_models},
+ {NULL, NULL}
+};
+
+}
+
+
+extern "C" {
+
+int luaopen_libcn( lua_State *L)
+{
+#ifdef HAVE_LUA_51
+ printf( "register cnlib\n");
+ luaL_register(L, "cnlib", cnlib_funtable, 0);
+#else // this must be 5.2
+ printf( "newlib cnlib\n");
+ luaL_newlib(L, cnlib);
+#endif
+ return 1;
+}
+
+}
+
+
+// Local Variables:
+// Mode: c++
+// indent-tabs-mode: nil
+// tab-width: 8
+// c-basic-offset: 8
+// End:
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-med/cnrun.git
More information about the debian-med-commit
mailing list