Skip to content

Objecttypes

wolfwejgaard edited this page Jan 4, 2015 · 31 revisions

####Tcl Variables and Commands# Tcl has a rich set of data types. There are all common variations of numbers. And strings, lists, arrays, and dicts (dictionaries). The Tcl data are handled as arguments to a command. The data type is specific to the command, but the data elements are all variables. The command checks if the contents of the variable conforms to the type of the command.

There are two classes of commands. E.g.:

  • string length string
  • llength list

String commands are subcommands to a general command string, list commands are general.

####TclForth Objecttypes#

TclForth provides a uniform way to handle the Tcl data types as objects that respond to private methods. TclForth objecttypes are static classes that create instances and apply methods.

  • string length
  • list length

The objecttypes are implemented as associative arrays. The names are the commands (messages) and the values are execution scripts (methods) of an object.

Example: Variable#

objecttype variable
     instance  {set obj [pop]}
     {}        {push $obj}
     get       {push $obj}
     @         {push $obj}
     set       {set obj [pop]}
     !         {set obj [pop]}
     ....

####Comments#

  • The instance method is used when an object is created, it defines the instance data.
  • If an object is used without a message, the default message {} applies.
  • You can apply several messages to the same method: Being a Forth programmer you might prefer to use @ and !.
  • TclForth implements the [data types](data types): Constant, Variable, Array, List, String, File
  • The objecttypes are defined in forth.fth. They can easily be expanded.

####Custom Objecttype File#

With a little knowledge of the native Tcl you can create your own types. Like this:

Objecttype file  
	instance  {set obj "[pop] handle" ; }
	{}        {@list $obj 1}   
	open-w    {push [open [lindex $obj 0] w]; !list obj 1 }
	open      {push [open [lindex $obj 0] r]; !list obj 1 }
	close     {close [lindex $obj 1]}
	put       {puts [lindex $obj 1] [pop]}
	get       {push [gets [lindex $obj 1]]}
	read      {push [read [lindex $obj 1]]}
	eof       {push [eof [lindex $obj 1]]}
Example:  "forth.fth" file sfile
Use:      sfile open
          begin sfile get .cr
                sfile eof
          until
          sfile close
Clone this wiki locally