*** empty log message ***

This commit is contained in:
hyung-hwan 2006-03-02 15:10:59 +00:00
parent 3d06a114c2
commit f02f75b381
11 changed files with 305 additions and 301 deletions

View File

@ -1,4 +1,4 @@
SRCS = awk.c tree.c tab.c hash.c parse.c run.c sa.c
SRCS = awk.c tree.c tab.c map.c parse.c run.c sa.c
OBJS = $(SRCS:.c=.obj)
OUT = xpawk.lib

View File

@ -1,5 +1,5 @@
SRCS = awk.c tree.c tab.c hash.c parse.c run.c sa.c
OBJS = awk.obj tree.obj tab.obj hash.obj parse.obj run.obj sa.obj
SRCS = awk.c tree.c tab.c map.c parse.c run.c sa.c
OBJS = awk.obj tree.obj tab.obj map.obj parse.obj run.obj sa.obj
OUT = xpawk.lib
CC = lcc

View File

@ -1,4 +1,4 @@
/* * $Id: awk.c,v 1.23 2006-02-23 15:37:34 bacon Exp $ */
/* * $Id: awk.c,v 1.24 2006-03-02 15:10:59 bacon Exp $ */
#include <xp/awk/awk.h>
@ -23,7 +23,7 @@ xp_awk_t* xp_awk_open (xp_awk_t* awk)
return XP_NULL;
}
if (xp_awk_hash_open(&awk->tree.funcs, 256, __free_func) == XP_NULL) {
if (xp_awk_map_open(&awk->tree.funcs, 256, __free_func) == XP_NULL) {
xp_str_close (&awk->token.name);
if (awk->__dynamic) xp_free (awk);
return XP_NULL;
@ -31,14 +31,14 @@ xp_awk_t* xp_awk_open (xp_awk_t* awk)
if (xp_awk_tab_open(&awk->parse.globals) == XP_NULL) {
xp_str_close (&awk->token.name);
xp_awk_hash_close (&awk->tree.funcs);
xp_awk_map_close (&awk->tree.funcs);
if (awk->__dynamic) xp_free (awk);
return XP_NULL;
}
if (xp_awk_tab_open(&awk->parse.locals) == XP_NULL) {
xp_str_close (&awk->token.name);
xp_awk_hash_close (&awk->tree.funcs);
xp_awk_map_close (&awk->tree.funcs);
xp_awk_tab_close (&awk->parse.globals);
if (awk->__dynamic) xp_free (awk);
return XP_NULL;
@ -46,7 +46,7 @@ xp_awk_t* xp_awk_open (xp_awk_t* awk)
if (xp_awk_tab_open(&awk->parse.params) == XP_NULL) {
xp_str_close (&awk->token.name);
xp_awk_hash_close (&awk->tree.funcs);
xp_awk_map_close (&awk->tree.funcs);
xp_awk_tab_close (&awk->parse.globals);
xp_awk_tab_close (&awk->parse.locals);
if (awk->__dynamic) xp_free (awk);
@ -54,9 +54,9 @@ xp_awk_t* xp_awk_open (xp_awk_t* awk)
}
/* TODO: maybe a free function .... */
if (xp_awk_hash_open(&awk->run.named, 256, XP_NULL) == XP_NULL) {
if (xp_awk_map_open(&awk->run.named, 256, XP_NULL) == XP_NULL) {
xp_str_close (&awk->token.name);
xp_awk_hash_close (&awk->tree.funcs);
xp_awk_map_close (&awk->tree.funcs);
xp_awk_tab_close (&awk->parse.globals);
xp_awk_tab_close (&awk->parse.locals);
xp_awk_tab_close (&awk->parse.params);
@ -89,11 +89,11 @@ int xp_awk_close (xp_awk_t* awk)
{
xp_awk_clear (awk);
if (xp_awk_detsrc(awk) == -1) return -1;
xp_awk_hash_close (&awk->tree.funcs);
xp_awk_map_close (&awk->tree.funcs);
xp_awk_tab_close (&awk->parse.globals);
xp_awk_tab_close (&awk->parse.locals);
xp_awk_tab_close (&awk->parse.params);
xp_awk_hash_close (&awk->run.named);
xp_awk_map_close (&awk->run.named);
xp_str_close (&awk->token.name);
if (awk->__dynamic) xp_free (awk);
return 0;
@ -155,7 +155,7 @@ void xp_awk_clear (xp_awk_t* awk)
/* clear parse trees */
awk->tree.nglobals = 0;
xp_awk_hash_clear (&awk->tree.funcs);
xp_awk_map_clear (&awk->tree.funcs);
if (awk->tree.begin != XP_NULL) {
xp_assert (awk->tree.begin->next == XP_NULL);
xp_awk_clrpt (awk->tree.begin);

View File

@ -1,5 +1,5 @@
/*
* $Id: awk.h,v 1.29 2006-02-23 15:37:34 bacon Exp $
* $Id: awk.h,v 1.30 2006-03-02 15:10:59 bacon Exp $
*/
#ifndef _XP_AWK_AWK_H_
@ -15,7 +15,7 @@
#include <xp/awk/tree.h>
#include <xp/awk/tab.h>
#include <xp/awk/hash.h>
#include <xp/awk/map.h>
enum
{
@ -102,7 +102,7 @@ struct xp_awk_t
struct
{
xp_size_t nglobals;
xp_awk_hash_t funcs;
xp_awk_map_t funcs;
xp_awk_node_t* begin;
xp_awk_node_t* end;
xp_awk_chain_t* chain;
@ -121,7 +121,7 @@ struct xp_awk_t
/* run-time data structure */
struct
{
xp_awk_hash_t named;
xp_awk_map_t named;
} run;
/* source buffer management */

View File

@ -1,218 +0,0 @@
/*
* $Id: hash.c,v 1.4 2006-01-30 14:45:12 bacon Exp $
*/
#include <xp/awk/hash.h>
#ifndef __STAND_ALONE
#include <xp/bas/memory.h>
#include <xp/bas/string.h>
#include <xp/bas/assert.h>
#endif
// TODO: improve the entire hash routines.
// support automatic bucket resizing and rehashing, etc.
static xp_size_t __hash (const xp_char_t* key);
#define FREE_PAIR(hash,pair) \
do { \
xp_free ((pair)->key); \
if ((hash)->free_value != XP_NULL) \
(hash)->free_value ((pair)->value); \
xp_free (pair); \
} while (0)
xp_awk_hash_t* xp_awk_hash_open (
xp_awk_hash_t* hash, xp_size_t capa, void (*free_value) (void*))
{
if (hash == XP_NULL) {
hash = (xp_awk_hash_t*) xp_malloc (xp_sizeof(xp_awk_hash_t));
if (hash == XP_NULL) return XP_NULL;
hash->__dynamic = xp_true;
}
else hash->__dynamic = xp_false;
hash->buck = (xp_awk_pair_t**) xp_malloc (xp_sizeof(xp_awk_pair_t*) * capa);
if (hash->buck == XP_NULL) {
if (hash->__dynamic) xp_free (hash);
return XP_NULL;
}
hash->capa = capa;
hash->size = 0;
hash->free_value = free_value;
while (capa > 0) hash->buck[--capa] = XP_NULL;
return hash;
}
void xp_awk_hash_close (xp_awk_hash_t* hash)
{
xp_awk_hash_clear (hash);
xp_free (hash->buck);
if (hash->__dynamic) xp_free (hash);
}
void xp_awk_hash_clear (xp_awk_hash_t* hash)
{
xp_size_t i;
xp_awk_pair_t* pair, * next;
for (i = 0; i < hash->capa; i++) {
pair = hash->buck[i];
while (pair != XP_NULL) {
next = pair->next;
FREE_PAIR (hash, pair);
hash->size--;
pair = next;
}
hash->buck[i] = XP_NULL;
}
xp_assert (hash->size == 0);
}
xp_awk_pair_t* xp_awk_hash_get (xp_awk_hash_t* hash, xp_char_t* key)
{
xp_awk_pair_t* pair;
xp_size_t hc;
hc = __hash(key) % hash->capa;
pair = hash->buck[hc];
while (pair != XP_NULL) {
if (xp_strcmp(pair->key,key) == 0) return pair;
pair = pair->next;
}
return XP_NULL;
}
xp_awk_pair_t* xp_awk_hash_put (xp_awk_hash_t* hash, xp_char_t* key, void* value)
{
xp_awk_pair_t* pair;
xp_size_t hc;
hc = __hash(key) % hash->capa;
pair = hash->buck[hc];
while (pair != XP_NULL) {
if (xp_strcmp(pair->key,key) == 0) {
if (pair->key != key) {
xp_free (pair->key);
pair->key = key;
}
if (hash->free_value != XP_NULL) {
hash->free_value (pair->value);
}
pair->value = value;
return pair;
}
pair = pair->next;
}
pair = (xp_awk_pair_t*) xp_malloc (xp_sizeof(xp_awk_pair_t));
if (pair == XP_NULL) return XP_NULL;
pair->key = key;
pair->value = value;
pair->next = hash->buck[hc];
hash->buck[hc] = pair;
hash->size++;
return pair;
}
xp_awk_pair_t* xp_awk_hash_set (xp_awk_hash_t* hash, xp_char_t* key, void* value)
{
xp_awk_pair_t* pair;
xp_size_t hc;
hc = __hash(key) % hash->capa;
pair = hash->buck[hc];
while (pair != XP_NULL) {
if (xp_strcmp(pair->key,key) == 0) {
if (pair->key != key) {
xp_free (pair->key);
pair->key = key;
}
if (hash->free_value != XP_NULL) {
hash->free_value (pair->value);
}
pair->value = value;
return pair;
}
pair = pair->next;
}
return XP_NULL;
}
int xp_awk_hash_remove (xp_awk_hash_t* hash, xp_char_t* key)
{
xp_awk_pair_t* pair, * prev;
xp_size_t hc;
hc = __hash(key) % hash->capa;
pair = hash->buck[hc];
prev = XP_NULL;
while (pair != XP_NULL) {
if (xp_strcmp(pair->key,key) == 0) {
if (prev == XP_NULL)
hash->buck[hc] = pair->next;
else prev->next = pair->next;
FREE_PAIR (hash, pair);
hash->size--;
return 0;
}
prev = pair;
pair = pair->next;
}
return -1;
}
int xp_awk_hash_walk (xp_awk_hash_t* hash, int (*walker) (xp_awk_pair_t*))
{
xp_size_t i;
xp_awk_pair_t* pair, * next;
for (i = 0; i < hash->capa; i++) {
pair = hash->buck[i];
while (pair != XP_NULL) {
next = pair->next;
if (walker(pair) == -1) return -1;
pair = next;
}
}
return 0;
}
static xp_size_t __hash (const xp_char_t* key)
{
xp_size_t n = 0, i;
while (*key != XP_CHAR('\0')) {
xp_byte_t* bp = (xp_byte_t*)key;
for (i = 0; i < xp_sizeof(*key); i++) n = n * 31 + *bp++;
key++;
}
return n;
}

View File

@ -1,55 +0,0 @@
/*
* $Id: hash.h,v 1.4 2006-01-30 14:34:47 bacon Exp $
*/
#ifndef _XP_AWK_HASH_H_
#define _XP_AWK_HASH_H_
#ifdef __STAND_ALONE
#include <xp/awk/sa.h>
#else
#include <xp/types.h>
#include <xp/macros.h>
#endif
typedef struct xp_awk_hash_t xp_awk_hash_t;
typedef struct xp_awk_pair_t xp_awk_pair_t;
struct xp_awk_pair_t
{
xp_char_t* key;
void* value;
xp_awk_pair_t* next;
};
struct xp_awk_hash_t
{
xp_size_t size;
xp_size_t capa;
xp_awk_pair_t** buck;
void (*free_value) (void*);
xp_bool_t __dynamic;
};
#ifdef __cplusplus
extern "C" {
#endif
xp_awk_hash_t* xp_awk_hash_open (
xp_awk_hash_t* hash, xp_size_t capa, void (*free_value) (void*));
void xp_awk_hash_close (xp_awk_hash_t* hash);
void xp_awk_hash_clear (xp_awk_hash_t* hash);
xp_awk_pair_t* xp_awk_hash_get (xp_awk_hash_t* hash, xp_char_t* key);
xp_awk_pair_t* xp_awk_hash_put (xp_awk_hash_t* hash, xp_char_t* key, void* value);
xp_awk_pair_t* xp_awk_hash_set (xp_awk_hash_t* hash, xp_char_t* key, void* value);
int xp_awk_hash_remove (xp_awk_hash_t* hash, xp_char_t* key);
int xp_awk_hash_walk (xp_awk_hash_t* hash, int (*walker) (xp_awk_pair_t*));
#ifdef __cplusplus
}
#endif
#endif

View File

@ -1,4 +1,4 @@
SRCS = awk.c tree.c tab.c hash.c parse.c run.c sa.c
SRCS = awk.c tree.c tab.c map.c parse.c run.c sa.c
OBJS = $(SRCS:.c=.o)
OUT = libxpawk.a

218
ase/awk/map.c Normal file
View File

@ -0,0 +1,218 @@
/*
* $Id: map.c,v 1.1 2006-03-02 15:10:59 bacon Exp $
*/
#include <xp/awk/map.h>
#ifndef __STAND_ALONE
#include <xp/bas/memory.h>
#include <xp/bas/string.h>
#include <xp/bas/assert.h>
#endif
// TODO: improve the entire map routines.
// support automatic bucket resizing and remaping, etc.
static xp_size_t __hash (const xp_char_t* key);
#define FREE_PAIR(map,pair) \
do { \
xp_free ((pair)->key); \
if ((map)->free_value != XP_NULL) \
(map)->free_value ((pair)->value); \
xp_free (pair); \
} while (0)
xp_awk_map_t* xp_awk_map_open (
xp_awk_map_t* map, xp_size_t capa, void (*free_value) (void*))
{
if (map == XP_NULL) {
map = (xp_awk_map_t*) xp_malloc (xp_sizeof(xp_awk_map_t));
if (map == XP_NULL) return XP_NULL;
map->__dynamic = xp_true;
}
else map->__dynamic = xp_false;
map->buck = (xp_awk_pair_t**) xp_malloc (xp_sizeof(xp_awk_pair_t*) * capa);
if (map->buck == XP_NULL) {
if (map->__dynamic) xp_free (map);
return XP_NULL;
}
map->capa = capa;
map->size = 0;
map->free_value = free_value;
while (capa > 0) map->buck[--capa] = XP_NULL;
return map;
}
void xp_awk_map_close (xp_awk_map_t* map)
{
xp_awk_map_clear (map);
xp_free (map->buck);
if (map->__dynamic) xp_free (map);
}
void xp_awk_map_clear (xp_awk_map_t* map)
{
xp_size_t i;
xp_awk_pair_t* pair, * next;
for (i = 0; i < map->capa; i++) {
pair = map->buck[i];
while (pair != XP_NULL) {
next = pair->next;
FREE_PAIR (map, pair);
map->size--;
pair = next;
}
map->buck[i] = XP_NULL;
}
xp_assert (map->size == 0);
}
xp_awk_pair_t* xp_awk_map_get (xp_awk_map_t* map, xp_char_t* key)
{
xp_awk_pair_t* pair;
xp_size_t hc;
hc = __hash(key) % map->capa;
pair = map->buck[hc];
while (pair != XP_NULL) {
if (xp_strcmp(pair->key,key) == 0) return pair;
pair = pair->next;
}
return XP_NULL;
}
xp_awk_pair_t* xp_awk_map_put (xp_awk_map_t* map, xp_char_t* key, void* value)
{
xp_awk_pair_t* pair;
xp_size_t hc;
hc = __hash(key) % map->capa;
pair = map->buck[hc];
while (pair != XP_NULL) {
if (xp_strcmp(pair->key,key) == 0) {
if (pair->key != key) {
xp_free (pair->key);
pair->key = key;
}
if (map->free_value != XP_NULL) {
map->free_value (pair->value);
}
pair->value = value;
return pair;
}
pair = pair->next;
}
pair = (xp_awk_pair_t*) xp_malloc (xp_sizeof(xp_awk_pair_t));
if (pair == XP_NULL) return XP_NULL;
pair->key = key;
pair->value = value;
pair->next = map->buck[hc];
map->buck[hc] = pair;
map->size++;
return pair;
}
xp_awk_pair_t* xp_awk_map_set (xp_awk_map_t* map, xp_char_t* key, void* value)
{
xp_awk_pair_t* pair;
xp_size_t hc;
hc = __hash(key) % map->capa;
pair = map->buck[hc];
while (pair != XP_NULL) {
if (xp_strcmp(pair->key,key) == 0) {
if (pair->key != key) {
xp_free (pair->key);
pair->key = key;
}
if (map->free_value != XP_NULL) {
map->free_value (pair->value);
}
pair->value = value;
return pair;
}
pair = pair->next;
}
return XP_NULL;
}
int xp_awk_map_remove (xp_awk_map_t* map, xp_char_t* key)
{
xp_awk_pair_t* pair, * prev;
xp_size_t hc;
hc = __hash(key) % map->capa;
pair = map->buck[hc];
prev = XP_NULL;
while (pair != XP_NULL) {
if (xp_strcmp(pair->key,key) == 0) {
if (prev == XP_NULL)
map->buck[hc] = pair->next;
else prev->next = pair->next;
FREE_PAIR (map, pair);
map->size--;
return 0;
}
prev = pair;
pair = pair->next;
}
return -1;
}
int xp_awk_map_walk (xp_awk_map_t* map, int (*walker) (xp_awk_pair_t*))
{
xp_size_t i;
xp_awk_pair_t* pair, * next;
for (i = 0; i < map->capa; i++) {
pair = map->buck[i];
while (pair != XP_NULL) {
next = pair->next;
if (walker(pair) == -1) return -1;
pair = next;
}
}
return 0;
}
static xp_size_t __hash (const xp_char_t* key)
{
xp_size_t n = 0, i;
while (*key != XP_CHAR('\0')) {
xp_byte_t* bp = (xp_byte_t*)key;
for (i = 0; i < xp_sizeof(*key); i++) n = n * 31 + *bp++;
key++;
}
return n;
}

55
ase/awk/map.h Normal file
View File

@ -0,0 +1,55 @@
/*
* $Id: map.h,v 1.1 2006-03-02 15:10:59 bacon Exp $
*/
#ifndef _XP_AWK_MAP_H_
#define _XP_AWK_MAP_H_
#ifdef __STAND_ALONE
#include <xp/awk/sa.h>
#else
#include <xp/types.h>
#include <xp/macros.h>
#endif
typedef struct xp_awk_map_t xp_awk_map_t;
typedef struct xp_awk_pair_t xp_awk_pair_t;
struct xp_awk_pair_t
{
xp_char_t* key;
void* value;
xp_awk_pair_t* next;
};
struct xp_awk_map_t
{
xp_size_t size;
xp_size_t capa;
xp_awk_pair_t** buck;
void (*free_value) (void*);
xp_bool_t __dynamic;
};
#ifdef __cplusplus
extern "C" {
#endif
xp_awk_map_t* xp_awk_map_open (
xp_awk_map_t* map, xp_size_t capa, void (*free_value) (void*));
void xp_awk_map_close (xp_awk_map_t* map);
void xp_awk_map_clear (xp_awk_map_t* map);
xp_awk_pair_t* xp_awk_map_get (xp_awk_map_t* map, xp_char_t* key);
xp_awk_pair_t* xp_awk_map_put (xp_awk_map_t* map, xp_char_t* key, void* value);
xp_awk_pair_t* xp_awk_map_set (xp_awk_map_t* map, xp_char_t* key, void* value);
int xp_awk_map_remove (xp_awk_map_t* map, xp_char_t* key);
int xp_awk_map_walk (xp_awk_map_t* map, int (*walker) (xp_awk_pair_t*));
#ifdef __cplusplus
}
#endif
#endif

View File

@ -1,5 +1,5 @@
/*
* $Id: parse.c,v 1.53 2006-02-18 16:14:14 bacon Exp $
* $Id: parse.c,v 1.54 2006-03-02 15:10:59 bacon Exp $
*/
#include <xp/awk/awk.h>
@ -239,7 +239,7 @@ static void __dump (xp_awk_t* awk)
xp_printf (XP_TEXT("__global%lu;\n\n"), (unsigned long)i);
}
xp_awk_hash_walk (&awk->tree.funcs, __dump_func);
xp_awk_map_walk (&awk->tree.funcs, __dump_func);
if (awk->tree.begin != XP_NULL) {
xp_printf (XP_TEXT("BEGIN "));
@ -350,7 +350,7 @@ static xp_awk_node_t* __parse_function (xp_awk_t* awk)
}
name = XP_STR_BUF(&awk->token.name);
if (xp_awk_hash_get(&awk->tree.funcs, name) != XP_NULL) {
if (xp_awk_map_get(&awk->tree.funcs, name) != XP_NULL) {
/* the function is defined previously */
PANIC (awk, XP_AWK_EDUPFUNC);
}
@ -411,7 +411,7 @@ static xp_awk_node_t* __parse_function (xp_awk_t* awk)
if (awk->opt.parse & XP_AWK_UNIQUE) {
/* check if a parameter conflicts with a function */
if (xp_strcmp(name_dup, param) == 0 ||
xp_awk_hash_get(&awk->tree.funcs, param) != XP_NULL) {
xp_awk_map_get(&awk->tree.funcs, param) != XP_NULL) {
xp_free (name_dup);
xp_awk_tab_clear (&awk->parse.params);
PANIC (awk, XP_AWK_EDUPNAME);
@ -502,8 +502,8 @@ static xp_awk_node_t* __parse_function (xp_awk_t* awk)
func->nargs = nargs;
func->body = body;
xp_assert (xp_awk_hash_get(&awk->tree.funcs, name_dup) == XP_NULL);
if (xp_awk_hash_put(&awk->tree.funcs, name_dup, func) == XP_NULL) {
xp_assert (xp_awk_map_get(&awk->tree.funcs, name_dup) == XP_NULL);
if (xp_awk_map_put(&awk->tree.funcs, name_dup, func) == XP_NULL) {
xp_free (name_dup);
xp_awk_clrpt (body);
xp_free (func);
@ -702,7 +702,7 @@ static xp_awk_t* __collect_globals (xp_awk_t* awk)
if (awk->opt.parse & XP_AWK_UNIQUE) {
/* check if it conflict with a function name */
if (xp_awk_hash_get(&awk->tree.funcs, global) != XP_NULL) {
if (xp_awk_map_get(&awk->tree.funcs, global) != XP_NULL) {
PANIC (awk, XP_AWK_EDUPNAME);
}
}
@ -748,7 +748,7 @@ static xp_awk_t* __collect_locals (xp_awk_t* awk, xp_size_t nlocals)
if (awk->opt.parse & XP_AWK_UNIQUE) {
/* check if it conflict with a function name */
if (xp_awk_hash_get(&awk->tree.funcs, local) != XP_NULL) {
if (xp_awk_map_get(&awk->tree.funcs, local) != XP_NULL) {
PANIC (awk, XP_AWK_EDUPNAME);
}
}

View File

@ -1,5 +1,5 @@
/*
* $Id: run.c,v 1.2 2006-02-23 15:37:34 bacon Exp $
* $Id: run.c,v 1.3 2006-03-02 15:10:59 bacon Exp $
*/
#include <xp/awk/awk.h>
@ -29,6 +29,7 @@ static int __run_block (xp_awk_t* awk, xp_awk_node_t* node)
{
xp_assert (node->type == XP_AWK_NODE_BLOCK);
/*
xp_awk_node_t* p = node;
while (p != XP_NULL) {
@ -36,12 +37,14 @@ static int __run_block (xp_awk_t* awk, xp_awk_node_t* node)
if (__run_statement (awk, p) == -1) return -1;
p = p->next;
}
*/
return 0;
}
static int __run_statement (xp_awk_t* awk, xp_awk_node_t* node)
{
#if 0
switch (node->type) {
case XP_AWK_NODE_NULL:
/* do nothing */
@ -94,7 +97,7 @@ static int __run_assignment (xp_awk_t* awk, xp_awk_node_assign_t* node)
xp_awk_node_t* right = __eval_expr (awk, node->right);
if (right == NULL) return -1;
if (xp_awk_hash_insert(awk->run.named, right) == -1) {
if (xp_awk_map_insert(awk->run.named, right) == -1) {
awk->errnum = XP_AWK_ENOMEM;
return -1;
}
@ -119,6 +122,7 @@ static int __run_assignment (xp_awk_t* awk, xp_awk_node_assign_t* node)
awk->errnum = XP_AWK_EINTERNAL;
return -1;
}
#endif
return 0;
}