Browse Source

current code line in vm is now an instruction

Kajetan Johannes Hammerle 4 years ago
parent
commit
2f4f5cc112
3 changed files with 16 additions and 5 deletions
  1. 5 2
      Compiler.c
  2. 2 1
      Operation.h
  3. 9 2
      Script.c

+ 5 - 2
Compiler.c

@@ -39,7 +39,7 @@ static bool cAddBytes(const void* data, int length) {
 
 
 static bool cAddOperation(Operation token) {
 static bool cAddOperation(Operation token) {
     unsigned char c = token;
     unsigned char c = token;
-    return cAddBytes(&c, 1) && cAddBytes(&line, sizeof(line));
+    return cAddBytes(&c, 1);
 }
 }
 
 
 static Token tReadTokenAndLine() {
 static Token tReadTokenAndLine() {
@@ -125,7 +125,10 @@ static bool cLine() {
     Token t = tReadTokenAndLine();
     Token t = tReadTokenAndLine();
     if(t == T_END) {
     if(t == T_END) {
         return false;
         return false;
-    } else if(t == T_PRINT) {
+    } else if(!cAddOperation(OP_LINE) || !cAddBytes(&line, sizeof(line))) {
+        return false;
+    }
+    if(t == T_PRINT) {
         return cPrint();
         return cPrint();
     }
     }
     cUnexpectedToken(t);
     cUnexpectedToken(t);

+ 2 - 1
Operation.h

@@ -10,7 +10,8 @@ typedef enum Operation {
     OP_PUSH_FALSE,
     OP_PUSH_FALSE,
     OP_ADD,
     OP_ADD,
     OP_MUL,
     OP_MUL,
-    OP_PRINT
+    OP_PRINT,
+    OP_LINE
 } Operation;
 } Operation;
 
 
 #endif
 #endif

+ 9 - 2
Script.c

@@ -37,10 +37,10 @@ static Operation sReadOperation(Script* sc) {
     unsigned char c;
     unsigned char c;
     if(sRead(sc, &c, 1)) {
     if(sRead(sc, &c, 1)) {
         return OP_NOTHING;
         return OP_NOTHING;
-    } else if(sRead(sc, &sc->line, 2)) {
+    } /*else if(sRead(sc, &sc->line, 2)) {
         sError(sc, "operation without line near line %d", sc->line);
         sError(sc, "operation without line near line %d", sc->line);
         return OP_NOTHING;
         return OP_NOTHING;
-    }
+    }*/
     return c;
     return c;
 }
 }
 
 
@@ -152,6 +152,12 @@ static void sPrint(Script* sc) {
     }
     }
 }
 }
 
 
+static void sLine(Script* sc) {
+    if(sRead(sc, &sc->line, 2)) {
+        sError(sc, "line operation without a line near line %d", sc->line);
+    }
+}
+
 static void sConsumeInstruction(Script* sc) {
 static void sConsumeInstruction(Script* sc) {
     switch(sReadOperation(sc)) {
     switch(sReadOperation(sc)) {
         case OP_NOTHING: break;
         case OP_NOTHING: break;
@@ -163,6 +169,7 @@ static void sConsumeInstruction(Script* sc) {
         case OP_ADD: sIntBinary(sc, sIntAdd, sFloatAdd); break;
         case OP_ADD: sIntBinary(sc, sIntAdd, sFloatAdd); break;
         case OP_MUL: sIntBinary(sc, sIntMul, sFloatMul); break;
         case OP_MUL: sIntBinary(sc, sIntMul, sFloatMul); break;
         case OP_PRINT: sPrint(sc); break;
         case OP_PRINT: sPrint(sc); break;
+        case OP_LINE: sLine(sc); break;
     }
     }
 }
 }