2008-10-12 20:53:28 -04:00
|
|
|
#ifndef PICOC_H
|
|
|
|
#define PICOC_H
|
|
|
|
|
2008-10-14 07:18:43 -04:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
2008-10-12 20:53:28 -04:00
|
|
|
/* configurable options */
|
|
|
|
#define USE_MALLOC
|
2008-12-20 20:36:09 -05:00
|
|
|
#define GLOBAL_TABLE_SIZE 199 /* global variable table */
|
|
|
|
#define FUNCTION_STORE_MAX 50 /* maximum number of used-defined functions */
|
2008-12-21 21:29:19 -05:00
|
|
|
#define STACK_MAX 10 /* maximum function call stack depth */
|
2008-12-23 21:30:29 -05:00
|
|
|
#define PARAMETER_MAX 10 /* maximum number of parameters to a function */
|
2008-12-21 21:29:19 -05:00
|
|
|
#define LOCAL_TABLE_SIZE 11 /* maximum number of local variables */
|
2008-12-20 20:36:09 -05:00
|
|
|
#define LARGE_INT_POWER_OF_TEN 1000000000 /* the largest power of ten which fits in an int on this architecture */
|
2008-10-12 20:53:28 -04:00
|
|
|
|
|
|
|
/* handy definitions */
|
|
|
|
#ifndef TRUE
|
|
|
|
#define TRUE 1
|
|
|
|
#define FALSE 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef NULL
|
|
|
|
#define NULL 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef min
|
|
|
|
#define min(x,y) (((x)<(y))?(x):(y))
|
|
|
|
#endif
|
|
|
|
|
2008-12-26 22:04:58 -05:00
|
|
|
#define LOG10E 0.43429448190325182765
|
|
|
|
|
2008-10-12 20:53:28 -04:00
|
|
|
#ifndef PATH_MAX
|
|
|
|
#define PATH_MAX 1024
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* lexical tokens */
|
|
|
|
enum LexToken
|
|
|
|
{
|
|
|
|
TokenNone,
|
|
|
|
TokenEOF,
|
|
|
|
TokenIdentifier,
|
|
|
|
TokenIntegerConstant,
|
2008-12-26 21:25:49 -05:00
|
|
|
TokenFPConstant,
|
2008-10-12 20:53:28 -04:00
|
|
|
TokenStringConstant,
|
|
|
|
TokenCharacterConstant,
|
|
|
|
TokenType,
|
|
|
|
TokenOpenBracket,
|
|
|
|
TokenCloseBracket,
|
|
|
|
TokenAssign,
|
|
|
|
TokenPlus,
|
|
|
|
TokenMinus,
|
|
|
|
TokenAsterisk,
|
|
|
|
TokenSlash,
|
|
|
|
TokenEquality,
|
|
|
|
TokenLessThan,
|
|
|
|
TokenGreaterThan,
|
|
|
|
TokenLessEqual,
|
|
|
|
TokenGreaterEqual,
|
|
|
|
TokenSemicolon,
|
|
|
|
TokenArrow,
|
|
|
|
TokenAmpersand,
|
|
|
|
TokenLeftBrace,
|
|
|
|
TokenRightBrace,
|
|
|
|
TokenLeftAngleBracket,
|
|
|
|
TokenRightAngleBracket,
|
|
|
|
TokenLogicalAnd,
|
|
|
|
TokenLogicalOr,
|
|
|
|
TokenArithmeticOr,
|
|
|
|
TokenArithmeticExor,
|
|
|
|
TokenUnaryExor,
|
2008-11-21 21:56:08 -05:00
|
|
|
TokenUnaryNot,
|
2008-10-12 20:53:28 -04:00
|
|
|
TokenComma,
|
|
|
|
TokenDot,
|
|
|
|
TokenAddAssign,
|
|
|
|
TokenSubtractAssign,
|
|
|
|
TokenIncrement,
|
|
|
|
TokenDecrement,
|
|
|
|
TokenIntType,
|
|
|
|
TokenCharType,
|
2008-12-26 21:25:49 -05:00
|
|
|
TokenFloatType,
|
|
|
|
TokenDoubleType,
|
2008-10-12 20:53:28 -04:00
|
|
|
TokenVoidType,
|
|
|
|
TokenDo,
|
|
|
|
TokenElse,
|
|
|
|
TokenFor,
|
|
|
|
TokenIf,
|
2008-12-18 19:22:52 -05:00
|
|
|
TokenWhile,
|
|
|
|
TokenBreak,
|
|
|
|
TokenSwitch,
|
|
|
|
TokenCase,
|
|
|
|
TokenDefault,
|
|
|
|
TokenReturn
|
2008-10-12 20:53:28 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/* string type so we can use source file strings */
|
|
|
|
typedef struct _Str
|
|
|
|
{
|
|
|
|
int Len;
|
|
|
|
const char *Str;
|
|
|
|
} Str;
|
|
|
|
|
|
|
|
/* function definition - really just where it is in the source file */
|
|
|
|
struct FuncDef
|
|
|
|
{
|
|
|
|
Str Source;
|
|
|
|
Str FileName;
|
|
|
|
int StartLine;
|
|
|
|
};
|
|
|
|
|
2008-12-18 19:22:52 -05:00
|
|
|
/* values */
|
2008-11-21 21:56:08 -05:00
|
|
|
enum ValueType
|
|
|
|
{
|
2008-12-18 19:22:52 -05:00
|
|
|
TypeVoid,
|
2008-11-21 21:56:08 -05:00
|
|
|
TypeInt,
|
2008-12-26 21:25:49 -05:00
|
|
|
TypeFP,
|
2008-12-20 06:46:21 -05:00
|
|
|
TypeString,
|
|
|
|
TypeFunction
|
2008-11-21 21:56:08 -05:00
|
|
|
};
|
|
|
|
|
2008-10-14 07:46:42 -04:00
|
|
|
union AnyValue
|
|
|
|
{
|
|
|
|
int Integer;
|
2008-12-26 21:25:49 -05:00
|
|
|
double FP;
|
2008-10-14 07:46:42 -04:00
|
|
|
Str String;
|
|
|
|
};
|
2008-10-12 20:53:28 -04:00
|
|
|
|
2008-11-21 21:56:08 -05:00
|
|
|
struct Value
|
|
|
|
{
|
|
|
|
enum ValueType Typ;
|
|
|
|
union AnyValue Val;
|
|
|
|
};
|
|
|
|
|
2008-12-18 19:22:52 -05:00
|
|
|
/* hash table data structure */
|
|
|
|
struct TableEntry
|
|
|
|
{
|
|
|
|
Str Key;
|
|
|
|
struct Value Val;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Table
|
|
|
|
{
|
|
|
|
short Size;
|
|
|
|
struct TableEntry *HashTable;
|
|
|
|
};
|
|
|
|
|
2008-10-12 20:53:28 -04:00
|
|
|
/* lexer state - so we can lex nested files */
|
|
|
|
struct LexState
|
|
|
|
{
|
|
|
|
int Line;
|
|
|
|
const char *Pos;
|
|
|
|
const char *End;
|
2008-10-13 06:53:25 -04:00
|
|
|
const Str *FileName;
|
2008-10-12 20:53:28 -04:00
|
|
|
};
|
|
|
|
|
2008-12-21 21:29:19 -05:00
|
|
|
/* stack frame for function calls */
|
|
|
|
struct StackFrame
|
|
|
|
{
|
|
|
|
struct LexState ReturnLex; /* how we got here */
|
|
|
|
struct Table LocalTable; /* the local variables and parameters */
|
|
|
|
struct TableEntry LocalHashTable[LOCAL_TABLE_SIZE];
|
|
|
|
};
|
|
|
|
|
2008-10-12 20:53:28 -04:00
|
|
|
|
|
|
|
/* str.c */
|
|
|
|
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);
|
2008-10-13 06:53:25 -04:00
|
|
|
void StrPrintf(const char *Format, ...);
|
2008-10-14 07:18:43 -04:00
|
|
|
void vStrPrintf(const char *Format, va_list Args);
|
2008-10-12 20:53:28 -04:00
|
|
|
|
|
|
|
/* picoc.c */
|
|
|
|
void Fail(const char *Message, ...);
|
2008-11-21 21:56:08 -05:00
|
|
|
void ProgramFail(struct LexState *Lexer, const char *Message, ...);
|
2008-10-12 20:53:28 -04:00
|
|
|
void ScanFile(const Str *FileName);
|
|
|
|
|
|
|
|
/* table.c */
|
2008-12-21 21:29:19 -05:00
|
|
|
void TableInit(struct Table *Tbl, struct TableEntry *HashTable, int Size);
|
2008-12-20 22:13:25 -05:00
|
|
|
int TableSet(struct Table *Tbl, const Str *Key, struct Value *Val);
|
2008-12-18 19:22:52 -05:00
|
|
|
int TableGet(struct Table *Tbl, const Str *Key, struct Value **Val);
|
2008-10-12 20:53:28 -04:00
|
|
|
|
|
|
|
/* lex.c */
|
2008-10-13 06:53:25 -04:00
|
|
|
void LexInit(struct LexState *Lexer, const Str *Source, const Str *FileName, int Line);
|
2008-12-20 20:36:09 -05:00
|
|
|
enum LexToken LexGetToken(struct LexState *Lexer, union AnyValue *Value);
|
2008-12-22 06:52:31 -05:00
|
|
|
enum LexToken LexGetPlainToken(struct LexState *Lexer);
|
2008-12-20 20:36:09 -05:00
|
|
|
enum LexToken LexPeekToken(struct LexState *Lexer, union AnyValue *Value);
|
2008-12-22 06:52:31 -05:00
|
|
|
enum LexToken LexPeekPlainToken(struct LexState *Lexer);
|
2008-10-12 20:53:28 -04:00
|
|
|
|
|
|
|
/* parse.c */
|
|
|
|
void ParseInit(void);
|
2008-12-20 05:24:34 -05:00
|
|
|
void Parse(const Str *FileName, const Str *Source, int RunIt);
|
2008-10-12 20:53:28 -04:00
|
|
|
|
|
|
|
#endif /* PICOC_H */
|
|
|
|
|