Example #1
0
 /// <summary>
 /// Finds the object that best matches the phrase
 /// </summary>
 /// <param name="phrase">The words to match</param>
 /// <param name="index">The first word in phrase to match</param>
 /// <param name="score">The score for the best match: 0 if no match[out]</param>
 /// <param name="numWordsMatch">The number of words that match [out]</param>
 /// <param name="bestMatch">The object that best matches [out]</param>
 /// <param name="seen">Set of objects already examined</param>
 public void match(Lexer lexer, ref double score,
            ref int numWordsMatch,
            ref object bestMatch,
            ref LexState lexerState, Dictionary<object,object> seen)
 {
     // Skip if we've already seen this object
       if (seen.ContainsKey(this))
      return;
       // Add ourself to prevent cycles
       seen[this] = this;
     //      edge.match(lexer, ref score, ref numWordsMatch, ref bestMatch, ref lexerState, seen);
     #if true
       /// We only check to see if the gate matches the phrase
       if (null != edge.gate)
      edge.gate.match(lexer, ref score, ref numWordsMatch,
                          ref bestMatch, ref lexerState, seen);
       // Should only match if no door, or door is open
       if (null != edge.sink && (null == edge.gate || isOpen(edge.gate)))
       {
      // we only want match -- matchInContext checks the stuff from the parents..
      edge.sink.match(lexer, ref score, ref numWordsMatch,
                  ref bestMatch, ref lexerState, seen);
       }
     #endif
 }
Example #2
0
 public static void LuaXLexError(LexState ls, CharPtr msg, int token)
 {
     CharPtr buff = new char[MAXSRC];
     LuaOChunkID(buff, GetStr(ls.source), MAXSRC);
     msg = LuaOPushFString(ls.L, "%s:%d: %s", buff, ls.linenumber, msg);
     if (token != 0)
         LuaOPushFString(ls.L, "%s near " + LUA_QS, msg, TextToken(ls, token));
     LuaDThrow(ls.L, LUA_ERRSYNTAX);
 }
   /// <summary>
   /// Finds the object that best directly matches the phrase.
   /// 
   /// This naively finds the single best match.  Future is to return a list
   /// </summary>
   /// <param name="lexer">The text being matched</param>
   /// <param name="numWordsMatch">The number of words that match [out]</param>
   /// <returns>The object that matches; null on error</returns>
   object matchInContext(ZObject addressedTo, Lexer lexer, out int numWordsMatch,
                          ref LexState lexerState)
   {
      // A table of already examined objects
      var seen = new Dictionary<object,object>();

      // The score board for the search
      var score     = 0.0; // The score for the best match: 0 if no match
      numWordsMatch =   1; // The number of words that match: Minimum of 1
      object bestMatch = null;// The object that best matches
      matchInContext(addressedTo, lexer, ref score, ref numWordsMatch,
                     ref bestMatch, ref lexerState, seen);

      // Return the best match
      return bestMatch;
   }
Example #4
0
 /// <summary>
 /// Finds the object that best matches the phrase
 /// </summary>
 /// <param name="phrase">The words to match</param>
 /// <param name="index">The first word in phrase to match</param>
 /// <param name="score">The score for the best match: 0 if no match[out]</param>
 /// <param name="numWordsMatch">The number of words that match [out]</param>
 /// <param name="bestMatch">The object that best matches [out]</param>
 /// <param name="seen">Set of objects already examined</param>
 internal static void match(Edge<ZObject> edge, Lexer lexer, ref double score,
            ref int numWordsMatch,
            ref object bestMatch,
            ref LexState lexerState, Dictionary<object,object> seen)
 {
     /// We only check to see if the gate matches the phrase
       if (null != edge.gate)
      edge.gate.match(lexer, ref score, ref numWordsMatch,
                          ref bestMatch, ref lexerState, seen);
       // Should only match if no door, or door is open
       if (null != edge.sink && (null == edge.gate /*|| isOpen(gate)*/))
       {
      // we only want match -- matchInContext checks the stuff from the parents..
      edge.sink.match(lexer, ref score, ref numWordsMatch,
                  ref bestMatch, ref lexerState, seen);
       }
 }
