Added example functions lineno() and errorprintf()

git-svn-id: http://picoc.googlecode.com/svn/trunk@405 21eae674-98b7-11dd-bd71-f92a316d2d60
This commit is contained in:
zik.saleeba 2010-02-03 20:01:01 +00:00
parent 8f9095b3a4
commit 441f8b4073
4 changed files with 36 additions and 13 deletions

View file

@ -724,6 +724,15 @@ void Cautorun (struct ParseState *Parser, struct Value *ReturnValue, struct Valu
}
}
void Clineno (struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) {
ReturnValue->Val->Integer = Parser->Line;
}
void Cerrormsg (struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) {
PlatformErrorPrefix(Parser);
LibPrintf(Parser, ReturnValue, Param, NumArgs);
}
/* list of all library functions and their prototypes */
struct LibraryFunction PlatformLibrary[] =
{
@ -782,6 +791,8 @@ struct LibraryFunction PlatformLibrary[] =
{ Cnnmatchblob, "int nnmatchblob(int)" },
{ Cnnlearnblob, "void nnlearnblob(int)" },
{ Cautorun, "void autorun(int)" },
{ Clineno, "int lineno()" },
{ Cerrormsg, "void errormsg(char *)" },
{ NULL, NULL }
};

View file

@ -10,10 +10,21 @@ void Ctest (struct ParseState *Parser, struct Value *ReturnValue, struct Value *
Param[0]->Val->Integer = 1234;
}
void Clineno (struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) {
ReturnValue->Val->Integer = Parser->Line;
}
void Cerrormsg (struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) {
PlatformErrorPrefix(Parser);
LibPrintf(Parser, ReturnValue, Param, NumArgs);
}
/* list of all library functions and their prototypes */
struct LibraryFunction PlatformLibrary[] =
{
{ Ctest, "void test(int)" },
{ Clineno, "int lineno()" },
{ Cerrormsg, "void errorprintf(char *,...)" },
{ NULL, NULL }
};

View file

@ -379,6 +379,7 @@ void PrintInt(long Num, int FieldWidth, int ZeroPad, int LeftJustify, struct Out
void PrintStr(const char *Str, struct OutputStream *Stream);
void PrintFP(double Num, struct OutputStream *Stream);
void PrintType(struct ValueType *Typ, struct OutputStream *Stream);
void LibPrintf(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs);
/* platform.c */
void ProgramFail(struct ParseState *Parser, const char *Message, ...);
@ -389,6 +390,7 @@ void PlatformScanFile(const char *FileName);
char *PlatformGetLine(char *Buf, int MaxLen);
int PlatformGetCharacter();
void PlatformPutc(unsigned char OutCh, union OutputStreamInfo *);
void PlatformErrorPrefix(struct ParseState *Parser);
void PlatformPrintf(const char *Format, ...);
void PlatformVPrintf(const char *Format, va_list Args);
void PlatformExit();

View file

@ -42,18 +42,23 @@ void PrintSourceTextErrorLine(const char *FileName, const char *SourceText, int
}
#endif
/* exit with a message */
void ProgramFail(struct ParseState *Parser, const char *Message, ...)
/* display the source line and line number to identify an error */
void PlatformErrorPrefix(struct ParseState *Parser)
{
va_list Args;
if (Parser != NULL)
#ifdef FANCY_ERROR_REPORTING
PrintSourceTextErrorLine(Parser->FileName, Parser->SourceText, Parser->Line, Parser->CharacterPos);
#else
PlatformPrintf("%s:%d: ", Parser->FileName, Parser->Line);
#endif
}
/* exit with a message */
void ProgramFail(struct ParseState *Parser, const char *Message, ...)
{
va_list Args;
PlatformErrorPrefix(Parser);
va_start(Args, Message);
PlatformVPrintf(Message, Args);
va_end(Args);
@ -64,13 +69,7 @@ void ProgramFail(struct ParseState *Parser, const char *Message, ...)
/* like ProgramFail() but gives descriptive error messages for assignment */
void AssignFail(struct ParseState *Parser, const char *Format, struct ValueType *Type1, struct ValueType *Type2, int Num1, int Num2, const char *FuncName, int ParamNo)
{
if (Parser != NULL)
#ifdef FANCY_ERROR_REPORTING
PrintSourceTextErrorLine(Parser->FileName, Parser->SourceText, Parser->Line, Parser->CharacterPos);
#else
PlatformPrintf("%s:%d: ", Parser->FileName, Parser->Line);
#endif
PlatformErrorPrefix(Parser);
PlatformPrintf("can't %s ", (FuncName == NULL) ? "assign" : "set");
if (Type1 != NULL)