]> mj.ucw.cz Git - umpf.git/commitdiff
compile assignment
authorAnicka Bernathova <anicka@anicka.net>
Wed, 8 Apr 2009 20:07:24 +0000 (22:07 +0200)
committerAnicka Bernathova <anicka@anicka.net>
Wed, 8 Apr 2009 20:07:24 +0000 (22:07 +0200)
Makefile
code.c [new file with mode: 0644]
umpf.c
umpf.h

index 62772323215281a56f3313c6e364d74893bc40a5..86f9523830c364b1b673dafeae49b4c995ba552a 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -4,7 +4,7 @@ CC=gcc
 CFLAGS=-Wall -W -Wno-pointer-sign -Wstrict-prototypes -Wmissing-prototypes -O2 -g
 LDLIBS=-lpcre
 
-umpf: umpf.c cond.tab.o int.o lex.o ham.o lists.o lock.o
+umpf: umpf.c cond.tab.o lex.o ham.o lists.o lock.o code.o
        gcc -o $@ $^ $(LDLIBS)
 
 lock.o: lex.o cond.tab.o
@@ -13,10 +13,10 @@ cond.tab.o: cond.tab.c lex.o
 
 lex.o: lex.c cond.tab.c
 
-int.o: int.c
-
 ham.o: ham.c
 
+code.o: code.c
+
 lists.o: lists.c
 
 cond.tab.c: cond.y
