summaryrefslogtreecommitdiff
path: root/Master/texmf/asymptote/plain_debugger.asy
blob: 8568ee0bd8d1d7f9ea81361a2b3c07adad2a71f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
int debuggerlines=5;

int sourceline(string file, string text)
{
  string file=locatefile(file);
  string[] source=input(file);
  for(int line=0; line < source.length; ++line)
    if(find(source[line],text) >= 0) return line+1;
  write("no matching line in "+file+": \""+text+"\"");
  return 0;
}

void stop(string file, string text, code s=quote{})
{
  int line=sourceline(file,text);
  if(line > 0) stop(file,line,s);
}

void clear(string file, string text)
{
  int line=sourceline(file,text);
  if(line > 0) clear(file,line);
}

// Enable debugging.
bool debugging=true;

// Variables used by conditional expressions:
// e.g. stop("test",2,quote{ignore=(++count <= 10);});

bool ignore;
int count=0;

string debugger(string file, int line, int column, code s=quote{})
{
  int verbose=settings.verbose;
  settings.verbose=0;
  _eval(s,true);
  if(ignore) {
    ignore=false;
    settings.verbose=verbose;
    return "c";
  }
  static string s;
  if(debugging) {
    static string lastfile;
    static string[] source;
    bool help=false;
    while(true) {
      if(file != lastfile && file != "-") {source=input(file); lastfile=file;}
      write();
      for(int i=max(line-debuggerlines,0); i < min(line,source.length); ++i)
        write(source[i]);
      for(int i=0; i < column-1; ++i)
        write(" ",none);
      write("^"+(verbose == 5 ? " trace" : ""));

      if(help) {
        write("c:continue f:file h:help i:inst n:next r:return s:step t:trace q:quit x:exit");
        help=false;
      }

      string Prompt=file+": "+(string) line+"."+(string) column;
      Prompt += "? [%s] ";
      s=getstring(name="debug",default="h",prompt=Prompt,store=false);
      if(s == "h" || s == "?") {help=true; continue;}
      if(s == "c" || s == "s" || s == "n" || s == "i" || s == "f" || s == "r")
        break;
      if(s == "q") {debugging=false; abort();} // quit
      if(s == "x") {debugging=false; return "";} // exit
      if(s == "t") { // trace
        if(verbose == 0) {
          verbose=5;
        } else {
          verbose=0;
        }
        continue;
      }
      _eval(s+";",true);
    }
  }
  settings.verbose=verbose;
  return s;
}

atbreakpoint(debugger);