Added an initial version of stdlib.c
git-svn-id: http://picoc.googlecode.com/svn/trunk@434 21eae674-98b7-11dd-bd71-f92a316d2d60
This commit is contained in:
parent
0afc055ab1
commit
7a8014ee42
2
Makefile
2
Makefile
|
@ -6,7 +6,7 @@ TARGET = picoc
|
||||||
SRCS = picoc.c table.c lex.c parse.c expression.c heap.c type.c \
|
SRCS = picoc.c table.c lex.c parse.c expression.c heap.c type.c \
|
||||||
variable.c clibrary.c platform.c include.c \
|
variable.c clibrary.c platform.c include.c \
|
||||||
platform/platform_unix.c platform/library_unix.c \
|
platform/platform_unix.c platform/library_unix.c \
|
||||||
cstdlib/stdio.c cstdlib/math.c cstdlib/string.c
|
cstdlib/stdio.c cstdlib/math.c cstdlib/string.c cstdlib/stdlib.c
|
||||||
OBJS := $(SRCS:%.c=%.o)
|
OBJS := $(SRCS:%.c=%.o)
|
||||||
|
|
||||||
all: depend $(TARGET)
|
all: depend $(TARGET)
|
||||||
|
|
46
cstdlib/stdlib.c
Normal file
46
cstdlib/stdlib.c
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
/* stdlib.h library for large systems - small embedded systems use clibrary.c instead */
|
||||||
|
#include "../picoc.h"
|
||||||
|
|
||||||
|
#ifndef BUILTIN_MINI_STDLIB
|
||||||
|
|
||||||
|
static int ZeroValue = 0;
|
||||||
|
|
||||||
|
void StdlibMalloc(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
|
||||||
|
{
|
||||||
|
ReturnValue->Val->NativePointer = malloc(Param[0]->Val->Integer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void StdlibCalloc(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
|
||||||
|
{
|
||||||
|
ReturnValue->Val->NativePointer = calloc(Param[0]->Val->Integer, Param[1]->Val->Integer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void StdlibRealloc(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
|
||||||
|
{
|
||||||
|
ReturnValue->Val->NativePointer = realloc(Param[0]->Val->NativePointer, Param[1]->Val->Integer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void StdlibFree(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
|
||||||
|
{
|
||||||
|
free(Param[0]->Val->NativePointer);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* all stdlib.h functions */
|
||||||
|
struct LibraryFunction StdlibFunctions[] =
|
||||||
|
{
|
||||||
|
{ StdlibMalloc, "void *malloc(int);" },
|
||||||
|
{ StdlibCalloc, "void *calloc(int,int);" },
|
||||||
|
{ StdlibRealloc, "void *realloc(void *,int);" },
|
||||||
|
{ StdlibFree, "void free(void *);" },
|
||||||
|
{ NULL, NULL }
|
||||||
|
};
|
||||||
|
|
||||||
|
/* creates various system-dependent definitions */
|
||||||
|
void StdlibSetupFunc(void)
|
||||||
|
{
|
||||||
|
/* define NULL */
|
||||||
|
if (!VariableDefined(TableStrRegister("NULL")))
|
||||||
|
VariableDefinePlatformVar(NULL, "NULL", &IntType, (union AnyValue *)&ZeroValue, FALSE);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* !BUILTIN_MINI_STDLIB */
|
88
cstdlib/string.c
Normal file
88
cstdlib/string.c
Normal file
|
@ -0,0 +1,88 @@
|
||||||
|
/* string.h library for large systems - small embedded systems use clibrary.c instead */
|
||||||
|
#include "../picoc.h"
|
||||||
|
|
||||||
|
#ifndef BUILTIN_MINI_STDLIB
|
||||||
|
|
||||||
|
static int ZeroValue = 0;
|
||||||
|
|
||||||
|
void StringStrcpy(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
|
||||||
|
{
|
||||||
|
ReturnValue->Val->NativePointer = strcpy(Param[0]->Val->NativePointer, Param[1]->Val->NativePointer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void StringStrncpy(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
|
||||||
|
{
|
||||||
|
ReturnValue->Val->NativePointer = strncpy(Param[0]->Val->NativePointer, Param[1]->Val->NativePointer, Param[2]->Val->Integer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void StringStrcmp(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
|
||||||
|
{
|
||||||
|
ReturnValue->Val->Integer = strcmp(Param[0]->Val->NativePointer, Param[1]->Val->NativePointer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void StringStrncmp(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
|
||||||
|
{
|
||||||
|
ReturnValue->Val->Integer = strncmp(Param[0]->Val->NativePointer, Param[1]->Val->NativePointer, Param[2]->Val->Integer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void StringStrcat(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
|
||||||
|
{
|
||||||
|
ReturnValue->Val->NativePointer = strcat(Param[0]->Val->NativePointer, Param[1]->Val->NativePointer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void StringIndex(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
|
||||||
|
{
|
||||||
|
ReturnValue->Val->NativePointer = index(Param[0]->Val->NativePointer, Param[1]->Val->Integer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void StringRindex(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
|
||||||
|
{
|
||||||
|
ReturnValue->Val->NativePointer = rindex(Param[0]->Val->NativePointer, Param[1]->Val->Integer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void StringStrlen(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
|
||||||
|
{
|
||||||
|
ReturnValue->Val->Integer = strlen(Param[0]->Val->NativePointer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void StringMemset(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
|
||||||
|
{
|
||||||
|
memset(Param[0]->Val->NativePointer, Param[1]->Val->Integer, Param[2]->Val->Integer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void StringMemcpy(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
|
||||||
|
{
|
||||||
|
memcpy(Param[0]->Val->NativePointer, Param[1]->Val->NativePointer, Param[2]->Val->Integer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void StringMemcmp(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
|
||||||
|
{
|
||||||
|
ReturnValue->Val->Integer = memcmp(Param[0]->Val->NativePointer, Param[1]->Val->NativePointer, Param[2]->Val->Integer);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* all string.h functions */
|
||||||
|
struct LibraryFunction StringFunctions[] =
|
||||||
|
{
|
||||||
|
{ StringStrcpy, "char *strcpy(char *,char *);" },
|
||||||
|
{ StringStrncpy, "char *strncpy(char *,char *,int);" },
|
||||||
|
{ StringStrcmp, "int strcmp(char *,char *);" },
|
||||||
|
{ StringStrncmp, "int strncmp(char *,char *,int);" },
|
||||||
|
{ StringStrcat, "char *strcat(char *,char *);" },
|
||||||
|
{ StringIndex, "char *index(char *,int);" },
|
||||||
|
{ StringRindex, "char *rindex(char *,int);" },
|
||||||
|
{ StringStrlen, "int strlen(char *);" },
|
||||||
|
{ StringMemset, "void *memset(void *,int,int);" },
|
||||||
|
{ StringMemcpy, "void *memcpy(void *,void *,int);" },
|
||||||
|
{ StringMemcmp, "int memcmp(void *,void *,int);" },
|
||||||
|
{ NULL, NULL }
|
||||||
|
};
|
||||||
|
|
||||||
|
/* creates various system-dependent definitions */
|
||||||
|
void StringSetupFunc(void)
|
||||||
|
{
|
||||||
|
/* define NULL */
|
||||||
|
if (!VariableDefined(TableStrRegister("NULL")))
|
||||||
|
VariableDefinePlatformVar(NULL, "NULL", &IntType, (union AnyValue *)&ZeroValue, FALSE);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* !BUILTIN_MINI_STDLIB */
|
Loading…
Reference in a new issue