hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

expr.ha (35187B)


      1 // SPDX-License-Identifier: MPL-2.0
      2 // (c) Hare authors <https://harelang.org>
      3 
      4 use hare::ast;
      5 use hare::lex;
      6 use hare::lex::{ltok};
      7 use math;
      8 use strings;
      9 use types;
     10 
     11 // Parses an expression.
     12 export fn expr(lexer: *lex::lexer) (ast::expr | error) = {
     13 	const loc = lex::mkloc(lexer);
     14 
     15 	// All assignment-op tokens
     16 	const atoks: []ltok = [
     17 		ltok::EQUAL, ltok::BANDEQ, ltok::BOREQ, ltok::BXOREQ,
     18 		ltok::DIVEQ, ltok::LANDEQ, ltok::LOREQ, ltok::LXOREQ,
     19 		ltok::LSHIFTEQ, ltok::MINUSEQ, ltok::MODEQ, ltok::PLUSEQ,
     20 		ltok::RSHIFTEQ, ltok::TIMESEQ,
     21 	];
     22 
     23 	const ex = match (peek(lexer, ltok::IF, ltok::FOR, ltok::BREAK,
     24 		ltok::CONTINUE, ltok::RETURN, ltok::YIELD)?) {
     25 	case void =>
     26 		yield binarithm(lexer, void, 0)?;
     27 	case let tok: lex::token =>
     28 		yield switch (tok.0) {
     29 		case ltok::IF =>
     30 			yield if_expr(lexer)?;
     31 		case ltok::FOR =>
     32 			yield for_expr(lexer)?;
     33 		case ltok::BREAK, ltok::CONTINUE, ltok::RETURN =>
     34 			yield control(lexer)?;
     35 		case ltok::YIELD =>
     36 			yield yield_expr(lexer)?;
     37 		case => abort(); // Invariant
     38 		};
     39 	};
     40 
     41 	const tok = match (try(lexer, atoks...)?) {
     42 	case let tok: lex::token =>
     43 		yield tok;
     44 	case =>
     45 		return ex;
     46 	};
     47 
     48 	const is_obj_selector = match (ex.expr) {
     49 	case (ast::access_expr | ast::slice_expr) =>
     50 		yield true;
     51 	case let ex: ast::unarithm_expr =>
     52 		yield ex.op == ast::unarithm_op::DEREF;
     53 	case =>
     54 		yield false;
     55 	};
     56 	synassert(lex::mkloc(lexer), is_obj_selector,
     57 		"Expected an object-selector, pointer dereference, or slice for assignment target")?;
     58 	const ex = ast::assign_expr {
     59 		op = switch (tok.0) {
     60 		case ltok::EQUAL =>
     61 			yield void;
     62 		case ltok::BANDEQ =>
     63 			yield ast::binarithm_op::BAND;
     64 		case ltok::BOREQ =>
     65 			yield ast::binarithm_op::BOR;
     66 		case ltok::BXOREQ =>
     67 			yield ast::binarithm_op::BXOR;
     68 		case ltok::DIVEQ =>
     69 			yield ast::binarithm_op::DIV;
     70 		case ltok::LANDEQ =>
     71 			yield ast::binarithm_op::LAND;
     72 		case ltok::LOREQ =>
     73 			yield ast::binarithm_op::LOR;
     74 		case ltok::LSHIFTEQ =>
     75 			yield ast::binarithm_op::LSHIFT;
     76 		case ltok::LXOREQ =>
     77 			yield ast::binarithm_op::LXOR;
     78 		case ltok::MINUSEQ =>
     79 			yield ast::binarithm_op::MINUS;
     80 		case ltok::MODEQ =>
     81 			yield ast::binarithm_op::MODULO;
     82 		case ltok::PLUSEQ =>
     83 			yield ast::binarithm_op::PLUS;
     84 		case ltok::RSHIFTEQ =>
     85 			yield ast::binarithm_op::RSHIFT;
     86 		case ltok::TIMESEQ =>
     87 			yield ast::binarithm_op::TIMES;
     88 		case => abort(); // unreachable
     89 		},
     90 		object = alloc(ex),
     91 		value = alloc(expr(lexer)?),
     92 	};
     93 
     94 	return ast::expr {
     95 		start = loc,
     96 		end = lex::prevloc(lexer),
     97 		expr = ex,
     98 	};
     99 };
    100 
    101 fn assert_expr(lexer: *lex::lexer, is_static: bool) (ast::expr | error) = {
    102 	const tok = want(lexer, ltok::ABORT, ltok::ASSERT)?;
    103 
    104 	let expr = switch (tok.0) {
    105 	case ltok::ABORT =>
    106 		want(lexer, ltok::LPAREN)?;
    107 		const msg: nullable *ast::expr =
    108 			match (peek(lexer, ltok::RPAREN)?) {
    109 			case lex::token =>
    110 				yield null;
    111 			case =>
    112 				yield alloc(expr(lexer)?);
    113 			};
    114 		want(lexer, ltok::RPAREN)?;
    115 
    116 		yield ast::assert_expr {
    117 			cond      = null,
    118 			message   = msg,
    119 			is_static = is_static,
    120 		};
    121 	case ltok::ASSERT =>
    122 		want(lexer, ltok::LPAREN)?;
    123 		const cond: nullable *ast::expr =
    124 			alloc(expr(lexer)?);
    125 		const msg: nullable *ast::expr =
    126 			match (try(lexer, ltok::COMMA)?) {
    127 			case lex::token =>
    128 				yield alloc(expr(lexer)?);
    129 			case =>
    130 				yield null;
    131 			};
    132 		want(lexer, ltok::RPAREN)?;
    133 
    134 		yield ast::assert_expr {
    135 			cond      = cond,
    136 			message   = msg,
    137 			is_static = is_static,
    138 		};
    139 	case => abort(); // unreachable
    140 	};
    141 
    142 	return ast::expr {
    143 		start = tok.2,
    144 		end = lex::prevloc(lexer),
    145 		expr = expr,
    146 	};
    147 };
    148 
    149 fn alloc_expr(lexer: *lex::lexer) (ast::expr | error) = {
    150 	const start = want(lexer, ltok::ALLOC)?;
    151 	want(lexer, ltok::LPAREN)?;
    152 
    153 	const init = alloc(expr(lexer)?);
    154 	const expr =
    155 		switch (want(lexer, ltok::COMMA, ltok::ELLIPSIS, ltok::RPAREN)?.0) {
    156 		case ltok::COMMA =>
    157 			const capacity = alloc(expr(lexer)?);
    158 			want(lexer, ltok::RPAREN)?;
    159 			yield ast::alloc_expr {
    160 				init = init,
    161 				form = ast::alloc_form::COPY,
    162 				capacity = capacity,
    163 			};
    164 		case ltok::ELLIPSIS =>
    165 			want(lexer, ltok::RPAREN)?;
    166 			yield ast::alloc_expr {
    167 				init = init,
    168 				form = ast::alloc_form::COPY,
    169 				capacity = null,
    170 			};
    171 		case ltok::RPAREN =>
    172 			yield ast::alloc_expr {
    173 				init = init,
    174 				form = ast::alloc_form::OBJECT,
    175 				capacity = null,
    176 			};
    177 		case => abort(); // unreachable
    178 		};
    179 
    180 	return ast::expr {
    181 		start = start.2,
    182 		end = lex::prevloc(lexer),
    183 		expr = expr,
    184 	};
    185 };
    186 
    187 fn append_insert_expr(
    188 	lexer: *lex::lexer,
    189 	is_static: bool,
    190 ) (ast::expr | error) = {
    191 	const tok = want(lexer, ltok::APPEND, ltok::INSERT)?;
    192 	want(lexer, ltok::LPAREN)?;
    193 
    194 	const object = if (tok.0 == ltok::APPEND) objsel(lexer)?
    195 		else idxexpr(lexer)?;
    196 	want(lexer, ltok::COMMA)?;
    197 	const value = expr(lexer)?;
    198 
    199 	let length: nullable *ast::expr = null;
    200 	let variadic = false;
    201 	match (try(lexer, ltok::COMMA, ltok::ELLIPSIS)?) {
    202 	case let tok: lex::token =>
    203 		switch (tok.0) {
    204 		case ltok::COMMA =>
    205 			length = alloc(expr(lexer)?);
    206 		case ltok::ELLIPSIS =>
    207 			variadic = true;
    208 		case => abort();
    209 		};
    210 	case void => void;
    211 	};
    212 	want(lexer, ltok::RPAREN)?;
    213 
    214 	let expr = ast::append_expr {
    215 		object = alloc(object),
    216 		value = alloc(value),
    217 		length = length,
    218 		variadic = variadic,
    219 		is_static = is_static,
    220 	};
    221 	const expr = if (tok.0 == ltok::INSERT) {
    222 		yield expr: ast::insert_expr;
    223 	} else expr;
    224 
    225 	return ast::expr {
    226 		start = tok.2,
    227 		end = lex::prevloc(lexer),
    228 		expr = expr,
    229 	};
    230 };
    231 
    232 fn measurement(lexer: *lex::lexer) (ast::expr | error) = {
    233 	const tok = want(lexer, ltok::LEN, ltok::ALIGN, ltok::SIZE, ltok::OFFSET)?;
    234 	want(lexer, ltok::LPAREN)?;
    235 	const expr = switch (tok.0) {
    236 	case ltok::LEN =>
    237 		yield alloc(expr(lexer)?): ast::len_expr;
    238 	case ltok::ALIGN =>
    239 		yield alloc(_type(lexer)?): ast::align_expr;
    240 	case ltok::SIZE =>
    241 		yield alloc(_type(lexer)?): ast::size_expr;
    242 	case ltok::OFFSET =>
    243 		yield alloc(expr(lexer)?): ast::offset_expr;
    244 	case => abort(); // unreachable
    245 	};
    246 	want(lexer, ltok::RPAREN)?;
    247 
    248 	return ast::expr {
    249 		start = tok.2,
    250 		end = lex::prevloc(lexer),
    251 		expr = expr,
    252 	};
    253 };
    254 
    255 fn binarithm(
    256 	lexer: *lex::lexer,
    257 	lvalue: (ast::expr | void),
    258 	i: int,
    259 ) (ast::expr | error) = {
    260 	// Precedence climbing parser
    261 	// https://en.wikipedia.org/wiki/Operator-precedence_parser
    262 	let lvalue = match (lvalue) {
    263 	case void =>
    264 		yield cast(lexer, void)?;
    265 	case let expr: ast::expr =>
    266 		yield expr;
    267 	};
    268 
    269 	let tok = lex::lex(lexer)?;
    270 	for (let j = precedence(tok); j >= i; j = precedence(tok)) {
    271 		const op = binop_for_tok(tok);
    272 
    273 		let rvalue = cast(lexer, void)?;
    274 		tok = lex::lex(lexer)?;
    275 
    276 		for (let k = precedence(tok); k > j; k = precedence(tok)) {
    277 			lex::unlex(lexer, tok);
    278 			rvalue = binarithm(lexer, rvalue, k)?;
    279 			tok = lex::lex(lexer)?;
    280 		};
    281 
    282 		const expr = ast::expr {
    283 			start = lvalue.start,
    284 			end = lex::prevloc(lexer),
    285 			expr = ast::binarithm_expr {
    286 				op = op,
    287 				lvalue = alloc(lvalue),
    288 				rvalue = alloc(rvalue),
    289 			},
    290 		};
    291 		lvalue = expr;
    292 	};
    293 
    294 	lex::unlex(lexer, tok);
    295 	return lvalue;
    296 };
    297 
    298 fn binding_unpack(lexer: *lex::lexer) (ast::binding_unpack | error) = {
    299 	let fields: ast::binding_unpack = [];
    300 	for (true) {
    301 		const (tok, value, _) = want(lexer, ltok::NAME,
    302 			ltok::UNDERSCORE)?;
    303 		if (tok == ltok::UNDERSCORE) {
    304 			append(fields, void);
    305 		} else {
    306 			append(fields, value as str);
    307 		};
    308 		if (len(fields) == 1) {
    309 			want(lexer, ltok::COMMA)?;
    310 		} else {
    311 			match (try(lexer, ltok::COMMA)?) {
    312 			case void => break;
    313 			case lex::token => void;
    314 			};
    315 		};
    316 	};
    317 	want(lexer, ltok::RPAREN)?;
    318 	return fields;
    319 };
    320 
    321 fn binding(lexer: *lex::lexer, is_static: bool) (ast::expr | error) = {
    322 	const loc = lex::mkloc(lexer);
    323 	const tok = want(lexer, ltok::DEF, ltok::CONST, ltok::LET)?.0;
    324 	const kind = switch (tok) {
    325 	case ltok::DEF =>
    326 		assert(!is_static);
    327 		yield ast::binding_kind::DEF;
    328 	case ltok::CONST =>
    329 		yield ast::binding_kind::CONST;
    330 	case ltok::LET =>
    331 		yield ast::binding_kind::LET;
    332 	case => abort(); // unreachable
    333 	};
    334 
    335 	let bindings: []ast::binding = [];
    336 	for (true) {
    337 		const (tok, value, _) = want(lexer, ltok::NAME, ltok::LPAREN)?;
    338 		const name = switch (tok) {
    339 		case ltok::NAME =>
    340 			yield value as str;
    341 		case ltok::LPAREN =>
    342 			if (kind == ast::binding_kind::DEF) {
    343 				return syntaxerr(lex::mkloc(lexer),
    344 					"Can't use tuple unpacking with def");
    345 			};
    346 			yield binding_unpack(lexer)?;
    347 		case => abort();
    348 		};
    349 		const btype: nullable *ast::_type =
    350 			if (try(lexer, ltok::COLON)? is lex::token) {
    351 				yield alloc(_type(lexer)?);
    352 			} else null;
    353 		want(lexer, ltok::EQUAL)?;
    354 		const init = alloc(expr(lexer)?);
    355 		append(bindings, ast::binding {
    356 			name = name,
    357 			_type = btype,
    358 			init = init,
    359 		});
    360 		match (try(lexer, ltok::COMMA)?) {
    361 		case void => break;
    362 		case lex::token => void;
    363 		};
    364 	};
    365 
    366 	return ast::expr {
    367 		start = loc,
    368 		end = lex::prevloc(lexer),
    369 		expr = ast::binding_expr {
    370 			is_static = is_static,
    371 			kind = kind,
    372 			bindings = bindings,
    373 		},
    374 	};
    375 };
    376 
    377 fn builtin(lexer: *lex::lexer) (ast::expr | error) = {
    378 	const tok = match (peek(lexer, ltok::ALIGN, ltok::ALLOC, ltok::APPEND,
    379 		ltok::FREE, ltok::DELETE, ltok::ABORT, ltok::ASSERT,
    380 		ltok::INSERT, ltok::STATIC, ltok::SIZE, ltok::LEN, ltok::OFFSET,
    381 		ltok::VASTART, ltok::VAARG, ltok::VAEND)?) {
    382 	case let tok: lex::token =>
    383 		yield tok;
    384 	case void =>
    385 		return postfix(lexer, void);
    386 	};
    387 	switch (tok.0) {
    388 	case ltok::ALLOC =>
    389 		return alloc_expr(lexer);
    390 	case ltok::APPEND, ltok::INSERT =>
    391 		return append_insert_expr(lexer, false);
    392 	case ltok::DELETE =>
    393 		return delete_expr(lexer, false);
    394 	case ltok::FREE =>
    395 		return free_expr(lexer);
    396 	case ltok::ABORT, ltok::ASSERT =>
    397 		return assert_expr(lexer, false);
    398 	case ltok::STATIC =>
    399 		want(lexer, ltok::STATIC)!;
    400 		return static_expr(lexer);
    401 	case ltok::ALIGN, ltok::SIZE, ltok::LEN, ltok::OFFSET =>
    402 		return measurement(lexer);
    403 	case ltok::VASTART =>
    404 		want(lexer, ltok::VASTART)?;
    405 		want(lexer, ltok::LPAREN)?;
    406 		want(lexer, ltok::RPAREN)?;
    407 		return ast::expr {
    408 			start = tok.2,
    409 			end = lex::prevloc(lexer),
    410 			expr = void: ast::vastart_expr: ast::variadic_expr,
    411 		};
    412 	case ltok::VAARG =>
    413 		want(lexer, ltok::VAARG)?;
    414 		want(lexer, ltok::LPAREN)?;
    415 		const expr = alloc(objsel(lexer)?);
    416 		want(lexer, ltok::RPAREN)?;
    417 		return ast::expr {
    418 			start = tok.2,
    419 			end = lex::prevloc(lexer),
    420 			expr = expr: ast::vaarg_expr: ast::variadic_expr,
    421 		};
    422 	case ltok::VAEND =>
    423 		want(lexer, ltok::VAEND)?;
    424 		want(lexer, ltok::LPAREN)?;
    425 		const expr = alloc(objsel(lexer)?);
    426 		want(lexer, ltok::RPAREN)?;
    427 		return ast::expr {
    428 			start = tok.2,
    429 			end = lex::prevloc(lexer),
    430 			expr = expr: ast::vaend_expr: ast::variadic_expr,
    431 		};
    432 	case => abort(); // Invariant
    433 	};
    434 };
    435 
    436 fn call(lexer: *lex::lexer, lvalue: ast::expr) (ast::expr | error) = {
    437 	let args: []*ast::expr = [];
    438 	let variadic = false;
    439 
    440 	for (true) {
    441 		match (try(lexer, ltok::RPAREN)?) {
    442 		case lex::token => break;
    443 		case void => void;
    444 		};
    445 
    446 		append(args, alloc(expr(lexer)?));
    447 
    448 		match (try(lexer, ltok::ELLIPSIS)?) {
    449 		case lex::token =>
    450 			variadic = true;
    451 			want(lexer, ltok::RPAREN)?;
    452 			break;
    453 		case void => void;
    454 		};
    455 
    456 		switch (want(lexer, ltok::COMMA, ltok::RPAREN)?.0) {
    457 		case ltok::RPAREN => break;
    458 		case => void;
    459 		};
    460 	};
    461 
    462 	return ast::expr {
    463 		start = lvalue.start,
    464 		end = lex::prevloc(lexer),
    465 		expr = ast::call_expr {
    466 			lvalue = alloc(lvalue),
    467 			variadic = variadic,
    468 			args = args,
    469 		},
    470 	};
    471 };
    472 
    473 fn cast(lexer: *lex::lexer, lvalue: (ast::expr | void)) (ast::expr | error) = {
    474 	const lvalue = match (lvalue) {
    475 	case void =>
    476 		yield unarithm(lexer)?;
    477 	case let e: ast::expr =>
    478 		yield e;
    479 	};
    480 	const tok = match (try(lexer, ltok::COLON, ltok::AS, ltok::IS)?) {
    481 	case void =>
    482 		return lvalue;
    483 	case let tok: lex::token =>
    484 		yield tok.0;
    485 	};
    486 	const kind = switch (tok) {
    487 	case ltok::COLON =>
    488 		yield ast::cast_kind::CAST;
    489 	case ltok::AS =>
    490 		yield ast::cast_kind::ASSERTION;
    491 	case ltok::IS =>
    492 		yield ast::cast_kind::TEST;
    493 	case => abort();
    494 	};
    495 	let typ = match (try(lexer, ltok::NULL)?) {
    496 	case let t: lex::token =>
    497 		yield alloc(ast::_type {
    498 			start = t.2,
    499 			end = lex::prevloc(lexer),
    500 			flags = 0,
    501 			repr = ast::builtin_type::NULL,
    502 		});
    503 	case void =>
    504 		yield alloc(_type(lexer)?);
    505 	};
    506 	return cast(lexer, ast::expr {
    507 		start = lvalue.start,
    508 		end = lex::prevloc(lexer),
    509 		expr = ast::cast_expr {
    510 			kind = kind,
    511 			value = alloc(lvalue),
    512 			_type = typ,
    513 		},
    514 	})?;
    515 };
    516 
    517 fn literal(lexer: *lex::lexer) (ast::expr | error) = {
    518 	const tok = want(lexer)?;
    519 	const expr: ast::literal_expr = switch (tok.0) {
    520 	case ltok::LIT_RCONST, ltok::LIT_STR =>
    521 		yield tok.1 as (rune | str);
    522 	case ltok::LIT_U8, ltok::LIT_U16, ltok::LIT_U32, ltok::LIT_U64,
    523 		ltok::LIT_UINT, ltok::LIT_SIZE =>
    524 		yield ast::number_literal {
    525 			suff = tok.0,
    526 			value = tok.1 as u64,
    527 			sign = false,
    528 		};
    529 	case ltok::LIT_I8, ltok::LIT_I16, ltok::LIT_I32, ltok::LIT_I64,
    530 		ltok::LIT_INT =>
    531 		const n = tok.1 as u64;
    532 		yield ast::number_literal {
    533 			suff = tok.0,
    534 			value = n: i64,
    535 			sign = false,
    536 		};
    537 	case ltok::LIT_ICONST =>
    538 		const n = tok.1 as u64;
    539 		yield ast::number_literal {
    540 			suff = tok.0,
    541 			value = if (n <= types::I64_MAX: u64) n: i64 else n,
    542 			sign = false,
    543 		};
    544 	case ltok::LIT_F32, ltok::LIT_F64, ltok::LIT_FCONST =>
    545 		yield ast::number_literal {
    546 			suff = tok.0,
    547 			value = tok.1 as f64,
    548 			sign = false,
    549 		};
    550 	case ltok::VOID =>
    551 		yield void;
    552 	case ltok::DONE =>
    553 		yield done;
    554 	case ltok::TRUE =>
    555 		yield true;
    556 	case ltok::FALSE =>
    557 		yield false;
    558 	case ltok::NULL =>
    559 		yield ast::_null;
    560 	case =>
    561 		return syntaxerr(lex::mkloc(lexer), "Expected literal expression");
    562 	};
    563 	return ast::expr {
    564 		start = tok.2,
    565 		end = lex::prevloc(lexer),
    566 		expr = expr,
    567 	};
    568 };
    569 
    570 fn control(lexer: *lex::lexer) (ast::expr | error) = {
    571 	let tok = want(lexer, ltok::BREAK, ltok::CONTINUE, ltok::RETURN)?;
    572 	let label = if (tok.0 == ltok::BREAK || tok.0 == ltok::CONTINUE) {
    573 		yield match (try(lexer, ltok::COLON)?) {
    574 		case lex::token =>
    575 			yield want(lexer, ltok::NAME)?.1 as str;
    576 		case void =>
    577 			yield "";
    578 		};
    579 	} else "";
    580 	const expr = switch (tok.0) {
    581 	case ltok::BREAK =>
    582 		yield label: ast::break_expr;
    583 	case ltok::CONTINUE =>
    584 		yield label: ast::continue_expr;
    585 	case ltok::RETURN =>
    586 		yield match (peek(lexer, ltok::COMMA, ltok::ELSE, ltok::RBRACE,
    587 			ltok::RBRACKET, ltok::RPAREN, ltok::SEMICOLON,
    588 			ltok::EOF)?) {
    589 		case void =>
    590 			yield alloc(expr(lexer)?): ast::return_expr;
    591 		case lex::token =>
    592 			yield null: ast::return_expr;
    593 		};
    594 	case => abort(); // unreachable
    595 	};
    596 	return ast::expr {
    597 		start = tok.2,
    598 		end = lex::prevloc(lexer),
    599 		expr = expr,
    600 	};
    601 };
    602 
    603 fn delete_expr(lexer: *lex::lexer, is_static: bool) (ast::expr | error) = {
    604 	const start = want(lexer, ltok::DELETE)?;
    605 	want(lexer, ltok::LPAREN)?;
    606 	const expr = alloc(postfix(lexer, void)?);
    607 	// TODO: Assert that this was an indexing expression
    608 	want(lexer, ltok::RPAREN)?;
    609 	return ast::expr {
    610 		start = start.2,
    611 		end = lex::prevloc(lexer),
    612 		expr = ast::delete_expr {
    613 			object = expr,
    614 			is_static = is_static,
    615 		},
    616 	};
    617 };
    618 
    619 fn compound_expr(lexer: *lex::lexer) (ast::expr | error) = {
    620 	let items: []*ast::expr = [];
    621 
    622 	const start = want(lexer, ltok::LBRACE, ltok::COLON)?;
    623 	const label = switch (start.0) {
    624 	case ltok::COLON =>
    625 		const tok = want(lexer, ltok::NAME)?;
    626 		want(lexer, ltok::LBRACE)?;
    627 		yield tok.1 as str;
    628 	case =>
    629 		yield "";
    630 	};
    631 
    632 	for (true) {
    633 		append(items, alloc(stmt(lexer)?));
    634 		if (try(lexer, ltok::RBRACE)? is lex::token) {
    635 			break;
    636 		};
    637 	};
    638 
    639 	return ast::expr {
    640 		start = start.2,
    641 		end = lex::prevloc(lexer),
    642 		expr = ast::compound_expr {
    643 			exprs = items,
    644 			label = label,
    645 		},
    646 	};
    647 };
    648 
    649 fn stmt(lexer: *lex::lexer) (ast::expr | error) = {
    650 	const expr = match (try(lexer, ltok::DEFER, ltok::DEF,
    651 		ltok::LET, ltok::CONST, ltok::STATIC)?) {
    652 	case let tok: lex::token =>
    653 		yield switch (tok.0) {
    654 		case ltok::DEFER =>
    655 			let expr = alloc(expr(lexer)?);
    656 			yield ast::expr {
    657 				start = tok.2,
    658 				end = lex::prevloc(lexer),
    659 				expr = expr: ast::defer_expr,
    660 			};
    661 		case ltok::DEF, ltok::CONST, ltok::LET =>
    662 			lex::unlex(lexer, tok);
    663 			yield binding(lexer, false)?;
    664 		case ltok::STATIC =>
    665 			yield match (peek(lexer, ltok::LET, ltok::CONST)?) {
    666 			case lex::token =>
    667 				yield binding(lexer, true)?;
    668 			case void =>
    669 				yield static_expr(lexer)?;
    670 			};
    671 		case => abort(); // unreachable
    672 		};
    673 	case void =>
    674 		yield expr(lexer)?;
    675 	};
    676 
    677 	want(lexer, ltok::SEMICOLON)?;
    678 	return expr;
    679 };
    680 
    681 fn for_expr(lexer: *lex::lexer) (ast::expr | error) = {
    682 	const tok = want(lexer, ltok::FOR)?;
    683 	const label = if (try(lexer, ltok::COLON)? is lex::token) {
    684 		const tok = want(lexer, ltok::NAME)?;
    685 		yield tok.1 as str;
    686 	} else "";
    687 	want(lexer, ltok::LPAREN)?;
    688 
    689 	let kind = void: (ast::for_kind | void);
    690 	let predicate_loc = lex::mkloc(lexer);
    691 
    692 	const bindings = match (try(lexer, ltok::LET, ltok::CONST)?) {
    693 	case let tok: lex::token =>
    694 		const binding_kind = switch (tok.0) {
    695 		case ltok::LET =>
    696 			yield ast::binding_kind::LET;
    697 		case ltok::CONST =>
    698 			yield ast::binding_kind::CONST;
    699 		case => abort(); // unreachable
    700 		};
    701 
    702 		let bindings: []ast::binding = [];
    703 
    704 		for (true) {
    705 			const (tok, value, _) = want(lexer,
    706 				ltok::NAME, ltok::LPAREN)?;
    707 			const binding_name = switch (tok) {
    708 			case ltok::NAME =>
    709 				yield value as str;
    710 			case ltok::LPAREN =>
    711 				yield binding_unpack(lexer)?;
    712 			case => abort(); // unreachable
    713 			};
    714 			const btype: nullable *ast::_type =
    715 				if (try(lexer, ltok::COLON)? is lex::token) {
    716 					yield alloc(_type(lexer)?);
    717 				} else null;
    718 
    719 			const (tok, _, _) = want(lexer, ltok::EQUAL,
    720 				ltok::DOUBLE_DOT, ltok::BAND, ltok::ARROW)?;
    721 
    722 			if (kind is void) {
    723 				switch (tok) {
    724 				case ltok::EQUAL =>
    725 					kind = ast::for_kind::ACCUMULATOR;
    726 				case ltok::DOUBLE_DOT =>
    727 					kind = ast::for_kind::EACH_VALUE;
    728 				case ltok::BAND =>
    729 					want(lexer, ltok::DOUBLE_DOT)?;
    730 					kind = ast::for_kind::EACH_POINTER;
    731 				case ltok::ARROW =>
    732 					kind = ast::for_kind::ITERATOR;
    733 				case => abort(); // unreachable
    734 				};
    735 			} else if (kind as ast::for_kind !=
    736 					ast::for_kind::ACCUMULATOR
    737 					|| tok != ltok::EQUAL) {
    738 				return syntaxerr(lex::mkloc(lexer),
    739 					"Cannot create multiple bindings in non-c-style loop");
    740 			};
    741 
    742 			const init_expr = alloc(expr(lexer)?);
    743 
    744 			append(bindings, ast::binding {
    745 				name = binding_name,
    746 				_type = btype,
    747 				init = init_expr,
    748 			});
    749 
    750 			match (try(lexer, ltok::COMMA)?) {
    751 			case lex::token =>
    752 				void;
    753 			case void =>
    754 				break;
    755 			};
    756 		};
    757 
    758 		if (kind as ast::for_kind == ast::for_kind::ACCUMULATOR) {
    759 			want(lexer, ltok::SEMICOLON)?;
    760 		};
    761 
    762 		yield alloc(ast::expr {
    763 			start = predicate_loc,
    764 			end = lex::prevloc(lexer),
    765 			expr = ast::binding_expr {
    766 				is_static = false,
    767 				kind = binding_kind,
    768 				bindings = bindings,
    769 			},
    770 		});
    771 	case void =>
    772 		kind = ast::for_kind::ACCUMULATOR;
    773 		yield null;
    774 	};
    775 
    776 	const cond: nullable *ast::expr = null;
    777 	const afterthought: nullable *ast::expr = null;
    778 
    779 	if (kind as ast::for_kind == ast::for_kind::ACCUMULATOR) {
    780 		cond = alloc(expr(lexer)?);
    781 		match (try(lexer, ltok::SEMICOLON)) {
    782 		case lex::token =>
    783 			afterthought = alloc(expr(lexer)?);
    784 		case void => void;
    785 		};
    786 	};
    787 
    788 	want(lexer, ltok::RPAREN)?;
    789 
    790 	const body = alloc(expr(lexer)?);
    791 	return ast::expr {
    792 		start = tok.2,
    793 		end = lex::prevloc(lexer),
    794 		expr = ast::for_expr {
    795 			kind = kind as ast::for_kind,
    796 			bindings = bindings,
    797 			cond = cond,
    798 			afterthought = afterthought,
    799 			body = body,
    800 			label = label,
    801 		},
    802 	};
    803 };
    804 
    805 fn free_expr(lexer: *lex::lexer) (ast::expr | error) = {
    806 	const start = want(lexer, ltok::FREE)?;
    807 	want(lexer, ltok::LPAREN)?;
    808 	const expr = alloc(expr(lexer)?);
    809 	want(lexer, ltok::RPAREN)?;
    810 	return ast::expr {
    811 		start = start.2,
    812 		end = lex::prevloc(lexer),
    813 		expr = expr: ast::free_expr,
    814 	};
    815 };
    816 
    817 fn if_expr(lexer: *lex::lexer) (ast::expr | error) = {
    818 	const start = want(lexer, ltok::IF)?;
    819 	want(lexer, ltok::LPAREN)?;
    820 	const cond = alloc(expr(lexer)?);
    821 	want(lexer, ltok::RPAREN)?;
    822 	const tbranch = alloc(expr(lexer)?);
    823 	const fbranch: nullable *ast::expr = match (try(lexer, ltok::ELSE)?) {
    824 	case void =>
    825 		yield null;
    826 	case lex::token =>
    827 		yield alloc(expr(lexer)?);
    828 	};
    829 	return ast::expr {
    830 		start = start.2,
    831 		end = lex::prevloc(lexer),
    832 		expr = ast::if_expr {
    833 			cond = cond,
    834 			tbranch = tbranch,
    835 			fbranch = fbranch,
    836 		},
    837 	};
    838 };
    839 
    840 fn indexing(lexer: *lex::lexer, lvalue: ast::expr) (ast::expr | error) = {
    841 	let is_slice = false;
    842 	let start: nullable *ast::expr = null, end: nullable *ast::expr = null;
    843 
    844 	if (try(lexer, ltok::DOUBLE_DOT)? is lex::token) {
    845 		is_slice = true;
    846 	} else {
    847 		start = alloc(expr(lexer)?);
    848 	};
    849 	if (!is_slice && try(lexer, ltok::DOUBLE_DOT)? is lex::token) {
    850 		is_slice = true;
    851 	};
    852 	if (is_slice && peek(lexer, ltok::RBRACKET)? is void) {
    853 		end = alloc(expr(lexer)?);
    854 	};
    855 
    856 	want(lexer, ltok::RBRACKET)?;
    857 	return ast::expr {
    858 		start = lvalue.start,
    859 		end = lex::prevloc(lexer),
    860 		expr = if (is_slice) ast::slice_expr {
    861 			object = alloc(lvalue),
    862 			start = start,
    863 			end = end,
    864 		} else ast::access_index {
    865 			object = alloc(lvalue),
    866 			index = {
    867 				assert(end == null);
    868 				yield start as *ast::expr;
    869 			},
    870 		},
    871 	};
    872 };
    873 
    874 fn objsel(lexer: *lex::lexer) (ast::expr | error) = {
    875 	let expr = postfix(lexer, void)?;
    876 	synassert(lex::mkloc(lexer), expr.expr is ast::access_expr,
    877 		"Expected object selector")?;
    878 	return expr;
    879 };
    880 
    881 fn idxexpr(lexer: *lex::lexer) (ast::expr | error) = {
    882 	const expr = postfix(lexer, void)?;
    883 	synassert(lex::mkloc(lexer), expr.expr is ast::access_expr
    884 		&& expr.expr as ast::access_expr is ast::access_index,
    885 		"Expected indexing expression")?;
    886 	return expr;
    887 };
    888 
    889 fn plain_expression(lexer: *lex::lexer) (ast::expr | error) = {
    890 	let tok = peek(lexer)? as lex::token;
    891 	if (tok.0 >= ltok::LIT_U8 && tok.0 <= ltok::LAST_LITERAL) {
    892 		return literal(lexer);
    893 	};
    894 	switch (tok.0) {
    895 	case ltok::TRUE, ltok::FALSE, ltok::NULL, ltok::VOID, ltok::DONE =>
    896 		return literal(lexer);
    897 	case ltok::LBRACKET =>
    898 		return plain_array(lexer)?;
    899 	case ltok::STRUCT =>
    900 		let s = plain_struct(lexer, [])?;
    901 		return ast::expr {
    902 			start = tok.2,
    903 			end = lex::prevloc(lexer),
    904 			expr = s,
    905 		};
    906 	case ltok::LPAREN =>
    907 		want(lexer, ltok::LPAREN)?;
    908 		let ex = expr(lexer)?;
    909 		switch (want(lexer, ltok::RPAREN, ltok::COMMA)?.0) {
    910 		case ltok::RPAREN =>
    911 			return ex;
    912 		case ltok::COMMA =>
    913 			return plain_tuple(lexer, ex, tok.2)?;
    914 		case => abort();
    915 		};
    916 	case ltok::NAME =>
    917 		let id = ident(lexer)?;
    918 		match (peek(lexer, ltok::LBRACE)?) {
    919 		case void =>
    920 			return ast::expr {
    921 				start = tok.2,
    922 				end = lex::prevloc(lexer),
    923 				expr = id: ast::access_identifier,
    924 			};
    925 		case lex::token =>
    926 			let s = plain_struct(lexer, id)?;
    927 			return ast::expr {
    928 				start = tok.2,
    929 				end = lex::prevloc(lexer),
    930 				expr = s,
    931 			};
    932 		};
    933 	case =>
    934 		return syntaxerr(lex::mkloc(lexer),
    935 			"Unexpected {}, was expecting an expression",
    936 			lex::tokstr(tok));
    937 	};
    938 };
    939 
    940 fn plain_array(lexer: *lex::lexer) (ast::expr | error) = {
    941 	const start = want(lexer, ltok::LBRACKET)?;
    942 
    943 	let values: []*ast::expr = [];
    944 	let expand = false;
    945 	for (true) {
    946 		match (try(lexer, ltok::RBRACKET)?) {
    947 		case lex::token => break;
    948 		case void => void;
    949 		};
    950 
    951 		append(values, alloc(expr(lexer)?));
    952 
    953 		match (try(lexer, ltok::COMMA, ltok::ELLIPSIS)?) {
    954 		case void =>
    955 			want(lexer, ltok::RBRACKET)?;
    956 			break;
    957 		case let tok: lex::token =>
    958 			switch (tok.0) {
    959 			case ltok::ELLIPSIS =>
    960 				expand = true;
    961 				want(lexer, ltok::RBRACKET)?;
    962 				break;
    963 			case ltok::COMMA => void;
    964 			case => abort();
    965 			};
    966 		};
    967 	};
    968 	return ast::expr {
    969 		start = start.2,
    970 		end = lex::prevloc(lexer),
    971 		expr = ast::array_literal {
    972 			expand = expand,
    973 			values = values,
    974 		},
    975 	};
    976 };
    977 
    978 fn plain_struct(
    979 	lexer: *lex::lexer,
    980 	alias: ast::ident,
    981 ) (ast::struct_literal | error) = {
    982 	if (len(alias) == 0) {
    983 		want(lexer, ltok::STRUCT)?;
    984 	};
    985 	want(lexer, ltok::LBRACE)?;
    986 
    987 	let autofill = false;
    988 	let fields: [](ast::struct_value | *ast::struct_literal) = [];
    989 	for (true) {
    990 		const tok = want(lexer, ltok::ELLIPSIS,
    991 			ltok::NAME, ltok::STRUCT)?;
    992 		switch (tok.0) {
    993 		case ltok::ELLIPSIS =>
    994 			synassert(lex::mkloc(lexer), len(alias) != 0,
    995 				"Cannot use auto-fill with anonymous struct")?;
    996 			autofill = true;
    997 			want(lexer, ltok::RBRACE)?;
    998 			break;
    999 		case ltok::NAME, ltok::STRUCT =>
   1000 			lex::unlex(lexer, tok);
   1001 			append(fields, struct_field(lexer)?);
   1002 		case => abort(); // unreachable
   1003 		};
   1004 
   1005 		switch (want(lexer, ltok::COMMA, ltok::RBRACE)?.0) {
   1006 		case ltok::RBRACE => break;
   1007 		case ltok::COMMA =>
   1008 			if (try(lexer, ltok::RBRACE)? is lex::token) {
   1009 				break;
   1010 			};
   1011 		case => abort(); // unreachable
   1012 		};
   1013 	};
   1014 
   1015 	return ast::struct_literal {
   1016 		autofill = autofill,
   1017 		alias = alias,
   1018 		fields = fields,
   1019 	};
   1020 };
   1021 
   1022 fn struct_field(
   1023 	lexer: *lex::lexer,
   1024 ) (ast::struct_value | *ast::struct_literal | error) = {
   1025 	const tok = want(lexer, ltok::NAME, ltok::STRUCT)?;
   1026 	switch (tok.0) {
   1027 	case ltok::NAME =>
   1028 		const name = strings::dup(tok.1 as str);
   1029 		const tok = match (try(lexer, ltok::COLON,
   1030 			ltok::DOUBLE_COLON, ltok::EQUAL)?) {
   1031 		case let tok: lex::token =>
   1032 			yield tok;
   1033 		case void =>
   1034 			let id: ast::ident = alloc([name]);
   1035 			return alloc(plain_struct(lexer, id)?);
   1036 		};
   1037 
   1038 		switch (tok.0) {
   1039 		case ltok::COLON =>
   1040 			const _type = alloc(_type(lexer)?);
   1041 			want(lexer, ltok::EQUAL)?;
   1042 			const init = alloc(expr(lexer)?);
   1043 			return ast::struct_value {
   1044 				name = name,
   1045 				_type = _type,
   1046 				init = init,
   1047 			};
   1048 		case ltok::DOUBLE_COLON =>
   1049 			let id: ast::ident = alloc([name]);
   1050 			let rest = ident(lexer)?;
   1051 			append(id, rest...);
   1052 			return alloc(plain_struct(lexer, id)?);
   1053 		case ltok::EQUAL =>
   1054 			return ast::struct_value {
   1055 				name = name,
   1056 				_type = null,
   1057 				init = alloc(expr(lexer)?),
   1058 			};
   1059 		case => abort(); // Invariant
   1060 		};
   1061 	case ltok::STRUCT =>
   1062 		lex::unlex(lexer, tok);
   1063 		return alloc(plain_struct(lexer, [])?);
   1064 	case => abort(); // Invariant
   1065 	};
   1066 };
   1067 
   1068 fn plain_tuple(
   1069 	lexer: *lex::lexer,
   1070 	ex: ast::expr,
   1071 	start: lex::location
   1072 ) (ast::expr | error) = {
   1073 	let values: []*ast::expr = [];
   1074 	append(values, alloc(ex));
   1075 
   1076 	for (true) {
   1077 		append(values, alloc(expr(lexer)?));
   1078 
   1079 		match (try(lexer, ltok::COMMA)?) {
   1080 		case lex::token =>
   1081 			match (try(lexer, ltok::RPAREN)) {
   1082 			case lex::token => break;
   1083 			case => void;
   1084 			};
   1085 		case void =>
   1086 			want(lexer, ltok::RPAREN)?;
   1087 			break;
   1088 		};
   1089 	};
   1090 
   1091 	return ast::expr {
   1092 		start = start,
   1093 		end = lex::prevloc(lexer),
   1094 		expr = values: ast::tuple_literal,
   1095 	};
   1096 };
   1097 
   1098 fn postfix(lexer: *lex::lexer, lvalue: (ast::expr | void)) (ast::expr | error) = {
   1099 	let lvalue = match (lvalue) {
   1100 	case void =>
   1101 		yield plain_expression(lexer)?;
   1102 	case let ex: ast::expr =>
   1103 		yield ex;
   1104 	};
   1105 
   1106 	let tok = match (try(lexer, ltok::LPAREN, ltok::DOT,
   1107 		ltok::LBRACKET, ltok::QUESTION, ltok::LNOT)?) {
   1108 	case void =>
   1109 		return lvalue;
   1110 	case let tok: lex::token =>
   1111 		yield tok;
   1112 	};
   1113 
   1114 	let next = switch (tok.0) {
   1115 	case ltok::LPAREN =>
   1116 		yield call(lexer, lvalue)?;
   1117 	case ltok::DOT =>
   1118 		yield postfix_dot(lexer, lvalue)?;
   1119 	case ltok::LBRACKET =>
   1120 		yield indexing(lexer, lvalue)?;
   1121 	case ltok::QUESTION =>
   1122 		yield ast::expr {
   1123 			start = lvalue.start,
   1124 			end = lex::prevloc(lexer),
   1125 			expr = alloc(lvalue): ast::propagate_expr,
   1126 		};
   1127 	case ltok::LNOT =>
   1128 		yield ast::expr {
   1129 			start = lvalue.start,
   1130 			end = lex::prevloc(lexer),
   1131 			expr = alloc(lvalue): ast::error_assert_expr,
   1132 		};
   1133 	case => abort();
   1134 	};
   1135 
   1136 	return postfix(lexer, next);
   1137 };
   1138 
   1139 fn postfix_dot(
   1140 	lexer: *lex::lexer,
   1141 	lvalue: ast::expr,
   1142 ) (ast::expr | error) = {
   1143 	match (try(lexer, ltok::NAME)?) {
   1144 	case let tok: lex::token =>
   1145 		return ast::expr {
   1146 			start = lvalue.start,
   1147 			end = lex::prevloc(lexer),
   1148 			expr = ast::access_field {
   1149 				object = alloc(lvalue),
   1150 				field = tok.1 as str,
   1151 			},
   1152 		};
   1153 	case void =>
   1154 		let lit = literal(lexer)?;
   1155 		let val = lit.expr as ast::literal_expr;
   1156 		synassert(lex::mkloc(lexer), val is ast::number_literal,
   1157 			"Expected integer literal")?;
   1158 		let val = val as ast::number_literal;
   1159 		return ast::expr {
   1160 			start = lvalue.start,
   1161 			end = lex::prevloc(lexer),
   1162 			expr = ast::access_tuple {
   1163 				object = alloc(lvalue),
   1164 				value = alloc(lit),
   1165 			},
   1166 		};
   1167 	};
   1168 };
   1169 
   1170 fn static_expr(lexer: *lex::lexer) (ast::expr | error) = {
   1171 	const tok = want(lexer, ltok::ABORT, ltok::ASSERT, ltok::APPEND,
   1172 		ltok::INSERT, ltok::DELETE)?;
   1173 	lex::unlex(lexer, tok);
   1174 
   1175 	switch (tok.0) {
   1176 	case ltok::ABORT, ltok::ASSERT =>
   1177 		return assert_expr(lexer, true);
   1178 	case ltok::APPEND, ltok::INSERT =>
   1179 		return append_insert_expr(lexer, true);
   1180 	case ltok::DELETE =>
   1181 		return delete_expr(lexer, true);
   1182 	case => abort(); // unreachable
   1183 	};
   1184 };
   1185 
   1186 fn switch_expr(lexer: *lex::lexer) (ast::expr | error) = {
   1187 	const start = want(lexer, ltok::SWITCH)?;
   1188 
   1189 	const label = if (try(lexer, ltok::COLON)? is lex::token) {
   1190 		const tok = want(lexer, ltok::NAME)?;
   1191 		yield tok.1 as str;
   1192 	} else "";
   1193 
   1194 	want(lexer, ltok::LPAREN)?;
   1195 	const value = expr(lexer)?;
   1196 	want(lexer, ltok::RPAREN)?;
   1197 
   1198 	want(lexer, ltok::LBRACE)?;
   1199 
   1200 	let cases: []ast::switch_case = [];
   1201 	for (true) {
   1202 		want(lexer, ltok::CASE)?;
   1203 
   1204 		let opts: []*ast::expr = [];
   1205 		if (try(lexer, ltok::ARROW)? is void) for (true) {
   1206 			append(opts, alloc(expr(lexer)?));
   1207 			switch (want(lexer, ltok::ARROW, ltok::COMMA)?.0) {
   1208 			case ltok::ARROW =>
   1209 				break;
   1210 			case ltok::COMMA =>
   1211 				if (try(lexer, ltok::ARROW)? is lex::token) {
   1212 					break;
   1213 				};
   1214 			case => abort(); // unreachable
   1215 			};
   1216 		};
   1217 
   1218 		let exprs: []*ast::expr = [];
   1219 		for (true) {
   1220 			append(exprs, alloc(stmt(lexer)?));
   1221 			match (peek(lexer, ltok::CASE, ltok::RBRACE)?) {
   1222 			case lex::token =>
   1223 				break;
   1224 			case void => void;
   1225 			};
   1226 		};
   1227 
   1228 		append(cases, ast::switch_case {
   1229 			options = opts,
   1230 			exprs = exprs,
   1231 		});
   1232 
   1233 		if (try(lexer, ltok::RBRACE)? is lex::token) {
   1234 			break;
   1235 		};
   1236 	};
   1237 
   1238 	return ast::expr {
   1239 		start = start.2,
   1240 		end = lex::prevloc(lexer),
   1241 		expr = ast::switch_expr {
   1242 			value = alloc(value),
   1243 			cases = cases,
   1244 			label = label,
   1245 		},
   1246 	};
   1247 };
   1248 
   1249 fn match_case(lexer: *lex::lexer) (ast::match_case | error) = {
   1250 	want(lexer, ltok::CASE)?;
   1251 	let tok = lex::lex(lexer)?;
   1252 	let loc = tok.2;
   1253 	let name: str = "", typ: nullable *ast::_type = null;
   1254 	switch (tok.0) {
   1255 	case ltok::NULL =>
   1256 		typ = alloc(ast::_type {
   1257 			start = loc,
   1258 			end = lex::prevloc(lexer),
   1259 			flags = 0,
   1260 			repr = ast::builtin_type::NULL,
   1261 		});
   1262 	case ltok::LET =>
   1263 		name = want(lexer, ltok::NAME)?.1 as str;
   1264 		want(lexer, ltok::COLON)?;
   1265 		typ = alloc(_type(lexer)?);
   1266 	case ltok::ARROW =>
   1267 		lex::unlex(lexer, tok);
   1268 	case =>
   1269 		lex::unlex(lexer, tok);
   1270 		typ = alloc(_type(lexer)?);
   1271 	};
   1272 	want(lexer, ltok::ARROW)?;
   1273 	let exprs: []*ast::expr = [];
   1274 	for (true) {
   1275 		append(exprs, alloc(stmt(lexer)?));
   1276 		if (peek(lexer, ltok::CASE, ltok::RBRACE)? is lex::token) {
   1277 			break;
   1278 		};
   1279 	};
   1280 
   1281 	return ast::match_case {
   1282 		name = name,
   1283 		_type = typ,
   1284 		exprs = exprs,
   1285 	};
   1286 };
   1287 
   1288 fn match_expr(lexer: *lex::lexer) (ast::expr | error) = {
   1289 	const start = want(lexer, ltok::MATCH)?;
   1290 	const label = if (try(lexer, ltok::COLON)? is lex::token) {
   1291 		const tok = want(lexer, ltok::NAME)?;
   1292 		yield tok.1 as str;
   1293 	} else "";
   1294 	want(lexer, ltok::LPAREN)?;
   1295 	const value = expr(lexer)?;
   1296 	want(lexer, ltok::RPAREN)?;
   1297 	want(lexer, ltok::LBRACE)?;
   1298 
   1299 	let cases: []ast::match_case = [];
   1300 	for (true) {
   1301 		append(cases, match_case(lexer)?);
   1302 		if (try(lexer, ltok::RBRACE)? is lex::token) {
   1303 			break;
   1304 		};
   1305 	};
   1306 
   1307 	return ast::expr {
   1308 		start = start.2,
   1309 		end = lex::prevloc(lexer),
   1310 		expr = ast::match_expr {
   1311 			value = alloc(value),
   1312 			cases = cases,
   1313 			label = label,
   1314 		},
   1315 	};
   1316 };
   1317 
   1318 fn unarithm(lexer: *lex::lexer) (ast::expr | error) = {
   1319 	const tok = match (try(lexer,
   1320 		ltok::MINUS, ltok::BNOT, ltok::LNOT, ltok::TIMES, ltok::BAND,
   1321 		ltok::SWITCH, ltok::MATCH, ltok::COLON, ltok::LBRACE)?) {
   1322 	case void =>
   1323 		return builtin(lexer);
   1324 	case let tok: lex::token =>
   1325 		yield switch (tok.0) {
   1326 		case ltok::SWITCH =>
   1327 			lex::unlex(lexer, tok);
   1328 			return switch_expr(lexer);
   1329 		case ltok::MATCH =>
   1330 			lex::unlex(lexer, tok);
   1331 			return match_expr(lexer);
   1332 		case ltok::COLON, ltok::LBRACE =>
   1333 			lex::unlex(lexer, tok);
   1334 			return compound_expr(lexer);
   1335 		case =>
   1336 			yield tok;
   1337 		};
   1338 	};
   1339 
   1340 	const op = switch (tok.0) {
   1341 	case ltok::MINUS =>
   1342 		yield ast::unarithm_op::MINUS;
   1343 	case ltok::BNOT =>
   1344 		yield ast::unarithm_op::BNOT;
   1345 	case ltok::LNOT =>
   1346 		yield ast::unarithm_op::LNOT;
   1347 	case ltok::TIMES =>
   1348 		yield ast::unarithm_op::DEREF;
   1349 	case ltok::BAND =>
   1350 		yield ast::unarithm_op::ADDR;
   1351 	case => abort();
   1352 	};
   1353 
   1354 	const operand = unarithm(lexer)?;
   1355 	const expr = :blk {
   1356 		if (op == ast::unarithm_op::MINUS) match (operand.expr) {
   1357 		case let c: ast::literal_expr =>
   1358 			match (c) {
   1359 			case let n: ast::number_literal =>
   1360 				let sign = false;
   1361 				const val = match (n.value) {
   1362 				case let i: i64 =>
   1363 					sign = i < 0;
   1364 					yield -i;
   1365 				case let u: u64 => void;
   1366 				case let f: f64 =>
   1367 					sign = math::signf64(f) < 0;
   1368 					yield -f;
   1369 				};
   1370 
   1371 				if (val is void) yield;
   1372 				yield :blk, ast::number_literal {
   1373 					suff = n.suff,
   1374 					value = val as (i64 | f64),
   1375 					sign = sign,
   1376 				}: ast::literal_expr;
   1377 			case => void;
   1378 			};
   1379 		case => void;
   1380 		};
   1381 
   1382 		yield ast::unarithm_expr {
   1383 			op = op,
   1384 			operand = alloc(operand),
   1385 		};
   1386 	};
   1387 	return ast::expr {
   1388 		start = tok.2,
   1389 		end = lex::prevloc(lexer),
   1390 		expr = expr,
   1391 	};
   1392 };
   1393 
   1394 fn yield_expr(lexer: *lex::lexer) (ast::expr | error) = {
   1395 	const start = want(lexer, ltok::YIELD)?;
   1396 	let label = "";
   1397 	let value: nullable *ast::expr = null;
   1398 	match (try(lexer, ltok::COLON, ltok::COMMA, ltok::ELSE, ltok::RBRACE,
   1399 		ltok::RBRACKET, ltok::RPAREN, ltok::SEMICOLON, ltok::EOF)?) {
   1400 	case void =>
   1401 		value = alloc(expr(lexer)?);
   1402 	case let t: lex::token =>
   1403 		if (t.0 == ltok::COLON) {
   1404 			label = want(lexer, ltok::NAME)?.1 as str;
   1405 			match (try(lexer, ltok::COMMA)?) {
   1406 			case void => void;
   1407 			case lex::token =>
   1408 				value = alloc(expr(lexer)?);
   1409 			};
   1410 		} else {
   1411 			lex::unlex(lexer, t);
   1412 		};
   1413 	};
   1414 	return ast::expr {
   1415 		start = start.2,
   1416 		end = lex::prevloc(lexer),
   1417 		expr = ast::yield_expr {
   1418 			label = label,
   1419 			value = value,
   1420 		},
   1421 	};
   1422 };
   1423 
   1424 fn binop_for_tok(tok: lex::token) ast::binarithm_op = {
   1425 	switch (tok.0) {
   1426 	case ltok::BAND =>
   1427 		return ast::binarithm_op::BAND;
   1428 	case ltok::BOR =>
   1429 		return ast::binarithm_op::BOR;
   1430 	case ltok::BXOR =>
   1431 		return ast::binarithm_op::BXOR;
   1432 	case ltok::DIV =>
   1433 		return ast::binarithm_op::DIV;
   1434 	case ltok::GT =>
   1435 		return ast::binarithm_op::GT;
   1436 	case ltok::GTEQ =>
   1437 		return ast::binarithm_op::GTEQ;
   1438 	case ltok::LAND =>
   1439 		return ast::binarithm_op::LAND;
   1440 	case ltok::LEQUAL =>
   1441 		return ast::binarithm_op::LEQUAL;
   1442 	case ltok::LESS =>
   1443 		return ast::binarithm_op::LESS;
   1444 	case ltok::LESSEQ =>
   1445 		return ast::binarithm_op::LESSEQ;
   1446 	case ltok::LOR =>
   1447 		return ast::binarithm_op::LOR;
   1448 	case ltok::LSHIFT =>
   1449 		return ast::binarithm_op::LSHIFT;
   1450 	case ltok::LXOR =>
   1451 		return ast::binarithm_op::LXOR;
   1452 	case ltok::MINUS =>
   1453 		return ast::binarithm_op::MINUS;
   1454 	case ltok::MODULO =>
   1455 		return ast::binarithm_op::MODULO;
   1456 	case ltok::NEQUAL =>
   1457 		return ast::binarithm_op::NEQUAL;
   1458 	case ltok::PLUS =>
   1459 		return ast::binarithm_op::PLUS;
   1460 	case ltok::RSHIFT =>
   1461 		return ast::binarithm_op::RSHIFT;
   1462 	case ltok::TIMES =>
   1463 		return ast::binarithm_op::TIMES;
   1464 	case => abort();
   1465 	};
   1466 };
   1467 
   1468 fn precedence(tok: lex::token) int = {
   1469 	switch (tok.0) {
   1470 	case ltok::LOR =>
   1471 		return 0;
   1472 	case ltok::LXOR =>
   1473 		return 1;
   1474 	case ltok::LAND =>
   1475 		return 2;
   1476 	case ltok::LEQUAL, ltok::NEQUAL =>
   1477 		return 3;
   1478 	case ltok::LESS, ltok::LESSEQ, ltok::GT, ltok::GTEQ =>
   1479 		return 4;
   1480 	case ltok::BOR =>
   1481 		return 5;
   1482 	case ltok::BXOR =>
   1483 		return 6;
   1484 	case ltok::BAND =>
   1485 		return 7;
   1486 	case ltok::LSHIFT, ltok::RSHIFT =>
   1487 		return 8;
   1488 	case ltok::PLUS, ltok::MINUS =>
   1489 		return 9;
   1490 	case ltok::TIMES, ltok::DIV, ltok::MODULO =>
   1491 		return 10;
   1492 	case =>
   1493 		return -1;
   1494 	};
   1495 };