#include <string.h>
#include <Zend/zend_types.h>
+#include <Zend/zend_API.h> /* fcall */
#include "parser_proc.h"
static inline PSI_Token *PSI_TokenCopy(PSI_Token *src);
+typedef struct zend_fcall {
+ zend_fcall_info fci;
+ zend_fcall_info_cache fcc;
+} zend_fcall;
+
typedef union impl_val {
char cval;
int8_t i8;
zend_bool bval;
zend_long lval;
zend_string *str;
+ zend_fcall *cb;
} zend;
void *ptr;
- uint8_t _dbg[sizeof(void *)];
} impl_val;
typedef struct decl_type {
PSI_Token *token;
char *name;
token_t type;
- struct decl_type *real;
- struct decl_struct *strct;
- struct decl_union *unn;
- struct decl_enum *enm;
- struct decl *func;
+ union {
+ struct decl_arg *def;
+ struct decl_struct *strct;
+ struct decl_union *unn;
+ struct decl_enum *enm;
+ struct decl *func;
+ } real;
} decl_type;
static inline decl_type *init_decl_type(token_t type, const char *name) {
return t;
}
-static inline decl_type *real_decl_type(decl_type *type) {
- while (type->real) {
- type = type->real;
+static inline int weak_decl_type(decl_type *type) {
+ switch (type->type) {
+ case PSI_T_CHAR:
+ case PSI_T_SHORT:
+ case PSI_T_INT:
+ case PSI_T_LONG:
+ case PSI_T_NAME:
+ return type->type;
+ default:
+ return 0;
}
- return type;
}
static inline void free_decl(struct decl *decl);
free(type->token);
}
if (type->type == PSI_T_FUNCTION) {
- free_decl(type->func);
+ free_decl(type->real.func);
}
free(type->name);
free(type);
decl_type *type;
decl_var *var;
decl_struct_layout *layout;
- struct let_stmt *let;
impl_val val;
void *ptr;
+ void *let;
void *mem;
} decl_arg;
arg->var = var;
var->arg = arg;
arg->ptr = &arg->val;
+ arg->let = arg->ptr;
return arg;
}
free(arg);
}
+static inline decl_type *real_decl_type(decl_type *type) {
+ while (weak_decl_type(type)) {
+ type = type->real.def->type;
+ }
+ return type;
+}
+
typedef struct decl_typedefs {
size_t count;
decl_arg **list;
typedef struct impl_var {
PSI_Token *token;
char *name;
+ struct impl_arg *arg;
unsigned reference:1;
} impl_var;
return var;
}
+static inline impl_var *copy_impl_var(impl_var *var) {
+ impl_var *cpy = malloc(sizeof(*cpy));
+
+ memcpy(cpy, var, sizeof(*cpy));
+ cpy->name = strdup(cpy->name);
+ if (cpy->token) {
+ cpy->token = PSI_TokenCopy(cpy->token);
+ }
+ return cpy;
+}
+
static inline void free_impl_var(impl_var *var) {
if (var->token) {
free(var->token);
impl_arg *arg = calloc(1, sizeof(*arg));
arg->type = type;
arg->var = var;
+ arg->var->arg = arg;
arg->def = def;
return arg;
}
free(alloc);
}
+typedef struct let_callback {
+ struct let_func *func;
+ struct set_values *args;
+ decl *decl;
+} let_callback;
+
+static inline void free_let_func(struct let_func *func);
+static inline void free_set_values(struct set_values *vals);
+static inline let_callback *init_let_callback(struct let_func *func, struct set_values *args) {
+ let_callback *cb = calloc(1, sizeof(*cb));
+
+ cb->func = func;
+ cb->args = args;
+ return cb;
+}
+
+static inline void free_let_callback(let_callback *cb) {
+ free_let_func(cb->func);
+ free_set_values(cb->args);
+ free(cb);
+}
+
+typedef impl_val *(*let_func_handler)(impl_val *tmp, decl_type *type, impl_arg *iarg, void **to_free);
+
typedef struct let_func {
token_t type;
char *name;
impl_var *var;
- impl_arg *arg;
+ let_func_handler handler;
} let_func;
static inline let_func *init_let_func(token_t type, const char *name, impl_var *var) {
PSI_LET_NULL,
PSI_LET_NUMEXP,
PSI_LET_CALLOC,
+ PSI_LET_CALLBACK,
PSI_LET_FUNC,
PSI_LET_TMP,
} kind;
union {
num_exp *num;
let_calloc *alloc;
+ let_callback *callback;
let_func *func;
decl_var *var;
} data;
case PSI_LET_CALLOC:
let->data.alloc = data;
break;
+ case PSI_LET_CALLBACK:
+ let->data.callback = data;
+ break;
case PSI_LET_FUNC:
let->data.func = data;
break;
case PSI_LET_CALLOC:
free_let_calloc(let->data.alloc);
break;
+ case PSI_LET_CALLBACK:
+ free_let_callback(let->data.callback);
+ break;
case PSI_LET_FUNC:
free_let_func(let->data.func);
break;
typedef struct let_stmt {
decl_var *var;
let_val *val;
-
- void *ptr;
} let_stmt;
static inline let_stmt *init_let_stmt(decl_var *var, let_val *val) {
struct set_value *set;
impl_val *val;
} outer;
- struct set_value **inner;
- size_t count;
+ struct set_values *inner;
} set_value;
+typedef struct set_values {
+ set_value **vals;
+ size_t count;
+} set_values;
+
+
static inline set_value *init_set_value(set_func *func, decl_vars *vars) {
set_value *val = calloc(1, sizeof(*val));
val->func = func;
val->vars = vars;
return val;
}
+
+static inline set_values *add_set_value(set_values *vals, set_value *val);
static inline set_value *add_inner_set_value(set_value *val, set_value *inner) {
- val->inner = realloc(val->inner, ++val->count * sizeof(*val->inner));
- val->inner[val->count-1] = inner;
+ val->inner = add_set_value(val->inner, inner);
inner->outer.set = val;
return val;
}
free_decl_vars(val->vars);
}
if (val->inner && (!val->outer.set || val->outer.set->inner != val->inner)) {
- size_t i;
- for (i = 0; i < val->count; ++i) {
- free_set_value(val->inner[i]);
- }
- free(val->inner);
+ free_set_values(val->inner);
}
if (val->num) {
free_num_exp(val->num);
free(val);
}
+static inline set_values *init_set_values(set_value *val) {
+ set_values *vals = calloc(1, sizeof(*vals));
+ if (val) {
+ vals->count = 1;
+ vals->vals = calloc(1, sizeof(val));
+ vals->vals[0] = val;
+ }
+ return vals;
+}
+
+static inline set_values *add_set_value(set_values *vals, set_value *val) {
+ if (!vals) {
+ vals = calloc(1, sizeof(*vals));
+ }
+ vals->vals = realloc(vals->vals, ++vals->count * sizeof(val));
+ vals->vals[vals->count-1] = val;
+ return vals;
+}
+
+static inline void free_set_values(set_values *vals) {
+ if (vals->vals) {
+ size_t i;
+
+ for (i = 0; i < vals->count; ++i) {
+ free_set_value(vals->vals[i]);
+ }
+ free(vals->vals);
+ }
+ free(vals);
+}
+
typedef struct set_stmt {
impl_var *var;
set_value *val;
#define PSI_ERROR 16
#define PSI_WARNING 32
-typedef void (*psi_error_cb)(PSI_Token *token, int type, const char *msg, ...);
+typedef void (*psi_error_cb)(void *context, PSI_Token *token, int type, const char *msg, ...);
#define PSI_DATA(D) ((PSI_Data *) (D))
#define PSI_DATA_MEMBERS \
decl_file file; \
decl_libs libs; \
} psi; \
- psi_error_cb error
+ psi_error_cb error; \
+ unsigned errors; \
+ unsigned flags
typedef struct PSI_Data {
PSI_DATA_MEMBERS;
} PSI_Data;
FILE *fp;
token_t num;
void *proc;
- unsigned flags, errors, line, col;
+ unsigned line, col;
char *cur, *tok, *lim, *eof, *ctx, *mrk, buf[BSIZE];
} PSI_Parser;
}
#define PSI_PARSER_DEBUG 0x1
+#define PSI_PARSER_SILENT 0x2
PSI_Parser *PSI_ParserInit(PSI_Parser *P, const char *filename, psi_error_cb error, unsigned flags);
void PSI_ParserSyntaxError(PSI_Parser *P, const char *fn, size_t ln, const char *msg, ...);