~lbnz/xr0

d4526054916924e4cbc5716b042bc788649f1844 — Claude Betz 5 months ago 6f4c3ca
checking for segfault
M include/ast.h => include/ast.h +13 -2
@@ 4,6 4,19 @@
#include "util.h"

struct ast_expr;
struct lexememarker;

struct lexememarker *
ast_expr_getstart(struct ast_expr *);

struct lexememarker *
ast_expr_getend(struct ast_expr *);

struct ast_expr *
ast_expr_setstart(struct ast_expr *, struct lexememarker *);

struct ast_expr *
ast_expr_setend(struct ast_expr *, struct lexememarker *);

struct ast_expr *
ast_expr_identifier_create(char *);


@@ 221,8 234,6 @@ enum ast_jump_kind {

struct ast_stmt;

struct lexememarker;

struct lexememarker *
ast_stmt_lexememarker(struct ast_stmt *);


M src/0v/ast/expr/expr.c => src/0v/ast/expr/expr.c +69 -12
@@ 5,12 5,39 @@

#include "ast.h"
#include "ext.h"
#include "lex.h"
#include "expr.h"
#include "math.h"
#include "state.h"
#include "stmt/stmt.h"
#include "util.h"

struct lexememarker *
ast_expr_getstart(struct ast_expr *expr)
{
	return expr->start;
}

struct lexememarker *
ast_expr_getend(struct ast_expr *expr)
{
	return expr->end;
}

struct ast_expr *
ast_expr_setstart(struct ast_expr *expr, struct lexememarker *loc)
{
	expr->start = lexememarker_copy(loc);
	return expr;
}

struct ast_expr *
ast_expr_setend(struct ast_expr *expr, struct lexememarker *loc)
{
	expr->end = lexememarker_copy(loc);
	return expr;
}

static struct ast_expr *
ast_expr_create()
{


@@ 663,6 690,12 @@ char *
ast_expr_str(struct ast_expr *expr)
{
	struct strbuilder *b = strbuilder_create();
	strbuilder_printf(
		b,
		"[start: %s, end: %s]: ",
		lexememarker_str(expr->start),
		lexememarker_str(expr->end)
	);
	switch (expr->kind) {
	case EXPR_IDENTIFIER:
		strbuilder_printf(b, expr->u.string);


@@ 709,55 742,79 @@ ast_expr_str(struct ast_expr *expr)
struct ast_expr *
ast_expr_copy(struct ast_expr *expr)
{
	struct ast_expr *copy;
	
	assert(expr);
	switch (expr->kind) {
	case EXPR_IDENTIFIER:
		return ast_expr_identifier_create(dynamic_str(expr->u.string));
		copy = ast_expr_identifier_create(dynamic_str(expr->u.string));
		break;
	case EXPR_CONSTANT:
		return expr->u.constant.ischar
		copy = expr->u.constant.ischar
			? ast_expr_constant_create_char(expr->u.constant.constant)
			: ast_expr_constant_create(expr->u.constant.constant);
		break;
	case EXPR_STRING_LITERAL:
		return ast_expr_literal_create(dynamic_str(expr->u.string));
		copy = ast_expr_literal_create(dynamic_str(expr->u.string));
		break;
	case EXPR_BRACKETED:
		return ast_expr_bracketed_create(ast_expr_copy(expr->root));
		copy = ast_expr_bracketed_create(ast_expr_copy(expr->root));
		break;
	case EXPR_CALL:
		return ast_expr_copy_call(expr);
		copy = ast_expr_copy_call(expr);
		break;
	case EXPR_INCDEC:
		return ast_expr_incdec_create(
		copy = ast_expr_incdec_create(
			ast_expr_copy(expr->root),
			expr->u.incdec.inc,
			expr->u.incdec.pre
		);
		break;
	case EXPR_STRUCTMEMBER:
		return ast_expr_member_create(
		copy = ast_expr_member_create(
			ast_expr_copy(expr->root), dynamic_str(expr->u.string)
		);
		break;
	case EXPR_UNARY:
		return ast_expr_unary_create(
		copy = ast_expr_unary_create(
			ast_expr_copy(expr->root),
			expr->u.unary_op
		);
		break;
	case EXPR_BINARY:
		return ast_expr_binary_create(
		copy = ast_expr_binary_create(
			ast_expr_copy(expr->u.binary.e1),
			expr->u.binary.op,
			ast_expr_copy(expr->u.binary.e2)
		);
		break;
	case EXPR_ASSIGNMENT:
		return ast_expr_assignment_create(
		copy = ast_expr_assignment_create(
			ast_expr_copy(expr->root),
			ast_expr_copy(expr->u.assignment_value)
		);
		break;
	case EXPR_ISDEALLOCAND:
		return ast_expr_isdeallocand_create(
		copy = ast_expr_isdeallocand_create(
			ast_expr_copy(expr->root)
		);
		break;
	case EXPR_ARBARG:
		return ast_expr_arbarg_create();
		copy = ast_expr_arbarg_create();
		break;
	default:
		assert(false);
	}

	assert(copy);
	copy->start = expr->start
		? lexememarker_copy(expr->start)
		: NULL;
	copy->end = expr->end
		? lexememarker_copy(expr->end)
		: NULL;

	return copy;
}

enum ast_expr_kind

M src/0v/ast/expr/expr.h => src/0v/ast/expr/expr.h +2 -0
@@ 63,6 63,8 @@ struct ast_expr {
		} binary;
		struct ast_expr *assignment_value;
	} u;

	struct lexememarker *start, *end;
};

enum ast_expr_kind

M src/0v/ast/expr/verify.c => src/0v/ast/expr/verify.c +1 -0
@@ 118,6 118,7 @@ expr_isdeallocand_rangedecide(struct ast_expr *expr, struct ast_expr *lw,
struct error *
ast_expr_exec(struct ast_expr *expr, struct state *state)
{
	printf("expr: %s\n", ast_expr_str(expr));
	struct result *res = ast_expr_eval(expr, state);
	if (result_iserror(res)) {
		return result_as_error(res);

M src/0v/ast/gram.y => src/0v/ast/gram.y +164 -55
@@ 14,6 14,8 @@

extern char *yytext;

struct lexememarker *start;

int
yylex();



@@ 204,36 206,51 @@ primary_expression
	;

postfix_expression
	: primary_expression
	: primary_expression {
		struct lexememarker *end = lexloc();
		$$ = ast_expr_setend(ast_expr_setstart($1, start), end);
	}
	| postfix_expression '[' expression ']' {
		$$ = ast_expr_unary_create(
			ast_expr_binary_create($1, BINARY_OP_ADDITION, $3),
			UNARY_OP_DEREFERENCE
		$$ = ast_expr_setend(
			ast_expr_unary_create(
				ast_expr_binary_create($1, BINARY_OP_ADDITION, $3),
				UNARY_OP_DEREFERENCE
			),
			lexloc()
		);
	}
	| postfix_expression '(' ')'
		{ $$ = ast_expr_call_create($1, 0, NULL); }
		{ $$ = ast_expr_setend(ast_expr_call_create($1, 0, NULL), lexloc()); }
	| postfix_expression '(' argument_expression_list ')'
		{ $$ = ast_expr_call_create($1, $3.n, $3.expr); }
		{ $$ = ast_expr_setend(ast_expr_call_create($1, $3.n, $3.expr), lexloc()); }
	| postfix_expression '.' identifier
		{ $$ = ast_expr_member_create($1, $3); }
		{ $$ = ast_expr_setend(ast_expr_member_create($1, $3), lexloc()); }
	| postfix_expression PTR_OP identifier {
		$$ = ast_expr_member_create(
			ast_expr_unary_create(
				ast_expr_binary_create(
					$1,
					BINARY_OP_ADDITION,
					ast_expr_constant_create(0)
		$$ = ast_expr_setend(
			ast_expr_member_create(
				ast_expr_unary_create(
					ast_expr_binary_create(
						$1,
						BINARY_OP_ADDITION,
						ast_expr_constant_create(0)
					),
					UNARY_OP_DEREFERENCE
				),
				UNARY_OP_DEREFERENCE
				$3
			),
			$3
			lexloc()
		);
	}
	| postfix_expression INC_OP {
		$$ = ast_expr_setend(
			ast_expr_incdec_create($1, true, false), lexloc()
		);
	}
	| postfix_expression DEC_OP {
		$$ = ast_expr_setend(
			ast_expr_incdec_create($1, false, false), lexloc()
		);
	}
	| postfix_expression INC_OP
		{ $$ = ast_expr_incdec_create($1, true, false); }
	| postfix_expression DEC_OP
		{ $$ = ast_expr_incdec_create($1, false, false); }
	;

argument_expression_list


@@ 244,23 261,43 @@ argument_expression_list
	;

isdeallocand_expression
	: postfix_expression 
	: postfix_expression {
		struct lexememarker *end = lexloc();
		$$ = ast_expr_setend(ast_expr_setstart($1, start), end);
		start = lexloc();
	}
	| ISDEALLOCAND_OP isdeallocand_expression {
		$$ = ast_expr_isdeallocand_create($2);
		$$ = ast_expr_setend(ast_expr_isdeallocand_create($2), lexloc());
	}
	;

unary_expression
	: isdeallocand_expression
	| INC_OP unary_expression
		{ $$ = ast_expr_incdec_create($2, true, true); }
	| DEC_OP unary_expression
		{ $$ = ast_expr_incdec_create($2, false, true); }
	| unary_operator cast_expression
		{ $$ = ast_expr_unary_create($2, $1); }
	: isdeallocand_expression {
		struct lexememarker *end = lexloc();
		$$ = ast_expr_setend(ast_expr_setstart($1, start), end);
		start = lexloc();
	}
	| INC_OP unary_expression {
		$$ = ast_expr_setend(
			ast_expr_incdec_create($2, true, true), lexloc()
		);
	}
	| DEC_OP unary_expression {
		$$ = ast_expr_setend(
			ast_expr_incdec_create($2, false, true), lexloc()
		);
	}
	| unary_operator cast_expression {
		$$ = ast_expr_setend(
			ast_expr_unary_create($2, $1), lexloc()
		);
	}
	/*| SIZEOF unary_expression*/
	| SIZEOF '(' type_name ')'
		{ $$ = ast_expr_constant_create(1); /* XXX */ }
	| SIZEOF '(' type_name ')' {
		$$ = ast_expr_setend(
			ast_expr_constant_create(1), lexloc()
		); /* XXX */
	}
	;

unary_operator


@@ 273,27 310,49 @@ unary_operator
	;

cast_expression
	: unary_expression
	: unary_expression {
		struct lexememarker *end = lexloc();
		$$ = ast_expr_setend(ast_expr_setstart($1, start), end);
		start = lexloc();
	}
	/*| '(' type_name ')' cast_expression*/
	;

multiplicative_expression
	: cast_expression
	: cast_expression {
		struct lexememarker *end = lexloc();
		$$ = ast_expr_setend(ast_expr_setstart($1, start), end);
		start = lexloc();
	}
	| multiplicative_expression '*' cast_expression
	/*| multiplicative_expression '/' cast_expression*/
/*| multiplicative_expression '%' cast_expression*/
;

additive_expression
	: multiplicative_expression
	| additive_expression '+' multiplicative_expression
		{ $$ = ast_expr_binary_create($1, BINARY_OP_ADDITION, $3); }
	| additive_expression '-' multiplicative_expression
		{ $$ = ast_expr_binary_create($1, BINARY_OP_SUBTRACTION, $3); }
	: multiplicative_expression {
		struct lexememarker *end = lexloc();
		$$ = ast_expr_setend(ast_expr_setstart($1, start), end);
		start = lexloc();
	}
	| additive_expression '+' multiplicative_expression {
		$$ = ast_expr_setend(
			ast_expr_binary_create($1, BINARY_OP_ADDITION, $3), lexloc()
		);
	}
	| additive_expression '-' multiplicative_expression {
		$$ = ast_expr_setend(
			ast_expr_binary_create($1, BINARY_OP_SUBTRACTION, $3), lexloc()
		);
	}
	;

shift_expression
	: additive_expression
	: additive_expression {
		struct lexememarker *end = lexloc();
		$$ = ast_expr_setend(ast_expr_setstart($1, start), end);
		start = lexloc();
	}
	| shift_expression LEFT_OP additive_expression
	| shift_expression RIGHT_OP additive_expression
	;


@@ 311,9 370,16 @@ justification
	;

relational_expression
	: shift_expression
	| relational_expression relational_operator justification shift_expression
		{ $$ = ast_expr_binary_create($1, $2, $4); }
	: shift_expression {
		struct lexememarker *end = lexloc();
		$$ = ast_expr_setend(ast_expr_setstart($1, start), end);
		start = lexloc();
	}
	| relational_expression relational_operator justification shift_expression {
		$$ = ast_expr_setend(
			ast_expr_binary_create($1, $2, $4), lexloc()
		);
	}
	;

equality_operator


@@ 322,45 388,82 @@ equality_operator
	;

equality_expression
	: relational_expression
	| equality_expression equality_operator justification relational_expression
		{ $$ = ast_expr_binary_create($1, $2, $4); }
	: relational_expression {
		struct lexememarker *end = lexloc();
		$$ = ast_expr_setend(ast_expr_setstart($1, start), end);
		start = lexloc();
	}
	| equality_expression equality_operator justification relational_expression {
		$$ = ast_expr_setend(
			ast_expr_binary_create($1, $2, $4), lexloc()
		);
	}
	;

and_expression
	: equality_expression
	: equality_expression {
		struct lexememarker *end = lexloc();
		$$ = ast_expr_setend(ast_expr_setstart($1, start), end);
		start = lexloc();
	}
	/*| and_expression '&' equality_expression*/
	;

exclusive_or_expression
	: and_expression
	: and_expression {
		struct lexememarker *end = lexloc();
		$$ = ast_expr_setend(ast_expr_setstart($1, start), end);
		start = lexloc();
	}
	/*| exclusive_or_expression '^' and_expression*/
	;

inclusive_or_expression
	: exclusive_or_expression
	: exclusive_or_expression {
		struct lexememarker *end = lexloc();
		$$ = ast_expr_setend(ast_expr_setstart($1, start), end);
		start = lexloc();
	}
	/*| inclusive_or_expression '|' exclusive_or_expression*/
	;

logical_and_expression
	: inclusive_or_expression
	: inclusive_or_expression {
		struct lexememarker *end = lexloc();
		$$ = ast_expr_setend(ast_expr_setstart($1, start), end);
		start = lexloc();
	}
	| logical_and_expression AND_OP inclusive_or_expression
	;

logical_or_expression
	: logical_and_expression
	: logical_and_expression {
		struct lexememarker *end = lexloc();
		$$ = ast_expr_setend(ast_expr_setstart($1, start), end);
		start = lexloc();
	}
	| logical_or_expression OR_OP logical_and_expression
	;

conditional_expression
	: logical_or_expression
	: logical_or_expression {
		struct lexememarker *end = lexloc();
		$$ = ast_expr_setend(ast_expr_setstart($1, start), end);
		start = lexloc();
	}

	/*| logical_or_expression '?' expression ':' conditional_expression*/
	;

assignment_expression
	: conditional_expression
	| unary_expression assignment_operator assignment_expression
		{ $$ = ast_expr_assignment_create($1, $3); }
	: conditional_expression {
		struct lexememarker *end = lexloc();
		$$ = ast_expr_setend(ast_expr_setstart($1, start), end);
		start = lexloc();
	}
	| unary_expression assignment_operator assignment_expression {
		$$ = ast_expr_setend(ast_expr_assignment_create($1, $3), lexloc());
	}
	;

assignment_operator


@@ 378,7 481,11 @@ assignment_operator
	;

expression
	: assignment_expression
	: assignment_expression {
		struct lexememarker *end = lexloc();
		$$ = ast_expr_setend(ast_expr_setstart($1, start), end);
		start = lexloc();
	}
	/*| expression ',' assignment_expression*/
	;



@@ 661,7 768,9 @@ statement
	: labelled_statement
	| compound_statement
		{ $$ = ast_stmt_setlexememarker(ast_stmt_create_compound($1), lexloc()); }
	| expression_statement
	| expression_statement {
		start = lexloc();
	}
	| selection_statement
	| iteration_statement
	| jump_statement

M src/0v/ast/stmt/stmt.c => src/0v/ast/stmt/stmt.c +1 -0
@@ 637,6 637,7 @@ ast_stmt_copy(struct ast_stmt *stmt)
		assert(false);
	}

	assert(copy);
	copy->loc = stmt->loc
		? lexememarker_copy(stmt->loc)
		: NULL;