Annotation of hatari/src/debug/evaluate.c, revision 1.1.1.4

1.1       root        1: /*
1.1.1.4 ! root        2:   Hatari - evaluate.c
1.1       root        3: 
1.1.1.4 ! root        4:   Copyright (C) 1994, 2009-2014 by Eero Tamminen
1.1       root        5: 
1.1.1.3   root        6:   This file is distributed under the GNU General Public License, version 2
                      7:   or at your option any later version. Read the file gpl.txt for details.
1.1       root        8: 
                      9:   calculate.c - parse numbers, number ranges and expressions. Supports
1.1.1.2   root       10:   most unary and binary operations. Parenthesis are used for indirect
                     11:   ST RAM value addressing.
                     12: 
1.1       root       13:   Originally based on code from my Clac calculator MiNT filter version.
                     14: */
                     15: const char Eval_fileid[] = "Hatari calculate.c : " __DATE__ " " __TIME__;
                     16: 
                     17: #include <ctype.h>
                     18: #include <limits.h>
                     19: #include <errno.h>
                     20: #include <stdio.h>
                     21: #include <stdlib.h>
                     22: #include <stdbool.h>
                     23: #include <SDL_types.h>
1.1.1.2   root       24: #include "breakcond.h"
1.1       root       25: #include "configuration.h"
                     26: #include "dsp.h"
                     27: #include "debugcpu.h"
                     28: #include "evaluate.h"
                     29: #include "main.h"
                     30: #include "m68000.h"
1.1.1.2   root       31: #include "stMemory.h"
1.1       root       32: #include "symbols.h"
                     33: 
                     34: /* define which character indicates which type of number on expression  */
1.1.1.2   root       35: #define PREFIX_BIN '%'                            /* binary decimal     */
                     36: #define PREFIX_DEC '#'                             /* normal decimal    */
                     37: #define PREFIX_HEX '$'                             /* hexadecimal       */
1.1       root       38: 
                     39: /* define error codes                                                   */
                     40: #define CLAC_EXP_ERR "No expression given"
                     41: #define CLAC_GEN_ERR "Syntax error"
                     42: #define CLAC_PAR_ERR "Mismatched parenthesis"
                     43: #define CLAC_DEF_ERR "Undefined result (1/0)"
                     44: #define CLAC_STK_ERR "Operation/value stack full"
                     45: #define CLAC_OVF_ERR "Overflow"
                     46: #define CLAC_OVR_ERR "Mode overflow"
                     47: #define CLAC_PRG_ERR "Internal program error"
                     48: 
                     49: /* define internal allocation sizes (should be enough ;-)              */
1.1.1.2   root       50: #define PARDEPTH_MAX   16              /* max. parenth. nesting depth  */
                     51: #define OSTACK_MAX     64              /* size of the operator stack   */
                     52: #define VSTACK_MAX     64              /* size of the value stack      */