Example #5
0
 private static void primaryexp(LexState ls, expdesc v)
 {
     /* primaryexp .
         prefixexp { `.' NAME | `[' exp `]' | `:' NAME funcargs | funcargs } */
       FuncState fs = ls.fs;
       prefixexp(ls, v);
       for (;;) {
     switch (ls.t.token) {
       case '.': {  /* field */
         field(ls, v);
         break;
       }
       case '[': {  /* `[' exp1 `]' */
         expdesc key = new expdesc();
         LuaKExp2AnyReg(fs, v);
         yindex(ls, key);
         LuaKIndexed(fs, v, key);
         break;
       }
       case ':': {  /* `:' NAME funcargs */
         expdesc key = new expdesc();
         LuaXNext(ls);
         checkname(ls, key);
         LuaKSelf(fs, v, key);
         funcargs(ls, v);
         break;
       }
       case '(': case (int)RESERVED.TK_STRING: case '{': {  /* funcargs */
         LuaKExp2NextReg(fs, v);
         funcargs(ls, v);
         break;
       }
       default: return;
     }
       }
 }
Example #6
0
 /* }====================================================================== */
 private static void parlist(LexState ls)
 {
     /* parlist . [ param { `,' param } ] */
       FuncState fs = ls.fs;
       Proto f = fs.f;
       int nparams = 0;
       f.is_vararg = 0;
       if (ls.t.token != ')') {  /* is `parlist' not empty? */
     do {
       switch (ls.t.token) {
         case (int)RESERVED.TK_NAME: {  /* param . NAME */
           new_localvar(ls, str_checkname(ls), nparams++);
           break;
         }
         case (int)RESERVED.TK_DOTS: {  /* param . `...' */
           LuaXNext(ls);
     #if LUA_COMPAT_VARARG
           /* use `arg' as default name */
           new_localvarliteral(ls, "arg", nparams++);
           f.is_vararg = VARARG_HASARG | VARARG_NEEDSARG;
     #endif
           f.is_vararg |= VARARG_ISVARARG;
           break;
         }
         default: LuaXSyntaxError(ls, "<name> or " + LUA_QL("...") + " expected"); break;
       }
     } while ((f.is_vararg==0) && (testnext(ls, ',')!=0));
       }
       adjustlocalvars(ls, nparams);
       f.numparams = CastByte(fs.nactvar - (f.is_vararg & VARARG_HASARG));
       LuaKReserveRegs(fs, fs.nactvar);  /* reserve register for parameters */
 }
Example #7
0
 private static void new_localvar(LexState ls, TString name, int n)
 {
     FuncState fs = ls.fs;
       luaY_checklimit(fs, fs.nactvar+n+1, LUAI_MAXVARS, "local variables");
       fs.actvar[fs.nactvar+n] = (ushort)registerlocalvar(ls, name);
 }
Example #8
0
 private static void localstat(LexState ls)
 {
     /* stat . LOCAL NAME {`,' NAME} [`=' explist1] */
       int nvars = 0;
       int nexps;
       expdesc e = new expdesc();
       do {
     new_localvar(ls, str_checkname(ls), nvars++);
       } while (testnext(ls, ',') != 0);
       if (testnext(ls, '=') != 0)
     nexps = explist1(ls, e);
       else {
     e.k = expkind.VVOID;
     nexps = 0;
       }
       adjust_assign(ls, nvars, nexps, e);
       adjustlocalvars(ls, nvars);
 }
Example #9
0
 private static void listfield(LexState ls, ConsControl cc)
 {
     expr(ls, cc.v);
       luaY_checklimit(ls.fs, cc.na, MAXINT, "items in a constructor");
       cc.na++;
       cc.tostore++;
 }
Example #10
0
 private static void ifstat(LexState ls, int line)
 {
     /* ifstat . IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */
       FuncState fs = ls.fs;
       int flist;
       int escapelist = NO_JUMP;
       flist = test_then_block(ls);  /* IF cond THEN block */
       while (ls.t.token == (int)RESERVED.TK_ELSEIF) {
     LuaKConcat(fs, ref escapelist, LuaKJump(fs));
     LuaKPatchToHere(fs, flist);
     flist = test_then_block(ls);  /* ELSEIF cond THEN block */
       }
       if (ls.t.token == (int)RESERVED.TK_ELSE) {
     LuaKConcat(fs, ref escapelist, LuaKJump(fs));
     LuaKPatchToHere(fs, flist);
     LuaXNext(ls);  /* skip ELSE (after patch, for correct line info) */
     block(ls);  /* `else' part */
       }
       else
     LuaKConcat(fs, ref escapelist, flist);
       LuaKPatchToHere(fs, escapelist);
       check_match(ls, (int)RESERVED.TK_END, (int)RESERVED.TK_IF, line);
 }
