sponsor Vim development Vim logo Vim Book Ad

tcalc : A small ruby-based RPN-calculator

 script karma  Rating 4/1, Downloaded by 4787  Comments, bugs, improvements  Vim wiki

created by
Tom Link
 
script type
utility
 
description
It turns out that ruby is pretty good at maths and deals well with
complex or rational numbers etc. Anyway, in certain occasions a postfix
syntax and a stack are better suited for calculations than ruby's infix
syntax. So, here comes ...

:TCalc[!]
    A small ruby-based[*] RPN-calculator, a simple ASCII function
    plotter, and stack-based playground :-).

The tcalc.rb script can also be used as a stand-alone calculator (with
optional support for curses and readline). Run "tcalc --help" for
information on the command-line options.

Input:
    - Numbers (anything that starts with "-" or a decimal)
    - Strings (anything that matches /^"(.*?)"$/)
        - Be aware that the strings and numbers get evaluated by ruby.
          You can thus execute ruby code by input like: -1;p'foo'
    - Methods & constants of the following classes are available:
        Float    :: http://www.ruby-doc.org/core/classes/Float.html
        Complex  :: http://www.ruby-doc.org/core/classes/Complex.html
        Rational :: http://www.ruby-doc.org/core/classes/Rational.html
        Integer  :: http://www.ruby-doc.org/core/classes/Integer.html
        Matrix   :: http://www.ruby-doc.org/core/classes/Matrix.html
        Vector   :: http://www.ruby-doc.org/core/classes/Vector.html
        Math     :: http://www.ruby-doc.org/core/classes/Math.html
    - #N (pull the item at position N to the top)
    - Commands: COMMAND[COUNT][,ARGUMENT]
        - If COUNT is "#", the top element will be used.
    - Enter, escape => exit

Tokens are separated by blanks (à la forth). The blanks between the
words thus are significant.
 
install details
As VIM-plugin:
Extract the archive's contents to ~/vimfiles (or ~/.vim).

Stand-alone ruby script:
Copy the file ruby/tcalc.rb to your path (you could also rename it to
tcalc).


Also available via git
http://github.com/tomtom/tcalc_vim
 

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
tcalc.vba 0.13 2011-05-26 7.0 Tom Link - help command (not really useful yet)
- help (not really useful yet)
- ruby 1.8 compatibility
- FIX: help command
- Interpret .[0-9]+ as float (suggested by John Orr)
- .gitignore
MD5 checksum: b2f2bf21e1fe9cb0bdc612a7cb973621
tcalc.zip 0.11 2007-12-05 7.0 Tom Link - New words: all?, any?, array_*, and, or, !=
- Curses frontend: Show possible completions; map 127 to backspace, F1
to 'ls'; improved handling of cursor keys etc.
- Debugger (sort of)
- FIX: Nested blocks & more
tcalc.zip 0.10 2007-11-30 7.0 Tom Link - rm,* ... Remove all words
- If g:tcalc_lines < 0, use fixed window height.
- VIM: use the tcalc window to display plots, lists etc.
- FIX: Nested words and some more
tcalc.zip 0.9 2007-11-28 7.0 Tom Link - FIX: Curses frontend: Display error messages properly
- FIX: readline support.
- FIX: sort words on completion
- Distribute as zip
tcalc.vba.gz 0.8 2007-11-27 7.0 Tom Link - Named arguments: args is a synonym for assert but provides for named
arguments.
- New words: Sequence/seq, map, mmap, plot (a simple ASCII function
plotter), stack_size, stack_empty?, iqueue_size, iqueue_empty?
- Syntactic sugar for assignments: VALUE -> VAR
- Defined "Array" as a synonym for "group"
- "define" command as alternative to the forth-like syntax for defining
words
- Dynamic binding of words/variables (the words "begin ... end"
establish a new scope)
- The stack, the input queue, and the dictionary are accessible like
words (__STACK__, __IQUEUE__, __WORDS__)
- TCalc and tcalc#Calculator take initial tokens as argument.
- TCalc! with [!] will reset the stack & input queue.
- Completion of partial commands
- Readline-support for CLI mode (--no-curses).
- Simple key handling for the curses-based frontend
- Non-VIM-versons save the history in ~/.tcalc/history.txt
- #VAR,METHOD has slightly changed.
- TCalc syntax file.
- FIX: Command line completion
tcalc.vba.gz 0.7 2007-10-29 7.0 Tom Link - Comments: /* ... */
- New words:
    - assert: Display an error message if the stack doesn't match the
    assertion.
    - validate: Like assert but push a boolean (the result of the check)
    on the stack.
    - do: synonym for recapture.
    - source: load a file (see also g:tcalc_dir)
    - require: load a ruby library
    - p: print an object (doesn't do much, but prettyprint seems
    to have problems)
    - history (useful when using tcalc as stand-alone calculator)
- tcalc.rb can now be used as stand-alone program (a simple
curses-based frontend).
tcalc.vba.gz 0.6 2007-10-26 7.0 Tom Link - Included support for rational and complex numbers
- Included matrix support
- Syntax for pushing arrays [ a b c ... ]
- New "at" method to select an item from array-like objects
- Removed shortcut variables.
tcalc.vba.gz 0.5 2007-10-12 7.0 Tom Link - Minor fix: command regexp
tcalc.vba.gz 0.4 2007-10-08 7.0 Tom Link - COUNT can be "#", in which case the top number on the stack will be
used (e.g. "3 dup3" is the same as "3 3 dup#")
- Disabled vars, (, ) commands
- Variables are words
- New words can be defined in a forth-like manner ":NAME ... ;"
- Built-in commands get evaluated before any methods.
- Messages can be sent to objects on the stack by "#N,METHOD", e.g. "1 2
g2 3 #1,<<" yields "[1,2,3]"
- The copyN, cN command now means: push a copy of element N.
- ( ... ) push unprocessed tokens as array
- recapture command (feed an array of unprocessed tokens to the input
queue)
- if, ifelse commands
- delN, deleteN commands
- Can push strings ("foo bar")
- "Symbols" à la 'foo (actually a string)
tcalc.vba.gz 0.3 2007-10-07 7.0 Tom Link - The swap count argument is increased by one (for conformance with the
rot command).
- Shortcuts are now RPN expression (elements at the stack can be
referred to by # (= top element) or #N).
- Removed g:tcalc_reverse_display
- Positions on the stack can be referred to by #N.
- rot works the other way round
- d, dup command
- clear command
- print, hex, HEX, oct, dec, bin, float, format commands
- Removed dependency on tlib
- Variables; ls, vars, let, =, rm commands
- Command line completion for variables and commands
tcalc.vba.gz 0.2 2007-10-07 7.0 Tom Link - Arguments were (not properly) reverted: 12 4 / now yields 3.
- The input will be split into tokens, i.e. you can input "1 2 + <cr>"
or "1<cr>2<cr>+<cr>". (Command-line completions doesn't work properly
though.)
- The syntax has slightly changed: "CmdCount,Arg", eg, "y3,a"
tcalc.vba.gz 0.1 2007-10-07 7.0 Tom Link Initial upload
ip used for rating: 142.132.191.50

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Vim at Github