1.1       root       53: 
                     54: /* operation with lowest precedence, used to finish calculations */
                     55: #define LOWEST_PREDECENCE '|'
                     56: 
                     57: /* globals + function identifier stack(s)                              */
                     58: static struct {
                     59:        const char *error;              /* global error code            */
                     60:        bool valid;                     /* value validation             */
                     61: } id = {0, 0};
                     62: 
                     63: /* parenthesis and function stacks                                     */
                     64: static struct {
                     65:        int idx;                        /* parenthesis level            */
                     66:        int max;                        /* maximum idx                  */
                     67:        int opx[PARDEPTH_MAX + 1];      /* current op index for par     */
                     68:        int vax[PARDEPTH_MAX + 1];      /* current val index for par    */
                     69: } par = {0, PARDEPTH_MAX, {0}, {0}};
                     70: 
                     71: static struct {                                        /* operator stack       */
                     72:        int idx;
                     73:        int max;
                     74:        char buf[OSTACK_MAX + 1];
                     75: } op = {0, OSTACK_MAX, ""};
                     76: 
                     77: static struct value_stk {                      /* value stack  */
                     78:        int idx;
                     79:        int max;
                     80:        long long buf[VSTACK_MAX + 1];
                     81: } val = {0, VSTACK_MAX, {0}};
                     82: 
                     83: /* -------------------------------------------------------------------- */
                     84: /* macros                                                              */
                     85: 
                     86: /* increment stack index and put value on stack (ie. PUSH)             */
                     87: #define PUSH(stk,val) \
                     88:        if((stk).idx < (stk).max) {             \
                     89:                (stk).idx += 1;                 \
                     90:                (stk).buf[(stk).idx] = (val);   \
                     91:        } else {                                \
                     92:                id.error = CLAC_STK_ERR;        \
                     93:        }
                     94: 
                     95: /* -------------------------------------------------------------------- */
                     96: /* declare subfunctions                                                        */
                     97: 
                     98: /* parse in-between operations */
                     99: static void operation(long long value, char op);
                    100: /* parse unary operators       */
                    101: static void unary (char op);
                    102: /* apply a prefix to a value */
                    103: static void apply_prefix(void);
                    104: /* juggle stacks, if possible  */
                    105: static void eval_stack(void);
                    106: /* operator -> operator level  */
                    107: static int get_level(int stk_offset);
                    108: /* evaluate operation          */
                    109: static long long apply_op(char op, long long x, long long y);
                    110: 
                    111: /* increase parenthesis level  */
                    112: static void open_bracket(void);
                    113: /* decrease parenthesis level  */
                    114: static long long close_bracket(long long x);
                    115: 
                    116: 
                    117: /**
                    118:  * Parse & set an (unsigned) number, assuming it's in the configured
                    119:  * default number base unless it has a prefix:
                    120:  * - '$' / '0x' / '0h' => hexadecimal
                    121:  * - '#' / '0d' => normal decimal
                    122:  * - '%' / '0b' => binary decimal
                    123:  * - '0o' => octal decimal
                    124:  * Return how many characters were parsed or zero for error.
                    125:  */
                    126: static int getNumber(const char *str, Uint32 *number, int *nbase)
                    127: {
                    128:        char *end;
1.1.1.3   root      129:        const char *start = str;
1.1       root      130:        int base = ConfigureParams.Debugger.nNumberBase;
                    131:        unsigned long int value;
                    132: 
                    133:        if (!str[0]) {
                    134:                fprintf(stderr, "Value missing!\n");
                    135:                return 0;
                    136:        }
                    137:        
                    138:        /* determine correct number base */
                    139:        if (str[0] == '0') {
                    140: 
                    141:                /* 0x & 0h = hex, 0d = dec, 0o = oct, 0b = bin ? */
                    142:                switch(str[1]) {
                    143:                case 'b':
                    144:                        base = 2;
                    145:                        break;
                    146:                case 'o':
                    147:                        base = 8;
                    148:                        break;
                    149:                case 'd':
                    150:                        base = 10;
                    151:                        break;
                    152:                case 'h':
                    153:                case 'x':
                    154:                        base = 16;
                    155:                        break;
                    156:                default:
                    157:                        str -= 2;
                    158:                }
                    159:                str += 2;
                    160:        }
1.1.1.4 ! root      161:        else if (!isxdigit((unsigned char)str[0])) {
1.1       root      162: 
                    163:                /* doesn't start with (hex) number -> is it prefix? */
                    164:                switch (*str++) {
                    165:                case PREFIX_BIN:
                    166:                        base = 2;
                    167:                        break;
                    168:                case PREFIX_DEC:
                    169:                        base = 10;
                    170:                        break;
                    171:                case PREFIX_HEX:
                    172:                        base = 16;
                    173:                        break;
                    174:                default:
                    175:                        fprintf(stderr, "Unrecognized number prefix in '%s'!\n", start);
                    176:                        return 0;
                    177:                }
                    178:        }
                    179:        *nbase = base;
                    180: 
                    181:        /* parse number */
                    182:        errno = 0;
                    183:        value = strtoul(str, &end, base);
                    184:        if (errno == ERANGE && value == LONG_MAX) {
                    185:                fprintf(stderr, "Overflow with value '%s'!\n", start);
                    186:                return 0;
                    187:        }
                    188:        if ((errno != 0 && value == 0) || end == str) {
                    189:                fprintf(stderr, "Invalid value '%s'!\n", start);
                    190:                return 0;
                    191:        }
                    192:        *number = value;
                    193:        return end - start;
                    194: }
                    195: 
                    196: 
                    197: /**
1.1.1.2   root      198:  * Parse unsigned register/symbol/number value and set it to "number"
                    199:  * and the number base used for parsing to "base".
1.1       root      200:  * Return how many characters were parsed or zero for error.
                    201:  */
