xref: /freebsd/sys/contrib/openzfs/module/lua/lparser.h (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1*61145dc2SMartin Matuska // SPDX-License-Identifier: MIT
2eda14cbcSMatt Macy /*
3eda14cbcSMatt Macy ** $Id: lparser.h,v 1.70.1.1 2013/04/12 18:48:47 roberto Exp $
4eda14cbcSMatt Macy ** Lua Parser
5eda14cbcSMatt Macy ** See Copyright Notice in lua.h
6eda14cbcSMatt Macy */
7eda14cbcSMatt Macy 
8eda14cbcSMatt Macy #ifndef lparser_h
9eda14cbcSMatt Macy #define lparser_h
10eda14cbcSMatt Macy 
11eda14cbcSMatt Macy #include "llimits.h"
12eda14cbcSMatt Macy #include "lobject.h"
13eda14cbcSMatt Macy #include "lzio.h"
14eda14cbcSMatt Macy 
15eda14cbcSMatt Macy 
16eda14cbcSMatt Macy /*
17eda14cbcSMatt Macy ** Expression descriptor
18eda14cbcSMatt Macy */
19eda14cbcSMatt Macy 
20eda14cbcSMatt Macy typedef enum {
21eda14cbcSMatt Macy   VVOID,	/* no value */
22eda14cbcSMatt Macy   VNIL,
23eda14cbcSMatt Macy   VTRUE,
24eda14cbcSMatt Macy   VFALSE,
25eda14cbcSMatt Macy   VK,		/* info = index of constant in `k' */
26eda14cbcSMatt Macy   VKNUM,	/* nval = numerical value */
27eda14cbcSMatt Macy   VNONRELOC,	/* info = result register */
28eda14cbcSMatt Macy   VLOCAL,	/* info = local register */
29eda14cbcSMatt Macy   VUPVAL,       /* info = index of upvalue in 'upvalues' */
30eda14cbcSMatt Macy   VINDEXED,	/* t = table register/upvalue; idx = index R/K */
31eda14cbcSMatt Macy   VJMP,		/* info = instruction pc */
32eda14cbcSMatt Macy   VRELOCABLE,	/* info = instruction pc */
33eda14cbcSMatt Macy   VCALL,	/* info = instruction pc */
34eda14cbcSMatt Macy   VVARARG	/* info = instruction pc */
35eda14cbcSMatt Macy } expkind;
36eda14cbcSMatt Macy 
37eda14cbcSMatt Macy 
38eda14cbcSMatt Macy #define vkisvar(k)	(VLOCAL <= (k) && (k) <= VINDEXED)
39eda14cbcSMatt Macy #define vkisinreg(k)	((k) == VNONRELOC || (k) == VLOCAL)
40eda14cbcSMatt Macy 
41eda14cbcSMatt Macy typedef struct expdesc {
42eda14cbcSMatt Macy   expkind k;
43eda14cbcSMatt Macy   union {
44eda14cbcSMatt Macy     struct {  /* for indexed variables (VINDEXED) */
45eda14cbcSMatt Macy       short idx;  /* index (R/K) */
46eda14cbcSMatt Macy       lu_byte t;  /* table (register or upvalue) */
47eda14cbcSMatt Macy       lu_byte vt;  /* whether 't' is register (VLOCAL) or upvalue (VUPVAL) */
48eda14cbcSMatt Macy     } ind;
49eda14cbcSMatt Macy     int info;  /* for generic use */
50eda14cbcSMatt Macy     lua_Number nval;  /* for VKNUM */
51eda14cbcSMatt Macy   } u;
52eda14cbcSMatt Macy   int t;  /* patch list of `exit when true' */
53eda14cbcSMatt Macy   int f;  /* patch list of `exit when false' */
54eda14cbcSMatt Macy } expdesc;
55eda14cbcSMatt Macy 
56eda14cbcSMatt Macy 
57eda14cbcSMatt Macy /* description of active local variable */
58eda14cbcSMatt Macy typedef struct Vardesc {
59eda14cbcSMatt Macy   short idx;  /* variable index in stack */
60eda14cbcSMatt Macy } Vardesc;
61eda14cbcSMatt Macy 
62eda14cbcSMatt Macy 
63eda14cbcSMatt Macy /* description of pending goto statements and label statements */
64eda14cbcSMatt Macy typedef struct Labeldesc {
65eda14cbcSMatt Macy   TString *name;  /* label identifier */
66eda14cbcSMatt Macy   int pc;  /* position in code */
67eda14cbcSMatt Macy   int line;  /* line where it appeared */
68eda14cbcSMatt Macy   lu_byte nactvar;  /* local level where it appears in current block */
69eda14cbcSMatt Macy } Labeldesc;
70eda14cbcSMatt Macy 
71eda14cbcSMatt Macy 
72eda14cbcSMatt Macy /* list of labels or gotos */
73eda14cbcSMatt Macy typedef struct Labellist {
74eda14cbcSMatt Macy   Labeldesc *arr;  /* array */
75eda14cbcSMatt Macy   int n;  /* number of entries in use */
76eda14cbcSMatt Macy   int size;  /* array size */
77eda14cbcSMatt Macy } Labellist;
78eda14cbcSMatt Macy 
79eda14cbcSMatt Macy 
80eda14cbcSMatt Macy /* dynamic structures used by the parser */
81eda14cbcSMatt Macy typedef struct Dyndata {
82eda14cbcSMatt Macy   struct {  /* list of active local variables */
83eda14cbcSMatt Macy     Vardesc *arr;
84eda14cbcSMatt Macy     int n;
85eda14cbcSMatt Macy     int size;
86eda14cbcSMatt Macy   } actvar;
87eda14cbcSMatt Macy   Labellist gt;  /* list of pending gotos */
88eda14cbcSMatt Macy   Labellist label;   /* list of active labels */
89eda14cbcSMatt Macy } Dyndata;
90eda14cbcSMatt Macy 
91eda14cbcSMatt Macy 
92eda14cbcSMatt Macy /* control of blocks */
93eda14cbcSMatt Macy struct BlockCnt;  /* defined in lparser.c */
94eda14cbcSMatt Macy 
95eda14cbcSMatt Macy 
96eda14cbcSMatt Macy /* state needed to generate code for a given function */
97eda14cbcSMatt Macy typedef struct FuncState {
98eda14cbcSMatt Macy   Proto *f;  /* current function header */
99eda14cbcSMatt Macy   Table *h;  /* table to find (and reuse) elements in `k' */
100eda14cbcSMatt Macy   struct FuncState *prev;  /* enclosing function */
101eda14cbcSMatt Macy   struct LexState *ls;  /* lexical state */
102eda14cbcSMatt Macy   struct BlockCnt *bl;  /* chain of current blocks */
103eda14cbcSMatt Macy   int pc;  /* next position to code (equivalent to `ncode') */
104eda14cbcSMatt Macy   int lasttarget;   /* 'label' of last 'jump label' */
105eda14cbcSMatt Macy   int jpc;  /* list of pending jumps to `pc' */
106eda14cbcSMatt Macy   int nk;  /* number of elements in `k' */
107eda14cbcSMatt Macy   int np;  /* number of elements in `p' */
108eda14cbcSMatt Macy   int firstlocal;  /* index of first local var (in Dyndata array) */
109eda14cbcSMatt Macy   short nlocvars;  /* number of elements in 'f->locvars' */
110eda14cbcSMatt Macy   lu_byte nactvar;  /* number of active local variables */
111eda14cbcSMatt Macy   lu_byte nups;  /* number of upvalues */
112eda14cbcSMatt Macy   lu_byte freereg;  /* first free register */
113eda14cbcSMatt Macy } FuncState;
114eda14cbcSMatt Macy 
115eda14cbcSMatt Macy 
116eda14cbcSMatt Macy LUAI_FUNC Closure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
117eda14cbcSMatt Macy                                 Dyndata *dyd, const char *name, int firstchar);
118eda14cbcSMatt Macy 
119eda14cbcSMatt Macy 
120eda14cbcSMatt Macy #endif
121