Skip to content
kensmithPublic

About

stack based math

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

c

A stack-based calculator REPL, ported from Mancalc to Go with readline and expr support.

go install github.com/kensmith/c@latest
c

Enter each value, expression, or RPN operator on its own line. The prompt shows the stack from bottom to top; the rightmost value is the top.

Stack operations

Numeric operators consume their operands and push their results. Unary operators replace the top value; binary operators replace the top two values, preserving any values below them. Multi-result operators such as frexp consume one operand and put their first result on top.

Operators Stack effect
sum, avg, min, max, var, sd Replace the entire stack with one result.
isinf, isninf, isnan, signbit Replace the top value with 1 or 0; preserve deeper values.

All six reducers return 0 for an empty stack. var and sd calculate sample variance and standard deviation, and return 0 for a singleton stack. The four predicates require an operand; underflow leaves the empty stack unchanged. isinf tests positive infinity, isninf tests negative infinity, and signbit also recognizes negative zero.

For example, enter these four lines on an empty stack:

1
2
3
sum

The resulting stack is [ 6 ].

Constants such as pi and the random generator r push a value without consuming anything. rn consumes its upper bound. Stack commands perform their named actions: pop removes the top value, swap exchanges the top two, sort reorders the stack, and clear empties it. f prints the stack and noop leaves it alone.

Expressions

Expressions read a snapshot of the stack: s[0] is the top value, s[1] is the next value down, and so on. Evaluating an expression pushes its numeric result without consuming existing values. For example, starting with an empty stack:

2
3
s[0] + s[1]

The resulting stack is [ 2 3 5 ]. Likewise, sum(s) preserves its inputs, while the standalone RPN command sum consumes them.

See the quality review and follow-up plan for remaining correctness and maintenance work.

About

stack based math

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages