Transitional state
git-svn-id: http://picoc.googlecode.com/svn/trunk@109 21eae674-98b7-11dd-bd71-f92a316d2d60
This commit is contained in:
parent
aa213f4129
commit
54efa99a65
119
intrinsic.c
119
intrinsic.c
|
@ -1,11 +1,29 @@
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <math.h>
|
|
||||||
#include "picoc.h"
|
#include "picoc.h"
|
||||||
|
|
||||||
|
/* initialise a library */
|
||||||
|
void LibraryInit(struct Table *GlobalTable, const char *LibraryName, struct IntrinsicFunction *FuncList[])
|
||||||
|
{
|
||||||
|
struct ParseState Parser;
|
||||||
|
int Count;
|
||||||
|
char *Identifier;
|
||||||
|
struct ValueType *ReturnType;
|
||||||
|
struct Value *NewValue;
|
||||||
|
void *Tokens;
|
||||||
|
const char *IntrinsicName = TableStrRegister("c library");
|
||||||
|
|
||||||
|
for (Count = 0; Count < sizeof(Intrinsics) / sizeof(struct IntrinsicFunction); Count++)
|
||||||
|
{
|
||||||
|
Tokens = LexAnalyse(IntrinsicName, Intrinsics[Count].Prototype, strlen(Intrinsics[Count].Prototype));
|
||||||
|
LexInitParser(&Parser, Tokens, IntrinsicName, Count+1, TRUE);
|
||||||
|
TypeParse(&Parser, &ReturnType, &Identifier);
|
||||||
|
NewValue = ParseFunctionDefinition(&Parser, ReturnType, Identifier, TRUE);
|
||||||
|
NewValue->Val->FuncDef.Intrinsic = Intrinsics[Count].Func;
|
||||||
|
HeapFree(Tokens);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* print an integer to a stream without using printf/sprintf */
|
/* print an integer to a stream without using printf/sprintf */
|
||||||
void IntrinsicPrintInt(int Num, FILE *Stream)
|
void PrintInt(int Num, FILE *Stream)
|
||||||
{
|
{
|
||||||
int Div;
|
int Div;
|
||||||
int Remainder = 0;
|
int Remainder = 0;
|
||||||
|
@ -38,7 +56,7 @@ void IntrinsicPrintInt(int Num, FILE *Stream)
|
||||||
|
|
||||||
#ifndef NO_FP
|
#ifndef NO_FP
|
||||||
/* print a double to a stream without using printf/sprintf */
|
/* print a double to a stream without using printf/sprintf */
|
||||||
void IntrinsicPrintFP(double Num, FILE *Stream)
|
void PrintFP(double Num, FILE *Stream)
|
||||||
{
|
{
|
||||||
int Exponent = 0;
|
int Exponent = 0;
|
||||||
|
|
||||||
|
@ -48,7 +66,7 @@ void IntrinsicPrintFP(double Num, FILE *Stream)
|
||||||
Exponent = log(Num) / LOG10E - 0.999999999;
|
Exponent = log(Num) / LOG10E - 0.999999999;
|
||||||
|
|
||||||
Num /= pow(10.0, Exponent);
|
Num /= pow(10.0, Exponent);
|
||||||
IntrinsicPrintInt((int)Num, Stream);
|
PrintInt((int)Num, Stream);
|
||||||
fputc('.', Stream);
|
fputc('.', Stream);
|
||||||
for (Num -= (int)Num; Num != 0.0; Num *= 10.0)
|
for (Num -= (int)Num; Num != 0.0; Num *= 10.0)
|
||||||
fputc('0' + (int)Num, Stream);
|
fputc('0' + (int)Num, Stream);
|
||||||
|
@ -56,18 +74,13 @@ void IntrinsicPrintFP(double Num, FILE *Stream)
|
||||||
if (Exponent)
|
if (Exponent)
|
||||||
{
|
{
|
||||||
fputc('e', Stream);
|
fputc('e', Stream);
|
||||||
IntrinsicPrintInt(Exponent, Stream);
|
PrintInt(Exponent, Stream);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* intrinsic functions made available to the language */
|
/* intrinsic functions made available to the language */
|
||||||
void IntrinsicPrintInteger(struct Value *ReturnValue, struct Value **Param, int NumArgs)
|
void LibPrintf(struct Value *ReturnValue, struct Value **Param, int NumArgs)
|
||||||
{
|
|
||||||
printf("%d\n", Param[0]->Val->Integer);
|
|
||||||
}
|
|
||||||
|
|
||||||
void IntrinsicPrintf(struct Value *ReturnValue, struct Value **Param, int NumArgs)
|
|
||||||
{
|
{
|
||||||
struct Value *CharArray = Param[0]->Val->Pointer.Segment;
|
struct Value *CharArray = Param[0]->Val->Pointer.Segment;
|
||||||
char *Format;
|
char *Format;
|
||||||
|
@ -124,10 +137,10 @@ void IntrinsicPrintf(struct Value *ReturnValue, struct Value **Param, int NumArg
|
||||||
fputs(Str, stdout);
|
fputs(Str, stdout);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'd': IntrinsicPrintInt(NextArg->Val->Integer, stdout); break;
|
case 'd': PrintInt(NextArg->Val->Integer, stdout); break;
|
||||||
case 'c': fputc(NextArg->Val->Integer, stdout); break;
|
case 'c': fputc(NextArg->Val->Integer, stdout); break;
|
||||||
#ifndef NO_FP
|
#ifndef NO_FP
|
||||||
case 'f': IntrinsicPrintFP(NextArg->Val->FP, stdout); break;
|
case 'f': PrintFP(NextArg->Val->FP, stdout); break;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -141,77 +154,7 @@ void IntrinsicPrintf(struct Value *ReturnValue, struct Value **Param, int NumArg
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct IntrinsicFunction CLibrary[] =
|
||||||
|
|
||||||
void IntrinsicSayHello(struct Value *ReturnValue, struct Value **Param, int NumArgs)
|
|
||||||
{
|
{
|
||||||
printf("Hello\n");
|
{ LibPrintf, "void printf(char *, ...)" },
|
||||||
}
|
|
||||||
|
|
||||||
struct IntrinsicFunction
|
|
||||||
{
|
|
||||||
void (*Func)(struct Value *, struct Value **, int);
|
|
||||||
const char *Prototype;
|
|
||||||
} Intrinsics[] =
|
|
||||||
{
|
|
||||||
{ IntrinsicSayHello, "void sayhello()" },
|
|
||||||
{ IntrinsicPrintf, "void printf(char *, ...)" },
|
|
||||||
{ IntrinsicPrintInteger,"void printint(int)" },
|
|
||||||
};
|
};
|
||||||
|
|
||||||
void IntrinsicInit(struct Table *GlobalTable)
|
|
||||||
{
|
|
||||||
struct ParseState Parser;
|
|
||||||
int Count;
|
|
||||||
char *Identifier;
|
|
||||||
struct ValueType *ReturnType;
|
|
||||||
struct Value *NewValue;
|
|
||||||
void *Tokens;
|
|
||||||
const char *IntrinsicName = TableStrRegister("intrinsic");
|
|
||||||
|
|
||||||
for (Count = 0; Count < sizeof(Intrinsics) / sizeof(struct IntrinsicFunction); Count++)
|
|
||||||
{
|
|
||||||
Tokens = LexAnalyse(IntrinsicName, Intrinsics[Count].Prototype, strlen(Intrinsics[Count].Prototype));
|
|
||||||
LexInitParser(&Parser, Tokens, IntrinsicName, Count+1, TRUE);
|
|
||||||
TypeParse(&Parser, &ReturnType, &Identifier);
|
|
||||||
NewValue = ParseFunctionDefinition(&Parser, ReturnType, Identifier, TRUE);
|
|
||||||
NewValue->Val->FuncDef.Intrinsic = Intrinsics[Count].Func;
|
|
||||||
HeapFree(Tokens);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void IntrinsicHostPrintf(const char *Format, ...)
|
|
||||||
{
|
|
||||||
va_list Args;
|
|
||||||
|
|
||||||
va_start(Args, Format);
|
|
||||||
IntrinsicHostVPrintf(Format, Args);
|
|
||||||
va_end(Args);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* printf for compiler error reporting */
|
|
||||||
void IntrinsicHostVPrintf(const char *Format, va_list Args)
|
|
||||||
{
|
|
||||||
const char *FPos;
|
|
||||||
|
|
||||||
for (FPos = Format; *FPos != '\0'; FPos++)
|
|
||||||
{
|
|
||||||
if (*FPos == '%')
|
|
||||||
{
|
|
||||||
FPos++;
|
|
||||||
switch (*FPos)
|
|
||||||
{
|
|
||||||
case 's': fputs(va_arg(Args, char *), stdout); break;
|
|
||||||
case 'd': IntrinsicPrintInt(va_arg(Args, int), stdout); break;
|
|
||||||
case 'c': fputc(va_arg(Args, int), stdout); break;
|
|
||||||
#ifndef NO_FP
|
|
||||||
case 'f': IntrinsicPrintFP(va_arg(Args, double), stdout); break;
|
|
||||||
#endif
|
|
||||||
case '%': fputc('%', stdout); break;
|
|
||||||
case '\0': FPos--; break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
putchar(*FPos);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue