Skip to content
This repository was archived by the owner on Nov 7, 2026. It is now read-only.

Commit 79433d8

Browse files
committed
Simulation: Aussprung und Aufruf ausführen
Der Aussprung verlässt wie break die innerste Schleife. Außerhalb jeder Schleife beendet er das Unterprogramm bzw. das Programm. Ein Aufruf sucht unter den anderen offenen Reitern das Diagramm mit dem passenden Titel (ohne Klammern, Groß- und Kleinschreibung egal), führt es mit denselben Variablen aus und macht danach weiter. Währenddessen bleibt der Aufruf-Kasten markiert. Fehlt das Diagramm, wird der Aufruf mit einem Hinweis übersprungen; zu tiefe Aufrufketten brechen ab. Web und App gleich, Hinweise in allen 21 Sprachen, Lektion 15 angepasst. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5L4wNpcXBufwAn6U3AMDj
1 parent eab8dd7 commit 79433d8

55 files changed

Lines changed: 875 additions & 342 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎VisuStruct-iOS/VisuStructCore/Sources/VisuStructCore/VisuStructSimulationEngine.swift‎

Lines changed: 233 additions & 74 deletions
Large diffs are not rendered by default.

‎VisuStruct-iOS/VisuStructCore/Tests/VisuStructCoreTests/SimulationEngineTests.swift‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,72 @@ final class SimulationEngineTests: XCTestCase {
175175
XCTAssertEqual(state.outputLines, ["q=3 d=3.0 e=3.5 n=-3"])
176176
}
177177

178+
func testJumpLeavesInnermostLoop() {
179+
let statement = { (text: String) in VisuStructElement(type: .anweisung, textLines: [text]) }
180+
let jump = VisuStructElement(type: .aussprung, textLines: ["Schleife verlassen"])
181+
let document = VisuStructDocument(elements: [
182+
statement("int summe = 0"),
183+
VisuStructElement(type: .endlosschleife, textLines: [], loopBody: [
184+
statement("input: int zahl \"Zahl?\""),
185+
VisuStructElement(type: .verzweigung, textLines: ["zahl == 0"], cases: [
186+
VisuStructCase(name: "true", elements: [jump]),
187+
VisuStructCase(name: "false", elements: [statement("summe = summe + zahl")]),
188+
]),
189+
]),
190+
VisuStructElement(type: .forSchleife, textLines: ["int i = 0", "i < 10", "i++"], loopBody: [
191+
VisuStructElement(type: .verzweigung, textLines: ["i == 2"], cases: [
192+
VisuStructCase(name: "true", elements: [jump]),
193+
VisuStructCase(name: "false", elements: [statement("ø")]),
194+
]),
195+
]),
196+
statement("print: \"Summe: \" + summe + \" i=\" + i"),
197+
jump,
198+
statement("print: \"nie\""),
199+
])
200+
let state = runSimulation(document, inputs: ["3", "4", "0"])
201+
XCTAssertEqual(state.outputLines, ["Summe: 7 i=2"])
202+
XCTAssertEqual(state.notice, .finished)
203+
}
204+
205+
func testCallRunsSubprogram() {
206+
let statement = { (text: String) in VisuStructElement(type: .anweisung, textLines: [text]) }
207+
let call = { (name: String) in VisuStructElement(type: .aufruf, textLines: [name]) }
208+
let titel = VisuStructDocument(caption: "zeigeTitel()", elements: [statement("print: \"== Quiz ==\""), statement("int punkte = 0")])
209+
let zaehlen = VisuStructDocument(caption: "zaehlen", elements: [
210+
VisuStructElement(type: .whileSchleife, textLines: ["true"], loopBody: [statement("punkte++"), VisuStructElement(type: .aussprung)]),
211+
statement("print: \"punkte=\" + punkte"),
212+
VisuStructElement(type: .aussprung),
213+
statement("print: \"nie\""),
214+
])
215+
let main = VisuStructDocument(elements: [
216+
call("zeigeTitel()"), call("zaehlen();"), call("zaehlen()"), call("fehlt()"), statement("print: \"Ende \" + punkte"),
217+
])
218+
var engine = VisuStructSimulationEngine(document: main, subprograms: ["zeigetitel": titel, "zaehlen": zaehlen])
219+
engine.step()
220+
XCTAssertEqual(engine.currentStepPath, [0])
221+
var sawNotFound = false
222+
while engine.canStep {
223+
engine.step()
224+
if engine.state.notice == .subprogramNotFound("fehlt()") { sawNotFound = true }
225+
}
226+
XCTAssertTrue(sawNotFound)
227+
XCTAssertEqual(engine.state.outputLines, ["== Quiz ==", "punkte=1", "punkte=2", "Ende 2"])
228+
XCTAssertEqual(engine.state.notice, .finished)
229+
XCTAssertEqual(VisuStructSimulationEngine.subprogramKey(" ZeigeTitel( ); "), "zeigetitel")
230+
}
231+
232+
func testSelfCallStopsWithNotice() {
233+
let selfCall = VisuStructDocument(caption: "rekursion", elements: [VisuStructElement(type: .aufruf, textLines: ["rekursion()"])])
234+
var engine = VisuStructSimulationEngine(document: selfCall, subprograms: ["rekursion": selfCall])
235+
var count = 0
236+
while engine.canStep, count < 1000 {
237+
engine.step()
238+
count += 1
239+
}
240+
XCTAssertTrue(engine.state.isHalted)
241+
XCTAssertEqual(engine.state.notice, .callTooDeep)
242+
}
243+
178244
func testBooleanInput() {
179245
let document = VisuStructDocument(elements: [
180246
VisuStructElement(type: .anweisung, textLines: ["input: boolean ok \"Okay?\""]),

‎VisuStruct-iOS/VisuStructCore/Tests/VisuStructCoreTests/TestSupport.swift‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ enum ExampleFiles {
3636
func runSimulation(
3737
_ document: VisuStructDocument,
3838
inputs: [String] = [],
39+
subprograms: [String: VisuStructDocument] = [:],
3940
maxSteps: Int = 10_000,
4041
file: StaticString = #filePath,
4142
line: UInt = #line
4243
) -> VisuStructSimulationState {
43-
var engine = VisuStructSimulationEngine(document: document)
44+
var engine = VisuStructSimulationEngine(document: document, subprograms: subprograms)
4445
var remainingInputs = inputs[...]
4546
var stepsTaken = 0
4647
while stepsTaken < maxSteps {

‎VisuStruct-iOS/VisuStructXcodeOpen/VisuStruct/Simulation/SimulationController.swift‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ final class SimulationController {
2020
self.engine = VisuStructSimulationEngine(document: tabsModel.currentTab.document)
2121
}
2222

23+
/// Die anderen offenen Reiter, nach Titel und Diagrammtitel, damit ein Aufruf wie `zeigeTitel()` sie findet.
24+
private var subprograms: [String: VisuStructDocument] {
25+
var result: [String: VisuStructDocument] = [:]
26+
for (index, tab) in tabsModel.tabs.enumerated() where index != tabsModel.currentTabIndex {
27+
for name in [tab.document.caption, tab.title] {
28+
let key = VisuStructSimulationEngine.subprogramKey(name)
29+
if !key.isEmpty, result[key] == nil { result[key] = tab.document }
30+
}
31+
}
32+
return result
33+
}
34+
2335
/// Wie `@AppStorage` in den Einstellungen; bei jedem Schritt neu gelesen, damit Änderungen während des Abspielens greifen.
2436
private var stepDelay: SimulationStepDelayOption {
2537
SimulationStepDelayOption.fromStored(
@@ -133,7 +145,7 @@ final class SimulationController {
133145

134146
/// Neue Engine für das aktuelle Dokument, Markierung auf den ersten Schritt.
135147
private func restartEngine() {
136-
var fresh = VisuStructSimulationEngine(document: tabsModel.currentTab.document)
148+
var fresh = VisuStructSimulationEngine(document: tabsModel.currentTab.document, subprograms: subprograms)
137149
fresh.reset()
138150
engine = fresh
139151
inputText = ""

‎VisuStruct-iOS/VisuStructXcodeOpen/VisuStruct/Simulation/SimulationPanels.swift‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,10 @@ extension VisuStructSimulationNotice {
374374
return lang.tr("sim_notice_number_expected")
375375
case .booleanExpected:
376376
return lang.tr("sim_notice_boolean_expected")
377+
case .subprogramNotFound(let name):
378+
return String(format: lang.tr("sim_notice_subprogram_not_found_fmt"), name)
379+
case .callTooDeep:
380+
return lang.tr("sim_notice_call_too_deep")
377381
case .other(let text):
378382
return text
379383
}

‎VisuStruct-iOS/VisuStructXcodeOpen/VisuStruct/ar.lproj/Localizable.strings‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@
231231
"sim_notice_whole_number_expected" = "أدخل عددًا صحيحًا.";
232232
"sim_notice_number_expected" = "أدخل رقمًا.";
233233
"sim_notice_boolean_expected" = "أدخل true أو false.";
234+
"sim_notice_subprogram_not_found_fmt" = "لا يوجد مخطط مفتوح باسم %@. تم تخطي الاستدعاء.";
235+
"sim_notice_call_too_deep" = "استدعاءات متداخلة كثيرة جدًا. هل يستدعي برنامج فرعي نفسه؟";
234236

235237
"codegen_pseudocode_toggle" = "شبه كود (ليس كودًا قابلًا للتشغيل)";
236238
"codegen_pseudocode_note" = "شبه كود – ليس برنامجًا قابلًا للتشغيل";

‎VisuStruct-iOS/VisuStructXcodeOpen/VisuStruct/da.lproj/Localizable.strings‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@
141141
"sim_notice_whole_number_expected" = "Indtast et heltal.";
142142
"sim_notice_number_expected" = "Indtast et tal.";
143143
"sim_notice_boolean_expected" = "Indtast true eller false.";
144+
"sim_notice_subprogram_not_found_fmt" = "Intet åbent diagram hedder %@. Kaldet blev sprunget over.";
145+
"sim_notice_call_too_deep" = "For mange indlejrede kald. Kalder et underprogram sig selv?";
144146
"phone_hide_text_editor" = "Skjul";
145147
"phone_title_done" = "Færdig";
146148

‎VisuStruct-iOS/VisuStructXcodeOpen/VisuStruct/de.lproj/Localizable.strings‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@
232232
"sim_notice_whole_number_expected" = "Bitte eine ganze Zahl eingeben.";
233233
"sim_notice_number_expected" = "Bitte eine Zahl eingeben.";
234234
"sim_notice_boolean_expected" = "Bitte true oder false eingeben.";
235+
"sim_notice_subprogram_not_found_fmt" = "Kein offenes Diagramm heißt %@. Der Aufruf wurde übersprungen.";
236+
"sim_notice_call_too_deep" = "Zu viele verschachtelte Aufrufe. Ruft sich ein Unterprogramm selbst auf?";
235237

236238
"codegen_pseudocode_toggle" = "Pseudocode (kein lauffähiger Code)";
237239
"codegen_pseudocode_note" = "Pseudocode – kein lauffähiges Programm";

‎VisuStruct-iOS/VisuStructXcodeOpen/VisuStruct/el.lproj/Localizable.strings‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@
228228
"sim_notice_whole_number_expected" = "Εισαγάγετε έναν ακέραιο αριθμό.";
229229
"sim_notice_number_expected" = "Εισαγάγετε έναν αριθμό.";
230230
"sim_notice_boolean_expected" = "Πληκτρολογήστε true ή false.";
231+
"sim_notice_subprogram_not_found_fmt" = "Κανένα ανοιχτό διάγραμμα δεν ονομάζεται %@. Η κλήση παραλείφθηκε.";
232+
"sim_notice_call_too_deep" = "Πάρα πολλές ένθετες κλήσεις. Καλεί ένα υποπρόγραμμα τον εαυτό του;";
231233

232234
"codegen_pseudocode_toggle" = "Ψευδοκώδικας (μη εκτελέσιμος κώδικας)";
233235
"codegen_pseudocode_note" = "Ψευδοκώδικας – όχι εκτελέσιμο πρόγραμμα";

‎VisuStruct-iOS/VisuStructXcodeOpen/VisuStruct/en.lproj/Localizable.strings‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@
232232
"sim_notice_whole_number_expected" = "Please enter a whole number.";
233233
"sim_notice_number_expected" = "Please enter a number.";
234234
"sim_notice_boolean_expected" = "Please enter true or false.";
235+
"sim_notice_subprogram_not_found_fmt" = "No open diagram is called %@. The call was skipped.";
236+
"sim_notice_call_too_deep" = "Too many nested calls. Does a subprogram call itself?";
235237

236238
"codegen_pseudocode_toggle" = "Pseudocode (not runnable code)";
237239
"codegen_pseudocode_note" = "Pseudocode – not a runnable program";

0 commit comments

Comments
 (0)