Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nat-418/tutstack
An example Tcl package that implements a stack data structure.
https://github.com/nat-418/tutstack
Last synced: 13 days ago
JSON representation
An example Tcl package that implements a stack data structure.
- Host: GitHub
- URL: https://github.com/nat-418/tutstack
- Owner: nat-418
- License: 0bsd
- Created: 2021-11-10T11:45:34.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2021-11-09T08:27:13.000Z (about 3 years ago)
- Last Synced: 2024-11-05T11:36:45.711Z (about 2 months ago)
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Tutstack
The following example of a Tcl package is sourced from the [official Tcl 8.5
documentation](https://www.tcl.tk/man/tcl8.5/tutorial/Tcl31.html).This example creates a package which provides a stack data structure:
```
# Register the package
package provide tutstack 1.0
package require Tcl 8.5# Create the namespace
namespace eval ::tutstack {
# Export commands
namespace export create destroy push pop peek empty# Set up state
variable stack
variable id 0
}# Create a new stack
proc ::tutstack::create {} {
variable stack
variable idset token "stack[incr id]"
set stack($token) [list]
return $token
}# Destroy a stack
proc ::tutstack::destroy {token} {
variable stackunset stack($token)
}# Push an element onto a stack
proc ::tutstack::push {token elem} {
variable stacklappend stack($token) $elem
}# Check if stack is empty
proc ::tutstack::empty {token} {
variable stackset num [llength $stack($token)]
return [expr {$num == 0}]
}# See what is on top of the stack without removing it
proc ::tutstack::peek {token} {
variable stackif {[empty $token]} {
error "stack empty"
}return [lindex $stack($token) end]
}# Remove an element from the top of the stack
proc ::tutstack::pop {token} {
variable stackset ret [peek $token]
set stack($token) [lrange $stack($token) 0 end-1]
return $ret
}
```To make a Tcl package, run this command:
```
$ echo 'pkg_mkIndex .' | tclsh
```You should now have a
And some code which uses it:
```
package require tutstack 1.0set stack [tutstack::create]
foreach num {1 2 3 4 5} { tutstack::push $stack $num }while { ![tutstack::empty $stack] } {
puts "[tutstack::pop $stack]"
}tutstack::destroy $stack
```
You should now have a `pkgIndex.tcl` file that looks like this:
```
# Tcl package index file, version 1.1
# This file is generated by the "pkg_mkIndex" command
# and sourced either when an application starts up or
# by a "package unknown" script. It invokes the
# "package ifneeded" command to set up package-related
# information so that packages will be loaded automatically
# in response to "package require" commands. When this
# script is sourced, the variable $dir must contain the
# full path name of this file's directory.package ifneeded tutstack 1.0 [list source [file join $dir tutstack.tcl]]
```