[This is preliminary documentation and subject to change]

Liogo anywhere

Logo is a powerful dynamic language so it could be great to use the power of Logo language in some other applications. It's exactly what Liogo allow you to do using Liogo OLE Automation Interface.

OLE Automation is a way for a Windows application to expose its features for others applications. Many applications may call an OLE Automation interface. Most famous are Office Applications (using Visual Basic for Application) and VBScript command line. I'm going to show you in this tutorial how you can use Liogo from these applications. Note that OLE Automation is not supported on Linux.

Before continue this tutorial, you must ensure that the Liogo OLE Automation Interface is installed on your PC. The package is installed with "Complete" install or when the "Developer" option is choosed on the custom setup (see figure below). If you don't choose this option at setup, please launch again the setup wizard and update your setup.

Launch Liogo runtime

Liogo OLE Automation is exposed by the way of a COM object named "Liogo.Application". To call Liogo, you first have to create a "Liogo.Application". Here how you can create this object in VBScript:


var liogo = CreateObject("Liogo.Application")

Here the object is created then affected to a VBScript variable "liogo".

The first thing to do now is to initialize the Liogo runtime. You just need to call the Initialize method on your Liogo object.


liogo.Initialize("")

The Initialize method take a parameter used as settings string. We'll see this feature shortly. At the moment, because we don't need a specific settings, we use an empty string.

Three methods allow you to communicate with the Liogo runtime:

Here is a really simple VBScript program named "oletest.vbs" wich use each method described. Note than line numbers has been added for convenience:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
' Call Liogo using OLE

Set liogo = CreateObject("Liogo.Application")
liogo.Initialize("")

liogo.Execute("print """&"Hello| |Liogo")

liogo.SetVar "w", 6

wscript.Echo liogo.Execute("sum :w 2")

wscript.Echo liogo.GetVar("w")

liogo.Terminate()

Line 6 use Execute to call the Logo print to display a welcome message. Note that you should use the Logo quote to handle string constants.

Line 8 call SetVar to set the value of the Logo global variable "w" to the number 6. This line is equivalent to the logo command:


make "w 6

Line 10 use Execute to do an arithmetic operation on the Logo variable "w". The result of this operation is printed using the VBScript Echo function. Instead of printing, this result could be set in a VBScript variable as well.

Line 12 use SetVar to get the value of the Logo variable "w".

About data types

Each time a data is exchanged between Liogo and VBScript, a conversion is need. In the previous sample, only numeric value are used: at line 8, a VBScript number is converted to a Logo number, conversely at line 10 and 12, a Logo number is converted to a VBScript number.

The Liogo OLE Automation Interface converts automatically values between Logo and the multi-usage COM data type named Variant. The table below list equivalent between Logo and Variant.

Logo value
Type name Logo valueVariant value
Number110110
Word"Hello'Hello'
List[A B C]Array("A", "B", "C")
Array{A B C}Array("A", "B", "C")
Nested lists or nested arrays    [[1 2 3] [4 5 6]    Array(Array(1, 2, 3), Array(4, 5, 6))

Magic square

Excel sheets and specifically Excel formula are a great place to use Logo. Because Liogo has automatic conversion with arrays, it's easy to use Logo expression to fill or get a cell range.

Following is an Excel file name "magicsquare.xls" using Liogo to compute a Magic Square.

As you probably know, a magic square is an arrangement of numbers in a square such that the numbers in all rows, all columns, and both diagonals sum to the same constant. In this sample, a part of the square is fill or compute using Liogo and another part is fill or compute using Excel.

Number 1 on the previous schema show how the first line is inialized using a Logo list. Here is the VBA source code behind 1:

Range("B5:E5").Value = liogo.Execute("(list 1 12 8 13)")

Number 2 on the schema show how you could compute a sum on an Excel line. The VBA source code is:

liogo.SetVar "c", Range("B5:E5").Value
Range("F5").Value = liogo.Execute("reduce ""sum first :c")

Number 3 on the schema show how to initialize a set of lines at one time:

Range("B5:E5").Value = liogo.Execute("(list [11 2 14 7] [16 5 9 4])")

Finally, Number 4 in the schema is an operation on a set of columns:

liogo.SetVar "c", Range("B5:B8").Value
Range("B9").Value = liogo.Execute("reduce ""sum firsts :c")

Handling errors

Sometimes your Logo code could experience errors. Common errors occurs when you call an not existing procedure or when you try to get a value from a not initialized global variable. Two methods can handle these cases: GetErrorId and GetErrorText. Here a sample using these methods:

liogo.Execute("print summ 3 5")
if liogo.GetErrorId() <> -1 then
    wscript.Echo liogo.GetErrorText()
end if

v = liogo.GetVar("xyz")
if liogo.GetErrorId() <> -1 then
    wscript.Echo liogo.GetErrorText()
end if

GetErrorId is used to get the error code occured. -1 is returned when no error occured.

GetErrorText is used to get the error message. An empty string is returned when no error occured.

Custom procedures and performance

Because Liogo is a Logo compiler, each time you use Execute methods, Liogo need to call the compiler. Of course, there is an overhead to call multiple times the Logo compiler at runtime. You have two ways to avoid this overhead.

The first way is to use each time the same string as parameter of Execute. The following script computes an approximative value for PI using Logo graphics commands.

    Dim n As Integer
    n = 10
    For i = 6 To 16
        liogo.SetVar "n", n
        liogo.Execute "home cs repeat :n [fd 1 rt 180/:n] make ""r 2*:n/(first pos)"
        Range("B" & i).Value = n
        Range("C" & i).Value = liogo.GetVar("r")
        n = n * 2
    Next i

On the VBA inner loop, you could see that i've used Logo variables for the Logo loop and for result. Thought the loop number change, the same string is always used. So, the Liogo compiler is called only one time.

Another way to optimize performance is to call your custom DLLs. As an example, take again the "fact.dll" assembly generated in First step with Liogo Tutorial. To call this assembly, just call the load Logo command.

liogo.Execute "load ""fact"
wscript.Echo liogo.Execute("fact 10")

Note that, due to an issue in the current version, all customs DLLs should be copied in the %SYSTEMROOT%\SYSTEM32 directory (most commonly C:\WINDOWS\SYSTEM32).

Settings for your OLE application

If you complete Deploy and customizetutorial, you already know that you could custom your Liogo application. There is no way to use a settings file when you call Liogo from OLE. However, Liogo OLE Interface let you set your settings using the first argument of Initialize call. In the following sample, we used settings to change the current language for Logo commands and messages.

 
	var liogo = CreateObject("Liogo.Application")
	liogo.Initialize ("LIOGO.Culture.Core=fr;LIOGO.Culture.UI=fr")

Of course, all settings described in the Settings reference could be changed as well.