//略过第一个符号 'var' void grammar_parser::declare(set<e_word_t> follows,int& stk_index) { word ident=m_words.get(); if(ident.m_type==ewt_ident) { grammar_debug(ident); _symbol* p_sym=NULL; if((get_top_gener_id()==1)&& (get_global_table()==get_top_table())) { p_sym=push_symbol(_symbol(es_value,ident.m_str_value,eab_global_data,alloc_global_data_space()),true); } else { p_sym=push_symbol(_symbol(es_value,ident.m_str_value,eab_reg_sb,stk_index)); gen_load_const_instruction(stk_index,0); } word assgin=m_words.get(); if(assgin.m_type==ewt_key_assign) { grammar_debug(assgin); expression(follows,stk_index); if(p_sym) gen_code(e_save,p_sym->m_addr_t,p_sym->m_addr); gen_pop_instruction(stk_index); } else m_words.push(assgin); word semicolon=m_words.get(); if(semicolon.m_type==ewt_key_semicolon) { grammar_debug(semicolon); return; } else m_words.push(semicolon); } else m_words.push(ident); test_and_skip(follows,_create_syms()); }
/* value */ static int _value(State * state) /* symbol | register | immediate */ { #ifdef DEBUG fprintf(stderr, "DEBUG: %s()\n", __func__); #endif if(_parser_in_set(state, TS_SYMBOL)) return _symbol(state); else if(_parser_in_set(state, TS_REGISTER)) return _register(state); else if(_parser_in_set(state, TS_IMMEDIATE)) return _immediate(state); return _parser_error(state, "%s", "Expected symbol, register or" " immediate value"); }
Symbol InternedStrings::_symbol(const std::string& s) { auto it = string_to_sym_.find(s); if (it != string_to_sym_.end()) return it->second; auto pos = s.find("::"); if (pos == std::string::npos) { std::stringstream ss; ss << "all symbols must have a namespace, <namespace>::<string>, but found: " << s; throw std::runtime_error(ss.str()); } Symbol ns = _symbol("namespaces::" + s.substr(0, pos)); Symbol sym(sym_to_info_.size()); string_to_sym_[s] = sym; sym_to_info_.push_back({ns, s, s.substr(pos + strlen("::"))}); return sym; }
//略过第一个符号 'function' void grammar_parser::function(set<e_word_t> follows) { //堆栈的样子 //param1,param2,param3,param_count,ret_vaule,prev_reg_sb,prev_reg_ip,其中prev_reg_sb对应堆栈起始位置 word ident=m_words.get(); if(ident.m_type==ewt_ident) { grammar_debug(ident); _symbol* p_sym=push_symbol(_symbol(es_function,ident.m_str_value,eab_absolute_ip,get_new_code_addr(true)),true); if(get_top_gener_id()>1) report_error("语法错误:不支持函数内定义函数\n"); _instruction* p_jmp=gen_code(e_jmp,eab_absolute_ip,0,0,true); create_gener(); create_table(); word lmbrach=m_words.get(); if(lmbrach.m_type=ewt_key_lsbranch) { grammar_debug(lmbrach); p_sym->m_fun_params_count=fun_params(follows); } int stk_index=2; word lbbranch=m_words.get(); if(lbbranch.m_type==ewt_key_lbbranch) { grammar_debug(lbbranch); body(follows,stk_index,NULL); } else m_words.push(lbbranch); gen_code(e_ret);//如果没有写返回语句,加一句默认的返回语句 p_jmp->m_addr=merge_code(); pop_table(); pop_gener(); } else { report_error("语法错误:函数定义缺少函数名\n"); m_words.push(ident); } test_and_skip(follows,_create_syms()); }
//略过第一个符号 '(' int grammar_parser::fun_params(set<e_word_t> follows) { int params_count=0; vector<_symbol*> syms; while(true) { word tmp=m_words.get(); if(tmp.m_type==ewt_ident) { grammar_debug(tmp); _symbol* p_sym=push_symbol(_symbol(es_value,tmp.m_str_value,eab_reg_sb,params_count)); if(p_sym) syms.push_back(p_sym); params_count++; } else if(tmp.m_type==ewt_key_rsbranch) { grammar_debug(tmp); for(vector<_symbol*>::iterator it=syms.begin();it!=syms.end();it++) { (*it)->m_addr-=(params_count+2); } return params_count; } else { m_words.push(tmp); break; } word comma=m_words.get(); if(comma.m_type==ewt_key_comma) { grammar_debug(comma); continue; } else m_words.push(comma); } test_and_skip(follows,_create_syms()); return 0; }
void graph(FILE * fp) { static char buf[BUFLNG], arg[BUFLNG / 2], xtype[16], ytype[16]; static double xa, ya, xap, yap, xmin, xmax, ymin, ymax; static double xs = -NSCALE, ys = -NSCALE; int n, c; char *s, *p; double x, y, lpt, th, dt, lscale, rad; int is_grid, old_lbl = 0; char xory; h *= fct; w *= fct; for (n = 0; (s = fgets(buf, BUFLNG, fp));) { s = getarg(s, arg); if (s == NULL || *arg == '#'); /* comment line */ else if ((!is_t && strcmp(arg, "x") == 0) || (is_t && strcmp(arg, "y") == 0)) { s = gettyp(s, xtype); if (sscanf(s, "%lf %lf %lf", &xmin, &xmax, &xa) != 3) xa = xmin; if (strncmp(xtype, "log", 3) == 0) { xmin = log10(xmin); xmax = log10(xmax); xa = log10(xa); is_xlog = (xtype[3] == '*') ? -1 : 1; } xfct = xl / (xmax - xmin); xap = (xa - xmin) * xfct; x00 = -xmin * xfct; } else if ((!is_t && strcmp(arg, "y") == 0) || (is_t && strcmp(arg, "x") == 0)) { s = gettyp(s, ytype); if (sscanf(s, "%lf %lf %lf", &ymin, &ymax, &ya) != 3) ya = ymin; if (strncmp(ytype, "log", 3) == 0) { ymin = log10(ymin); ymax = log10(ymax); ya = log10(ya); is_ylog = (ytype[3] == '*') ? -1 : 1; } yfct = (yl) ? yl / (ymax - ymin) : 0; yap = (ya - ymin) * yfct; y00 = -ymin * yfct; } else if ((!is_t && strncmp(arg, "xscale", 6) == 0) || (is_t && strncmp(arg, "yscale", 6) == 0)) { is_grid = *(arg + 6); if (type < 0 || (ya != ymin && ya != ymax)) { plot(0.0, yap, 3); plot(xl, yap, 2); } ys = yap - h - MSCALE; while ((s = getarg(s, p = arg)) != NULL) { if (*p != '"') { x = atof((is_number(*p)) ? p : p + 1); if (strncmp(xtype, "mel", 3) == 0) x = argapf(x / nz(xmax, xmin), atof(xtype + 3)) * nz(xmax, xmin); else if (is_xlog) x = log10(x); x = (x - xmin) * xfct; lscale = (*p == 's') ? LSCALE / 2 : LSCALE; if (*p != '\\' && *p != '@') { plot(x, yap, 3); plot(x, yap + lscale, 2); if (type > 0 && !is_grid && yap == 0) { plot(x, yl, 3); plot(x, yl - lscale, 2); } } else if (*p == '\\') ++p; } if (is_number(*p) || *p++ == '"') _symbol(x - sleng(p, h, w) / 2, ys - ysadj(), p, h, w, 0.0); } } else if ((!is_t && strncmp(arg, "yscale", 6) == 0) || (is_t && strncmp(arg, "xscale", 6) == 0)) { is_grid = *(arg + 6); if (type < 0 || (xa != xmin && xa != xmax)) { plot(xap, 0.0, 3); plot(xap, yl, 2); } while ((s = getarg(s, p = arg)) != NULL) { if (*p != '"') { y = atof((is_number(*p)) ? p : p + 1); if (strncmp(ytype, "mel", 3) == 0) y = argapf(y / nz(ymax, ymin), atof(ytype + 3)) * nz(ymax, ymin); else if (is_ylog) y = log10(y); y = (y - ymin) * yfct; lscale = (*p == 's') ? LSCALE / 2 : LSCALE; if (*p != '\\' && *p != '@') { plot(xap, y, 3); plot(xap + lscale, y, 2); if (type > 0 && !is_grid && xap == 0) { plot(xl, y, 3); plot(xl - lscale, y, 2); } } else if (*p == '\\') ++p; } if (is_number(*p) || *p++ == '"') { x = xap - sleng(p, h, w) - MSCALE; if (x < xs) xs = x; _symbol(x, y - h * 0.5, p, h, w, 0.0); } } } else if (strcmp(arg + 1, "grid") == 0) { draw_fig0(xbuf, ybuf, n, wf, hf, fct); if ((!is_t && (*arg == 'x')) || (is_t && (*arg == 'y'))) { ybuf[0] = 0; ybuf[1] = yl; while ((s = getarg(s, arg)) != NULL) { x = atof(arg); if (is_xlog) x = log10(x); xbuf[0] = xbuf[1] = (x - xmin) * xfct; draw_fig0(xbuf, ybuf, 2, wf, hf, fct); } } else { xbuf[0] = 0; xbuf[1] = xl; while ((s = getarg(s, arg)) != NULL) { y = atof(arg); if (is_ylog) y = log10(y); ybuf[0] = ybuf[1] = (y - ymin) * yfct; draw_fig0(xbuf, ybuf, 2, wf, hf, fct); } } n = 0; } else if (strcmp(arg + 1, "circle") == 0) { xory = *arg; s = getarg(s, arg); x = xt(atof(arg)); s = getarg(s, arg); y = yt(atof(arg)); swap(&x, &y); x = xfct * x + x00; y = yfct * y + y00; while ((s = getarg(s, arg)) != NULL) { if ((!is_t && xory == 'x') || (is_t && xory == 'y')) rad = xt(atof(arg)) * xfct; else rad = yt(atof(arg)) * yfct; pntstyl(ptyp); circle(x, y, rad, rad, 0., 360.); } } else if (strcmp(arg, "circle") == 0) { s = getarg(s, arg); x = xt(atof(arg)); s = getarg(s, arg); y = yt(atof(arg)); swap(&x, &y); x = xfct * x + x00; y = yfct * y + y00; while ((s = getarg(s, arg)) != NULL) { rad = atof(arg); pntstyl(ptyp); circle(x, y, rad, rad, 0., 360.); } } else if (strcmp(arg + 1, "name") == 0) { s = getname(s, p = arg + 1); if ((!is_t && *arg == 'x') || (is_t && *arg == 'y')) _symbol((xl - sleng(s, h, w)) / 2, (*p) ? -atof(p) - h : ys - h - NSCALE, s, h, w, 0.0); else _symbol((*p) ? -atof(p) : xs - MSCALE, (yl - sleng(s, h, w)) / 2, s, h, w, 90.0); } else if (strncmp(arg, "title", 5) == 0 || strncmp(arg, "print", 5) == 0) { sscanf(s, "%lf %lf", &x, &y); swap(&x, &y); if (*arg == 'p') { x = xfct * xt(x) + x00; y = yfct * yt(y) + y00; } s = gettxt_fig(s); th = getarg(s + strlen(s) + 1, arg) ? atof(arg) : 0; if (*(arg + 5)) { x -= rx(LADJ * h / 2, h / 2, th); y -= ry(LADJ * h / 2, h / 2, th); } _symbol(x, y, s, h, w, th); } else if (strcmp(arg, "eod") == 0 || strcmp(arg, "EOD") == 0) { draw_fig0(xbuf, ybuf, n, wf, hf, fct); n = 0; } else if (strcmp(arg, "pen") == 0) { n = flush(xbuf, ybuf, n, wf, hf, fct); pen(atoi(s)); } else if (strcmp(arg, "join") == 0) { n = flush(xbuf, ybuf, n, wf, hf, fct); join(atoi(s)); } else if (strcmp(arg, "csize") == 0) { if (sscanf(s, "%lf %lf", &h, &w) != 2) w = h; } else if (strcmp(arg, "hight") == 0) { if (sscanf(s, "%lf %lf", &mh, &mw) != 2) mw = mh; } else if (strcmp(arg, "line") == 0) { n = flush(xbuf, ybuf, n, wf, hf, fct); if (sscanf(s, "%d %lf", <ype, &lpt) != 2) { if (ltype > 0) lpt = lpit[ltype - 1]; } if (--ltype >= 0) mode(lmod[ltype], lpt); } else if (strcmp(arg, "italic") == 0) italic(atof(s)); else if (strcmp(arg, "mark") == 0) { while (*s == ' ' || *s == '\t') ++s; if (*s == '\\' && *(s + 1) == '0') *label = '\0'; else strcpy(label, s); } else if (strcmp(arg, "paint") == 0) { sscanf(s, "%d %lf %lf", &ptyp, &dhat, &that); } else if (strcmp(arg, "clip") == 0) { draw_fig0(xbuf, ybuf, n, wf, hf, fct); for (n = 0; (s = getarg(s, arg)) != NULL; ++n) { x = xt(atof(arg)); if ((s = getarg(s, arg)) == NULL) break; y = yt(atof(arg)); swap(&x, &y); xbuf[n] = xfct * x + x00; ybuf[n] = yfct * y + y00; } if (n == 0) { xclip0 = yclip0 = 0; xclip1 = xl; yclip1 = yl; swap(&xclip1, &yclip1); } else if (n == 2) { xclip0 = xbuf[0]; yclip0 = ybuf[0]; xclip1 = xbuf[1]; yclip1 = ybuf[1]; } n = 0; } else if (strcmp(arg, "box") == 0) { draw_fig0(xbuf, ybuf, n, wf, hf, fct); for (n = 0; (s = getarg(s, arg)) != NULL; ++n) { x = xt(atof(arg)); if ((s = getarg(s, arg)) == NULL) break; y = yt(atof(arg)); swap(&x, &y); xbuf[n] = xfct * x + x00; ybuf[n] = yfct * y + y00; } if (n == 2) { xbuf[2] = xbuf[1]; ybuf[3] = ybuf[2] = ybuf[1]; ybuf[1] = ybuf[0]; xbuf[3] = xbuf[0]; n = 4; } polyg(xbuf, ybuf, n, wf, hf, fct); n = 0; } else { x = xt(atof(arg)); s = getarg(s, arg); y = yt(atof(arg)); swap(&x, &y); xbuf[n] = x = xfct * x + x00; ybuf[n] = y = yfct * y + y00; if (is_in(x, y) && ((s = getarg(s, arg)) || *label || old_lbl > 0)) { c = 0; if (s || *label) { if (s == NULL) s = getarg(label, arg); if (*arg == '\\' && (abs(c = atoi(arg + 1))) < 16) mark(abs(c), &x, &y, 1, mh, 1); else if (abs(c) == 16) { pntstyl(ptyp); circle(x, y, mh / 2, mh / 2, 0., 360.); } else { if (c) { *arg = c; *(arg + 1) = '\0'; } _symbol(x - LADJ * h / 2, y - w / 2, arg, h, w, atof(s)); } } if (c > 0) n = flush(xbuf, ybuf, n, wf, hf, fct); if ((c > 0 || old_lbl > 0) && n) { dt = atan2(y - ybuf[0], x - xbuf[0]); if (old_lbl > 0) { xbuf[0] += MADJ * mh * cos(dt); ybuf[0] += MADJ * mh * sin(dt); } if (c > 0) { xbuf[1] -= MADJ * mh * cos(dt); ybuf[1] -= MADJ * mh * sin(dt); } draw_fig0(xbuf, ybuf, 2, wf, hf, fct); xbuf[0] = x; ybuf[0] = y; n = 0; } old_lbl = c; } if (++n >= BUFLNG) n = flush(xbuf, ybuf, n, wf, hf, fct); } } draw_fig0(xbuf, ybuf, n, wf, hf, fct); }