Calico Console

From IPRE Wiki
Jump to: navigation, search

The Console programming language is based on the Unix and DOS command line. This is a simplified version of a full shell language, but you can do quite a bit. There are no flow-of-control constructs (if, for, while, etc.). You cannot run executables with this language, but you can run Calico scripts.

This language does not depend on what operating system you are running on. That is, if you are on Windows, then you can still use the Unix commands. Likewise, if you are on Mac OSX, then you can use the DOS commands. The Console language is a stand-alone system that does not use the underlying OS.

Console Features

The Console language is a first-class Calico language. It supports:

  • tracing, stepping, and break-point debugging
  • language interop
  • using Calico modules

As a shell language, you can chain little commands together to create a useful script. Use the pipe character "|" to send the output of one command to the input of another. Use ">" and ">>" to redirect output to files. Also, you can use back-quoted expressions to insert executed Console commands into other locations in a script.

To use the tracer, stepping, and break-point, put your commands into a New Console Script, and step from there.

Calico-console.gif

Pipe and Redirect

You can pipe commands:

ls | cat -n

You can redirect output:

ls | cat -n > output

You can also append to an existing file:

ls | cat -n >> output

Unix Commands

These are the commands available when in unix mode:

  • cat - concatenate files
  • cd - change directory
  • cp - copy files and folders
  • echo - create output
  • open - open a file in Calico
  • eval - evaluate text in Calico
  • exec - execute a file in Calico
  • grep - search for matches
  • head - get first few lines of output
  • help - get help on commands
  • more - see output one page at a time
  • ls - list files
  • mkdir - make a directory
  • more - see output one page at a time
  • mv - move files and folders
  • open - open a file in Calico
  • print - display output
  • pwd - print working directory
  • rm - remove file or folders
  • rmdir - remove folders
  • show - show an image graphically
  • sort - sort data
  • switch - to unix or dos
  • tail - get end of output

DOS Commands

The are the commands available when in dos mode:

  • cd - change directory
  • chdir - change directory
  • cp - copy files and folders
  • rm - remove file or folders
  • ls - list files
  • print - display output
  • open - open a file in Calico
  • rm - remove file or folders
  • eval - evaluate text in Calico
  • exec - execute a file in Calico
  • head - get first few lines of output
  • help - get help on commands
  • mkdir - make a directory
  • md - make a directory
  • more - see output one page at a time
  • mv - move files and folders
  • open - open a file in Calico
  • pwd - print working directory
  • rmdir - remove folders
  • move - move files and folders
  • mv - move files and folders
  • rmdir - remove folders
  • show - show an image graphically
  • sort - sort data
  • switch - to unix or dos
  • tail - get last few lines of output

Variables

Variables are stored in the global, shared-between-languages environment space.

There two ways to set variables: directly (using Console constructs), and using variables and letting Python evaluate the right-hand side of the assignment.

Directly uses the command SET:

SET x="Hello World"
echo $x
SET data = `ls *.txt`
echo $data

You can also use Python to evaluate an expression:

x='"Hello World"'
echo $x

Here are some additional examples using Python to evaluate expressions:

x = 1
x = $x + 1
echo $x

For more complex Python expressions, you might need to quote the expression:

x = 1
x = 'x + 1'
echo $x

If you then switch to another language, you will see that the variable x is now defined there.

Interop

There are three ways to interoperate with other languages:

  • exec STATEMENT LANGUAGE
  • eval EXPRESSION LANGUAGE
  • x = EXPRESSION

Here is an example controlling a robot using Python and Myro with eval and exec:

exec "import Myro" python
exec init() python
eval getLight() python
eval 'getLight("left")' python

Here are examples using Python to evaluate an expression:

x = '"This is a string"'
y = 'x + ", and this too"'
z = 1
z = $z + 1
z = 'z + 1'

The first creates a string in Python, and assigns it to a shared variable x.

The second uses the global variable x and adds more string to it.

The third uses Python to evaluate a number, 1.

The forth evaluates $z in Console, and then uses Python to add one to it.

The fifth passes 'z + 1' and lets Python lookup z and do the math.

Examples

console> echo Hello World
Hello World
console> echo "Hello, World!"
Hello, World!


