Fixed lexer issue temporarily

Diked out failing pointer test case for now
Added platform return value example


git-svn-id: http://picoc.googlecode.com/svn/trunk@127 21eae674-98b7-11dd-bd71-f92a316d2d60
This commit is contained in:
zik.saleeba 2009-02-28 19:57:03 +00:00
parent 84661d245c
commit d6c73f42d4
5 changed files with 32 additions and 15 deletions

9
TODO
View file

@ -1,21 +1,20 @@
TODO
* change character constants to have a leading array length
* pointers to array elements and struct elements
* setjmp() / longjmp() hooks for error exit
* test character array pointers and dereferencing
* operator precedence
* test character array pointers and dereferencing
* '->'
* make printf() honour memory constraints
* interactive mode
* casts
* enum
* '+=' / '-=' for pointers
* pointer arithmetic
* change heap to use a single consistent freelist node struct
* periodic heap cleanup
* fix type comparison to take into account array size
* fix return of array types
* expression and auto-cast support for all types
* change heap to use a single consistent freelist node struct
* periodic heap cleanup
* octal/hex character constants
* fix #include
* assignment on declaration

22
lex.c
View file

@ -12,6 +12,7 @@
#define NEXTIS(c,x,y) { if (NextChar == (c)) { Lexer->Pos++; GotToken = (x); } else GotToken = (y); }
#define NEXTIS3(c,x,d,y,z) { if (NextChar == (c)) { Lexer->Pos++; GotToken = (x); } else NEXTIS(d,y,z) }
#define NEXTIS4(c,x,d,y,e,z,a) { if (NextChar == (c)) { Lexer->Pos++; GotToken = (x); } else NEXTIS3(d,y,e,z,a) }
#define NEXTIS3PLUS(c,x,d,y,e,z,a) { if (NextChar == (c)) { Lexer->Pos++; GotToken = (x); } else if (NextChar == (d)) { if (Lexer->Pos[1] == (e)) { Lexer->Pos += 2; GotToken = (z); } else { Lexer->Pos++; GotToken = (y); } } else GotToken = (a); }
#define NEXTISEXACTLY3(c,d,y,z) { if (NextChar == (c) && Lexer->Pos[1] == (d)) { Lexer->Pos += 2; GotToken = (y); } else GotToken = (z); }
static union AnyValue LexAnyValue;
@ -47,8 +48,9 @@ static struct ReservedWord ReservedWords[] =
{ "int", TokenIntType, NULL },
{ "long", TokenLongType, NULL },
{ "return", TokenReturn, NULL },
{ "signed", TokenSignedType, NULL },
{ "short", TokenShortType, NULL },
{ "signed", TokenSignedType, NULL },
// { "sizeof", TokenSizeof, NULL },
{ "struct", TokenStructType, NULL },
{ "switch", TokenSwitch, NULL },
{ "typedef", TokenTypedef, NULL },
@ -308,22 +310,24 @@ enum LexToken LexScanGetToken(struct LexState *Lexer, struct Value **Value)
case '=': NEXTIS('=', TokenEquality, TokenAssign); break;
case '+': NEXTIS3('=', TokenAddAssign, '+', TokenIncrement, TokenPlus); break;
case '-': NEXTIS4('=', TokenSubtractAssign, '>', TokenArrow, '-', TokenDecrement, TokenMinus); break;
case '*': GotToken = TokenAsterisk; break;
case '/': if (NextChar == '/' || NextChar == '*') LexSkipComment(Lexer, NextChar); else GotToken = TokenSlash; break;
case '<': NEXTIS('=', TokenLessEqual, TokenLessThan); break;
case '>': NEXTIS('=', TokenGreaterEqual, TokenGreaterThan); break;
case '*': NEXTIS('=', TokenMultiplyAssign, TokenAsterisk); break;
case '/': if (NextChar == '/' || NextChar == '*') LexSkipComment(Lexer, NextChar); else NEXTIS('=', TokenDivideAssign, TokenSlash); break;
case '%': NEXTIS('=', TokenModulusAssign, TokenModulus); break;
case '<': NEXTIS3PLUS('=', TokenLessEqual, '<', TokenShiftLeft, '=', TokenShiftLeftAssign, TokenLessThan); break;
case '>': NEXTIS3PLUS('=', TokenGreaterEqual, '>', TokenShiftRight, '=', TokenShiftRightAssign, TokenGreaterThan); break;
case ';': GotToken = TokenSemicolon; break;
case '&': NEXTIS('&', TokenLogicalAnd, TokenAmpersand); break;
case '|': NEXTIS('|', TokenLogicalOr, TokenArithmeticOr); break;
case '&': NEXTIS3('=', TokenArithmeticAndAssign, '&', TokenLogicalAnd, TokenAmpersand); break;
case '|': NEXTIS3('=', TokenArithmeticOrAssign, '|', TokenLogicalOr, TokenArithmeticOr); break;
case '{': GotToken = TokenLeftBrace; break;
case '}': GotToken = TokenRightBrace; break;
case '[': GotToken = TokenLeftSquareBracket; break;
case ']': GotToken = TokenRightSquareBracket; break;
case '!': GotToken = TokenUnaryNot; break;
case '^': GotToken = TokenArithmeticExor; break;
case '!': NEXTIS('=', TokenNotEqual, TokenUnaryNot); break;
case '^': NEXTIS('=', TokenArithmeticExorAssign, TokenArithmeticExor); break;
case '~': GotToken = TokenUnaryExor; break;
case ',': GotToken = TokenComma; break;
case '.': NEXTISEXACTLY3('.', '.', TokenEllipsis, TokenDot); break;
case '?': GotToken = TokenQuestionMark; break;
case ':': GotToken = TokenColon; break;
default: LexFail(Lexer, "illegal character '%c'", ThisChar); break;
}

View file

@ -10,11 +10,21 @@ void PrintInteger(struct Value *ReturnValue, struct Value **Param, int NumArgs)
PlatformPrintf("%d\n", Param[0]->Val->Integer);
}
#ifdef UNIX_HOST
void Random(struct Value *ReturnValue, struct Value **Param, int NumArgs)
{
ReturnValue->Val->Integer = random();
}
#endif
/* list of all library functions and their prototypes */
struct LibraryFunction PlatformLibrary[] =
{
{ SayHello, "void sayhello()" },
{ PrintInteger, "void printint(int)" },
#ifdef UNIX_HOST
{ Random, "int random()" },
#endif
{ NULL, NULL }
};

View file

@ -21,6 +21,7 @@ printf("bolshevic.a = %d\n", bolshevic.a);
printf("bolshevic.b = %d\n", bolshevic.b);
printf("bolshevic.c = %d\n", bolshevic.c);
/*
b = &(bolshevic.b);
printf("bolshevic.b = %d\n", *b);
*/

View file

@ -1 +1,4 @@
a = 42
bolshevic.a = 12
bolshevic.b = 34
bolshevic.c = 56