2008-10-12 20:53:28 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2009-02-01 22:27:05 -05:00
|
|
|
#include <string.h>
|
2008-10-12 20:53:28 -04:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "picoc.h"
|
|
|
|
|
|
|
|
/* all platform-dependent code is in this file */
|
|
|
|
|
2009-02-01 06:31:18 -05:00
|
|
|
void ProgramFail(struct ParseState *Parser, const char *Message, ...)
|
2008-10-12 20:53:28 -04:00
|
|
|
{
|
|
|
|
va_list Args;
|
|
|
|
|
2009-02-01 06:31:18 -05:00
|
|
|
if (Parser != NULL)
|
|
|
|
printf("%s:%d: ", Parser->FileName, Parser->Line);
|
2009-01-29 17:26:04 -05:00
|
|
|
|
2008-10-12 20:53:28 -04:00
|
|
|
va_start(Args, Message);
|
2009-02-01 06:31:18 -05:00
|
|
|
vprintf(Message, Args);
|
|
|
|
printf("\n");
|
2008-10-12 20:53:28 -04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2009-02-01 06:31:18 -05:00
|
|
|
/* read a file into memory */
|
|
|
|
char *ReadFile(const char *FileName)
|
2008-10-12 20:53:28 -04:00
|
|
|
{
|
|
|
|
struct stat FileInfo;
|
2009-01-05 00:50:04 -05:00
|
|
|
char *ReadText;
|
2008-10-12 20:53:28 -04:00
|
|
|
FILE *InFile;
|
|
|
|
|
2009-02-01 06:31:18 -05:00
|
|
|
if (stat(FileName, &FileInfo))
|
|
|
|
ProgramFail(NULL, "can't read file %s\n", FileName);
|
2008-10-12 20:53:28 -04:00
|
|
|
|
2009-02-01 22:27:05 -05:00
|
|
|
ReadText = HeapAlloc(FileInfo.st_size + 1);
|
2009-01-05 00:50:04 -05:00
|
|
|
if (ReadText == NULL)
|
2009-02-01 06:31:18 -05:00
|
|
|
ProgramFail(NULL, "out of memory\n");
|
2009-01-05 00:50:04 -05:00
|
|
|
|
2009-02-01 06:31:18 -05:00
|
|
|
InFile = fopen(FileName, "r");
|
2008-10-12 20:53:28 -04:00
|
|
|
if (InFile == NULL)
|
2009-02-01 06:31:18 -05:00
|
|
|
ProgramFail(NULL, "can't read file %s\n", FileName);
|
2008-10-12 20:53:28 -04:00
|
|
|
|
2009-01-05 00:50:04 -05:00
|
|
|
if (fread(ReadText, 1, FileInfo.st_size, InFile) != FileInfo.st_size)
|
2009-02-01 06:31:18 -05:00
|
|
|
ProgramFail(NULL, "can't read file %s\n", FileName);
|
2008-10-12 20:53:28 -04:00
|
|
|
|
2009-02-01 22:27:05 -05:00
|
|
|
ReadText[FileInfo.st_size] = '\0';
|
2008-10-12 20:53:28 -04:00
|
|
|
fclose(InFile);
|
|
|
|
|
2009-02-01 06:31:18 -05:00
|
|
|
return ReadText;
|
2008-10-12 20:53:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* read and scan a file for definitions */
|
2009-02-01 06:31:18 -05:00
|
|
|
void ScanFile(const char *FileName)
|
2008-10-12 20:53:28 -04:00
|
|
|
{
|
2009-02-01 06:31:18 -05:00
|
|
|
char *SourceStr = ReadFile(FileName);
|
2009-02-01 22:27:05 -05:00
|
|
|
Parse(FileName, SourceStr, strlen(SourceStr), TRUE);
|
2009-02-01 06:31:18 -05:00
|
|
|
HeapFree(SourceStr);
|
2008-10-12 20:53:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
if (argc < 2)
|
2009-02-01 06:31:18 -05:00
|
|
|
ProgramFail(NULL, "Format: picoc <program.c> <args>...\n");
|
2008-10-12 20:53:28 -04:00
|
|
|
|
2009-02-01 06:31:18 -05:00
|
|
|
HeapInit();
|
|
|
|
StrInit();
|
2009-02-01 23:53:45 -05:00
|
|
|
LexInit();
|
2008-10-12 20:53:28 -04:00
|
|
|
ParseInit();
|
|
|
|
|
2009-02-01 06:31:18 -05:00
|
|
|
ScanFile(argv[1]);
|
2008-10-12 20:53:28 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|