Example #11
0
 private static void expr(LexState ls, expdesc v)
 {
     subexpr(ls, v, 0);
 }
Example #12
0
 private static int explist1(LexState ls, expdesc v)
 {
     /* explist1 . expr { `,' expr } */
       int n = 1;  /* at least one expression */
       expr(ls, v);
       while (testnext(ls, ',') != 0) {
     LuaKExp2NextReg(ls.fs, v);
     expr(ls, v);
     n++;
       }
       return n;
 }
Example #13
0
 private static int exp1(LexState ls)
 {
     expdesc e = new expdesc();
       int k;
       expr(ls, e);
       k = (int)e.k;
       LuaKExp2NextReg(ls.fs, e);
       return k;
 }
Example #14
0
 private static void error_expected(LexState ls, int token)
 {
     LuaXSyntaxError(ls,
       LuaOPushFString(ls.L, LUA_QS + " expected", LuaXTokenToString(ls, token)));
 }
Example #15
0
 private static void enterlevel(LexState ls)
 {
     if (++ls.L.nCcalls > LUAI_MAXCCALLS)
     LuaXLexError(ls, "chunk has too many syntax levels", 0);
 }
Example #16
0
 private static int funcname(LexState ls, expdesc v)
 {
     /* funcname . NAME {field} [`:' NAME] */
       int needself = 0;
       singlevar(ls, v);
       while (ls.t.token == '.')
     field(ls, v);
       if (ls.t.token == ':') {
     needself = 1;
     field(ls, v);
       }
       return needself;
 }
Example #17
0
 private static void funcstat(LexState ls, int line)
 {
     /* funcstat . FUNCTION funcname body */
       int needself;
       expdesc v = new expdesc(), b = new expdesc();
       LuaXNext(ls);  /* skip FUNCTION */
       needself = funcname(ls, v);
       body(ls, b, needself, line);
       LuaKStoreVar(ls.fs, v, b);
       LuaKFixLine(ls.fs, line);  /* definition `happens' in the first line */
 }
Example #18
0
 private static void exprstat(LexState ls)
 {
     /* stat . func | assignment */
       FuncState fs = ls.fs;
       LHS_assign v = new LHS_assign();
       primaryexp(ls, v.v);
       if (v.v.k == expkind.VCALL)  /* stat . func */
     SETARG_C(GetCode(fs, v.v), 1);  /* call statement uses no results */
       else {  /* stat . assignment */
     v.prev = null;
     assignment(ls, v, 1);
       }
 }
Example #19
0
 private static void leavelevel(LexState ls)
 {
     ls.L.nCcalls--;
 }
Example #20
0
 /*============================================================*/
 /* GRAMMAR RULES */
 /*============================================================*/
 private static void field(LexState ls, expdesc v)
 {
     /* field . ['.' | ':'] NAME */
       FuncState fs = ls.fs;
       expdesc key = new expdesc();
       LuaKExp2AnyReg(fs, v);
       LuaXNext(ls);  /* skip the dot or colon */
       checkname(ls, key);
       LuaKIndexed(fs, v, key);
 }
Example #21
0
 private static void localfunc(LexState ls)
 {
     expdesc v = new expdesc(), b = new expdesc();
       FuncState fs = ls.fs;
       new_localvar(ls, str_checkname(ls), 0);
       init_exp(v, expkind.VLOCAL, fs.freereg);
       LuaKReserveRegs(fs, 1);
       adjustlocalvars(ls, 1);
       body(ls, b, 0, ls.linenumber);
       LuaKStoreVar(fs, v, b);
       /* debug information will only see the variable after this point! */
       getlocvar(fs, fs.nactvar - 1).startpc = fs.pc;
 }