diff --git a/code.c b/code.c
new file mode 100644 (file)
index 0000000..fbddbe1
--- /dev/null
+++ b/code.c
@@ -0,0 +1,132 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+
+#include "umpf.h"
+
+#define HASHSIZE 103
+#define MAGIC 19
+#define BUFSIZE 1024
+
+static struct list* 
+new_var_hash(void)
+{
+       struct list* res;
+       int i;
+
+       res = xmalloc (HASHSIZE * sizeof(struct list));
+       for (i = 0; i < HASHSIZE; i++)
+               list_init(res + i);
+       
+       return res;
+}
+
+static int
+get_bucket_number(char* name)
+{
+       unsigned int n = 0;
+       unsigned char* p = name;
+
+        while (*p != '\0'){
+                n = n * MAGIC + toupper(*p++);
+        }
+        n %= HASHSIZE;
+
+        return n;
+}
+
+/* if not found, variable with value "" is created  */ 
+static int
+find_var(char* name, struct list* hash)
+{
+       int n;
+       struct variable *p;
+
+       n = get_bucket_number(name);
+       int nocase = isupper(*name);
+       LIST_FOREACH(p, hash + n)
+               if (!(nocase ? strcasecmp : strcmp)(p->name,name))
+                       return p->varcode;
+
+       p = xmalloc(sizeof(struct variable));
+       p->name = xstrdup(name);
+       p->varcode = current_varcode++;
+       list_add_last(hash+n, &p->car);
+       return p->varcode;
+}
+
+void
+init(void)
+{
+       list_init(&input_code);
+       var_hash = new_var_hash();
+       const_tab = xmalloc(BUFSIZE);
+       cur_const_s = BUFSIZE;
+}
+
+static int
+store_const(char* c)
+{
+       if (cur_const_n >= cur_const_s) {
+               cur_const_s *= 2;
+               const_tab = xrealloc(const_tab, cur_const_s);
+       }
+
+       const_tab[cur_const_n] = c;
+
+       return -cur_const_n++;  
+}
+
+static void
+new_instr(struct code c)
+{
+       struct code* p = xmalloc(sizeof(struct code));
+       *p = c;
+       list_add_last(&input_code, &p->car);
+}
+
+static void
+do_ass(struct tree* t)
+{
+       int var_l, var_r;
+       struct code ins;
+       var_l = find_var(t->pt.ass.left->pt.leaf.value, var_hash);
+
+       if (t->st == ST_LEAF) { 
+               if (t->pt.leaf.type == L_VAR)
+                       var_r = find_var(t->pt.leaf.value, var_hash);
+               else
+                       var_r = store_const(t->pt.leaf.value);
+               ins.opc = SET;
+               ins.op1.i = var_l;
+               ins.op2.i = var_r;
+               new_instr(ins);
+       }
+}
+
+void
+compile(struct tree* t)
+{
+       if (!t)
+               return;
+       switch(t->st) {
+               case ST_BLOCK:
+                       compile(t->pt.block.head);
+                       compile(t->pt.block.tail);
+                       break;
+               case ST_EMPTY:
+                       break;
+               case ST_ASS:
+                       do_ass(t);
+                       break;
+               default:
+                       die("compile: got to default");
+       }
+}
+
+void
+print_code(struct tree* t)
+{
+
+}
diff --git a/umpf.c b/umpf.c
index 266fe7aa4c54a61bc07d7fefad033c302de4a35d..bb8ca58f0fa002dcb08e62a7d3e5733b17e76316 100644 (file)
--- a/umpf.c
+++ b/umpf.c
@@ -8,12 +8,12 @@ main(int argc, char** argv)
 {
        int res;
        
-       //FIXME:
+/*     //FIXME:
        struct passwd* p;
        p = getpwuid(getuid());
        char* default_mbox = cat("/var/mail/", p->pw_name);
        get_default_mailbox(default_mbox);
-
+*/
        if (argc < 2)
                die("Usage: ./umpf conf_file");
 
@@ -26,17 +26,17 @@ main(int argc, char** argv)
        if (res)
                return res;
 
-//     print_tree(input_tree,0);
+       init();
+       compile(input_tree);
 
-       var_hash = new_var_hash();
+       print_code(input_tree);
 
-       current_headers = make_hlist();
-       current_body = get_body();
+//     var_hash = new_var_hash();
+//     current_headers = make_hlist();
+//     current_body = get_body();
 //     print_headers(current_headers);
-
-       save_current_headers(var_hash);
-       interp(input_tree, var_hash);
-       
+//     save_current_headers(var_hash);
+//     interp(input_tree, var_hash);
 //     print_vars(var_hash);
 
        return 0;
diff --git a/umpf.h b/umpf.h
index 7cac96808f1d3ea6e58577af8c116ce250bab7d4..c12492fc2418150d29b0f93c2a2792c60c4f401d 100644 (file)
--- a/umpf.h
+++ b/umpf.h
@@ -78,13 +78,6 @@ int line;
 FILE* conf;
 
 /* int.c */
-struct variable {
-       struct node car;
-       char* name;
-       char* value;
-       int modified;
-};
-
 struct hlist {
        struct node car;
        char* name;
@@ -104,16 +97,9 @@ struct action {
        struct email e;
 };
 
-struct list* var_hash;
+/* ham.c */
 char* default_mailbox;
 
-void print_tree(struct tree* t, int ind);
-void interp(struct tree* t, struct list* hash);
-struct list* new_var_hash(void);
-void print_vars(struct list* hash);
-void save_current_headers(struct list* hash);
-
-/* ham.c */
 struct list* current_headers;
 struct email* current_body;
 struct list* make_hlist(void);
@@ -126,3 +112,45 @@ void save_gids(void);
 void close_mailbox(int fd, char* path, int is_default_mailbox);
 int open_mailbox(char* path, int is_default_mailbox);
 char* cat(char* l, char* r);
+
+/* code.c */
+enum opcode {
+       SET,
+       JUMP,
+       JUMP_IF,
+       JUMP_UNLESS,
+       DELIVER,
+       CALL_EXT,
+       NOP
+};
+
+union op {
+       int i;
+       char* s;
+       struct code* c;
+};     
+
+struct code {
+       struct node car;
+       enum opcode opc;
+       union op op1;
+       union op op2;
+       union op op3;
+};
+
+struct variable {
+       struct node car;
+       char* name;
+       int varcode;    
+};
+
+struct list input_code;
+struct list* var_hash;
+int current_varcode;
+char** var_tab; 
+char** const_tab;
+int cur_const_n, cur_const_s;
+
+void init(void);
+void compile(struct tree* t);
+void print_code(struct tree* t);