Charles.Forsyth20070601-14568bd193319 years agoHistory
272 lines · 4.9 KB · Text
RawDownload
.TH CALC 1.SH NAMEcalc \- calculator language.SH SYNOPSIS.B calc[.B -s] [.I file].PP.B calc[.B -s] [.I expression].SH DESCRIPTION.I Calcinterprets a simple language for floating-point arithmeticwith Limbo-like syntax andfunctions..PPIf no.I fileor.I expressionis given.I calcinterprets the standard input..PP.I Calcinput consists of.I expressionsand.IR statements .Expressions are evaluated and their results printed.Statements, typically assignments and functiondefinitions, produce no output unless they explicitly call.IR print ..PPComments begin with # and extend to the end of the line as in Limbo..PPNumbers may have a base specified using C or Limbo syntax..PPVariable names have the usual syntax, including .LR _ .They may be introduced without a declaration and have an initial default valueof 0.0..PPThe predefined variable.B degreescan be set to specify angles in degrees rather than radians in the trigonometric functions below. It is initially 0 (angles in radians by default)..PPThe predefined variable.B printbasecan be set to any integer between 2 and 36 inclusive to specifythe base of all values output..PPThe constants.BR e ,.BR Pi (π) ,.BR Phi (φ) ,.BR Gamma (γ) ,.BR Infinity (∞) ,and.BR Nan (NaN)are predefined..PPExpressions are formed with these Limbo-like operators, listed bydecreasing precedence..TP.B ! ~ + - ++ --.TP.B **.TP.B * / % //.TP.B + -.TP.B << >>.TP.B > >= < <= <->.TP.B == != -> <-.TP.BR & " " " " ↑.TP.B ^.TP.BR | " " " " ↓.TP.B &&.TP.B ||.TP.B ? :.TP.B = := += -= *= /= %= //= &= ^= |= <<= >>=.TP.B ,.PPIf the.B -sflag is given, a strict interpretation of the declaration rules are enforced - all variables must be declared and initialized first using the .B :=operator. Otherwise undeclared variables are declared and initialized to 0.0 in thecurrent scope. In either case, .B :=always forces a new declaration..PPThe extra non-Limbo operators are factorial (! when postfix), integer division (//),conditional (? and :) comma (,), logical equivalence (<->), implication (->), reverse implication (<-), nand (↑) and nor (↓)..PPUnary operators, assignment operators, **, ? and : are right associative as usual..PPThe comma operator may be replaced by white space in expressions..PPBuilt in functions are.BR abs ,.BR acos ,.BR acosh ,.BR asin ,.BR asinh ,.BR atan ,.BR atanh ,.BR atan2 ,.BR cbrt ,.BR ceiling ,.BR cos ,.BR cosh ,.BR erf ,.BR exp ,.BR floor ,.BR frac ,.BR gamma (Γ) ,.BR int ,.BR log ,.BR log10 ,.BR max ,.BR min ,.BR pow ,.BR rand ,.BR round ,.BR sign ,.BR sin ,.BR sinh ,.BR sqrt ,.BR tan ,and.BR tanh ..PPFunctions of one argument may be written without brackets:.sp.EX	sin 45	sqrt 2.EE.spThese behave as unary operators with the highest precedence..PPSum and product operators are available using sigma (Σ) and pi (Π).For example.sp.EX	sigma(i = 0, 100, 1/i!).EE.spgives the value 2.7182818284590455 ..PPSimple definite integration can be done:.sp.EX	integral(x = -1.96, 1.96, exp(-0.5*x*x)/sqrt(2*Pi)).EE.spoutputs 0.9500042096998785 .∫ may be used in place of integral..PPFor the sake of completeness, the derivative of a function at a givenpoint can be calculated:.sp.EX	differential(x=1, x*x+5*x+6).EE.spgives 7.Δ may be used in place of differential..PPThere is limited support for the solution of equations.For example.sp.EX	solve(x**2-5*x+6==0).EE.spoutputs the values 2 and 3. The value returned by.B solveis the largest of the roots. To specify the variable to solve for, ifambiguous, simply add it as a second parameter as in, for example,.sp.EX	solve(x**2-5*x+6==y**3+z, x).EE.spThis will substitute the current values of.B yand.B zand solve for.B x.To tune the solution process, the predefined variables.B solvelimit(default value 100) and.B solvestep(default value 1) are available.The former specifies the maximum absolute solutionto search for. The latterspecifies the interval increment to apply when searchingfor sign changes..PP.B Printprints a list of expressions that may includestring constants such as\fL"hello\en"\f1.\fP.PP.B Readreads in a list of values interactively. The list of variables to assignthese values should follow..PPOther files may be read in using the Limbo include statement..PPControl flow statements are.BR break ,.BR continue ,.BR exit ,.BR return ,.BR if - else ,.BR while ,.BR do - while ,and.BR for ,with braces for grouping..PPThe use of semi-colon and newline is optional..PPFunctions are introduced by the keyword.BR fn ..SH EXAMPLE.EXfn ack(a, b){	n = n+1	if(a == 0)		return b+1;	if(b == 0)		return ack(a-1, 1);	return ack(a-1, ack(a, b-1));} for(i = 0; i < 4; i++)	for(j = 0; j < 4; j++){		n = 0		print "ack(", i, ",", j, ")=", ack(i, j), "\n"		print n, " calls", "\n"	}.EE.SH SOURCE.B /appl/cmd/calc.b.SH "SEE ALSO".IR fc (1),.IR math (2)