File size: 3,093 Bytes
ae4627d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
'use strict';

function half (arg) {
    const name = arg.name;
    const width = arg.width;
    return [`${name}[${width / 2 - 1}:0]`, `${name}[${width - 1}:${width / 2}]`];
}

function signed (op) {
    return function (args) {
        const args2 = (args.length === 1) ? half(args[0]) : args.map(e => e.name);
        return '(' + args2.map(e => '$signed(' + e + ')').join(' ' + op + ' ') + ')';
    };
}

function logical (op) {
    return function (args) {
        const args2 = (args.length === 1) ? half(args[0]) : args.map(e => e.name);
        return '(' + args2.join(' ' + op + ' ') + ')';
    };
}

module.exports = {

    /*
    **   exponentiation,  numeric ** integer,  result numeric
    abs  absolute value,  abs numeric,  result numeric
    not  complement,      not logic or boolean,  result same

    *    multiplication,  numeric * numeric,  result numeric
    /    division,        numeric / numeric,  result numeric
    mod  modulo,          integer mod integer,  result integer
    rem  remainder,       integer rem integer,  result integer

    +    unary plus,      + numeric,  result numeric
    -    unary minus,     - numeric,  result numeric

    +    addition,        numeric + numeric,  result numeric
    -    subtraction,     numeric - numeric,  result numeric
    &    concatenation,   array or element & array or element,
                            result array
    */

    add:  signed('+'),
    '+':  signed('+'),
    sub:  signed('-'),
    '-':  signed('-'),
    mul:  signed('*'),
    '*':  signed('*'),

    sll:  logical('<<'), // shift left logical,     logical array sll integer,  result same
    srl:  logical('>>>'), // shift right logical,    logical array srl integer,  result same
    sla:  logical('<<'), // shift left arithmetic,  logical array sla integer,  result same
    sra:  logical('>>'), // shift right arithmetic, logical array sra integer,  result same
    rol:  logical('rol'), // rotate left,            logical array rol integer,  result same
    ror:  logical('ror'), // rotate right,           logical array ror integer,  result same

    /*
    =    test for equality, result is boolean
    /=   test for inequality, result is boolean
    <    test for less than, result is boolean
    <=   test for less than or equal, result is boolean
    >    test for greater than, result is boolean
    >=   test for greater than or equal, result is boolean
    */

    and:  logical('&'), // logical and,                logical array or boolean,  result is same
    or:   logical('|'), // logical or,                 logical array or boolean,  result is same
    nand: logical('&'), // logical complement of and,  logical array or boolean,  result is same
    nor:  logical('|'), // logical complement of or,   logical array or boolean,  result is same
    xor:  logical('^'), // logical exclusive or,       logical array or boolean,  result is same
    xnor: logical('^'),  // logical complement of exclusive or,  logical array or boolean,  result is same

    concat: args => '{' + args.reverse().map(e => e.name).join(', ') + '}'
};