Converting to use shared strings
git-svn-id: http://picoc.googlecode.com/svn/trunk@45 21eae674-98b7-11dd-bd71-f92a316d2d60
This commit is contained in:
parent
a9a93f3aa4
commit
8bd1c18d24
40
intrinsic.c
40
intrinsic.c
|
@ -2,12 +2,6 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "picoc.h"
|
#include "picoc.h"
|
||||||
|
|
||||||
#define NUM_INTRINSICS 3
|
|
||||||
|
|
||||||
Str IntrinsicFilename = { 9, "intrinsic" };
|
|
||||||
struct Value IntrinsicValue[NUM_INTRINSICS];
|
|
||||||
int IntrinsicReferenceNo[NUM_INTRINSICS];
|
|
||||||
|
|
||||||
void IntrinsicPrintInt(void)
|
void IntrinsicPrintInt(void)
|
||||||
{
|
{
|
||||||
printf("%d\n", Parameter[0]->Val->Integer);
|
printf("%d\n", Parameter[0]->Val->Integer);
|
||||||
|
@ -36,35 +30,17 @@ struct IntrinsicFunction
|
||||||
|
|
||||||
void IntrinsicInit(struct Table *GlobalTable)
|
void IntrinsicInit(struct Table *GlobalTable)
|
||||||
{
|
{
|
||||||
struct LexState Lexer;
|
struct ParseState Parser;
|
||||||
Str Source;
|
|
||||||
int Count;
|
int Count;
|
||||||
Str Identifier;
|
const char *Identifier;
|
||||||
struct ValueType *Typ;
|
struct ValueType *ReturnType;
|
||||||
|
struct Value *NewValue;
|
||||||
|
|
||||||
for (Count = 0; Count < sizeof(Intrinsics) / sizeof(struct IntrinsicFunction); Count++)
|
for (Count = 0; Count < sizeof(Intrinsics) / sizeof(struct IntrinsicFunction); Count++)
|
||||||
{
|
{
|
||||||
LexInit(&Lexer, Intrinsics[Count].Prototype, strlen(Source.Str), &IntrinsicFilename, Count+1);
|
LexInit(&Parser, Intrinsics[Count].Prototype, strlen(Intrinsics[Count].Prototype), "", Count+1);
|
||||||
TypeParse(&Lexer, &Typ, &Identifier);
|
TypeParse(&Parser, &ReturnType, &Identifier);
|
||||||
IntrinsicReferenceNo[Count] = -1 - Count;
|
NewValue = ParseFunctionDefinition(&Parser, ReturnType, Identifier, TRUE);
|
||||||
IntrinsicValue[Count].Typ = &FunctionType;
|
NewValue->Val->FuncDef.Intrinsic = Intrinsics[Count].Func;
|
||||||
IntrinsicValue[Count].Val = (union AnyValue *)&IntrinsicReferenceNo[Count];
|
|
||||||
IntrinsicValue[Count].ValOnHeap = FALSE;
|
|
||||||
IntrinsicValue[Count].ValOnStack = FALSE;
|
|
||||||
TableSet(GlobalTable, &Identifier, &IntrinsicValue[Count]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void IntrinsicGetLexer(struct LexState *Lexer, int IntrinsicId)
|
|
||||||
{
|
|
||||||
Lexer->Line = -IntrinsicId;
|
|
||||||
Lexer->Pos = Intrinsics[-1-IntrinsicId].Prototype;
|
|
||||||
Lexer->End = Lexer->Pos + strlen(Lexer->Pos);
|
|
||||||
Lexer->FileName = &IntrinsicFilename;
|
|
||||||
}
|
|
||||||
|
|
||||||
void IntrinsicCall(struct LexState *Lexer, struct Value *Result, struct ValueType *ReturnType, int IntrinsicId)
|
|
||||||
{
|
|
||||||
Intrinsics[-1-IntrinsicId].Func();
|
|
||||||
Result->Typ = &VoidType;
|
|
||||||
}
|
|
||||||
|
|
9
parse.c
9
parse.c
|
@ -360,7 +360,7 @@ int ParseIntExpression(struct ParseState *Parser, int RunIt)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* parse a function definition and store it for later */
|
/* parse a function definition and store it for later */
|
||||||
void ParseFunctionDefinition(struct ParseState *Parser, struct ValueType *ReturnType, Str *Identifier)
|
struct Value *ParseFunctionDefinition(struct ParseState *Parser, struct ValueType *ReturnType, const char *Identifier, int IsProtoType)
|
||||||
{
|
{
|
||||||
struct ValueType *ParamTyp;
|
struct ValueType *ParamTyp;
|
||||||
Str Identifier;
|
Str Identifier;
|
||||||
|
@ -401,6 +401,8 @@ void ParseFunctionDefinition(struct ParseState *Parser, struct ValueType *Return
|
||||||
ProgramFail(ParamParser, "comma expected");
|
ProgramFail(ParamParser, "comma expected");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!IsPrototype)
|
||||||
|
{
|
||||||
if (LexGetToken(Parser, NULL, FALSE) != TokenLeftBrace)
|
if (LexGetToken(Parser, NULL, FALSE) != TokenLeftBrace)
|
||||||
ProgramFail(Parser, "bad function definition");
|
ProgramFail(Parser, "bad function definition");
|
||||||
|
|
||||||
|
@ -408,9 +410,12 @@ void ParseFunctionDefinition(struct ParseState *Parser, struct ValueType *Return
|
||||||
ProgramFail(Parser, "function definition expected");
|
ProgramFail(Parser, "function definition expected");
|
||||||
|
|
||||||
FuncValue->Val->FuncDef.Body.End = Parser->Pos;
|
FuncValue->Val->FuncDef.Body.End = Parser->Pos;
|
||||||
|
}
|
||||||
|
|
||||||
if (!TableSet(&GlobalTable, Identifier, FuncValue))
|
if (!TableSet(&GlobalTable, Identifier, FuncValue))
|
||||||
ProgramFail(Parser, "'%S' is already defined", Identifier);
|
ProgramFail(Parser, "'%S' is already defined", Identifier);
|
||||||
|
|
||||||
|
return FuncValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* parse a #define macro definition and store it for later */
|
/* parse a #define macro definition and store it for later */
|
||||||
|
@ -572,7 +577,7 @@ int ParseStatement(struct ParseState *Parser, int RunIt)
|
||||||
|
|
||||||
/* handle function definitions */
|
/* handle function definitions */
|
||||||
if (LexGetToken(Parser, NULL, FALSE) == TokenOpenBracket)
|
if (LexGetToken(Parser, NULL, FALSE) == TokenOpenBracket)
|
||||||
ParseFunctionDefinition(Parser, &Typ, &Identifier);
|
ParseFunctionDefinition(Parser, &Typ, &Identifier, FALSE);
|
||||||
else
|
else
|
||||||
VariableDefine(Parser, &Identifier, VariableAllocValueFromType(Parser, Typ, FALSE));
|
VariableDefine(Parser, &Identifier, VariableAllocValueFromType(Parser, Typ, FALSE));
|
||||||
break;
|
break;
|
||||||
|
|
6
picoc.c
6
picoc.c
|
@ -1,5 +1,6 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
@ -32,7 +33,7 @@ char *ReadFile(const char *FileName)
|
||||||
if (stat(FileName, &FileInfo))
|
if (stat(FileName, &FileInfo))
|
||||||
ProgramFail(NULL, "can't read file %s\n", FileName);
|
ProgramFail(NULL, "can't read file %s\n", FileName);
|
||||||
|
|
||||||
ReadText = HeapAlloc(FileInfo.st_size);
|
ReadText = HeapAlloc(FileInfo.st_size + 1);
|
||||||
if (ReadText == NULL)
|
if (ReadText == NULL)
|
||||||
ProgramFail(NULL, "out of memory\n");
|
ProgramFail(NULL, "out of memory\n");
|
||||||
|
|
||||||
|
@ -43,6 +44,7 @@ char *ReadFile(const char *FileName)
|
||||||
if (fread(ReadText, 1, FileInfo.st_size, InFile) != FileInfo.st_size)
|
if (fread(ReadText, 1, FileInfo.st_size, InFile) != FileInfo.st_size)
|
||||||
ProgramFail(NULL, "can't read file %s\n", FileName);
|
ProgramFail(NULL, "can't read file %s\n", FileName);
|
||||||
|
|
||||||
|
ReadText[FileInfo.st_size] = '\0';
|
||||||
fclose(InFile);
|
fclose(InFile);
|
||||||
|
|
||||||
return ReadText;
|
return ReadText;
|
||||||
|
@ -52,7 +54,7 @@ char *ReadFile(const char *FileName)
|
||||||
void ScanFile(const char *FileName)
|
void ScanFile(const char *FileName)
|
||||||
{
|
{
|
||||||
char *SourceStr = ReadFile(FileName);
|
char *SourceStr = ReadFile(FileName);
|
||||||
Parse(FileName, SourceStr, TRUE);
|
Parse(FileName, SourceStr, strlen(SourceStr), TRUE);
|
||||||
HeapFree(SourceStr);
|
HeapFree(SourceStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
17
picoc.h
17
picoc.h
|
@ -153,9 +153,17 @@ struct Value
|
||||||
/* hash table data structure */
|
/* hash table data structure */
|
||||||
struct TableEntry
|
struct TableEntry
|
||||||
{
|
{
|
||||||
const char *Key;
|
struct TableEntry *Next; /* next item in this hash chain */
|
||||||
struct Value *Val;
|
union TableEntryPayload
|
||||||
struct TableEntry *Next;
|
{
|
||||||
|
struct ValueEntry
|
||||||
|
{
|
||||||
|
const char *Key; /* points to the shared string table */
|
||||||
|
struct Value *Val; /* the value we're storing */
|
||||||
|
} v; /* used for tables of values */
|
||||||
|
|
||||||
|
const char Key[1]; /* dummy size - used for the shared string table */
|
||||||
|
} p;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Table
|
struct Table
|
||||||
|
@ -208,6 +216,7 @@ void ParseInit(void);
|
||||||
int ParseExpression(struct ParseState *Parser, struct Value **Result, int ResultOnHeap, int RunIt);
|
int ParseExpression(struct ParseState *Parser, struct Value **Result, int ResultOnHeap, int RunIt);
|
||||||
int ParseIntExpression(struct ParseState *Parser, int RunIt);
|
int ParseIntExpression(struct ParseState *Parser, int RunIt);
|
||||||
int ParseStatement(struct ParseState *Parser, int RunIt);
|
int ParseStatement(struct ParseState *Parser, int RunIt);
|
||||||
|
struct Value *ParseFunctionDefinition(struct ParseState *Parser, struct ValueType *ReturnType, const char *Identifier, int IsProtoType);
|
||||||
void Parse(const char *FileName, const char *Source, int SourceLen, int RunIt);
|
void Parse(const char *FileName, const char *Source, int SourceLen, int RunIt);
|
||||||
|
|
||||||
/* type.c */
|
/* type.c */
|
||||||
|
@ -217,8 +226,6 @@ void TypeParse(struct ParseState *Parser, struct ValueType **Typ, const char **I
|
||||||
|
|
||||||
/* intrinsic.c */
|
/* intrinsic.c */
|
||||||
void IntrinsicInit(struct Table *GlobalTable);
|
void IntrinsicInit(struct Table *GlobalTable);
|
||||||
void IntrinsicGetLexer(struct ParseState *Parser, int IntrinsicId);
|
|
||||||
void IntrinsicCall(struct ParseState *Parser, struct Value *Result, struct ValueType *ReturnType, int IntrinsicId);
|
|
||||||
|
|
||||||
/* heap.c */
|
/* heap.c */
|
||||||
void HeapInit();
|
void HeapInit();
|
||||||
|
|
Loading…
Reference in a new issue