Difference between revisions of "CalicoDevelopment"

From IPRE Wiki
Jump to: navigation, search
(C# Compiler)
 
(6 intermediate revisions by 2 users not shown)
Line 25: Line 25:
  
 
== Translations ==
 
== Translations ==
 +
 +
If you are interested in getting Calico running in a language other than
 +
English, or if you would like to get students involved in a computer
 +
science project through an alternative approach, this information may be
 +
relative to you.
  
 
Calico was designed to be used with non-English languages, and to be used in many cultures. This effects many aspects of a project, including:
 
Calico was designed to be used with non-English languages, and to be used in many cultures. This effects many aspects of a project, including:
Line 35: Line 40:
 
* language direction (left-to-right, vs. right-to-left)
 
* language direction (left-to-right, vs. right-to-left)
  
These issues are sometimes shortened to "internationalization" (i18n) and "localization" (l10n).  
+
These issues are sometimes shortened to "internationalization" (i18n) and "localization" (l10n).
 +
 
 +
Calico is written a manner to use translations through the
 +
"gettext" system. All strings in the main Calico program have been marked
 +
for version 2.2.8, and exported to the calico.pot, on-line here:
 +
 
 +
http://svn.cs.brynmawr.edu/Calico/trunk/locale/
 +
 
 +
That file is copied to language files (such as es.po and fr.po, for
 +
Spanish and French, respectively) in that same folder. Those po files are
 +
then compiled to .mo files.
 +
 
 +
You also need the Gtk language files on your operating system. On Linux,
 +
those can be installed through the language-packs (eg, sudo apt-get
 +
install language-pack-es).
 +
 
 +
Once that has been done, running Calico, like this (on Linux and Mac):
 +
 
 +
LANGUAGE=es ./StartCalico
 +
 
 +
should result in Calico running in your the identified language. (There
 +
are some strings not translated. Those are bugs.) On Windows, you can edit the StartCalico.bat file.
 +
 
 +
There are many tools for editing .po files. But, we have also uploaded the
 +
calico.pot file to:
  
Calico use the GNU GetText system to help handle these issues. The "locale" in located in the Calico/locale folder.
+
https://poeditor.com/projects/view?id=161
  
There is also a crowd-sourced on-line version of the po files here:
+
That is an on-line editing tool for Calico's strings. Currently, there are
 +
versions for Spansh (es), French, (fr) and Chinese (ch). All of those
 +
could use editing by native speakers.
  
http://poeditor.com/join/project?hash=b595db3c20c33af52b06f0cbc1609fb2
+
If you have questions, please don't hesitate to ask.
  
 
== Virtual Machine ==
 
== Virtual Machine ==
Line 56: Line 87:
 
== C# Compiler ==
 
== C# Compiler ==
  
Mono contains a C# compiler, called mcs, so you are done.
+
Mono contains a C# compiler, called mcs. We use MonoDevelop as our IDE. It is available for all platforms.
  
 
== Monodevelop ==
 
== Monodevelop ==
Line 162: Line 193:
 
== Getting Source Code ==
 
== Getting Source Code ==
  
For those with write permissions to the SVN Bryn Mawr College source code repository, this will give you Calico2:
+
The calico source code is currently hosted on [http://bitbucket.org bitbucket] using [http://git-scm.com/ git].
  
svn co http://svn.cs.brynmawr.edu/Calico/trunk Calico
+
  http://git.calicoproject.org
 +
 
 +
You can clone the repository using git (assuming you have a bitbucket account):
 +
 
 +
  git clone https://USERNAME@bitbucket.org/ipre/calico.git
 +
 
 +
or if you don't have a bitbucket account you can use:
 +
 
 +
  git clone https://bitbucket.org/ipre/calico.git
 +
 
 +
There are a variety of turials online for using [http://git-scm.com/docs/gittutorial git] and [https://confluence.atlassian.com/display/BITBUCKET/Bitbucket+101 bitbucket]
 +
 
 +
 
 +
=== Contributing Code ===
 +
 
 +
Feel free to clone the Calico repository and make improvements. Once completed you can send a pull request to have the changes integrated into the main branch.
 +
 
 +
=== Getting Source Code for Calico 1 ===
 +
 
 +
Calico1 still uses SVN.
 +
 
 +
For those with write permissions to the SVN Bryn Mawr College source code repository
  
or:
 
  
 
  svn co http://svn.cs.brynmawr.edu/Calico/branches/Calico1 Calico
 
  svn co http://svn.cs.brynmawr.edu/Calico/branches/Calico1 Calico
  
which will give you Calico1.
+
 
  
 
Developers who have an Bryn Mawr College SVN account should use:
 
Developers who have an Bryn Mawr College SVN account should use:
  
svn co https://svn.cs.brynmawr.edu/Calico-dev/trunk Calico
+
svn co https://svn.cs.brynmawr.edu/Calico-dev/branches/Calico1 Calico
  
or:
 
  
svn co https://svn.cs.brynmawr.edu/Calico-dev/branches/Calico1 Calico
+
== Support Libraries ==
  
You can browse the source code here:
+
Please see [[Calico Download]] for additional libraries needed to run and develop Calico.
 
+
* http://svn.cs.brynmawr.edu/viewvc/Calico/
+
  
 
== Educational Research ==
 
== Educational Research ==
Line 206: Line 254:
 
== Language ==
 
== Language ==
  
A Language file in Calico/languages/*.dll defines everything needed for a language: the editing document, the language engine, etc. Documents can do things like open, save, and display data for editing. Engines can do things like execute text, execute files, and parse files. Engines also allow for the languages to share data and functionality.
+
See [[Calico: Language Development]]
 
+
You can also define a Calico Language in terms of another Calico Language. For this example, you need to create a directory in Calico/languages, and create a file in that directory.
+
 
+
For example:
+
 
+
# create a folder named '''Calico/languages/Test''' (name can be most anything)
+
# create a file in that folder named '''CalicoTest.py''' (name should start with "Calico"; currently Python and Ruby are the only languages available for defining new languages)
+
 
+
That file could have something like the following:
+
 
+
<pre>
+
# File: Calico/languages/CalicoTest.py
+
# Setup:
+
import sys, os
+
import clr                # allows connection to CLR/DLR bits
+
clr.AddReference("Calico") # the main Calico.exe
+
import System
+
import Calico
+
 
+
# Now, define the Document, Engine, and Language classes:
+
class TestEngine(Calico.Engine):
+
    def PostSetup(self, calico):
+
        pass
+
 
+
    def Execute(self, text):
+
        print("ok")
+
        return True
+
 
+
    def ExecuteFile(self, filename):
+
        print("Run filename '%s'!" % filename)
+
        return True
+
 
+
    def ReadyToExecute(self, text):
+
        ## Return True if expression parses ok.
+
        return True
+
 
+
class TestDocument(Calico.TextDocument):
+
    def GetAuthors(self):
+
        return System.Array[str]([
+
            "Name1 <name1@place.edu>",
+
            "Name2 <name2@place.edu>"
+
        ])
+
 
+
class TestLanguage(Calico.Language):
+
    def __init__(self):
+
        self.name = "test"
+
        self.proper_name = "Test"
+
        self.extensions = System.Array[str](["tt"])
+
        self.mimetype = "text/plain"
+
        self.LineComment = "::"
+
 
+
    def MakeEngine(self, manager):
+
        self.engine = TestEngine(manager)
+
 
+
    def MakeDocument(self, calico, filename):
+
        return TestDocument(calico, filename, self.name, self.mimetype)
+
 
+
    def GetUseLibraryString(self, fullname):
+
        pass
+
 
+
# And finally define a method of loading it:
+
def MakeLanguage():
+
    return TestLanguage()
+
</pre>
+
 
+
In a couple of places we are being careful about exactly the type of item we create (for example, creating an Array rather than a list). At some point, we can make the interface a little more flexible.
+
  
 
= TODO =
 
= TODO =

Latest revision as of 00:33, 13 June 2013

This page describes how to work on the Calico source code. It is meant for developers who would like to understand how Calico works.

This document describes the latest version of Calico, version 2 and greater.

What is Calico?

First, let's take a step back and see Calico in the big picture. What is it exactly?

Calico is:

  1. A program written in C# that runs under Mono and .NET
  2. It has abstractions for dynamically loading two kinds of libraries:
    1. Languages
    2. Libraries

Calico is mostly GUI code, but it is really designed to be the "glue" between Languages and Libraries. Calico uses Gtk# (pronounced "gee tee kay sharp") for the graphics. Gtk is a general purpose "tool kit"... it is written in C, but has wrappers in most languages, including C++, Python, and C#. There are other graphical tool kits, but we chose Gtk because of its good support and tight connection with Mono.

.NET and Mono are Virtual Machines (VM), and a whole lot more. To try to be clear for the rest of this document, We'll use the term VM to refer to either the .NET or Mono virtual machines when it doesn't matter which.

You see the sharp sign (#) on a few libraries; this is usually a sign to indicate that the library works on the .NET VM. Not all programs written for the .NET VM will run on the Mono VM, and not all code written for the Mono VM will run on the .NET VM.

Getting Started with Development

If you want to just run Calico, see Calico Download.

Translations

If you are interested in getting Calico running in a language other than English, or if you would like to get students involved in a computer science project through an alternative approach, this information may be relative to you.

Calico was designed to be used with non-English languages, and to be used in many cultures. This effects many aspects of a project, including:

  • language, including plural noun formations
  • font
  • numeric representations
  • currency
  • date/time representations
  • language direction (left-to-right, vs. right-to-left)

These issues are sometimes shortened to "internationalization" (i18n) and "localization" (l10n).

Calico is written a manner to use translations through the "gettext" system. All strings in the main Calico program have been marked for version 2.2.8, and exported to the calico.pot, on-line here:

http://svn.cs.brynmawr.edu/Calico/trunk/locale/

That file is copied to language files (such as es.po and fr.po, for Spanish and French, respectively) in that same folder. Those po files are then compiled to .mo files.

You also need the Gtk language files on your operating system. On Linux, those can be installed through the language-packs (eg, sudo apt-get install language-pack-es).

Once that has been done, running Calico, like this (on Linux and Mac):

LANGUAGE=es ./StartCalico

should result in Calico running in your the identified language. (There are some strings not translated. Those are bugs.) On Windows, you can edit the StartCalico.bat file.

There are many tools for editing .po files. But, we have also uploaded the calico.pot file to:

https://poeditor.com/projects/view?id=161

That is an on-line editing tool for Calico's strings. Currently, there are versions for Spansh (es), French, (fr) and Chinese (ch). All of those could use editing by native speakers.

If you have questions, please don't hesitate to ask.

Virtual Machine

First you'll need a VM. We'll be using the latest version, called version 4. You might need to update some items on your computer.

  • On all systems, you'll need Mono, version 2.10 or later
    • On Windows and Mac:
    • On Linux, you can use your package manager to install Mono
      • On Ubuntu, Debian systems: sudo apt-get install mono
      • On Fedora, Redhat systems: yum install mono

C# Compiler

Mono contains a C# compiler, called mcs. We use MonoDevelop as our IDE. It is available for all platforms.

Monodevelop

For more sophisticated devlopment, we'll use MonoDevelop. You can install it from here:

http://monodevelop.com/Download

Gtk#

The only other item we'll need for building programs in general is Gtk#, the graphics tool kit for the VM. It contains the low-level C libraries, and the "wrappers" for any VM language, like C#.

  • Windows: it comes with Mono
  • Mac: it comes with Mono
  • Linux: you'll need to install it from your package manager
    • Ubuntu: sudo apt-get install gtk-sharp

From the Bottom Up

To understand all of the technology of Calico, let's start at the beginning, Hello World.

We'll be running these commands from the command line. In Windows, you can run "cmd"; under Mac and Linux we'll use the Terminal or Console.

On any platform, create a file named HelloWorld.cs with the contents:

// HelloWorld.cs
public class HelloWorld {
    public static void Main(string [] args) {
        System.Console.WriteLine("Hello, World!");
    }
}

How to edit? You can use the Text Editor, gedit, Emacs, etc. We'll use MonoDevelop later, but for now, we just need to create a simple text file.

Now, we'll compile this C# code for the VM using either:

  • .NET, use csc
  • Mono, use mcs

For the rest of this document, we'll just use "mcs" to represent whichever of these you need (eg, "mcs" on Mono, "csc" on .NET).

To compile:

mcs -out:HelloWorld.exe HelloWorld.cs

This will create the file HelloWorld.exe ready to run on the VM. On Windows, you can just type "HelloWorld" or double-click it and it should display the text in a console. On Linux or Mono where the .exe is not associated with Mono, you will need to run it like:

mono HelloWorld.exe

When writing programs, you may get tired of typing the fully specified name of a function, like "System.Console.WriteLine". In C# you can use the form "using XXX;" to include everything in that "name space":

// HelloWorld2.cs
using System;

public class HelloWorld {
    public static void Main(string [] args) {
        Console.WriteLine("Hello, World!");
    }
}

The file "HelloWorld.exe" can be used as a library in the VM. But you can also compile C# code to be used as a library only. In that way, you don't need a "Main" static method. When you compile to a library, the file will have an extension of ".DLL".

// MyLibrary.cs
public class MyLibrary {
    public static int Answer() {
        return 42;
    }
}
mcs -out:MyLibrary.dll -target:library MyLibrary.cs

To use the library:

// HelloWorld3.cs
using System;

public class HelloWorld {
    public static void Main(string [] args) {
        Console.WriteLine("Hello, World; the answer is {0}", MyLibrary.Answer());
    }
}

And compile the code and library:

mcs -out:HelloWorld3.exe -r:MyLibrary HelloWorld3.cs

Running that gives:

$ mcs -out:HelloWorld3.exe -r:MyLibrary HelloWorld3.cs 
$ mono ./HelloWorld3.exe 
Hello, World; the answer is 42

Calico2

Let's now dive into Calico. First, you'll need the source code if you don't already have it.

Getting Source Code

The calico source code is currently hosted on bitbucket using git.

 http://git.calicoproject.org

You can clone the repository using git (assuming you have a bitbucket account):

 git clone https://USERNAME@bitbucket.org/ipre/calico.git

or if you don't have a bitbucket account you can use:

 git clone https://bitbucket.org/ipre/calico.git

There are a variety of turials online for using git and bitbucket


Contributing Code

Feel free to clone the Calico repository and make improvements. Once completed you can send a pull request to have the changes integrated into the main branch.

Getting Source Code for Calico 1

Calico1 still uses SVN.

For those with write permissions to the SVN Bryn Mawr College source code repository


svn co http://svn.cs.brynmawr.edu/Calico/branches/Calico1 Calico


Developers who have an Bryn Mawr College SVN account should use:

svn co https://svn.cs.brynmawr.edu/Calico-dev/branches/Calico1 Calico


Support Libraries

Please see Calico Download for additional libraries needed to run and develop Calico.

Educational Research

You will also need:

  • mono
  • gtk (and libgtk2.0-cil-dev, for package-config files)

Overview

There are 5 directories in the Calico folder:

  1. bin - contains the startup exe and dll files
    1. bin/Lib - contains the standard Python libraries
  2. languages - contains the language definition files for Python, Ruby, Scheme, etc.
  3. modules - Cross-language modules that can be used by all Calico languages
  4. examples - sample code, broken down by language
  5. Source - the code to build the Calico .exe

Language

See Calico: Language Development

TODO

See NOTES in the Calico download (package or svn).

Screen Shots

Pyjama-010.gif

For more, see CalicoScreenShots

Python and Ruby

Calico allows you to easily jump back and forth between languages. Enter this code in the command box:

def fib(n):
    if n <= 2: return 1
    return fib(n - 1) + fib(n - 2)

Make sure the bottom status line has Python checked, and press the Run! button (or simply hit <enter> when in the command box).

Now, enter this code in the command box (make sure to set bottom status line has Ruby checked).

class Hello_world
  def greet
    puts "Hello World"
  end
end
hw = Hello_world.new
hw.greet

Cross-language Access

In this example, you'll see that you can access code and variables defined in other languages.

Enter the following in the Interactive Command Box:

class PythonClass:
     def hello(self, value):
         print "Python says hello to", value

This defines a PythonClass. To test it out, enter the following in the command box:

pc = PythonClass()
pc.hello("Python")

In the Output Box, you'll of course see:

Python says hello to Python

Now, switch to Ruby mode (bottom line status bar, select Ruby) and enter this in the command box:

pc = python_class().new
pc.hello "Ruby"

This uses the PythonClass (now called python_class). In Ruby, you can create an object by placing the .new after it. You call the method .hello by placing the arguments after the method name (parens are not needed in Ruby).

You will see in the output box:

Python says hello to Ruby

which is Python code running inside Ruby!

While in Ruby Mode, enter:

class Ruby_class
   def hello caller
     puts "Ruby says hello to #{caller}"
   end
end

To see that work, try:

rc = Ruby_class.new
rc.hello "Ruby"

and you'll of course see:

Ruby says hello to Ruby

Finally, switch back to Python Mode, and enter:

import Ruby_class
rc = Ruby_class()
rc.hello("Python")

and you'll see:

Ruby says hello to Python

which is Ruby code running in Python!



Interacting with .NET

The languages of Calico can also interact with .NET/Mono. If you are already familiar with .NET/Mono programs (such as those written in C# or Visual Basic) then you will recognize some of these idioms and names.

As a simple example, let's use the interactive abilities to directly interact with some Windows Forms. In order to do this in Calico, will turn the threaded ability off, so as to talk to the GUI in the same thread as Calico is running in. Let's create a window (called a Form in .NET parlance):

# file: winform.py
import clr
clr.AddReference("System.Windows.Forms")
from System.Windows.Forms import *
win = Form()
win.Show()

Importing the special module named clr is the gateway to interacting with the underlying .NET/Mono subsystem. From the clr module, you can add a reference to any Dynamic Link Library (DLL) so that you can access library functions and objects. In this example, we add a reference to the System.Windows.Forms DLL which contains a library of Graphical User Interface (GUI) objects. We then can create a Form, save it in a variable named win, and call the form's Show() method.

If you'd like to give the window a name, enter this in Calico's interactive command box:

win.Text = "This is the Window Title"

These windows will go away when you close Calico, or you can close them manually.

Developing a Language for the DLR in the DLR

This section needs to be revised for Calico2

Python and Ruby are defined to interact as above by taking advantage of the Dynamic Language Runtime (DLR). To further show this off, here is an example of defining a new language, much like Lisp, called SymPL written in Python using the tools of the DLR:

  1. lexer.py - the lexer
  2. parser.py - the parser
  3. etgen.py - expression tree generators
  4. runtime.py - runtime helpers (e.g., lookup, import)
  5. test.py - the Read Eval Print Loop

Some sample SymPL programs:

  1. test.sympl - function tests
  2. ops.sympl - operator tests
  3. lists.sympl - list tests

Writing Fast Module Code Once

Perhaps the nicest aspect of the DLR is that you can develop code (in CSharp, for example) that is "compiled" and used directly. Such can be imported and used by any language in the DLR suite as if it were written in that language. For example, consider this simple example:

// file: Testing.cs
public class Point {
  private int _x;
  private int _y;
  public Point(int x, int y) {
	_x = x;
	_y = y;
  }
  public int x {
	get { return _x; }
	set { _x = value; }
  }
  public int y {
	get { return _y; }
	set { _y = value; }
  }
  public string __repr__() {
	return "<Point at (" + _x + "," + _y +")>";
  }
}

This can be compiled in Mono:

gmcs Testing.cs -target:library

or put into a Project in Visual Studio, and compiled into a library.

You should then put the resulting Testing.dll somewhere will Calico can find it (either put in the same directory with the calico.exe, or put it in a directory and add the path of the directory to Python's sys.path):

import sys
sys.path.append("/path/to/folder")

Finally, you can use it in Calico:

>>> import clr
>>> clr.AddReference("Testing")
>>> import Point
>>> Point
<type 'Point'>
>>> p = Point(1,2)
>>> p
<Point at (1,2)>
>>> p.x
1
>>> p.x = 9
>>> p
<Point at (9,2)>
>>> 

Creating Interactive Programs and Graphics for the Web

Theoretically, once you have a working program in Calico, you can save the program to run on the Web in a manner very much like Flash applications. The resulting program can (possibly) be run in a browser using Silverlight/Moonlight. Need to explore this.

Troubleshooting

If you have any trouble, find bugs, or want to make a feature request, please do that at:

calico.codeplex.com/WorkItem/