diff options
author | Julian T <julian@jtle.dk> | 2020-02-11 12:24:56 +0100 |
---|---|---|
committer | Julian T <julian@jtle.dk> | 2020-02-11 12:24:56 +0100 |
commit | 6db1a2cdd3b96731f2e092d55d8c2136eabc52d0 (patch) | |
tree | 2be8fae8ce82d708ed9f00f376dda14420850e80 /sem1/osc/mm11/regn | |
parent | 57305119e05559c1c37e903aef89cd43f44c42c9 (diff) |
Rename and cleanup
Diffstat (limited to 'sem1/osc/mm11/regn')
-rw-r--r-- | sem1/osc/mm11/regn/Makefile | 30 | ||||
-rw-r--r-- | sem1/osc/mm11/regn/regn.l | 40 | ||||
-rw-r--r-- | sem1/osc/mm11/regn/regn.y | 54 | ||||
-rw-r--r-- | sem1/osc/mm11/regn/symtab.c | 50 | ||||
-rw-r--r-- | sem1/osc/mm11/regn/symtab.h | 20 |
5 files changed, 0 insertions, 194 deletions
diff --git a/sem1/osc/mm11/regn/Makefile b/sem1/osc/mm11/regn/Makefile deleted file mode 100644 index 442feba..0000000 --- a/sem1/osc/mm11/regn/Makefile +++ /dev/null @@ -1,30 +0,0 @@ - -LEX=flex -YACC=bison -LIBS=-ly -lfl -lm -CC=gcc - -PROG=regn -TRASH=lex.yy.c $(PROG).tab.c $(PROG) $(PROG).tab.h $(PROG).output - -$(PROG): $(PROG).tab.o lex.yy.o symtab.o - $(CC) -o $@ $^ $(LIBS) - -$(PROG).tab.c $(PROG).tab.h: $(PROG).y - $(YACC) -d -v $(PROG).y - -lex.yy.c: $(PROG).l - $(LEX) $(PROG).l - -%.o: %.c - $(CC) -c -o $@ $^ - -PHONY: clean run - -run: $(PROG) - ./$(PROG) - -clean: - rm -f *.o - rm -f $(TRASH) - diff --git a/sem1/osc/mm11/regn/regn.l b/sem1/osc/mm11/regn/regn.l deleted file mode 100644 index bbaadb8..0000000 --- a/sem1/osc/mm11/regn/regn.l +++ /dev/null @@ -1,40 +0,0 @@ -%{ -#include <math.h> -#include <string.h> -#include "symtab.h" -#include "regn.tab.h" -%} - -realtal ([0-9]+|([0-9]*\.[0-9]+))([eE][-+]?[0-9]+)? -var_begin let -op_log log -op_exp exp -op_sqrt sqrt -var [A-Za-z][A-Za-z0-9]* - -%% -{realtal} {yylval.dval = atof(yytext); - return TAL;} -{var_begin} {return VAR_BEGIN;} -{op_log} {return LOG;} -{op_exp} {return EXP;} -{op_sqrt} {return SQRT;} - -{var} {yylval.string = strdup(yytext); return VAR;} - -[ \t] ; - - -'$' {return 0;} - -\n|. {return yytext[0];} - -%% - -void init_sym() -{ - int i; - for (i = 0; i < HASHSIZE; i++) - symbolarray[i] = NULL; -} - diff --git a/sem1/osc/mm11/regn/regn.y b/sem1/osc/mm11/regn/regn.y deleted file mode 100644 index d0f67eb..0000000 --- a/sem1/osc/mm11/regn/regn.y +++ /dev/null @@ -1,54 +0,0 @@ -%{ -#include <stdio.h> -#include <math.h> -#include "symtab.h" -#include <string.h> -%} - -%union { - char *string; - double dval; -} - -%token <string> VAR -%token <dval> TAL -%token LOG EXP SQRT VAR_BEGIN - -%left '-' '+' -%right LOG EXP SQRT -%left '*' '/' -%right UMINUS - -%type <dval> expression - -%% - -statement_list: statement '\n' - | statement_list statement '\n' ; - -statement: expression {printf("= %f\n",$1);} - | VAR_BEGIN VAR '=' expression {symnode_t *n = sym_lookup($2); - if( !n ) {n = sym_insert($2); } - n->value = $4;}; - -expression: expression '+' expression {$$ = $1 + $3;} - | expression '-' expression {$$ = $1 - $3;} - | expression '*' expression {$$ = $1 * $3;} - | expression '/' expression {if ($3 == 0.0) - yyerror("divide dy zero"); - else $$ = $1 / $3;} - | '-' expression %prec UMINUS {$$ = - $2;} - | '(' expression ')' {$$= $2;} - | LOG expression {$$ = log($2);} - | EXP expression {$$ = exp($2);} - | SQRT expression {$$ = sqrt($2);} - | TAL {$$ = $1;} - | VAR {symnode_t *n = sym_lookup($1); - if( !n ) { yyerror("Var not found"); } else { $$ = n->value;} }; -%% - -int main() -{ - yyparse(); -} - diff --git a/sem1/osc/mm11/regn/symtab.c b/sem1/osc/mm11/regn/symtab.c deleted file mode 100644 index 8103203..0000000 --- a/sem1/osc/mm11/regn/symtab.c +++ /dev/null @@ -1,50 +0,0 @@ -#include "symtab.h" -#include <stdlib.h> -#include <string.h> - -unsigned int hash(char *s) { - uint32_t hv = 0; - for( int i = 0; s[i] != '\0'; i++ ) { - // take MSB 6 bits of hv and xors with LSB of s[i] - uint32_t v = ( hv >> 26 ) ^ (s[i] & 0x3f); - - // Push those back on hv - hv = (hv << 4) | v; - } - // Return appropriate size - return hv % HASHSIZE; -} - - -symnode_t *sym_insert(char *var) { - unsigned int index = hash(var); - - // Save old value - symnode_t *oldSym = symbolarray[index]; - - // Make new - symbolarray[index] = malloc(sizeof(symnode_t)); - if( symbolarray[index] == NULL ) { - // If malloc failed - symbolarray[index] = oldSym; - return NULL; - } - - // Link old one - symbolarray[index]->next = oldSym; - symbolarray[index]->name = var; - - return symbolarray[index]; -} - -symnode_t *sym_lookup(char *var) { - unsigned int index = hash(var); - symnode_t *n = symbolarray[index]; - - // Look trough list - while(n != NULL && strcmp(n->name, var) != 0) { - n = n->next; - } - - return n; -} diff --git a/sem1/osc/mm11/regn/symtab.h b/sem1/osc/mm11/regn/symtab.h deleted file mode 100644 index c61f3a8..0000000 --- a/sem1/osc/mm11/regn/symtab.h +++ /dev/null @@ -1,20 +0,0 @@ -#include <stdint.h> -#define HASHSIZE 100 - -typedef struct symnode_struct { - char *name; - struct symnode_struct *next; - double value; -} symnode_t; - -symnode_t *sym_insert(char *var); -symnode_t *sym_lookup(char *var); - -struct symnode { - struct symnode *next; - char *name; - int type; - double value; -}; - -symnode_t *symbolarray[HASHSIZE]; |