2009-02-01 06:31:18 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2008-10-13 06:53:25 -04:00
|
|
|
#include <ctype.h>
|
2008-12-26 21:25:49 -05:00
|
|
|
#include <math.h>
|
2009-02-01 06:31:18 -05:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
2008-10-13 06:53:25 -04:00
|
|
|
|
2008-10-12 20:53:28 -04:00
|
|
|
#include "picoc.h"
|
|
|
|
|
2008-10-13 06:53:25 -04:00
|
|
|
|
2009-01-03 23:08:49 -05:00
|
|
|
#define isCidstart(c) (isalpha(c) || (c)=='_' || (c)=='#')
|
2008-10-14 07:46:42 -04:00
|
|
|
#define isCident(c) (isalnum(c) || (c)=='_')
|
2008-10-13 06:53:25 -04:00
|
|
|
|
2009-01-03 23:08:49 -05:00
|
|
|
#define NEXTIS(c,x,y) { if (NextChar == (c)) { Lexer->Pos++; return (x); } else return (y); }
|
|
|
|
#define NEXTIS3(c,x,d,y,z) { if (NextChar == (c)) { Lexer->Pos++; return (x); } else NEXTIS(d,y,z) }
|
|
|
|
#define NEXTIS4(c,x,d,y,e,z,a) { if (NextChar == (c)) { Lexer->Pos++; return (x); } else NEXTIS3(d,y,e,z,a) }
|
2008-10-12 20:53:28 -04:00
|
|
|
|
2009-01-26 03:57:32 -05:00
|
|
|
static union AnyValue LexAnyValue;
|
|
|
|
static struct Value LexValue = { TypeVoid, &LexAnyValue, FALSE, FALSE };
|
2008-10-13 06:53:25 -04:00
|
|
|
|
2008-10-12 20:53:28 -04:00
|
|
|
struct ReservedWord
|
|
|
|
{
|
|
|
|
const char *Word;
|
|
|
|
enum LexToken Token;
|
|
|
|
};
|
|
|
|
|
2008-10-13 06:53:25 -04:00
|
|
|
static struct ReservedWord ReservedWords[] =
|
2008-10-12 20:53:28 -04:00
|
|
|
{
|
2009-01-03 23:08:49 -05:00
|
|
|
{ "#define", TokenHashDefine },
|
|
|
|
{ "#include", TokenHashInclude },
|
2008-10-16 03:04:23 -04:00
|
|
|
{ "break", TokenBreak },
|
|
|
|
{ "case", TokenCase },
|
2008-10-12 20:53:28 -04:00
|
|
|
{ "char", TokenCharType },
|
2008-10-16 03:04:23 -04:00
|
|
|
{ "default", TokenDefault },
|
2008-10-12 20:53:28 -04:00
|
|
|
{ "do", TokenDo },
|
2008-12-26 21:25:49 -05:00
|
|
|
{ "double", TokenDoubleType },
|
2008-10-12 20:53:28 -04:00
|
|
|
{ "else", TokenElse },
|
2009-01-23 06:34:12 -05:00
|
|
|
{ "enum", TokenEnumType },
|
2008-12-26 21:25:49 -05:00
|
|
|
{ "float", TokenFloatType },
|
2008-10-12 20:53:28 -04:00
|
|
|
{ "for", TokenFor },
|
|
|
|
{ "if", TokenIf },
|
|
|
|
{ "int", TokenIntType },
|
2009-01-23 06:34:12 -05:00
|
|
|
{ "long", TokenLongType },
|
2008-10-16 03:04:23 -04:00
|
|
|
{ "return", TokenReturn },
|
2009-01-23 06:34:12 -05:00
|
|
|
{ "signed", TokenSignedType },
|
|
|
|
{ "short", TokenShortType },
|
|
|
|
{ "struct", TokenStructType },
|
2008-10-16 03:04:23 -04:00
|
|
|
{ "switch", TokenSwitch },
|
2009-01-23 06:34:12 -05:00
|
|
|
{ "typedef", TokenTypedef },
|
|
|
|
{ "union", TokenUnionType },
|
|
|
|
{ "unsigned", TokenUnsignedType },
|
2008-10-16 03:04:23 -04:00
|
|
|
{ "void", TokenVoidType },
|
|
|
|
{ "while", TokenWhile }
|
2008-10-12 20:53:28 -04:00
|
|
|
};
|
|
|
|
|
2009-02-01 06:31:18 -05:00
|
|
|
struct LexState
|
2008-10-12 20:53:28 -04:00
|
|
|
{
|
2009-02-01 06:31:18 -05:00
|
|
|
const char *Pos;
|
|
|
|
const char *End;
|
|
|
|
int Line;
|
|
|
|
const char *FileName;
|
|
|
|
};
|
|
|
|
|
|
|
|
void LexInit(struct ParseState *Parser, const char *Source, int SourceLen, const char *FileName, int Line)
|
|
|
|
{
|
|
|
|
Parser->Pos = Source;
|
|
|
|
Parser->End = Source + SourceLen;
|
|
|
|
Parser->Line = Line;
|
|
|
|
Parser->FileName = FileName;
|
2008-10-12 20:53:28 -04:00
|
|
|
}
|
|
|
|
|
2009-02-01 06:31:18 -05:00
|
|
|
void LexFail(struct LexState *Lexer, const char *Message, ...)
|
|
|
|
{
|
|
|
|
va_list Args;
|
|
|
|
|
|
|
|
printf("%s:%d: ", Lexer->FileName, Lexer->Line);
|
|
|
|
va_start(Args, Message);
|
|
|
|
vprintf(Message, Args);
|
|
|
|
printf("\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
enum LexToken LexCheckReservedWord(const char *Word)
|
2008-10-12 20:53:28 -04:00
|
|
|
{
|
|
|
|
int Count;
|
|
|
|
|
|
|
|
for (Count = 0; Count < sizeof(ReservedWords) / sizeof(struct ReservedWord); Count++)
|
|
|
|
{
|
2009-02-01 06:31:18 -05:00
|
|
|
if (strcmp(Word, ReservedWords[Count].Word) == 0)
|
2008-10-13 06:53:25 -04:00
|
|
|
return ReservedWords[Count].Token;
|
2008-10-12 20:53:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return TokenNone;
|
|
|
|
}
|
|
|
|
|
2009-02-01 06:31:18 -05:00
|
|
|
enum LexToken LexGetNumber(struct LexState *Lexer, struct Value *Value)
|
2008-10-12 20:53:28 -04:00
|
|
|
{
|
2008-10-14 22:09:47 -04:00
|
|
|
int Result = 0;
|
2008-12-26 21:25:49 -05:00
|
|
|
double FPResult;
|
|
|
|
double FPDiv;
|
2008-10-14 22:09:47 -04:00
|
|
|
|
2008-12-26 21:25:49 -05:00
|
|
|
for (; Lexer->Pos != Lexer->End && isdigit(*Lexer->Pos); Lexer->Pos++)
|
|
|
|
Result = Result * 10 + (*Lexer->Pos - '0');
|
|
|
|
|
2009-02-01 06:31:18 -05:00
|
|
|
Value->Typ = &IntType;
|
|
|
|
Value->Val->Integer = Result;
|
2008-12-26 21:25:49 -05:00
|
|
|
if (Lexer->Pos == Lexer->End || *Lexer->Pos != '.')
|
|
|
|
return TokenIntegerConstant;
|
|
|
|
|
2009-02-01 06:31:18 -05:00
|
|
|
Value->Typ = &FPType;
|
2008-12-26 21:25:49 -05:00
|
|
|
Lexer->Pos++;
|
|
|
|
for (FPDiv = 0.1, FPResult = (double)Result; Lexer->Pos != Lexer->End && isdigit(*Lexer->Pos); Lexer->Pos++, FPDiv /= 10.0)
|
|
|
|
FPResult += (*Lexer->Pos - '0') * FPDiv;
|
|
|
|
|
|
|
|
if (Lexer->Pos != Lexer->End && (*Lexer->Pos == 'e' || *Lexer->Pos == 'E'))
|
2008-12-20 06:46:21 -05:00
|
|
|
{
|
|
|
|
Lexer->Pos++;
|
2008-12-26 21:25:49 -05:00
|
|
|
for (Result = 0; Lexer->Pos != Lexer->End && isdigit(*Lexer->Pos); Lexer->Pos++)
|
|
|
|
Result = Result * 10 + (*Lexer->Pos - '0');
|
|
|
|
|
|
|
|
FPResult *= pow(10.0, (double)Result);
|
2008-12-20 06:46:21 -05:00
|
|
|
}
|
2008-12-26 21:25:49 -05:00
|
|
|
|
|
|
|
return TokenFPConstant;
|
2008-10-12 20:53:28 -04:00
|
|
|
}
|
|
|
|
|
2009-02-01 06:31:18 -05:00
|
|
|
enum LexToken LexGetWord(struct LexState *Lexer, struct Value *Value)
|
2008-10-12 20:53:28 -04:00
|
|
|
{
|
2008-10-14 07:46:42 -04:00
|
|
|
const char *Pos = Lexer->Pos + 1;
|
|
|
|
enum LexToken Token;
|
|
|
|
|
|
|
|
while (Lexer->Pos != Lexer->End && isCident(*Pos))
|
|
|
|
Pos++;
|
|
|
|
|
2009-02-01 06:31:18 -05:00
|
|
|
Value->Typ = &StringType;
|
|
|
|
Value->Val->String = (char *)StrRegister2(Lexer->Pos, Pos - Lexer->Pos);
|
2008-10-14 07:46:42 -04:00
|
|
|
Lexer->Pos = Pos;
|
|
|
|
|
2009-02-01 06:31:18 -05:00
|
|
|
Token = LexCheckReservedWord(Value->Val->String);
|
2008-10-14 07:46:42 -04:00
|
|
|
if (Token != TokenNone)
|
|
|
|
return Token;
|
|
|
|
|
2008-10-13 06:53:25 -04:00
|
|
|
return TokenIdentifier;
|
|
|
|
}
|
|
|
|
|
2009-02-01 06:31:18 -05:00
|
|
|
enum LexToken LexGetStringConstant(struct LexState *Lexer, struct Value *Value)
|
2008-10-13 06:53:25 -04:00
|
|
|
{
|
2008-10-14 22:09:47 -04:00
|
|
|
int Escape = FALSE;
|
2009-02-01 06:31:18 -05:00
|
|
|
const char *StartPos = Lexer->Pos;
|
2008-10-14 22:09:47 -04:00
|
|
|
|
2009-02-01 06:31:18 -05:00
|
|
|
// XXX - do escaping here
|
|
|
|
Value->Typ = &StringType;
|
2008-12-18 23:24:55 -05:00
|
|
|
while (Lexer->Pos != Lexer->End && (*Lexer->Pos != '"' || Escape))
|
2008-10-14 22:09:47 -04:00
|
|
|
{
|
|
|
|
if (Escape)
|
|
|
|
Escape = FALSE;
|
|
|
|
else if (*Lexer->Pos == '\\')
|
|
|
|
Escape = TRUE;
|
2008-12-18 23:24:55 -05:00
|
|
|
|
|
|
|
Lexer->Pos++;
|
2008-10-14 22:09:47 -04:00
|
|
|
}
|
2009-02-01 06:31:18 -05:00
|
|
|
Value->Val->String = (char *)StrRegister2(StartPos, Lexer->Pos - StartPos);
|
2008-12-18 23:24:55 -05:00
|
|
|
if (*Lexer->Pos == '"')
|
2008-10-14 22:09:47 -04:00
|
|
|
Lexer->Pos++;
|
|
|
|
|
2008-10-13 06:53:25 -04:00
|
|
|
return TokenStringConstant;
|
|
|
|
}
|
|
|
|
|
2009-02-01 06:31:18 -05:00
|
|
|
enum LexToken LexGetCharacterConstant(struct LexState *Lexer, struct Value *Value)
|
2008-10-13 06:53:25 -04:00
|
|
|
{
|
2009-02-01 06:31:18 -05:00
|
|
|
Value->Typ = &IntType;
|
|
|
|
Value->Val->Integer = Lexer->Pos[1];
|
2008-10-14 07:46:42 -04:00
|
|
|
if (Lexer->Pos[2] != '\'')
|
2009-02-01 06:31:18 -05:00
|
|
|
LexFail(Lexer, "illegal character '%c'", Lexer->Pos[2]);
|
2008-10-14 07:46:42 -04:00
|
|
|
|
|
|
|
Lexer->Pos += 3;
|
2008-10-13 06:53:25 -04:00
|
|
|
return TokenCharacterConstant;
|
2008-10-12 20:53:28 -04:00
|
|
|
}
|
|
|
|
|
2009-02-01 06:31:18 -05:00
|
|
|
enum LexToken LexGetComment(struct LexState *Lexer, char NextChar, struct Value *Value)
|
2009-01-03 23:08:49 -05:00
|
|
|
{
|
|
|
|
Lexer->Pos++;
|
|
|
|
if (NextChar == '*')
|
|
|
|
{ /* conventional C comment */
|
|
|
|
while (Lexer->Pos != Lexer->End && (*(Lexer->Pos-1) != '*' || *Lexer->Pos != '/'))
|
|
|
|
Lexer->Pos++;
|
|
|
|
|
|
|
|
if (Lexer->Pos != Lexer->End)
|
|
|
|
Lexer->Pos++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ /* C++ style comment */
|
|
|
|
while (Lexer->Pos != Lexer->End && *Lexer->Pos != '\n')
|
|
|
|
Lexer->Pos++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return LexGetToken(Lexer, Value);
|
|
|
|
}
|
|
|
|
|
2009-02-01 06:31:18 -05:00
|
|
|
enum LexToken LexGetTokenToStack(struct LexState *Lexer, struct Value **Value)
|
2008-10-12 20:53:28 -04:00
|
|
|
{
|
|
|
|
char ThisChar;
|
|
|
|
char NextChar;
|
|
|
|
|
2009-02-01 06:31:18 -05:00
|
|
|
if (Lexer->Pos == Lexer->End)
|
|
|
|
{
|
|
|
|
char LineBuffer[LINEBUFFER_MAX];
|
|
|
|
if (fgets(&LineBuffer[0], LINEBUFFER_MAX, stdin) == NULL)
|
|
|
|
return TokenEOF;
|
|
|
|
}
|
|
|
|
|
2009-01-26 03:57:32 -05:00
|
|
|
*Value = &LexValue;
|
2008-10-12 20:53:28 -04:00
|
|
|
while (Lexer->Pos != Lexer->End && isspace(*Lexer->Pos))
|
2008-10-14 22:09:47 -04:00
|
|
|
{
|
|
|
|
if (*Lexer->Pos == '\n')
|
|
|
|
Lexer->Line++;
|
2009-01-03 23:08:49 -05:00
|
|
|
|
2008-10-12 20:53:28 -04:00
|
|
|
Lexer->Pos++;
|
2008-10-14 22:09:47 -04:00
|
|
|
}
|
2008-10-12 20:53:28 -04:00
|
|
|
|
|
|
|
ThisChar = *Lexer->Pos;
|
|
|
|
if (isCidstart(ThisChar))
|
2009-02-01 06:31:18 -05:00
|
|
|
return LexGetWord(Lexer, *Value);
|
2008-10-12 20:53:28 -04:00
|
|
|
|
|
|
|
if (isdigit(ThisChar))
|
2009-02-01 06:31:18 -05:00
|
|
|
return LexGetNumber(Lexer, *Value);
|
2008-10-12 20:53:28 -04:00
|
|
|
|
|
|
|
NextChar = (Lexer->Pos+1 != Lexer->End) ? *(Lexer->Pos+1) : 0;
|
2009-01-03 23:08:49 -05:00
|
|
|
Lexer->Pos++;
|
2008-10-12 20:53:28 -04:00
|
|
|
switch (ThisChar)
|
|
|
|
{
|
2009-02-01 06:31:18 -05:00
|
|
|
case '"': return LexGetStringConstant(Lexer, *Value);
|
|
|
|
case '\'': return LexGetCharacterConstant(Lexer, *Value);
|
2008-10-12 20:53:28 -04:00
|
|
|
case '(': return TokenOpenBracket;
|
|
|
|
case ')': return TokenCloseBracket;
|
2008-10-13 06:53:25 -04:00
|
|
|
case '=': NEXTIS('=', TokenEquality, TokenAssign);
|
2008-10-12 20:53:28 -04:00
|
|
|
case '+': NEXTIS3('=', TokenAddAssign, '+', TokenIncrement, TokenPlus);
|
|
|
|
case '-': NEXTIS4('=', TokenSubtractAssign, '>', TokenArrow, '-', TokenDecrement, TokenMinus);
|
|
|
|
case '*': return TokenAsterisk;
|
2009-02-01 06:31:18 -05:00
|
|
|
case '/': if (NextChar == '/' || NextChar == '*') return LexGetComment(Lexer, NextChar, *Value); else return TokenSlash;
|
2008-10-13 06:53:25 -04:00
|
|
|
case '<': NEXTIS('=', TokenLessEqual, TokenLessThan);
|
|
|
|
case '>': NEXTIS('=', TokenGreaterEqual, TokenGreaterThan);
|
|
|
|
case ';': return TokenSemicolon;
|
2008-10-12 20:53:28 -04:00
|
|
|
case '&': NEXTIS('&', TokenLogicalAnd, TokenAmpersand);
|
|
|
|
case '|': NEXTIS('|', TokenLogicalOr, TokenArithmeticOr);
|
|
|
|
case '{': return TokenLeftBrace;
|
|
|
|
case '}': return TokenRightBrace;
|
2009-01-24 21:09:44 -05:00
|
|
|
case '[': return TokenLeftSquareBracket;
|
|
|
|
case ']': return TokenRightSquareBracket;
|
2008-11-21 21:56:08 -05:00
|
|
|
case '!': return TokenUnaryNot;
|
2008-10-12 20:53:28 -04:00
|
|
|
case '^': return TokenArithmeticExor;
|
|
|
|
case '~': return TokenUnaryExor;
|
|
|
|
case ',': return TokenComma;
|
|
|
|
case '.': return TokenDot;
|
|
|
|
}
|
|
|
|
|
2009-02-01 06:31:18 -05:00
|
|
|
LexFail(Lexer, "illegal character '%c'", ThisChar);
|
2008-10-12 20:53:28 -04:00
|
|
|
return TokenEOF;
|
|
|
|
}
|
|
|
|
|
2009-02-01 06:31:18 -05:00
|
|
|
void LexTokeniseToStack(struct LexState *Lexer, struct Value **Value)
|
2009-01-04 23:52:33 -05:00
|
|
|
{
|
2009-02-01 06:31:18 -05:00
|
|
|
XXX - finish this
|
|
|
|
}
|
|
|
|
|
|
|
|
enum LexToken LexGetToken(struct ParseState *Parser, struct Value **Value, int IncPos)
|
|
|
|
{
|
|
|
|
enum LexToken;
|
|
|
|
|
|
|
|
while (Parser->Pos != Parser->End && (enum LexToken)*(unsigned char *)Parser->Pos == TokenEndOfLine)
|
|
|
|
{ /* skip leading newlines */
|
|
|
|
Pos->Line++;
|
|
|
|
Pos++;
|
|
|
|
}
|
2009-01-04 23:52:33 -05:00
|
|
|
|
2009-02-01 06:31:18 -05:00
|
|
|
if (Parser->Pos == Parser->End)
|
|
|
|
return TokenEOF;
|
|
|
|
|
|
|
|
LexToken = (enum LexToken)*(unsigned char *)Parser->Pos;
|
|
|
|
if (LexToken >= TokenIdentifier && LexToken <= TokenCharacterConstant)
|
|
|
|
{ /* this token requires a value */
|
|
|
|
int ValueLen = sizeof(struct Value) + ((struct Value *)Parser->Pos)->Typ->Sizeof;
|
|
|
|
if (Value != NULL)
|
|
|
|
{ /* copy the value out (aligns it in the process) */
|
|
|
|
memcpy(LexValue, (struct Value *)Parser->Pos, ValueLen);
|
|
|
|
*Value = &LexValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IncPos)
|
|
|
|
Parser->Pos += ValueLen + 1;
|
2009-01-04 23:52:33 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-02-01 06:31:18 -05:00
|
|
|
if (IncPos)
|
|
|
|
Parser->Pos++;
|
2009-01-04 23:52:33 -05:00
|
|
|
}
|
|
|
|
|
2009-02-01 06:31:18 -05:00
|
|
|
return LexToken;
|
2008-12-22 06:52:31 -05:00
|
|
|
}
|
|
|
|
|