Revised stack and parameter handling in intrinsic functions
git-svn-id: http://picoc.googlecode.com/svn/trunk@92 21eae674-98b7-11dd-bd71-f92a316d2d60
This commit is contained in:
parent
ebc32dc1f0
commit
957b620703
10
intrinsic.c
10
intrinsic.c
|
@ -2,24 +2,24 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "picoc.h"
|
#include "picoc.h"
|
||||||
|
|
||||||
void IntrinsicPrintInt(void)
|
void IntrinsicPrintInt(struct Value *ReturnValue, struct Value **Param)
|
||||||
{
|
{
|
||||||
printf("%d\n", TopStackFrame->Parameter[0]->Val->Integer);
|
printf("%d\n", Param[0]->Val->Integer);
|
||||||
}
|
}
|
||||||
|
|
||||||
void IntrinsicPrintf(void)
|
void IntrinsicPrintf(struct Value *ReturnValue, struct Value **Param)
|
||||||
{
|
{
|
||||||
printf("IntrinsicPrintf\n");
|
printf("IntrinsicPrintf\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
void IntrinsicSayHello(void)
|
void IntrinsicSayHello(struct Value *ReturnValue, struct Value **Param)
|
||||||
{
|
{
|
||||||
printf("Hello\n");
|
printf("Hello\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
struct IntrinsicFunction
|
struct IntrinsicFunction
|
||||||
{
|
{
|
||||||
void (*Func)(void);
|
void (*Func)(struct Value *, struct Value **);
|
||||||
const char *Prototype;
|
const char *Prototype;
|
||||||
} Intrinsics[] =
|
} Intrinsics[] =
|
||||||
{
|
{
|
||||||
|
|
20
parse.c
20
parse.c
|
@ -20,6 +20,7 @@ void ParseFunctionCall(struct ParseState *Parser, struct Value **Result, const c
|
||||||
{
|
{
|
||||||
struct Value *FuncValue;
|
struct Value *FuncValue;
|
||||||
struct Value *Param;
|
struct Value *Param;
|
||||||
|
struct Value **IntrinsicParam;
|
||||||
int ArgCount;
|
int ArgCount;
|
||||||
enum LexToken Token = LexGetToken(Parser, NULL, TRUE); /* open bracket */
|
enum LexToken Token = LexGetToken(Parser, NULL, TRUE); /* open bracket */
|
||||||
|
|
||||||
|
@ -30,8 +31,9 @@ void ParseFunctionCall(struct ParseState *Parser, struct Value **Result, const c
|
||||||
ProgramFail(Parser, "not a function - can't call");
|
ProgramFail(Parser, "not a function - can't call");
|
||||||
|
|
||||||
*Result = VariableAllocValueFromType(Parser, FuncValue->Val->FuncDef.ReturnType, FALSE);
|
*Result = VariableAllocValueFromType(Parser, FuncValue->Val->FuncDef.ReturnType, FALSE);
|
||||||
VariableStackFrameAdd(Parser, FuncValue->Val->FuncDef.Intrinsic ? FuncValue->Val->FuncDef.NumParams : 0);
|
HeapPushStackFrame();
|
||||||
TopStackFrame->ReturnValue = *Result;
|
if (FuncValue->Val->FuncDef.Intrinsic)
|
||||||
|
IntrinsicParam = HeapAllocStack(sizeof(struct Value *) * FuncValue->Val->FuncDef.NumParams);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* parse arguments */
|
/* parse arguments */
|
||||||
|
@ -48,7 +50,7 @@ void ParseFunctionCall(struct ParseState *Parser, struct Value **Result, const c
|
||||||
ProgramFail(Parser, "parameter %d to %s() is the wrong type", ArgCount, FuncName);
|
ProgramFail(Parser, "parameter %d to %s() is the wrong type", ArgCount, FuncName);
|
||||||
|
|
||||||
if (FuncValue->Val->FuncDef.Intrinsic)
|
if (FuncValue->Val->FuncDef.Intrinsic)
|
||||||
TopStackFrame->Parameter[ArgCount] = Param;
|
IntrinsicParam[ArgCount] = Param;
|
||||||
else
|
else
|
||||||
VariableDefine(Parser, FuncValue->Val->FuncDef.ParamName[ArgCount], Param);
|
VariableDefine(Parser, FuncValue->Val->FuncDef.ParamName[ArgCount], Param);
|
||||||
}
|
}
|
||||||
|
@ -71,21 +73,25 @@ void ParseFunctionCall(struct ParseState *Parser, struct Value **Result, const c
|
||||||
if (ArgCount < FuncValue->Val->FuncDef.NumParams)
|
if (ArgCount < FuncValue->Val->FuncDef.NumParams)
|
||||||
ProgramFail(Parser, "not enough arguments to '%s'", FuncName);
|
ProgramFail(Parser, "not enough arguments to '%s'", FuncName);
|
||||||
|
|
||||||
TopStackFrame->NumParams = ArgCount;
|
|
||||||
if (FuncValue->Val->FuncDef.Intrinsic == NULL)
|
if (FuncValue->Val->FuncDef.Intrinsic == NULL)
|
||||||
{ /* run a user-defined function */
|
{ /* run a user-defined function */
|
||||||
struct ParseState FuncParser = FuncValue->Val->FuncDef.Body;
|
struct ParseState FuncParser = FuncValue->Val->FuncDef.Body;
|
||||||
|
VariableStackFrameAdd(Parser, FuncValue->Val->FuncDef.Intrinsic ? FuncValue->Val->FuncDef.NumParams : 0);
|
||||||
|
TopStackFrame->NumParams = ArgCount;
|
||||||
|
TopStackFrame->ReturnValue = *Result;
|
||||||
|
|
||||||
if (!ParseStatement(&FuncParser))
|
if (!ParseStatement(&FuncParser))
|
||||||
ProgramFail(&FuncParser, "function body expected");
|
ProgramFail(&FuncParser, "function body expected");
|
||||||
|
|
||||||
if (FuncValue->Val->FuncDef.ReturnType != (*Result)->Typ)
|
if (FuncValue->Val->FuncDef.ReturnType != (*Result)->Typ)
|
||||||
ProgramFail(&FuncParser, "bad type of return value");
|
ProgramFail(&FuncParser, "bad type of return value");
|
||||||
|
|
||||||
|
VariableStackFramePop(Parser);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
FuncValue->Val->FuncDef.Intrinsic();
|
FuncValue->Val->FuncDef.Intrinsic(*Result, IntrinsicParam);
|
||||||
|
|
||||||
VariableStackFramePop(Parser);
|
HeapPopStackFrame();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue