File: make_interpret_functions.pike

package info (click to toggle)
pike8.0 8.0.702-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 79,608 kB
  • sloc: ansic: 266,508; xml: 186,324; makefile: 3,537; sh: 1,731; cpp: 1,328; lisp: 655; awk: 441; asm: 242; objc: 240; pascal: 157; perl: 34; sed: 34
file content (160 lines) | stat: -rw-r--r-- 3,524 bytes parent folder | download | duplicates (6)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#! /usr/bin/env pike

mapping ops=([]);

int errors;

array fixit2(array x)
{
  array ret=({});
  array opcodes=({});
  for(int e=0;e<sizeof(x);e++)
    {
      if(objectp(x[e]) && ops[(string)x[e]])
      {
	opcodes+=x[e..e]+fixit2(x[e+1])[0];
	array args = x[e+1]/ ({ Parser.Pike.Token(",") });
	array tmp = fixit2(args[-1][0]);
	
	ret+=({ tmp[0] });
	opcodes+=tmp[1];
	e++;
	// Note: There's always an '}' at the end of x.
	if ((string)x[e+1] != ";") {
	  werror("Missing semicolon after definition of opcode %s(%s)\n",
		 (string)args[0][1], (string)args[1][0]);
	  errors++;
	} else {
	  ret += ({ x[++e] });
	}
	// Note: There must not be code after a nested opcode definition,
	//       it will not work in the byte-code interpreter case.
	if (x[e+1] != "}") {
	  werror("Remainder: after %s(%s): %O\n",
		 (string)args[0][1], (string)args[1][0], x[e+1..]);
	  errors++;
	}
      }
      else if(arrayp(x[e]))
      {
	array tmp=fixit2(x[e]);
	ret+=tmp[0];
	opcodes+=tmp[1];
      }
      else
      {
	ret+=({x[e]});
      }
    }
  return ({ ret, opcodes });
}

array fixit(array x)
{
  array ret=({});
  for(int e=0;e<sizeof(x);e++)
    {
      if(objectp(x[e]) && ops[(string)x[e]])
      {
//	werror("DOING %s\n",(string)(x[e+1][1]));
	array tmp=fixit2(x[e+1]);
	ret+=({x[e]}) + tmp[0] + tmp[1];
	e++;

	/* Skip traling ; */
	if(e+1<sizeof(x) && objectp(x[e+1]) && ";" == (string)x[e+1])
	{
	  object tmp=x[e+1];
	  tmp->text="";
	  ret+=({tmp});
	  e++;
	}
      }
      else
      {
	ret+=({x[e]});
      }
    }
  return ret;
}

array filter_out_cases(array x)
{
  array y=x/({ Parser.Pike.Token("CASE") });
  for(int e=1;e<sizeof(y);e++) y[e]=y[e][1..];
  return y*({});
}

array fix_cases(array x)
{
  int start=0;
  int undone=0;
  array ret=({});

  while(1)
  {
    int casepos=search(x,Parser.Pike.Token("CASE"), start);
    //werror("%O\n",Parser.Pike.simple_reconstitute(x[casepos..casepos+1]));
    if(casepos == -1) break;
    int donepos=search(x,Parser.Pike.Token("DONE"),casepos+1);
    
    ret+=x[undone..casepos-1]+
      ({
	Parser.Pike.Token("OPCODE0"),
	  ({
	    Parser.Pike.Token("("),
	      x[casepos+1][1],
	      Parser.Pike.Token(","),
	      Parser.Pike.Token(sprintf("%O",(string)x[casepos+1][1])),
	      Parser.Pike.Token(","),
	      ({ Parser.Pike.Token("{") })+
	      filter_out_cases(x[casepos+2..donepos-1])+
	      ({  Parser.Pike.Token("}") }),
	      Parser.Pike.Token(")"),
	      Parser.Pike.Token(";"),
	      })
	  });
    
    start=casepos+1;
    undone=donepos+1;
  }

  return ret + x[undone..];
}

int main(int argc, array(string) argv)
{
  if(argc<2 || argv[1]=="--help") {
    werror("Generates interpret_functions_fixed.h from interpret_functions.h\n"
	   "Usage: make_interpret_functions.pike interpret_functions.h > outfile\n");
    return 1;
  }

  string file=argv[1];
  mixed x=Stdio.read_file(file);
  x -= "\r"; // < 7.6.120 didn't hide \r.
  x=Parser.Pike.split(x);
  x=Parser.Pike.tokenize(x,file);
  x=Parser.Pike.hide_whitespaces(x);
  x=Parser.Pike.group(x);

  for(int e=0;e<=2;e++) {
    foreach( ({"","TAIL"}), string tail) {
      foreach( ({"", "JUMP", "PTRJUMP", "RETURN", "BRANCH"}), string kind) {
	string postfix = tail+kind;
	if (sizeof(postfix)) postfix = "_" + postfix;
	ops[sprintf("OPCODE%d%s",e,postfix)]=1;
      }
    }
  }

  x=fix_cases(x);
  x=fixit(x);

  if(getenv("PIKE_DEBUG_PRECOMPILER"))
    write(Parser.Pike.simple_reconstitute(x));
  else
    write(Parser.Pike.reconstitute_with_line_numbers(x));

  return errors;
}