Beispiel #1
0
/* start parsing the input line                                          */
static void parse_input(cmds* cmd)
{
	cmds* elem;
	/* examine first token                                               */
	scan();
	if (lookahead.kind==END)
	{
		return;
	}

	/* create new command element, add it to the list, and parse it      */
	elem = cmd_new();
	if (cmd==NULL)
	{
		root=cmd=elem;
	}
	else
	{
		cmd->next = elem;
	}
	parse_pipe(elem, &elem->prog);

	/* parse next command from input                                     */
	parse_input(elem);
}
Beispiel #2
0
static char			parse_logic(t_token **list, t_node **tree)
{
	t_node			*node;
	t_token			*save;

	if ((node = ft_nodenew(LOGIC)) != NULL && parse_pipe(list, &node->left))
	{
		save = *list;
		if (ft_tokenstep(list) && is_logic((*list)->data, node)
			&& (*list)->type == SPECIAL)
		{
			if (!ft_tokenstep(list) || !parse_logic(list, &node->right))
				return (!ft_clear_tree(&node));
		}
		else
			*list = save;
		if (node->right)
			*tree = node;
		else
		{
			*tree = node->left;
			free(node->data);
			free(node);
		}
		return (1);
	}
	return (!ft_clear_tree(&node));
}
Beispiel #3
0
static void parse_pipe(cmds* cmd, prog_args* prog)
{
	prog_args* elem = NULL;
	/* parse a command                                                   */
	parse_cmd(cmd, prog);
    /* not in pipe?                                                      */
	if (lookahead.kind!=STROKE)
	{
		return;
	}
	/* builtin and starting pipe?                                        */
	if( !(cmd->kind==PROG || cmd->kind==PIPE) )
	{
		raise_error(PARSER_ILLEGAL_COMBINATION);
	}
	/* in pipe                                                           */
	cmd->kind=PIPE;
	/* output redirection?                                               */
	if (prog->output != NULL)
	{
		raise_error(PARSER_ILLEGAL_REDIRECTION);
	}
	/* skip pipe symbol and parse next command                           */
	scan();
	elem = prog_new();
	prog->next=elem;
	parse_pipe(cmd,elem);
}
Beispiel #4
0
static Command* parse_cmdline(char* buf)
{
    /*if (nr_token > 0) reset_parser();*/
    /*dump_tokens(buf);*/

    if (nr_token > 0) reset_parser();
    return parse_pipe(buf);
}
Beispiel #5
0
static struct ast_node *parse_pipe(struct parser_context *context)
{
    struct ast_node *left = parse_val(context);
    if(token_match(context, TOK_PIPE)) {
        token_read(context);
        struct ast_node *rdir = node_create(NODE_PIPE, sizeof(struct ast_node));
        node_add_child(rdir, left);
        node_add_child(rdir, parse_pipe(context));
        return rdir;
    }
    return left;
}
Beispiel #6
0
t_point init_def(Hash *h, char *name, int level)
{
	Function *f = new_Function(NIL, 0);
	t_point s = make_Func(f);
	HashMember *hm = NULL;
	if (name != NULL) {
		hm = add_string_Hash(h, name, s);
		hm->info = level;
		if (level == 0) gc_inc_immortal(s);
	}

	Hash *new_h = clone_Hash(h);
	char chars[MAX_NAME_LENGTH];
	int param_counter = level;

	// nacteni parametru funkce
	while (is_whitespace(chars[0] = read_char()));
	if (chars[0] != OPEN_TAG) ERROR(SYNTAX_ERROR);

	while (read_word(chars, 1))
		add_string_Hash(new_h, chars, pnew_Param(++param_counter));

	if (chars[0] != '\0') {
		add_string_Hash(new_h, chars, pnew_Param(++param_counter));

		// nastaveni parametru se zbytkem z volani
		if (chars[0] == REMAIN_PARAMS_TAG) {
			f->more_params = 1;
			param_counter--;
		}
	}

	// asociace funkce
	f->built_in = 0;
	f->params_count = param_counter;

	// vytvoreni tela funkce
	f->body.structure = parse_pipe(new_h, param_counter+f->more_params);
	free_Hash(new_h);

	return make_Func(f);
}
Beispiel #7
0
void play()
{
	gc_init();
	Hash *h = get_basic_hash();
	t_point parsed;
	char c;

	while ((c = read_char()) != EOF) {
		if (c == OPEN_TAG) {
			parsed = parse_pipe(h, 0);
			if (parsed != NIL && quiet) {
				resolve_Thunk(parsed);
			} else if (parsed != NIL && !quiet) {
				print_Symbol(parsed);
			} else if (prompt && !quiet)
				printf("OK\n");
			gc();
		}
	}

#ifdef DEBUG
	printf("\n\n"); gc_score();
#endif
}
Beispiel #8
0
t_point parse_pipe(Hash *h, int level)
{
	char chars[MAX_NAME_LENGTH];
	char *c = chars;
	int whitespaces = 1; // cetli jsme whitespace?
	int first = 1;
	int is_def = 0;
	int is_close_tag = 0;
	Cons all = {NIL, NIL};
	Cons *l = &all;
	t_point s = NIL;


	while ((c - chars) < MAX_NAME_LENGTH && (*c = read_char()) != EOF) {
		if (is_whitespace(*c) || (is_close_tag = (*c == CLOSE_TAG))) {
			if (whitespaces && !is_close_tag) continue;

			*c = '\0';
			whitespaces = 1;

			if (first && ((is_def = (strcmp(chars, "def") == 0))
						|| (strcmp(chars, "lambda") == 0)))
			{
				if (is_close_tag) ERROR(SYNTAX_ERROR);
				if (is_def) {
					// is def
					if (!read_word(chars, 0)) ERROR(SYNTAX_ERROR);
					init_def(h, chars, level);
				} else {
					// is lambda
					return kontext_params(init_def(h, NULL, level), level);
				}

				break;
			}

			// c is not empy
			if (c != chars) {
				l->b = create_token(h, chars, level);

				if (first) {
					l->a = l->b;
					l->b = NIL;
				} else {
					l->b = pnew_List(l->b);
					l = next(l);
				}
			}

			if (is_close_tag) break;

			c = chars;
			first = 0;
			continue;
		}

		// nacitame dal
		if (!whitespaces) { c++; continue; }

		switch (*c) {
			case '\'': s = parse_char(); break;
			case '"':  s = parse_string(); break;
			case OPEN_TAG: s = parse_pipe(h, level); break;

			default: c++; whitespaces = 0; continue;
		}

		if (s != NIL) {
			if (first) {
				l->a = s;
				l->b = NIL;
				first = 0;
			} else {
				l->b = pnew_List(s);
				l = next(l);
			}

			s = NIL;
		}
	}

	if ((c - chars) >= MAX_NAME_LENGTH)
		ERROR(SYNTAX_ERROR);
	
	if (all.b == NIL && !is_Func(all.a))
		return all.a;
	else
		return pnew_Thunk(all.a, get_Cons(all.b));
}