Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
print("Hello from Lua") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
print("Hello from Lua") | ||
|
||
function example2_fun () | ||
return "Hello from Lua function" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |