JTcl

Implementation of programming language Tcl

JTcl is Tcl interpreter written in Java. The first release of JTcl took place on November 17, 2010. JTcl is licensed under BSD open source license.

JTcl is based on Jacl; it aims to continue modernization of Jacl code base that began with Google Summer of Code 2009 Jacl Modernization Project.

JTcl implements most of Tcl 8.4 syntax and features, limited only by the restrictions of JVM. The choice of version to support is explained by the fact that this was the version used in GSOC 2009, and later versions of Tcl contain a lot of changes. However, Tcl team aims to add support for some of the most popular Tcl 8.5 commands.

Main features:

  • JTcl implements Tcl 8.4 commands, arguments, options and semantics. The major exceptions are commands that are limited by Java 1.5 API restrictions, such as file stat.
  • JTcl provides Tcl 8.4-compatible regular expression engine by using Java library regular expression package and using syntax translation where required.
  • fileevent and fcopy commands interact with a new channel subsystem for event-driven I/O processing.
  • exec and open commands now support Tcl‘s pipeline and redirection syntax, with some caveats.
  • 64-bit integer support.
  • trace command now supports command and execution tracing, as well as updated variable trace options.

JTcl logo
JTcl logo

Examples:

Hello, World!:

Example for versions ActiveTcl 8.5, JTcl 2.1.0, Tcl 8.4, Tcl 8.5.7
puts "Hello, World!"

Factorial:

Example for versions ActiveTcl 8.5, JTcl 2.1.0, Tcl 8.4, Tcl 8.5.7

This example uses iterative factorial definition. In Tcl 8.4 values of factorials starting with 13! are calculated incorrectly due to overflow. In later versions and other implementations all values are correct.

set fact 1
for {set i 0} {$i <= 16} {incr i} {
    puts "$i! = $fact"
    set fact [expr {$fact * ($i + 1)}]
}

Quadratic equation:

Example for versions ActiveTcl 8.5, JTcl 2.1.0, Tcl 8.4, Tcl 8.5.7
set A [gets stdin]
if {$A==0} { 
    puts "Not a quadratic equation."; 
    return 
}
set B [gets stdin]
set C [gets stdin]
set D [expr {$B*$B-4*$A*$C}]
set r [expr {-0.5*$B/$A}]
set i [expr {0.5*sqrt(abs($D))/$A}]
if {$D==0} {
    puts "x = $r"
} elseif {$D>0} {
    puts "x1 = [expr {$r+$i}]"
    puts "x2 = [expr {$r-$i}]"
} else {
    puts "x1 = ($r, $i)"
    puts "x2 = ($r, [expr {-$i}])"
}

CamelCase:

Example for versions ActiveTcl 8.5, JTcl 2.1.0, Tcl 8.4, Tcl 8.5.7

This example shows character-wise string processing. A regular expression is used to check whether current character is a letter.

set S [gets stdin]
set S [string tolower $S]
set L [string length $S]
set lastSpace 1
set cc ""
for {set i 0} {$i < $L} {incr i} {
    set letter [string index $S $i]
    if { [string match {[a-z]} $letter] } {
        if { $lastSpace == 1 } { set letter [string toupper $letter] }
        append cc $letter
        set lastSpace 0
    } else {
        set lastSpace 1
    }
}
puts $cc

Fibonacci numbers:

Example for versions ActiveTcl 8.5, JTcl 2.1.0, Tcl 8.4, Tcl 8.5.7

This example uses iterative definition of Fibonacci numbers.

set fib1 0
set fib2 1
set s ""
for {set i 0} {$i < 16} {incr i} {
    set fib3 [expr {$fib1 + $fib2}]
    set fib1 $fib2
    set fib2 $fib3
    append s "$fib1, "
}
puts "$s..."

Fibonacci numbers:

Example for versions ActiveTcl 8.5, JTcl 2.1.0, Tcl 8.4, Tcl 8.5.7

This example uses tail recursion to calculate the numbers. eval command allows to evaluate the result of calling fib with given arguments without declaring fib as part of any namespace.

proc fib {f1 f2 n} {
    if {$n==0} {
        return $f1
    } else {
        return [eval fib $f2 [expr {$f1 + $f2}] [expr {$n - 1}]]
    }
}

set s ""
for {set i 0} {$i < 16} {incr i} {
    append s [eval fib 1 1 $i] ", "
}
puts "$s..."