robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
3b00ddb601
master
1 2#if !defined(lpvm_h ) 3#define lpvm_h 4 5#include "lpcap.h" 6 7 8/* 9** About Character sets in instructions: a set is a bit map with an 10** initial offset, in bits, and a size, in number of instructions. 11** aux1 has the default value for the bits outsize that range. 12*/ 13 14 15/* Virtual Machine's instructions */ 16typedef enum Opcode { 17IAny ,/* if no char, fail */ 18IChar ,/* if char != aux1, fail */ 19ISet ,/* if char not in set, fail */ 20ITestAny ,/* in no char, jump to 'offset' */ 21ITestChar ,/* if char != aux1, jump to 'offset' */ 22ITestSet ,/* if char not in set, jump to 'offset' */ 23ISpan ,/* read a span of chars in set */ 24IUTFR ,/* if codepoint not in range [offset, utf_to], fail */ 25IBehind ,/* walk back 'aux1' characters (fail if not possible) */ 26IRet ,/* return from a rule */ 27IEnd ,/* end of pattern */ 28IChoice ,/* stack a choice; next fail will jump to 'offset' */ 29IJmp ,/* jump to 'offset' */ 30ICall ,/* call rule at 'offset' */ 31IOpenCall ,/* call rule number 'key' (must be closed to a ICall) */ 32ICommit ,/* pop choice and jump to 'offset' */ 33IPartialCommit ,/* update top choice to current position and jump */ 34IBackCommit ,/* backtrack like "fail" but jump to its own 'offset' */ 35IFailTwice ,/* pop one choice and then fail */ 36IFail ,/* go back to saved state on choice and jump to saved offset */ 37IGiveup ,/* internal use */ 38IFullCapture ,/* complete capture of last 'off' chars */ 39IOpenCapture ,/* start a capture */ 40ICloseCapture , 41ICloseRunTime , 42IEmpty /* to fill empty slots left by optimizations */ 43}Opcode ; 44 45 46/* 47** All array of instructions has a 'codesize' as its first element 48** and is referred by a pointer to its second element, which is the 49** first actual opcode. 50*/ 51typedef union Instruction { 52struct Inst { 53byte code ; 54byte aux1 ; 55union { 56short key ; 57struct { 58byte offset ; 59byte size ; 60 }set ; 61 }aux2 ; 62 }i ; 63int offset ; 64uint codesize ; 65byte buff [1 ]; 66}Instruction ; 67 68 69/* extract 24-bit value from an instruction */ 70#define utf_to (inst ) (((inst)->i.aux2.key << 8) | (inst)->i.aux1) 71 72 73int charinset (const Instruction * i ,const byte * buff ,uint c ); 74const char * match (lua_State * L ,const char * o ,const char * s ,const char * e , 75Instruction * op ,Capture * capture ,int ptop ); 76 77 78#endif 79