|
@@ -16,9 +16,8 @@ static ByteCode* code;
|
|
|
|
|
|
static int16 line = 1;
|
|
|
|
|
|
-static StringIntMap vars;
|
|
|
static int varIndex = 0;
|
|
|
-
|
|
|
+static StringIntMap vars[2];
|
|
|
static StringIntMap functions;
|
|
|
|
|
|
static void cError(const char* format, ...) {
|
|
@@ -29,10 +28,8 @@ static void cError(const char* format, ...) {
|
|
|
}
|
|
|
|
|
|
static int cAddVar(const char* var) {
|
|
|
- int index = varIndex;
|
|
|
- if(simAdd(&vars, var, &index)) {
|
|
|
- varIndex++;
|
|
|
- }
|
|
|
+ int index = vars[varIndex].entries;
|
|
|
+ simAdd(vars + varIndex, var, &index);
|
|
|
return index;
|
|
|
}
|
|
|
|
|
@@ -205,6 +202,10 @@ static bool cLiteral() {
|
|
|
static bool cLine();
|
|
|
|
|
|
static bool cFunction() {
|
|
|
+ if(varIndex == 1) {
|
|
|
+ cError("function inside function on line %d", line);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
if(!cConsumeToken(T_LITERAL)) {
|
|
|
return false;
|
|
|
}
|
|
@@ -217,7 +218,7 @@ static bool cFunction() {
|
|
|
return false;
|
|
|
}
|
|
|
cAddOperation(OP_GOTO);
|
|
|
- int p = cReserveInt();
|
|
|
+ int gotoIndex = cReserveInt();
|
|
|
|
|
|
int functionIndex = code->length;
|
|
|
if(!simAdd(&functions, name, &functionIndex)) {
|
|
@@ -225,6 +226,11 @@ static bool cFunction() {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
+ varIndex = 1;
|
|
|
+ vars[1].entries = 0;
|
|
|
+ cAddOperation(OP_PUSH);
|
|
|
+ int pushIndex = cReserveInt();
|
|
|
+
|
|
|
int oldLine = line;
|
|
|
while(!cConsumeTokenIf(T_CLOSE_CURVED_BRACKET)) {
|
|
|
if(cConsumeTokenIf(T_END)) {
|
|
@@ -233,8 +239,14 @@ static bool cFunction() {
|
|
|
}
|
|
|
cLine();
|
|
|
}
|
|
|
+
|
|
|
+ varIndex = 0;
|
|
|
+ cAddOperation(OP_POP);
|
|
|
+ cAddInt(vars[1].entries);
|
|
|
+ cSetInt(pushIndex, vars[1].entries);
|
|
|
+
|
|
|
cAddOperation(OP_RETURN);
|
|
|
- cSetInt(p, code->length);
|
|
|
+ cSetInt(gotoIndex, code->length);
|
|
|
return true;
|
|
|
}
|
|
|
|
|
@@ -263,22 +275,23 @@ static bool cLine() {
|
|
|
}
|
|
|
|
|
|
static void cForEachLine() {
|
|
|
- varIndex = 0;
|
|
|
cAddOperation(OP_PUSH);
|
|
|
int globalVars = cReserveInt();
|
|
|
while(cLine()) {
|
|
|
}
|
|
|
cAddOperation(OP_POP);
|
|
|
- cSetInt(globalVars, varIndex);
|
|
|
- cAddInt(varIndex);
|
|
|
+ cSetInt(globalVars, vars[varIndex].entries);
|
|
|
+ cAddInt(vars[varIndex].entries);
|
|
|
}
|
|
|
|
|
|
static void cAllocAndCompile() {
|
|
|
- simInit(&vars);
|
|
|
+ simInit(vars);
|
|
|
+ simInit(vars + 1);
|
|
|
simInit(&functions);
|
|
|
cForEachLine();
|
|
|
simDelete(&functions);
|
|
|
- simDelete(&vars);
|
|
|
+ simDelete(vars + 1);
|
|
|
+ simDelete(vars);
|
|
|
}
|
|
|
|
|
|
ByteCode* cCompile() {
|