IMHO we need not the additional indirection through a CONST_SYM symbol
for immediate values.

Create a new op PUSH_IMMED for numeric constants.

Depends-on: [PATCH v3] parse "define" in the parser

---

 source/interpret.c |   33 +++++++++++++++++++++++++++++++--
 source/interpret.h |    4 ++--
 source/parse.y     |   16 ++++++----------
 3 files changed, 39 insertions(+), 14 deletions(-)

diff --quilt old/source/interpret.c new/source/interpret.c
--- old/source/interpret.c
+++ new/source/interpret.c
@@ -81,10 +81,11 @@ static void saveContext(RestartData *con
 static void restoreContext(RestartData *context);
 static int returnNoVal(void);
 static int returnVal(void);
 static int returnValOrNone(int valOnStack);
 static int pushSymVal(void);
+static int pushImmed(void);
 static int pushArgVal(void);
 static int pushArgCount(void);
 static int pushArgArray(void);
 static int pushArraySymVal(void);
 static int dupStack(void);
@@ -203,11 +204,12 @@ static int (*OpFns[N_OPS])() = {returnNo
     add, subtract, multiply, divide, modulo, negate, increment, decrement,
     gt, lt, ge, le, eq, ne, bitAnd, bitOr, and, or, not, power, concat,
     assign, callSubroutine, fetchRetVal, branch, branchTrue, branchFalse,
     branchNever, arrayRef, arrayAssign, beginArrayIter, arrayIter, inArray,
     deleteArrayElement, pushArraySymVal,
-    arrayRefAndAssignSetup, pushArgVal, pushArgCount, pushArgArray};
+    arrayRefAndAssignSetup, pushArgVal, pushArgCount, pushArgArray,
+    pushImmed};
 
 /* Stack-> symN-sym0(FP), argArray, nArgs, oldFP, retPC, argN-arg1, next, ... 
*/
 #define FP_ARG_ARRAY_CACHE_INDEX (-1)
 #define FP_ARG_COUNT_INDEX (-2)
 #define FP_OLD_FP_INDEX (-3)
@@ -1232,10 +1234,32 @@ static int pushSymVal(void)
     PUSH(symVal)
 
     return STAT_OK;
 }
 
