Now compiles cleanly

git-svn-id: http://picoc.googlecode.com/svn/trunk@3 21eae674-98b7-11dd-bd71-f92a316d2d60
This commit is contained in:
zik.saleeba 2008-10-13 10:53:25 +00:00
parent 7bfc2c0be5
commit b5f685e744
4 changed files with 59 additions and 20 deletions

43
lex.c
View file

@ -1,17 +1,23 @@
#include <ctype.h>
#include "picoc.h"
#define isCidstart(c) (isalpha(c) || (c)=='_')
#define LEXINC Lexer->Pos++
#define NEXTIS(c,x,y) { if (NextChar == (c)) { LEXINC; return (x); } else return (y); }
#define NEXTIS3(c,x,d,y,z) { if (NextChar == (c)) { LEXINC; return (x); } else NEXTIS(d,y,z) }
#define NEXTIS4(c,x,d,y,e,z,a) { if (NextChar == (c)) { LEXINC; return (x); } else NEXTIS3(d,y,e,z,a) }
struct ReservedWord
{
const char *Word;
enum LexToken Token;
};
static char *ReservedWords[] =
static struct ReservedWord ReservedWords[] =
{
{ "char", TokenCharType },
{ "do", TokenDo },
@ -23,11 +29,12 @@ static char *ReservedWords[] =
{ "void", TokenVoidType }
};
void LexInit(struct LexState *Lexer, const Str *Source, int Line)
void LexInit(struct LexState *Lexer, const Str *Source, const Str *FileName, int Line)
{
Lexer->Pos = Source->Str;
Lexer->End = Source->Str + Source->Len;
Lexer->Line = Line;
Lexer->FileName = FileName;
}
@ -37,8 +44,8 @@ enum LexToken LexCheckReservedWord(const Str *Word)
for (Count = 0; Count < sizeof(ReservedWords) / sizeof(struct ReservedWord); Count++)
{
if (StrEqualC(Word, ReservedWord[Count].Word))
return ReservedWord[Count].Token;
if (StrEqualC(Word, ReservedWords[Count].Word))
return ReservedWords[Count].Token;
}
return TokenNone;
@ -47,13 +54,29 @@ enum LexToken LexCheckReservedWord(const Str *Word)
enum LexToken LexGetNumber(struct LexState *Lexer)
{
XXX
// XXX
return TokenIntegerConstant;
}
enum LexToken LexGetWord(struct LexState *Lexer)
{
XXX
// XXX
return TokenIdentifier;
}
enum LexToken LexGetStringConstant(struct LexState *Lexer)
{
// XXX
return TokenStringConstant;
}
enum LexToken LexGetCharacterConstant(struct LexState *Lexer)
{
// XXX
return TokenCharacterConstant;
}
@ -83,14 +106,14 @@ enum LexToken LexGetToken(struct LexState *Lexer)
case '\'': return LexGetCharacterConstant(Lexer);
case '(': return TokenOpenBracket;
case ')': return TokenCloseBracket;
case '=': NEXTIS('=', TokenEquality, TokenAssignment);
case '=': NEXTIS('=', TokenEquality, TokenAssign);
case '+': NEXTIS3('=', TokenAddAssign, '+', TokenIncrement, TokenPlus);
case '-': NEXTIS4('=', TokenSubtractAssign, '>', TokenArrow, '-', TokenDecrement, TokenMinus);
case '*': return TokenAsterisk;
case '/': return TokenSlash;
case '<': NEXTIS('=', TokenLessEqual, TokenLess);
case '>': NEXTIS('=', TokenGreaterEqual, TokenGreater);
case ';': return TokenSemiColon;
case '<': NEXTIS('=', TokenLessEqual, TokenLessThan);
case '>': NEXTIS('=', TokenGreaterEqual, TokenGreaterThan);
case ';': return TokenSemicolon;
case '&': NEXTIS('&', TokenLogicalAnd, TokenAmpersand);
case '|': NEXTIS('|', TokenLogicalOr, TokenArithmeticOr);
case '{': return TokenLeftBrace;

View file

@ -22,7 +22,7 @@ void ProgramError(const Str *FileName, int Line, const char *Message, ...)
{
va_list Args;
StrPrintf("%S:%d: ", FileName, Line)
StrPrintf("%S:%d: ", FileName, Line);
va_start(Args, Message);
vprintf(Message, Args);
printf("\n");

View file

@ -111,6 +111,7 @@ struct LexState
int Line;
const char *Pos;
const char *End;
const Str *FileName;
};
@ -119,7 +120,7 @@ void StrToC(char *Dest, int DestSize, const Str *Source);
void StrFromC(Str *Dest, const char *Source);
int StrEqual(const Str *Str1, const Str *Str2);
int StrEqualC(const Str *Str1, const char *Str2);
void StrPrintf(const str *Format, ...);
void StrPrintf(const char *Format, ...);
/* picoc.c */
void Fail(const char *Message, ...);
@ -132,8 +133,8 @@ void TableSet(struct Table *Tbl, const Str *Key, void *Value);
void *TableLookup(struct Table *Tbl, const Str *Key);
/* lex.c */
void LexInit(const Str *Source);
enum LexToken LexGetToken(void);
void LexInit(struct LexState *Lexer, const Str *Source, const Str *FileName, int Line);
enum LexToken LexGetToken(struct LexState *Lexer);
/* parse.c */
void ParseInit(void);

27
str.c
View file

@ -1,3 +1,4 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
@ -35,21 +36,35 @@ int StrEqualC(const Str *Str1, const char *Str2)
}
void StrPrintf(const str *Format, ...)
void StrPrintf(const char *Format, ...)
{
char FormatBuf[MAX_FORMAT];
const char *FPos;
char *BPos;
va_list Args;
Str *str;
va_start(Args, Format);
for (FPos = Format, BPos = &FormatBuf[0]; *FPos != '\0'; FPos++, BPos++)
for (FPos = Format; *FPos != '\0'; FPos++)
{
if (*FPos == '%')
{
XXX
FPos++;
switch (*FPos)
{
case 'S':
str = va_arg(Args, Str *);
fwrite(str->Str, 1, str->Len, stdout);
break;
case 's': fputs(va_arg(Args, char *), stdout); break;
case 'd': printf("%d", va_arg(Args, int)); break;
case 'c': fputc(va_arg(Args, int), stdout); break;
case '%': fputc('%', stdout); break;
case '\0': FPos--; break;
}
}
*BPos = *FPos;
else
putchar(*FPos);
}
va_end(Args);
}