Exemplo n.º 1
0
Arquivo: cli.c Projeto: Agyar/lhnb
static void* vars (int argc, char **argv, void *data)
{
	ItemT *titem = items;

	cli_outfunf ("all variables:");

	while (titem) {
		#ifdef HIDE_NULL_HELP
			if(titem->usage)
		#endif
		if (is_variable (titem)) {
			if (titem->string) {
				cli_outfunf ("%15s [%s]\t- %s", titem->name,
					  titem->string, titem->usage);
			} else if (titem->integer) {
				cli_outfunf ("%15s [%i]\t- %s", titem->name,
					  *titem->integer, titem->usage);
			} else {
				cli_outfunf ("%s\tis a broken variable", titem->name);
			}
		}
		titem = titem->next;
	}

	cli_outfunf ("----------------");
	cli_outfunf ("to change a variable: \"variablename newvalue\"");
	return data;
}
Exemplo n.º 2
0
int parse_line (char *line, int line_length)
{
	int i, ret, buffer_index;
	char buffer[32];	
	
	memset(buffer, 0, 32);
	buffer_index = ret = 0;
	
	printf("%s", line);
	
	for (i = 0; i < line_length; i++) {		
		if (isspace(line[i]) && !buffer_index)
			continue;
		else if (line[i] == '#') {
			ret = DIRECTIVE_FOUND;
			break;
		} else if (isspace(line[i])) {
			if (is_string(buffer)) 			
				return parse_string(line, i, line_length, 1);
			else if (is_variable(buffer[0]) && find_assign(line, i, line_length)) 
				return parse_string(line, i-1, line_length, 0);				
			else {
				memset(buffer, 0, 32);
				buffer_index = 0;
			}
		} else {
			buffer[buffer_index++] = line[i];
			if (check_overflow(buffer_index, 32))
				return OVERFLOW_DETECTED;
		}
	}
	fwrite(line, strlen(line), sizeof(char), tmp);
	
	return ret;
}
Exemplo n.º 3
0
/***********************************************************************//**
 * @brief Set number of column elements for specific row
 *
 * @param[in] row Row index.
 * @param[in] elements Number of elements in @p row.
 *
 * @exception GException::out_of_range
 *            Invalid row index specified.
 * @exception GException::invalid_argument
 *            Invalid number of elements specified.
 *
 * Sets the number of elements in column for a specific @p row.
 ***************************************************************************/
void GFitsTableCol::elements(const int& row, const int& elements)
{
    // Check row value
    if (row < 0 || row >= length()) {
        throw GException::out_of_range(G_ELEMENTS1, "Row index", row, length());
    }

    // Check that elements is non-negative
    if (elements < 0) {
        std::string msg = "Number of elements " + gammalib::str(elements) +
                          " can not be negative.\nPlease specify a"
                          " non-negative number of elements.";
        throw GException::invalid_argument(G_ELEMENTS1, msg);
    }

    // First handle the case of a variable-length column
    if (is_variable()) {

        // Determine number of elements to add or to remove
        int difference = elements - (m_rowstart[row+1] - m_rowstart[row]);

        // If difference is positive, then add elements at the end of
        // the vector. This is done using resize_data() which will insert
        // "difference" elements at the index specified as first argument
        if (difference > 0) {
            resize_data(m_rowstart[row+1], difference);
        }

        // ... else if difference is negative then remove elements from
        // the end of the array. This is done using resize_data() which
        // will remove "difference" element from the index on specified as
        // first argument
        else if (difference < 0) {
            resize_data(m_rowstart[row+1]+difference, difference);
        }

        // Update row start indices
        for (int i = row + 1; i <= length(); ++i) {
            m_rowstart[i] += difference;
        }

        // Update maximum column length
        m_varlen = 0;
        for (int i = 0; i < length(); ++i) {
            int len = m_rowstart[row+1] - m_rowstart[row];
            if (len > m_varlen) {
                m_varlen = len;
            }
        }
        
    } // endif: we had a variable-length column

    // Return
    return;
}
Exemplo n.º 4
0
struct node *build_expression_tree(int operands, int num_tokens, char **tokens)
{
	int i, top;
	char *str;
	struct node *tmp;
	struct node *stack[operands];

	top = 0;
	for (i = 1; i < num_tokens; ++i) {
		str = *(tokens + i);
		if (is_number(str)) {
			stack[top] = create_node(str, NUMBER);
			++top;
		} else if (is_variable(str)) {
			stack[top] = create_node(str, VARIABLE);
			++top;
		} else if (is_bin_operator(str)) {
			if (top >= 2) {
				tmp = create_node(str, BIN_OPERATOR);
				tmp->left = stack[top - 2];
				tmp->right = stack[top - 1];
				stack[top - 2] = tmp;
				--top;
			} else {
				fprintf(stderr, "Not enough elements on stack"
					" to apply binary operator: %s. Token"
					" number: %d.\n", str, i);
				return NULL;
			}
		} else if (is_un_operator(str)) {
			if (top >= 1) {
				tmp = create_node(str, UN_OPERATOR);
				tmp->right = stack[top - 1];
				stack[top - 1] = tmp;
			} else {
				fprintf(stderr, "Not enough elements on stack"
					" to apply unary operator: %s. Token"
					" number: %d.\n", str, i);
				return NULL;
			}
		} else {
			fprintf(stderr, "Unrecognized token: %s", str);
			return NULL;
		}
	}
	if (top == 1) {
		return stack[top - 1];
	} else {
		fprintf(stderr, "More or less than one elements left on"
			" stack after all applying all operations.\n");
		return NULL;
	}
}
Exemplo n.º 5
0
int op_let_un_math(target_x86_t *target_x86, char *str_dst, char *str_src, char *op)
{
	char line[STR_LINE_SIZE];
	var_t *var;

	if( is_variable(str_src) )
	{
		var_t *var;
		
		var = target_x86_get_var(target_x86, str_src);
		str_src = var->addr;
	}
	
	var = target_x86_get_var(target_x86, str_dst);
	str_dst = var->addr;

	sprintf(line, "mov eax, %s", str_src);
	add_line(target_x86, TARGET_TEXT, line);

	if( strcmp(op, "-") == 0 )
	{
		add_line(target_x86, TARGET_TEXT, "neg eax");
	}
	
	if( strcmp(op, "not") == 0 )
	{
		add_line(target_x86, TARGET_TEXT, "neg eax");
	}

	if( strcmp(op, "neg") == 0 )
	{
		add_line(target_x86, TARGET_TEXT, "mov ecx, 0");

		add_line(target_x86, TARGET_TEXT, "cmp eax, 0");
		sprintf(line, "jne .label_%d", target_x86->label_count);
		add_line(target_x86, TARGET_TEXT, line);
		
		add_line(target_x86, TARGET_TEXT, "mov ecx, 1");

		sprintf(line, ".label_%d:", target_x86->label_count);
		add_line(target_x86, TARGET_TEXT, line);
		
		add_line(target_x86, TARGET_TEXT, "mov eax, ecx");

		target_x86->label_count++;
	}
	
	sprintf(line, "mov %s, eax", str_dst);
	add_line(target_x86, TARGET_TEXT, line);
	
	return 0;
}
Exemplo n.º 6
0
/***********************************************************************//**
 * @brief Load table column from FITS file
 *
 * Loads table column from FITS file by calling the load_column_variable()
 * method for variable-length columns and load_column_fixed() for fixed-
 * length columns.
 ***************************************************************************/
void GFitsTableCol::load_column(void)
{
    // Load variable-length or fixed-length column from FITS file
    if (is_variable()) {
        load_column_variable();
    }
    else {
        load_column_fixed();
    }

    // Return
    return;
}
Exemplo n.º 7
0
/***********************************************************************//**
 * @brief Save table column into FITS file
 *
 * Save table column into FITS file by calling the save_column_variable()
 * method for variable-length columns and save_column_fixed() for fixed-
 * length columns.
 ***************************************************************************/
void GFitsTableCol::save_column(void)
{
    // Save variable-length or fixed-length column into FITS file
    if (is_variable()) {
        save_column_variable();
    }
    else {
        save_column_fixed();
    }

    // Return
    return;
}
Exemplo n.º 8
0
int num_operands(int num_tokens, char **tokens)
{
	int i, operands;
	const char *tok;

	operands = 0;
	for (i = 1; i < num_tokens; ++i) {
		tok = *(tokens + i);
		if (is_number(tok) || is_variable(tok)) {
			++operands;
		}
	}
	return operands;
}
Exemplo n.º 9
0
Arquivo: cli.c Projeto: Agyar/lhnb
char *cli_getstring(char *variable){
		ItemT *titem = items;
		while (titem) {
			if (is_variable (titem)) {
				if (!strcmp (variable, titem->name)) {
					if(titem->string)
						return(titem->string);
					if(titem->integer)
						return NULL; /* FIXME: use a static buffer perhaps */
				}
			}
			titem = titem->next;
		}
		return "";
}
Exemplo n.º 10
0
int map_expr(double dataf[], int datai[], int elem_id, double coord[])
{
    int i,len,temp;
    struct Elem out;
    int *results;
    double *constf;
    results = &datai[elem_id+6];
    constf = &dataf[datai[elem_id+2]];
    len = datai[elem_id+5];
    init_elem_stack();
    for(i=0;i<len;i++)
    {
        if(is_operator(dataf,datai,results[i])||(decode(dataf,datai,results[i])==op_constf))
        {   
            if(decode(dataf,datai,results[i])==op_constf)
            {
                op_CONSTF(constf);
                continue;
            }
            call_ops(decode(dataf,datai,results[i]));
        }
        else
        {
            if(is_variable(results[i]))
            {
                temp = decode(dataf,datai,results[i]);
                if(temp%2==0)
                    op_NUMi(datai[temp/2]);
                else
                    op_NUMf(dataf[(temp-1)/2]);
                continue;
            }
            op_NUMi(decode(dataf,datai,results[i]));
        }
    }
    out = elem_pop();
    if(out.type==INT) 
    {
        update_gen(dataf,datai,elem_id,out.data.i,0);
        printf("%d\n",datai[(decode(dataf,datai,datai[elem_id+4]))/2]);  
    }  
    if(out.type==FLOAT)
    {
        update_gen(dataf,datai,elem_id,0,out.data.f);
        printf("%f\n",dataf[(decode(dataf,datai,datai[elem_id+4])-1)/2]);
    }
    return 1;
}
Exemplo n.º 11
0
/***********************************************************************//**
 * @brief Compute offset of column element in memory
 *
 * @param[in] row Row of column.
 * @param[in] inx Vector index in column row.
 *
 * @exception GException::out_of_range
 *            Table row or vector index are out of valid range.
 *
 * Computes the offset of a column element in the storage array from the
 * @p row number and the vector index. The method also supports both
 * variable-length and fixed-length columns.
 ***************************************************************************/
int GFitsTableCol::offset(const int& row, const int& inx) const
{
    // Check row value
    #if defined(G_RANGE_CHECK)
    if (row < 0 || row >= m_length) {
        throw GException::out_of_range(G_OFFSET, row, 0, m_length-1);
    }
    #endif

    // Check inx value
    #if defined(G_RANGE_CHECK)
    if (inx < 0 || inx >= elements(row)) {
        throw GException::out_of_range(G_OFFSET, inx, 0, elements(row));
    }
    #endif

    // Calculate pixel offset
    int offset = (is_variable()) ? m_rowstart[row] + inx : row * m_number + inx;

    // Return offset
    return offset;
}
Exemplo n.º 12
0
data_t *eval(const data_t *exp, data_t *env) {
	if(eval_plz_die) {
		eval_plz_die = 0;
		ExitThread(0);
	}

	if(is_self_evaluating(exp))
		return (data_t*)exp;
	if(is_variable(exp))
		return lookup_variable_value(exp, env);
	if(is_quoted_expression(exp))
		return get_text_of_quotation(exp);
	if(is_assignment(exp))
		return eval_assignment(exp, env);
	if(is_definition(exp))
		return eval_definition(exp, env);
	if(is_if(exp))
		return eval_if(exp, env);
	if(is_lambda(exp))
		return make_procedure(get_lambda_parameters(exp), get_lambda_body(exp), env);
	if(is_begin(exp))
		return eval_sequence(get_begin_actions(exp), env);
	if(is_cond(exp))
		return eval(cond_to_if(exp), env);
	if(is_letrec(exp))
		return eval(letrec_to_let(exp), env);
	if(is_let_star(exp))
		return eval(let_star_to_nested_lets(exp), env);
	if(is_let(exp))
		return eval(let_to_combination(exp), env);
	if(is_application(exp))		
		return apply(
			eval(get_operator(exp), env),
			get_list_of_values(get_operands(exp), env));
	
	printf("Unknown expression type -- EVAL '");
	return make_symbol("error");
}
Exemplo n.º 13
0
int op_let_assign(target_x86_t *target_x86, char *str_dst, char *str_src)
{
	char line[STR_LINE_SIZE];
	var_t *var;

	if( is_variable(str_src) )
	{
		var_t *var;
		
		var = target_x86_get_var(target_x86, str_src);
		str_src = var->addr;
	}
	
	var = target_x86_get_var(target_x86, str_dst);
	str_dst = var->addr;

	sprintf(line, "mov eax, %s", str_src);
	add_line(target_x86, TARGET_TEXT, line);
	
	sprintf(line, "mov %s, eax", str_dst);
	add_line(target_x86, TARGET_TEXT, line);
	
	return 0;
}
Exemplo n.º 14
0
bool is_bound(object *expression, object *env) {
    return is_variable(expression) ? !is_error(lookup_variable_value(expression, env)) : false;
}
Exemplo n.º 15
0
object *eval(object *exp, object *env) {

    object *procedure;
    object *arguments;
    object *result;
    bool tailcall = false;

    do {

        if (is_self_evaluating(exp))
            return exp;

        if (is_variable(exp))
            return lookup_variable_value(exp, env);

        if (is_quoted(exp))
            return text_of_quotation(exp);

        if (is_assignment(exp))
            return eval_assignment(exp, env);

        if (is_definition(exp))
            return eval_definition(exp, env);

        if (is_if(exp)) {
            exp = is_true(eval(if_predicate(exp), env)) ? if_consequent(exp) : if_alternative(exp);
            tailcall = true;
            continue;
        }

        if (is_lambda(exp))
            return make_compound_proc(lambda_parameters(exp), lambda_body(exp), env);

        if (is_begin(exp)) {
            exp = begin_actions(exp);
            while (!is_last_exp(exp)) {
                eval(first_exp(exp), env);
                exp = rest_exps(exp);
            }
            exp = first_exp(exp);
            tailcall = true;
            continue;
        }

        if (is_cond(exp)) {
            exp = cond_to_if(exp);
            tailcall = true;
            continue;
        }

        if (is_let(exp)) {
            exp = let_to_application(exp);
            tailcall = true;
            continue;
        }

        if (is_and(exp)) {
            exp = and_tests(exp);
            if (is_empty(exp))
                 return make_boolean(true);
            while (!is_last_exp(exp)) {
                result = eval(first_exp(exp), env);
                if (is_false(result))
                    return result;
                exp = rest_exps(exp);
            }
            exp = first_exp(exp);
            tailcall = true;
            continue;
        }

        if (is_or(exp)) {
            exp = or_tests(exp);
            if (is_empty(exp)) {
                return make_boolean(false);
            }
            while (!is_last_exp(exp)) {
                result = eval(first_exp(exp), env);
                if (is_true(result))
                    return result;
                exp = rest_exps(exp);
            }
            exp = first_exp(exp);
            tailcall = true;
            continue;
        }

        if (is_application(exp)) {

            procedure = eval(operator(exp), env);
            arguments = list_of_values(operands(exp), env);

            if (is_primitive_proc(procedure) && procedure->data.primitive_proc.fn == eval_proc) {
                exp = eval_expression(arguments);
                env = eval_environment(arguments);
                tailcall = true;
                continue;
            }

            if (is_primitive_proc(procedure) && procedure->data.primitive_proc.fn == apply_proc) {
                procedure = apply_operator(arguments);
                arguments = apply_operands(arguments);
            }

            if (is_primitive_proc(procedure))
                return (procedure->data.primitive_proc.fn)(arguments);

            if (is_compound_proc(procedure)) {
                env = extend_environment(procedure->data.compound_proc.parameters, arguments, procedure->data.compound_proc.env);
                exp = make_begin(procedure->data.compound_proc.body);
                tailcall = true;
                continue;
            }

            return make_error(342, "unknown procedure type");
        } // is_application()

    } while (tailcall);

    fprintf(stderr, "cannot eval unknown expression type\n");
    exit(EXIT_FAILURE);
}
Exemplo n.º 16
0
Arquivo: bit.hpp Projeto: sempuki/code
 uint8_t *data () { return is_variable (header)? variable : bytes; }
Exemplo n.º 17
0
static OBJ analyze_r(const struct analyze_t *arg)
{
	OBJ op;
	OBJ ret;
	struct analyze_t new_arg;

	new_arg = *arg;
	ret = OBJ_NULL;

	if (is_self_evaluating(new_arg.sexp))
		ret = new_arg.sexp;
	else if (is_variable(new_arg.sexp))
		ret = analyze_variable_cell(new_arg.sexp,new_arg.env,new_arg.macro,new_arg.params,new_arg.macro_expand_env);
	else if(obj_pairp(new_arg.sexp))
 	{
		if(obj_pairp(car(new_arg.sexp)))
		{
			new_arg.sexp = car(new_arg.sexp);
			op = fake_eval(&new_arg);
			new_arg = *arg;
		}
		else
			op = analyze_variable_value(car(new_arg.sexp),new_arg.env,new_arg.macro,new_arg.params,new_arg.macro_expand_env);
		if(op == OBJ_NULL) /* error handle---fixme!! */
			return OBJ_NULL;
		if(obj_corep(op))
		{
			switch(obj_core_type(op))
			{
			case DEFINE:
			case DEFINE_SYNTAX:
				new_arg.sexp = cdr(new_arg.sexp);
				ret = analyze_define(&new_arg);
				break;
			case SET:
				new_arg.sexp = cdr(new_arg.sexp);
				ret = analyze_set(&new_arg);
				break;
			case IF:
				ret = analyze_if(cdr(new_arg.sexp),new_arg.env,new_arg.tail);
				break;
			case QUOTE:
				ret = obj_make_quote(cadr(new_arg.sexp));
				break;
			case BEGIN:
				new_arg.sexp = cdr(new_arg.sexp);
				ret = analyze_begin(&new_arg);
				break;
			case LAMBDA:
				new_arg.sexp = cdr(new_arg.sexp);
				ret = analyze_lambda(&new_arg);
				break;
			case SYNTAX_RULES:
				ret = analyze_syntax_rules(cdr(new_arg.sexp),new_arg.env);
				break;
			default:
				fprintf(stderr,"unknown core tag\n");
			}
		}
		else if(obj_syntaxp(op))
		{
			OBJ params;
			OBJ data;
			OBJ patten;
			OBJ template;
			int match;

			match = 0;
			data = obj_syntax_data(op);
			while(obj_pairp(data))
			{
				patten = caar(data);
bool expression_calc::is_operand( const std::string& s ) 
{
	return is_numeric( s ) || is_variable( s );
}
Exemplo n.º 19
0
Arquivo: eval.c Projeto: ingramj/bs
object *bs_eval(object *exp, object *env)
{
tailcall:
    if (is_empty_list(exp)) {
        error("unable to evaluate empty list");
    } else if (is_self_evaluating(exp)) {
        return exp;
    } else if (is_variable(exp)) {
        return lookup_variable_value(exp, env);
    } else if (is_quoted(exp)) {
        return quoted_expression(exp);
    } else if (is_assignment(exp)) {
        return eval_assignment(exp, env);
    } else if (is_definition(exp)) {
        return eval_definition(exp, env);
    } else if (is_if(exp)) {
        if (is_true(bs_eval(if_predicate(exp), env))) {
            exp = if_consequent(exp);
        } else {
            exp = if_alternate(exp);
        }
        goto tailcall;
    } else if (is_lambda(exp)) {
        return make_compound_proc(lambda_parameters(exp),
                lambda_body(exp),
                env);
    } else if (is_begin(exp)) {
        exp = begin_actions(exp);
        if (is_empty_list(exp)) {
            error("empty begin block");
        }
        while (!is_empty_list(cdr(exp))) {
            bs_eval(car(exp), env);
            exp = cdr(exp);
        }
        exp = car(exp);
        goto tailcall;
    } else if (is_cond(exp)) {
        exp = cond_to_if(exp);
        goto tailcall;
    } else if (is_let(exp)) {
        exp = let_to_application(exp);
        goto tailcall;
    } else if (is_and(exp)) {
        exp = and_tests(exp);
        if (is_empty_list(exp)) {
            return get_boolean(1);
        }
        object *result;
        while (!is_empty_list(cdr(exp))) {
            result = bs_eval(car(exp), env);
            if (is_false(result)) {
                return result;
            }
            exp = cdr(exp);
        }
        exp = car(exp);
        goto tailcall;
    } else if (is_or(exp)) {
        exp = or_tests(exp);
        if (is_empty_list(exp)) {
            return get_boolean(0);
        }
        object *result;
        while (!is_empty_list(cdr(exp))) {
            result = bs_eval(car(exp), env);
            if (is_true(result)) {
                return result;
            }
            exp = cdr(exp);
        }
        exp = car(exp);
        goto tailcall;
    } else if (is_application(exp)) {
        object *procedure = bs_eval(application_operator(exp), env);
        object *parameters = eval_parameters(application_operands(exp), env);

        // handle eval specially for tailcall requirement.
        if (is_primitive_proc(procedure) &&
                procedure->value.primitive_proc == eval_proc) {
            exp = eval_expression(parameters);
            env = eval_environment(parameters);
            goto tailcall;
        }

        // handle apply specially for tailcall requirement.
        if (is_primitive_proc(procedure) &&
                procedure->value.primitive_proc == apply_proc) {
            procedure = apply_operator(parameters);
            parameters = apply_operands(parameters);
        }

        if (is_primitive_proc(procedure)) {
            return (procedure->value.primitive_proc)(parameters);
        } else if (is_compound_proc(procedure)) {
            env = extend_environment(
                    procedure->value.compound_proc.parameters,
                    parameters,
                    procedure->value.compound_proc.env);
            exp = make_begin(procedure->value.compound_proc.body);
            goto tailcall;
        } else {
            error("unable to apply unknown procedure type");
        }
    } else {
        error("unable to evaluate expression");
    }
}
Exemplo n.º 20
0
uint64_t
InsnSemanticsExpr::LeafNode::get_name() const
{
    assert(is_variable() || is_memory());
    return name;
}
Number shunting_yard(std::vector<std::string> & tokens, std::vector<std::string> & out_ops, bool boolean_exp = false) {
    prepare();
    std::string previous_token;
    Operator * op;
    Operator * previous_op = new Sum();
    std::string last_token = "";
    std::string token;

    for(auto s = tokens.begin(); s != tokens.end(); ++s) {
        token = *s;
        //std::cout<<token<<std::endl;
        if(is_number(token)) {
            if(is_number(previous_token)) {
                throw std::string("Too many operands");
            }
            if(previous_op && previous_op->sign() == ")") {
                throw std::string("Missing operation between paretheses");
            }
            Number i = parse_number(token);
            if(i >= Number("10", "99") || i <= Number("-10", "99")) {
                //std::cout<<i<<'\n';
                throw std::string("Number out of valid range");
            }

            if(is_variable(token)) {
                std::stringstream g;
                g << i;
                out_ops.push_back(token + "= " + g.str());
            }
            output_stack.push(i);
            previous_op = nullptr;
        }
        else if((op = get_function_op(token)) != nullptr) {
            operator_stack.push(op);
        }

        else if (token == ",") {
            while(!operator_stack.empty() && operator_stack.top()->sign() != "(") {
                evaluate(operator_stack.top(), out_ops);
                operator_stack.pop();
            }
            // error
            if(operator_stack.empty()) {
                throw std::string("mismatched parens");
            }
            // dirty fix, the problem is that in get_operator we only return a
            // negative if there was an operator before, so root(3, -27) was being
            // treated as a substraction
            previous_op = new Sum();
        }

        else if((op = get_operator(token, previous_op, boolean_exp)) != nullptr) {
            if(op->sign() == "(") {
                if(!previous_op || (previous_op && previous_op->sign() == ")" )) {
                    throw std::string("Missing operation between paretheses");
                }
                operator_stack.push(op);
            } else if(op->sign() == ")") {
                while(!operator_stack.empty() && operator_stack.top()->sign() != "(") {
                    evaluate(operator_stack.top(), out_ops);
                    operator_stack.pop();
                }
                // error
                if(operator_stack.empty()) {
                    throw std::string("mismatched parens");
                }
                operator_stack.pop();
                if(!operator_stack.empty() && operator_stack.top()->sign() == "r") {
                    evaluate_function(operator_stack.top(), out_ops);
                    operator_stack.pop();
                }
            }
            else {
                while(!operator_stack.empty() &&
                        ( (op->associativity() == assoc::LEFT && op->precedence() <= operator_stack.top()->precedence())
                          ||
                          (op->associativity() == assoc::RIGHT && op->precedence() < operator_stack.top()->precedence())))
                {
                    evaluate(operator_stack.top(), out_ops);
                    operator_stack.pop();
                }
                operator_stack.push(op);
            }
            previous_op = op;
        }

        else if(token == "=" && !boolean_exp) {
            if(s+1 != tokens.end())
                last_token = *(s+1);
            break;
        }
        // unknown token
        else {
            throw std::string("Unexpected Token '" + token + "'");
        }
        previous_token = token;
    }

    while(!operator_stack.empty()) {
        op = operator_stack.top();
        if(op->sign() == "(") {
            throw std::string("mismatched parens");
        }
        operator_stack.pop();
        evaluate(op, out_ops);
    }

    if(token != "=" && !boolean_exp) {
        throw std::string("Missing =");
    }

    if(!last_token.empty() && !boolean_exp) {
        if(var_map.find(last_token) != var_map.end()) {
            var_map[last_token] = output_stack.top();
            if(tokens[tokens.size()-1] != last_token) {
                throw std::string("Warning, no more expressions after variable assignment");
            }
            // out ops A=
            std::stringstream ss;
            ss << output_stack.top();
            out_ops.push_back(last_token + "= " + ss.str());
            //std::cout << var_map[last_token] << '\n';
        }
        else {
            throw std::string("Warning, no more expressions after =");
        }

    }
    if(output_stack.empty()) return Number ("0","0");

    Number ans = output_stack.top();
    output_stack.pop();
    if(!output_stack.empty()) throw std::string("too many arguments to function");
    return ans;

}
Exemplo n.º 22
0
int parse_string (char *line, int offset, int line_length, int type)
{
	int state, i, ret;	
	char var_name[32];
	int var_index;
	char assignment[256];
	int assign_index;
	
	char buffer[256];
	int size;
	
	memset(var_name, 0, 32);
	var_index = 0;		
	memset(assignment, 0, 256);
	assign_index = 0;		
	state = ret = 0;

	for (i = offset; i < line_length && state != 3; i++) {
		switch(state) {
		case 0:
			if (isspace(line[i]))
				continue;
			
			if (is_variable(line[i])) {
				var_name[var_index++] = line[i];
				ret = check_overflow(var_index, 32);
			} else {
				if (line[i] == ';')
					state = 3;
				else if (line[i] == '=')
					state = 1;
			}		
			break;
		case 1:
			if (!isspace(line[i])) {
				if (line[i] == '\"')
					state = 2;
			} else
				continue;	
			break;
		case 2:
			if (!isspace(line[i])) {
				if (line[i] == '\"')
					state = 3;
				else {
					assignment[assign_index++] = line[i];	
					ret = check_overflow(assign_index, 256);		
				}	
			} else
				continue;	
			break;	
		default:
			break;
		}
	}	
	if (type == 1) {
		if (root != NULL)
			insert_symbol(root, var_name);

		size = snprintf(buffer, 256, "\tchar %s = calloc(%d, sizeof(char));\n\0", 
			var_name, assign_index+1);
		fwrite(buffer, size, sizeof(char), tmp);
		
		if (assign_index) {
			size = snprintf(buffer, 256, "\tstrcpy(%s, \"%s\");\n\0", 
				var_name, assignment);
			fwrite(buffer, size, sizeof(char), tmp);
		}
	} else {
		size = snprintf(buffer, 256, "\t%s = realloc(%s, %d);\n\0", 
			var_name, var_name, assign_index+1);
		fwrite(buffer, size, sizeof(char), tmp);
		
		size = snprintf(buffer, 256, "\tstrcpy(%s, \"%s\");\n\0", 
			var_name, (assign_index)? assignment : "0");
		fwrite(buffer, size, sizeof(char), tmp);
	} 
	
	printf("Var_name: %s - Assign: %s Assign_size: %d\n", var_name, assignment, assign_index);	
	return ret;
}
Exemplo n.º 23
0
static pSlipObject slip_eval(pSlip gd, pSlipObject exp, pSlipEnvironment env)
{
	pSlipObject proc;
	pSlipObject args;

	tailcall:
	if (is_self_evaluating(exp) == S_TRUE)
	{
		return exp;
	}
	else if (is_variable(exp) == S_TRUE)
	{
		return lookup_variable_value(gd, exp, env);
	}
	else if (is_quoted(gd, exp) == S_TRUE)
	{
		return text_of_quotation(exp);
	}
	else if (is_assignment(gd, exp) == S_TRUE)
	{
		return eval_assignment(gd, exp, env);
	}
	else if (is_definition(gd, exp) == S_TRUE)
	{
		return eval_definition(gd, exp, env);
	}
	else if (is_if(gd, exp) == S_TRUE)
	{
		exp = is_true(gd, slip_eval(gd, if_predicate(exp), env)) == S_TRUE ? if_consequent(exp) : if_alternative(gd, exp);
		goto tailcall;
	}
	else if (is_lambda(gd, exp) == S_TRUE)
	{
		return s_NewCompoundProc(gd, lambda_parameters(exp), lambda_body(exp), env);
	}
	else if (is_begin(gd, exp) == S_TRUE)
	{
		exp = begin_actions(exp);
		while (!is_last_exp(gd, exp))
		{
			slip_eval(gd, first_exp(exp), env);
			exp = rest_exps(exp);
		}
		exp = first_exp(exp);
		goto tailcall;
	}
	else if (is_cond(gd, exp) == S_TRUE)
	{
		exp = cond_to_if(gd, exp);
		goto tailcall;
	}
	else if (is_let(gd, exp) == S_TRUE)
	{
		exp = let_to_application(gd, exp);
		goto tailcall;
	}
	else if (is_application(exp) == S_TRUE)
	{
		proc = slip_eval(gd, slip_operator(exp), env);
		if (proc == NULL)
			return gd->singleton_False;

		if (proc->type == eType_PRIMITIVE_PROC || proc->type == eType_COMPOUND_PROC)
		{
			args = list_of_values(gd, operands(exp), env);
			if (args == NULL)
				return gd->singleton_False;

			if (sIsObject_PrimitiveProc(proc) == S_TRUE)
			{
				return proc->data.prim_proc.func(gd, args);
			}
			else if (sIsObject_CompoundProc(proc) == S_TRUE)
			{
				env = setup_environment(gd, proc->data.comp_proc.env, proc->data.comp_proc.params, args);
				exp = make_begin(gd, proc->data.comp_proc.code);
				goto tailcall;
			}
			else
			{
				throw_error(gd, "unknown procedure type\n");
				return gd->singleton_False;
			}
		}
		else
			return proc;
	}
	else
	{
		throw_error(gd, "cannot eval unknown expression type\n");
		return NULL;
	}

	throw_error(gd, "what??\n");
	return NULL;
}
Exemplo n.º 24
0
static int target_x86_prepare_function(target_x86_t *target_x86, int n)
{
	int i;
	
	for(i = n; i < target_x86->array_line->count; i++)
	{
		char *line = (char *) array_get(target_x86->array_line, i);
		
		if( strncmp(line, "begin_function", 14) == 0 )
		{
			char cmd[STR_SIZE];
			char param[STR_SIZE];
			
			sscanf(line, "%s %s", cmd, param);
			
			target_x86->name = strdup(param);
		}
		
		if( strncmp(line, "param", 5) == 0 )
		{
			char cmd[STR_SIZE];
			char param[STR_SIZE];
		
			sscanf(line, "%s %s", cmd, param);
			target_x86_add_param_var(target_x86, param);
		}

		if( strncmp(line, "let", 3) == 0 )
		{
			char cmd[STR_SIZE];
			char param1[STR_SIZE];
			char param2[STR_SIZE];
			char param3[STR_SIZE];
			char op[STR_SIZE];
			int res;
			
			res = analye_op_let(line, param1, param2, param3, op);
			
			if( res == LET_BIN )
			{
				if( is_variable(param1) )
				{
					target_x86_add_local_var(target_x86, param1);
				}
					
				if( is_variable(param2) )
				{
					target_x86_add_local_var(target_x86, param2);
				}
				
				if( is_variable(param3) )
				{
					target_x86_add_local_var(target_x86, param3);
				}
			}
	
			if( res == LET_UN || res == LET_AS )
			{
				if( is_variable(param1) )
				{
					target_x86_add_local_var(target_x86, param1);
				}
					
				if( is_variable(param2) )
				{
					target_x86_add_local_var(target_x86, param2);
				}
			}
		}

		if( strncmp(line, "end_function", 12) == 0 )
		{
			break;
		}
	}	
}
Exemplo n.º 25
0
/***********************************************************************//**
 * @brief Print column information
 *
 * @param[in] chatter Chattiness (defaults to NORMAL).
 * @return String containing column information.
 *
 * @todo Format and cfitsio information is mainly for debugging. This could
 * vanish in a more stable version of the code, or it could be compiled-in
 * conditionally using a debug option. Alternatively, a higher chatter level
 * may be required to see this information.
 ***************************************************************************/
std::string GFitsTableCol::print(const GChatter& chatter) const
{
    // Initialise result string
    std::string result;

    // Continue only if chatter is not silent
    if (chatter != SILENT) {

        // Append formatted column name. Optionally add units
        if (unit().length() > 0) {
            result.append(gammalib::parformat(name()+" ("+unit()+")"));
        }
        else {
            result.append(gammalib::parformat(name()));
        }

        // Append column number. This will be "-" if the column does not exist
        // in the FITS file.
        if (m_colnum > 0) {
            result.append(gammalib::right(gammalib::str(m_colnum),4)+" ");
        }
        else {
            result.append(gammalib::right("[-]",4)+" ");
        }

        // Append loading information
        if (is_loaded()) {
            result.append("[loaded]     ");
        }
        else {
            result.append("[not loaded] ");
        }

        // Append format information
        result.append("["+tform_binary()+","+ascii_format()+"]");

        // Append dimensions (if available)
        if (!dim().empty()) {
    
            // Build TDIM string
            std::string value = "("+gammalib::str(dim()[0]);
            for (int k = 1; k < dim().size(); ++k) {
                value += ","+gammalib::str(dim()[k]);
            }
            value += ")";
        
            // Append
            result.append(" "+value);
        }

        // Append cfitsio information
        if (is_variable()) {
            result.append(" repeat=" + gammalib::str(repeat()));
            result.append(" width="  + gammalib::str(width()));
            result.append(" number=" + gammalib::str(number()));
            result.append(" length=" + gammalib::str(length()));
            result.append(" size="   + gammalib::str(m_size));
            result.append(" varlen=");
            if (m_varlen > 0) {
                result.append(gammalib::str(m_varlen));
            }
            else {
                result.append("undetermined");
            }
        }
        else {
            result.append(" repeat=" + gammalib::str(repeat()));
            result.append(" width="  + gammalib::str(width()));
            result.append(" number=" + gammalib::str(number()));
            result.append(" length=" + gammalib::str(length()));
            result.append(" size="   + gammalib::str(m_size));
        }

    } // endif: chatter was not silent

    // Compile option: print content
    #if defined(G_PRINT_CONTENT)
    if (chatter != SILENT) {

        // Fetch data if necessary
        if (!is_loaded()) fetch_data();

        // Loop over all rows
        for (int row = 0; row < length(); ++row) {
            result.append("\n");
            if (is_variable()) {
                result.append("start=");
                result.append(gammalib::str(m_rowstart[row]));
                result.append(":");
            }
            for (int inx = 0; inx < elements(row); ++inx) {
                result.append(" "+string(row, inx));
            }
        }
        if (is_variable()) {
            result.append("\nend=");
            result.append(gammalib::str(m_rowstart[length()]));
        }
    }
    #endif

    // Return result
    return result;
}
Exemplo n.º 26
0
Arquivo: cli.c Projeto: Agyar/lhnb
void *cli_docmd (char *commandline, void *data)
{
	int largc=0;
	char **largv;
	
	ItemT *titem = items;
	void *ret=data;
	cli_calllevel++;

	if (cli_precmd)
		cli_precmd (commandline);

	if (!inited) {
		init_cli ();
		titem = items;
		inited = 1;
	}

	largv=argv_tokenize(commandline);
	if(largv)largc=argc_of_argv(largv);

	if((!largc) || largv[0][0]=='\0' ){
		free(largv);
		return ret;
	}
	
	while (titem) {
		if (!strcmp (largv[0], titem->name)) {
			if (is_command (titem)) {

				ret=titem->func (largc,largv, data);
				
				if (cli_postcmd)
					cli_postcmd (commandline);
				cli_calllevel--;

				free(largv);
				return ret;
			} else if (is_variable (titem)) {
				if (largc==1) {
					if (titem->string) {
						cli_outfunf ("%s\t[%s]\t- %s\n", titem->name,
							  titem->string, titem->usage);
					} else if (titem->integer) {
						cli_outfunf ("%s\t[%i]\t- %s\n", titem->name,
							  *titem->integer, titem->usage);
					} else {
						cli_outfunf ("%s\tis a broken variable\n", titem->name);
					}
				} else {
					if (titem->integer)
						*titem->integer = atoi (largv[1]);
					if (titem->string)
						strcpy (titem->string, largv[1]);
					if (titem->func)
						ret=titem->func (largc,largv, data);
				}
				if (cli_postcmd)
					cli_postcmd (commandline);
				cli_calllevel--;

				free(largv);				
				return ret;
			}
		}
		titem = titem->next;
	}
	if(cli_unknown)
		cli_unknown(1,&commandline,data);
	if (cli_postcmd)
		cli_postcmd (commandline);
	cli_calllevel--;
	
	free(largv);
	return ret;
}
Exemplo n.º 27
0
bool Grammar::is_actor(const std::string& str) {
    return is_connection(str) || is_context(str) || is_type(str) || is_variable(str);
}
Exemplo n.º 28
0
int op_let_bin_math(target_x86_t *target_x86, char *str_dst, char *str_src_a, char *str_src_b, char *op)
{
	char line[STR_LINE_SIZE];
	var_t *var;

	if( is_variable(str_src_a) )
	{
		var_t *var;
		
		var = target_x86_get_var(target_x86, str_src_a);
		str_src_a = var->addr;
	}
	
	if( is_variable(str_src_b) )
	{
		var_t *var;
		
		var = target_x86_get_var(target_x86, str_src_b);
		str_src_b = var->addr;
	}
	
	var = target_x86_get_var(target_x86, str_dst);
	str_dst = var->addr;

	sprintf(line, "mov eax, %s", str_src_a);
	add_line(target_x86, TARGET_TEXT, line);
	
	sprintf(line, "mov ebx, %s", str_src_b);
	add_line(target_x86, TARGET_TEXT, line);

	if( strcmp(op, "+") == 0 )
	{
		add_line(target_x86, TARGET_TEXT, "add eax, ebx");
	}
	
	if( strcmp(op, "-") == 0 )
	{
		add_line(target_x86, TARGET_TEXT, "sub eax, ebx");
	}

	if( strcmp(op, "*") == 0 )
	{
		add_line(target_x86, TARGET_TEXT, "imul ebx");
	}
	
	if( strcmp(op, "/") == 0 )
	{
		add_line(target_x86, TARGET_TEXT, "mov edx, 0");
		add_line(target_x86, TARGET_TEXT, "div ebx");
	}

	if( strcmp(op, "%") == 0 )
	{
		add_line(target_x86, TARGET_TEXT, "mov edx, 0");
		add_line(target_x86, TARGET_TEXT, "div ebx");
		add_line(target_x86, TARGET_TEXT, "mov eax, edx");
	}

	if( strcmp(op, "and") == 0 )
	{
		add_line(target_x86, TARGET_TEXT, "mov ecx, 0");

		add_line(target_x86, TARGET_TEXT, "cmp eax, 1");
		sprintf(line, "jne .label_%d", target_x86->label_count);
		add_line(target_x86, TARGET_TEXT, line);
		
		add_line(target_x86, TARGET_TEXT, "cmp ebx, 1");
		sprintf(line, "jne .label_%d", target_x86->label_count);
		add_line(target_x86, TARGET_TEXT, line);

		add_line(target_x86, TARGET_TEXT, "mov ecx, 1");

		sprintf(line, ".label_%d:", target_x86->label_count);
		add_line(target_x86, TARGET_TEXT, line);
		
		add_line(target_x86, TARGET_TEXT, "mov eax, ecx");

		target_x86->label_count++;
	}

	if( strcmp(op, "or") == 0 )
	{
		add_line(target_x86, TARGET_TEXT, "mov ecx, 0");

		add_line(target_x86, TARGET_TEXT, "cmp eax, 1");
		sprintf(line, "je .label_%d", target_x86->label_count);
		add_line(target_x86, TARGET_TEXT, line);
		
		add_line(target_x86, TARGET_TEXT, "cmp ebx, 1");
		sprintf(line, "je .label_%d", target_x86->label_count);
		add_line(target_x86, TARGET_TEXT, line);

		sprintf(line, "jmp .label_%d", target_x86->label_count+1);
		add_line(target_x86, TARGET_TEXT, line);


		sprintf(line, ".label_%d:", target_x86->label_count);
		add_line(target_x86, TARGET_TEXT, line);
		
		add_line(target_x86, TARGET_TEXT, "mov ecx, 1");

		sprintf(line, ".label_%d:", target_x86->label_count+1);
		add_line(target_x86, TARGET_TEXT, line);

		add_line(target_x86, TARGET_TEXT, "mov eax, ecx");

		target_x86->label_count += 2;
	}

	if( strcmp(op, "==") == 0 ) op_let_bin_cmp(target_x86, "je");
	if( strcmp(op, "!=") == 0 ) op_let_bin_cmp(target_x86, "jne");
	if( strcmp(op, ">") == 0 ) op_let_bin_cmp(target_x86, "ja");
	if( strcmp(op, ">=") == 0 ) op_let_bin_cmp(target_x86, "jae");
	if( strcmp(op, "<") == 0 ) op_let_bin_cmp(target_x86, "jb");
	if( strcmp(op, "<=") == 0 ) op_let_bin_cmp(target_x86, "jbe");
	
	sprintf(line, "mov %s, eax", str_dst);
	add_line(target_x86, TARGET_TEXT, line);
	
	return 0;
}
Exemplo n.º 29
0
bool Grammar::is_keyword_message_argument(const std::string& str) {
    return is_object_value(str) || is_namespace(str) || is_variable(str);
}
Exemplo n.º 30
0
/***********************************************************************//**
 * @brief Returns TFORM code for binary table column
 *
 * Constructs the TFORM code for a binary table column, supporting
 * fixed-length and variable-length column types.
 ***************************************************************************/
std::string GFitsTableCol::tform_binary(void) const
{
    // Set type code string
    std::string typecode = "";
    switch (std::abs(type())) {
    case __TBIT:
        typecode = "X";
        break;
    case __TBYTE:
        typecode = "B";
        break;
    case __TLOGICAL:
        typecode = "L";
        break;
    case __TSTRING:
        // If there are substrings then add width of substring
        typecode = "A";
        if (repeat() > width()) {
            typecode.append(gammalib::str(width()));
        }
        break;
    case __TUSHORT:
        typecode = "U";
        break;
    case __TSHORT:
        typecode = "I";
        break;
    case __TULONG:
        typecode = "V";
        break;
    case __TLONG:
        typecode = "J";
        break;
    case __TFLOAT:
        typecode = "E";
        break;
    case __TLONGLONG:
        typecode = "K";
        break;
    case __TDOUBLE:
        typecode = "D";
        break;
    case __TCOMPLEX:
        typecode = "C";
        break;
    case __TDBLCOMPLEX:
        typecode = "M";
        break;
    default:
        break;
    }

    // Build TFORM code
    std::string tform;
    if (std::abs(type()) == __TSTRING) {
        tform.append(gammalib::str(repeat()));
    }
    else {
        tform.append(gammalib::str(number()));
    }
    if (is_variable()) {
        tform.append("P");
    }
    tform.append(typecode);
    if (is_variable() && m_varlen > 0) {
        tform.append("("+gammalib::str(m_varlen)+")");
    }
    
    // Return TFORM code
    return tform;
}