ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> tcl-core
tcl-core
[TCLCORE] TIP #266: Numbers are Commands
by Kristoffer Lawson other posts by this author
Apr 12 2006 9:29AM messages near this date
[TCLCORE] Tcl/Tk 8.4.13 RC 2 | Re: [TCLCORE] TIP #266: Numbers are Commands
TIP #266: NUMBERS ARE COMMANDS 
================================
 Version:      $Revision: 1.1 $
 Author:       Kristoffer Lawson <setok_at_fishpool.com> 
 State:        Draft
 Type:         Project
 Tcl-Version:  8.5
 Vote:         Pending
 Created:      Tuesday, 11 April 2006
 URL:          http://purl.org/tcl/tip/266.html
 WebEdit:      http://purl.org/tcl/tip/edit/266
 Post-History: 

-------------------------------------------------------------------------

 ABSTRACT 
==========

 This TIP describes a change to Tcl's command dispatch which would allow 
 every number to act as a command. 

 RATIONALE 
===========

 Maths in Tcl are continuously a stumbling block. The *expr* command is 
 ugly and cumbersome. It adds a new C-like syntax within Tcl and easily 
 makes lines less readable, especially with the most common use of 
 simple arithmetic. In addition it is confusing to the programmer to 
 have to remember there is a completely separate set of commands, or 
 functions, which are only available to the *expr* command. 

 This proposal offers a clean solution which could eventually totally 
 replace *expr*, yet offers few or no backward compatibility problems 
 and fits in with the Tcl philosophy of "every piece of executable code 
 is a command". 

 This addition is easy to build as a separate extension using the 
 mechanisms provided by Tcl. However, this will not be widely used 
 unless it is stamped as part of the language and part of its unique 
 philosophy. In addition, implementations in the core can gain from many 
 forms of optimisation that would not be possible with a normal 
 extension. 

 There is an existing proposal for making operands into commands. That 
 proposal is still a valid one, but the description in this TIP leads to 
 syntax which will be more familiar to most programmers. It does not 
 have operator precedence, which is a difference from normal 
 mathematics, but one that should be easier to grasp than the reversed 
 notation that operator commands offer. In fact, there are many claims 
 which state operator precedence is a mistake anyway. 

 SPECIFICATION 
===============

 Every number that can be handled by Tcl is a command. 

 The format for number commands is: 

       /number/ /operation/ ?/arg1 arg2 .../? 

 For example: 

    8 * 5 + 2

 The first argument must be an operator, and can be one of any specified 
 for the *expr* command (+, -, *, /, ...) 

 Subsequent arguments can be either numbers or operators. 

 The command evaluates its arguments from left-to-right (no precedence) 
 with the result of the left used as the operand for the next operation. 
 /e.g./ to get 242 a simple addition can be made: 

    200 + 42

 If multiple numbers are given without operators between them, the last 
 operator is used. /e.g./ for the result "42" the following can be used: 

    20 + 10 10 2

 To get "23", the following can be used: 

    20 + 10 10 2 / 2 + 2

 Normal Tcl brackets can be used for grouping purposes. /e.g./ the 
 result "31337" can be reached with the following: 

    62674 / [1 + 1]

 Number commands act just like any other commands in Tcl. They can be 
 nested with brackets, aliased, replaced and even removed. The only 
 difference is that they do not appear in the list returned by *info 
 commands*. However, any replacements made on them do. 

 COMPATIBILITY 
---------------

 Because of the ability to replace number-commands, there are no real 
 backwards compatibility issues. There are very few, if any, extensions 
 that make numbers into commands and any that do will still work with 
 the only exception being software that would mix extensions that would 
 depend on the functionality in this TIP and extensions that create 
 their own commands which are only numbers. In any case, the author is 
 not aware of any extensions that create commands in that manner. 

 The only other area where incompatibilities would arise would be if 
 someone is using an *unknown* mechanism which creates similar 
 functionality to the one presented here. These extensions do exist, but 
 are mostly presented as brain-twisters instead of for serious use. 

 Additionally, Tcl does not and cannot guarantee that new commands will 
 not appear in the global namespace. Indeed, every new release does 
 this. 

 For these reasons, this proposal can be implemented with a minor 
 version bump. 

 IMPLEMENTATION DISCUSSION 
===========================

 The first natural step is to add a detection phase before the normal 
 unknown mechanism. In fact, a prototype implementation can easily and 
 trivially be built with Tcl's *unknown* command. The detection should 
 work by checking if the unknown command is a number and if so, it will 
 run the number command functionality. Otherwise, the normal Tcl chain 
 of events is followed. 

 If the command is replaced, this number mechanism is never reached, as 
 a normal command exists to handle the situation. 

 If the command is removed, a special flag should be set to specify that 
 the number in question cannot be used as a command. 

 At a later date, further integration with the bytecode engine can be 
 made. The details of such optimizations are outside the scope of this 
 TIP. 

 ALTERNATIVE APPROACHES 
========================

 Instead of allowing multiple number arguments to use the same operator, 
 a requirement could be made that every other argument must be an 
 operator and every other one an operand. This might make the 
 specification somewhat clearer, but would mean a mathematical operation 
 on a list would not be as easy. 

 COPYRIGHT 
===========

 This document has been placed in the public domain. 

-------------------------------------------------------------------------

 TIP AutoGenerator - written by Donal K. Fellows 


-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
Tcl-Core mailing list
Tcl-Core@[...].net
https://lists.sourceforge.net/lists/listinfo/tcl-core
Thread:
Kristoffer Lawson
Arjen Markus
Peter MacDonald
Kristoffer Lawson
Peter MacDonald
Kristoffer Lawson
Peter MacDonald
Kristoffer Lawson
Arjen Markus
dgp
Arjen Markus
Brian Griffin
Peter MacDonald
Kristoffer Lawson
Gustaf Neumann
Donal K. Fellows
Donald G Porter
Neil Madden
David N. Welton
Arjen Markus
Kristoffer Lawson
Neil Madden
Kristoffer Lawson
Lars Hellström
Neil Madden
Kristoffer Lawson

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved