Commits

Anonymous committed 0ffaef7

Add operator map, to dump operators better. Add comparison operators.

Comments (0)

Files changed (7)

     "Name",
     "Literal"
 };
+
+static const char *AR_ASTOPMAP[] = {
+    "AND",
+    "OR",
+    "EQ",
+    "NEQ",
+    "GT",
+    "GTE",
+    "LT",
+    "LTE",
+    "ADD",
+    "SUB",
+    "MUL",
+    "DIV",
+    "MOD",
+    "POW",
+    "POS",
+    "NEG",
+    "NOT"
+};
                 AR_AS_CHARP(AR_REPR(((ArExprNode *)node)->v.Literal.value)));
     } else if (AR_AST_IS(node, Name)) {
         fprintf(info->fp, "%s\n", ((ArExprNode *)node)->v.Name.ident);
+    } else if (AR_AST_IS(node, BinaryOp)) {
+        fprintf(info->fp, "binary %s\n",
+                AR_ASTOPMAP[((ArExprNode *)node)->v.BinaryOp.op]);
+    } else if (AR_AST_IS(node, UnaryOp)) {
+        fprintf(info->fp, "unary %s\n",
+                AR_ASTOPMAP[((ArExprNode *)node)->v.UnaryOp.op]);
     } else if (AR_AST_IS(node, Compound) &&
                ((ArStmtNode *)node)->v.Compound.stmts->count == 0) {
         fprintf(info->fp, "PASS\n");
 } ArAstType;
 
 /* if you change this, adapt augops[] in compile.c! */
-typedef enum {
+typedef enum _ArAstOp {
     AR_ASTOP_AND,
     AR_ASTOP_OR,
+    AR_ASTOP_EQ,
+    AR_ASTOP_NEQ,
+    AR_ASTOP_GT,
+    AR_ASTOP_GTE,
+    AR_ASTOP_LT,
+    AR_ASTOP_LTE,
     AR_ASTOP_ADD,
     AR_ASTOP_SUB,
     AR_ASTOP_MUL,
 enum ArOpcode augops[] = {
     0,
     0,
+    0,
+    0,
+    0,
+    0,
+    0,
+    0,
     AR_OP_ADD,
     AR_OP_SUB,
     AR_OP_MUL,
     case AR_ASTOP_DIV:    OP1(DIV); break;
     case AR_ASTOP_MOD:    OP1(MOD); break;
     case AR_ASTOP_POW:    OP1(POW); break;
-    default: AR_FATAL("arana_compile_binary: unhandled node type %s\n",
-                      AR_ASTMAP[node->type]);
+    default: AR_FATAL("arana_compile_binary: unhandled operator %s\n",
+                      AR_ASTOPMAP[node->v.BinaryOp.op]);
     }
 }
 
     case AR_ASTOP_NEG:    OP1(NEG); break;
     case AR_ASTOP_NOT:    OP1(NOT); break;
     default: AR_FATAL("arana_compile_unary: unhandled operator %s\n",
-                      AR_ASTMAP[node->type]);
+                      AR_ASTOPMAP[node->v.UnaryOp.op]);
     }
 }
 
 expr(A) ::= expr(B) MOD expr(C).   { A = AR_AST(BinaryOp, B, C, AR_ASTOP_MOD); }
 expr(A) ::= expr(B) POW expr(C).   { A = AR_AST(BinaryOp, B, C, AR_ASTOP_POW); }
 
+expr(A) ::= expr(B) EQ  expr(C).    { A = AR_AST(BinaryOp, B, C, AR_ASTOP_EQ); }
+expr(A) ::= expr(B) NEQ expr(C).   { A = AR_AST(BinaryOp, B, C, AR_ASTOP_NEQ); }
+expr(A) ::= expr(B) GT  expr(C).    { A = AR_AST(BinaryOp, B, C, AR_ASTOP_GT); }
+expr(A) ::= expr(B) GTE expr(C).   { A = AR_AST(BinaryOp, B, C, AR_ASTOP_GTE); }
+expr(A) ::= expr(B) LT  expr(C).    { A = AR_AST(BinaryOp, B, C, AR_ASTOP_LT); }
+expr(A) ::= expr(B) LTE expr(C).   { A = AR_AST(BinaryOp, B, C, AR_ASTOP_LTE); }
+
 // attribute access (XXX is that good?)
 expr(A) ::= expr(B) DOT NAME(C).
                   { A = AR_AST(Attribute, B, AR_AST(Literal, C), AR_CTX_Load); }
 
     arana_ast_set_filename(AR_ISIG, ast, "<stdin>");
     arana_ast_optimize(AR_ISIG, ast);
+    arana_ast_dump(AR_ISIG, ast, stdout);
     ArCode *codeobj = AR_COMPILE(ast);
     AR_DEBUG_CODE(codeobj);
     ArFrame *frame = AR_FRAME(codeobj);

tools/gen-astmap.py

 os.chdir(os.path.join(os.path.dirname(__file__), os.path.pardir, "arana"))
 
 astmap = {}
+astopmap = {}
 i = 0
 
 f = file("ast.h")
         if len(pieces) == 1 and pieces[0].startswith("AR_AST_"):
             astmap[i] = pieces[0][7:]
             i += 1
+i = 0
+for line in f:
+    line = line.strip()
+    if line == 'typedef enum _ArAstOp {':
+        in_block = True
+    elif in_block:
+        if line == '} ArAstOp;':
+            break
+        if line.strip().startswith('/*'):
+            continue
+        pieces = map(str.strip, line.rstrip(',').split("=", 1))
+        if len(pieces) == 1 and pieces[0].startswith("AR_ASTOP_"):
+            astopmap[i] = pieces[0][9:]
+            i += 1
 
 f = file("_astmap.h", "w")
 f.write("/* automatically generated */\n")
         f.write(",\n")
     asttype = astmap.get(astid)
     f.write("    \"" + asttype + "\"")
+f.write("\n};\n\n");
+f.write("static const char *AR_ASTOPMAP[] = {\n");
+for astopid in sorted(astopmap):
+    if astopid:
+        f.write(",\n")
+    astop = astopmap.get(astopid)
+    f.write("    \"" + astop + "\"")
 f.write("\n};\n");
 f.close()