]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
switch() { } done right.
authorPavel Machek <pavel@ucw.cz>
Thu, 28 Oct 1999 21:03:36 +0000 (21:03 +0000)
committerPavel Machek <pavel@ucw.cz>
Thu, 28 Oct 1999 21:03:36 +0000 (21:03 +0000)
Makefile
bird.conf
conf/confbase.Y
filter/config.Y
filter/filter.c
filter/tree.c

index a2bba642cec80d62340964e157a9500016bb52d6..2221101d2b332bf02fcb1209ce5ae5116f88f581 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -4,12 +4,12 @@
 
 objdir=obj
 
-all depend:
+all depend tags:
        $(MAKE) -C $(objdir) $@
 
 clean:
        $(MAKE) -C $(objdir) clean
-       find . -name "*~" -or -name "*.[oa]" -or -name "\#*\#" -or -name TAGS -or -name core -or -name depend -or -name .#* | xargs rm -f
+       find . -name "*~" -or -name "*.[oa]" -or -name "\#*\#" -or -name TAGS -or -name core -or -name depend -or -name ".#*" | xargs rm -f
 
 distclean: clean
        rm -rf $(objdir)
index b1220c32341ac6db275191fb2ae6abb1e8eafe46..3b8f6a7beb8019adc53f5a7e703b037431cde395 100644 (file)
--- a/bird.conf
+++ b/bird.conf
@@ -14,7 +14,7 @@ function callme (int arg1; int arg2;)
 
        case arg1 {
        2: print "dva"; print "jeste jednou dva";
-       [ 3 .. 5 ]: print "tri az pet";
+       3 .. 5: print "tri az pet";
        else: print "neco jineho";
        }       
 }
@@ -40,8 +40,11 @@ prefix px;
        print "Testing prefixes: 1.2.3.4/18 = " px;
        print "Testing pairs: (1,2) = " (1,2);
 
+       print "What will this do? " [ 1, 2, 1, 1, 1, 3, 4, 1, 1, 1, 5 ];
+
        print "Testing functions...";
-       callme ( 1, 2 );
+#      callme ( 1, 2 );
+       callme ( 2, 2 );
        callme ( 2, 2 );
        callme ( 3, 2 );
        callme ( 4, 2 );
index a6eb87649ac9d3a0c1cbd9816bea72eff1db743a..b266d25391c89305f750138984217ef30fa32cbb 100644 (file)
@@ -43,7 +43,7 @@ CF_DECLS
 
 %type <i> expr bool pxlen datetime
 
-%nonassoc '=' '<' '>' '~' ELSE IF '.'
+%nonassoc '=' '<' '>' '~' IF ELSE '.'
 %left '+' '-'
 %left '*' '/' '%'
 %left '!'
index 93854836a019cc799cc6c57e821242cc5f8e86cc..d5a9dca960cc180ab0a55c634c37431bacd81d4a 100644 (file)
@@ -11,7 +11,6 @@
        FIXME: whole system of paths, path ~ string, path.prepend(), path.originate
        FIXME: create community lists
        FIXME: access to dynamic attributes
-       FIXME: make case faster
  */
 
 CF_HDR
@@ -37,10 +36,10 @@ CF_KEYWORDS(FUNCTION, PRINT, PRINTN, CONST,
        FILTER
        )
 
-%type <x> term block cmds cmd function_body ifthen constant print_one print_list var_list switch_body
+%type <x> term block cmds cmd function_body ifthen constant print_one print_list var_list
 %type <f> filter filter_body
 %type <i> type break_command pair
-%type <e> set_item set_items
+%type <e> set_item set_items switch_body
 %type <v> set_atom prefix prefix_s ipa
 %type <s> decls function_params 
 
@@ -191,6 +190,20 @@ set_items:
  | set_items ',' set_item { $$ = $3; $$->left = $1; }
  ;
 
+/* 2 shift/reduce conflicts here. Curable by replacing cmds with block which breaks syntax */
+switch_body: /* EMPTY */ { $$ = NULL; }
+ | set_item ':' cmds switch_body {
+     $$ = $1;
+     $$->data = $3;
+     $$->left = $4;
+   }
+ | ELSE ':' cmds {
+     $$ = f_new_tree(); 
+     $$->from.type = T_VOID; 
+     $$->to.type = T_VOID;
+     $$->data = $3;
+   }
+ ;
 
 constant:
    CONST '(' expr ')' { $$ = f_new_inst(); $$->code = 'c'; $$->a1.i = T_INT; $$->a2.i = $3; }
@@ -286,23 +299,6 @@ var_list: term {
    }
  ;
 
-/* 2 shift/reduce conflicts here. Curable by replacing cmds with block which breaks syntax */
-switch_body: /* EMPTY */ { $$ = NULL; }
- | term ':' cmds switch_body {
-     $$ = f_new_inst();
-     $$->code = 'of';
-     $$->a1.p = $1;
-     $$->a2.p = $3;
-     $$->next = $4;
-   }
- | ELSE ':' cmds {
-     $$ = f_new_inst();
-     $$->code = 'el';
-     $$->a1.p = NULL;
-     $$->a2.p = $3;
-   }
- ;
-
 cmd:
    ifthen { 
      $$ = $1;
@@ -346,9 +342,9 @@ cmd:
    }
  | CASE term '{' switch_body '}' {
       $$ = f_new_inst();
-      $$->code = 'sw';
+      $$->code = 'SW';
       $$->a1.p = $2;
-      $$->a2.p = $4;
+      $$->a2.p = build_tree( $4 );
    }
  ;
 
index f6b2d4beeeb10371daaa08d6f8a12c7187c69f1c..dc8235ed0a6b5dfb552d3ba420bdd2cd65585f1a 100644 (file)
@@ -54,6 +54,13 @@ struct f_inst *startup_func = NULL;
 int
 val_compare(struct f_val v1, struct f_val v2)
 {
+  if ((v1.type == T_VOID) && (v2.type == T_VOID))
+    return 0;
+  if (v1.type == T_VOID)       /* Hack for else */
+    return -1;
+  if (v2.type == T_VOID)
+    return 1;
+
   if (v1.type != v2.type)
     return CMP_ERROR;
   switch (v1.type) {
@@ -64,7 +71,7 @@ val_compare(struct f_val v1, struct f_val v2)
     return 1;
   case T_IP:
     return ipa_compare(v1.val.ip, v2.val.ip);
-  default: return CMP_ERROR;
+  default: { printf( "Error comparing\n" ); return CMP_ERROR; }
   }
 }
 
@@ -114,40 +121,6 @@ static struct rte **f_rte;
 
 static struct f_val interpret(struct f_inst *what);
 
-static struct f_val
-interpret_switch(struct f_inst *what, struct f_val control)
-{
-  struct f_val this, res;
-  int i;
-  res.type = T_VOID;
-
-  if (!what)
-    return res;
-
-  switch(what->code) {
-  case 'el':
-    return interpret(what->a2.p);
-
-  case 'of':
-    this = interpret(what->a1.p);
-    i = val_compare(control, this);
-    if (!i)
-      return interpret(what->a2.p);
-    if (i==CMP_ERROR) {
-      i = val_in_range(control, this);
-      if (i==1)
-       return interpret(what->a2.p);
-      if (i==CMP_ERROR)
-       runtime( "incompatible types in case" );
-    }
-    break;
-    
-  default:
-    bug( "This can not happen (%x)\n", what->code );
-  }
-  return interpret_switch(what->next, control);
-}
-
 static struct f_val
 interpret(struct f_inst *what)
 {
@@ -309,9 +282,22 @@ interpret(struct f_inst *what)
     ONEARG;
     res = interpret(what->a2.p);
     break;
-  case 'sw': /* SWITCH alias CASE */
+  case 'SW':
     ONEARG;
-    interpret_switch(what->a2.p, v1);
+    {
+      struct f_tree *t = find_tree(what->a2.p, v1);
+      if (!t) {
+       v1.type = T_VOID;
+       t = find_tree(what->a2.p, v1);
+       if (!t) {
+         printf( "No else statement?\n ");
+         break;
+       }
+      }        
+      if (!t->data)
+       die( "Impossible: no code associated!\n" );
+      return interpret(t->data);
+    }
     break;
   case 'iM': /* IP.MASK(val) */
     TWOARGS_C;
index ea1f18f234a7a3905c87ebae5213871cb03032bd..43888f0be3c96925dd0a6a14b036df749751b71d 100644 (file)
@@ -73,7 +73,7 @@ struct f_tree *
 find_tree(struct f_tree *t, struct f_val val)
 {
   if (!t)
-    return 0;
+    return NULL;
   if ((val_compare(t->from, val) != 1) &&
       (val_compare(t->to, val) != -1))
     return t;