1.1.1.2   root      202: static int getValue(const char *str, Uint32 *number, int *base, bool bForDsp)
1.1       root      203: {
                    204:        char name[64];
                    205:        const char *end;
                    206:        Uint32 mask, *addr;
1.1.1.2   root      207:        int len;
1.1       root      208: 
1.1.1.4 ! root      209:        for (end = str; *end == '_' || isalnum((unsigned char)*end); end++);
        !           210: 
1.1       root      211:        len = end-str;
                    212:        if (len >= (int)sizeof(name)) {
                    213:                fprintf(stderr, "ERROR: symbol name at '%s' too long (%d chars)\n", str, len);
                    214:                return 0;
                    215:        }
                    216:        memcpy(name, str, len);
                    217:        name[len] = '\0';
                    218: 
1.1.1.2   root      219:        *base = 0; /* no base (e.g. variable) */
                    220: 
                    221:        /* internal Hatari variable? */
                    222:        if (BreakCond_GetHatariVariable(name, number)) {
                    223:                return len;
                    224:        }
                    225: 
1.1       root      226:        if (bForDsp) {
1.1.1.3   root      227:                int regsize = DSP_GetRegisterAddress(name, &addr, &mask);
1.1       root      228:                /* DSP register or symbol? */
1.1.1.3   root      229:                switch (regsize) {
                    230:                case 16:
                    231:                        *number = (*((Uint16*)addr) & mask);
1.1       root      232:                        return len;
1.1.1.3   root      233:                case 32:
                    234:                        *number = (*addr & mask);
1.1       root      235:                        return len;
1.1.1.3   root      236:                default:
                    237:                        if (Symbols_GetDspAddress(SYMTYPE_ALL, name, number)) {
                    238:                                return len;
                    239:                        }
1.1       root      240:                }
                    241:        } else {
                    242:                /* a special case CPU register? */
                    243:                if (strcasecmp(name, "PC") == 0) {
                    244:                        *number = M68000_GetPC();
                    245:                        return len;
                    246:                }
                    247:                if (strcasecmp(name, "SR") == 0) {
                    248:                        *number = M68000_GetSR();
                    249:                        return len;
                    250:                }
                    251:                /* a normal CPU  register or symbol? */
                    252:                if (DebugCpu_GetRegisterAddress(name, &addr)) {
                    253:                        *number = *addr;
                    254:                        return len;
                    255:                }
                    256:                if (Symbols_GetCpuAddress(SYMTYPE_ALL, name, number)) {
                    257:                        return len;
                    258:                }
                    259:        }
                    260: 
                    261:        /* none of above, assume it's a number */
1.1.1.2   root      262:        return getNumber(str, number, base);
1.1       root      263: }
                    264: 
                    265: 
1.1.1.2   root      266: /* Check that number string is OK and isn't followed by unrecognized
                    267:  * character (last char char is zero). If not, complain about it.
1.1       root      268:  * Return true for success and false for failure.
                    269:  */
