diff --git a/parse.c b/parse.c index af04a16..fef6f93 100644 --- a/parse.c +++ b/parse.c @@ -92,20 +92,23 @@ void ParseParameterList(struct LexState *CallLexer, struct LexState *FuncLexer, { ParseType(FuncLexer, &Typ); Token = LexGetToken(FuncLexer, &Identifier); - if (Token != TokenIdentifier) - ProgramFail(FuncLexer, "invalid parameter"); - - if (RunIt) - { - if (Parameter[ParamCount].Typ != Typ) - ProgramFail(CallLexer, "parameter %d has the wrong type", ParamCount+1); - - VariableDefine(FuncLexer, &Identifier.String, &Parameter[ParamCount]); - } - - Token = LexGetPlainToken(FuncLexer); if (Token != TokenComma && Token != TokenCloseBracket) - ProgramFail(FuncLexer, "comma expected"); + { /* there's an identifier */ + if (Token != TokenIdentifier) + ProgramFail(FuncLexer, "invalid parameter"); + + if (RunIt) + { + if (Parameter[ParamCount].Typ != Typ) + ProgramFail(CallLexer, "parameter %d has the wrong type", ParamCount+1); + + VariableDefine(FuncLexer, &Identifier.String, &Parameter[ParamCount]); + } + + Token = LexGetPlainToken(FuncLexer); + if (Token != TokenComma && Token != TokenCloseBracket) + ProgramFail(FuncLexer, "comma expected"); + } } if (ParameterUsed == 0) Token = LexGetPlainToken(FuncLexer); @@ -580,9 +583,9 @@ int ParseStatement(struct LexState *Lexer, int RunIt) LexToEndOfLine(Lexer); break; - case TokenBreak: case TokenSwitch: case TokenCase: + case TokenBreak: case TokenReturn: case TokenDefault: ProgramFail(Lexer, "not implemented yet"); diff --git a/picoc.c b/picoc.c index c464085..e503c00 100644 --- a/picoc.c +++ b/picoc.c @@ -31,10 +31,11 @@ void ProgramFail(struct LexState *Lexer, const char *Message, ...) /* read a file into memory. this is the only function using malloc(). * do it differently for embedded devices without malloc */ -char *ReadFile(const Str *FileName) +Str ReadFile(const Str *FileName) { struct stat FileInfo; - char *Text; + char *ReadText; + Str Text; FILE *InFile; char CFileName[PATH_MAX]; @@ -43,15 +44,19 @@ char *ReadFile(const Str *FileName) if (stat(CFileName, &FileInfo)) Fail("can't read file %s\n", CFileName); - Text = malloc(FileInfo.st_size); - + ReadText = malloc(FileInfo.st_size); + if (ReadText == NULL) + Fail("out of memory\n"); + InFile = fopen(CFileName, "r"); if (InFile == NULL) Fail("can't read file %s\n", CFileName); - if (fread(Text, 1, FileInfo.st_size, InFile) != FileInfo.st_size) + if (fread(ReadText, 1, FileInfo.st_size, InFile) != FileInfo.st_size) Fail("can't read file %s\n", CFileName); + Text.Str = ReadText; + Text.Len = FileInfo.st_size; fclose(InFile); return Text; @@ -60,11 +65,7 @@ char *ReadFile(const Str *FileName) /* read and scan a file for definitions */ void ScanFile(const Str *FileName) { - char *Source; - Str SourceStr; - - Source = ReadFile(FileName); - StrFromC(&SourceStr, Source); + Str SourceStr = ReadFile(FileName); Parse(FileName, &SourceStr, TRUE); }