diameter 发表于 2011-5-28 19:22:52

Malical 2.0的头文件

Malical.h当前进度

#define TOKEN_BUFFER_SIZE 256
#define SYMBOL_BUFFER_SZIE 32
#define STACK_SIZE 64
int MalicalError (char *);
int MalicalTokenError ();
typedef enum
{
    B_NO,
    B_IF,
    B_OT
} BLOCK;
typedef enum
{
    TT_NUM=1,
    TT_NIL,
    TT_BOOL,
    TT_STR,
    TT_SYM,
    TT_SIGN
   
} TOKEN_TYPE;
extern char token[];
extern char line[];
typedef enum
{
    SIGN_ADD = 256,
    SIGN_SUB,
    SIGN_MUL,
    SIGN_DIV,
    SIGN_POW,
    SIGN_BLK,
    SIGN_BRK,
    SIGN_AND,
    SIGN_OR ,
    SIGN_NOT,
    SIGN_EQ ,
    SIGN_NEQ,
    SIGN_BT ,
    SIGN_BE ,
    SIGN_LT ,
    SIGN_LE
} SIGN;
int GetToken (char *context,int pos,TOKEN_TYPE *type);
int SignToValue (char *sign);
typedef union
{
    int      b;
    char    *str;
    double    n;
}Value;
typedef enum
{
    VT_NIL,
    VT_BOOL,
    VT_STR,
    VT_NUM
}Type;
typedef struct
{
    Type    tag;
    Value    value;
}Object;
void         obj_ini (Object *d);
void         obj_clear (Object *d);
Object*      obj_copy (Object *d,const Object *s);
Object*      obj_create (Object *d,Type tag,void *value);
char*          obj_to_str(Object *s, char *str);
int EvalExpr (char *expr,Object *objr);


object的实现借鉴了lua源码的思路
这次为了实现bool、string、double变量,采用了大量函数拷贝……
C语言没有class,又不让用C++,每次initi.alize、delete都要手动调用……
在9860上运行可能会变慢

diameter 发表于 2011-5-28 19:34:50

我不得不这么写 Object l,r,result;
SIGN p_s;
obj_ini(&l);
obj_ini(&r);
obj_ini(&result);
p_s = sstk[--stop];
objs_pop(&obj_stk,&r);
objs_pop(&obj_stk,&l);
EvalLR(&l,&r,p_s,&result);
//puts(obj_to_str(&result,buf));
objs_push(&obj_stk,&result);
obj_clear(&l);
obj_clear(&r);
obj_clear(&result);

要不然到时候l、r是字符串的话没有free掉heap memory一切就杯具了

hcz 发表于 2011-5-29 16:40:29

支持一个。。。看上去定义很全

diameter 发表于 2011-6-6 15:54:03

没法看了……
#define TOKEN_BUFFER_SIZE 256
#define SYMBOL_BUFFER_SZIE 32
#define STRING_BUFFER_SIZE 128
#define STACK_SIZE 64
#define NAME_LENGTH 12

extern const char *TYPE_TEXT[];

int MalicalError (const char *);
int MalicalTokenError ();

typedef enum
{
B_NO,
B_IF,
B_OT
} BLOCK;

typedef enum
{
TT_NUM=1,
TT_NIL,
TT_BOOL,
TT_STR,
TT_SYM,
TT_SIGN,
TT_BK,
TT_REF
} TOKEN_TYPE;

extern char token[];
extern char line[];

typedef enum
{
SIGN_ADD = 256,
SIGN_SUB,
SIGN_MUL,
SIGN_DIV,
SIGN_POW,
SIGN_BLK,
SIGN_BRK,
SIGN_AND,
SIGN_OR ,
SIGN_NOT,
SIGN_EQ ,
SIGN_NEQ,
SIGN_BT ,
SIGN_BE ,
SIGN_LT ,
SIGN_LE ,
SIGN_STO
} SIGN;

typedef struct
{
char *name;
SIGN value;
}SIGNVALUE;

int GetToken (char *context,int pos,TOKEN_TYPE *type);
int Match (char *text,char *find,TOKEN_TYPE type,int pl);
int GetTokenE (char *text,char l,char r,int pl);
int GetLine(char *text,int p);
int LineEmpty (int p);
int SignToValue (char *sign);

typedef union
{
int b;
char *str;
double n;
void *ref;
}Value;

typedef enum
{
VT_NIL,
VT_BOOL,
VT_STR,
VT_NUM,
VT_REF
}Type;

typedef struct
{
Type tag;
Value value;
}Object;

typedef struct
{
Object *stk;
int size;
}ObjectStack;

void obj_ini (Object *d);
void obj_clear (Object *d);
Object* obj_copy (Object *d,const Object *s);
Object* obj_create (Object *d,Type tag,void *value);
char* obj_to_str(Object *s, char *str);

void objs_push (ObjectStack *s,Object *n_o);
void objs_pop (ObjectStack *s,Object *n_o);
void objs_clear (ObjectStack *s);

#define o_is_num(o) ((o)->tag==VT_NUM)
#define o_is_bool(o) ((o)->tag==VT_BOOL)
#define o_is_str(o) ((o)->tag==VT_STR)


int EvalExpr (char *expr,Object *objr);

typedef struct
{
char name;
Object obj;
}Variable;

typedef struct
{
Variable *vars;
int size;
}VarStack;

void var_copy (Variable *d,Variable *s);
void var_create (Variable *v,const char *name,Object *obj);
void var_stk_push (VarStack * vs,Variable *v);
void var_stk_clear (VarStack * vs);


extern VarStack GlobalVar;

extern VarStack *LocalVar[];
extern int LocalTop;

void new_local_var_stack ();
void delete_local_var_stack ();

Variable* find_var (char *name);

typedef struct
{
char name;
int pos;
}FUNCTION;

void func_push (const char *name,int pos);
int find_func (const char *name);

// parser
void CallFun (const char *name,ObjectStack*);
int RunIfBlock (int p,int todo,int _todo);
int RunWhileBlock (int b,char *expr,int _todo);
int RunFunction (int p,int todo,int is_block);
int MalicalPrescan ();
int RunFile (char* file_name);

extern Object ReturnObject;
页: [1]
查看完整版本: Malical 2.0的头文件