1.1.1.2   root      270: static bool isNumberOK(const char *str, int offset, int base)
1.1       root      271: {
1.1.1.2   root      272:        const char *basestr;
                    273: 
1.1       root      274:        if (!offset) {
                    275:                return false;
                    276:        }
1.1.1.2   root      277:        if (!str[offset]) {
                    278:                /* no extra chars after the parsed part */
                    279:                return true;
1.1       root      280:        }
1.1.1.2   root      281:        switch (base) {
                    282:        case 0:
                    283:                fprintf(stderr, "Name '%s' contains non-alphanumeric characters!\n", str);
1.1       root      284:                return false;
1.1.1.2   root      285:        case 2:
                    286:                basestr = "binary";
                    287:                break;
                    288:        case 8:
                    289:                basestr = "octal";
                    290:                break;
                    291:        case 10:
                    292:                basestr = "decimal";
                    293:                break;
                    294:        case 16:
                    295:                basestr = "hexadecimal";
                    296:                break;
                    297:        default:
                    298:                basestr = "unknown";
1.1       root      299:        }
1.1.1.2   root      300:        fprintf(stderr, "Extra characters in %s based number '%s'!\n", basestr, str);
                    301:        return false;
                    302: }
                    303: 
                    304: /**
                    305:  * Parse & set an (unsigned) number, assume it's in the configured
                    306:  * default number base unless it has a suitable prefix.
                    307:  * Return true for success and false for failure.
                    308:  */
                    309: bool Eval_Number(const char *str, Uint32 *number)
                    310: {
                    311:        int offset, base;
                    312:        /* TODO: add CPU/DSP flag and use getValue() instead of getNumber()
                    313:         * like getRange() does, so that user can use variable names in
                    314:         * addition to numbers.
                    315:         */
                    316:        offset = getNumber(str, number, &base);
1.1.1.4 ! root      317:        if (!offset)
        !           318:                return false;
        !           319:        else
        !           320:                return isNumberOK(str, offset, base);
1.1       root      321: }
                    322: 
                    323: 
                    324: /**
1.1.1.2   root      325:  * Parse an address range, eg. "$fa0000[-$fa0100]" or "pc[-a0]" and
                    326:  * output appropriate warnings if range or values are invalid.
                    327:  * Address can also be a register/variable/symbol name.
1.1       root      328:  * returns:
1.1.1.2   root      329:  * -1 if invalid address or range,
                    330:  *  0 if single address,
                    331:  * +1 if a range.
1.1       root      332:  */
1.1.1.2   root      333: int Eval_Range(char *str1, Uint32 *lower, Uint32 *upper, bool fordsp)
1.1       root      334: {
1.1.1.2   root      335:        int offset, base, ret;
1.1       root      336:        bool fDash = false;
                    337:        char *str2 = str1;
                    338: 
                    339:        while (*str2) {
                    340:                if (*str2 == '-') {
                    341:                        *str2++ = '\0';
                    342:                        fDash = true;
                    343:                        break;
                    344:                }
                    345:                str2++;
                    346:        }
                    347: 
1.1.1.2   root      348:        offset = getValue(str1, lower, &base, fordsp);
1.1.1.4 ! root      349:        if (offset == 0 || !isNumberOK(str1, offset, base)) {
1.1.1.2   root      350:                /* first number not OK */
                    351:                fprintf(stderr,"Invalid address value '%s'!\n", str1);
                    352:                ret = -1;
                    353:        } else {
                    354:                /* first number OK */
                    355:                ret = 0;
1.1       root      356:        }
1.1.1.2   root      357:        if (fDash) {
                    358:                offset = getValue(str2, upper, &base, fordsp);
1.1.1.4 ! root      359:                if (offset == 0 || !isNumberOK(str2, offset, base)) {
1.1.1.2   root      360:                        /* second number not OK */
                    361:                        fprintf(stderr, "Invalid address value '%s'!\n", str2);
                    362:                        ret = -1;
                    363:                } else {
                    364:                        if (*lower > *upper) {
                    365:                                fprintf(stderr,"Invalid range ($%x > $%x)!\n", *lower, *upper);
                    366:                                /* not a range */
                    367:                                ret = -1;
                    368:                        } else {
                    369:                                /* second number & range OK */
                    370:                                ret = 1;
                    371:                        }
                    372:                }
                    373:                *--str2 = '-';
                    374:        }
                    375:        return ret;
1.1       root      376: }
                    377: 
                    378: 
                    379: /**
                    380:  * Evaluate expression. bForDsp determines which registers and symbols
                    381:  * are interpreted. Sets given value and parsing offset.
                    382:  * Return error string or NULL for success.
                    383:  */
                    384: const char* Eval_Expression(const char *in, Uint32 *out, int *erroff, bool bForDsp)
                    385: {
                    386:        /* in    : expression to evaluate                               */
                    387:        /* out   : final parsed value                                   */
                    388:        /* value : current parsed value                                 */
                    389:        /* mark  : current character in expression                      */
                    390:        /* valid : expression validation flag, set when number parsed   */
                    391:        /* end   : 'expression end' flag                                */
                    392:        /* offset: character offset in expression                       */
                    393: 
                    394:        long long value;
1.1.1.2   root      395:        int dummy, offset = 0;
1.1       root      396:        char mark;
                    397:        
                    398:        /* Uses global variables:       */
                    399: 
                    400:        par.idx = 0;                    /* parenthesis stack pointer    */
                    401:        par.opx[0] = par.vax[0] = 0;    /* additional stack pointers    */
                    402:        op.idx = val.idx = -1;
                    403: 
                    404:        id.error = NULL;
                    405:        id.valid = false;               /* value validation             */
                    406:        value = 0;
                    407: 
                    408:        /* parsing loop, repeated until expression ends */
                    409:        do {
                    410:                mark = in[offset];
                    411:                switch(mark) {
                    412:                case '\0':
                    413:                        break;
                    414:                case ' ':
                    415:                case '\t':
                    416:                        offset ++;              /* jump over white space */
                    417:                        break;
                    418:                case '~':                       /* prefixes */
                    419:                        unary(mark);
                    420:                        offset ++;
                    421:                        break;
                    422:                case '>':                       /* operators  */
                    423:                case '<':
                    424:                        offset ++;
                    425:                        /* check that it's '>>' or '<<' */
                    426:                        if (in[offset] != mark)
                    427:                        {
                    428:                                id.error = CLAC_GEN_ERR;
                    429:                                break;
                    430:                        }
                    431:                        operation (value, mark);
                    432:                        offset ++;
                    433:                        break;
                    434:                case '|':
                    435:                case '&':
                    436:                case '^':
                    437:                case '+':
                    438:                case '-':
                    439:                case '*':
                    440:                case '/':
                    441:                        operation (value, mark);
                    442:                        offset ++;
                    443:                        break;
                    444:                case '(':
                    445:                        open_bracket ();
                    446:                        offset ++;
                    447:                        break;
                    448:                case ')':
                    449:                        value = close_bracket (value);
                    450:                        offset ++;
                    451:                        break;
                    452:                default:
                    453:                        /* register/symbol/number value needed? */
                    454:                        if (id.valid == false) {
                    455:                                Uint32 tmp;
                    456:                                int consumed;
1.1.1.2   root      457:                                consumed = getValue(&(in[offset]), &tmp, &dummy, bForDsp);
1.1       root      458:                                /* number parsed? */
                    459:                                if (consumed) {
                    460:                                        offset += consumed;
                    461:                                        id.valid = true;
                    462:                                        value = tmp;
                    463:                                        break;
                    464:                                }
                    465:                        }
                    466:                        id.error = CLAC_GEN_ERR;
                    467:                }
                    468: 
                    469:        /* until exit or error message                                  */
                    470:        } while(mark && !id.error);
                    471: 
                    472:         /* result of evaluation                                        */
                    473:         if (val.idx >= 0)
                    474:                *out = val.buf[val.idx];
                    475: 
                    476:        /* something to return?                                         */
                    477:        if (!id.error) {
                    478:                if (id.valid) {
                    479: 
                    480:                        /* evaluate rest of the expression              */
                    481:                        operation (value, LOWEST_PREDECENCE);
                    482:                        if (par.idx)                    /* mismatched   */
                    483:                                id.error = CLAC_PAR_ERR;
                    484:                        else                            /* result out   */
                    485:                                *out = val.buf[val.idx];
                    486: 
                    487:                } else {
                    488:                        if ((val.idx < 0) && (op.idx < 0)) {
                    489:                                id.error = CLAC_EXP_ERR;
                    490:                        } else                  /* trailing operators   */
                    491:                                id.error = CLAC_GEN_ERR;
                    492:                }
                    493:        }
                    494: 
                    495:        *erroff = offset;
                    496:        if (id.error) {
                    497:                *out = 0;
                    498:                return id.error;
                    499:        }
                    500:        return NULL;
                    501: }
                    502: 
                    503: 
                    504: /* ==================================================================== */
                    505: /*                     expression evaluation                           */
                    506: /* ==================================================================== */
                    507: 
                    508: static void operation (long long value, char oper)
                    509: {
                    510:        /* uses globals par[], id.error[], op[], val[]
                    511:         * operation executed if the next one is on same or lower level
                    512:         */
                    513:        /* something to calc? */
                    514:        if(id.valid == true) {
                    515:                
                    516:                /* add new items to stack */
                    517:                PUSH(op, oper);
                    518:                PUSH(val, value);
                    519:                
                    520:                /* more than 1 operator  */
                    521:                if(op.idx > par.opx[par.idx]) {
                    522: 
                    523:                        /* but only one value */
                    524:                        if(val.idx == par.vax[par.idx]) {
                    525:                                apply_prefix();
                    526:                        } else {
                    527:                                /* evaluate all possible operations */
                    528:                                eval_stack();
                    529:                        }
                    530:                }
                    531:                /* next a number needed */
                    532:                id.valid = false;
                    533:        } else {
                    534:                /* pre- or post-operators instead of in-betweens? */
                    535:                unary(oper);
                    536:        }
                    537: }
                    538: 
                    539: /**
                    540:  * handle unary operators
                    541:  */
                    542: static void unary (char oper)
                    543: {
                    544:        /* check pre-value operators
                    545:         * have to be parenthesised
                    546:         */
                    547:        if(id.valid == false && op.idx < par.opx[par.idx])
                    548:        {
                    549:                switch(oper) {
                    550:                case '+':               /* not needed */
                    551:                        break;
                    552:                case '-':
                    553:                case '~':
                    554:                        PUSH(op, oper);
                    555:                        break;
                    556:                default:
                    557:                        id.error = CLAC_GEN_ERR;
                    558:                }
                    559:        }
                    560:        else
                    561:                id.error = CLAC_GEN_ERR;
                    562: }
                    563: 
                    564: /**
                    565:  * apply a prefix to the current value
                    566:  */
                    567: static void apply_prefix(void)
                    568: {
                    569:        long long value = val.buf[val.idx];
                    570: 
                    571:        op.idx--;
                    572:        switch(op.buf[op.idx]) {
                    573:        case '-':
                    574:                value = (-value);
                    575:                break;
                    576:        case '~':
                    577:                value = (~value);
                    578:                break;
                    579:        default:
                    580:                id.error = CLAC_PRG_ERR;
                    581:        }
                    582:        val.buf[val.idx] = value;
                    583:        op.buf[op.idx] = op.buf[op.idx + 1];
                    584: }
                    585: 
                    586: /* -------------------------------------------------------------------- */
                    587: /**
                    588:  * evaluate operators if precedence allows it
                    589:  */
                    590: /* evaluate all possible (according to order of precedence) operators  */
                    591: static void eval_stack (void)
                    592: {
                    593:        /* uses globals par[], op[], val[]      */
                    594: 
                    595:        /* # of operators >= 2 and prev. op-level >= current op-level ? */
                    596:        while ((op.idx > par.opx[par.idx]) && get_level (-1) >= get_level (0)) {
                    597: 
                    598:                /* shorten value stacks by one  */
                    599:                /* + calculate resulting value  */
                    600:                op.idx -= 1;
                    601:                val.idx -= 1;
                    602:                val.buf[val.idx] = apply_op(op.buf[op.idx],
                    603:                        val.buf[val.idx], val.buf[val.idx + 1]);
                    604: 
                    605:                /* pull the just used operator out of the stack         */
                    606:                op.buf[op.idx] = op.buf[op.idx + 1];
                    607:        }
                    608: }
                    609: 
                    610: /* -------------------------------------------------------------------- */
                    611: /**
                    612:  * return the precedence level of a given operator
                    613:  */
                    614: static int get_level (int offset)
                    615: {
                    616:        /* used globals par[], op[]
                    617:         * returns operator level of: operator[stack idx + offset]
                    618:         */
                    619:        switch(op.buf[op.idx + offset]) {
                    620:        case '|':      /* binary operations  */
                    621:        case '&':
                    622:        case '^':
                    623:                return 0;
                    624:                
                    625:        case '>':      /* bit shifting    */
                    626:        case '<':
                    627:                return 1;
                    628:                
                    629:        case '+':
                    630:        case '-':
                    631:                return 2;
                    632:                
                    633:        case '*':
                    634:        case '/':
                    635:                return 3;
                    636:                
                    637:        default:
                    638:                id.error = CLAC_PRG_ERR;
                    639:        }
                    640:        return 6;
                    641: }
                    642: 
                    643: /* -------------------------------------------------------------------- */
                    644: /**
                    645:  * apply operator to given values, return the result
                    646:  */
                    647: static long long apply_op (char opcode, long long value1, long long value2)
                    648: {
                    649:        /* uses global id.error[]               */
                    650:        /* returns the result of operation      */
                    651: 
                    652:        switch (opcode) {
                    653:         case '|':
                    654:                value1 |= value2;
                    655:                break;
                    656:         case '&':
                    657:                value1 &= value2;
                    658:                break;
                    659:         case '^':
                    660:                value1 ^= value2;
                    661:                break;
                    662:         case '>':
                    663:                value1 >>= value2;
                    664:         case '<':
                    665:                value1 <<= value2;
                    666:                break;
                    667:        case '+':
                    668:                value1 += value2;
                    669:                break;
                    670:        case '-':
                    671:                value1 -= value2;
                    672:                break;
                    673:        case '*':
                    674:                value1 *= value2;
                    675:                break;
                    676:        case '/':
                    677:                /* don't divide by zero */
                    678:                if (value2)
                    679:                        value1 /= value2;
                    680:                else
                    681:                        id.error = CLAC_DEF_ERR;
                    682:                break;
                    683:         default:
                    684:                id.error = CLAC_PRG_ERR;
                    685:        }
                    686:        return value1;                          /* return result        */
                    687: }
                    688: 
                    689: 
                    690: /* ==================================================================== */
                    691: /*                     parenthesis and help                            */
                    692: /* ==================================================================== */
                    693: 
                    694: /**
1.1.1.4 ! root      695:  * open parenthesis, push values & operators to stack
1.1       root      696:  */
                    697: static void open_bracket (void)
                    698: {
                    699:        if (id.valid == false) {                /* preceded by operator */
                    700:                if (par.idx < PARDEPTH_MAX) {   /* not nested too deep  */
                    701:                        par.idx ++;
                    702:                        par.opx[par.idx] = op.idx + 1;
                    703:                        par.vax[par.idx] = val.idx + 1;
                    704:                } else
                    705:                        id.error = CLAC_STK_ERR;
                    706:        } else
                    707:                id.error = CLAC_GEN_ERR;
                    708: }
                    709: 
                    710: /* -------------------------------------------------------------------- */
                    711: /**
1.1.1.4 ! root      712:  * close parenthesis, and evaluate / pop stacks
1.1       root      713:  */
                    714: /* last parsed value, last param. flag, trigonometric mode     */
                    715: static long long close_bracket (long long value)
                    716: {
                    717:        /* returns the value of the parenthesised expression    */
                    718: 
                    719:        if (id.valid) {                 /* preceded by an operator      */
1.1.1.4 ! root      720:                if (par.idx > 0) {      /* parenthesis has a pair       */
1.1.1.2   root      721:                        Uint32 addr;
                    722: 
1.1       root      723:                        /* calculate the value of parenthesised exp.    */
                    724:                        operation (value, LOWEST_PREDECENCE);
1.1.1.2   root      725:                        /* fetch the indirect ST RAM value */
                    726:                        addr = val.buf[val.idx];
                    727:                        value = STMemory_ReadLong(addr);
                    728:                        fprintf(stderr, "  value in RAM at ($%x).l = $%llx\n", addr, value);
                    729:                        /* restore state before parenthesis */
                    730:                        op.idx = par.opx[par.idx] - 1;
1.1       root      731:                        val.idx = par.vax[par.idx] - 1;
                    732:                        par.idx --;
                    733: 
                    734:                        /* next operator */
                    735:                        id.valid = true;
                    736:                } else
                    737:                        id.error = CLAC_PAR_ERR;
                    738:        } else
                    739:                id.error = CLAC_GEN_ERR;
                    740: 
                    741:        return value;
                    742: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.