int main(int argc, char* argv[]){ //print_enum_info(); dprintf("HELLO\n"); create_var("x", INTE, NOSTYPE, "Asldkfj"); create_var("x", INTE, NOSTYPE, "Asldkfj"); //create_var("CDAB", INT, NOSTYPE, "A"); //create_var("BCDA", INT, NOSTYPE, "A"); //create_var("ABCD", INT, NOSTYPE, "GLOBAL"); }
/* public: variable access (set) function */ int eval_set_var(const char *name, double value) { VarFn *var; if(G_varfn_table == NULL) { /* allocate the var table */ G_varfn_table = ht_create(500, vhash, vcomp, NULL, vdel); if(G_varfn_table == NULL) return 1; } /* find named var, update value or insert var/value */ if(ht_lookup(G_varfn_table, name, (void*)&var)) { /* not found, insert new variable */ var = create_var(name, value); if(var == NULL) return 2; if(ht_insert(G_varfn_table, (void*)(var->name), (void*)var)) return 3; G_var_count++; }else if(var->fn != NULL) return 4; else var->value = value; return 0; }
int my_set(t_cmd *cmd, t_shell *shell) { t_cmd *tmp; t_env *exist; tmp = cmd->next->next; while (tmp != cmd) { if (verif_var(tmp)) { if ((exist = already_exist_var(tmp, shell->var)) && update_var(tmp, exist) == -1) return (-1); else if (!exist) if (create_var(tmp, shell->var) == -1) return (-1); } else { ft_putstr_fd("Var should begin with a letter\n", 2); return (0); } tmp = tmp->next; } if (cmd->next->next == cmd) aff_var(shell->var); return (1); }
VAR * get_var (const char * var_name) { VAR * v = find_var(var_name); if (v) return v; else { return create_var(var_name); } }
/* Create a new global var if necessary, and set it up correctly. * Do not take environment into account. */ static Var * find_global_var_without_env(const char *name, const char *ename, uint32_t k) { unsigned int slot; Var *v; slot = ohash_lookup_interval(&global_variables, name, ename, k); v = ohash_find(&global_variables, slot); if (v == NULL) { v = create_var(name, ename); v->flags = VAR_DUMMY; ohash_insert(&global_variables, slot, v); } return v; }
static struct var evaluate( struct block *block, enum optype op, const struct typetree *t, ...) { va_list args; struct op ir = {0}; ir.type = op; ir.a = create_var(t); ir.a.lvalue = 0; va_start(args, t); ir.b = va_arg(args, struct var); if (NOPERANDS(op) == 2) { ir.c = va_arg(args, struct var); }
Node* create_node(int type) { Node* node = malloc(sizeof(Node)); node->type = type; if (type == NOMBRE) node->val = atof(yytext); if (type == VARIABLE) { node->var = search_var(yytext); if (node->var == NULL) { node->var = create_var((char*)_strdup(yytext)); push_var(node->var); } } return node; }
static struct block *postfix_expression(struct block *block) { struct var root; block = primary_expression(block); root = block->expr; while (1) { const struct member *field; const struct typetree *type; struct var expr, copy, *arg; struct token tok; int i, j; switch ((tok = peek()).token) { case '[': do { /* Evaluate a[b] = *(a + b). The semantics of pointer arithmetic * takes care of multiplying b with the correct width. */ consume('['); block = expression(block); root = eval_expr(block, IR_OP_ADD, root, block->expr); root = eval_deref(block, root); consume(']'); } while (peek().token == '['); break; case '(': type = root.type; if (is_pointer(root.type) && is_function(root.type->next)) type = type_deref(root.type); else if (!is_function(root.type)) { error("Expression must have type pointer to function, was %t.", root.type); exit(1); } consume('('); arg = calloc(nmembers(type), sizeof(*arg)); for (i = 0; i < nmembers(type); ++i) { if (peek().token == ')') { error("Too few arguments, expected %d but got %d.", nmembers(type), i); exit(1); } block = assignment_expression(block); arg[i] = block->expr; /* todo: type check here. */ if (i < nmembers(type) - 1) { consume(','); } } while (is_vararg(type) && peek().token != ')') { consume(','); arg = realloc(arg, (i + 1) * sizeof(*arg)); block = assignment_expression(block); arg[i] = block->expr; i++; } consume(')'); for (j = 0; j < i; ++j) param(block, arg[j]); free(arg); root = eval_call(block, root); break; case '.': consume('.'); tok = consume(IDENTIFIER); field = find_type_member(root.type, tok.strval); if (!field) { error("Invalid field access, no member named '%s'.", tok.strval); exit(1); } root.type = field->type; root.offset += field->offset; break; case ARROW: consume(ARROW); tok = consume(IDENTIFIER); if (is_pointer(root.type) && is_struct_or_union(root.type->next)) { field = find_type_member(type_deref(root.type), tok.strval); if (!field) { error("Invalid field access, no member named '%s'.", tok.strval); exit(1); } /* Make it look like a pointer to the field type, then perform * normal dereferencing. */ root.type = type_init(T_POINTER, field->type); root = eval_deref(block, root); root.offset = field->offset; } else { error("Invalid field access."); exit(1); } break; case INCREMENT: consume(INCREMENT); copy = create_var(root.type); eval_assign(block, copy, root); expr = eval_expr(block, IR_OP_ADD, root, var_int(1)); eval_assign(block, root, expr); root = copy; break; case DECREMENT: consume(DECREMENT); copy = create_var(root.type); eval_assign(block, copy, root); expr = eval_expr(block, IR_OP_SUB, root, var_int(1)); eval_assign(block, root, expr); root = copy; break; default: block->expr = root; return block; } } }
int i_execute_line (SOURCE_FILE * sf,const int todo) { l_get_line(sf);pline = line; l_get_token(); if (token_type==TT_LINE_END) { return EXE_DO;// skip empty line } else if (IS_KEYWORD(token_type)) { if (token_type==KEY_END) { return EXE_END; } else if (token_type==KEY_IF) { return i_execute_if (sf,todo); } else if (token_type==KEY_WHILE) { return i_execute_while(sf,todo); } else if (token_type==KEY_REPEAT) { return i_execute_repeat(sf,todo); } if (token_type==KEY_UNTIL) { return EXE_UNTIL; } else if (token_type==KEY_FOR) { return i_execute_for(sf,todo); } else if (token_type==KEY_CASE) { return i_execute_case(sf,todo); } else if (token_type==KEY_ELSEIF) { return EXE_ELSEIF; } else if (token_type==KEY_WHEN) { return EXE_WHEN; } else if (token_type==KEY_ELSE) { return EXE_ELSE; } if (!todo) return EXE_DO; if (token_type==KEY_BREAK) { return EXE_BREAK; } else if (token_type==KEY_RETURN) { return EXE_RETURN; } else if (token_type==KEY_GOSUB) { extern int i_call_sub (SOURCE_FILE *,const char *); l_get_token(); return i_call_sub(sf,token); } else if (token_type==KEY_DIM) { ARRAY * array; long size; while(1) { l_get_token(); // new array array = (ARRAY*)calloc(sizeof(ARRAY),1); array->name = s_strdup(token); list_push(&list_array,array); // l_get_token();// skip '(' size = (long)calc_check(FALSE,pline); if (size<=0) { merror_msg("illegal array size %d!",size); } array->size = size; array->array = calloc(sizeof(real),size); { int i; for (i=0;i<size;++i) array->array[i] = 0.0; } l_get_token();// skip ')' l_get_token(); if (token_type==TT_COM) continue; else if (token_type==TT_LINE_END) break; } } else if (token_type==KEY_EXIT) { d_exit(); } } else if(token_type==TT_ID) { if (str_eq(token,"print")) { while(1) { l_get_token(); if (token_type==TT_STRING) { printf("%s",token); } else { real result; char buf[128]; l_put_back(); result = calc_check(FALSE,pline); d_ftoa(result,buf); printf(buf); } l_get_token(); if (token_type==TT_LINE_END)break; }//while }//print else if (str_eq(token,"input")) { VAR * v;char buf[64]; while(1) { l_get_token(); v = get_var(token); gets(64,buf);puts(buf); v->value = atof(buf); l_get_token(); if (token_type==TT_LINE_END)break; }//while }//input else { ARRAY * a; VAR * var; var = find_var(token); if (var!=NULL) { l_get_token(); //skip '=' var->value = calc_check(FALSE,pline); return EXE_DO; } a = find_array(token); if(a!=NULL) { int index; l_get_token();//skip ( index = (int)calc_check(FALSE,pline); // delete this line,calc_check call skip ( automatically //l_get_token();//skip ) l_get_token();// skip = assign_element(a,index,calc_check(FALSE,pline)); return EXE_DO; } var = create_var(token); l_get_token(); //skip '=' var->value = calc_check(FALSE,pline); return EXE_DO; }//assign }// TT_ID else { merror_illegal_token(); } return EXE_DO; }
struct list * read_config(const char *filename) { APP_DEBUG_FNAME; struct list *beg_cmd; struct list *beg_var; struct list *l; FILE *in; char *line; size_t len; ssize_t read_len; beg_cmd = NULL; beg_var = NULL; len = CONF_LINE_MAXLENGTH; line = alloc_string_size(len); in = fopen(filename, "r"); if (in == NULL) { ERR("fopen('%s'): %s", filename, strerr()); myabort(); } else { while ((read_len = getline(&line, &len, in)) != -1) { // remove \n from the end if (read_len > 0) line[read_len - 1] = '\0'; switch (check_line(line)) { case LINE_VARIABLE: l = malloc(sizeof (struct list)); l->next = beg_var; l->item = create_var(line, beg_var); beg_var = l; break; case LINE_COMMAND: l = malloc(sizeof (struct list)); l->next = beg_cmd; l->item = create_cmd(line, beg_var); beg_cmd = l; break; case LINE_BAD: WARN("bad line structure '%s'", line); break; case LINE_IGNORE: DEBUG("ignoring line '%s'", line); break; } l = NULL; } if (ferror(in)) WARN("ferror while reading '%s', try to continue", filename); fclose(in); print_cfg(beg_var, beg_cmd); } free(line); delete_list(&beg_var); return (beg_cmd); }