Atom

Command-line calculator

Save as calculator.atom and run atom run calculator.atom -- 12 '*' 7. Requires os.

Read the three arguments

The shell passes 12, *, and 7 as separate arguments. Convert the two operands to numbers before choosing an operation. The example command prints 84.

Excerpt
try:
    left = convert(f64, os.args[0])
    op = os.args[1]
    right = convert(f64, os.args[2])
Show full source 25 lines
calculator.atom
use os

if count(os.args) != 3:
    print('Usage: calculator NUMBER OPERATOR NUMBER')
    os.exit(2)

try:
    left = convert(f64, os.args[0])
    op = os.args[1]
    right = convert(f64, os.args[2])
    if op == '+':
        print(left + right)
    elif op == '-':
        print(left - right)
    elif op == '*':
        print(left * right)
    elif op == '/':
        print(left / right)
    elif op == '**':
        print(left ** right)
    else:
        throw 'Unknown operator {op}'
catch problem:
    print(problem)
    os.exit(1)