aboutsummaryrefslogtreecommitdiff
path: root/miscutils/bc.c
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2018-12-26 19:24:15 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2018-12-26 19:24:15 +0100
commit1c69ec1597f0f3ddb8123e9122cd7737debd261c (patch)
treeb77fdde9a1ff1e31a1709feab133d978f3365316 /miscutils/bc.c
parent8a56e3643f83770ba92255ca5c576c20e645617f (diff)
downloadbusybox-w32-1c69ec1597f0f3ddb8123e9122cd7737debd261c.tar.gz
busybox-w32-1c69ec1597f0f3ddb8123e9122cd7737debd261c.tar.bz2
busybox-w32-1c69ec1597f0f3ddb8123e9122cd7737debd261c.zip
bc: reduce indentation, no code changes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to '')
-rw-r--r--miscutils/bc.c1647
1 files changed, 823 insertions, 824 deletions
diff --git a/miscutils/bc.c b/miscutils/bc.c
index 0e5f26ce4..5b79b2db1 100644
--- a/miscutils/bc.c
+++ b/miscutils/bc.c
@@ -3090,184 +3090,184 @@ static BC_STATUS zbc_lex_token(void)
3090 3090
3091 // This is the workhorse of the lexer. 3091 // This is the workhorse of the lexer.
3092 switch (c) { 3092 switch (c) {
3093// case '\0': // probably never reached 3093// case '\0': // probably never reached
3094// p->lex_inbuf--; 3094// p->lex_inbuf--;
3095// p->lex = XC_LEX_EOF; 3095// p->lex = XC_LEX_EOF;
3096// break; 3096// break;
3097 case '\n': 3097 case '\n':
3098 p->lex_line++; 3098 p->lex_line++;
3099 p->lex = XC_LEX_NLINE; 3099 p->lex = XC_LEX_NLINE;
3100 break; 3100 break;
3101 case '\t': 3101 case '\t':
3102 case '\v': 3102 case '\v':
3103 case '\f': 3103 case '\f':
3104 case '\r': 3104 case '\r':
3105 case ' ': 3105 case ' ':
3106 bc_lex_whitespace(); 3106 bc_lex_whitespace();
3107 break; 3107 break;
3108 case '!': 3108 case '!':
3109 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT); 3109 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
3110 if (p->lex == BC_LEX_OP_BOOL_NOT) { 3110 if (p->lex == BC_LEX_OP_BOOL_NOT) {
3111 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("!"); 3111 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
3112 if (s) RETURN_STATUS(s);
3113 }
3114 break;
3115 case '"':
3116 s = zbc_lex_string();
3117 break;
3118 case '#':
3119 s = zbc_POSIX_does_not_allow("'#' script comments");
3120 if (s) RETURN_STATUS(s); 3112 if (s) RETURN_STATUS(s);
3121 bc_lex_lineComment(); 3113 }
3122 break; 3114 break;
3123 case '%': 3115 case '"':
3124 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MODULUS, XC_LEX_OP_MODULUS); 3116 s = zbc_lex_string();
3125 break; 3117 break;
3126 case '&': 3118 case '#':
3127 c2 = *p->lex_inbuf; 3119 s = zbc_POSIX_does_not_allow("'#' script comments");
3128 if (c2 == '&') { 3120 if (s) RETURN_STATUS(s);
3129 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("&&"); 3121 bc_lex_lineComment();
3130 if (s) RETURN_STATUS(s); 3122 break;
3131 p->lex_inbuf++; 3123 case '%':
3132 p->lex = BC_LEX_OP_BOOL_AND; 3124 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MODULUS, XC_LEX_OP_MODULUS);
3133 } else { 3125 break;
3134 p->lex = XC_LEX_INVALID; 3126 case '&':
3135 s = bc_error_bad_character('&'); 3127 c2 = *p->lex_inbuf;
3136 } 3128 if (c2 == '&') {
3137 break; 3129 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
3138 case '(': 3130 if (s) RETURN_STATUS(s);
3139 case ')': 3131 p->lex_inbuf++;
3140 p->lex = (BcLexType)(c - '(' + BC_LEX_LPAREN); 3132 p->lex = BC_LEX_OP_BOOL_AND;
3141 break; 3133 } else {
3142 case '*': 3134 p->lex = XC_LEX_INVALID;
3143 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MULTIPLY, XC_LEX_OP_MULTIPLY); 3135 s = bc_error_bad_character('&');
3144 break; 3136 }
3145 case '+': 3137 break;
3146 c2 = *p->lex_inbuf; 3138 case '(':
3147 if (c2 == '+') { 3139 case ')':
3148 p->lex_inbuf++; 3140 p->lex = (BcLexType)(c - '(' + BC_LEX_LPAREN);
3149 p->lex = BC_LEX_OP_INC; 3141 break;
3150 } else 3142 case '*':
3151 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_PLUS, XC_LEX_OP_PLUS); 3143 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MULTIPLY, XC_LEX_OP_MULTIPLY);
3152 break; 3144 break;
3153 case ',': 3145 case '+':
3154 p->lex = BC_LEX_COMMA; 3146 c2 = *p->lex_inbuf;
3155 break; 3147 if (c2 == '+') {
3156 case '-': 3148 p->lex_inbuf++;
3157 c2 = *p->lex_inbuf; 3149 p->lex = BC_LEX_OP_INC;
3158 if (c2 == '-') { 3150 } else
3159 p->lex_inbuf++; 3151 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_PLUS, XC_LEX_OP_PLUS);
3160 p->lex = BC_LEX_OP_DEC; 3152 break;
3161 } else 3153 case ',':
3162 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MINUS, XC_LEX_OP_MINUS); 3154 p->lex = BC_LEX_COMMA;
3163 break; 3155 break;
3164 case '.': 3156 case '-':
3165 if (isdigit(*p->lex_inbuf)) 3157 c2 = *p->lex_inbuf;
3166 s = zbc_lex_number(c); 3158 if (c2 == '-') {
3167 else { 3159 p->lex_inbuf++;
3168 p->lex = BC_LEX_KEY_LAST; 3160 p->lex = BC_LEX_OP_DEC;
3169 s = zbc_POSIX_does_not_allow("'.' as 'last'"); 3161 } else
3170 } 3162 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MINUS, XC_LEX_OP_MINUS);
3171 break; 3163 break;
3172 case '/': 3164 case '.':
3173 c2 = *p->lex_inbuf; 3165 if (isdigit(*p->lex_inbuf))
3174 if (c2 == '*')
3175 s = zbc_lex_comment();
3176 else
3177 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_DIVIDE, XC_LEX_OP_DIVIDE);
3178 break;
3179 case '0':
3180 case '1':
3181 case '2':
3182 case '3':
3183 case '4':
3184 case '5':
3185 case '6':
3186 case '7':
3187 case '8':
3188 case '9':
3189 case 'A':
3190 case 'B':
3191 case 'C':
3192 case 'D':
3193 case 'E':
3194 case 'F':
3195 s = zbc_lex_number(c); 3166 s = zbc_lex_number(c);
3196 break; 3167 else {
3197 case ';': 3168 p->lex = BC_LEX_KEY_LAST;
3198 p->lex = BC_LEX_SCOLON; 3169 s = zbc_POSIX_does_not_allow("'.' as 'last'");
3199 break; 3170 }
3200 case '<': 3171 break;
3201 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_LE, XC_LEX_OP_REL_LT); 3172 case '/':
3202 break; 3173 c2 = *p->lex_inbuf;
3203 case '=': 3174 if (c2 == '*')
3204 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN); 3175 s = zbc_lex_comment();
3205 break; 3176 else
3206 case '>': 3177 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_DIVIDE, XC_LEX_OP_DIVIDE);
3207 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_GE, XC_LEX_OP_REL_GT); 3178 break;
3208 break; 3179 case '0':
3209 case '[': 3180 case '1':
3210 case ']': 3181 case '2':
3211 p->lex = (BcLexType)(c - '[' + BC_LEX_LBRACKET); 3182 case '3':
3212 break; 3183 case '4':
3213 case '\\': 3184 case '5':
3214 if (*p->lex_inbuf == '\n') { 3185 case '6':
3215 p->lex = XC_LEX_WHITESPACE; 3186 case '7':
3216 p->lex_inbuf++; 3187 case '8':
3217 } else 3188 case '9':
3218 s = bc_error_bad_character(c); 3189 case 'A':
3219 break; 3190 case 'B':
3220 case '^': 3191 case 'C':
3221 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_POWER, XC_LEX_OP_POWER); 3192 case 'D':
3222 break; 3193 case 'E':
3223 case 'a': 3194 case 'F':
3224 case 'b': 3195 s = zbc_lex_number(c);
3225 case 'c': 3196 break;
3226 case 'd': 3197 case ';':
3227 case 'e': 3198 p->lex = BC_LEX_SCOLON;
3228 case 'f': 3199 break;
3229 case 'g': 3200 case '<':
3230 case 'h': 3201 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_LE, XC_LEX_OP_REL_LT);
3231 case 'i': 3202 break;
3232 case 'j': 3203 case '=':
3233 case 'k': 3204 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
3234 case 'l': 3205 break;
3235 case 'm': 3206 case '>':
3236 case 'n': 3207 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_GE, XC_LEX_OP_REL_GT);
3237 case 'o': 3208 break;
3238 case 'p': 3209 case '[':
3239 case 'q': 3210 case ']':
3240 case 'r': 3211 p->lex = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
3241 case 's': 3212 break;
3242 case 't': 3213 case '\\':
3243 case 'u': 3214 if (*p->lex_inbuf == '\n') {
3244 case 'v': 3215 p->lex = XC_LEX_WHITESPACE;
3245 case 'w': 3216 p->lex_inbuf++;
3246 case 'x': 3217 } else
3247 case 'y': 3218 s = bc_error_bad_character(c);
3248 case 'z': 3219 break;
3249 s = zbc_lex_identifier(); 3220 case '^':
3250 break; 3221 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_POWER, XC_LEX_OP_POWER);
3251 case '{': 3222 break;
3252 case '}': 3223 case 'a':
3253 p->lex = (BcLexType)(c - '{' + BC_LEX_LBRACE); 3224 case 'b':
3254 break; 3225 case 'c':
3255 case '|': 3226 case 'd':
3256 c2 = *p->lex_inbuf; 3227 case 'e':
3257 if (c2 == '|') { 3228 case 'f':
3258 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("||"); 3229 case 'g':
3259 if (s) RETURN_STATUS(s); 3230 case 'h':
3260 p->lex_inbuf++; 3231 case 'i':
3261 p->lex = BC_LEX_OP_BOOL_OR; 3232 case 'j':
3262 } else { 3233 case 'k':
3263 p->lex = XC_LEX_INVALID; 3234 case 'l':
3264 s = bc_error_bad_character(c); 3235 case 'm':
3265 } 3236 case 'n':
3266 break; 3237 case 'o':
3267 default: 3238 case 'p':
3239 case 'q':
3240 case 'r':
3241 case 's':
3242 case 't':
3243 case 'u':
3244 case 'v':
3245 case 'w':
3246 case 'x':
3247 case 'y':
3248 case 'z':
3249 s = zbc_lex_identifier();
3250 break;
3251 case '{':
3252 case '}':
3253 p->lex = (BcLexType)(c - '{' + BC_LEX_LBRACE);
3254 break;
3255 case '|':
3256 c2 = *p->lex_inbuf;
3257 if (c2 == '|') {
3258 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
3259 if (s) RETURN_STATUS(s);
3260 p->lex_inbuf++;
3261 p->lex = BC_LEX_OP_BOOL_OR;
3262 } else {
3268 p->lex = XC_LEX_INVALID; 3263 p->lex = XC_LEX_INVALID;
3269 s = bc_error_bad_character(c); 3264 s = bc_error_bad_character(c);
3270 break; 3265 }
3266 break;
3267 default:
3268 p->lex = XC_LEX_INVALID;
3269 s = bc_error_bad_character(c);
3270 break;
3271 } 3271 }
3272 3272
3273 RETURN_STATUS(s); 3273 RETURN_STATUS(s);
@@ -3357,74 +3357,74 @@ static BC_STATUS zdc_lex_token(void)
3357 3357
3358 // This is the workhorse of the lexer. 3358 // This is the workhorse of the lexer.
3359 switch (c) { 3359 switch (c) {
3360// case '\0': // probably never reached 3360// case '\0': // probably never reached
3361// p->lex = XC_LEX_EOF; 3361// p->lex = XC_LEX_EOF;
3362// break; 3362// break;
3363 case '\n': 3363 case '\n':
3364 // '\n' is XC_LEX_NLINE, not XC_LEX_WHITESPACE 3364 // '\n' is XC_LEX_NLINE, not XC_LEX_WHITESPACE
3365 // (and "case '\n':" is not just empty here) 3365 // (and "case '\n':" is not just empty here)
3366 // only to allow interactive dc have a way to exit 3366 // only to allow interactive dc have a way to exit
3367 // "parse" stage of "parse,execute" loop 3367 // "parse" stage of "parse,execute" loop
3368 // on <enter>, not on _next_ token (which would mean 3368 // on <enter>, not on _next_ token (which would mean
3369 // commands are not executed on pressing <enter>). 3369 // commands are not executed on pressing <enter>).
3370 // IOW: typing "1p<enter>" should print "1" _at once_, 3370 // IOW: typing "1p<enter>" should print "1" _at once_,
3371 // not after some more input. 3371 // not after some more input.
3372 p->lex_line++; 3372 p->lex_line++;
3373 p->lex = XC_LEX_NLINE; 3373 p->lex = XC_LEX_NLINE;
3374 break; 3374 break;
3375 case '\t': 3375 case '\t':
3376 case '\v': 3376 case '\v':
3377 case '\f': 3377 case '\f':
3378 case '\r': 3378 case '\r':
3379 case ' ': 3379 case ' ':
3380 bc_lex_whitespace(); 3380 bc_lex_whitespace();
3381 break; 3381 break;
3382 case '!': 3382 case '!':
3383 c2 = *p->lex_inbuf; 3383 c2 = *p->lex_inbuf;
3384 if (c2 == '=') 3384 if (c2 == '=')
3385 p->lex = XC_LEX_OP_REL_NE; 3385 p->lex = XC_LEX_OP_REL_NE;
3386 else if (c2 == '<') 3386 else if (c2 == '<')
3387 p->lex = XC_LEX_OP_REL_LE; 3387 p->lex = XC_LEX_OP_REL_LE;
3388 else if (c2 == '>') 3388 else if (c2 == '>')
3389 p->lex = XC_LEX_OP_REL_GE; 3389 p->lex = XC_LEX_OP_REL_GE;
3390 else 3390 else
3391 RETURN_STATUS(bc_error_bad_character(c)); 3391 RETURN_STATUS(bc_error_bad_character(c));
3392 p->lex_inbuf++; 3392 p->lex_inbuf++;
3393 break; 3393 break;
3394 case '#': 3394 case '#':
3395 bc_lex_lineComment(); 3395 bc_lex_lineComment();
3396 break; 3396 break;
3397 case '.': 3397 case '.':
3398 if (isdigit(*p->lex_inbuf)) 3398 if (isdigit(*p->lex_inbuf))
3399 s = zbc_lex_number(c);
3400 else
3401 s = bc_error_bad_character(c);
3402 break;
3403 case '0':
3404 case '1':
3405 case '2':
3406 case '3':
3407 case '4':
3408 case '5':
3409 case '6':
3410 case '7':
3411 case '8':
3412 case '9':
3413 case 'A':
3414 case 'B':
3415 case 'C':
3416 case 'D':
3417 case 'E':
3418 case 'F':
3419 s = zbc_lex_number(c); 3399 s = zbc_lex_number(c);
3420 break; 3400 else
3421 case '[':
3422 s = zdc_lex_string();
3423 break;
3424 default:
3425 p->lex = XC_LEX_INVALID;
3426 s = bc_error_bad_character(c); 3401 s = bc_error_bad_character(c);
3427 break; 3402 break;
3403 case '0':
3404 case '1':
3405 case '2':
3406 case '3':
3407 case '4':
3408 case '5':
3409 case '6':
3410 case '7':
3411 case '8':
3412 case '9':
3413 case 'A':
3414 case 'B':
3415 case 'C':
3416 case 'D':
3417 case 'E':
3418 case 'F':
3419 s = zbc_lex_number(c);
3420 break;
3421 case '[':
3422 s = zdc_lex_string();
3423 break;
3424 default:
3425 p->lex = XC_LEX_INVALID;
3426 s = bc_error_bad_character(c);
3427 break;
3428 } 3428 }
3429 3429
3430 RETURN_STATUS(s); 3430 RETURN_STATUS(s);
@@ -3559,10 +3559,9 @@ static void bc_parse_reset(void)
3559 3559
3560static void bc_parse_free(void) 3560static void bc_parse_free(void)
3561{ 3561{
3562 IF_BC(BcParse *p = &G.prs;) 3562 IF_BC(bc_vec_free(&G.prs.exits);)
3563 IF_BC(bc_vec_free(&p->exits);) 3563 IF_BC(bc_vec_free(&G.prs.conds);)
3564 IF_BC(bc_vec_free(&p->conds);) 3564 IF_BC(bc_vec_free(&G.prs.ops);)
3565 IF_BC(bc_vec_free(&p->ops);)
3566 bc_vec_free(&G.prs.lex_strnumbuf); 3565 bc_vec_free(&G.prs.lex_strnumbuf);
3567} 3566}
3568 3567
@@ -3571,7 +3570,7 @@ static void bc_parse_create(size_t fidx)
3571 BcParse *p = &G.prs; 3570 BcParse *p = &G.prs;
3572 memset(p, 0, sizeof(BcParse)); 3571 memset(p, 0, sizeof(BcParse));
3573 3572
3574 bc_char_vec_init(&G.prs.lex_strnumbuf); 3573 bc_char_vec_init(&p->lex_strnumbuf);
3575 IF_BC(bc_vec_init(&p->exits, sizeof(size_t), NULL);) 3574 IF_BC(bc_vec_init(&p->exits, sizeof(size_t), NULL);)
3576 IF_BC(bc_vec_init(&p->conds, sizeof(size_t), NULL);) 3575 IF_BC(bc_vec_init(&p->conds, sizeof(size_t), NULL);)
3577 IF_BC(bc_vec_init(&p->ops, sizeof(BcLexType), NULL);) 3576 IF_BC(bc_vec_init(&p->ops, sizeof(BcLexType), NULL);)
@@ -3938,26 +3937,26 @@ static BC_STATUS zbc_parse_incdec(BcInst *prev, bool *paren_expr,
3938 *nexprs = *nexprs + 1; 3937 *nexprs = *nexprs + 1;
3939 3938
3940 switch (type) { 3939 switch (type) {
3941 case XC_LEX_NAME: 3940 case XC_LEX_NAME:
3942 s = zbc_parse_name(prev, flags | BC_PARSE_NOCALL); 3941 s = zbc_parse_name(prev, flags | BC_PARSE_NOCALL);
3943 break; 3942 break;
3944 case BC_LEX_KEY_IBASE: 3943 case BC_LEX_KEY_IBASE:
3945 case BC_LEX_KEY_LAST: 3944 case BC_LEX_KEY_LAST:
3946 case BC_LEX_KEY_OBASE: 3945 case BC_LEX_KEY_OBASE:
3947 bc_parse_push(type - BC_LEX_KEY_IBASE + XC_INST_IBASE); 3946 bc_parse_push(type - BC_LEX_KEY_IBASE + XC_INST_IBASE);
3948 s = zbc_lex_next(); 3947 s = zbc_lex_next();
3949 break; 3948 break;
3950 case BC_LEX_KEY_SCALE: 3949 case BC_LEX_KEY_SCALE:
3951 s = zbc_lex_next(); 3950 s = zbc_lex_next();
3952 if (s) RETURN_STATUS(s); 3951 if (s) RETURN_STATUS(s);
3953 if (p->lex == BC_LEX_LPAREN) 3952 if (p->lex == BC_LEX_LPAREN)
3954 s = bc_error_bad_token();
3955 else
3956 bc_parse_push(XC_INST_SCALE);
3957 break;
3958 default:
3959 s = bc_error_bad_token(); 3953 s = bc_error_bad_token();
3960 break; 3954 else
3955 bc_parse_push(XC_INST_SCALE);
3956 break;
3957 default:
3958 s = bc_error_bad_token();
3959 break;
3961 } 3960 }
3962 3961
3963 if (!s) bc_parse_push(inst); 3962 if (!s) bc_parse_push(inst);
@@ -4489,74 +4488,74 @@ static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed)
4489 4488
4490 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex); 4489 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex);
4491 switch (p->lex) { 4490 switch (p->lex) {
4492 case XC_LEX_OP_MINUS: 4491 case XC_LEX_OP_MINUS:
4493 case BC_LEX_OP_INC: 4492 case BC_LEX_OP_INC:
4494 case BC_LEX_OP_DEC: 4493 case BC_LEX_OP_DEC:
4495 case BC_LEX_OP_BOOL_NOT: 4494 case BC_LEX_OP_BOOL_NOT:
4496 case BC_LEX_LPAREN: 4495 case BC_LEX_LPAREN:
4497 case XC_LEX_NAME: 4496 case XC_LEX_NAME:
4498 case XC_LEX_NUMBER: 4497 case XC_LEX_NUMBER:
4499 case BC_LEX_KEY_IBASE: 4498 case BC_LEX_KEY_IBASE:
4500 case BC_LEX_KEY_LAST: 4499 case BC_LEX_KEY_LAST:
4501 case BC_LEX_KEY_LENGTH: 4500 case BC_LEX_KEY_LENGTH:
4502 case BC_LEX_KEY_OBASE: 4501 case BC_LEX_KEY_OBASE:
4503 case BC_LEX_KEY_READ: 4502 case BC_LEX_KEY_READ:
4504 case BC_LEX_KEY_SCALE: 4503 case BC_LEX_KEY_SCALE:
4505 case BC_LEX_KEY_SQRT: 4504 case BC_LEX_KEY_SQRT:
4506 s = zbc_parse_expr(BC_PARSE_PRINT); 4505 s = zbc_parse_expr(BC_PARSE_PRINT);
4507 break; 4506 break;
4508 case XC_LEX_STR: 4507 case XC_LEX_STR:
4509 s = zbc_parse_pushSTR(); 4508 s = zbc_parse_pushSTR();
4510 bc_parse_push(XC_INST_PRINT_STR); 4509 bc_parse_push(XC_INST_PRINT_STR);
4511 break; 4510 break;
4512 case BC_LEX_KEY_BREAK: 4511 case BC_LEX_KEY_BREAK:
4513 case BC_LEX_KEY_CONTINUE: 4512 case BC_LEX_KEY_CONTINUE:
4514 s = zbc_parse_break_or_continue(p->lex); 4513 s = zbc_parse_break_or_continue(p->lex);
4515 break; 4514 break;
4516 case BC_LEX_KEY_FOR: 4515 case BC_LEX_KEY_FOR:
4517 s = zbc_parse_for(); 4516 s = zbc_parse_for();
4518 break; 4517 break;
4519 case BC_LEX_KEY_HALT: 4518 case BC_LEX_KEY_HALT:
4520 bc_parse_push(BC_INST_HALT); 4519 bc_parse_push(BC_INST_HALT);
4521 s = zbc_lex_next(); 4520 s = zbc_lex_next();
4522 break; 4521 break;
4523 case BC_LEX_KEY_IF: 4522 case BC_LEX_KEY_IF:
4524 s = zbc_parse_if(); 4523 s = zbc_parse_if();
4525 break; 4524 break;
4526 case BC_LEX_KEY_LIMITS: 4525 case BC_LEX_KEY_LIMITS:
4527 // "limits" is a compile-time command, 4526 // "limits" is a compile-time command,
4528 // the output is produced at _parse time_. 4527 // the output is produced at _parse time_.
4529 printf( 4528 printf(
4530 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n" 4529 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n"
4531 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n" 4530 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n"
4532 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n" 4531 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n"
4533 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n" 4532 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n"
4534 "BC_NAME_MAX = "BC_MAX_NAME_STR "\n" 4533 "BC_NAME_MAX = "BC_MAX_NAME_STR "\n"
4535 "BC_NUM_MAX = "BC_MAX_NUM_STR "\n" 4534 "BC_NUM_MAX = "BC_MAX_NUM_STR "\n"
4536 "MAX Exponent = "BC_MAX_EXP_STR "\n" 4535 "MAX Exponent = "BC_MAX_EXP_STR "\n"
4537 "Number of vars = "BC_MAX_VARS_STR "\n" 4536 "Number of vars = "BC_MAX_VARS_STR "\n"
4538 ); 4537 );
4539 s = zbc_lex_next(); 4538 s = zbc_lex_next();
4540 break; 4539 break;
4541 case BC_LEX_KEY_PRINT: 4540 case BC_LEX_KEY_PRINT:
4542 s = zbc_parse_print(); 4541 s = zbc_parse_print();
4543 break; 4542 break;
4544 case BC_LEX_KEY_QUIT: 4543 case BC_LEX_KEY_QUIT:
4545 // "quit" is a compile-time command. For example, 4544 // "quit" is a compile-time command. For example,
4546 // "if (0 == 1) quit" terminates when parsing the statement, 4545 // "if (0 == 1) quit" terminates when parsing the statement,
4547 // not when it is executed 4546 // not when it is executed
4548 QUIT_OR_RETURN_TO_MAIN; 4547 QUIT_OR_RETURN_TO_MAIN;
4549 case BC_LEX_KEY_RETURN: 4548 case BC_LEX_KEY_RETURN:
4550 if (!p->in_funcdef) 4549 if (!p->in_funcdef)
4551 RETURN_STATUS(bc_error("'return' not in a function")); 4550 RETURN_STATUS(bc_error("'return' not in a function"));
4552 s = zbc_parse_return(); 4551 s = zbc_parse_return();
4553 break; 4552 break;
4554 case BC_LEX_KEY_WHILE: 4553 case BC_LEX_KEY_WHILE:
4555 s = zbc_parse_while(); 4554 s = zbc_parse_while();
4556 break; 4555 break;
4557 default: 4556 default:
4558 s = bc_error_bad_token(); 4557 s = bc_error_bad_token();
4559 break; 4558 break;
4560 } 4559 }
4561 4560
4562 dbg_lex_done("%s:%d done", __func__, __LINE__); 4561 dbg_lex_done("%s:%d done", __func__, __LINE__);
@@ -4612,162 +4611,162 @@ static BcStatus bc_parse_expr_empty_ok(uint8_t flags)
4612 get_token = false; 4611 get_token = false;
4613 s = BC_STATUS_SUCCESS; 4612 s = BC_STATUS_SUCCESS;
4614 switch (t) { 4613 switch (t) {
4615 case BC_LEX_OP_INC: 4614 case BC_LEX_OP_INC:
4616 case BC_LEX_OP_DEC: 4615 case BC_LEX_OP_DEC:
4617 dbg_lex("%s:%d LEX_OP_INC/DEC", __func__, __LINE__); 4616 dbg_lex("%s:%d LEX_OP_INC/DEC", __func__, __LINE__);
4618 s = zbc_parse_incdec(&prev, &paren_expr, &nexprs, flags); 4617 s = zbc_parse_incdec(&prev, &paren_expr, &nexprs, flags);
4619 rprn = bin_last = false; 4618 rprn = bin_last = false;
4620 //get_token = false; - already is 4619 //get_token = false; - already is
4621 break; 4620 break;
4622 case XC_LEX_OP_MINUS: 4621 case XC_LEX_OP_MINUS:
4623 dbg_lex("%s:%d LEX_OP_MINUS", __func__, __LINE__); 4622 dbg_lex("%s:%d LEX_OP_MINUS", __func__, __LINE__);
4624 s = zbc_parse_minus(&prev, ops_bgn, rprn, &nexprs); 4623 s = zbc_parse_minus(&prev, ops_bgn, rprn, &nexprs);
4625 rprn = false; 4624 rprn = false;
4626 //get_token = false; - already is 4625 //get_token = false; - already is
4627 bin_last = (prev == XC_INST_MINUS); 4626 bin_last = (prev == XC_INST_MINUS);
4628 break; 4627 break;
4629 case BC_LEX_OP_ASSIGN_POWER: 4628 case BC_LEX_OP_ASSIGN_POWER:
4630 case BC_LEX_OP_ASSIGN_MULTIPLY: 4629 case BC_LEX_OP_ASSIGN_MULTIPLY:
4631 case BC_LEX_OP_ASSIGN_DIVIDE: 4630 case BC_LEX_OP_ASSIGN_DIVIDE:
4632 case BC_LEX_OP_ASSIGN_MODULUS: 4631 case BC_LEX_OP_ASSIGN_MODULUS:
4633 case BC_LEX_OP_ASSIGN_PLUS: 4632 case BC_LEX_OP_ASSIGN_PLUS:
4634 case BC_LEX_OP_ASSIGN_MINUS: 4633 case BC_LEX_OP_ASSIGN_MINUS:
4635 case BC_LEX_OP_ASSIGN: 4634 case BC_LEX_OP_ASSIGN:
4636 dbg_lex("%s:%d LEX_ASSIGNxyz", __func__, __LINE__); 4635 dbg_lex("%s:%d LEX_ASSIGNxyz", __func__, __LINE__);
4637 if (prev != XC_INST_VAR && prev != XC_INST_ARRAY_ELEM 4636 if (prev != XC_INST_VAR && prev != XC_INST_ARRAY_ELEM
4638 && prev != XC_INST_SCALE && prev != XC_INST_IBASE 4637 && prev != XC_INST_SCALE && prev != XC_INST_IBASE
4639 && prev != XC_INST_OBASE && prev != BC_INST_LAST 4638 && prev != XC_INST_OBASE && prev != BC_INST_LAST
4640 ) { 4639 ) {
4641 return bc_error("bad assignment:" 4640 return bc_error("bad assignment:"
4642 " left side must be variable" 4641 " left side must be variable"
4643 " or array element" 4642 " or array element"
4644 ); // note: shared string 4643 ); // note: shared string
4645 } 4644 }
4646 // Fallthrough. 4645 // Fallthrough.
4647 case XC_LEX_OP_POWER: 4646 case XC_LEX_OP_POWER:
4648 case XC_LEX_OP_MULTIPLY: 4647 case XC_LEX_OP_MULTIPLY:
4649 case XC_LEX_OP_DIVIDE: 4648 case XC_LEX_OP_DIVIDE:
4650 case XC_LEX_OP_MODULUS: 4649 case XC_LEX_OP_MODULUS:
4651 case XC_LEX_OP_PLUS: 4650 case XC_LEX_OP_PLUS:
4652 case XC_LEX_OP_REL_EQ: 4651 case XC_LEX_OP_REL_EQ:
4653 case XC_LEX_OP_REL_LE: 4652 case XC_LEX_OP_REL_LE:
4654 case XC_LEX_OP_REL_GE: 4653 case XC_LEX_OP_REL_GE:
4655 case XC_LEX_OP_REL_NE: 4654 case XC_LEX_OP_REL_NE:
4656 case XC_LEX_OP_REL_LT: 4655 case XC_LEX_OP_REL_LT:
4657 case XC_LEX_OP_REL_GT: 4656 case XC_LEX_OP_REL_GT:
4658 case BC_LEX_OP_BOOL_NOT: 4657 case BC_LEX_OP_BOOL_NOT:
4659 case BC_LEX_OP_BOOL_OR: 4658 case BC_LEX_OP_BOOL_OR:
4660 case BC_LEX_OP_BOOL_AND: 4659 case BC_LEX_OP_BOOL_AND:
4661 dbg_lex("%s:%d LEX_OP_xyz", __func__, __LINE__); 4660 dbg_lex("%s:%d LEX_OP_xyz", __func__, __LINE__);
4662 if (((t == BC_LEX_OP_BOOL_NOT) != bin_last) 4661 if (((t == BC_LEX_OP_BOOL_NOT) != bin_last)
4663 || (t != BC_LEX_OP_BOOL_NOT && prev == XC_INST_BOOL_NOT) 4662 || (t != BC_LEX_OP_BOOL_NOT && prev == XC_INST_BOOL_NOT)
4664 ) { 4663 ) {
4665 return bc_error_bad_expression(); 4664 return bc_error_bad_expression();
4666 } 4665 }
4667 nrelops += (t >= XC_LEX_OP_REL_EQ && t <= XC_LEX_OP_REL_GT); 4666 nrelops += (t >= XC_LEX_OP_REL_EQ && t <= XC_LEX_OP_REL_GT);
4668 prev = BC_TOKEN_2_INST(t); 4667 prev = BC_TOKEN_2_INST(t);
4669 bc_parse_operator(t, ops_bgn, &nexprs); 4668 bc_parse_operator(t, ops_bgn, &nexprs);
4670 s = zbc_lex_next(); 4669 s = zbc_lex_next();
4671 rprn = false; 4670 rprn = false;
4672 //get_token = false; - already is 4671 //get_token = false; - already is
4673 bin_last = (t != BC_LEX_OP_BOOL_NOT); 4672 bin_last = (t != BC_LEX_OP_BOOL_NOT);
4674 break; 4673 break;
4675 case BC_LEX_LPAREN: 4674 case BC_LEX_LPAREN:
4676 dbg_lex("%s:%d LEX_LPAREN", __func__, __LINE__); 4675 dbg_lex("%s:%d LEX_LPAREN", __func__, __LINE__);
4677 if (BC_PARSE_LEAF(prev, rprn)) 4676 if (BC_PARSE_LEAF(prev, rprn))
4678 return bc_error_bad_expression(); 4677 return bc_error_bad_expression();
4679 bc_vec_push(&p->ops, &t); 4678 bc_vec_push(&p->ops, &t);
4680 nparens++; 4679 nparens++;
4681 get_token = true; 4680 get_token = true;
4682 paren_expr = false; 4681 paren_expr = false;
4683 rprn = bin_last = false; 4682 rprn = bin_last = false;
4684 break; 4683 break;
4685 case BC_LEX_RPAREN: 4684 case BC_LEX_RPAREN:
4686 dbg_lex("%s:%d LEX_RPAREN", __func__, __LINE__); 4685 dbg_lex("%s:%d LEX_RPAREN", __func__, __LINE__);
4687 if (bin_last || prev == XC_INST_BOOL_NOT) 4686 if (bin_last || prev == XC_INST_BOOL_NOT)
4688 return bc_error_bad_expression(); 4687 return bc_error_bad_expression();
4689 if (nparens == 0) { 4688 if (nparens == 0) {
4690 goto exit_loop; 4689 goto exit_loop;
4691 } 4690 }
4692 if (!paren_expr) { 4691 if (!paren_expr) {
4693 dbg_lex_done("%s:%d done (returning EMPTY_EXP)", __func__, __LINE__); 4692 dbg_lex_done("%s:%d done (returning EMPTY_EXP)", __func__, __LINE__);
4694 return BC_STATUS_PARSE_EMPTY_EXP; 4693 return BC_STATUS_PARSE_EMPTY_EXP;
4695 } 4694 }
4696 s = zbc_parse_rightParen(ops_bgn, &nexprs); 4695 s = zbc_parse_rightParen(ops_bgn, &nexprs);
4697 nparens--; 4696 nparens--;
4698 get_token = true; 4697 get_token = true;
4699 paren_expr = rprn = true; 4698 paren_expr = rprn = true;
4700 bin_last = false; 4699 bin_last = false;
4701 break; 4700 break;
4702 case XC_LEX_NAME: 4701 case XC_LEX_NAME:
4703 dbg_lex("%s:%d LEX_NAME", __func__, __LINE__); 4702 dbg_lex("%s:%d LEX_NAME", __func__, __LINE__);
4704 if (BC_PARSE_LEAF(prev, rprn)) 4703 if (BC_PARSE_LEAF(prev, rprn))
4705 return bc_error_bad_expression(); 4704 return bc_error_bad_expression();
4706 s = zbc_parse_name(&prev, flags & ~BC_PARSE_NOCALL); 4705 s = zbc_parse_name(&prev, flags & ~BC_PARSE_NOCALL);
4707 paren_expr = true; 4706 paren_expr = true;
4708 rprn = bin_last = false; 4707 rprn = bin_last = false;
4709 //get_token = false; - already is 4708 //get_token = false; - already is
4710 nexprs++; 4709 nexprs++;
4711 break; 4710 break;
4712 case XC_LEX_NUMBER: 4711 case XC_LEX_NUMBER:
4713 dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__); 4712 dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__);
4714 if (BC_PARSE_LEAF(prev, rprn)) 4713 if (BC_PARSE_LEAF(prev, rprn))
4715 return bc_error_bad_expression(); 4714 return bc_error_bad_expression();
4716 bc_parse_pushNUM(); 4715 bc_parse_pushNUM();
4717 prev = XC_INST_NUM; 4716 prev = XC_INST_NUM;
4718 get_token = true; 4717 get_token = true;
4719 paren_expr = true; 4718 paren_expr = true;
4720 rprn = bin_last = false; 4719 rprn = bin_last = false;
4721 nexprs++; 4720 nexprs++;
4722 break; 4721 break;
4723 case BC_LEX_KEY_IBASE: 4722 case BC_LEX_KEY_IBASE:
4724 case BC_LEX_KEY_LAST: 4723 case BC_LEX_KEY_LAST:
4725 case BC_LEX_KEY_OBASE: 4724 case BC_LEX_KEY_OBASE:
4726 dbg_lex("%s:%d LEX_IBASE/LAST/OBASE", __func__, __LINE__); 4725 dbg_lex("%s:%d LEX_IBASE/LAST/OBASE", __func__, __LINE__);
4727 if (BC_PARSE_LEAF(prev, rprn)) 4726 if (BC_PARSE_LEAF(prev, rprn))
4728 return bc_error_bad_expression(); 4727 return bc_error_bad_expression();
4729 prev = (char) (t - BC_LEX_KEY_IBASE + XC_INST_IBASE); 4728 prev = (char) (t - BC_LEX_KEY_IBASE + XC_INST_IBASE);
4730 bc_parse_push((char) prev); 4729 bc_parse_push((char) prev);
4731 get_token = true; 4730 get_token = true;
4732 paren_expr = true; 4731 paren_expr = true;
4733 rprn = bin_last = false; 4732 rprn = bin_last = false;
4734 nexprs++; 4733 nexprs++;
4735 break; 4734 break;
4736 case BC_LEX_KEY_LENGTH: 4735 case BC_LEX_KEY_LENGTH:
4737 case BC_LEX_KEY_SQRT: 4736 case BC_LEX_KEY_SQRT:
4738 dbg_lex("%s:%d LEX_LEN/SQRT", __func__, __LINE__); 4737 dbg_lex("%s:%d LEX_LEN/SQRT", __func__, __LINE__);
4739 if (BC_PARSE_LEAF(prev, rprn)) 4738 if (BC_PARSE_LEAF(prev, rprn))
4740 return bc_error_bad_expression(); 4739 return bc_error_bad_expression();
4741 s = zbc_parse_builtin(t, flags, &prev); 4740 s = zbc_parse_builtin(t, flags, &prev);
4742 get_token = true; 4741 get_token = true;
4743 paren_expr = true; 4742 paren_expr = true;
4744 rprn = bin_last = false; 4743 rprn = bin_last = false;
4745 nexprs++; 4744 nexprs++;
4746 break; 4745 break;
4747 case BC_LEX_KEY_READ: 4746 case BC_LEX_KEY_READ:
4748 dbg_lex("%s:%d LEX_READ", __func__, __LINE__); 4747 dbg_lex("%s:%d LEX_READ", __func__, __LINE__);
4749 if (BC_PARSE_LEAF(prev, rprn)) 4748 if (BC_PARSE_LEAF(prev, rprn))
4750 return bc_error_bad_expression(); 4749 return bc_error_bad_expression();
4751 s = zbc_parse_read(); 4750 s = zbc_parse_read();
4752 prev = XC_INST_READ; 4751 prev = XC_INST_READ;
4753 get_token = true; 4752 get_token = true;
4754 paren_expr = true; 4753 paren_expr = true;
4755 rprn = bin_last = false; 4754 rprn = bin_last = false;
4756 nexprs++; 4755 nexprs++;
4757 break; 4756 break;
4758 case BC_LEX_KEY_SCALE: 4757 case BC_LEX_KEY_SCALE:
4759 dbg_lex("%s:%d LEX_SCALE", __func__, __LINE__); 4758 dbg_lex("%s:%d LEX_SCALE", __func__, __LINE__);
4760 if (BC_PARSE_LEAF(prev, rprn)) 4759 if (BC_PARSE_LEAF(prev, rprn))
4761 return bc_error_bad_expression(); 4760 return bc_error_bad_expression();
4762 s = zbc_parse_scale(&prev, flags); 4761 s = zbc_parse_scale(&prev, flags);
4763 prev = XC_INST_SCALE; 4762 prev = XC_INST_SCALE;
4764 //get_token = false; - already is 4763 //get_token = false; - already is
4765 paren_expr = true; 4764 paren_expr = true;
4766 rprn = bin_last = false; 4765 rprn = bin_last = false;
4767 nexprs++; 4766 nexprs++;
4768 break; 4767 break;
4769 default: 4768 default:
4770 return bc_error_bad_token(); 4769 return bc_error_bad_token();
4771 } 4770 }
4772 4771
4773 if (s || G_interrupt) // error, or ^C: stop parsing 4772 if (s || G_interrupt) // error, or ^C: stop parsing
@@ -4913,65 +4912,65 @@ static BC_STATUS zdc_parse_token(BcLexType t)
4913 s = BC_STATUS_SUCCESS; 4912 s = BC_STATUS_SUCCESS;
4914 get_token = true; 4913 get_token = true;
4915 switch (t) { 4914 switch (t) {
4916 case XC_LEX_OP_REL_EQ: 4915 case XC_LEX_OP_REL_EQ:
4917 case XC_LEX_OP_REL_LE: 4916 case XC_LEX_OP_REL_LE:
4918 case XC_LEX_OP_REL_GE: 4917 case XC_LEX_OP_REL_GE:
4919 case XC_LEX_OP_REL_NE: 4918 case XC_LEX_OP_REL_NE:
4920 case XC_LEX_OP_REL_LT: 4919 case XC_LEX_OP_REL_LT:
4921 case XC_LEX_OP_REL_GT: 4920 case XC_LEX_OP_REL_GT:
4922 dbg_lex("%s:%d LEX_OP_REL_xyz", __func__, __LINE__); 4921 dbg_lex("%s:%d LEX_OP_REL_xyz", __func__, __LINE__);
4923 s = zdc_parse_cond(t - XC_LEX_OP_REL_EQ + XC_INST_REL_EQ); 4922 s = zdc_parse_cond(t - XC_LEX_OP_REL_EQ + XC_INST_REL_EQ);
4924 get_token = false; 4923 get_token = false;
4925 break; 4924 break;
4926 case DC_LEX_SCOLON: 4925 case DC_LEX_SCOLON:
4927 case DC_LEX_COLON: 4926 case DC_LEX_COLON:
4928 dbg_lex("%s:%d LEX_[S]COLON", __func__, __LINE__); 4927 dbg_lex("%s:%d LEX_[S]COLON", __func__, __LINE__);
4929 s = zdc_parse_mem(XC_INST_ARRAY_ELEM, true, t == DC_LEX_COLON); 4928 s = zdc_parse_mem(XC_INST_ARRAY_ELEM, true, t == DC_LEX_COLON);
4930 break; 4929 break;
4931 case XC_LEX_STR: 4930 case XC_LEX_STR:
4932 dbg_lex("%s:%d LEX_STR", __func__, __LINE__); 4931 dbg_lex("%s:%d LEX_STR", __func__, __LINE__);
4933 dc_parse_string(); 4932 dc_parse_string();
4934 break; 4933 break;
4935 case XC_LEX_NEG: 4934 case XC_LEX_NEG:
4936 dbg_lex("%s:%d LEX_NEG", __func__, __LINE__); 4935 dbg_lex("%s:%d LEX_NEG", __func__, __LINE__);
4937 s = zbc_lex_next(); 4936 s = zbc_lex_next();
4938 if (s) RETURN_STATUS(s); 4937 if (s) RETURN_STATUS(s);
4939 if (G.prs.lex != XC_LEX_NUMBER) 4938 if (G.prs.lex != XC_LEX_NUMBER)
4940 RETURN_STATUS(bc_error_bad_token());
4941 bc_parse_pushNUM();
4942 bc_parse_push(XC_INST_NEG);
4943 break;
4944 case XC_LEX_NUMBER:
4945 dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__);
4946 bc_parse_pushNUM();
4947 break;
4948 case DC_LEX_READ:
4949 dbg_lex("%s:%d LEX_KEY_READ", __func__, __LINE__);
4950 bc_parse_push(XC_INST_READ);
4951 break;
4952 case DC_LEX_OP_ASSIGN:
4953 case DC_LEX_STORE_PUSH:
4954 dbg_lex("%s:%d LEX_OP_ASSIGN/STORE_PUSH", __func__, __LINE__);
4955 assign = (t == DC_LEX_OP_ASSIGN);
4956 inst = assign ? XC_INST_VAR : DC_INST_PUSH_TO_VAR;
4957 s = zdc_parse_mem(inst, true, assign);
4958 break;
4959 case DC_LEX_LOAD:
4960 case DC_LEX_LOAD_POP:
4961 dbg_lex("%s:%d LEX_OP_LOAD[_POP]", __func__, __LINE__);
4962 inst = t == DC_LEX_LOAD_POP ? DC_INST_PUSH_VAR : DC_INST_LOAD;
4963 s = zdc_parse_mem(inst, true, false);
4964 break;
4965 case DC_LEX_STORE_IBASE:
4966 case DC_LEX_STORE_SCALE:
4967 case DC_LEX_STORE_OBASE:
4968 dbg_lex("%s:%d LEX_OP_STORE_I/OBASE/SCALE", __func__, __LINE__);
4969 inst = t - DC_LEX_STORE_IBASE + XC_INST_IBASE;
4970 s = zdc_parse_mem(inst, false, true);
4971 break;
4972 default:
4973 dbg_lex_done("%s:%d done (bad token)", __func__, __LINE__);
4974 RETURN_STATUS(bc_error_bad_token()); 4939 RETURN_STATUS(bc_error_bad_token());
4940 bc_parse_pushNUM();
4941 bc_parse_push(XC_INST_NEG);
4942 break;
4943 case XC_LEX_NUMBER:
4944 dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__);
4945 bc_parse_pushNUM();
4946 break;
4947 case DC_LEX_READ:
4948 dbg_lex("%s:%d LEX_KEY_READ", __func__, __LINE__);
4949 bc_parse_push(XC_INST_READ);
4950 break;
4951 case DC_LEX_OP_ASSIGN:
4952 case DC_LEX_STORE_PUSH:
4953 dbg_lex("%s:%d LEX_OP_ASSIGN/STORE_PUSH", __func__, __LINE__);
4954 assign = (t == DC_LEX_OP_ASSIGN);
4955 inst = assign ? XC_INST_VAR : DC_INST_PUSH_TO_VAR;
4956 s = zdc_parse_mem(inst, true, assign);
4957 break;
4958 case DC_LEX_LOAD:
4959 case DC_LEX_LOAD_POP:
4960 dbg_lex("%s:%d LEX_OP_LOAD[_POP]", __func__, __LINE__);
4961 inst = t == DC_LEX_LOAD_POP ? DC_INST_PUSH_VAR : DC_INST_LOAD;
4962 s = zdc_parse_mem(inst, true, false);
4963 break;
4964 case DC_LEX_STORE_IBASE:
4965 case DC_LEX_STORE_SCALE:
4966 case DC_LEX_STORE_OBASE:
4967 dbg_lex("%s:%d LEX_OP_STORE_I/OBASE/SCALE", __func__, __LINE__);
4968 inst = t - DC_LEX_STORE_IBASE + XC_INST_IBASE;
4969 s = zdc_parse_mem(inst, false, true);
4970 break;
4971 default:
4972 dbg_lex_done("%s:%d done (bad token)", __func__, __LINE__);
4973 RETURN_STATUS(bc_error_bad_token());
4975 } 4974 }
4976 4975
4977 if (!s && get_token) s = zbc_lex_next(); 4976 if (!s && get_token) s = zbc_lex_next();
@@ -5050,62 +5049,62 @@ static BcVec* bc_program_search(char *id, bool var)
5050static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex) 5049static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex)
5051{ 5050{
5052 switch (r->t) { 5051 switch (r->t) {
5053 case XC_RESULT_STR: 5052 case XC_RESULT_STR:
5054 case XC_RESULT_TEMP: 5053 case XC_RESULT_TEMP:
5055 case XC_RESULT_IBASE: 5054 case XC_RESULT_IBASE:
5056 case XC_RESULT_SCALE: 5055 case XC_RESULT_SCALE:
5057 case XC_RESULT_OBASE: 5056 case XC_RESULT_OBASE:
5058 *num = &r->d.n; 5057 *num = &r->d.n;
5059 break; 5058 break;
5060 case XC_RESULT_CONSTANT: { 5059 case XC_RESULT_CONSTANT: {
5061 BcStatus s; 5060 BcStatus s;
5062 char *str; 5061 char *str;
5063 unsigned base_t; 5062 unsigned base_t;
5064 size_t len; 5063 size_t len;
5065
5066 str = *bc_program_const(r->d.id.idx);
5067 len = strlen(str);
5068
5069 bc_num_init(&r->d.n, len);
5070
5071 hex = hex && len == 1;
5072 base_t = hex ? 16 : G.prog.ib_t;
5073 s = zbc_num_parse(&r->d.n, str, base_t);
5074 if (s) {
5075 bc_num_free(&r->d.n);
5076 RETURN_STATUS(s);
5077 }
5078 *num = &r->d.n;
5079 r->t = XC_RESULT_TEMP;
5080 break;
5081 }
5082 case XC_RESULT_VAR:
5083 case XC_RESULT_ARRAY:
5084 case XC_RESULT_ARRAY_ELEM: {
5085 BcVec *v;
5086 5064
5087 v = bc_program_search(r->d.id.name, r->t == XC_RESULT_VAR); 5065 str = *bc_program_const(r->d.id.idx);
5066 len = strlen(str);
5088 5067
5089 if (r->t == XC_RESULT_ARRAY_ELEM) { 5068 bc_num_init(&r->d.n, len);
5090 v = bc_vec_top(v); 5069
5091 if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1); 5070 hex = hex && len == 1;
5092 *num = bc_vec_item(v, r->d.id.idx); 5071 base_t = hex ? 16 : G.prog.ib_t;
5093 } else 5072 s = zbc_num_parse(&r->d.n, str, base_t);
5094 *num = bc_vec_top(v); 5073 if (s) {
5095 break; 5074 bc_num_free(&r->d.n);
5075 RETURN_STATUS(s);
5096 } 5076 }
5077 *num = &r->d.n;
5078 r->t = XC_RESULT_TEMP;
5079 break;
5080 }
5081 case XC_RESULT_VAR:
5082 case XC_RESULT_ARRAY:
5083 case XC_RESULT_ARRAY_ELEM: {
5084 BcVec *v;
5085
5086 v = bc_program_search(r->d.id.name, r->t == XC_RESULT_VAR);
5087
5088 if (r->t == XC_RESULT_ARRAY_ELEM) {
5089 v = bc_vec_top(v);
5090 if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1);
5091 *num = bc_vec_item(v, r->d.id.idx);
5092 } else
5093 *num = bc_vec_top(v);
5094 break;
5095 }
5097#if ENABLE_BC 5096#if ENABLE_BC
5098 case BC_RESULT_LAST: 5097 case BC_RESULT_LAST:
5099 *num = &G.prog.last; 5098 *num = &G.prog.last;
5100 break; 5099 break;
5101 case BC_RESULT_ONE: 5100 case BC_RESULT_ONE:
5102 *num = &G.prog.one; 5101 *num = &G.prog.one;
5103 break; 5102 break;
5104#endif 5103#endif
5105#if SANITY_CHECKS 5104#if SANITY_CHECKS
5106 default: 5105 default:
5107 // Testing the theory that dc does not reach LAST/ONE 5106 // Testing the theory that dc does not reach LAST/ONE
5108 bb_error_msg_and_die("BUG:%d", r->t); 5107 bb_error_msg_and_die("BUG:%d", r->t);
5109#endif 5108#endif
5110 } 5109 }
5111 5110
@@ -6422,245 +6421,245 @@ static BC_STATUS zbc_program_exec(void)
6422 dbg_exec("inst at %zd:%d results.len:%d", ip->inst_idx - 1, inst, G.prog.results.len); 6421 dbg_exec("inst at %zd:%d results.len:%d", ip->inst_idx - 1, inst, G.prog.results.len);
6423 switch (inst) { 6422 switch (inst) {
6424#if ENABLE_BC 6423#if ENABLE_BC
6425 case BC_INST_JUMP_ZERO: { 6424 case BC_INST_JUMP_ZERO: {
6426 BcNum *num; 6425 BcNum *num;
6427 bool zero; 6426 bool zero;
6428 dbg_exec("BC_INST_JUMP_ZERO:"); 6427 dbg_exec("BC_INST_JUMP_ZERO:");
6429 s = zbc_program_prep(&ptr, &num); 6428 s = zbc_program_prep(&ptr, &num);
6430 if (s) RETURN_STATUS(s); 6429 if (s) RETURN_STATUS(s);
6431 zero = (bc_num_cmp(num, &G.prog.zero) == 0); 6430 zero = (bc_num_cmp(num, &G.prog.zero) == 0);
6432 bc_vec_pop(&G.prog.results); 6431 bc_vec_pop(&G.prog.results);
6433 if (!zero) { 6432 if (!zero) {
6434 bc_program_index(code, &ip->inst_idx); 6433 bc_program_index(code, &ip->inst_idx);
6435 break;
6436 }
6437 // else: fall through
6438 }
6439 case BC_INST_JUMP: {
6440 size_t idx = bc_program_index(code, &ip->inst_idx);
6441 size_t *addr = bc_vec_item(&func->labels, idx);
6442 dbg_exec("BC_INST_JUMP: to %ld", (long)*addr);
6443 ip->inst_idx = *addr;
6444 break; 6434 break;
6445 } 6435 }
6446 case BC_INST_CALL: 6436 // else: fall through
6447 dbg_exec("BC_INST_CALL:"); 6437 }
6448 s = zbc_program_call(code, &ip->inst_idx); 6438 case BC_INST_JUMP: {
6449 goto read_updated_ip; 6439 size_t idx = bc_program_index(code, &ip->inst_idx);
6450 case BC_INST_INC_PRE: 6440 size_t *addr = bc_vec_item(&func->labels, idx);
6451 case BC_INST_DEC_PRE: 6441 dbg_exec("BC_INST_JUMP: to %ld", (long)*addr);
6452 case BC_INST_INC_POST: 6442 ip->inst_idx = *addr;
6453 case BC_INST_DEC_POST: 6443 break;
6454 dbg_exec("BC_INST_INCDEC:"); 6444 }
6455 s = zbc_program_incdec(inst); 6445 case BC_INST_CALL:
6456 break; 6446 dbg_exec("BC_INST_CALL:");
6457 case BC_INST_HALT: 6447 s = zbc_program_call(code, &ip->inst_idx);
6458 dbg_exec("BC_INST_HALT:"); 6448 goto read_updated_ip;
6459 QUIT_OR_RETURN_TO_MAIN; 6449 case BC_INST_INC_PRE:
6460 break; 6450 case BC_INST_DEC_PRE:
6461 case XC_INST_RET: 6451 case BC_INST_INC_POST:
6462 case BC_INST_RET0: 6452 case BC_INST_DEC_POST:
6463 dbg_exec("BC_INST_RET[0]:"); 6453 dbg_exec("BC_INST_INCDEC:");
6464 s = zbc_program_return(inst); 6454 s = zbc_program_incdec(inst);
6465 goto read_updated_ip; 6455 break;
6466 case XC_INST_BOOL_OR: 6456 case BC_INST_HALT:
6467 case XC_INST_BOOL_AND: 6457 dbg_exec("BC_INST_HALT:");
6458 QUIT_OR_RETURN_TO_MAIN;
6459 break;
6460 case XC_INST_RET:
6461 case BC_INST_RET0:
6462 dbg_exec("BC_INST_RET[0]:");
6463 s = zbc_program_return(inst);
6464 goto read_updated_ip;
6465 case XC_INST_BOOL_OR:
6466 case XC_INST_BOOL_AND:
6468#endif // ENABLE_BC 6467#endif // ENABLE_BC
6469 case XC_INST_REL_EQ: 6468 case XC_INST_REL_EQ:
6470 case XC_INST_REL_LE: 6469 case XC_INST_REL_LE:
6471 case XC_INST_REL_GE: 6470 case XC_INST_REL_GE:
6472 case XC_INST_REL_NE: 6471 case XC_INST_REL_NE:
6473 case XC_INST_REL_LT: 6472 case XC_INST_REL_LT:
6474 case XC_INST_REL_GT: 6473 case XC_INST_REL_GT:
6475 dbg_exec("BC_INST_BOOL:"); 6474 dbg_exec("BC_INST_BOOL:");
6476 s = zbc_program_logical(inst); 6475 s = zbc_program_logical(inst);
6477 break; 6476 break;
6478 case XC_INST_READ: 6477 case XC_INST_READ:
6479 dbg_exec("XC_INST_READ:"); 6478 dbg_exec("XC_INST_READ:");
6480 s = zbc_program_read(); 6479 s = zbc_program_read();
6481 goto read_updated_ip; 6480 goto read_updated_ip;
6482 case XC_INST_VAR: 6481 case XC_INST_VAR:
6483 dbg_exec("XC_INST_VAR:"); 6482 dbg_exec("XC_INST_VAR:");
6484 s = zbc_program_pushVar(code, &ip->inst_idx, false, false); 6483 s = zbc_program_pushVar(code, &ip->inst_idx, false, false);
6485 break; 6484 break;
6486 case XC_INST_ARRAY_ELEM: 6485 case XC_INST_ARRAY_ELEM:
6487 case XC_INST_ARRAY: 6486 case XC_INST_ARRAY:
6488 dbg_exec("XC_INST_ARRAY[_ELEM]:"); 6487 dbg_exec("XC_INST_ARRAY[_ELEM]:");
6489 s = zbc_program_pushArray(code, &ip->inst_idx, inst); 6488 s = zbc_program_pushArray(code, &ip->inst_idx, inst);
6490 break; 6489 break;
6491#if ENABLE_BC 6490#if ENABLE_BC
6492 case BC_INST_LAST: 6491 case BC_INST_LAST:
6493 dbg_exec("BC_INST_LAST:"); 6492 dbg_exec("BC_INST_LAST:");
6494 r.t = BC_RESULT_LAST; 6493 r.t = BC_RESULT_LAST;
6495 bc_vec_push(&G.prog.results, &r); 6494 bc_vec_push(&G.prog.results, &r);
6496 break; 6495 break;
6497#endif 6496#endif
6498 case XC_INST_IBASE: 6497 case XC_INST_IBASE:
6499 case XC_INST_OBASE: 6498 case XC_INST_OBASE:
6500 case XC_INST_SCALE: 6499 case XC_INST_SCALE:
6501 dbg_exec("XC_INST_internalvar(%d):", inst - XC_INST_IBASE); 6500 dbg_exec("XC_INST_internalvar(%d):", inst - XC_INST_IBASE);
6502 bc_program_pushGlobal(inst); 6501 bc_program_pushGlobal(inst);
6503 break; 6502 break;
6504 case XC_INST_SCALE_FUNC: 6503 case XC_INST_SCALE_FUNC:
6505 case XC_INST_LENGTH: 6504 case XC_INST_LENGTH:
6506 case XC_INST_SQRT: 6505 case XC_INST_SQRT:
6507 dbg_exec("BC_INST_builtin:"); 6506 dbg_exec("BC_INST_builtin:");
6508 s = zbc_program_builtin(inst); 6507 s = zbc_program_builtin(inst);
6509 break; 6508 break;
6510 case XC_INST_NUM: 6509 case XC_INST_NUM:
6511 dbg_exec("XC_INST_NUM:"); 6510 dbg_exec("XC_INST_NUM:");
6512 r.t = XC_RESULT_CONSTANT; 6511 r.t = XC_RESULT_CONSTANT;
6513 r.d.id.idx = bc_program_index(code, &ip->inst_idx); 6512 r.d.id.idx = bc_program_index(code, &ip->inst_idx);
6514 bc_vec_push(&G.prog.results, &r); 6513 bc_vec_push(&G.prog.results, &r);
6515 break; 6514 break;
6516 case XC_INST_POP: 6515 case XC_INST_POP:
6517 dbg_exec("XC_INST_POP:"); 6516 dbg_exec("XC_INST_POP:");
6518 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) 6517 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
6519 s = bc_error_stack_has_too_few_elements(); 6518 s = bc_error_stack_has_too_few_elements();
6520 else 6519 else
6521 bc_vec_pop(&G.prog.results); 6520 bc_vec_pop(&G.prog.results);
6522 break; 6521 break;
6523 case XC_INST_PRINT: 6522 case XC_INST_PRINT:
6524 case XC_INST_PRINT_POP: 6523 case XC_INST_PRINT_POP:
6525 case XC_INST_PRINT_STR: 6524 case XC_INST_PRINT_STR:
6526 dbg_exec("XC_INST_PRINTxyz:"); 6525 dbg_exec("XC_INST_PRINTxyz:");
6527 s = zbc_program_print(inst, 0); 6526 s = zbc_program_print(inst, 0);
6528 break; 6527 break;
6529 case XC_INST_STR: 6528 case XC_INST_STR:
6530 dbg_exec("XC_INST_STR:"); 6529 dbg_exec("XC_INST_STR:");
6531 r.t = XC_RESULT_STR; 6530 r.t = XC_RESULT_STR;
6532 r.d.id.idx = bc_program_index(code, &ip->inst_idx); 6531 r.d.id.idx = bc_program_index(code, &ip->inst_idx);
6533 bc_vec_push(&G.prog.results, &r); 6532 bc_vec_push(&G.prog.results, &r);
6534 break; 6533 break;
6535 case XC_INST_POWER: 6534 case XC_INST_POWER:
6536 case XC_INST_MULTIPLY: 6535 case XC_INST_MULTIPLY:
6537 case XC_INST_DIVIDE: 6536 case XC_INST_DIVIDE:
6538 case XC_INST_MODULUS: 6537 case XC_INST_MODULUS:
6539 case XC_INST_PLUS: 6538 case XC_INST_PLUS:
6540 case XC_INST_MINUS: 6539 case XC_INST_MINUS:
6541 dbg_exec("BC_INST_binaryop:"); 6540 dbg_exec("BC_INST_binaryop:");
6542 s = zbc_program_op(inst); 6541 s = zbc_program_op(inst);
6543 break; 6542 break;
6544 case XC_INST_BOOL_NOT: { 6543 case XC_INST_BOOL_NOT: {
6545 BcNum *num; 6544 BcNum *num;
6546 dbg_exec("XC_INST_BOOL_NOT:"); 6545 dbg_exec("XC_INST_BOOL_NOT:");
6547 s = zbc_program_prep(&ptr, &num); 6546 s = zbc_program_prep(&ptr, &num);
6548 if (s) RETURN_STATUS(s); 6547 if (s) RETURN_STATUS(s);
6549 bc_num_init_DEF_SIZE(&r.d.n); 6548 bc_num_init_DEF_SIZE(&r.d.n);
6550 if (bc_num_cmp(num, &G.prog.zero) == 0) 6549 if (bc_num_cmp(num, &G.prog.zero) == 0)
6551 bc_num_one(&r.d.n); 6550 bc_num_one(&r.d.n);
6552 //else bc_num_zero(&r.d.n); - already is 6551 //else bc_num_zero(&r.d.n); - already is
6553 bc_program_retire(&r, XC_RESULT_TEMP); 6552 bc_program_retire(&r, XC_RESULT_TEMP);
6554 break; 6553 break;
6555 } 6554 }
6556 case XC_INST_NEG: 6555 case XC_INST_NEG:
6557 dbg_exec("XC_INST_NEG:"); 6556 dbg_exec("XC_INST_NEG:");
6558 s = zbc_program_negate(); 6557 s = zbc_program_negate();
6559 break; 6558 break;
6560#if ENABLE_BC 6559#if ENABLE_BC
6561 case BC_INST_ASSIGN_POWER: 6560 case BC_INST_ASSIGN_POWER:
6562 case BC_INST_ASSIGN_MULTIPLY: 6561 case BC_INST_ASSIGN_MULTIPLY:
6563 case BC_INST_ASSIGN_DIVIDE: 6562 case BC_INST_ASSIGN_DIVIDE:
6564 case BC_INST_ASSIGN_MODULUS: 6563 case BC_INST_ASSIGN_MODULUS:
6565 case BC_INST_ASSIGN_PLUS: 6564 case BC_INST_ASSIGN_PLUS:
6566 case BC_INST_ASSIGN_MINUS: 6565 case BC_INST_ASSIGN_MINUS:
6567#endif 6566#endif
6568 case XC_INST_ASSIGN: 6567 case XC_INST_ASSIGN:
6569 dbg_exec("BC_INST_ASSIGNxyz:"); 6568 dbg_exec("BC_INST_ASSIGNxyz:");
6570 s = zbc_program_assign(inst); 6569 s = zbc_program_assign(inst);
6571 break; 6570 break;
6572#if ENABLE_DC 6571#if ENABLE_DC
6573 case DC_INST_POP_EXEC: 6572 case DC_INST_POP_EXEC:
6574 dbg_exec("DC_INST_POP_EXEC:"); 6573 dbg_exec("DC_INST_POP_EXEC:");
6575 bc_vec_pop(&G.prog.exestack); 6574 bc_vec_pop(&G.prog.exestack);
6576 goto read_updated_ip; 6575 goto read_updated_ip;
6577 case DC_INST_MODEXP: 6576 case DC_INST_MODEXP:
6578 dbg_exec("DC_INST_MODEXP:"); 6577 dbg_exec("DC_INST_MODEXP:");
6579 s = zdc_program_modexp(); 6578 s = zdc_program_modexp();
6580 break; 6579 break;
6581 case DC_INST_DIVMOD: 6580 case DC_INST_DIVMOD:
6582 dbg_exec("DC_INST_DIVMOD:"); 6581 dbg_exec("DC_INST_DIVMOD:");
6583 s = zdc_program_divmod(); 6582 s = zdc_program_divmod();
6584 break; 6583 break;
6585 case DC_INST_EXECUTE: 6584 case DC_INST_EXECUTE:
6586 case DC_INST_EXEC_COND: 6585 case DC_INST_EXEC_COND:
6587 dbg_exec("DC_INST_EXEC[_COND]:"); 6586 dbg_exec("DC_INST_EXEC[_COND]:");
6588 s = zdc_program_execStr(code, &ip->inst_idx, inst == DC_INST_EXEC_COND); 6587 s = zdc_program_execStr(code, &ip->inst_idx, inst == DC_INST_EXEC_COND);
6589 goto read_updated_ip; 6588 goto read_updated_ip;
6590 case DC_INST_PRINT_STACK: { 6589 case DC_INST_PRINT_STACK: {
6591 size_t idx; 6590 size_t idx;
6592 dbg_exec("DC_INST_PRINT_STACK:"); 6591 dbg_exec("DC_INST_PRINT_STACK:");
6593 for (idx = 0; idx < G.prog.results.len; ++idx) { 6592 for (idx = 0; idx < G.prog.results.len; ++idx) {
6594 s = zbc_program_print(XC_INST_PRINT, idx); 6593 s = zbc_program_print(XC_INST_PRINT, idx);
6595 if (s) break; 6594 if (s) break;
6596 }
6597 break;
6598 }
6599 case DC_INST_CLEAR_STACK:
6600 dbg_exec("DC_INST_CLEAR_STACK:");
6601 bc_vec_pop_all(&G.prog.results);
6602 break;
6603 case DC_INST_STACK_LEN:
6604 dbg_exec("DC_INST_STACK_LEN:");
6605 dc_program_stackLen();
6606 break;
6607 case DC_INST_DUPLICATE:
6608 dbg_exec("DC_INST_DUPLICATE:");
6609 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
6610 RETURN_STATUS(bc_error_stack_has_too_few_elements());
6611 ptr = bc_vec_top(&G.prog.results);
6612 dc_result_copy(&r, ptr);
6613 bc_vec_push(&G.prog.results, &r);
6614 break;
6615 case DC_INST_SWAP: {
6616 BcResult *ptr2;
6617 dbg_exec("DC_INST_SWAP:");
6618 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
6619 RETURN_STATUS(bc_error_stack_has_too_few_elements());
6620 ptr = bc_vec_item_rev(&G.prog.results, 0);
6621 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
6622 memcpy(&r, ptr, sizeof(BcResult));
6623 memcpy(ptr, ptr2, sizeof(BcResult));
6624 memcpy(ptr2, &r, sizeof(BcResult));
6625 break;
6626 }
6627 case DC_INST_ASCIIFY:
6628 dbg_exec("DC_INST_ASCIIFY:");
6629 s = zdc_program_asciify();
6630 break;
6631 case DC_INST_PRINT_STREAM:
6632 dbg_exec("DC_INST_PRINT_STREAM:");
6633 s = zdc_program_printStream();
6634 break;
6635 case DC_INST_LOAD:
6636 case DC_INST_PUSH_VAR: {
6637 bool copy = inst == DC_INST_LOAD;
6638 s = zbc_program_pushVar(code, &ip->inst_idx, true, copy);
6639 break;
6640 }
6641 case DC_INST_PUSH_TO_VAR: {
6642 char *name = bc_program_name(code, &ip->inst_idx);
6643 s = zbc_program_copyToVar(name, true);
6644 free(name);
6645 break;
6646 } 6595 }
6647 case DC_INST_QUIT: 6596 break;
6648 dbg_exec("DC_INST_QUIT:"); 6597 }
6649 if (G.prog.exestack.len <= 2) 6598 case DC_INST_CLEAR_STACK:
6650 QUIT_OR_RETURN_TO_MAIN; 6599 dbg_exec("DC_INST_CLEAR_STACK:");
6651 bc_vec_npop(&G.prog.exestack, 2); 6600 bc_vec_pop_all(&G.prog.results);
6652 goto read_updated_ip; 6601 break;
6653 case DC_INST_NQUIT: 6602 case DC_INST_STACK_LEN:
6654 dbg_exec("DC_INST_NQUIT:"); 6603 dbg_exec("DC_INST_STACK_LEN:");
6655 s = zdc_program_nquit(); 6604 dc_program_stackLen();
6656 //goto read_updated_ip; - just fall through to it 6605 break;
6606 case DC_INST_DUPLICATE:
6607 dbg_exec("DC_INST_DUPLICATE:");
6608 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
6609 RETURN_STATUS(bc_error_stack_has_too_few_elements());
6610 ptr = bc_vec_top(&G.prog.results);
6611 dc_result_copy(&r, ptr);
6612 bc_vec_push(&G.prog.results, &r);
6613 break;
6614 case DC_INST_SWAP: {
6615 BcResult *ptr2;
6616 dbg_exec("DC_INST_SWAP:");
6617 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
6618 RETURN_STATUS(bc_error_stack_has_too_few_elements());
6619 ptr = bc_vec_item_rev(&G.prog.results, 0);
6620 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
6621 memcpy(&r, ptr, sizeof(BcResult));
6622 memcpy(ptr, ptr2, sizeof(BcResult));
6623 memcpy(ptr2, &r, sizeof(BcResult));
6624 break;
6625 }
6626 case DC_INST_ASCIIFY:
6627 dbg_exec("DC_INST_ASCIIFY:");
6628 s = zdc_program_asciify();
6629 break;
6630 case DC_INST_PRINT_STREAM:
6631 dbg_exec("DC_INST_PRINT_STREAM:");
6632 s = zdc_program_printStream();
6633 break;
6634 case DC_INST_LOAD:
6635 case DC_INST_PUSH_VAR: {
6636 bool copy = inst == DC_INST_LOAD;
6637 s = zbc_program_pushVar(code, &ip->inst_idx, true, copy);
6638 break;
6639 }
6640 case DC_INST_PUSH_TO_VAR: {
6641 char *name = bc_program_name(code, &ip->inst_idx);
6642 s = zbc_program_copyToVar(name, true);
6643 free(name);
6644 break;
6645 }
6646 case DC_INST_QUIT:
6647 dbg_exec("DC_INST_QUIT:");
6648 if (G.prog.exestack.len <= 2)
6649 QUIT_OR_RETURN_TO_MAIN;
6650 bc_vec_npop(&G.prog.exestack, 2);
6651 goto read_updated_ip;
6652 case DC_INST_NQUIT:
6653 dbg_exec("DC_INST_NQUIT:");
6654 s = zdc_program_nquit();
6655 //goto read_updated_ip; - just fall through to it
6657#endif // ENABLE_DC 6656#endif // ENABLE_DC
6658 read_updated_ip: 6657 read_updated_ip:
6659 // Instruction stack has changed, read new pointers 6658 // Instruction stack has changed, read new pointers
6660 ip = bc_vec_top(&G.prog.exestack); 6659 ip = bc_vec_top(&G.prog.exestack);
6661 func = bc_program_func(ip->func); 6660 func = bc_program_func(ip->func);
6662 code = func->code.v; 6661 code = func->code.v;
6663 dbg_exec("func:%zd bytes:%zd ip:%zd", ip->func, func->code.len, ip->inst_idx); 6662 dbg_exec("func:%zd bytes:%zd ip:%zd", ip->func, func->code.len, ip->inst_idx);
6664 } 6663 }
6665 6664
6666 if (s || G_interrupt) { 6665 if (s || G_interrupt) {