虚拟机?

· · 个人记录

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fastio ios::sync_with_stdio(0);cin.tie(0),cout.tie(0)
#define endl "\n"
string code[]={
    "new_number longint a",
    "new_number longint b",
    "input a",
    "input b",
    "new_number longint c",
    "calc - a b c",
    "print c",
    "exit"
};
struct bite{
    string type;
    int data;
}memory[1024768];
int cnt;
map<string,int> lists;
map<string,int> number;
int new_memory(){
    return ++cnt;
}
string read_string(int &i,int line){
    while(code[line][i]==' '){
        i++;
    }
    string ans="";
    for(;i<(int)code[line].size();i++){
        if(code[line][i]==' ') break;
        ans+=code[line][i];
    }
    return ans;
}
int string_to_int(string s){
    int result=0;
    for(int i=0;i<(int)s.size();i++){
        result=(result<<3)+(result<<1)+(s[i]-'0');
    }
    return result;
}
int list_to_memory(int &i,int line){
    string listname=read_string(i,line);
    int listbegin=lists[listname];
    string xb=read_string(i,line);
    int x=string_to_int(xb);
    if(memory[listbegin].type=="leftlong"){
        return listbegin+x*2;
    }
    return listbegin+x;
}
ll memory_to_longint(int t){
    int m=memory[t+1].data;
    int n=memory[t].data;
    return ((n*1ll)<<32)+(unsigned int)m;
}
ll get_value(int &i,int line){
    string token=read_string(i,line);
    if(token=="list"){
        int pos=list_to_memory(i,line);
        if(memory[pos].type=="leftlong"){
            return memory_to_longint(pos);
        }
        else{
            return memory[pos].data;
        }
    }
    if(isdigit(token[0])){
        return string_to_int(token);
    }
    int pos=number[token];
    if(memory[pos].type=="leftlong"){
        return memory_to_longint(pos);
    }
    else{
        return memory[pos].data;
    }
}
void set_value(int pos,ll val){
    if(memory[pos].type=="leftlong"){
        memory[pos].data=(val>>32);
        memory[pos+1].data=val%(1ll<<33);
    }
    else{
        memory[pos].data=(int)val;
    }
}
void new_number(int &i,int line){
    string type=read_string(i,line);
    string name=read_string(i,line);
    if(type=="longint"){
        int m1=new_memory();
        int m2=new_memory();
        memory[m1].type="leftlong";
        memory[m2].type="rightlong";
        number[name]=m1;
        return;
    }
    int m=new_memory();
    memory[m].type=type;
    number[name]=m;
}
void new_list(int &i,int line){
    string type=read_string(i,line);
    string name=read_string(i,line);
    string xb=read_string(i,line);
    if(type=="longint"){
        int m1=new_memory();
        int m2=new_memory();
        memory[m1].type="leftlong";
        memory[m2].type="rightlong";
        lists[name]=m1;
        int x=string_to_int(xb);
        for(int i=1;i<x;i++){
            int m1=new_memory();
            int m2=new_memory();
            memory[m1].type="leftlong";
            memory[m2].type="rightlong";
        }
        return;
    }
    int m=new_memory();
    memory[m].type=type;
    int x=string_to_int(xb);
    lists[name]=m;
    for(int i=1;i<x;i++){
        m=new_memory();
        memory[m].type=type;
    }
}
void input(int &i,int line){
    string inname=read_string(i,line);
    int m;
    if(inname=="list"){
        m=list_to_memory(i,line);
    }
    else{
        m=number[inname];
    }
    if(memory[m].type=="leftlong"){
        ll x;
        cin>>x;
        memory[m].data=(x>>32);
        memory[m+1].data=x%(1ll<<33);
    }
    else if(memory[m].type=="char"){
        char c;
        cin>>c;
        memory[m].data=c;
    }
    else if(memory[m].type=="int"){
        int x;
        cin>>x;
        memory[m].data=x;
    }
    else if(memory[m].type=="bool"){
        int x;
        cin>>x;
        memory[m].data=(x!=0);
    }
}
void add(int &i,int line){
    ll v1=get_value(i,line);
    ll v2=get_value(i,line);
    string th_tok=read_string(i,line);
    int dst;
    if(th_tok=="list"){
        dst=list_to_memory(i,line);
    }
    else{
        dst=number[th_tok];
    }
    set_value(dst,v1+v2);
}
void sub(int &i,int line){
    ll v1=get_value(i,line);
    ll v2=get_value(i,line);
    string th_tok=read_string(i,line);
    int dst;
    if(th_tok=="list"){
        dst=list_to_memory(i,line);
    }
    else{
        dst=number[th_tok];
    }
    set_value(dst,v1-v2);
}
void mul(int &i,int line){
    ll v1=get_value(i,line);
    ll v2=get_value(i,line);
    string th_tok=read_string(i,line);
    int dst;
    if(th_tok=="list"){
        dst=list_to_memory(i,line);
    }
    else{
        dst=number[th_tok];
    }
    set_value(dst,v1*v2);   
}
void divi(int &i,int line){
    ll v1=get_value(i,line);
    ll v2=get_value(i,line);
    string th_tok=read_string(i,line);
    int dst;
    if(th_tok=="list"){
        dst=list_to_memory(i,line);
    }
    else{
        dst=number[th_tok];
    }
    set_value(dst,v1/v2);
}
void mod(int &i,int line){
    ll v1=get_value(i,line);
    ll v2=get_value(i,line);
    string th_tok=read_string(i,line);
    int dst;
    if(th_tok=="list"){
        dst=list_to_memory(i,line);
    }
    else{
        dst=number[th_tok];
    }
    set_value(dst,v1%v2);
}
void print(int &i,int line){
    string out=read_string(i,line);
    int m;
    if(out=="list"){
        m=list_to_memory(i,line);
    }
    else{
        m=number[out];
    }
    if(memory[m].type=="int"){
        cout<<memory[m].data;
    }
    else if(memory[m].type=="leftlong"){
        cout<<memory_to_longint(m);
    }
    else if(memory[m].type=="bool"){
        cout<<(memory[m].data!=0);
    }
    else if(memory[m].type=="char"){
        cout<<(char)memory[m].data;
    }
}
int main(){
    int line=0;
    while(true){
        int i=0;
        string op=read_string(i,line);
        if(op=="new_number"){
            new_number(i,line);
            line++;
        }
        else if(op=="new_list"){
            new_list(i,line);
            line++;
        }
        else if(op=="input"){
            input(i,line);
            line++;
        }
        else if(op=="calc"){
            string calcop=read_string(i,line);
            if(calcop=="+"){
                add(i,line);
                line++;
            }
            else if(calcop=="-"){
                sub(i,line);
                line++;
            }
            else if(calcop=="*"){
                mul(i,line);
                line++;
            }
            else if(calcop=="/"){
                divi(i,line);
                line++;
            }
            else if(calcop=="%"){
                mod(i,line);
                line++;
            }
        }
        else if(op=="print"){
            print(i,line);
            line++;
        }
        else if(op=="exit"){
            break;
        }
    }
    return 0;
}