Skip to content

Commit

Permalink
WIP adding token type support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Bandur committed Mar 30, 2017
1 parent 7c8f77d commit 0da16de
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 20 deletions.
56 changes: 36 additions & 20 deletions c/vdmclib/src/main/TypedValue.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,22 @@ TVP newQuote(unsigned int x)
{ .quoteVal = x });
}

TVP newToken(TVP x)
{
char *str = unpackString(x);
int hashVal = 5381;
int c;

while (c = *str++)
hashVal = ((hashVal << 2) + hashVal) + c;

free(str);

return newTypeValue(VDM_TOKEN, (TypedValueType
)
{ .intVal = hashVal });
}

TVP newCollection(size_t size, vdmtype type)
{
struct Collection* ptr = (struct Collection*) malloc(sizeof(struct Collection));
Expand Down Expand Up @@ -575,36 +591,36 @@ void vdmFree(TVP ptr)
#ifndef NO_PRODUCTS
case VDM_PRODUCT:
{
UNWRAP_COLLECTION(cptr, ptr);
for (int i = 0; i < cptr->size; i++)
UNWRAP_COLLECTION(cptr, ptr);
for (int i = 0; i < cptr->size; i++)
{
if (cptr->value[i] != NULL)
{
if (cptr->value[i] != NULL)
{
vdmFree(cptr->value[i]);
}
vdmFree(cptr->value[i]);
}
free(cptr->value);
free(cptr);
ptr->value.ptr = NULL;
break;
}
free(cptr->value);
free(cptr);
ptr->value.ptr = NULL;
break;
}
#endif
#ifndef NO_SEQS
case VDM_SEQ:
{
UNWRAP_COLLECTION(cptr, ptr);
for (int i = 0; i < cptr->size; i++)
UNWRAP_COLLECTION(cptr, ptr);
for (int i = 0; i < cptr->size; i++)
{
if (cptr->value[i] != NULL)
{
if (cptr->value[i] != NULL)
{
vdmFree(cptr->value[i]);
}
vdmFree(cptr->value[i]);
}
free(cptr->value);
free(cptr);
ptr->value.ptr = NULL;
break;
}
free(cptr->value);
free(cptr);
ptr->value.ptr = NULL;
break;
}
#endif
#ifndef NO_SETS
case VDM_SET:
Expand Down
2 changes: 2 additions & 0 deletions c/vdmclib/src/main/TypedValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ typedef enum
#ifndef NO_RECORDS
VDM_RECORD,
#endif
VDM_TOKEN,
VDM_CLASS
} vdmtype;

Expand Down Expand Up @@ -160,6 +161,7 @@ TVP newBool(bool x);
TVP newReal(double x);
TVP newChar(char x);
TVP newQuote(unsigned int x);
TVP newToken(TVP x);



Expand Down
19 changes: 19 additions & 0 deletions c/vdmclib/src/main/VdmGC.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,25 +214,44 @@ TVP newBoolGC(bool x, TVP *from)
)
{ .boolVal = x }, from);
}

TVP newRealGC(double x, TVP *from)
{
return newTypeValueGC(VDM_REAL, (TypedValueType
)
{ .doubleVal = x }, from);
}

TVP newCharGC(char x, TVP *from)
{
return newTypeValueGC(VDM_CHAR, (TypedValueType
)
{ .charVal = x }, from);
}

TVP newQuoteGC(unsigned int x, TVP *from)
{
return newTypeValueGC(VDM_QUOTE, (TypedValueType
)
{ .quoteVal = x }, from);
}

TVP newTokenGC(TVP x, TVP *from)
{
char *str = unpackString(x);
int hashVal = 5381;
int c;

while (c = *str++)
hashVal = ((hashVal << 2) + hashVal) + c;

free(str);

return newTypeValueGC(VDM_TOKEN, (TypedValueType
)
{ .intVal = hashVal }, from);
}

TVP newCollectionGC(size_t size, vdmtype type)
{
struct Collection* ptr = (struct Collection*) malloc(sizeof(struct Collection));
Expand Down
2 changes: 2 additions & 0 deletions c/vdmclib/src/main/VdmGC.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define VDMGC_H_

#include "Vdm.h"
#include "VdmUnpackString.h"

struct alloc_list_node
{
Expand Down Expand Up @@ -31,6 +32,7 @@ TVP newBoolGC(bool x, TVP *from);
TVP newRealGC(double x, TVP *from);
TVP newCharGC(char x, TVP *from);
TVP newQuoteGC(unsigned int x, TVP *from);
TVP newTokenGC(TVP x, TVP *from);
TVP vdmEqualsGC(TVP a, TVP b, TVP *from);
//#endif /* WITH_GC */

Expand Down

0 comments on commit 0da16de

Please sign in to comment.