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

Commit 6d0a0ea

Browse files
Merge pull request #36 from code4teaching/sim-ganzzahl
Simulation: Ganzzahl-Division, Aussprung und Aufruf
2 parents b78d097 + 79433d8 commit 6d0a0ea

60 files changed

Lines changed: 1115 additions & 519 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/CodeGeneration/VisuStructCodeGenerator+Emit.swift‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ extension VisuStructCodeGenerator {
166166
out += blockComment(lines: forDiagram, target: target, indent: indent)
167167
}
168168
}
169+
// Im JavaScript-Export: `/` zwischen ganzen Zahlen wie in Java abschneiden
170+
func js(_ code: String) -> String {
171+
target == .javascript ? javaScriptIntegerDivision(code, intVariables: options.jsIntVariables) : code
172+
}
169173
for raw in body {
170174
if raw.isEmpty { continue }
171175
if isDiagramEmptyPlaceholderText(raw) {
@@ -174,53 +178,49 @@ extension VisuStructCodeGenerator {
174178
}
175179
if target == .java {
176180
if let s = emitJavaInputLineIfNeeded(raw, indent: indent, arrayTypes: options.arrayElementTypes) {
177-
out += s
181+
out += js(s)
178182
continue
179183
}
180184
if let s = emitJavaPrintLineIfNeeded(raw, indent: indent) {
181-
out += s
185+
out += js(s)
182186
continue
183187
}
184188
}
185189
if target == .javascript {
186190
if let s = emitJavaScriptInputLineIfNeeded(raw, indent: indent, arrayTypes: options.arrayElementTypes) {
187-
out += s
191+
out += js(s)
188192
continue
189193
}
190194
if let s = emitJavaScriptPrintLineIfNeeded(raw, indent: indent) {
191-
out += s
195+
out += js(s)
192196
continue
193197
}
194198
}
195199
if target == .python {
196200
if let s = emitPythonInputLineIfNeeded(raw, indent: indent, arrayTypes: options.arrayElementTypes) {
197-
out += s
201+
out += js(s)
198202
continue
199203
}
200204
if let s = emitPythonPrintLineIfNeeded(raw, indent: indent, options: options) {
201-
out += s
205+
out += js(s)
202206
continue
203207
}
204208
}
205209
if let s = emitConstTypedLineIfNeeded(raw, target: target, indent: indent, intVariables: options.jsIntVariables) {
206-
out += s
210+
out += js(s)
207211
continue
208212
}
209213
if let s = emitTranslatedJavaLikeAssignmentIfNeeded(raw, target: target, indent: indent, options: options) {
210-
out += s
214+
out += js(s)
211215
continue
212216
}
213217
if let s = emitBareLeadingStringAsPrintIfNeeded(raw, target: target, indent: indent, options: options) {
214-
out += s
218+
out += js(s)
215219
continue
216220
}
217221
let line: String = {
218222
if isCStyleSemicolonTarget(target) {
219-
if target == .javascript,
220-
let transformed = javaScriptIntegerDivisionIfNeeded(raw, intVariables: options.jsIntVariables) {
221-
return formatCStyleStatementLine(transformed)
222-
}
223-
return formatCStyleStatementLine(raw)
223+
return formatCStyleStatementLine(js(raw))
224224
}
225225
return pythonStatementLine(raw, intVariables: options.jsIntVariables, stringVariables: options.stringVariables)
226226
}()
@@ -248,7 +248,7 @@ extension VisuStructCodeGenerator {
248248
case .python:
249249
out += sp(indent) + prefix + pythonExpression(cond, intVariables: options.jsIntVariables) + suffix
250250
case .javascript:
251-
out += sp(indent) + prefix + cond + suffix
251+
out += sp(indent) + prefix + javaScriptIntegerDivision(cond, intVariables: options.jsIntVariables) + suffix
252252
}
253253
}
254254

‎VisuStruct-iOS/VisuStructCore/Sources/VisuStructCore/CodeGeneration/VisuStructCodeGenerator+JavaLike.swift‎

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -299,31 +299,32 @@ extension VisuStructCodeGenerator {
299299
return out
300300
}
301301

302-
static func javaScriptIntegerDivisionIfNeeded(_ raw: String, intVariables: Set<String>) -> String? {
303-
let t = stripTrailingSemicolon(raw).trimmingCharacters(in: .whitespacesAndNewlines)
304-
if t.isEmpty { return nil }
305-
306-
// x = x / y (einfaches Muster, ohne weitere Operatoren)
307-
let assignPat = #"^\s*([a-zA-Z_]\w*)\s*=\s*([a-zA-Z_]\w*|\d+)\s*/\s*([a-zA-Z_]\w*|\d+)\s*$"#
308-
if let g = regexGroups(assignPat, t), g.count >= 4 {
309-
let lhs = g[1]
310-
let a = g[2]
311-
let b = g[3]
312-
guard intVariables.contains(lhs) else { return nil }
313-
// Nur wenn der LHS auch wirklich der Dividend ist (typischer int-div Update).
314-
guard lhs == a else { return nil }
315-
return "\(lhs) = Math.floor(\(a) / \(b))"
302+
/// Wie in Java: `/` zwischen ganzen Zahlen schneidet in JavaScript mit `Math.trunc` ab, auch bei `x /= y`.
303+
static func javaScriptIntegerDivision(_ code: String, intVariables: Set<String>) -> String {
304+
func isInt(_ operand: String) -> Bool {
305+
!operand.isEmpty && (operand.allSatisfy { $0.isASCII && $0.isNumber } || intVariables.contains(operand))
316306
}
317-
318-
// x /= y
319-
let divEqPat = #"^\s*([a-zA-Z_]\w*)\s*/=\s*([a-zA-Z_]\w*|\d+)\s*$"#
320-
if let g2 = regexGroups(divEqPat, t), g2.count >= 3 {
321-
let lhs = g2[1]
322-
let b = g2[2]
323-
guard intVariables.contains(lhs) else { return nil }
324-
return "\(lhs) = Math.floor(\(lhs) / \(b))"
307+
return mapOutsideStringLiterals(code) { part in
308+
let divided = replaceSimpleRegex(
309+
part,
310+
pattern: #"(?<![\w./])([a-zA-Z_]\w*|\d+)\s*/\s*([a-zA-Z_]\w*|\d+)(?![\w.(\[/])"#,
311+
options: [],
312+
transform: { m, ns in
313+
let a = ns.substring(with: m.range(at: 1))
314+
let b = ns.substring(with: m.range(at: 2))
315+
return isInt(a) && isInt(b) ? "Math.trunc(\(a) / \(b))" : ns.substring(with: m.range)
316+
}
317+
)
318+
return replaceSimpleRegex(
319+
divided,
320+
pattern: #"(?<![\w.])([a-zA-Z_]\w*)\s*/=\s*([a-zA-Z_]\w*|\d+)(?![\w.(\[])"#,
321+
options: [],
322+
transform: { m, ns in
323+
let a = ns.substring(with: m.range(at: 1))
324+
let b = ns.substring(with: m.range(at: 2))
325+
return isInt(a) && isInt(b) ? "\(a) = Math.trunc(\(a) / \(b))" : ns.substring(with: m.range)
326+
}
327+
)
325328
}
326-
327-
return nil
328329
}
329330
}

0 commit comments

Comments
 (0)