~pixelherodev/zyg

476c460b87ebf4809ae38a37dcc93f8af6530f95 — Noam Preil 5 months ago 5890d0e
[parser] minor optimizations
1 files changed, 33 insertions(+), 44 deletions(-)

M parser.c
M parser.c => parser.c +33 -44
@@ 146,10 146,8 @@ parser_dedup_node_tag(uint32_t **list, uint32_t tag)
			return (*list)[i];
	// Not yet present!
	i = c3append(&ctx.tree, tag, 0);
	if(i == -1)
		FATAL("Out of memory", 0);
	if(!stb_sb_push_u32(list, i))
		return -1;
	if(i == -1 || !stb_sb_push_u32(list, i))
		OOM();
	return i;
}



@@ 184,7 182,7 @@ p_test_decl(void)
		FATAL("Parsing test block failed", 0);
	index = c3append(&ctx.tree, C3_TEST, 2, name, block);
	if(index == -1)
		FATAL("Out of memory", 0);
		OOM();
	return index;
}



@@ 197,8 195,8 @@ p_tl_comptime(void)
	if(block == -1)
		return -1;
	index = c3append(&ctx.tree, C3_COMPTIME, 1, block);
	if(index == 0)
		FATAL("Out of memory", 0);
	if(index == -1)
		OOM();
	return index;
}



@@ 353,9 351,9 @@ p_init(void)
		expr = p_expr();
		field = c3append(&ctx.tree, C3_FIELD_INIT, 2, ident, expr);
		if(field == -1)
			FATAL("ICE: OOM", 0);
			OOM();
		if(!stb_sb_push_u32(&fields, field))
			FATAL("ICE: OOM", 0);
			OOM();
		if(parser_is(1, C3_COMMA)){
			parser_advance();
			if(parser_is(1, C3_DOT))


@@ 369,7 367,7 @@ p_init(void)
	expr = c3appendarr(&ctx.tree, C3_INITIALIZED, stb_sb_count(fields), fields);
	stb_sb_free(fields);
	if(expr == -1)
		FATAL("ICE: OOM", 0);
		OOM();
	return expr;
}



@@ 405,15 403,14 @@ p_asm(void)
	if(arg == -1)
		FATAL("failed to read asm string", 0);
	if(!stb_sb_push_u32(&kids, arg))
		FATAL("ICE: OOM", 0);
		OOM();
	parser_advance();
	while(parser_is(0, C3_COMMA)){
		parser_advance();
	while(parser_is_consume(C3_COMMA)){
		arg = p_expr();
		if(arg == -1)
			FATAL("Failed to parse identifier", 0);
		if(!stb_sb_push_u32(&kids, arg))
			FATAL("ICE: OOM", 0);
			OOM();
		parser_advance();
	}
	if(!parser_is(0, C3_RPAREN)){


@@ 431,32 428,32 @@ p_asm(void)
static uint32_t
p_primary_expr(void)
{
	if(parser_is(0, C3_ASM))
		return p_asm();
	if(parser_is(0, C3_KW_IF))
		FATAL("TODO: if", 0);
	if(parser_is(0, C3_BREAK))
		FATAL("TODO: break", 0);
	if(parser_is(0, C3_COMPTIME))
		FATAL("TODO: comptime expr", 0);
	if(parser_is(0, C3_NOSUSPEND))
		FATAL("TODO: nosuspend expr", 0);
	if(parser_is(0, C3_INLINE) || parser_is(0, C3_WHILE) || parser_is(0, C3_FOR))
		return p_loop();
	if(parser_is(0, C3_CONTINUE))
		FATAL("TODO: continue", 0);
	if(parser_is(0, C3_BREAK))
		FATAL("TODO: break", 0);
	if(parser_is(0, C3_RESUME))
		FATAL("TODO: resume", 0);
	if(parser_is(0, C3_IDENT) && parser_is(1, C3_COLON))
		FATAL("TODO: labelled loop", 0);
	if(parser_is(0, C3_RETURN))
		return p_return();
	if(parser_is(0, C3_INLINE) || parser_is(0, C3_NOINLINE) || parser_is(0, C3_FN))
	if(parser_is(0, C3_FN))
		return p_fnexpr();
	if(parser_is(0, C3_INLINE) || parser_is(0, C3_WHILE) || parser_is(0, C3_FOR))
		return p_loop();
	if(parser_is(0, C3_LBRACE))
		return p_block();
	if(parser_is(0, C3_LBRACE))
		FATAL("TODO: curlysuffixexpr '%s'", c3tagstr(ctx.tokens.tags[ctx.i]));
	if(parser_is(0, C3_ASM))
		return p_asm();
	if(parser_is(0, C3_COMPTIME))
		FATAL("TODO: comptime expr", 0);
	if(parser_is(0, C3_NOSUSPEND))
		FATAL("TODO: nosuspend expr", 0);
	return p_curly_suffix_expr();
}



@@ 813,12 810,8 @@ p_container(uint16_t tag)
			ERROR("failed to parse %s", c3tagstr(ctx.tokens.tags[init_token]));
			return -1;
		}
		if(kid != 0){
			if(!stb_sb_push_u32(&kids, kid)){
	    			ERROR("Out of memory", 0);
	    			return 0;
			}
		}
		if(kid != 0 && !stb_sb_push_u32(&kids, kid))
			OOM();
		parser_advance();
	}
	root = c3appendarr(&ctx.tree, tag, stb_sb_count(kids), kids);


@@ 902,10 895,10 @@ p_primary_type_expr(void)
{
	if(parser_is(0, C3_IDENT))
		return p_ident();
	if(parser_is(0, C3_FALSE))
		return parser_dedup_node_tag(&ctx.accel.values, C3_VALUE_FALSE);
	if(parser_is(0, C3_TRUE))
		return parser_dedup_node_tag(&ctx.accel.values, C3_VALUE_TRUE);
	if(parser_is(0, C3_FALSE))
		return parser_dedup_node_tag(&ctx.accel.values, C3_VALUE_FALSE);
	if(parser_is(0, C3_KEYWORD_NULL))
		return parser_dedup_node_tag(&ctx.accel.values, C3_VALUE_NULL);
	if(parser_is(0, C3_NORETURN))


@@ 920,6 913,8 @@ p_primary_type_expr(void)
		return parser_dedup_node_tag(&ctx.accel.types, C3_TYPE_U32);
	if(parser_is(0, C3_DISCARD))
		return parser_dedup_node_tag(&ctx.accel.misc, C3_DISCARD);
	if(parser_is(0, C3_IMPORT))
		return p_import();
	if(parser_is(0, C3_BUILTIN_PANIC)){
		parser_advance();
		return c3append(&ctx.tree, C3_BUILTIN_PANIC, 1, p_fncall_args());


@@ 1033,7 1028,7 @@ p_type_expr(void)
		ERROR("failed to read error_union_expr", 0);
		return -1;
	}
	return parser_apply_prefix(expr, ops);
	return ops == NULL ? expr : parser_apply_prefix(expr, ops);
}

static uint32_t


@@ 1117,10 1112,8 @@ p_fnproto(void)
			ERROR("failed to read fn paramdecl",0);
			return -1;
		}
		if(!stb_sb_push_u32(&params, child)){
			ERROR("ICE: OOM",0);
			return -1;
		}
		if(!stb_sb_push_u32(&params, child))
			OOM();
		parser_advance();
		if(parser_is(0, C3_COMMA)){
			more = 1;


@@ 1189,8 1182,6 @@ p_expr(void)
{
	uint32_t expr;
	// TODO: don't special-case these here
	if(parser_is(0, C3_IMPORT))
		return p_import();
	expr = p_boolor_expr();
	return expr;
}


@@ 1369,10 1360,8 @@ static int
parser_gen_types(void)
{
	ctx.tree.graphs.type = c3appendarr(&ctx.tree, C3_CONTEXTUAL, stb_sb_count(ctx.accel.types), ctx.accel.types);
	if(ctx.tree.graphs.type == -1){
		ERROR("ICE: OOM", 0);
		return 0;
	}
	if(ctx.tree.graphs.type == -1)
		OOM();
	return 1;
}