Skip to content

Commit

Permalink
Add 2 examples and a Makefile.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikokrock committed Jun 9, 2014
1 parent f305703 commit 7580001
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 0 deletions.
22 changes: 22 additions & 0 deletions examples/example1/example1.gpr
@@ -0,0 +1,22 @@
with "Lua";

project Example1 is

for Languages use ("ada");

for Source_Dirs use ("src");
for Object_Dir use "obj";
for Main use ("main.adb");

package Compiler is
for Default_Switches("ada") use ("-O2",
"-gnat12",
"-gnatwa",
"-gnatyg");
end Compiler;

package Linker is
for Default_Switches("ada") use ("-L../..", "-llua");
end Linker;

end Example1;
1 change: 1 addition & 0 deletions examples/example1/example1.lua
@@ -0,0 +1 @@
print("Hello from Lua")
37 changes: 37 additions & 0 deletions examples/example1/src/main.adb
@@ -0,0 +1,37 @@
-- The example show how to create a new lua state and launch a lua script

with Lua; use Lua;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Exceptions; use Ada.Exceptions;

function Main return Integer is
S : constant Lua_State := New_State;
-- Here we create a new state using New_State function

Status : Lua_Return_Code;

begin
Open_Libs (S);
-- Load the lua "standard" libraries

Ada.Text_IO.Put_Line ("Load script");
Load_File (S, "../example1.lua");
-- Load a script. Note that loading a script does not execute it. This
-- includes toplevel code.

Ada.Text_IO.Put_Line ("Execute script");
Status := Lua.PCall (S);

if Status /= LUA_OK then
-- An error occurs during the execution
Put_Line (Status'Img);
Put_Line (To_Ada (S, -1));
return 1;
end if;

return 0;
exception
when E : Lua_Error =>
Put_Line (Exception_Message (E));
return 1;
end Main;
22 changes: 22 additions & 0 deletions examples/example2/example2.gpr
@@ -0,0 +1,22 @@
with "Lua";

project Example2 is

for Languages use ("ada");

for Source_Dirs use ("src");
for Object_Dir use "obj";
for Main use ("main.adb");

package Compiler is
for Default_Switches("ada") use ("-O2",
"-gnat12",
"-gnatwa",
"-gnatyg");
end Compiler;

package Linker is
for Default_Switches("ada") use ("-L../..", "-llua");
end Linker;

end Example2;
5 changes: 5 additions & 0 deletions examples/example2/example2.lua
@@ -0,0 +1,5 @@
print("Hello from Lua")

function example2_fun ()
return "Hello from Lua function"
end
33 changes: 33 additions & 0 deletions examples/example2/src/main.adb
@@ -0,0 +1,33 @@
-- The example show how to create a new lua state and launch a lua script

with Lua; use Lua;
with Lua.Utils; use Lua.Utils;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Exceptions; use Ada.Exceptions;

function Main return Integer is
S : constant Lua_State := New_State;
-- Here we create a new state using New_State function

begin
Open_Libs (S);
-- Load the lua "standard" libraries

Put_Line ("Load script");
Load_File (S, "../example2.lua");
-- Load a script. Note that loading a script does not execute it. This
-- includes toplevel code.

Put_Line ("Execute script");
PCall (S);
-- A first execution is needed (kind of elaboration)

-- Call a function declared in the script that return a string
Put_Line (Call_Function (S, "example2_fun"));
return 0;

exception
when E : Lua_Error =>
Put_Line ("exception: " & Exception_Message (E));
return 1;
end Main;

0 comments on commit 7580001

Please sign in to comment.