Language: C++

/*
 * by purple
 * at 12-03-06 7:13:04 PM
 */

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <string>
#include <sstream>
#include <deque>
#include <map>

using namespace std;

#define sz(x) ((int)((x).size()))
#define out(x) printf(#x" %d\n", x)
#define rep(i,n) for(int i=0;i<(n);++i)
#define repf(i,a,b) for(int i=(a);i<=(b);++i)

template<class T>
struct arrayBase {
    deque<T> val;

    void clear() {
        val.clear();
    }
    void insert(T x) {
        val.push_back(x);
    }
	void pop(int tms) {
		rep (i, tms) {
			val.pop_front();
		}
	}
};

int arOperation(int lhs, int rhs, int op) {
    switch (op) {
        case 0: return lhs + rhs;
        case 1: return lhs - rhs;
        case 2: return lhs * rhs;
    }
}

template<class T>
arrayBase<T> arOperation(const arrayBase<T>& lhs, const arrayBase<T>& rhs, int op) {
    arrayBase<T> res = lhs;
    rep (i, sz(res.val)) {
        res.val[i] = arOperation(res.val[i], rhs.val[i], op);
    }
    return res;
}

template<class T>
arrayBase<T> arOperation(const arrayBase<T>& lhs, int x, int op) {
    arrayBase<T> res = lhs;
    rep (i, sz(res.val)) {
        res.val[i] = arOperation(res.val[i], x, op);
    }
    return res;
}

template<class T>
T selfOperation(const arrayBase<T>& lhs, int op) {
    T res = lhs.val[sz(lhs.val) - 1];
    for (int k = sz(lhs.val) - 2; k >= 0; --k) {
        res = arOperation(lhs.val[k], res, op);
    }
    return res;
}

typedef arrayBase<int> array1D;
typedef arrayBase<array1D> array2D;
typedef arrayBase<array2D> array3D;

void show(array1D x) {
    rep (i, sz(x.val)) {
        printf ("%d%c", x.val[i], i == sz(x.val) - 1? '\n': ' ');
    }
}

void show(array2D x) {
    rep (i, sz(x.val)) {
        show(x.val[i]);
    }
}

void show(array3D x) {
    rep (i, sz(x.val)) {
        show(x.val[i]);
        if (i != sz(x.val) - 1) puts ("");
    }
}

const int INV = 65536;
const string NOW = "ISEA_";
const string allop[8] = 
    { "+", "-", "*", "/", "=", "itoa", "drop", "rho" };

map<string, int> vartype;
map<string, array1D> m1d;
map<string, array2D> m2d;
map<string, array3D> m3d;

int getType(string name, int type = 0) {
    if (vartype.find(name) == vartype.end()) {
        vartype[name] = type;
    }
    return vartype[name];
}

int toInt(string name) {
    if (vartype.find(name) == vartype.end()) {
        return INV;
    }
    if (vartype[name] == 1 && sz(m1d[name].val) == 1) {
        return m1d[name].val[0];
    }
    return INV;
}

void show(string name) {
    int type = getType(name);
    switch (type) {
        case 0: show(m1d[name]); break;
        case 1: show(m2d[name]); break;
        case 2: show(m3d[name]); break;
    }
}

void insert(string name, array1D x) {
    vartype[name] = 1;
    m1d[name] = x;
}

void insert(string name, array2D x) {
    vartype[name] = 2;
    m2d[name] = x;
}

void insert(string name, array3D x) {
    vartype[name] = 3;
    m3d[name] = x;
}

array1D join(array1D lhs, array1D rhs) {
	array1D res = lhs;
	rep (i, sz(rhs.val)) {
		res.insert(rhs.val[i]);
	}
	return res;
}

array1D convert1D(array2D x) {
	array1D res = x.val[0];
	repf (i, 1, sz(x.val) - 1) {
		res = join(res, x.val[i]);
	}
	return res;
}

array1D convert1D(array3D x) {
	array1D res = convert1D(x.val[0]);
	repf (i, 1, sz(x.val) - 1) {
		res = join(res, convert1D(x.val[i]));
	}
	return res;
}

vector<string> all;
char s[88], TMS = 'A';

bool isDigit(string x) {
    rep (i, sz(x)) {
        if (i == 0 && x[i] == '-') {
            continue;
        }
        if (x[i] < '0' || x[i] > '9') {
            return false;
        }
    }
    return true;
}

int toDigit(string x) {
    int res = 0, sig = 1;
    rep (i, sz(x)) {
        if (i == 0 && x[i] == '-') {
            sig = -1;
            continue;
        }
        res = res * 10 + x[i] - '0';
    }
    return res * sig;
}

int getOpType(string x) {
    rep (i, 8) {
        if (x == allop[i]) {
            return i;
        }
    }
    return -1;
}

string getArray1D(int l, int r) {
    array1D tmp;
    tmp.clear();
    repf (i, l, r) {
        tmp.insert(toDigit(all[i]));
    }
    string namenow = NOW + TMS++;
    insert(namenow, tmp);
    return namenow;
}

string arOperation(string lhs, int x, int optype) {
    string namenow = NOW + TMS++;
    int type = getType(lhs);
    switch (type) {
        case 0: insert(namenow, arOperation(m1d[lhs], x, optype)); break;
        case 1: insert(namenow, arOperation(m2d[lhs], x, optype)); break;
        case 2: insert(namenow, arOperation(m3d[lhs], x, optype)); break;
    }
    return namenow;
}

string arOperation(string lhs, string rhs, int optype) {
    string namenow = NOW + TMS++;
    int type = getType(lhs);
    switch (type) {
        case 0: insert(namenow, arOperation(m1d[lhs], m1d[rhs], optype)); break;
        case 1: insert(namenow, arOperation(m2d[lhs], m2d[lhs], optype)); break;
        case 2: insert(namenow, arOperation(m3d[lhs], m3d[lhs], optype)); break;
    }
    return namenow;
}

