ObjC/Swift 手写简单编程语言

crella95 · 2022年10月16日 · 最后由 crella95 回复于 2024年08月23日 · 899 次阅读

simple While and Sub and If

module main;
// crispy basic programming language
import std.stdio;
import std.conv;
import std.string;
import std.file;
import std.process;
//todo:
// UNSUPPORT define subroutine and run subroutine



int main(string[] args)
{
    string[15] ab ;
    writeln(">>> readfile abccb.txt");
    File fgl = File("abccb.txt", "r");
    string fmm = "";
    int fi = 0;
    while (fi < 14){
        ab[fi] = chomp(fgl.readln());
        fi += 1;
    }
    fgl.close();
    //writeln(ab[0]);
    writeln("You can't use Sub() or Co() inside a While() !!!");
    tour_cmd(ab);
    return 1;
}
//int a 6
//co [clog a] [cwrite [hello!]] [clog a] --> step to step execute commands..
//ifle a [cwrite [happy!]] [cwrite [good!]] --> if (a>0){ print("happy!"); print("good!");}

// to function while(...){}, use this: A only one while cycle: 
// co [clog a] [fgc a] [fig a [y-1]] [whilele a]  --> while(a>0){ writeline(a); a = a-1;}

// OR while(...) {} with more than one cycle:
//int a 7
//int b 4
//co [#f 7] [#f 13]  [clog a] [fig a [$a-1]]  [whilele a 13] [int a 7] [fig b [$b-2]] [clog b] [whilele b 7]
// ---- > a = 7; b = 4; do  { do  { print(a); a = a-1; } while (a > 0) ; a = 7; b = b-2;} while (b>0);
// You can't use Sub() or Co() inside a While() !!!

// to function continual Sub(), use:
//int b -7
//fsub xx [fgc b] [fig b [y-1]] [clog b] 
//fsub yy [cwrite [Hi b!]] [clog b] [rsub xx]
//fsub zz [cwrite [Next b!]] [clog b] [rsub yy]
//rsub zz
//rsub xx

// work out math problem
float evv(string inp){
    string ncc = "";
    float[5000] en;
    int[5000] ff;
    int i = 0;
    int j = 0;
    if (inp[0] == '-'){
        i += 1;
        ncc = "-";
    }
    // don't support brackets ( )
    // execute division first then multiply then minus then plus
    // suppose no continual operators;
    while (i < inp.length){
        if (inp[i] == '+'){
            en[j] = ncc.to!float;   
            ff[j] = 1;
            j += 1;
            ncc = "";
        } else if (inp[i] == '-'){
            en[j] = ncc.to!float;   
            ff[j] = 2;
            j += 1;
            ncc = "";
        } else if (inp[i] == '*'){
            en[j] = ncc.to!float;   
            ff[j] = 3;
            j += 1;
            ncc = "";
        } else if (inp[i] == '/'){
            en[j] = ncc.to!float;
            ff[j] = 4;
            j += 1;
            ncc = "";
        } else {
            ncc = ncc~inp[i];
        }
        i += 1;
    }
    if (ncc.length > 0) {
        en[j] = ncc.to!float;
        j += 1;
    }
    if (ff[0] == 0){
        return en[0].to!int;
    }
    float fg = 0;
    i = 0;
    while (i < j){
        if (ff[i] == 4){
            en[i] = en[i] / en[i+1];
            ff[i] = 3;
            en[i+1] = 1; // division -> multiply
        }
        i += 1;
    }
    i = j - 1;
    while (i >= 0){
        if (ff[i] == 3){
            en[i] = en[i] * en[i+1];
            ff[i] = 1;
            en[i+1] = 0; // multiply -> minus
        }
        i -= 1;
    }
    i = 0;
    while (i < j){
        if (ff[i] == 2){
            en[i+1] = en[i] - en[i+1];
            ff[i] = 1;
            en[i] = 0; // multiply -> minus
        }
        i += 1;
    }
    i = 0;
    while (i < j){
        fg = fg + en[i];
        i += 1;
    }
    return fg;
}
// make 'a+b' to '1+2.5'