Example #22
0
 private static void forbody(LexState ls, int base_, int line, int nvars, int isnum)
 {
     /* forbody . DO block */
       BlockCnt bl = new BlockCnt();
       FuncState fs = ls.fs;
       int prep, endfor;
       adjustlocalvars(ls, 3);  /* control variables */
       checknext(ls, (int)RESERVED.TK_DO);
       prep = (isnum != 0) ? LuaKCodeAsBx(fs, OpCode.OP_FORPREP, base_, NO_JUMP) : LuaKJump(fs);
       enterblock(fs, bl, 0);  /* scope for declared variables */
       adjustlocalvars(ls, nvars);
       LuaKReserveRegs(fs, nvars);
       block(ls);
       leaveblock(fs);  /* end of scope for declared variables */
       LuaKPatchToHere(fs, prep);
       endfor = (isnum!=0) ? LuaKCodeAsBx(fs, OpCode.OP_FORLOOP, base_, NO_JUMP) :
                      LuaKCodeABC(fs, OpCode.OP_TFORLOOP, base_, 0, nvars);
       LuaKFixLine(fs, line);  /* pretend that `OP_FOR' starts the loop */
       LuaKPatchList(fs, ((isnum!=0) ? endfor : LuaKJump(fs)), prep + 1);
 }
Example #23
0
 public static void check_condition(LexState ls, bool c, CharPtr msg)
 {
     if (!(c)) LuaXSyntaxError(ls, msg);
 }
Example #24
0
 private static void forlist(LexState ls, TString indexname)
 {
     /* forlist . NAME {,NAME} IN explist1 forbody */
       FuncState fs = ls.fs;
       expdesc e = new expdesc();
       int nvars = 0;
       int line;
       int base_ = fs.freereg;
       /* create control variables */
       new_localvarliteral(ls, "(for generator)", nvars++);
       new_localvarliteral(ls, "(for state)", nvars++);
       new_localvarliteral(ls, "(for control)", nvars++);
       /* create declared variables */
       new_localvar(ls, indexname, nvars++);
       while (testnext(ls, ',') != 0)
     new_localvar(ls, str_checkname(ls), nvars++);
       checknext(ls, (int)RESERVED.TK_IN);
       line = ls.linenumber;
       adjust_assign(ls, 3, explist1(ls, e), e);
       LuaKCheckStack(fs, 3);  /* extra space to call generator */
       forbody(ls, base_, line, nvars - 3, 0);
 }
Example #25
0
 private static void open_func(LexState ls, FuncState fs)
 {
     LuaState L = ls.L;
       Proto f = LuaFNewProto(L);
       fs.f = f;
       fs.prev = ls.fs;  /* linked list of funcstates */
       fs.ls = ls;
       fs.L = L;
       ls.fs = fs;
       fs.pc = 0;
       fs.lasttarget = -1;
       fs.jpc = NO_JUMP;
       fs.freereg = 0;
       fs.nk = 0;
       fs.np = 0;
       fs.nlocvars = 0;
       fs.nactvar = 0;
       fs.bl = null;
       f.source = ls.source;
       f.maxstacksize = 2;  /* registers 0/1 are always valid */
       fs.h = luaH_new(L, 0, 0);
       /* anchor table of constants and prototype (to avoid being collected) */
       SetHValue2S(L, L.top, fs.h);
       IncrTop(L);
       SetPTValue2S(L, L.top, f);
       IncrTop(L);
 }
Example #26
0
 private static void fornum(LexState ls, TString varname, int line)
 {
     /* fornum . NAME = exp1,exp1[,exp1] forbody */
       FuncState fs = ls.fs;
       int base_ = fs.freereg;
       new_localvarliteral(ls, "(for index)", 0);
       new_localvarliteral(ls, "(for limit)", 1);
       new_localvarliteral(ls, "(for step)", 2);
       new_localvar(ls, varname, 3);
       checknext(ls, '=');
       exp1(ls);  /* initial value */
       checknext(ls, ',');
       exp1(ls);  /* limit */
       if (testnext(ls, ',') != 0)
     exp1(ls);  /* optional step */
       else {  /* default step = 1 */
     LuaKCodeABx(fs, OpCode.OP_LOADK, fs.freereg, LuaKNumberK(fs, 1));
     LuaKReserveRegs(fs, 1);
       }
       forbody(ls, base_, line, 1, 1);
 }
