Code Monkey home page Code Monkey logo

fortran-lua53's Introduction

fortran-lua53

A collection of ISO C binding interfaces to Lua 5.3 for Fortran 2008, to call Lua from Fortran and vice versa.

Similar projects:

  • AOTUS: Library that provides a Fortran wrapper to use Lua scripts as configuration files (MIT).
  • FortLua: Example that shows how to load Lua configuration files from Fortran, based on AOTUS (MIT).
  • fortran-lua54: Fortran 2008 bindings to Lua 5.4 (ISC).
  • f2k3-lua: Lua bindings for loading configuration files only (MIT).
  • luaf: Selected bindings to Lua 5.1 (MIT).

Build

Install Lua 5.3 with development headers. On FreeBSD, run:

# pkg install lang/lua53

On Debian Linux, install:

# apt-get install liblua5.3 liblua5.3-dev

Use xmake to build fortran-lua53:

$ xmake

This outputs libfortran-lua53.a and lua.mod to build/. Without xmake, just compile the library using the provided Makefile:

$ make

Or, run the Fortran Package Manager:

$ fpm build --profile=release

Link your Fortran applications against libfortran-lua53.a, and liblua-5.3.a or -llua53. On Linux, you have to link against liblua5.3.a or -llua5.3 respectively instead. The include and library search paths may differ as well.

Example

The following basic example shows how to call the Lua function hello() in script.lua from Fortran.

-- script.lua
function hello()
    print('Hello from Lua!')
end

Make sure that script.lua is stored in the same directory as the Fortran application.

! example.f90
program main
    use, intrinsic :: iso_c_binding, only: c_ptr
    use :: lua
    implicit none
    type(c_ptr) :: l
    integer     :: rc

    l = lual_newstate()
    call lual_openlibs(l)

    rc = lual_dofile(l, 'script.lua')
    rc = lua_getglobal(l, 'hello')
    rc = lua_pcall(l, 0, 0, 0)

    call lua_close(l)
end program main

Compile, (dynamically) link, and run the example with:

$ gfortran -I/usr/local/include/lua53/ -L/usr/local/lib/lua/5.3/ \
  -o example example.f90 libfortran-lua53.a -llua-5.3
$ ./example
Hello from Lua!

On Linux, change the prefix /usr/local to /usr. To link Lua 5.3 statically, run instead:

$ gfortran -o example example.f90 libfortran-lua53.a /usr/local/lib/liblua-5.3.a

Further Examples

Additional examples can be found in examples/.

  • fibonacci: calls a recursive Lua routine loaded from file.
  • library: calls a Fortran routine inside a shared library from Lua.
  • string: runs Lua code stored in a Fortran string.
  • table: reads values from a Lua table.

fpm

You can add fortran-lua53 as an fpm dependency:

[dependencies]
fortran-lua53 = { git = "https://github.com/interkosmos/fortran-lua53.git" }

Compatibility

The integer and float types used by Lua internally depend on the targeted platform. The program test/types.c outputs the types:

$ make test
$ ./types
lua_integer.: c_long_long
lua_number..: c_double
lua_kcontext: c_intptr_t

You may have to alter lua_integer, lua_number, and lua_kcontext in src/lua.f90 accordingly.

Coverage

Function Name Fortran Interface Name Bound Wrapper
luaL_addchar
luaL_addlstring
luaL_addsize
luaL_addstring
luaL_addvalue
luaL_argcheck
luaL_argerror
luaL_buffinit
luaL_buffinitsize
luaL_callmeta
luaL_checkany
luaL_checkinteger
luaL_checklstring
luaL_checknumber
luaL_checkoption
luaL_checkstack
luaL_checkstring
luaL_checktype
luaL_checkudata
luaL_checkversion
luaL_dofile lual_dofile
luaL_dostring
luaL_error
luaL_execresult
luaL_fileresult
luaL_getmetafield
luaL_getmetatable
luaL_getsubtable
luaL_gsub
luaL_len lual_len
luaL_loadbufferx
luaL_loadfile lual_loadfile
luaL_loadfilex lual_loadfilex
luaL_loadstring lual_loadstring
luaL_newlib
luaL_newlibtable
luaL_newmetatable
luaL_newstate lual_newstate
luaL_openlibs lual_openlibs
luaL_opt
luaL_optinteger
luaL_optlstring
luaL_optnumber
luaL_optstring
luaL_prepbuffer
luaL_prepbuffsize
luaL_pushresult
luaL_pushresultsize
luaL_ref
luaL_register
luaL_requiref
luaL_setfuncs
luaL_setmetatable
luaL_testudata
luaL_tolstring
luaL_traceback
luaL_typename
luaL_unref
luaL_where
lua_absindex
lua_arith lua_arith
lua_atpanic
lua_call lua_call
lua_callk lua_callk
lua_checkstack lua_checkstack
lua_close lua_close
lua_compare lua_compare
lua_concat lua_concat
lua_copy lua_copy
lua_createtable lua_createtable
lua_dump
lua_error
lua_gc lua_gc
lua_getallocf
lua_getextraspace
lua_getfield lua_getfield
lua_getglobal lua_getglobal
lua_geti
lua_getmetatable
lua_gettable
lua_gettop lua_gettop
lua_getuservalue
lua_insert
lua_isboolean lua_isboolean
lua_iscfunction lua_iscfunction
lua_isfunction lua_isfunction
lua_isinteger lua_isinteger
lua_islightuserdata
lua_isnil lua_isnil
lua_isnone lua_isnone
lua_isnoneornil lua_isnoneornil
lua_isnumber lua_isnumber
lua_isstring lua_isstring
lua_istable lua_istable
lua_isthread lua_isthread
lua_isuserdata lua_isuserdata
lua_isyieldable lua_isyieldable
lua_len
lua_load lua_load
lua_newstate
lua_newtable lua_newtable
lua_newthread
lua_newuserdata
lua_next
lua_numbertointeger
lua_pcall lua_pcall
lua_pcallk lua_pcallk
lua_pop lua_pop
lua_pushboolean lua_pushboolean
lua_pushcclosure lua_pushcclosure
lua_pushcfunction
lua_pushfstring
lua_pushglobaltable
lua_pushinteger lua_pushinteger
lua_pushlightuserdata lua_pushlightuserdata
lua_pushliteral
lua_pushlstring lua_pushlstring
lua_pushnil lua_pushnil
lua_pushnumber lua_pushnumber
lua_pushstring lua_pushstring
lua_pushthread lua_pushthread
lua_pushvalue lua_pushvalue
lua_pushvfstring
lua_rawequal
lua_rawget lua_rawget
lua_rawgeti lua_rawgeti
lua_rawgetp
lua_rawlen lua_rawlen
lua_rawset lua_rawset
lua_rawseti lua_rawseti
lua_rawsetp
lua_register lua_register
lua_remove
lua_replace
lua_resume
lua_rotate
lua_setallocf
lua_setfield lua_setfield
lua_setglobal lua_setglobal
lua_seti lua_seti
lua_setmetatable
lua_settable lua_settable
lua_settop lua_settop
lua_setuservalue
lua_status lua_status
lua_stringtonumber
lua_toboolean lua_toboolean
lua_tocfunction
lua_tointeger lua_tointeger
lua_tointegerx lua_tointegerx
lua_tolstring
lua_tonumber lua_tonumber
lua_tonumberx lua_tonumberx
lua_topointer
lua_tostring lua_tostring
lua_tothread
lua_touserdata
lua_type lua_type
lua_typename lua_typename
lua_upvalueindex
lua_version
lua_xmove
lua_yield
lua_yieldk