array1D rhoOperation(int d1, array1D x, int beginID = 0) {
	array1D res;
	res.clear();
	int arraysz = sz(x.val);
	rep (i, d1) {
		res.insert(x.val[(i + beginID) % arraysz]);
	}
	return res;
}

array2D rhoOperation(int d1, int d2, array1D x, int beginID = 0) {
	array2D res;
	res.clear();
	int arraysz = sz(x.val);
	rep (i, d1) {
		res.insert(rhoOperation(d2, x, (beginID + i * d2) % arraysz));
	}
	return res;
}

array3D rhoOperation(int d1, int d2, int d3, array1D x) {
	array3D res;
	res.clear();
	int arraysz = sz(x.val);
	rep (i, d1) {
		res.insert(rhoOperation(d2, d3, x, (i * d2 * d3) % arraysz));
	}
	return res;
}

string rhoOperation(array1D lhs, array1D x) {
    string namenow = NOW + TMS++;
    int rhosz = sz(lhs.val);
    switch (rhosz) {
		case 0: insert(namenow, rhoOperation(lhs.val[0], x)); break;
		case 1: insert(namenow, rhoOperation(lhs.val[0], lhs.val[1], x)); break;
		case 2: insert(namenow, rhoOperation(lhs.val[0], lhs.val[1], lhs.val[2], x)); break;
	}
	return namenow;
}

string doOperation(string lhs, string rhs, int optype) {
    if (optype >= 0 && optype <= 2) {
        int tmpInt = toInt(lhs);
        if (tmpInt != INV) {
            return arOperation(rhs, tmpInt, optype);
        }
        tmpInt = toInt(rhs);
        if (tmpInt != INV) {
            return arOperation(lhs, tmpInt, optype);
        }
        return arOperation(lhs, rhs, optype);
    }
    if (optype == 4) {
        int type = getType(rhs);
        switch (type) {
            case 0: insert(lhs, m1d[rhs]); break;
            case 1: insert(lhs, m2d[rhs]); break;
            case 2: insert(lhs, m3d[rhs]); break;
        }
        return lhs;
    }
    if (optype == 6) {
        int dropNum = m1d[lhs].val[0];
        int type = getType(rhs);
        switch (type) {
            case 0: m1d[rhs].pop(dropNum); break;
            case 1: m2d[rhs].pop(dropNum); break;
            case 2: m3d[rhs].pop(dropNum); break;
		}
		return rhs;
    }
	if (optype == 7) {
		array1D tmp;
        int type = getType(rhs);
		switch (type) {
			case 0: tmp = m1d[rhs]; break;
			case 1: tmp = convert1D(m2d[rhs]); break;
			case 2: tmp = convert1D(m3d[rhs]); break;
		}
		return rhoOperation(m1d[lhs], tmp);
	}
}

string selfOperation(string name, int optype) {
    string namenow = NOW + TMS++;
    int type = getType(name);
    if (type == 1) {
        array1D tmp;
        tmp.clear();
        tmp.insert(selfOperation(m1d[name], optype));
        insert(namenow, tmp);
    } else if (type == 2) {
        insert(namenow, selfOperation(m2d[name], optype));
    } else if (type == 3) {
        insert(namenow, selfOperation(m3d[name], optype));
    }
    return namenow;
}

string itoaOperation(int x) {
    string namenow = NOW + TMS++;
    array1D tmp;
    tmp.clear();
    repf (i, 1, x) {
        tmp.insert(i);
    }
    insert(namenow, tmp);
    return namenow;
}

string APLparser(int l, int r) {
    int id = l;
    string tmp;
    
    int firstOp = getOpType(all[r]);
    if (firstOp >= 0 && firstOp <= 2) {
        tmp = APLparser(l + 2, r);
        return selfOperation(tmp, firstOp);
    } 
    if (firstOp == 5) {
        id = l + 2;
        tmp = itoaOperation(toInt(all[l + 1]));
    } else if (isDigit(all[l])) {
        for (; id <= r && isDigit(all[id]); ++id) ;
        tmp = getArray1D(l, id - 1);
    } else if (all[l] == "(") {
        int cnt = 1;
        for (; id <= r && cnt != 0; ++id) {
            if (all[id] == "(") ++cnt;
            else if (all[id] == ")") --cnt;
        }
        tmp = APLparser(l + 1, id - 1);
    } else {
        id = l + 1;
        tmp = all[l];
    }
    if (id <= r) {
        int optype = getOpType(all[id]);
        string tmp2 = APLparser(id + 1, r);
        return doOperation(tmp, tmp2, optype);
    }
    return tmp;
}

void APLparser() {
    all.clear();
    string str(s), buf;
    stringstream ss(str);
    while (ss >> buf) {
        all.push_back(buf);
    }
    TMS = 'A';
    show(APLparser(0, sz(all) - 1));
}

int main() {
    int Case = 1;
    while (gets(s), s[0] != '#') {
        printf ("Case %d: %s\n", Case++, s);
        APLparser();
    }
    return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:399:12: error: 'gets' was not declared in this scope; did you mean 'getw'?
  399 |     while (gets(s), s[0] != '#') {
      |            ^~~~
      |            getw
a.cc: In function 'int arOperation(int, int, int)':
a.cc:45:1: warning: control reaches end of non-void function [-Wreturn-type]
   45 | }
      | ^
a.cc: In function 'std::string doOperation(std::string, std::string, int)':
a.cc:323:1: warning: control reaches end of non-void function [-Wreturn-type]
  323 | }
      | ^