console> ls
.:
Calico Calico.sln Makefile Packages.mdproj
console> ls -l            
/home/dblank/Calico/Source/Calico
/home/dblank/Calico/Source/Calico.sln
/home/dblank/Calico/Source/Makefile
/home/dblank/Calico/Source/Packages.mdproj
console> ls -l | grep Make
/home/dblank/Calico/Source/Makefile
console> echo `grep xml *` | cat -n 
grep: no such file 'Calico'
     1 Packages.mdproj: <?xml version="1.0" encoding="utf-8"?> Packages.mdproj: <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">

Example showing running code from Console via Calico --repl:

dblank@vaio:~/Calico$ calico --repl
Loading Calico version 2.3.8...
python> :lang console
console> ls
.:
Lib Makefile NOTES README.txt Source StartCalico StartCalico.app
StartCalico.bat bin data examples languages locale make-macpack make-zip
make-zip-mac make-zip.bat modules plugins server src
console> cd examples
/home/dblank/Calico/examples
console> ls  
.:
__init__.py csharp data dinah fsharp images jigsaw logo lua python ruby scheme
sounds spreadsheet sympl
console> cd python
/home/dblank/Calico/examples/python
console> ls
.:
AI CS Graphics Misc Music Processing Robots Science Sprites __init__.py nehe
console> cd CS
/home/dblank/Calico/examples/python/CS
console> ls
.:
Functions.py MakeACalicoLanguage.py fsm_eval.py helloworld.py levenshtein.py
mergesort.py ngrams.py simulation.py
console> cat Functions.py
# Python Function Examples

# Create a function that will add 1 to a given number:

def add1(x):
    return x + 1

# Create a function that will add two numbers together:

def addem(x, y):
    return x + y

# Call both of the functions, and display the results:

print(add1(4))
print(addem(42, 8))

console> exec Functions.py       
5
50
Done
None
console> :lang python
python> dir()
['__builtins__', '__doc__', '__file__', '__name__', 'add1', 'addem', 'calico', 'input', 'raw_input']
Ok
python> add1(45)  
46
Ok
python> ^d  

dblank@vaio:~/Calico$ 

eval Examples

Directly calling eval:

console>> eval "1 + 1" python
2
console>> eval "(+ 1 1)" scheme
2
console>> eval "(+ 40 2)" scheme | cat -n
     1 42

Piping to eval:

console>> echo '"1 + 1"' python | eval
2

Piping to an evaluated expression:

console>> echo 1  | eval "lambda x: x" python
1
console>> echo 1  | eval "lambda x: int(x)" python
1
console>> echo 1  | eval "lambda x: int(x) + 1" python
2
## In Calico Scheme, func make a function callable
## from other languages:
console>> echo 1 | eval "(func (lambda (n) n))" scheme
1
console>> exec "import Myro" python
True
console>> eval "Myro.pickAFile()" python
/home/dblank/Calico/languages/CSharp/CalicoCSharp.cs
console>> eval "Myro.pickAFile()" python | cat -n
     1 /home/dblank/Calico/modules/Myro/Robots/Hummingbird.cs
console>> eval "Myro.pickAFile()" python | cat
/home/dblank/Calico/modules/Myro/Robots/Hummingbird.cs
console>> cat `eval "Myro.pickAFile()" python`
...
console>> grep "def " `eval "Myro.pickAFile()" python`
console>> grep "import " `eval "Myro.pickAFile()" python`
/home/dblank/Downloads/alarm.py: from Myro import *

Pipe objects and functions:

console>> eval "lambda x: x" python | eval "lambda f: f(1)" python
1

Chain of functions in different languages (requires Calico 2.3.9 or greater):

echo -e "1\n2\n3" | eval "lambda x: int(x) + 1" python | eval "(func (lambda (n) (* n 2)))" scheme

Notice that the first function, the x is a string. Subsequent functions, it is an integer.

Infinite Streams (requires Calico 2.3.9 or greater):

# nats.py
def nats():
    count = 0
    while True:
        yield count
        count += 1

echo can take a generator:

echo `eval "nats()" python` | eval "lambda n: n + 1" python | less