Licence

ISC

fortran-lua53's People

Contributors

interkosmos avatar zoziha avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

fortran-lua53's Issues

Error: Type mismatch in argument 'ctx' at (1); passed INTEGER(8) to INTEGER(4)

 <INFO> LINKER OPTIONS:   -L/opt/local/lib -llua.5.3
 <INFO> INCLUDE DIRECTORIES:  []
 + mkdir -p build/gfortran_EDF3F128DBFCA123
[  0%]                        lua.f90
 + mkdir -p build/gfortran_EDF3F128DBFCA123/fortran-lua53/
 + gfortran -c ././src/lua.f90  -O3 -funroll-loops -Wimplicit-interface -fPIC -fmax-errors=1 -fcoarray=single -J build/gfortran_EDF3F128DBFCA123 -Ibuild/gfortran_EDF3F128DBFCA123 -o build/gfortran_EDF3F128DBFCA123/fortran-lua53/src_lua.f90.o
././src/lua.f90:921:74:

  921 |         call lua_callk(l, nargs, nresults, int(0, kind=i8), c_null_funptr)
      |                                                                          1
Error: Type mismatch in argument 'ctx' at (1); passed INTEGER(8) to INTEGER(4)
compilation terminated due to -fmax-errors=1.
[ 50%]                        lua.f90  done.

././src/lua.f90:921:74:

  921 |         call lua_callk(l, nargs, nresults, int(0, kind=i8), c_null_funptr)
      |                                                                          1
Error: Type mismatch in argument 'ctx' at (1); passed INTEGER(8) to INTEGER(4)
compilation terminated due to -fmax-errors=1.
<ERROR> Compilation failed for object " src_lua.f90.o "
<ERROR>stopping due to failed compilation
STOP 1

fpm build: <ERROR>Project does not contain any installable targets

Here also the same error with FPM:

--->  Building fortran-lua53
xinstall: mkdir /opt/local/var/macports/build/_opt_PPCRosettaPorts_fortran_fortran-lua53/fortran-lua53/work/bin
Executing:  cd "/opt/local/var/macports/build/_opt_PPCRosettaPorts_fortran_fortran-lua53/fortran-lua53/work/fortran-lua53-0.1.0" && /opt/local/bin/fpm install --verbose --prefix="/opt/local/var/macports/build/_opt_PPCRosettaPorts_fortran_fortran-lua53/fortran-lua53/work/opt/local" --profile="release" --link-flag="-L/opt/local/lib -llua.5.3" 
 + mkdir -p build/dependencies
 <INFO> BUILD_NAME: build/gfortran
 <INFO> COMPILER:  gfortran
 <INFO> C COMPILER:  gcc
 <INFO> CXX COMPILER: g++
 <INFO> COMPILER OPTIONS:   -O3 -funroll-loops -Wimplicit-interface -fPIC -fmax-errors=1 -fcoarray=single
 <INFO> C COMPILER OPTIONS:  
 <INFO> CXX COMPILER OPTIONS: 
 <INFO> LINKER OPTIONS:   -L/opt/local/lib -llua.5.3
 <INFO> INCLUDE DIRECTORIES:  []
<ERROR>Project does not contain any installable targets
STOP 1
Command failed:  cd "/opt/local/var/macports/build/_opt_PPCRosettaPorts_fortran_fortran-lua53/fortran-lua53/work/fortran-lua53-0.1.0" && /opt/local/bin/fpm install --verbose --prefix="/opt/local/var/macports/build/_opt_PPCRosettaPorts_fortran_fortran-lua53/fortran-lua53/work/opt/local" --profile="release" --link-flag="-L/opt/local/lib -llua.5.3" 
Exit code: 1

add topics

I suggest adding the topic fortran-package-manager, since the fpm topic refers to many other things.
You could also add the topic interoperability.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.