public void function_call() { int initStackSize, endStackSize; Lexem hlp = curLex; getLex(); if (curLex.get_type() == Type_of_lex.LEX_openRoundBracket) { getLex(); initStackSize = getStackSize(); int num = call_params(); if (tableId.get_num_of_param(hlp.getName()) != num) { throw new SystemException("wrong num params in function " + hlp.getName() + " call"); } endStackSize = getStackSize(); //убрать изстекалексем оставшееся после работы с параметрами вызова for (int i = 0; i < endStackSize - initStackSize; i++) { pop(); } if (curLex.get_type() != Type_of_lex.LEX_closeRoundBracket) { throw new SystemException(curLex.ToString()); } getLex(); tableId.poliz_put_lex(hlp); } else { throw new SystemException(curLex.ToString()); } }
//Element elem_val;//Если тип переменной - Element //может номер в новой таблице, которая в webBrowser? /// <summary> /// Конструктор /// </summary> /// <param name="lex">лексема</param> /// <param name="elem">значение HtmlElement</param> /// <param name="collect">значение HtmlElementCollection</param> public LexID(Lexem lex, HtmlElement elem = null, List <HtmlElement> collect = null) { html_elem = elem; html_elem_collect = collect;//Не копирование, а просто присваивание ссылки value = ""; name = lex.getName(); if (elem != null) { type = Type_of_lex._htmlelement; value = name; } else if (collect != null) { type = Type_of_lex._htmlelementcollect; value = name; } else if (lex.get_type() != Type_of_lex.LEX_id) { type = lex.get_type(); } else { type = Type_of_lex._undefined; } }
public void addId(Lexem lex) { if (isDeclare(lex)) { throw new SystemException("повторное описание переменной " + lex.getName() + " в функции " + funcName); } tId.Add(new LexID(lex)); curId++; }
//-------------------------- void checkId(bool add = true) { if (!tableId.checkId(curLex)) { throw new SystemException(curLex.getName() + " not declare"); } if (add) { tableId.poliz_put_lex(curLex); } }
public void _const() { switch (curLex.get_type()) { case Type_of_lex.LEX_string: curLex.put_type(Type_of_lex._string); push(curLex); tableId.poliz_put_lex(curLex); getLex(); break; case Type_of_lex.LEX_num: curLex.put_type(Type_of_lex._int); //push(curLex); Lexem hlp = curLex; getLex(); if (curLex.get_type() == Type_of_lex.LEX_dot) //float { getLex(); if (curLex.get_type() != Type_of_lex.LEX_num) { throw new SystemException(curLex.ToString()); } else { //сделать лексему типа double string value = hlp.getName() + "." + curLex.getName(); hlp.put_type(Type_of_lex._double); hlp.putName(value); getLex(); } } push(hlp); tableId.poliz_put_lex(hlp); break; case Type_of_lex.LEX_FALSE: curLex.put_type(Type_of_lex._bool); curLex.putName("false"); push(curLex); tableId.poliz_put_lex(curLex); getLex(); break; case Type_of_lex.LEX_TRUE: curLex.put_type(Type_of_lex._bool); curLex.putName("true"); push(curLex); tableId.poliz_put_lex(curLex); getLex(); break; default: throw new SystemException(curLex.ToString()); } }
public void putValOfLex(Lexem lex, string val) { for (int i = 0; i < tId.Count; i++) { if (tId[i].getName() == lex.getName()) { tId[i].putValue(val); } } }
public void putTypeOfLex(Lexem lex, Type_of_lex type) { for (int i = 0; i < tId.Count; i++) { if (tId[i].getName() == lex.getName()) { tId[i].putType(type); } } }
public bool isDeclare(Lexem lex) { if (getNumOfLex(lex.getName()) != -1) { return(true); } else { return(false); } }
public bool checkId(Lexem lex) { if (funcIdTable[curFunc].isDeclare(lex)) { int num = funcIdTable[curFunc].getNumOfLex(lex.getName()); Type_of_lex type = funcIdTable[curFunc].getTypeOfLex(lex.getName()); funcIdTable[curFunc].push(type); return(true); } else if (isFuncDeclare(lex.getName())) { Type_of_lex type = getTypeOfFunc(lex.getName()); funcIdTable[curFunc].push(type); return(true); } else { return(false); } }
bool id_is_func(Lexem lex) { int num = tableId.getNumOfFunc(lex.getName()); if (num >= 0) { return(true); } else { return(false); } }
bool getBoolFromValue(Lexem lex) { string val = lex.getName(); switch (lex.get_type()) { case Type_of_lex._string: if (val == "") { return(false); } else { return(true); } case Type_of_lex._bool: if (val.ToLower() == "false") { return(false); } else { return(true); } case Type_of_lex._double: if (Convert.ToDouble(val) == 0) { return(false); } else { return(true); } case Type_of_lex._int: if (Convert.ToInt32(val) == 0) { return(false); } else { return(true); } default: return(false); } }
public int getNumOfLex(Lexem lex) { return(funcIdTable[curFunc].getNumOfLex(lex.getName())); }
/// <summary> /// Конструктор /// </summary> /// <param name="lex">Инициализирует экземпляр класса существующей лексемой</param> public Lexem(Lexem lex) { t_lex = lex.get_type(); name = lex.getName(); }
/// <summary> /// Реализует выполнение ПОЛИЗа /// </summary> public void execute() { Lexem lex, lex1, lex2; bool b; string val, hlp_str; int i, j, size = prog.get_free(), num; Type_of_lex t; if (wb.exit_run > 0) { wb.exit_run--; cur_step_poliz++; } while (cur_step_poliz < size) { curLex = prog.arr[cur_step_poliz]; switch (curLex.get_type()) { case Type_of_lex._int: case Type_of_lex._double: case Type_of_lex._string: case Type_of_lex._bool: case Type_of_lex.POLIZ_ADDR: case Type_of_lex.POLIZ_LABEL: args.Push(curLex); break; case Type_of_lex.LEX_id: //Проверили, не вызов ли функции if (!id_is_func(curLex)) { lex = new Lexem(tableId.get_type_of_lex(curLex.getName()), tableId.get_value_of_lex(curLex.getName())); args.Push(lex); } else { if (tableId.getNumOfFunc(curLex.getName()) < tableId.get_inet_func_num()) { bool need_exit_in_wait_loop = run_inet_func(curLex.getName()); if (need_exit_in_wait_loop) { system_print(">>>exit in main wait loop<<<"); wb.exit_run--; cur_step_poliz++; return; } } else { run_func(curLex.getName()); } } break; case Type_of_lex.LEX_ElCcnt: lex = args.Pop(); args.Push(new Lexem(Type_of_lex._int, tableId.get_htmlElemCollect_of_lex(lex.getName()).Count.ToString())); break; case Type_of_lex.LEX_ElCbyNum: lex = args.Pop(); //iterator lex1 = args.Pop(); //elem_collection lex2 = args.Pop(); //elem HtmlElement hc = tableId.get_htmlElemCollect_of_lex(lex1.getName())[Convert.ToInt32(lex.getName())]; tableId.putHtmlElemValueOfLex(tableId.getNumOfLex(lex2), hc); break; case Type_of_lex.LEX_WRITE: //System.Console.WriteLine(args.Pop().getName()); system_print(args.Pop().getName()); break; case Type_of_lex.LEX_READ: lex = args.Pop(); //val = System.Console.ReadLine(); val = system_read(); tableId.putValueOfLex(Convert.ToInt32(lex.getName()), val); break; case Type_of_lex.LEX_not: lex = args.Pop(); args.Push(new Lexem(lex.get_type(), getValueFromBool(!getBoolFromValue(lex)))); break; case Type_of_lex.LEX_or: lex1 = args.Pop(); lex2 = args.Pop(); b = getBoolFromValue(lex1) || getBoolFromValue(lex2); args.Push(new Lexem(Type_of_lex._bool, getValueFromBool(b))); break; case Type_of_lex.LEX_and: lex1 = args.Pop(); lex2 = args.Pop(); b = getBoolFromValue(lex1) && getBoolFromValue(lex2); args.Push(new Lexem(Type_of_lex._bool, getValueFromBool(b))); break; case Type_of_lex.POLIZ_GO: cur_step_poliz = Convert.ToInt32(args.Pop().getName()) - 1; break; case Type_of_lex.POLIZ_FGO: lex1 = args.Pop(); lex2 = args.Pop(); if (!getBoolFromValue(lex2)) { cur_step_poliz = Convert.ToInt32(lex1.getName()) - 1; } break; case Type_of_lex.LEX_UPLUS: break; case Type_of_lex.LEX_UMINUS: lex1 = args.Pop(); val = "-" + lex1.getName(); args.Push(new Lexem(lex1.get_type(), val)); break; case Type_of_lex.LEX_minusMinus: case Type_of_lex.LEX_MINUSMINUSright: lex1 = args.Pop(); num = Convert.ToInt32(lex1.getName()); t = tableId.getTypeofLex(num); hlp_str = tableId.get_value_of_lex(num); switch (t) { case Type_of_lex._string: val = hlp_str.Remove(hlp_str.Length - 1); break; case Type_of_lex._bool: bool a1 = getBoolFromValue(new Lexem(Type_of_lex._bool, hlp_str)); b = !a1; val = getValueFromBool(b); break; case Type_of_lex._double: val = (Convert.ToDouble(hlp_str) - 1).ToString(); break; case Type_of_lex._int: val = (Convert.ToInt32(hlp_str) - 1).ToString(); break; default: val = hlp_str.Remove(hlp_str.Length - 1); break; } tableId.putValueOfLex(Convert.ToInt32(lex1.getName()), val); if (curLex.get_type() == Type_of_lex.LEX_minusMinus) { args.Push(new Lexem(t, val)); } else { args.Push(new Lexem(t, hlp_str)); } break; case Type_of_lex.LEX_plusPlus: case Type_of_lex.LEX_PLUSPLUSright: lex1 = args.Pop(); num = Convert.ToInt32(lex1.getName()); t = tableId.getTypeofLex(num); hlp_str = tableId.get_value_of_lex(num); switch (t) { case Type_of_lex._string: val = hlp_str + " "; break; case Type_of_lex._bool: bool a1 = getBoolFromValue(new Lexem(Type_of_lex._bool, hlp_str)); b = !a1; val = getValueFromBool(b); break; case Type_of_lex._double: val = (Convert.ToDouble(hlp_str) + 1).ToString(); break; case Type_of_lex._int: val = (Convert.ToInt32(hlp_str) + 1).ToString(); break; default: val = hlp_str + " "; break; } tableId.putValueOfLex(Convert.ToInt32(lex1.getName()), val); if (curLex.get_type() == Type_of_lex.LEX_plusPlus) { args.Push(new Lexem(t, val)); } else { args.Push(new Lexem(t, hlp_str)); } break; case Type_of_lex.LEX_plus: lex1 = args.Pop(); lex2 = args.Pop(); t = get_real_type(lex1, lex2); switch (t) { case Type_of_lex._string: val = lex1.getName() + lex2.getName(); break; case Type_of_lex._bool: bool a1 = getBoolFromValue(lex1), a2 = getBoolFromValue(lex2); b = !a1 && a2 || a1 && !a2; val = getValueFromBool(b); break; case Type_of_lex._double: val = (Convert.ToDouble(lex1.getName()) + Convert.ToDouble(lex2.getName())).ToString(); break; case Type_of_lex._int: val = (Convert.ToInt32(lex1.getName()) + Convert.ToInt32(lex2.getName())).ToString(); break; default: val = lex1.getName() + lex2.getName(); break; } args.Push(new Lexem(t, val)); break; case Type_of_lex.LEX_mul: lex1 = args.Pop(); lex2 = args.Pop(); t = get_real_type(lex1, lex2); switch (t) { case Type_of_lex._string: val = lex1.getName() + lex2.getName(); break; case Type_of_lex._bool: bool a1 = getBoolFromValue(lex1), a2 = getBoolFromValue(lex2); b = a1 && a2; val = getValueFromBool(b); break; case Type_of_lex._double: val = (Convert.ToDouble(lex1.getName()) * Convert.ToDouble(lex2.getName())).ToString(); break; case Type_of_lex._int: val = (Convert.ToInt32(lex1.getName()) * Convert.ToInt32(lex2.getName())).ToString(); break; default: val = lex1.getName() + lex2.getName(); break; } args.Push(new Lexem(t, val)); break; case Type_of_lex.LEX_minus: lex1 = args.Pop(); lex2 = args.Pop(); t = get_real_type(lex1, lex2); switch (t) { case Type_of_lex._string: val = ""; break; case Type_of_lex._bool: bool a1 = getBoolFromValue(lex1), a2 = getBoolFromValue(lex2); b = !a1 && a2 || a1 && !a2; val = getValueFromBool(b); break; case Type_of_lex._double: val = (Convert.ToDouble(lex2.getName()) - Convert.ToDouble(lex1.getName())).ToString(); break; case Type_of_lex._int: val = (Convert.ToInt32(lex2.getName()) - Convert.ToInt32(lex1.getName())).ToString(); break; default: val = ""; break; } args.Push(new Lexem(t, val)); break; case Type_of_lex.LEX_slash: lex1 = args.Pop(); lex2 = args.Pop(); t = get_real_type(lex1, lex2); switch (t) { case Type_of_lex._string: val = ""; break; case Type_of_lex._bool: val = getValueFromBool(false); break; case Type_of_lex._double: val = (Convert.ToDouble(lex2.getName()) / Convert.ToDouble(lex1.getName())).ToString(); break; case Type_of_lex._int: val = (Convert.ToInt32(lex2.getName()) / Convert.ToInt32(lex1.getName())).ToString(); break; default: val = ""; break; } args.Push(new Lexem(t, val)); break; case Type_of_lex.LEX_less: lex1 = args.Pop(); lex2 = args.Pop(); t = get_real_type(lex1, lex2); switch (t) { case Type_of_lex._bool: val = ""; break; case Type_of_lex._double: val = (Convert.ToDouble(lex2.getName()) < Convert.ToDouble(lex1.getName())).ToString(); break; case Type_of_lex._int: val = (Convert.ToInt32(lex2.getName()) < Convert.ToInt32(lex1.getName())).ToString(); break; case Type_of_lex._string: default: i = String.Compare(lex2.getName(), lex1.getName()); if (i < 0) { val = "true"; } else { val = "false"; } break; } args.Push(new Lexem(Type_of_lex._bool, val)); break; case Type_of_lex.LEX_bigger: lex1 = args.Pop(); lex2 = args.Pop(); t = get_real_type(lex1, lex2); switch (t) { case Type_of_lex._bool: val = ""; break; case Type_of_lex._double: val = (Convert.ToDouble(lex2.getName()) > Convert.ToDouble(lex1.getName())).ToString(); break; case Type_of_lex._int: val = (Convert.ToInt32(lex2.getName()) > Convert.ToInt32(lex1.getName())).ToString(); break; case Type_of_lex._string: default: i = String.Compare(lex2.getName(), lex1.getName()); if (i > 0) { val = "true"; } else { val = "false"; } break; } args.Push(new Lexem(Type_of_lex._bool, val)); break; case Type_of_lex.LEX_lessEq: lex1 = args.Pop(); lex2 = args.Pop(); t = get_real_type(lex1, lex2); switch (t) { case Type_of_lex._bool: val = ""; break; case Type_of_lex._double: val = (Convert.ToDouble(lex2.getName()) <= Convert.ToDouble(lex1.getName())).ToString(); break; case Type_of_lex._int: val = (Convert.ToInt32(lex2.getName()) <= Convert.ToInt32(lex1.getName())).ToString(); break; case Type_of_lex._string: default: i = String.Compare(lex2.getName(), lex1.getName()); if (i <= 0) { val = "true"; } else { val = "false"; } break; } args.Push(new Lexem(Type_of_lex._bool, val)); break; case Type_of_lex.LEX_biggerEq: lex1 = args.Pop(); lex2 = args.Pop(); t = get_real_type(lex1, lex2); switch (t) { case Type_of_lex._bool: val = ""; break; case Type_of_lex._double: val = (Convert.ToDouble(lex2.getName()) >= Convert.ToDouble(lex1.getName())).ToString(); break; case Type_of_lex._int: val = (Convert.ToInt32(lex2.getName()) >= Convert.ToInt32(lex1.getName())).ToString(); break; case Type_of_lex._string: default: i = String.Compare(lex2.getName(), lex1.getName()); if (i >= 0) { val = "true"; } else { val = "false"; } break; } args.Push(new Lexem(Type_of_lex._bool, val)); break; case Type_of_lex.LEX_neq: val = (args.Pop().getName() != args.Pop().getName()).ToString(); args.Push(new Lexem(Type_of_lex._bool, val)); break; case Type_of_lex.LEX_eq: val = (args.Pop().getName() == args.Pop().getName()).ToString(); args.Push(new Lexem(Type_of_lex._bool, val)); break; case Type_of_lex.LEX_assign: lex1 = args.Pop(); //что присваиваем lex2 = args.Pop(); //куда присваиваем val = lex1.getName(); if (lex1.get_type() != Type_of_lex._htmlelement && lex1.get_type() != Type_of_lex._htmlelementcollect) { tableId.putValueOfLex(Convert.ToInt32(lex2.getName()), val); } //в вычислителе лежало имя, а реальное значение надо взять из таблицы идентификаторов else { tableId.putValueOfLex(Convert.ToInt32(lex2.getName()), val, true); } break; } cur_step_poliz++; }//end of while if (cur_step_poliz >= size) { if (wb.isConsoleVersion) { Application.Exit(); } } }
/// <summary> /// Реализует выполнение описанных пользователем функций /// </summary> /// <param name="func_name"></param> public void run_func(string func_name) { int old_curFunc = tableId.get_curFunc(); int num_of_func = tableId.getNumOfFunc(func_name); int num_params = tableId.get_num_of_param(func_name); int old_csp = cur_step_poliz; HtmlElement helem; List <HtmlElement> helemCollect, collect_to_run_func; Lexem[] lex_arr = new Lexem[num_params]; //в массиве лежат либо константы, либо переменные //создать новый экземпляр класса executer и вызвать из него выполнение проги setCurFunc(num_of_func); //Да, ничего не скопирует, но рекурсия станет возможной Executer func_exec = new Executer(tableId, in_stream, out_stream, wb); //передача параметров в вызываемую функцию for (int i = num_params - 1; i >= 0; i--) { lex_arr[i] = args.Pop(); } //обнуление всех переменных внутри функции //func_exec.tableId.initialize_all_vars(); for (int i = 0; i < num_params; i++) { if (lex_arr[i].get_type() == Type_of_lex._htmlelement || lex_arr[i].get_type() == Type_of_lex._htmlelementcollect) { //func_exec.tableId.putValueOfLex(i, lex_arr[i].getName(), true); setCurFunc(old_curFunc); if (lex_arr[i].get_type() == Type_of_lex._htmlelement) { helem = func_exec.tableId.get_htmlElem_of_lex(lex_arr[i].getName()); setCurFunc(num_of_func); func_exec.tableId.putHtmlElemValueOfLex(i, helem); } else { helemCollect = tableId.get_htmlElemCollect_of_lex(lex_arr[i].getName()); setCurFunc(num_of_func); collect_to_run_func = new List <HtmlElement>(helemCollect.Count); for (int j = 0; j < helemCollect.Count; j++) { collect_to_run_func.Add(helemCollect[j]); } //тут нужно передать не ссылку, а новый список func_exec.tableId.putHtmlElemCollectValueOfLex(i, collect_to_run_func); } } else { func_exec.tableId.putValueOfLex(i, lex_arr[i].getName()); } if (lex_arr[i].get_type() != Type_of_lex._undefined) { func_exec.putTypeofLex(i, lex_arr[i].get_type()); } } func_exec.execute(); Lexem lex = null; if (func_exec.args.Count > 0) { lex = func_exec.args_pop(); } //для добавления вспомогательной переменной if (lex != null) { Type_of_lex type = tableId.getTypeOfFunc(func_name); if (type == Type_of_lex._htmlelement || type == Type_of_lex._htmlelementcollect) { string res_name = ""; HtmlElement htmlElem = null; List <HtmlElement> htmlElemCollect = null; if (type == Type_of_lex._htmlelement) { htmlElem = tableId.get_htmlElem_of_lex(lex.getName()); htmlElemCollect = null; } else { htmlElemCollect = tableId.get_htmlElemCollect_of_lex(lex.getName()); htmlElem = null; } setCurFunc(old_curFunc); res_name = add_new_uniq_lexId(type, htmlElem, htmlElemCollect); lex.putName(res_name); } args.Push(lex); } setCurFunc(old_curFunc); cur_step_poliz = old_csp; }