"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "src/common/parser.y" between
wcalc-2.4.1.tar.gz and wcalc-2.5.tar.gz

About: Wcalc is a natural-expression command-line calculator.

parser.y  (wcalc-2.4.1):parser.y  (wcalc-2.5)
skipping to change at line 30 skipping to change at line 30
#include <stdlib.h> #include <stdlib.h>
#include "number.h" #include "number.h"
#include <stdarg.h> #include <stdarg.h>
#include <math.h> #include <math.h>
#include <float.h> #include <float.h>
#include <unistd.h> /* for isatty() */ #include <unistd.h> /* for isatty() */
#include "calculator.h" #include "calculator.h"
#include "variables.h" #include "variables.h"
#include "help.h"
#include "files.h" #include "files.h"
#include "conversion.h" #include "conversion.h"
#include "number_formatting.h" #include "number_formatting.h"
#include "output.h"
/* Based on the headstart code by Shawn Ostermann /* Based on the headstart code by Shawn Ostermann
* modified by Kyle Wheeler * modified by Kyle Wheeler
*/ */
/* include debugging code, in case we want it */ /* include debugging code, in case we want it */
#define YYDEBUG 1 #define YYDEBUG 1
/* to make it stop complaining about undeclared functions */ /* to make it stop complaining about undeclared functions */
int yylex(void); int yylex(void);
skipping to change at line 59 skipping to change at line 59
char * errstring = NULL; char * errstring = NULL;
int errloc = -1; int errloc = -1;
int show_line_numbers = 0; int show_line_numbers = 0;
%} %}
%union { /* the types that we use in the tokens */ %union { /* the types that we use in the tokens */
enum functions function; enum functions function;
enum operations operation; enum operations operation;
Number number; Number number;
int integer; long integer;
enum commands cmd; enum commands cmd;
char * variable; char * variable;
char character; char character;
struct conv_req conver; struct conv_req conver;
} }
%token DEC_CMD OCT_CMD HEX_CMD BIN_CMD GUARD_CMD DISPLAY_PREFS_CMD %token DEC_CMD OCT_CMD HEX_CMD BIN_CMD DISPLAY_PREFS_CMD
%token RADIAN_CMD REMEMBER_CMD LISTVAR_CMD STORE_CMD CMOD_CMD %token RADIAN_CMD REMEMBER_CMD LISTVAR_CMD STORE_CMD CMOD_CMD
%token PRINT_HELP_CMD PREFIX_CMD INT_CMD VERBOSE_CMD DELIM_CMD %token PRINT_HELP_CMD PREFIX_CMD INT_CMD VERBOSE_CMD DELIM_CMD
%token <variable> ASSERT_CMD
%token <integer> ENG_CMD HLIMIT_CMD ROUNDING_INDICATION_CMD %token <integer> ENG_CMD HLIMIT_CMD ROUNDING_INDICATION_CMD
%token <integer> PRECISION_CMD BITS_CMD BASE_CMD %token <integer> PRECISION_CMD BITS_CMD BASE_CMD GUARD_CMD
%token <conver> CONVERT_CMD %token <conver> CONVERT_CMD
%token EOLN OPEN_PARENTHESES CLOSE_PARENTHESES OPEN_BRACE %token EOLN OPEN_PARENTHESES CLOSE_PARENTHESES OPEN_BRACE
%token CLOSE_BRACE OPEN_BRACKET CLOSE_BRACKET %token CLOSE_BRACE OPEN_BRACKET CLOSE_BRACKET
%token WPLUS WMINUS WMULT WDIV WMOD EQUALS_SIGN WPOW WEXP WSQR %token WPLUS WMINUS WMULT WDIV WMOD EQUALS_SIGN WPOW WEXP WSQR
%token WOR WAND WEQUAL WNEQUAL WGT WLT WGEQ WLEQ %token WOR WAND WEQUAL WNEQUAL WGT WLT WGEQ WLEQ
%token WLSHFT WRSHFT WBOR WBAND WBXOR %token WLSHFT WRSHFT WBOR WBAND WBXOR
%token WBNOT WNOT WLOG WLN WROUND WABS WSQRT WCEIL WFLOOR WCBRT WLOGTWO WBANG %token WBNOT WNOT WLOG WLN WROUND WABS WSQRT WCEIL WFLOOR WCBRT WLOGTWO WBANG
%token WSIN WCOS WTAN WASIN WACOS WATAN WSINH WCOSH WTANH WASINH WACOSH WATANH %token WSIN WCOS WTAN WASIN WACOS WATAN WSINH WCOSH WTANH WASINH WACOSH WATANH
skipping to change at line 187 skipping to change at line 188
} }
/* if we got an error on the line */ /* if we got an error on the line */
; ;
eoln : EOLN { ++lines; } eoln : EOLN { ++lines; }
; ;
command : HEX_CMD { command : HEX_CMD {
$$ = isatty(0)?redisplay:nothing; $$ = isatty(0)?redisplay:nothing;
conf.output_format = HEXADECIMAL_FORMAT; conf.output_format = HEXADECIMAL_FORMAT;
if (standard_output) display_output_format(HEXADECIMAL_FORMAT); }
printf("Hexadecimal Formatted Output\n");}
| OCT_CMD { | OCT_CMD {
$$ = isatty(0)?redisplay:nothing; $$ = isatty(0)?redisplay:nothing;
conf.output_format = OCTAL_FORMAT; conf.output_format = OCTAL_FORMAT;
if (standard_output) display_output_format(OCTAL_FORMAT); }
printf("Octal Formatted Output\n");}
| BIN_CMD { | BIN_CMD {
$$ = isatty(0)?redisplay:nothing; $$ = isatty(0)?redisplay:nothing;
conf.output_format = BINARY_FORMAT; conf.output_format = BINARY_FORMAT;
if (standard_output) display_output_format(BINARY_FORMAT); }
printf("Binary Formatted Output\n");}
| DEC_CMD { | DEC_CMD {
$$ = isatty(0)?redisplay:nothing; $$ = isatty(0)?redisplay:nothing;
conf.output_format = DECIMAL_FORMAT; conf.output_format = DECIMAL_FORMAT;
if (standard_output) display_output_format(DECIMAL_FORMAT); }
printf("Decimal Formatted Output\n");} | ASSERT_CMD {
if (strcmp($1, pretty_answer)) {
fprintf(stderr, "Assertion on line %u:\n", lines);
fprintf(stderr, " Pretty Answer is: '%s'\n", pretty_answer);
fprintf(stderr, "...should have been: '%s'\n", $1);
exit(EXIT_FAILURE);
}
free($1);
}
| DSEP_CMD { | DSEP_CMD {
$$ = nothing; $$ = nothing;
if (conf.thou_delimiter != $1) { if (conf.thou_delimiter != $1) {
conf.dec_delimiter = $1; conf.dec_delimiter = $1;
if (standard_output) display_status("%c is now the decimal separator.", $1);
printf("%c is now the decimal separator.\n", $1); } else {
} else if (standard_output) { extern int column;
printf("%c cannot be the decimal separator. It is the thousands s column --;
eparator.\n", $1); report_error("%c cannot be the decimal separator. It is the thousands
separator.", $1);
}} }}
| IDSEP_CMD { | IDSEP_CMD {
$$ = nothing; $$ = nothing;
if (conf.in_thou_delimiter != $1 && (conf.in_thou_delimiter != 0 || conf. thou_delimiter != $1)) { if (conf.in_thou_delimiter != $1 && (conf.in_thou_delimiter != 0 || conf. thou_delimiter != $1)) {
conf.in_dec_delimiter = $1; conf.in_dec_delimiter = $1;
if (standard_output) display_status("%c is now the decimal separator for input.\n", $1);
printf("%c is now the decimal separator for input.\n", $1 } else {
); extern int column;
} else if (standard_output) { column --;
printf("%c cannot be the decimal separator. It is the thousands s report_error("%c cannot be the decimal separator. It is the thousands
eparator.\n", $1); separator.\n", $1);
}} }}
| TSEP_CMD { | TSEP_CMD {
$$ = nothing; $$ = nothing;
if (conf.dec_delimiter != $1) { if (conf.dec_delimiter != $1) {
conf.thou_delimiter = $1; conf.thou_delimiter = $1;
if (standard_output) display_status("%c is now the thousands separator.\n", $1);
printf("%c is now the thousands separator.\n", $1);
} else if (standard_output) { } else if (standard_output) {
printf("%c cannot be the thousands separator. It is the decimal s extern int column;
eparator.\n", $1); column --;
report_error("%c cannot be the thousands separator. It is the decimal
separator.\n", $1);
}} }}
| ITSEP_CMD { | ITSEP_CMD {
$$ = nothing; $$ = nothing;
if (conf.in_dec_delimiter != $1 && (conf.in_dec_delimiter != 0 || conf.de c_delimiter != $1)) { if (conf.in_dec_delimiter != $1 && (conf.in_dec_delimiter != 0 || conf.de c_delimiter != $1)) {
conf.in_thou_delimiter = $1; conf.in_thou_delimiter = $1;
if (standard_output) display_status("%c is now the thousands separator for input.\n", $1);
printf("%c is now the thousands separator for input.\n", } else {
$1); extern int column;
} else if (standard_output) { column --;
printf("%c cannot be the thousands separator. It is the decimal s report_error("%c cannot be the thousands separator. It is the decimal
eparator.\n", $1); separator.\n", $1);
}} }
}
| DELIM_CMD { | DELIM_CMD {
$$ = nothing; $$ = nothing;
conf.print_commas = ! conf.print_commas; conf.print_commas = ! conf.print_commas;
if (standard_output) { display_status("Will %sshow separators when printing large numbers.\n",co
printf("Will %sshow separators when printing large numbers.\n",co nf.print_commas?"":"not ");
nf.print_commas?"":"not ");
}
} }
| INT_CMD { | INT_CMD {
$$ = nothing; $$ = nothing;
conf.print_ints = ! conf.print_ints; conf.print_ints = ! conf.print_ints;
if (standard_output) { display_status("Will %suse abbreviations for large integers.\n",conf.prin
printf("Will %suse abbreviations for large integers.\n",conf.prin t_ints?"not ":"");
t_ints?"not ":"");
}
} }
| VERBOSE_CMD | VERBOSE_CMD
{ {
$$ = nothing; $$ = nothing;
conf.verbose = ! conf.verbose; conf.verbose = ! conf.verbose;
if (standard_output) { display_status("Will %secho the lines to be evaluated.\n",conf.verbose?""
printf("Will %secho the lines to be evaluated.\n",conf.verbose?"" :"not ");
:"not ");
}
} }
| DISPLAY_PREFS_CMD { | DISPLAY_PREFS_CMD {
if (standard_output) { display_prefs();
printf(" Display Precision: %-3i %s -> \\p
\n",conf.precision,((conf.precision==-1)?"(auto)":" "));
printf(" Internal Precision: %-24lu -> \\bits\n", (unsign
ed long) num_get_default_prec());
printf(" Engineering Output: %s -> \\e\
n",
(conf.engineering==always)?"always":(conf.enginee
ring==never)?"never ":"auto ");
printf(" Output Format: %s -> \\b, \\d, \\h, \\o\n",
output_string(conf.output_format));
printf(" Use Radians: %s -> \
\r or \\radians\n",conf.use_radians?"yes":"no ");
printf(" Print Prefixes: %s -> \
\pre or \\prefixes\n",conf.print_prefixes?"yes":"no ");
printf("Avoid Abbreviating Integers: %s -> \
\ints\n",conf.print_ints?"yes":"no ");
printf(" Rounding Indication: %s -> \\round\n",
conf.rounding_indication==SIMPLE_ROUNDING_INDICATION?"yes (simple) ":(conf.round
ing_indication==SIG_FIG_ROUNDING_INDICATION?"yes (sig_fig)":"no "));
printf(" Save Errors in History: %s -> \
\re\n",conf.remember_errors?"yes":"no ");
printf(" Thousands Delimiter: '%c' ->
\\tsep\n",conf.thou_delimiter);
printf(" Decimal Delimiter: '%c' ->
\\dsep\n",conf.dec_delimiter);
printf(" Precision Guard: %s -> \
\cons\n",conf.precision_guard?"yes":"no ");
printf(" History Limit: %s -> \
\hlimit\n",conf.history_limit?"yes":"no ");
if (conf.history_limit) {
printf(" History Limited To: %lu\n",conf.history_
limit_len);
}
printf(" Verbose: %s -> \
\verbose\n",conf.verbose?"yes":"no ");
printf(" Display Delimiters: %s -> \
\delim\n",conf.print_commas?"yes":"no ");
printf(" Modulo Operator: %s -> \\cmod\n"
,conf.c_style_mod?"C-style ":"not C-style");
}
$$ = nothing; $$ = nothing;
} }
| RADIAN_CMD { | RADIAN_CMD {
$$ = nothing; $$ = nothing;
conf.use_radians = ! conf.use_radians; conf.use_radians = ! conf.use_radians;
if (standard_output) display_status("Now Using %s\n", conf.use_radians?"Radians":"Degrees");}
printf("Now Using %s\n", conf.use_radians?"Radians":"Degrees");}
| GUARD_CMD { | GUARD_CMD {
$$ = nothing; $$ = nothing;
conf.precision_guard = ! conf.precision_guard; switch ($1) {
if (standard_output) case 0: conf.precision_guard = 0; break;
printf("Now %sUsing Conservative Precision\n", conf.precision_gua case 1: conf.precision_guard = 1; break;
rd?"":"Not ");} case -1: conf.precision_guard = ! conf.precision_guard; break;
}
display_status("Now %sUsing Conservative Precision\n", conf.precision_gua
rd?"":"Not ");}
| PRECISION_CMD { | PRECISION_CMD {
$$ = isatty(0)?redisplay:nothing; $$ = isatty(0)?redisplay:nothing;
conf.precision = $1; conf.precision = $1;
if (standard_output) { if (conf.precision == -1) {
printf("Precision = "); display_status("Precision = auto");
if (conf.precision == -1) printf("auto\n"); } else {
else printf("%i\n", conf.precision); display_status("Precision = %i", conf.precision);
}} } }
| HLIMIT_CMD { | HLIMIT_CMD {
$$ = nothing; $$ = nothing;
if ($1) { if ($1) {
conf.history_limit = 1; conf.history_limit = 1;
conf.history_limit_len = $1; conf.history_limit_len = $1;
} else { } else {
conf.history_limit = 0; conf.history_limit = 0;
conf.history_limit_len = 0; conf.history_limit_len = 0;
} }
} }
skipping to change at line 333 skipping to change at line 319
case never: conf.engineering = automatic; break; case never: conf.engineering = automatic; break;
case automatic: conf.engineering = always; break; case automatic: conf.engineering = always; break;
} }
} else { } else {
switch($1) { switch($1) {
case 1: conf.engineering = automatic; break; case 1: conf.engineering = automatic; break;
case 2: conf.engineering = always; break; case 2: conf.engineering = always; break;
case 3: conf.engineering = never; break; case 3: conf.engineering = never; break;
} }
} }
if (standard_output) display_status("Engineering notation is %s\n",(conf.engineering==always)?
printf("Engineering notation is %s\n",(conf.engineering==always)? "always used":(conf.engineering==never)?"never used":"used if convenient");
"always used":(conf.engineering==never)?"never used":"used if convenient");
$$ = isatty(0)?redisplay:nothing; $$ = isatty(0)?redisplay:nothing;
} }
| ROUNDING_INDICATION_CMD { | ROUNDING_INDICATION_CMD {
$$ = nothing; $$ = nothing;
if ($1 != -1) if ($1 != -1)
conf.rounding_indication = $1; conf.rounding_indication = $1;
else { else {
conf.rounding_indication += 1; conf.rounding_indication += 1;
conf.rounding_indication %= 3; conf.rounding_indication %= 3;
} }
if (standard_output) { display_status("Will display %s rounding indication",
printf("Will display "); (conf.rounding_indication==NO_ROUNDING_INDICATION)?"no":
switch(conf.rounding_indication) { ((conf.rounding_indication==SIMPLE_ROUNDING_INDICATION)?"simple"
case NO_ROUNDING_INDICATION: printf("no"); break; :"significant figure"));
case SIMPLE_ROUNDING_INDICATION: printf("simple"); break;
case SIG_FIG_ROUNDING_INDICATION: printf("significant fig
ure"); break;
}
printf(" rounding indication\n");
}
} }
| PREFIX_CMD { | PREFIX_CMD {
$$ = nothing; $$ = nothing;
conf.print_prefixes = ! conf.print_prefixes; conf.print_prefixes = ! conf.print_prefixes;
if (standard_output) display_status("Will %sprint number prefixes\n",conf.print_prefixes?"":"n
printf("Will %sprint number prefixes\n",conf.print_prefixes?"":"n ot ");
ot ");
} }
| REMEMBER_CMD { | REMEMBER_CMD {
$$ = nothing; $$ = nothing;
conf.remember_errors = ! conf.remember_errors; conf.remember_errors = ! conf.remember_errors;
if (standard_output) display_status("Statements that produce errors are %s.\n",conf.remember_e
printf("Statements that produce errors are %s.\n",conf.remember_e rrors?"recorded":"forgotten");
rrors?"recorded":"forgotten");
} }
| PRINT_HELP_CMD { | PRINT_HELP_CMD {
$$ = nothing; $$ = nothing;
if (standard_output) { display_interactive_help();
print_interactive_help();
}
} }
| OPEN_CMD { | OPEN_CMD {
extern char* open_file; extern char* open_file;
int i; int i;
unsigned int len = strlen($1)+1; unsigned int len = strlen($1)+1;
open_file = malloc(len); open_file = malloc(len);
snprintf(open_file,len,"%s",$1); strncpy(open_file, $1, len);
/* strip trailing spaces */ /* strip trailing spaces */
for (i=strlen(open_file)-1;i>=0;i--) { for (i=strlen(open_file)-1;i>=0;i--) {
if (open_file[i] != ' ') break; if (open_file[i] != ' ') break;
open_file[i] = 0; open_file[i] = 0;
} }
if (strlen(open_file) == 0) { if (strlen(open_file) == 0) {
free(open_file); free(open_file);
open_file = NULL; open_file = NULL;
report_error("Please specify a file name to open."); report_error("Please specify a file name to open.");
} }
skipping to change at line 437 skipping to change at line 412
} }
free($1.u1); free($1.u1);
free($1.u2); free($1.u2);
$$ = redisplay; $$ = redisplay;
} }
| BASE_CMD | BASE_CMD
{ {
if ($1 >= 2 && $1 <= 36) { if ($1 >= 2 && $1 <= 36) {
char * str, junk; char * str, junk;
str = num_to_str_complex(last_answer, $1, conf.engineering, -1, c onf.print_prefixes, &junk); str = num_to_str_complex(last_answer, $1, conf.engineering, -1, c onf.print_prefixes, &junk);
printf("base %i: %s\n",$1,str); display_status("base %i: %s\n",$1,str);
} else { } else {
report_error("Base must be greater than one and less than 37."); report_error("Base must be greater than one and less than 37.");
} }
$$ = nothing; $$ = nothing;
} }
| STORE_CMD VARIABLE | STORE_CMD VARIABLE
{ {
int retval = storeVar($2); int retval = storeVar($2);
if (retval == 0) { if (retval == 0) {
printf("successfully stored %s\n",$2); display_status("successfully stored %s\n",$2);
} else { } else {
report_error("Failure to store variable!"); report_error("Failure to store variable!");
} }
free($2); free($2);
} }
| CMOD_CMD | CMOD_CMD
{ {
$$ = nothing; $$ = nothing;
conf.c_style_mod = ! conf.c_style_mod; conf.c_style_mod = ! conf.c_style_mod;
if (standard_output) display_status("The mod (%%) operation will %sbehave like it does in the
printf("The mod (%%) operation will %sbehave like it does in the C programming language.\n",conf.c_style_mod?"":"not ");
C programming language.\n",conf.c_style_mod?"":"not ");
} }
; ;
optionalstring : STRING { optionalstring : STRING {
$$ = $1; $$ = $1;
} }
| { | {
$$ = NULL; $$ = NULL;
} }
; ;
assignment : ASSIGNMENT exp optionalstring assignment : ASSIGNMENT exp optionalstring
{ {
if (compute && ! scanerror) { if (compute && ! scanerror) {
/* if standard_error, q is reserved */ /* if standard_error, q is reserved */
if (standard_output && !strcmp($1,"q")) { if (standard_output && !strcmp($1,"q")) {
report_error("q cannot be assigned a value. q is used to exit."); report_error("q cannot be assigned a value. q is used to exit.");
} else { } else {
if (putval($1,$2,$3) == 0) { if (putval($1,$2,$3) == 0) {
if (standard_output) { display_val($1);
Number val;
num_init(val);
printf("%s", $1);
getvarval(val, $1);
print_this_result(val);
num_free(val);
}
} else { } else {
report_error("There was a problem assigning the v alue."); report_error("There was a problem assigning the v alue.");
} }
} }
num_free($2); num_free($2);
} else { } else {
scanerror = 0; scanerror = 0;
report_error("Scanner error halts parser."); report_error("Scanner error halts parser.");
} }
free($1); free($1);
skipping to change at line 507 skipping to change at line 474
free($3); free($3);
} }
} }
| ASSIGNMENT STRING optionalstring | ASSIGNMENT STRING optionalstring
{ {
if (compute && ! scanerror) { if (compute && ! scanerror) {
if (standard_output && !strcmp($1,"q")) { if (standard_output && !strcmp($1,"q")) {
report_error("q cannot be assigned an expression. q is us ed to exit."); report_error("q cannot be assigned an expression. q is us ed to exit.");
} else { } else {
if (putexp($1,$2,$3) == 0) { if (putexp($1,$2,$3) == 0) {
if (standard_output) { display_val($1);
printf("%s = %s\n", $1, getvar_full($1).e
xp);
}
} else { } else {
report_error("There was a problem assigning the e xpression."); report_error("There was a problem assigning the e xpression.");
} }
} }
} else { } else {
scanerror = 0; scanerror = 0;
report_error("Scanner error halts parser."); report_error("Scanner error halts parser.");
} }
free($1); free($1);
free($2); free($2);
 End of changes. 33 change blocks. 
142 lines changed or deleted 85 lines changed or added

Home  |  About  |  All  |  Newest  |  Fossies Dox  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTPS