~lbnz/xr0

b209eef6a254843f63406d2cffff4a6f55bf4482 — Xr0 Team 6 months ago 23ee8f3 feat/program-1
parsed
2 files changed, 63 insertions(+), 39 deletions(-)

M src/ast/gram.y
M tests/3-program/100-lex/parse.x
M src/ast/gram.y => src/ast/gram.y +9 -7
@@ 373,9 373,9 @@ expression
	/*| expression ',' assignment_expression*/
	;

/*constant_expression*/
	/*: conditional_expression*/
	/*;*/
constant_expression
	: conditional_expression
	;

declaration
	/*: declaration_specifiers ';'*/


@@ 661,10 661,11 @@ statement
labelled_statement
	: identifier ':' statement
		{ $$ = ast_stmt_create_labelled(lexloc(), $1, $3); }
	| CASE constant_expression ':' statement
		{ $$ = ast_stmt_create_nop(lexloc()); }
	| DEFAULT ':' statement
		{ $$ = ast_stmt_create_nop(lexloc()); }
	;
	/*| CASE constant_expression ':' statement*/
	/*| DEFAULT ':' statement*/
	/*;*/

compound_statement
	: '{' block '}' { $$ = $2; }


@@ 735,7 736,8 @@ selection_statement
			); 
			$$ = ast_stmt_create_sel(lexloc(), false, $3, $5, _else);
		}
	// | SWITCH '(' expression ')' statement
	| SWITCH '(' expression ')' statement
		{ $$ = ast_stmt_create_nop(lexloc()); }
	;

for_some

M tests/3-program/100-lex/parse.x => tests/3-program/100-lex/parse.x +54 -32
@@ 431,10 431,8 @@ struct tknameresult {
	char *pos;
};

typedef struct tknameresult (*nameparser)(char *);

nameparser
choose_nameparser(char *pos);
struct tknameresult
parse_name(char *pos);

struct stringresult
parse_action(char *input);


@@ 442,12 440,16 @@ parse_action(char *input);
struct tokenresult
parse_token(char *pos)
{
	struct tknameresult nameres = choose_nameparser(pos)(pos);
	struct stringresult actionres = parse_action(skiplinespace(nameres.pos));
	return (struct tokenresult){
		token_create(nameres.isliteral, nameres.name, actionres.s),
		actionres.pos,
	};
	struct tknameresult nameres;
	struct stringresult actionres;
	struct tokenresult res;

	nameres = parse_name(pos);
	actionres = parse_action(skiplinespace(nameres.pos));

	res.tk = token_create(nameres.isliteral, nameres.name, actionres.s);
	res.pos = actionres.pos;
	return res;
}

struct tknameresult


@@ 459,75 461,95 @@ parse_token_literal(char *input);
struct tknameresult
parse_token_pattern(char *pos);

nameparser
choose_nameparser(char *pos)
struct tknameresult
parse_name(char *pos)
{
	switch (*pos) {
	case '{':
		return &parse_token_id;
		return parse_token_id(pos);
	case '"':
		return &parse_token_literal;
		return parse_token_literal(pos);
	default:
		return &parse_token_pattern;
		return parse_token_pattern(pos);
	}
}

struct tknameresult
parse_token_id(char *pos)
{
	char *id = parse_id(++pos); /* skip '{' */
	char *id;
	struct tknameresult res;

	id = parse_id(++pos); /* skip '{' */
	pos += strlen(id);
	if (*pos != '}') {
		fprintf(stderr, "token id must end in '}' but has '%.*s\n'", 5,
			pos);
		exit(1);
	}
	return (struct tknameresult){false, id, pos + 1}; /* '}' */

	res.isliteral = false;
	res.name = id;
	res.pos = pos + 1; /* '}' */
	return res;
}

struct tknameresult
parse_token_literal(char *input)
{
	char *pos;
	struct tknameresult res;

	input++; /* skip '"' */
	char *pos = input;
	pos = input;
	for (pos++; *pos != '"'; pos++) {}
	return (struct tknameresult){
		true,
		substr(input, pos - input),
		pos + 1,
	};

	res.isliteral = true;
	res.name = substr(input, pos - input);
	res.pos = pos + 1;
	return res;
}

struct tknameresult
parse_token_pattern(char *pos)
{
	char *id = parse_id(pos);
	return (struct tknameresult){true, id, pos + strlen(id)};
	char *id;
	struct tknameresult res;

	id = parse_id(pos);

	res.isliteral = true;
	res.name = id;
	res.pos = pos + strlen(id);
	return res;
}

struct stringresult
parse_action(char *input)
{
	char *pos;
	struct stringresult res;

	if (*input != '{') {
		fprintf(stderr, "action must begin with '{' but has '%.*s\n",
			10, input);
		exit(1);
	}
	input++; /* skip '{' */
	char *pos = input;
	pos = input;
	for (; *pos != '}'; pos++) {}
	return (struct stringresult){
		substr(input, pos - input),
		pos + 1,
	};

	res.s = substr(input, pos - input);
	res.pos = pos + 1;
	return res;
}

char *
parse_toeof(char *input)
{
	char *s = input;
	while (*s != '\0') {
	char *s;

	for (s = input; *s != '\0'; 0) {
		s++;
	}
	return substr(input, s - input);