Example #27
0
 /*
 ** {======================================================================
 ** Expression parsing
 ** =======================================================================
 */
 private static void prefixexp(LexState ls, expdesc v)
 {
     /* prefixexp . NAME | '(' expr ')' */
       switch (ls.t.token) {
     case '(': {
       int line = ls.linenumber;
       LuaXNext(ls);
       expr(ls, v);
       check_match(ls, ')', '(', line);
       LuaKDischargeVars(ls.fs, v);
       return;
     }
     case (int)RESERVED.TK_NAME: {
       singlevar(ls, v);
       return;
     }
     default: {
       LuaXSyntaxError(ls, "unexpected symbol");
       return;
     }
       }
 }
Example #28
0
 private static void forstat(LexState ls, int line)
 {
     /* forstat . FOR (fornum | forlist) END */
       FuncState fs = ls.fs;
       TString varname;
       BlockCnt bl = new BlockCnt();
       enterblock(fs, bl, 1);  /* scope for loop and control variables */
       LuaXNext(ls);  /* skip `for' */
       varname = str_checkname(ls);  /* first variable name */
       switch (ls.t.token) {
     case '=': fornum(ls, varname, line); break;
     case ',':
     case (int)RESERVED.TK_IN:
         forlist(ls, varname);
         break;
     default: LuaXSyntaxError(ls, LUA_QL("=") + " or " + LUA_QL("in") + " expected"); break;
       }
       check_match(ls, (int)RESERVED.TK_END, (int)RESERVED.TK_FOR, line);
       leaveblock(fs);  /* loop scope (`break' jumps to this point) */
 }
Example #29
0
 private static void pushclosure(LexState ls, FuncState func, expdesc v)
 {
     FuncState fs = ls.fs;
       Proto f = fs.f;
       int oldsize = f.sizep;
       int i;
       LuaMGrowVector(ls.L, ref f.p, fs.np, ref f.sizep,
                   MAXARG_Bx, "constant table overflow");
       while (oldsize < f.sizep) f.p[oldsize++] = null;
       f.p[fs.np++] = func.f;
       LuaCObjBarrier(ls.L, f, func.f);
       init_exp(v, expkind.VRELOCABLE, LuaKCodeABx(fs, OpCode.OP_CLOSURE, 0, fs.np - 1));
       for (i=0; i<func.f.nups; i++) {
     OpCode o = ((int)func.upvalues[i].k == (int)expkind.VLOCAL) ? OpCode.OP_MOVE : OpCode.OP_GETUPVAL;
     LuaKCodeABC(fs, o, 0, func.upvalues[i].info, 0);
       }
 }
Example #30
0
 private static void funcargs(LexState ls, expdesc f)
 {
     FuncState fs = ls.fs;
       expdesc args = new expdesc();
       int base_, nparams;
       int line = ls.linenumber;
       switch (ls.t.token) {
     case '(': {  /* funcargs . `(' [ explist1 ] `)' */
       if (line != ls.lastline)
         LuaXSyntaxError(ls,"ambiguous syntax (function call x new statement)");
       LuaXNext(ls);
       if (ls.t.token == ')')  /* arg list is empty? */
         args.k = expkind.VVOID;
       else {
         explist1(ls, args);
         LuaKSetMultRet(fs, args);
       }
       check_match(ls, ')', '(', line);
       break;
     }
     case '{': {  /* funcargs . constructor */
       constructor(ls, args);
       break;
     }
     case (int)RESERVED.TK_STRING: {  /* funcargs . STRING */
       codestring(ls, args, ls.t.seminfo.ts);
       LuaXNext(ls);  /* must use `seminfo' before `next' */
       break;
     }
     default: {
       LuaXSyntaxError(ls, "function arguments expected");
       return;
     }
       }
       LuaAssert(f.k == expkind.VNONRELOC);
       base_ = f.u.s.info;  /* base_ register for call */
       if (hasmultret(args.k) != 0)
     nparams = LUA_MULTRET;  /* open call */
       else {
     if (args.k != expkind.VVOID)
       LuaKExp2NextReg(fs, args);  /* close last argument */
     nparams = fs.freereg - (base_+1);
       }
       init_exp(f, expkind.VCALL, LuaKCodeABC(fs, OpCode.OP_CALL, base_, nparams + 1, 2));
       LuaKFixLine(fs, line);
       fs.freereg = base_+1;  /* call remove function and arguments and leaves
                             (unless changed) one result */
 }