diff --git a/library_surveyor.c b/library_surveyor.c index 9eef65b..8fe4255 100644 --- a/library_surveyor.c +++ b/library_surveyor.c @@ -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 } }; diff --git a/library_unix.c b/library_unix.c index 9c46f3b..9b4ab2b 100644 --- a/library_unix.c +++ b/library_unix.c @@ -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)" }, + { Ctest, "void test(int)" }, + { Clineno, "int lineno()" }, + { Cerrormsg, "void errorprintf(char *,...)" }, { NULL, NULL } }; diff --git a/picoc.h b/picoc.h index 9295252..1d5ff86 100644 --- a/picoc.h +++ b/picoc.h @@ -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(); diff --git a/platform.c b/platform.c index 4b88cc7..f71a519 100644 --- a/platform.c +++ b/platform.c @@ -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)