Skip to content

Commit

Permalink
fix: fix traceProgram()
Browse files Browse the repository at this point in the history
  • Loading branch information
exKAZUu committed Sep 17, 2024
1 parent 82bd170 commit 9015b54
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 238 deletions.
31 changes: 15 additions & 16 deletions src/problems/problemData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,7 @@ s.set('x', double(<2-3>)); // CP
forwardGivenSteps(s.get('c'), s.get('x'));
function forwardGivenSteps(c, n) {
for (s.set('i', 0); s.get('i') < n; s.set('i', s.get('i') + 1)) {
for (s.set('i', 0); s.get('i') < n; s.set('i', s.get('i') + 1)) {
c.forward();
}
}
Expand Down Expand Up @@ -974,10 +974,10 @@ public class Main {
s.set('c', new Character());
for (s.set('i', 0); s.get('i') < 3; s.set('i', s.get('i') + 1)) {
for (s.set('j', 0); s.get('j') < 3; s.set('j', s.get('j') + 1)) {
if (isEqual(s.get('i'), s.get('j')))
s.get('c').turnRight(); // CP
else
forwardTwoSteps(s.get('c'));
if (isEqual(s.get('i'), s.get('j')))
s.get('c').turnRight(); // CP
else
forwardTwoSteps(s.get('c'));
}
s.get('c').turnLeft();
}
Expand Down Expand Up @@ -1025,7 +1025,7 @@ for (s.set('i', 0); s.get('i') < s.get('arr').length; s.set('i', s.get('i') + 1)
}
function forwardGivenSteps(c, n) {
for (s.set('j', 0); s.get('j') < n; s.set('j', s.get('j') + 1)) {
for (s.set('j', 0); s.get('j') < n; s.set('j', s.get('j') + 1)) {
c.forward();
}
}
Expand Down Expand Up @@ -1274,9 +1274,9 @@ s.set('c', s.get('a') * 2);
function f(x, y) {
try {
s.enterNewScope();
s.set('ret', x * y);
return s.get('ret');
s.enterNewScope([['x', x], ['y', y]]);
s.set('a', x * y);
return s.get('a');
} finally {
s.leaveScope();
}
Expand All @@ -1293,10 +1293,11 @@ public class Main {
a = f(a, b); // sid: 3
}
int c = a * 2; // sid: 4
}
public static int f(int x, int y) {
return x * y; // sid: 5
}
public static int f(int x, int y) {
int a = x * y; // sid: 5
return a;
}
}
`.trim(),
Expand All @@ -1305,8 +1306,7 @@ public class Main {
instrumented: `
s.set('c1', new Character());
s.get('c1').forward();
s.get('c1').turnLeft();
s.get('c1').penUp();
s.get('c1').turnRight();
s.set('i', 0);
s.get('c1').forward();
Expand All @@ -1323,8 +1323,7 @@ public class Main {
public static void main(String[] args) {
Character c1 = new Character(); // sid
c1.forward(); // sid
c1.turnLeft(); // sid
c1.penUp(); // sid
c1.turnRight(); // sid
int i = 0; // sid
c1.forward(); // sid
Expand Down
19 changes: 5 additions & 14 deletions src/problems/traceProgram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,26 +88,17 @@ class Scope {
if (this.vars[varName] !== undefined) {
return this.vars[varName];
}
if (this.parent) {
return this.parent.get(varName);
}
throw new Error();
}
set(varName, value, sid) {
if (!this.update(varName, value)) {
this.vars[varName] = value;
}
this.vars[varName] = value;
addTrace(sid);
}
update(varName, value) {
if (this.vars[varName] !== undefined) {
this.vars[varName] = value;
return true;
}
return this.parent && this.parent.update(varName, value);
}
enterNewScope() {
enterNewScope(params) {
s = new Scope(this);
for (const [k, v] of params) {
s.vars[k] = v;
}
}
leaveScope() {
if (!this.parent) throw new Error();
Expand Down
2 changes: 1 addition & 1 deletion src/utils/platform.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const macosPlatforms = /(macintosh|macintel|macppc|mac68k|macos)/i;

export function isMacOS(): boolean {
return macosPlatforms.test(window.navigator.userAgent.toLowerCase());
return typeof window !== 'undefined' && macosPlatforms.test(window.navigator.userAgent.toLowerCase());
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, test } from 'vitest';

import { generateProblem } from '../src/problems/generateProblem';
import { generateProblem } from '../../src/problems/generateProblem';

test.each([
{
Expand Down
Loading

0 comments on commit 9015b54

Please sign in to comment.