GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Description: A Nu and Objective-C wrapper for John Ousterhout's Tool Command Language (Tcl).
Homepage: http://programming.nu
Clone URL: git://github.com/timburks/nutcl.git
nutcl / demo.nu
100644 56 lines (48 sloc) 2.163 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
;; @file demo.nu
;; @discussion NuTcl demonstration.
;; @copyright Copyright (c) 2008 Neon Design Technology, Inc.
;;
;; Licensed under the Apache License, Version 2.0 (the "License");
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;; http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.
 
(load "NuTcl")
 
;; this command creates an array from its arguments and returns it.
(class ArrayCommand is TclCommand
     (- (id) main:(id) arguments is
        (arguments subarrayWithRange:(list 1 (- (arguments count) 1)))))
 
;; this command counts the members of an array and returns the count.
(class CountCommand is TclCommand
     (- (id) main:(id) arguments is
        (set a (arguments 1))
        (puts "counting elements in array #{(a description)}")
        (a count)))
 
;; this command adds its arguments and returns the result.
(class AddCommand is TclCommand
     (- (id) main:(id) arguments is
        ((arguments subarrayWithRange:(list 1 (- (arguments count) 1))) reduce:(do (x y) (+ x y)) from:0)))
 
;; this is our Tcl interpreter wrapper.
(class TclInterpreter
     (- (void) install:(id) commandClass withName:(id) name is
        (self addCommand:((commandClass alloc) init) withName:name)))
 
;; create a Tcl interpreter and install our commands.
(set tcl ((TclInterpreter alloc) init))
(tcl install:ArrayCommand withName:"array")
(tcl install:CountCommand withName:"count")
(tcl install:AddCommand withName:"add")
 
;; run the interpreter to evaluate some instances of our commands.
(tcl eval:<<-END
  set x [array 1 2 3]
  puts "we just made an array $x"
  puts "the number of elements in the array is [count $x]"
  puts "the sum of the first five whole numbers is [add 1 2 3 4 5]"
END)
 
;; try this in an interactive nush session
;(tcl interact)