|
@@ -2,6 +2,7 @@
|
|
|
#include <stdio.h>
|
|
|
|
|
|
#include "Compiler.h"
|
|
|
+#include "FunctionMap.h"
|
|
|
#include "Operation.h"
|
|
|
#include "StringIntMap.h"
|
|
|
#include "Tokenizer.h"
|
|
@@ -18,7 +19,7 @@ static int16 line = 1;
|
|
|
|
|
|
static int varIndex = 0;
|
|
|
static StringIntMap vars[2];
|
|
|
-static StringIntMap functions;
|
|
|
+static FunctionMap functions;
|
|
|
|
|
|
static void cError(const char* format, ...) {
|
|
|
va_list args;
|
|
@@ -172,11 +173,6 @@ static bool cSetVar(const char* literal) {
|
|
|
}
|
|
|
|
|
|
static bool cCallFunction(const char* literal) {
|
|
|
- int index;
|
|
|
- if(!simSearch(&functions, literal, &index)) {
|
|
|
- cError("unknown function on line %d", line);
|
|
|
- return false;
|
|
|
- }
|
|
|
int arguments = 0;
|
|
|
while(!cConsumeTokenIf(T_CLOSE_BRACKET)) {
|
|
|
arguments++;
|
|
@@ -187,8 +183,13 @@ static bool cCallFunction(const char* literal) {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
+ int address = fmSearchAddress(&functions, literal, arguments);
|
|
|
+ if(address == -1) {
|
|
|
+ cError("unknown function on line %d", line);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
cAddOperation(OP_GOSUB);
|
|
|
- cAddInt(index);
|
|
|
+ cAddInt(address);
|
|
|
cAddInt(arguments);
|
|
|
return cConsumeToken(T_SEMICOLON);
|
|
|
}
|
|
@@ -255,8 +256,7 @@ static bool cFunction() {
|
|
|
cAddOperation(OP_GOTO);
|
|
|
int gotoIndex = cReserveInt();
|
|
|
|
|
|
- int functionIndex = code->length;
|
|
|
- if(!simAdd(&functions, name, &functionIndex)) {
|
|
|
+ if(!fmAdd(&functions, name, arguments, code->length)) {
|
|
|
cError("function registered twice on line %d", line);
|
|
|
return false;
|
|
|
}
|
|
@@ -325,9 +325,9 @@ static void cAllocAndCompile() {
|
|
|
varIndex = 0;
|
|
|
simInit(vars);
|
|
|
simInit(vars + 1);
|
|
|
- simInit(&functions);
|
|
|
+ fmInit(&functions);
|
|
|
cForEachLine();
|
|
|
- simDelete(&functions);
|
|
|
+ fmDelete(&functions);
|
|
|
simDelete(vars + 1);
|
|
|
simDelete(vars);
|
|
|
}
|