EVAL-powered CLI calculator
Nov 1, 2019 7:26:18 GMT 1
Post by felixp7 on Nov 1, 2019 7:26:18 GMT 1
After upgrading to the latest BaCon, I wanted to play with EVAL, because it looks like a fun toy. But my first attempt had to go in another direction, so what else to do? Just go back to the command line, of course! This ended up taking all day yesterday, because ideas kept bubbling up. It's pretty much complete now though:
It's also kinda useless, of course (expression calculators are a dime a dozen), but does illustrate how to perform a few common tasks in BaCon:
and of course the use of EVAL itself. Hope this helps!
OPTION COMPARE 1
OPTION ERROR 0
GLOBAL quiet = FALSE
GLOBAL use_color = FALSE
GLOBAL interactive = FALSE
GLOBAL cli_expr$ = ""
GLOBAL log_file$ = ""
GLOBAL x TYPE FLOATING
GLOBAL cmd$, prev$ = ""
SUB write_log(expr$, FLOATING result)
LOCAL output$
IF log_file$ = "" THEN EXIT SUB
IF NOT(quiet) THEN
output$ = "> " & expr$ & "\n"
APPEND output$ TO log_file$
END IF
output$ = STR$(result) & "\n"
APPEND output$ TO log_file$
END SUB
SUB print_out(expr$, FLOATING result)
COLOR INTENSE
IF ERROR > 0 THEN
IF use_color THEN COLOR FG TO RED
PRINT " ", ERR$(ERROR)
ELSE
IF use_color THEN COLOR FG TO MAGENTA
PRINT " = ", x
write_log(expr$, result)
END IF
END SUB
REPEAT
opt = CMDLINE("hvqcie:f:")
IF opt = ASC("h") THEN
PRINT "Usage: bacal";
PRINT " -h | -v | [-q] [-c] [-i] [-e <expr>] [-f <log file>]"
PRINT " -h Show this help message and exit"
PRINT " -v Show version information and exit"
PRINT " -q Quiet mode: suppress banner and command echoing"
PRINT " -c Enable output coloring"
PRINT " -e Evaluate given expression and exit"
PRINT " -i Stay interactive when -e is given"
PRINT " -f Log session to the given file"
END
ELIF opt = ASC("v") THEN
PRINT "This is Bacal, the BaCon Calculator, v1.0"
PRINT "No Time To Play, 2019"
PRINT "Open source under the MIT License"
END
ELIF opt = ASC("q") THEN
quiet = TRUE
ELIF opt = ASC("c") THEN
use_color = TRUE
ELIF opt = ASC("i") THEN
interactive = TRUE
ELIF opt = ASC("e") THEN
cli_expr$ = ARGUMENT$
ELIF opt = ASC("f") THEN
log_file$ = ARGUMENT$
END IF
UNTIL opt = -1
IF cli_expr$ <> "" THEN
x = EVAL(cli_expr$)
print_out(cli_expr$, x)
prev$ = cli_expr$
IF NOT(interactive) THEN END
PRINT
END IF
IF NOT(quiet) THEN
COLOR INTENSE
IF use_color THEN COLOR FG TO MAGENTA
PRINT "Welcome to Bacal, the BaCon Calculator."
PRINT "Enter ? or 'h' or 'help' for a few usage tips."
PRINT "Enter 'bye' or 'quit' to leave the program."
PRINT "Enter an empty line to repeat the last expression."
PRINT "'x' always contains the most recent result."
PRINT
END IF
REPEAT
COLOR RESET
INPUT "> ", cmd$
cmd$ = CHOP$(cmd$)
IF cmd$ = "bye" OR cmd$ = "quit" THEN
CONTINUE
ELIF cmd$ = "?" OR cmd$ = "h" OR cmd$ = "help" THEN
PRINT "Enter ordinary arithmetic expressions."
PRINT "Supported operators: + - * / % ^"
PRINT "Supported functions: abs, atan2, ceil, cos, exp,"
PRINT " floor, ln (log), log (log10), log10, pow, sin, sqrt"
PRINT "and more. You can also use the constants pi and e."
ELIF cmd$ = "" THEN
IF prev$ <> "" THEN
IF NOT(quiet) THEN PRINT "> ", prev$
x = EVAL(prev$)
print_out(prev$, x)
END IF
ELSE
x = EVAL(cmd$)
print_out(cmd$, x)
prev$ = cmd$
END IF
UNTIL cmd$ = "bye" OR cmd$ = "quit"
COLOR RESET
It's also kinda useless, of course (expression calculators are a dime a dozen), but does illustrate how to perform a few common tasks in BaCon:
- parsing command-line options;
- error handling;
- logging to a file;
and of course the use of EVAL itself. Hope this helps!