add yield

This commit is contained in:
2026-06-27 04:12:04 +02:00
parent b4ce2c2117
commit f610be1b59
8 changed files with 426 additions and 16 deletions
+51
View File
@@ -2124,6 +2124,57 @@ bad_return_in_defer :: func() void {
testing.expect(t, return_in_defer)
}
@(test)
yield_compiles_and_runs :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-yield"
defer _ = os.remove(output)
status := compiler_core.compile_package("examples/programs/yield", output)
testing.expect_value(t, status, 0)
state := run_executable(output)
// Untyped/typed value blocks, the value captured before block defers run, and
// reassignment from a value block together produce 42.
testing.expect_value(t, state.exit_code, 42)
}
@(test)
yield_misuse_is_diagnosed :: proc(t: ^testing.T) {
// A value block that does not end in `yield`, and a `yield` nested inside an
// `if` within a value block (only the final statement may yield).
text := `main :: func() i32 {
missing :: {
k :: 5
}
nested :: {
if (true) {
yield 1
}
yield 2
}
return missing + nested
}
`
source_file := source.Source{path="test.bro", text=text}
diagnostics := source.init_diagnostics(&source_file)
defer source.destroy_diagnostics(&diagnostics)
symbols := symbol.init_table()
defer symbol.destroy_table(&symbols)
stream := lexer.lex(&source_file, &diagnostics, &symbols)
defer delete(stream.items)
ast_module := parser.parse(&stream, &source_file, &diagnostics)
defer ast.destroy_module(&ast_module)
hir_module := checker.check(&ast_module, &diagnostics, &symbols)
defer hir.destroy_module(&hir_module)
missing_yield := false
misplaced_yield := false
for diagnostic in diagnostics.items {
missing_yield = missing_yield || strings.contains(diagnostic.message, "a value block must end with an explicit 'yield'")
misplaced_yield = misplaced_yield || strings.contains(diagnostic.message, "'yield' is only valid as the final statement of a value block")
}
testing.expect(t, missing_yield)
testing.expect(t, misplaced_yield)
}
@(test)
foreign_function_links_from_c_source :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-foreign-source"