#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <stack>
using namespace std;
/*
* Kawashiro Industry Program Compiler and Interpreter (KIPI)
* (c) 1998-2022 Nitori Kawashiro
* 2022-2024 Seabird Starch Gunnhildr.
*
* All rights reserved.
*/
#define DEBUG_LEVEL 0
#ifdef ONLINE_JUDGE
#define NO_WARNING_CALL 1
#else
#define NO_WARNING_CALL 0
#endif
// Utility function.
void inhale() {
char fch;
while ((fch = cin.get()) != '\n');
}
vector<string> split(string data, char spl) {
vector<string> result;
string tmp = "";
for (auto &i : data) {
if (i == spl) {
if (tmp.length()) result.push_back(tmp); // Ignore continuous splitter
tmp = "";
} else {
tmp += i;
}
}
if (tmp.length()) result.push_back(tmp);
return result;
}
string lowercase(string x) {
for (auto &i : x) if (isupper(i)) i = i - 'A' + 'a';
return x;
}
string strip(string x) {
bool ok = false;
string tmp = "";
for (auto &i : x) {
if (i != ' ') {
ok = true;
}
if (ok) tmp += i;
}
while (tmp.length() && ((*tmp.rbegin()) == ' ')) tmp.pop_back();
return tmp;
}
// Memory allocation.
constexpr int MAX_ARRAY_SIZE = 4194304, MAX_STACK_SIZE = 524288;
constexpr int MAX_COMMAND_LEN = 50005;
int memory[MAX_ARRAY_SIZE];
// Register.
int regs[15];
#define R(x) (x+1)
#define E(x) (x+5)
#define Flag 10
#define Val 11
#define Ret 12
#define Line 13 /* Line register for current line id. SHOULD BE DYNAMICALLY UPDATED */
//#define DebugLevel 25
// Expected *Complier hole*:
// TODO: 'Current-instruction counter' support.
// Execution pointer.
// LineID starts from 1 b ut SeqID starts from 0. The latter
// allows direct refering.
// The size is actually larger
struct stack_data {
int lineid, seqid, reg;
stack_data() {
}
stack_data(int lineid, int seqid, int reg) {
}
};
// When coming back, obviously need "next-jumper" !
stack_data current; // Current execution pointer
//stack_data stk[MAX_STACK_SIZE];
stack<stack_data> stk;
void toggle_error(int lineid, int errid, string description = "Critical System Error") {
cerr << "Error " << errid << " at " << lineid << ": " << description << endl;
exit(errid);
}
void toggle_warning(int lineid, int errid, string description = "Critical System Error") {
#if (!NO_WARNING_CALL)
cerr << "Warning " << errid << " at " << lineid << ": " << description << endl;
#endif
}
#define assume(condit) if (!condit) toggle_error(-1, __LINE__, #condit)
#define assume_p(condit,line) if (!condit) toggle_error(line, __LINE__, #condit)
int get_compiler_constant(int id) {
assume(id == Line);
if (id == Line) return current.lineid;
else return 0;
}
map<string, int> func_refer; // Directly store names
// All lowercased.
struct var {
enum vartype {
constant, reg, static_mem, dynamic_mem, compiler
} varstate;
int vardata;
var() {
}
var(string code) {
code = lowercase(code);
if (!code.length()) return;
bool dyn = code.length() > 2 && code[0] == '@' && code[1] == '%';
if (code[0] == '%' || (dyn)) {
if (dyn) {
varstate = vartype::dynamic_mem;
code = code.substr(2);
} else {
varstate = vartype::reg;
code = code.substr(1);
}
if (code[0] == 'R' || code[0] == 'r') vardata = R(code[1] - '1');
else if (code[0] == 'E' || code[0] == 'e') vardata = E(code[1] - '1');
else if (code == "flag") vardata = Flag;
else if (code == "val") vardata = Val;
else if (code == "ret") vardata = Ret;
else if (code == "line") vardata = Line;
else {
toggle_error(-1, __LINE__, "Invalid register");
}
} else if (code[0] == '@') {
assume(code.length() >= 2);
varstate = vartype::static_mem;
vardata = atoi(code.substr(1).c_str());
} else if (code[0] == '#') {
if (code == "#line") {
varstate = vartype::compiler;
vardata = Line;
} else toggle_error(-1, __LINE__, "Invalid compiler constant");
} else {
varstate = vartype::constant;
vardata = atoi(code.c_str());
}
}
var(vartype vt, int vd) : varstate(vt), vardata(vd) {
}
int read() {
switch (varstate) {
case constant:
return vardata;
case reg:
return regs[vardata];
case static_mem:
return memory[vardata];
case dynamic_mem:
return memory[regs[vardata]];
case compiler:
return get_compiler_constant(vardata);
default:
return 0;
}
}
void write(int data) {
switch (varstate) {
case reg:
regs[vardata] = data;
break;
case static_mem:
memory[vardata] = data;
break;
case dynamic_mem:
memory[regs[vardata]] = data;
break;
default:
toggle_warning(-1, __LINE__, "Attempt to write to constant");
break;
}
}
};
var v_flag = var("%flag"), v_ret = var("%ret"), v_val = var("%val"), v_line = var("%line");
struct instruct {
enum instype {
i_func, /* function $xx, in preprocessor */
i_fcall, /* callfunc $xx, in preprocessor */
i_udef, /* udef */
i_hlt, /* hlt */
i_nop, /* nop */
i_set, /* set [0] [1], let [1] be [0] */
i_jmp, /* jmp [0] */
i_jif, /* jif [0] <1> */
i_call, /* call [0] */
i_ret, /* ret <1> */
i_inv,
i_add,
i_sub, /* minus */
i_mult,
i_idiv,
i_mod,
i_lsft,
i_rsft,
i_band,
i_bor,
i_bxor,
i_lgr, /* > */
i_lls, /* < */
i_lge, /* >= */
i_lle, /* <= */
i_leql, /* == */
i_land, /* && */
i_lor, /* || */
i_rint,
i_rch,
i_wint,
i_wch
} me = i_udef;
bool has_expanded = false;
var p1, p2, p3;
string special = "";
instruct() {
}
instruct(string code) {
code = lowercase(code);
vector<string> spl = split(code, ' ');
if (spl.size() < 1) return;
if (spl[0] == "function") {
me = i_func;
assume(spl.size() >= 2);
special = spl[1];
} else if (spl[0] == "callfunc") {
me = i_fcall;
assume(spl.size() >= 2);
special = spl[1];
} else if (spl[0] == "udef") {
me = i_udef;
} else if (spl[0] == "hlt") {
me = i_hlt;
} else if (spl[0] == "nop") {
me = i_nop;
} else if (spl[0] == "set") {
assume(spl.size() >= 3);
me = i_set;
p1 = var(spl[1]); p2 = var(spl[2]);
} else if (spl[0] == "jmp") {
assume(spl.size() >= 2);
me = i_jmp;
p1 = var(spl[1]);
} else if (spl[0] == "jif") {
me = i_jif;
if (spl.size() == 2) {
p1 = var(spl[1]); p2 = v_flag;
} else if (spl.size() >= 3) {
has_expanded = true;
p1 = var(spl[1]); p2 = var(spl[2]);
} else {
toggle_error(-1, __LINE__, "Incorrect JIF");
}
} else if (spl[0] == "call") {
assume(spl.size() >= 2);
me = i_call;
p1 = var(spl[1]);
} else if (spl[0] == "ret") {
me = i_ret;
if (spl.size() >= 2) {
has_expanded = true;
p1 = var(spl[1]);
}
} else if (spl[0] == "inv") {
me = i_inv;
assume(spl.size() >= 2);
if (spl.size() >= 2) {
has_expanded = true;
p1 = var(spl[1]); p2 = var(spl[2]);
} else {
p1 = var(spl[1]); p2 = v_val;
}
}
#define arith_exec_pre(name, term) if (spl[0] == #name) { \
me = i_##name; assume(spl.size() >= 3); \
p1 = var(spl[1]), p2 = var(spl[2]); \
if (spl.size() > 3) has_expanded = true, p3 = var(spl[3]); \
else p3 = term; \
}
// For %Val:
arith_exec_pre(add,v_val);
arith_exec_pre(sub,v_val);
arith_exec_pre(mult,v_val);
arith_exec_pre(idiv,v_val);
arith_exec_pre(mod,v_val);
arith_exec_pre(lsft,v_val);
arith_exec_pre(rsft,v_val);
arith_exec_pre(band,v_val);
arith_exec_pre(bor,v_val);
arith_exec_pre(bxor,v_val);
// For %Flag:
arith_exec_pre(lgr,v_flag);
arith_exec_pre(lls,v_flag);
arith_exec_pre(lle,v_flag);
arith_exec_pre(leql,v_flag);
arith_exec_pre(land,v_flag);
arith_exec_pre(lor,v_flag);
if (spl[0] == "rint") {
me = i_rint;
// assume(spl.size() >= 2);
if (spl.size() >= 2) p1 = var(spl[1]);
else p1 = v_val;
} else if (spl[0] == "rch") {
me = i_rch;
if (spl.size() >= 2) p1 = var(spl[1]);
else p1 = v_val;
} else if (spl[0] == "wint") {
me = i_wint;
#if DEBUG_LEVEL >= 10
cerr << "Preprocess WINT, spl " << spl.size() << endl;
#endif
if (spl.size() >= 2) p1 = var(spl[1]);
else p1 = v_val;
#if DEBUG_LEVEL >= 10
cerr << "End of processor" << endl;
#endif
} else if (spl[0] == "wch") {
me = i_wch;
if (spl.size() >= 2) p1 = var(spl[1]);
else p1 = v_val;
}
}
// ** Warning: This function will modify runtime variables **
// true -- Suppress automatic skipper.
bool execute() {
int tmp;
char tch;
switch (me) {
case i_func: case i_fcall: case i_udef: case i_nop:
break;
case i_hlt:
exit(0);
break;
case i_set:
p2.write(p1.read());
break;
case i_jif:
if (p2.read()) {
current.lineid = v_line.read() + p1.read();
current.seqid = 0;
return true;
}
break;
case i_call:
stk.push(current); // Has [return true] then.
// Pass through
case i_jmp:
if (me == i_jmp) current.lineid = v_line.read() + p1.read();
else current.lineid = p1.read();
current.seqid = 0;
return true;
break;
case i_ret: // No suppress, however, for this.
if (has_expanded) v_ret.write(p1.read());
assume(stk.size());
current = stk.top();
v_line.write(current.reg);
stk.pop();
break;
case i_inv:
p2.write(-p1.read()); // ~p1.read()
break;
#define arith_exec(name,op) case i_##name: \
p3.write(p1.read() op p2.read()); break
arith_exec(add,+);
arith_exec(sub,-);
arith_exec(mult,*);
arith_exec(idiv,/);
arith_exec(mod,%);
arith_exec(lsft,<<);
arith_exec(rsft,>>);
arith_exec(band,&);
arith_exec(bor,|);
arith_exec(bxor,^);
#define logic_exec(name,op) case i_##name: \
p3.write((p1.read() op p2.read()) ? 1 : 0); break
logic_exec(lgr,>);
logic_exec(lls,<);
logic_exec(lge,>=);
logic_exec(lle,<=);
logic_exec(leql,==);
logic_exec(land,&&);
logic_exec(lor,||);
case i_rint:
cin>>tmp;
p1.write(tmp);
break;
case i_rch:
tch = cin.get();
p1.write(tch);
break;
case i_wint:
cout<<p1.read();
break;
case i_wch:
cout<<((char)p1.read());
break;
default:
toggle_error(-1, __LINE__, "Invalid instruction");
}
return false;
}
};
vector<instruct> program[MAX_COMMAND_LEN];
int program_length; // aka. n
void flusher() {
cout<<flush;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
atexit(flusher);
cin>>program_length;
inhale();
int comment = 0; // Allowing cross-line comment
for (int i = 1; i <= program_length; i++) {
#if DEBUG_LEVEL >= 12
cerr << "Pre input" << endl;
#endif
string tmp, cinst = "";
getline(cin, tmp);
#if DEBUG_LEVEL >= 10
cerr << "Begin line processor: " << tmp << endl;
#endif
for (auto &j : tmp) {
#if DEBUG_LEVEL >= 15
cerr << "Character: " << j << endl;
#endif
if (j == '[') comment++;
else if (j == ']') comment--;
if (comment < 0) {
toggle_warning(i, __LINE__, "Comment closure does not match");
comment = 0;
}
if (comment == 0) {
if (j == ';') {
#if DEBUG_LEVEL >= 12
cerr << "Processing: \"" << cinst << "\"" << endl;
#endif
cinst = strip(cinst);
auto ins = instruct(cinst);
program[i].push_back(ins);
cinst = "";
} else cinst += j;
}
#if DEBUG_LEVEL >= 12
cerr << "End of dealer" << endl;
#endif
}
#if DEBUG_LEVEL >= 12
cerr << "End of string input" << endl;
#endif
if (cinst.length()) {
toggle_error(i, __LINE__, "Expected ';'");
}
#if DEBUG_LEVEL >= 12
cerr << "End of error checker" << endl;
#endif
}
for (int i = 1; i <= program_length; i++) {
for (auto &j : program[i]) {
if (j.me == instruct::instype::i_func) {
func_refer[j.special] = i;
if (j.special.length() && j.special[0] != '$') func_refer[string("$") + j.special] = i; // ?
//j.me = instruct::instype::i_nop;
j.me = instruct::instype::i_set;
j.p1 = var(var::vartype::constant, i);
j.p2 = v_line;
}
}
}
for (int i = 1; i <= program_length; i++) {
for (auto &j : program[i]) {
if (j.me == instruct::instype::i_fcall) {
// func_refer[j.special] = i;
// j.me = i_nop;
j.me = instruct::instype::i_call;
if (!func_refer.count(j.special)) toggle_error(i, __LINE__, "Unknown function");
j.p1 = var(var::vartype::constant, func_refer[j.special]);
}
}
}
//inhale();
current.lineid = 1; current.seqid = 0;
while (current.lineid <= program_length) {
current.reg = v_line.read();
if (current.seqid < program[current.lineid].size()) {
if (program[current.lineid][current.seqid].execute()) continue;
}
current.seqid++;
while (current.lineid <= program_length && current.seqid >= program[current.lineid].size()) {
current.seqid = 0;
current.lineid++;
}
}
cout<<flush;
return 0;
}剪贴板
剪贴板 rw1z3zks
剪贴板信息
剪贴板信息
- 发布
- 收录
- 更新