public bool ReplaceValueByTravarse(int itype, YVALUE dst) //トラバースして、最初に見つけたのを入れ替える。 { if (itype == type) //自身の入れ替えはNG { sys.logline("Unexpected. Cannot replace self"); return(false); } if (list == null) { return(false); } for (int i = 0; i < list.Count; i++) { if (list[i].type == itype) { list[i] = dst; return(true); } } for (int i = 0; i < list.Count; i++) { if (list[i].ReplaceValueByTravarse(itype, dst)) { return(true); } } return(false); }
private static YVALUE find(YVALUE v, object[] o) { var t = getyp(o); var fv = v.FindValueByTravarse(t); return(fv); }
static YVALUE any_return(YVALUE v, int type, number?n = null, string s = null) { v.type = type; if (n != null) { v.o = v.n = (number)n; } if (s != null) { v.s = s; if (v.o == null) { v.o = v.s; } } if (type == YDEF.BOOL) { v.o = (bool)(s.ToUpper() == "TRUE"); } if (v.o == null) { v.o = type; } return(v); }
internal bool CheckOne() //no more then return false; { if (m_l >= m_lines.Length) { var vtmp = new YVALUE(); vtmp.type = YDEF.EOF; add(vtmp); end_line(); return(false); } int wdlen; var v = lexUtil.GetWord(ref m_bInComment, out wdlen, m_c, m_lines[m_l], m_l, m_file_id); m_c += wdlen; add(v); if (v.type == YDEF.ERROR) { sys.error(string.Format("L:{0}C:{1}>{2}", v.dbg_line, v.dbg_col, v.s)); return(false); } if (v.type == YDEF.EOL) { end_line(); m_l++; m_c = 0; } return(true); }
public List <List <YVALUE> > Del_LF_And_Add_BOF_EOF(List <List <YVALUE> > src) { List <YVALUE> dst = new List <YVALUE>(); dst.Add(YVALUE.BOF()); foreach (var l in src) { foreach (var v in l) { if (v.IsType(YDEF.EOL)) { continue; } dst.Add(v); } } if (dst.Count > 0 && dst[dst.Count - 1].type != YDEF.EOF) { dst.Add(YVALUE.EOF()); } var final = new List <List <YVALUE> >(); final.Add(dst); return(final); }
// 16進数専用処理. ls[0]='0' ls[1]='x' static YVALUE isaHexNumber(YVALUE v, string ls, out int wdlen) { wdlen = 0; string s = "0x"; //決め打ち. for (int i = 2; i < ls.Length; i++) { if (IsHexNumberElement(ls[i])) { s += ls[i]; } else { wdlen = i; break; } } if (wdlen == 0) { wdlen = ls.Length; } System.Int32 i32; if (System.Int32.TryParse(s.Substring(2), System.Globalization.NumberStyles.HexNumber, null, out i32)) { return(any_return(v, YDEF.NUM, i32, s)); } else { return(err_return(v, s)); } }
void add(YVALUE v) { if (m_curline_value == null) { m_curline_value = new List <YVALUE>(); } m_curline_value.Add(v); }
public static YVALUE EOF() { var v = new YVALUE(); v.type = YDEF.EOF; v.s = "EOF"; v.o = v.s; return(v); }
public object CallFunc(YVALUE func, object[] param = null) { if (func != null) { //m_curslag = this; List <object> ol = param != null ? new List <object>(param) : null; m_statebuf = runtime.util.CallFunction(func, ol, m_statebuf); return(m_statebuf.m_cur); } throw new SystemException("CallFunc : ファンクションがありません : " + func.GetFunctionName()); }
public static string GetLineAndCol(YVALUE v) { if (v == null) { return("Line:?,Col:?"); } var line = v.get_dbg_line(true); var col = v.get_dbg_col(true); var file = v.get_dbg_file(); return(string.Format("Line:{0},Col:{1} in {2}", line, col, file)); }
internal static void error(string s, YVALUE v = null) { int line = -1; if (v != null) { line = v.get_dbg_line(true); } string es = "ERROR" + (line >= 0 ? "(L:" + line.ToString() + ")" : "") + ":" + s; conWriteLine(es); throw new SystemException(es); }
private int _countParamsInBracket(YVALUE v) { if (v == null || v.list == null || v.list.Count < 2 || v.type != YDEF.get_type(YDEF.sx_expr_bracket)) { return(0); } // c n // "()" : 2->0 n = ((c-2)+1) / 2 c=2 n=0.5...=0 // "(x)" : 3->1 // "(x,y)" : 5->2 // "(x,y,z)": 7->3 int num = ((v.list.Count - 2) + 1) / 2; return(num); }
public static string PrintValue(YVALUE v) { foreach (var e in Enum.GetValues(typeof(TOKEN))) { var i = (int)e; if (i > 0 && i < (int)TOKEN.MAX) { YVALUE find = v.IsType(i) ? v.FindValueByTravarse(i) : null; if (find != null) { if (i == (int)TOKEN.BOF || i == (int)TOKEN.EOF) { return("----" + NL); } return(find.o.ToString()); } } } Func <string, string> conv = (j) => { if (j == ";") { return(j + NL); } if (j == "{") { return(NL + j + NL); } if (j == "}") { return(j + NL); } if (string.IsNullOrEmpty(j)) { return(j); } return(j.StartsWith(" ") ? j : " " + j); }; string s = ""; if (v.list != null) { v.list.ForEach(i => s += conv(PrintValue(i))); } return(s); }
private static bool replace_l(List <YVALUE> l, int typ, YVALUE dst) { for (int i = 0; i < l.Count; i++) { var v = l[i]; if (v.type == typ) { l[i] = dst; return(true); } } for (int i = 0; i < l.Count; i++) { var v = l[i]; var b = replace(v, typ, dst); if (b) { return(true); } } return(false); }
// -- util for this clas -- static YVALUE err_return(YVALUE v, string s) { v.type = YDEF.ERROR; v.o = v.s = s; return(v); }
static YVALUE op_return(YVALUE v, string s) { // 参照 wikipedia:「演算子の優先順位」 v.s = s; v.o = v.s; int type = YDEF.UNKNOWN; string[] op3 = { "*", "/", "%" }; string[] op4 = { "+", "-" }; string[] op6 = { "<", "<=", ">", ">=" }; string[] op7 = { "==", "!=" }; string[] op11 = { "||" }; string[] op12 = { "&&" }; //string[] op14 = {"," }; string[] incop = { "++", "--" }; string[] asinop = { "=", "+=", "-=", "*=", "/=", "%=" }; string comma = ","; string period = "."; if (type == YDEF.UNKNOWN && Array.FindIndex(op3, i => i == s) >= 0) { type = YDEF.OP3; } if (type == YDEF.UNKNOWN && Array.FindIndex(op4, i => i == s) >= 0) { type = YDEF.OP4; } if (type == YDEF.UNKNOWN && Array.FindIndex(op6, i => i == s) >= 0) { type = YDEF.OP6; } if (type == YDEF.UNKNOWN && Array.FindIndex(op7, i => i == s) >= 0) { type = YDEF.OP7; } if (type == YDEF.UNKNOWN && Array.FindIndex(op11, i => i == s) >= 0) { type = YDEF.OP11; } if (type == YDEF.UNKNOWN && Array.FindIndex(op12, i => i == s) >= 0) { type = YDEF.OP12; } //if (type==YDEF.UNKNOWN && Array.FindIndex(op14,i=>i==s)>=0) type =YDEF.OP14; if (type == YDEF.UNKNOWN && Array.FindIndex(incop, i => i == s) >= 0) { type = YDEF.INCOP; } if (type == YDEF.UNKNOWN && Array.FindIndex(asinop, i => i == s) >= 0) { type = YDEF.ASINOP; } if (type == YDEF.UNKNOWN) { if (s == period) { type = YDEF.PERIOD; } else if (s == comma) { type = YDEF.COMMA; } else { type = YDEF.OP; } } v.type = type; return(v); }
private static bool _isMatchAndMake(List <YVALUE> list, int index, YDEF.TreeSet ts) { Func <int, YVALUE> get = (n) => { if (n >= list.Count) { return(null); } return(list[n]); }; List <YVALUE> args = new List <YVALUE>(); int removelength = ts.list.Count; for (int i = 0; i < ts.list.Count; i++) { var v = get(index + i); if (v == null) { return(false); } if (i == 0 && ts.list.Count == 1 && v.IsType(ts.type)) { return(false); //既に変換済み } int tstype = 0; var o = ts.list[i]; YVALUE nv = null; if (o.GetType() == typeof(string)) { if (v.GetString() == (string)o) { nv = v.GetTerminalValue_ascent(); } else { return(false); } } else { tstype = (int)o; if (tstype == YDEF.REST) // ※RESTは特殊処理。EOLまでのすべて(除EOL)が入る { removelength--; //本VALUE分を事前に引く var restv = new YVALUE(); restv.type = YDEF.REST; restv.list = new List <YVALUE>(); for (int j = index + i; j < list.Count; j++) { var v2 = get(j); if (v2 != null && !v2.IsType(YDEF.EOL)) { restv.list.Add(v2); removelength++; } else { break; } } args.Add(restv); break; } else { if (v.IsType(tstype)) { nv = v.FindValueByTravarse(tstype); } else { return(false); } } } if (nv != null) { args.Add(nv); } else { args.Add(v); } } //Yes, they match! then Make it. //var makefunc = (Func<int, YVALUE[], int[], YVALUE>)ts.make_func; var newv = ts.make_func(ts.type, args.ToArray(), ts.make_index.ToArray()); list.RemoveRange(index, removelength); list.Insert(index, newv); return(true); }
private static bool replace(YVALUE src, int typ, YVALUE dst) { return(src.ReplaceValueByTravarse(typ, dst)); }
private static YVALUE find(YVALUE v, int typ) { var fv = v.FindValueByTravarse(typ); return(fv); }
public void Goto(slagtool.YVALUE v) { m_sm.Goto(v); }
internal static YVALUE GetWord(ref bool bInComment, out int wdlen, int col, string i_line, int dbg_line = -1, int dbg_file_id = -1) { var v = new YVALUE(); v.type = YDEF.UNKNOWN; v.dbg_col = col; v.dbg_line = dbg_line; v.dbg_file_id = dbg_file_id; wdlen = 0; if (string.IsNullOrEmpty(i_line)) { return(any_return(v, YDEF.EOL)); } var line = i_line.TrimEnd(); if (string.IsNullOrEmpty(line) || col >= line.Length) { return(any_return(v, YDEF.EOL)); } var ls = i_line.Substring(col); if (string.IsNullOrEmpty(ls)) { return(any_return(v, YDEF.EOL)); } //※以降 lsには最低1文字あり //スラッシュ・アスタリスクコメント中の場合 if (bInComment) { /* Comment End を検索 */ var idx = ls.IndexOf(YDEF.CMTEND); if (idx < 0) { wdlen = ls.Length; return(any_return(v, YDEF.CMT, null, ls)); } else { bInComment = false; wdlen = idx + 2; return(any_return(v, YDEF.CMT, null, ls)); } } //スラッシュアスタリスクコメント開始時 if (ls.StartsWith(YDEF.CMTBGN)) { bInComment = true; wdlen = 2; return(any_return(v, YDEF.CMT, null, ls)); } //コメントは全部 if (ls.StartsWith(YDEF.CMTSTR)) { wdlen = ls.Length; return(any_return(v, YDEF.CMT, null, ls)); } //ダブルクォーテーションで囲まれた文字列はそのまま if (ls.StartsWith(YDEF.DQ)) { if (ls.Length > 1) { var idx = ls.IndexOf(YDEF.DQ, 1); if (idx < 0) { return(err_return(v, "End of Double Quatation is not found:1")); } wdlen = idx + 1; return(any_return(v, YDEF.QSTR, null, ls.Substring(0, idx + 1))); } else { return(err_return(v, "End of Double Quation is not found:2")); } } //連続したスペース・タブはそのまま if (ls[0] <= ' ') { for (var i = 0; i < ls.Length; i++) { if (ls[i] > ' ') { wdlen = i; return(any_return(v, YDEF.SP, null, " ")); } } return(any_return(v, YDEF.EOL)); } //数字 if (IsNumberElement(ls[0])) { wdlen = 0; string s = "" + ls[0]; for (int i = 1; i < ls.Length; i++) { if (IsNumberElement(ls[i])) { s += ls[i]; } else { wdlen = i; break; } } if (wdlen == 0) { wdlen = ls.Length; } number d; if (number.TryParse(s, out d)) { return(any_return(v, YDEF.NUM, d, s)); } else if (s == ".") { return(any_return(v, YDEF.PERIOD, null, ".")); } else { return(err_return(v, s)); } } //名前 if (IsNameElement(ls[0])) { wdlen = 0; string s = "" + ls[0]; for (int i = 1; i < ls.Length; i++) { if (IsNameElement(ls[i], true)) { s += ls[i]; } else { wdlen = i; break; } } if (wdlen == 0) { wdlen = ls.Length; } switch (s.ToUpper()) { case "FUNCTION": return(any_return(v, YDEF.FUNCTION, null, s)); case "VAR": return(any_return(v, YDEF.VAR, null, s)); case "IF": return(any_return(v, YDEF.IF, null, s)); case "ELSE": return(any_return(v, YDEF.ELSE, null, s)); case "FOR": return(any_return(v, YDEF.FOR, null, s)); case "WHILE": return(any_return(v, YDEF.WHILE, null, s)); case "SWITCH": return(any_return(v, YDEF.SWITCH, null, s)); case "CASE": return(any_return(v, YDEF.CASE, null, s)); case "DEFAULT": return(any_return(v, YDEF.DEFAULT, null, s)); case "BREAK": return(any_return(v, YDEF.BREAK, null, s)); case "CONTINUE": return(any_return(v, YDEF.CONTINUE, null, s)); case "RETURN": return(any_return(v, YDEF.RETURN, null, s)); case "NEW": return(any_return(v, YDEF.NEW, null, s)); case "IN": return(any_return(v, YDEF.IN, null, s)); case "TRUE": return(any_return(v, YDEF.BOOL, null, s)); case "FALSE": return(any_return(v, YDEF.BOOL, null, s)); case "NULL": return(any_return(v, YDEF.NULL, null, s)); } return(any_return(v, YDEF.NAME, null, s)); } //計算記号 2文字を先に。 "="は除外 string[] ops = lexPrimitive.operators_all;//{"++","--","==","!=","<=",">=","+=","-=","*=","/=","%=","&&","||","=","+","-","*","/","%",">","<","!",",","."}; //未サポート含む foreach (var op in ops) { if (op.Length == 2 && ls.Length >= 2) { var s = ls.Substring(0, 2); if (op == s) { wdlen = 2; return(op_return(v, s)); } } if (op.Length == 1 && ls.Length >= 1) { var s = ls.Substring(0, 1); if (op == s) { wdlen = 1; return(op_return(v, s)); } } } //その他 記号とみなす wdlen = 1; return(any_return(v, YDEF.OTR, null, "" + ls[0])); }