+/*
+** push immediate onto the stack
+** Before: Prog->  [immediate], next, ...
+**         TheStack-> next, ...
+** After:  Prog->  immediate, [next], ...
+**         TheStack-> [immediate], next, ...
+*/
+static int pushImmed(void)
+{
+    int immed;
+
+    DISASM_RT(PC-1, 2);
+    STACKDUMP(0, 3);
+
+    immed = PC->value;
+    PC++;
+
+    PUSH_INT(immed);
+
+    return STAT_OK;
+}
+
 static int pushArgVal(void)
 {
     int nArgs, argNum;
 
     DISASM_RT(PC-1, 1);
@@ -2964,11 +2988,12 @@ static void disasm(Inst *inst, int nInst
         "ARRAY_DELETE",                 /* deleteArrayElement */
         "PUSH_ARRAY_SYM",               /* pushArraySymVal */
         "ARRAY_REF_ASSIGN_SETUP",       /* arrayRefAndAssignSetup */
         "PUSH_ARG",                     /* $arg[expr] */
         "PUSH_ARG_COUNT",               /* $arg[] */
-        "PUSH_ARG_ARRAY"                /* $arg */
+        "PUSH_ARG_ARRAY",               /* $arg */
+        "PUSH_IMMED",                   /* pushImmed */
     };
     int i, j;
     
     printf("\n");
     for (i = 0; i < nInstr; ++i) {
@@ -2983,10 +3008,14 @@ static void disasm(Inst *inst, int nInst
                         strncmp(sym->name, "string #", 8) == 0) {
                         dumpVal(sym->value);
                     }
                     ++i;
                 }
+                else if (j == OP_PUSH_IMMED) {
+                    printf("%d", inst[i+1].value);
+                    ++i;
+                }
                 else if (j == OP_BRANCH || j == OP_BRANCH_FALSE ||
                         j == OP_BRANCH_NEVER || j == OP_BRANCH_TRUE) {
                     printf("to=(%d) %p", inst[i+1].value,
                             &inst[i+1] + inst[i+1].value);
                     ++i;
diff --quilt old/source/interpret.h new/source/interpret.h
--- old/source/interpret.h
+++ new/source/interpret.h
@@ -41,19 +41,19 @@
 #define LOOP_STACK_SIZE 256     /* (Approx.) Number of break/continue stmts
                                    allowed per program */
 
 enum symTypes {CONST_SYM, GLOBAL_SYM, LOCAL_SYM, ARG_SYM, PROC_VALUE_SYM,
        C_FUNCTION_SYM, MACRO_FUNCTION_SYM, ACTION_ROUTINE_SYM};
-#define N_OPS 43
+#define N_OPS 44
 enum operations {OP_RETURN_NO_VAL, OP_RETURN, OP_PUSH_SYM, OP_DUP, OP_ADD,
     OP_SUB, OP_MUL, OP_DIV, OP_MOD, OP_NEGATE, OP_INCR, OP_DECR, OP_GT, OP_LT,
     OP_GE, OP_LE, OP_EQ, OP_NE, OP_BIT_AND, OP_BIT_OR, OP_AND, OP_OR, OP_NOT,
     OP_POWER, OP_CONCAT, OP_ASSIGN, OP_SUBR_CALL, OP_FETCH_RET_VAL, OP_BRANCH,
     OP_BRANCH_TRUE, OP_BRANCH_FALSE, OP_BRANCH_NEVER, OP_ARRAY_REF,
     OP_ARRAY_ASSIGN, OP_BEGIN_ARRAY_ITER, OP_ARRAY_ITER, OP_IN_ARRAY,
     OP_ARRAY_DELETE, OP_PUSH_ARRAY_SYM, OP_ARRAY_REF_ASSIGN_SETUP, OP_PUSH_ARG,
-    OP_PUSH_ARG_COUNT, OP_PUSH_ARG_ARRAY};
+    OP_PUSH_ARG_COUNT, OP_PUSH_ARG_ARRAY, OP_PUSH_IMMED};
 
 enum typeTags {NO_TAG, INT_TAG, STRING_TAG, ARRAY_TAG};
 
 enum execReturnCodes {MACRO_TIME_LIMIT, MACRO_PREEMPT, MACRO_DONE, 
MACRO_ERROR};
 
diff --quilt old/source/parse.y new/source/parse.y
--- old/source/parse.y
+++ new/source/parse.y
@@ -57,18 +57,19 @@ static int AllowDefine;
 %}
 
 %union {
     Symbol *sym;
     Inst *inst;
-    int nArgs;
+    int num;
     AccumulatorData *acc;
 }
-%token <sym> NUMBER STRING SYMBOL
+%token <sym> STRING SYMBOL
+%token <num> NUMBER
 %token DELETE ARG_LOOKUP
 %token IF WHILE ELSE FOR BREAK CONTINUE RETURN
 %token <acc> DEFINE
-%type <nArgs> arglist
+%type <num> arglist
 %type <inst> cond comastmts for while else and or arrayexpr
 %type <sym> evalsym
 
 %nonassoc IF_NO_ELSE
 %nonassoc ELSE
@@ -346,11 +347,11 @@ arraylv:    SYMBOL {
 arrayexpr:  numexpr {
                 $$ = GetPC();
             }
             ;
 numexpr:    NUMBER {
-                ADD_OP(OP_PUSH_SYM); ADD_SYM($1);
+                ADD_OP(OP_PUSH_IMMED); ADD_IMMED($1);
             }
             | STRING {
                 ADD_OP(OP_PUSH_SYM); ADD_SYM($1);
             }
             | SYMBOL {
@@ -562,17 +563,12 @@ static int yylex(void)
         return 0;
     }
 
     /* process number tokens */
     if (isdigit((unsigned char)*InPtr))  { /* number */
-        char name[28];
-        sscanf(InPtr, "%d%n", &value.val.n, &len);
-        sprintf(name, "const %d", value.val.n);
+        sscanf(InPtr, "%d%n", &yylval.num, &len);
         InPtr += len;
-        value.tag = INT_TAG;
-        if ((yylval.sym=LookupSymbol(name)) == NULL)
-            yylval.sym = InstallSymbol(name, CONST_SYM, value);
         return NUMBER;
     }
 
     /* process symbol tokens.  A special case are action
        routine names which are allowed to contain '-' despite
-- 
NEdit Develop mailing list - [email protected]
http://www.nedit.org/mailman/listinfo/develop

Reply via email to