2009-03-15 03:07:21 -04:00
|
|
|
#include "picoc.h"
|
2011-02-17 02:11:20 -05:00
|
|
|
#include "interpreter.h"
|
2009-03-15 03:07:21 -04:00
|
|
|
|
2010-07-27 11:35:25 -04:00
|
|
|
/* the value passed to exit() */
|
2011-02-17 02:11:20 -05:00
|
|
|
int PicocExitValue = 0;
|
|
|
|
|
|
|
|
/* initialise everything */
|
|
|
|
void PicocInitialise(int StackSize)
|
|
|
|
{
|
|
|
|
BasicIOInit();
|
|
|
|
HeapInit(StackSize);
|
|
|
|
TableInit();
|
|
|
|
VariableInit();
|
|
|
|
LexInit();
|
|
|
|
TypeInit();
|
|
|
|
#ifndef NO_HASH_INCLUDE
|
|
|
|
IncludeInit();
|
|
|
|
#endif
|
|
|
|
LibraryInit();
|
|
|
|
#ifdef BUILTIN_MINI_STDLIB
|
|
|
|
LibraryAdd(&GlobalTable, "c library", &CLibrary[0]);
|
|
|
|
CLibraryInit();
|
|
|
|
#endif
|
|
|
|
PlatformLibraryInit();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* free memory */
|
|
|
|
void PicocCleanup()
|
|
|
|
{
|
|
|
|
PlatformCleanup();
|
|
|
|
#ifndef NO_HASH_INCLUDE
|
|
|
|
IncludeCleanup();
|
|
|
|
#endif
|
|
|
|
ParseCleanup();
|
|
|
|
LexCleanup();
|
|
|
|
VariableCleanup();
|
|
|
|
TypeCleanup();
|
|
|
|
TableStrFree();
|
|
|
|
HeapCleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* platform-dependent code for running programs */
|
|
|
|
#ifdef UNIX_HOST
|
|
|
|
|
|
|
|
#define CALL_MAIN_NO_ARGS_RETURN_VOID "main();"
|
|
|
|
#define CALL_MAIN_WITH_ARGS_RETURN_VOID "main(__argc,__argv);"
|
|
|
|
#define CALL_MAIN_NO_ARGS_RETURN_INT "__exit_value = main();"
|
|
|
|
#define CALL_MAIN_WITH_ARGS_RETURN_INT "__exit_value = main(__argc,__argv);"
|
|
|
|
|
|
|
|
void PicocCallMain(int argc, char **argv)
|
|
|
|
{
|
|
|
|
/* check if the program wants arguments */
|
|
|
|
struct Value *FuncValue = NULL;
|
|
|
|
|
|
|
|
if (!VariableDefined(TableStrRegister("main")))
|
|
|
|
ProgramFail(NULL, "main() is not defined");
|
|
|
|
|
|
|
|
VariableGet(NULL, TableStrRegister("main"), &FuncValue);
|
|
|
|
if (FuncValue->Typ->Base != TypeFunction)
|
|
|
|
ProgramFail(NULL, "main is not a function - can't call it");
|
|
|
|
|
|
|
|
if (FuncValue->Val->FuncDef.NumParams != 0)
|
|
|
|
{
|
|
|
|
/* define the arguments */
|
|
|
|
VariableDefinePlatformVar(NULL, "__argc", &IntType, (union AnyValue *)&argc, FALSE);
|
|
|
|
VariableDefinePlatformVar(NULL, "__argv", CharPtrPtrType, (union AnyValue *)&argv, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FuncValue->Val->FuncDef.ReturnType == &VoidType)
|
|
|
|
{
|
|
|
|
if (FuncValue->Val->FuncDef.NumParams == 0)
|
|
|
|
PicocParse("startup", CALL_MAIN_NO_ARGS_RETURN_VOID, strlen(CALL_MAIN_NO_ARGS_RETURN_VOID), TRUE, TRUE, FALSE);
|
|
|
|
else
|
|
|
|
PicocParse("startup", CALL_MAIN_WITH_ARGS_RETURN_VOID, strlen(CALL_MAIN_WITH_ARGS_RETURN_VOID), TRUE, TRUE, FALSE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
VariableDefinePlatformVar(NULL, "__exit_value", &IntType, (union AnyValue *)&PicocExitValue, TRUE);
|
|
|
|
|
|
|
|
if (FuncValue->Val->FuncDef.NumParams == 0)
|
|
|
|
PicocParse("startup", CALL_MAIN_NO_ARGS_RETURN_INT, strlen(CALL_MAIN_NO_ARGS_RETURN_INT), TRUE, TRUE, FALSE);
|
|
|
|
else
|
|
|
|
PicocParse("startup", CALL_MAIN_WITH_ARGS_RETURN_INT, strlen(CALL_MAIN_WITH_ARGS_RETURN_INT), TRUE, TRUE, FALSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2010-07-27 11:35:25 -04:00
|
|
|
|
2009-06-04 03:41:06 -04:00
|
|
|
void PrintSourceTextErrorLine(const char *FileName, const char *SourceText, int Line, int CharacterPos)
|
|
|
|
{
|
|
|
|
int LineCount;
|
|
|
|
const char *LinePos;
|
|
|
|
const char *CPos;
|
|
|
|
int CCount;
|
|
|
|
|
|
|
|
if (SourceText != NULL)
|
|
|
|
{
|
|
|
|
/* find the source line */
|
|
|
|
for (LinePos = SourceText, LineCount = 1; *LinePos != '\0' && LineCount < Line; LinePos++)
|
|
|
|
{
|
|
|
|
if (*LinePos == '\n')
|
|
|
|
LineCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* display the line */
|
|
|
|
for (CPos = LinePos; *CPos != '\n' && *CPos != '\0'; CPos++)
|
2010-06-11 13:12:49 -04:00
|
|
|
PrintCh(*CPos, CStdOut);
|
|
|
|
PrintCh('\n', CStdOut);
|
2009-06-04 03:41:06 -04:00
|
|
|
|
|
|
|
/* display the error position */
|
|
|
|
for (CPos = LinePos, CCount = 0; *CPos != '\n' && *CPos != '\0' && (CCount < CharacterPos || *CPos == ' '); CPos++, CCount++)
|
|
|
|
{
|
|
|
|
if (*CPos == '\t')
|
2010-06-11 13:12:49 -04:00
|
|
|
PrintCh('\t', CStdOut);
|
2009-06-04 03:41:06 -04:00
|
|
|
else
|
2010-06-11 13:12:49 -04:00
|
|
|
PrintCh(' ', CStdOut);
|
2009-06-04 03:41:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* assume we're in interactive mode - try to make the arrow match up with the input text */
|
|
|
|
for (CCount = 0; CCount < CharacterPos + strlen(INTERACTIVE_PROMPT_STATEMENT); CCount++)
|
2010-06-11 13:12:49 -04:00
|
|
|
PrintCh(' ', CStdOut);
|
2009-06-04 03:41:06 -04:00
|
|
|
}
|
|
|
|
PlatformPrintf("^\n%s:%d: ", FileName, Line, CharacterPos);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-02-03 15:01:01 -05:00
|
|
|
/* display the source line and line number to identify an error */
|
|
|
|
void PlatformErrorPrefix(struct ParseState *Parser)
|
2009-03-15 03:07:21 -04:00
|
|
|
{
|
|
|
|
if (Parser != NULL)
|
2009-06-04 03:41:06 -04:00
|
|
|
PrintSourceTextErrorLine(Parser->FileName, Parser->SourceText, Parser->Line, Parser->CharacterPos);
|
2010-02-03 15:01:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* exit with a message */
|
|
|
|
void ProgramFail(struct ParseState *Parser, const char *Message, ...)
|
|
|
|
{
|
|
|
|
va_list Args;
|
|
|
|
|
|
|
|
PlatformErrorPrefix(Parser);
|
2009-03-15 03:07:21 -04:00
|
|
|
va_start(Args, Message);
|
|
|
|
PlatformVPrintf(Message, Args);
|
2009-03-15 20:47:01 -04:00
|
|
|
va_end(Args);
|
2009-03-15 03:07:21 -04:00
|
|
|
PlatformPrintf("\n");
|
|
|
|
PlatformExit(1);
|
|
|
|
}
|
|
|
|
|
2009-06-04 03:41:06 -04:00
|
|
|
/* 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)
|
|
|
|
{
|
2010-02-03 15:01:01 -05:00
|
|
|
PlatformErrorPrefix(Parser);
|
2009-06-04 03:41:06 -04:00
|
|
|
PlatformPrintf("can't %s ", (FuncName == NULL) ? "assign" : "set");
|
|
|
|
|
|
|
|
if (Type1 != NULL)
|
|
|
|
PlatformPrintf(Format, Type1, Type2);
|
|
|
|
else
|
|
|
|
PlatformPrintf(Format, Num1, Num2);
|
|
|
|
|
|
|
|
if (FuncName != NULL)
|
|
|
|
PlatformPrintf(" in argument %d of call to %s()", ParamNo, FuncName);
|
|
|
|
|
|
|
|
ProgramFail(NULL, "");
|
|
|
|
}
|
|
|
|
|
2009-03-15 03:07:21 -04:00
|
|
|
/* exit lexing with a message */
|
|
|
|
void LexFail(struct LexState *Lexer, const char *Message, ...)
|
|
|
|
{
|
|
|
|
va_list Args;
|
|
|
|
|
2009-06-04 03:41:06 -04:00
|
|
|
PrintSourceTextErrorLine(Lexer->FileName, Lexer->SourceText, Lexer->Line, Lexer->CharacterPos);
|
2009-03-15 03:07:21 -04:00
|
|
|
va_start(Args, Message);
|
|
|
|
PlatformVPrintf(Message, Args);
|
2009-03-15 20:47:01 -04:00
|
|
|
va_end(Args);
|
2009-03-15 03:07:21 -04:00
|
|
|
PlatformPrintf("\n");
|
|
|
|
PlatformExit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* printf for compiler error reporting */
|
|
|
|
void PlatformPrintf(const char *Format, ...)
|
|
|
|
{
|
|
|
|
va_list Args;
|
|
|
|
|
|
|
|
va_start(Args, Format);
|
|
|
|
PlatformVPrintf(Format, Args);
|
|
|
|
va_end(Args);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlatformVPrintf(const char *Format, va_list Args)
|
|
|
|
{
|
|
|
|
const char *FPos;
|
|
|
|
|
|
|
|
for (FPos = Format; *FPos != '\0'; FPos++)
|
|
|
|
{
|
|
|
|
if (*FPos == '%')
|
|
|
|
{
|
|
|
|
FPos++;
|
|
|
|
switch (*FPos)
|
|
|
|
{
|
2010-06-11 13:12:49 -04:00
|
|
|
case 's': PrintStr(va_arg(Args, char *), CStdOut); break;
|
|
|
|
case 'd': PrintSimpleInt(va_arg(Args, int), CStdOut); break;
|
|
|
|
case 'c': PrintCh(va_arg(Args, int), CStdOut); break;
|
|
|
|
case 't': PrintType(va_arg(Args, struct ValueType *), CStdOut); break;
|
2009-03-15 03:07:21 -04:00
|
|
|
#ifndef NO_FP
|
2010-06-11 13:12:49 -04:00
|
|
|
case 'f': PrintFP(va_arg(Args, double), CStdOut); break;
|
2009-03-15 03:07:21 -04:00
|
|
|
#endif
|
2010-06-11 13:12:49 -04:00
|
|
|
case '%': PrintCh('%', CStdOut); break;
|
2009-03-15 03:07:21 -04:00
|
|
|
case '\0': FPos--; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2010-06-11 13:12:49 -04:00
|
|
|
PrintCh(*FPos, CStdOut);
|
2009-03-15 03:07:21 -04:00
|
|
|
}
|
|
|
|
}
|
2011-02-17 21:16:51 -05:00
|
|
|
|
|
|
|
/* make a new temporary name. takes a static buffer of char [7] as a parameter. should be initialised to "XX0000"
|
|
|
|
* where XX can be any characters */
|
|
|
|
char *PlatformMakeTempName(char *TempNameBuffer)
|
|
|
|
{
|
|
|
|
int CPos = 5;
|
|
|
|
|
|
|
|
while (CPos > 1)
|
|
|
|
{
|
|
|
|
if (TempNameBuffer[CPos] < '9')
|
|
|
|
{
|
|
|
|
TempNameBuffer[CPos]++;
|
2011-02-17 22:30:34 -05:00
|
|
|
return TableStrRegister(TempNameBuffer);
|
2011-02-17 21:16:51 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-02-17 22:30:34 -05:00
|
|
|
TempNameBuffer[CPos] = '0';
|
2011-02-17 21:16:51 -05:00
|
|
|
CPos--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-17 22:30:34 -05:00
|
|
|
return TableStrRegister(TempNameBuffer);
|
2011-02-17 21:16:51 -05:00
|
|
|
}
|