void erwrite(string msg){
    writeln("Error: <"~msg~">");
}
int tour_cmd(string[] fibre1){
    string[1000] gstrs;
    int gsi = 0;
    // WARGING!: array length must < 16100. It's weird on dmd2 on windows. dmd2 on linux works well.
    //string tmpstr = "";
    long[1000] gints;
    int gii = 0;
    float[1000] gflts;
    int gfi = 0;
    string[1000] gnms;
    int gni = 1;
    int[1000] gtypes;
    int[100] gcchs;
    /*
    1->int 2->flt 3->string

    5->local file operation
    7->array declare
    type allowed counts = 20
    */
    int[1000] gadrs;

    string lfp = "";
    string lftmp = "";

    int lfid = 1;
    string ctmp = "";

    string cs = "";
    string mmr = "";
    int mmi = 0;
    int mmj = 0;
    int mmk = 0;
    float mmh = 0;
    int cj = 0;
    string mms = "";
    int is_wait = 0; // spawnprocess and spawnshell, D's execute()
    //writeln(fibre1);
    //int continual_cs = 1;
    int var_found = 0;
    int waitn = 0;


    string[500] cqs; // execute commands cache 
    cs = fibre1[0];
    int cmj = 0;
    int cqj = 0; 

    string[1000] subfs;
    string[1000] subws;
    int subfid = 0;

    char[300] ckp;

    int f1len = fibre1.length.to!int;
    //writeln(f1len);
    string[100] curnm;
    int uk = 0;
    int mmu = 0;
    int[200] ccint;
    float[200] ccflt;
    string[200] ccstr;
//    float[200] ccfg;


    while (uk < f1len){
          // writeln("<<< "~cs);

            int cci = curnm.length - 1;

            while ( cci >= 0){
                curnm[cci] = ""; cci -= 1;
            }
            int constr = 0;
            cj = 0;
            cci = 0;
            int ccj = -1;
            int ccm = 300-1;

            while (ccm >= 0){
                ckp[ccm] = ' '; ccm -= 1;
            }
            ctmp = "";
            while (cci < cs.length){  
                                //writeln(cs[cci]);
                if (cs[cci] == ' '){
//writeln("ccj="~ccj.to!string);
                    if (constr >0 ){
                                            ccj += 1;
                       ckp[ccj] = ' ';

                    } else if (ccj >= 0){
                       //writeln("ckp="~ckp.to!string);

                         ctmp = "";
                         for (ccm = 0; ccm <= ccj; ccm += 1){
                            ctmp = ctmp ~ ckp[ccm];
                         }
                         curnm[cj] = ctmp;
                         cj += 1;  
                         ccj = -1;
                    }
                } else if (cs[cci] == '['){
                    if (constr > 0){
                        ccj += 1;
                        ckp[ccj] = '[';
                    }
                    constr += 1;

                } else if (cs[cci] == ']'){
                    if (constr == 1){
                        ctmp = "";
                        //writeln("ckp="~ckp.to!string);

                         for (ccm = 0; ccm <= ccj; ccm += 1){
                            ctmp = ctmp ~ ckp[ccm];
                         }
                         curnm[cj] = ctmp;
                         ccj = -1;
                         cj += 1; 
                         constr = 0;
                    } else {
                        constr -= 1;
                        ccj += 1;
                        ckp[ccj] = ']';
                    }
                } else {
                    ccj += 1;
                    ckp[ccj] = cs[cci];
                }
                cci += 1;
            }
            if (ccj >= 0){
                ctmp = "";

                 for (ccm = 0; ccm <= ccj; ccm += 1){
                    ctmp = ctmp ~ ckp[ccm];
                 }
                 curnm[cj] = ctmp;
                 cj += 1; 
            }

            if (curnm[0] == "int"){ // int a 1
                var_found = 0;
                mmk = gni;
                while (mmk >= 0) {
                    if (gtypes[mmk] > 0){
                       if (gnms[mmk] == curnm[1]){
                            gints[gadrs[mmk]] = curnm[2].to!int;
                            gtypes[mmk] = 1;
                            var_found = 1;
                            mmk = -1;
                        }
                    }
                    mmk -= 1;
                }
                if (var_found == 0){
                    gni += 1;
                    gnms[gni] = curnm[1];
                    //writeln(gnms[gni]);
                    gtypes[gni] = 1;
                    gadrs[gni] = gii;
                    gints[gii] = curnm[2].to!int;
                    gii += 1;
                }

            } else if (curnm[0] == "flt"){ // flt b 1.5
                var_found = 0;
                mmk = gni;
                while (mmk >= 0) {
                    if (gtypes[mmk] > 0){
                       if (gnms[mmk] == curnm[1]){
                            gflts[gadrs[mmk]] = curnm[2].to!float;
                            gtypes[mmk] = 2;
                            var_found = 1;
                            mmk = -3;
                        }
                    } 
                    mmk -= 1;

                }
                if (var_found == 0){
                } else {
                    gni += 1;
                    gnms[gni] = curnm[1];
                    gtypes[gni] = 2;
                    gadrs[gni] = gfi;
                    gflts[gfi] = curnm[2].to!float;
                    gfi += 1;
                }

            }else if (curnm[0] == "clog"){
                //writeln("start clog");
                var_found = 0;
               mmk = gni;
              // writeln(gnms);
                while (mmk >= 0) {
                   if (gtypes[mmk] == 1){
                        if (gnms[mmk] == curnm[1]){
                            writeln(gints[gadrs[mmk]]);
                            var_found = 1;
                            mmk = -3;
                        }
                    } else if (gtypes[mmk] == 2){
                        if (gnms[mmk] == curnm[1]){
                            writeln(gflts[gadrs[mmk]]);
                            var_found = 1;
                            mmk = -3;
                        }
                    } else if (gtypes[mmk] == 3){
                        if (gnms[mmk] == curnm[1]){
                            writeln(gstrs[gadrs[mmk]]);
                            var_found = 1;
                            mmk = -3;
                        }
                    } 
                    mmk -= 1;

                }
                if (var_found == 1){
                } else {
                    erwrite("couldn't find Var:"~curnm[1]);
                }

            }  else if (curnm[0] == "arylen"){
                mmk = gni;
                var_found = 0;
                long mmq = 0;
                while (mmk >= 0) {
                    if (gtypes[mmk] >0){
                       if (gnms[mmk] == curnm[2]){
                            mmq = gints[gadrs[mmk]];
                            mmk = -3;
                            var_found = 1;
                        }
                    }
                    mmk -= 1;
                }
                if (var_found == 1){
                    erwrite("couldn't find Var:"~curnm[2]);
                } else {
                    mmk = gni;
                    while (mmk >= 0) {
                        if (gtypes[mmk] == 1){
                           if (gnms[mmk] == curnm[1]){
                                gints[gadrs[mmk]] = mmq;
                                mmk = -3;
                                var_found = 1;
                            }
                        }
                        mmk -= 1;
                    }
                    if (var_found == 1){
                    } else {
                        gni += 1;
                        gnms[gni] = curnm[1];
                        gtypes[gni] = 1;
                        gadrs[gni] = gii;
                        gints[gii] = mmq;
                        gii += 1;
                    }
                }
            } else if (curnm[0] == "if"){ // if a [int b 1]  [ int c 0] ---> if (a == 0)
                var_found = 0;
                mmk = gni;

                while (mmk >= 0) {
                    if (gtypes[mmk] == 1 ){
                       if (gnms[mmk] == curnm[1]){
                                                        //mmk = -3;
                            if (gints[gadrs[mmk]] == 0){

                                var_found = 1;mmk = -3;
                            } else {
                                var_found = 2;mmk = -3;
                            }
                        }
                    } else if (gtypes[mmk] == 2 ){
                       if (gnms[mmk] == curnm[1]){
                                                            //mmk = -3;
                            if (gflts[gadrs[mmk]] == 0.0){
                                  var_found = 1;mmk = -3;
                            } else {
                                  var_found = 2;mmk = -3;
                            }
                        }
                    }
                    mmk -= 1;
                }
                int mma = 0;
                if (var_found ==1){
                    waitn = 1;
                    cs = "co ";
                    for (mma = 2; mma < cj; mma += 1){
                         cs = cs ~ " ["~curnm[mma]~"] ";
                    }

                } else if (var_found == 0){
                    erwrite("couldn't find Val: "~curnm[1]);
                }
            } else if (curnm[0] == "ifle"){ // if (a > 0) {}
                var_found = 0;
                mmk = gni;
                while (mmk >= 0) {
                    if (gtypes[mmk] == 1 ){
                       if (gnms[mmk] == curnm[1]){
                                                // mmk = -3;
                            if (gints[gadrs[mmk]] > 0){

                                var_found = 1;mmk = -3;
                            } else {
                                var_found = 2;mmk = -3;
                            }

                        }
                    } else if (gtypes[mmk] == 2 ){
                       if (gnms[mmk] == curnm[1]){
                                                 //mmk = -3;
                            if (gflts[gadrs[mmk]] > 0.0){

                                var_found = 1;mmk = -3;
                            } else {
                                var_found = 2;mmk = -3;
                            }
                        }
                    }
                    mmk -= 1;
                }
                int mma = 0;
                if (var_found == 1){
                    waitn = 1;
                    cs = "co ";
                    for (mma = 2; mma < cj; mma += 1){
                         cs = cs ~ " ["~curnm[mma]~"] ";
                    }
                } else if (var_found == 0){
                    erwrite("couldn't find Val: "~curnm[1]);
                }
            }else if (curnm[0] == "while"){ // while var 3 --> while (var == 0) { goto #f 3;}
                var_found = 0;
                mmk = gni;
                while (mmk >= 0) {
                    if (gtypes[mmk] == 1 ){
                       if (gnms[mmk] == curnm[1]){
                                                // mmk = -3;
                            if (gints[gadrs[mmk]] == 0){
                                var_found = 1;mmk = -3;
                            } else {
                                var_found = 2;mmk = -3;
                            }
                        }
                    } else if (gtypes[mmk] == 2 ){
                       if (gnms[mmk] == curnm[1]){
                                                // mmk = -3;
                            if (gflts[gadrs[mmk]] == 0.0){
                                var_found = 1;mmk = -3;
                            } else {
                                var_found = 2;mmk = -3;
                            }
                        }
                    }
                    mmk -= 1;
                }
                int mma = 0;
                if (var_found == 1){
                    waitn = 2 + curnm[2].to!int;

                } else if (var_found == 0){
                    erwrite("couldn't find Val: "~curnm[1]);
                }
            } else if (curnm[0] == "whilele"){ // while var 3 --> while (var == 0) { goto #f 3;}
                var_found = 0;
                mmk = gni;
                while (mmk >= 0) {

                    if (gtypes[mmk] == 1 ){
                       if (gnms[mmk] == curnm[1]){
                            if (gints[gadrs[mmk]] > 0){
                                var_found = 1;mmk = -3;
                            } else {
                                var_found = 2;mmk = -3;
                            }

                        }
                    } else if (gtypes[mmk] == 2 ){
                       if (gnms[mmk] == curnm[1]){
                            if (gflts[gadrs[mmk]] > 0.0){
                                var_found = 1;mmk = -3;
                            } else {
                                var_found = 2;mmk = -3;
                            }
                        }
                    }
                    mmk -= 1;
                }

                int mma = 0;
                if (var_found == 1){
                    waitn = 2 + curnm[2].to!int;
                } else if (var_found == 0){
                    erwrite("whilele: couldn't find Val: "~curnm[1]);
                }
                //writeln("wle var="~curnm[1]~" wn="~waitn.to!string);
            } else if (curnm[0] == "mkstr"){ //mkstr c {This is a string} {ha ha} -> c = 'This is a stringha ha'
                string mmss = curnm[2];
                mmj = 3;
                while (mmj < cj){
                    mmss = mmss~curnm[mmj];
                    mmj += 1;
                }
                mmk = gni;
                while (mmk >= 0) {
                    if (gtypes[mmk] > 0 ){
                       if (gnms[mmk] == curnm[1]){
                            gstrs[gadrs[mmk]] = mmss;
                            gtypes[mmk] = 3;
                            mmk = -3;
                            var_found = 1;
                        }
                    }
                    mmk -= 1;
                }
                if (var_found == 1){
                } else {
                    gni += 1;
                    gnms[gni] = curnm[1];
                    gtypes[gni] = 3;
                    gadrs[gni] = gsi;
                    gstrs[gii] = mmss;
                    gsi += 1;
                }

            } else if (curnm[0] == "aryint") { // aryint y 20
                mmi = curnm[2].to!int;
                gni += 1;
                gnms[gni] = curnm[1];
                gadrs[gni] = gii; 
                gtypes[gni] = 7;
                gcchs[gni] = mmi;
                for (mmj = mmi; mmj > 0; mmj -= 1){
                    gints[gii + mmj] = 0;
                }
                gii += mmi;

            } else if (curnm[0] == "aryflt") {
                mmi = curnm[2].to!int;
                gni += 1;
                gnms[gni] = curnm[1];
                gadrs[gni] = gfi; 
                gtypes[gni] = 8;
                gcchs[gni] = mmi;
                for (mmj = mmi; mmj > 0; mmj -= 1){
                    gflts[gfi + mmj] = 0;
                }
                gfi += mmi;
            } else if (curnm[1] == "arystr"){
                mmi = curnm[2].to!int;
                gni += 1;
                gnms[gni] = curnm[1];
                gadrs[gni] = gsi; 
                gtypes[gni] = 9;
                gcchs[gni] = mmi;
                for (mmj = mmi; mmj > 0; mmj -= 1){
                    gstrs[gfi + mmj] = "";
                }
                gsi += mmi;
          } else if (curnm[0] == "engine"){
              string[500] nab;
              for (int mmy = 1; mmy < cj; mmy += 1){
                  nab[mmy - 1] = curnm[mmy];
              }
              tour_cmd(nab);

            } else if (curnm[0] == "lfwrite"){ // lfwrite [filepath] [string text]

                File lfx = File(curnm[1], "w");
                lfx.write(curnm[2]);
                lfx.close();
            } else if (curnm[0] == "lfappend"){
                File lfx = File(curnm[1], "a");
                lfx.write(curnm[2]);
                lfx.close();
            } else if (curnm[0] == "mkcc"){
                // mkcc i x 21 ; assign Cache int[] 21 value to Variable x
                // mkcc sx y 2 15; assign Cache string[] 15 value to Array y[2]
                var_found = 0;
                mmk = gni;
                while (mmk >= 0) {
                    if (gtypes[mmk] > 0){
                       if (gnms[mmk] == curnm[2]){
                            if (curnm[1] == "i"){
                                gints[gadrs[mmk]] = ccint[curnm[3].to!int];
                                gtypes[mmk] = 1;
                                var_found = 1;
                                mmk = -1;
                            } else if (curnm[1] == "f"){
                                gflts[gadrs[mmk]] = ccflt[curnm[3].to!int];
                                gtypes[mmk] = 2;
                                var_found = 1;
                                mmk = -1;
                            } else if (curnm[1] == "s"){
                                gstrs[gadrs[mmk]] = ccstr[curnm[3].to!int];
                                gtypes[mmk] = 3;
                                var_found = 1;
                                mmk = -1;
                            } else if (curnm[1] == "ix"){
                                gints[gadrs[mmk] + curnm[3].to!int] = ccint[curnm[4].to!int];
                                gtypes[mmk] = 1;
                                var_found = 1;
                                mmk = -1;
                            } else if (curnm[1] == "fx"){
                                gflts[gadrs[mmk] + curnm[3].to!int] = ccflt[curnm[3].to!int];
                                gtypes[mmk] = 2;
                                var_found = 1;
                                mmk = -1;
                            } else if (curnm[1] == "sx"){
                                gstrs[gadrs[mmk] + curnm[3].to!int] = ccstr[curnm[3].to!int];
                                gtypes[mmk] = 3;
                                var_found = 1;
                                mmk = -1;
                            } 
                        }
                    }
                    mmk -= 1;
                }
                if (var_found == 0){
                    erwrite("can't find Val name:"~curnm[2]);
                }
            } else if (curnm[0] == "arycc"){
                // arycc i y 14 3 -> set cache ccint[3] = Array(int) y[14]
                mmk = gni;
                while (mmk >= 0) {
                    if (gtypes[mmk] > 0){
                       if (gnms[mmk] == curnm[2]){
                            if (gtypes[mmk] == 7){
                                if (curnm[1] == "i"){
                                    ccint[curnm[4].to!int] = gints[gadrs[mmk] + curnm[3].to!int ].to!int;
                                    var_found = 1;
                                    mmk = -1;
                                } else {
                                    erwrite("array assign type not match cache type");
                                }

                            } else if (gtypes[mmk] == 8){
                                if (curnm[1] == "f"){
                                    ccflt[curnm[4].to!int] = gflts[gadrs[mmk] + curnm[3].to!int ];
                                    var_found = 1;
                                    mmk = -1;
                                } else {
                                    erwrite("array assign type not match cache type");
                                } 
                            } else if (gtypes[mmk] == 9){
                                if (curnm[1] == "s"){
                                    ccstr[curnm[4].to!int] = gstrs[gadrs[mmk] + curnm[3].to!int ];
                                    var_found = 1;
                                    mmk = -1;
                                } else {
                                    erwrite("array assign type not match cache type");
                                } 
                            } else {
                                erwrite("array assign type illegal");
                            } 
                        }
                    }
                    mmk -= 1;
                }
                if (var_found == 0){
                    erwrite("can't find Val name:"~curnm[2]);
                }



            } else if (curnm[0] == "lfread"){ // lfread filehandle var -> var = filehandle's file content, all.
                lfp = curnm[1];
                lftmp = "";
                File lfx = File(lfp, "r");
                while (!lfx.eof()){
                    lftmp = lftmp~chomp(lfx.readln());
                }
                lfx.close();

                var_found = 0;

                mmk = gni;
                while (mmk >= 0) {
                    if (gtypes[mmk] > 0 ){
                       if (gnms[mmk] == curnm[2]){
                            gstrs[gadrs[mmk]] = lftmp;
                            gtypes[mmk] = 3;
                            var_found = 1;
                            mmk = -3;
                        }
                    }
                    mmk -= 1;
                }
                if (var_found == 1){
                } else {
                    gni += 1;
                    gnms[gni] = curnm[2];
                    gtypes[gni] = 3;
                    gadrs[gni] = gsi;
                    gstrs[gii] = lftmp;
                    gsi += 1;
                }

            } 
            else if (curnm[0] == "process"){ // process is_wait(0 OR 1) [exe] [param]
                //int xxcpid;
                int swj = cj - 2;
                string[10] xcmd;
                for (swj = 2; swj < cj; swj += 1){
                    xcmd[swj-2] = curnm[swj];
                }
                is_wait = curnm[1].to!int;
                if (is_wait == 1){
                    auto xxcpid = execute(xcmd);
                } else if (is_wait == 0){
                    auto xxcpid = spawnProcess(xcmd);
                }
            } else if (curnm[0] == "shell"){ // 
                auto xxcpid = executeShell(curnm[1]);
            } else if (curnm[0] == "cwrite"){ // 
                writeln(curnm[1]);
            } else if (curnm[0] == "eval"){ // 
                waitn = 1;
                cs = curnm[1];
            } else if (curnm[0] == "fgc"){ // fgc  [a b c] r [1+$*$-$] 
                //--> a,b,c is the cache of $,$,$
                mmk = 0;
                mms = curnm[1];
                string[10] fycr;
                int fyid = 0;
                mmr = "";
                while (mmk < mms.length){
                    if (mms[mmk] == ' '){
                        if (mmr.length > 0){
                            fycr[fyid] = mmr;
                            fyid += 1;
                        }
                        mmr = "";
                    } else {
                        mmr = mmr~mms[mmk];
                    }
                    mmk += 1;
                }
                if (mmr.length > 0){
                    fycr[fyid] = mmr;
                    fyid += 1;
                }
                float[10] ccfg;

                int mme = 0;
                while (mme < fyid){
                    var_found = 0;
                    mmk = gni;
                    while (mmk >= 0) {
                        if (gtypes[mmk] == 1){
                           if (gnms[mmk] == fycr[mme]){
                                ccfg[mme] = gints[gadrs[mmk]].to!float;
                                var_found = 1;
                                mmk = -3;
                            }
                        } else if (gtypes[mmk] == 2){
                           if (gnms[mmk] == fycr[mme]){
                                ccfg[mme] = gflts[gadrs[mmk]];
                                var_found = 1;
                                mmk = -3;
                            }
                        }

                        mmk -= 1;
                    }
                    if (var_found == 0){
                        erwrite("can't find Val name:"~curnm[mme]);
                    }
                    mme += 1;
                }

                mmr = "";
                mms = curnm[3];
                mme = 0;
                for (int j = 0; j < mms.length; j++){
                    if (mms[j] == '$'){
                        if (mme < fyid) {
                            mmr = mmr~(ccfg[mme].to!string);
                            mme += 1;
                        } else {
                            erwrite("array length out of range");
                        }
                    } else {
                        mmr = mmr~mms[j];
                    }
                }

                float rrb = evv(mmr);
                var_found = 0;
                mmk = gni;
                while (mmk >= 0) {
                    if (gtypes[mmk] > 0){
                       if (gnms[mmk] == curnm[1]){
                            gflts[gadrs[mmk]] = rrb;
                            gtypes[mmk] = 2;
                            var_found = 1;
                            mmk = -3;
                        }
                    }
                    mmk -= 1;
                }
                if (var_found == 0){
                    gni += 1;
                    gnms[gni] = curnm[2];
                    gtypes[gni] = 2;
                    gadrs[gni] = gfi;
                    gflts[gfi] = rrb;
                    gfi += 1;
                }

            } else if (curnm[0] == "fig"){
                // figure out math problem : fig axx [1+3.5*2-3] 

                float rra = evv(curnm[2]);
                var_found = 0;
                mmk = gni;
                while (mmk >= 0) {
                    if (gtypes[mmk] > 0){
                       if (gnms[mmk] == curnm[1]){
                            gflts[gadrs[mmk]] = rra;
                            gtypes[mmk] = 2;
                            var_found = 1;
                            mmk = -3;
                        }
                    }
                    mmk -= 1;
                }
                if (var_found == 0){
                    gni += 1;
                    gnms[gni] = curnm[1];
                    gtypes[gni] = 2;
                    gadrs[gni] = gfi;
                    gflts[gfi] = rra;
                    gfi += 1;
                }

            } else if (curnm[0] == "fstr"){ //fstr r b -> make b.tostring()
                    mms = "";
                    mmk = gni;
                    var_found = 0;
                    while (mmk >= 0) {
                        if (gtypes[mmk]  == 1){
                           if (gnms[mmk] == curnm[2]){
                                mms = gints[gadrs[mmk]].to!string;
                                mmk = -3;
                                var_found = 1;
                            }
                        } else if (gtypes[mmk]  == 2){
                           if (gnms[mmk] == curnm[2]){
                                mms = gflts[gadrs[mmk]].to!string;
                                mmk = -3;
                                var_found = 1;
                            }
                        } else if (gtypes[mmk]  == 3){
                           if (gnms[mmk] == curnm[2]){
                                mms = gstrs[gadrs[mmk]].dup;
                                mmk = -3;
                                var_found = 1;
                            }
                        } else {
                            erwrite("cannot change type to string");
                        }
                        mmk -= 1;
                    }
                    if (var_found == 0){
                        erwrite("couldn't find Var:"~curnm[2]);
                    } else {
                        var_found = 0;
                        mmk = gni;
                        while (mmk >= 0) {
                            if (gtypes[mmk]  > 0){
                               if (gnms[mmk] == curnm[1]){
                                    gstrs[gadrs[mmk]] = mms;
                                    gtypes[mmk] = 3;
                                    mmk = -3;
                                    var_found = 1;
                                }
                            }
                            mmk -= 1;
                        }
                        if (var_found == 1){
                        } else {
                            gni += 1;
                            gnms[gni] = curnm[1];
                            gstrs[gsi] = mms;
                            gadrs[gni] = gsi;
                            gtypes[gni] = 3;
                            gsi += 1;
                        }
                    }

            } else if (curnm[0] == "fflt"){
                mmk = gni;
                mmh = 0;
                var_found = 0;
                while (mmk >= 0) {
                    if (gtypes[mmk]  == 1){
                       if (gnms[mmk] == curnm[2]){
                            mmh = gints[gadrs[mmk]].to!int;
                            mmk = -3;
                            var_found = 1;
                        }
                    } else if (gtypes[mmk]  == 2){
                       if (gnms[mmk] == curnm[2]){
                            mmh = gflts[gadrs[mmk]];
                            mmk = -3;
                            var_found = 1;
                        }
                    } else if (gtypes[mmk]  == 3){
                       if (gnms[mmk] == curnm[2]){
                            mmh = gstrs[gadrs[mmk]].to!int;
                            mmk = -3;
                            var_found = 1;
                        }
                    } else {
                        erwrite("cannot change type to float");
                    }
                    mmk -= 1;
                }
                if (var_found == 0){
                    erwrite("couldn't find Var:"~curnm[2]);
                } else {
                    mmk = gni;
                    var_found = 0;
                    while (mmk >= 0) {
                        if (gtypes[mmk]  > 0){
                           if (gnms[mmk] == curnm[1]){
                                gflts[gadrs[mmk]] = mmh;
                                gtypes[mmk] = 2;
                                mmk = -3;
                                var_found = 1;
                            }
                        }
                        mmk -= 1;
                    }
                    if (var_found == 0){
                    } else {
                        gni += 1;
                        gnms[gni] = curnm[1];
                        gflts[gfi] = mmh;
                        gadrs[gni] = gfi;
                        gtypes[gni] = 2;
                        gfi += 1;
                    }
                }
           } else if (curnm[0] == "co"){ // execute continual commands
             mmk = 2;
             cmj += 1;
             cqs[cmj] = curnm[1];
             while (mmk < cj){
                    cmj += 1;
                    cqs[cmj] = curnm[mmk];
                    mmk += 1;          
                }
                mmk = 0;
            } else if (curnm[0] == "fsub"){ // fsub rr [...] [...]
               mmk = 2;
               mms = "co ";
               while (mmk < cj){
                   mms = mms ~ "[" ~ curnm[mmk] ~ "] ";
                   mmk += 1;
               }
               var_found = 0;
               mmk = subfid;
               while (mmk >= 0){
                   if (subfs[mmk] == curnm[1]){
                       var_found = 1;
                       subws[mmk] = mms;
                       subfid += 1;
                   }
                   mmk -= 1;
               }
               if (var_found == 0){
                   subfid += 1;
                   subws[subfid] = mms;
                   subfs[subfid] = curnm[1];
               }
               mmk = 0;
            } else if (curnm[0] == "rsub"){ // rsub rr
               var_found = 0;
               mmk = subfid;
               while (mmk >= 0){
                   if (subfs[mmk] == curnm[1]){
                       var_found = 1;
                       cmj += 1;
                       cqs[cmj] = subws[mmk];
                   }
                   mmk -= 1;
               }
               if (var_found == 0){
                   erwrite("can't find Sub:"~curnm[1]);
               }
            }

            // start execute tree next 
            if (cmj == 0){
                if (waitn == 0){
                    if (uk < f1len - 1){
                        uk += 1;
                        cs = fibre1[uk];
                    } else {
                        return 0;
                    }
                } else {
                    waitn = 0;
                }
            } else if (cqj > cmj){
              //  if (waitn >= 2){

               // } else {
                if (waitn < 2){
                    cqj = 0;
                    while (cqj < cqs.length){
                        cqs[cqj] = "";
                        cqj += 1;
                    }
                    cmj = 0;
                    cqj = 0;
                    uk += 1;
                    cs = fibre1[uk];
             //       writeln("xx cs="~cs);
                } else {
                    string mmsc = "#f "~(waitn-2).to!string;
                    int mmw = 0;
                    while (mmw == 0){
                        if (cqs[cqj] == mmsc){
                            mmw = 1;
                        } else {
                            cqj -= 1;
                        }
                    }
                    cs = cqs[cqj];
                   // writeln("cqj="~cqj.to!string);
                    waitn = 0;
                }

            } else if (cqj <= cmj ){
                if (waitn < 2){
                    cs = cqs[cqj];
                    cqj += 1;
                } else {
                    string mmsc = "#f "~(waitn-2).to!string;
                    int mmw = 0;
                    while (mmw == 0){
                        if (cqs[cqj] == mmsc){
                            mmw = 1;
                        } else {
                            cqj -= 1;
                        }
                    }
                    cs = cqs[cqj];
                    waitn = 0;
                }
            } 
    }
    return 1;
}

//string inner_string_tm(string inp){
//    string mms = inp.dup;
//    int mmi = mms.indexOf("&lb;").to!int;
//    while (mmi > -1){
//        mms = mms[0..mmi] ~ "[" ~ mms[(mmi + 4) .. (mms.length)];
//        mmi = mms.indexOf("&lb;").to!int;
//    }
//    mmi = mms.indexOf("&rb;").to!int;
//    while (mmi > -1){
//       mms = mms[0..mmi] ~ "]" ~ mms[(mmi + 4) .. (mms.length)];
//        mmi = mms.indexOf("&rb;").to!int;
//   }
//    return mms;
//}

不错~可以建立个 github 项目,然后把简介和链接贴过来。

2 楼 已删除
3 楼 已删除

突然发现 while 循环可以做成递归的形式,用全局变量

5 楼 已删除
6 楼 已删除
shiweifu 回复

更新了,应该大部分功能能正常运转,谢谢回复

8 楼 已删除
9 楼 已删除
crella95 回复

newer version:

需要 登录 后方可回复, 如果你还没有账号请 注册新账号