This repository has been archived by the owner on Jun 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 204
/
interpret_php.ml
467 lines (400 loc) · 11 KB
/
interpret_php.ml
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
(* Yoann Padioleau
*
* Copyright (C) 2010 Facebook
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation, with the
* special exception on linking described in file license.txt.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file
* license.txt for more details.
*)
open Common
open Ast_php
module Ast = Ast_php
module R = Runtime_php
module B = Lib_builtins_php
(*****************************************************************************)
(* Prelude *)
(*****************************************************************************)
(*
* Why ? There is already the Zend PHP interpreter and HPHPi.
*
* It can help me learn the complex PHP semantic which can be useful
* later when designing bug-finding tools for PHP.
* Also I never really wrote some interpreters/JIT/compilers so I have
* no gut feelings about their relative cost. How fast can be a naive
* PHP interpreter in OCaml vs the C Zend version ? vs a naive JIT ?
*
* references:
* - the art of the interpreter ?
* - PL zoo, http://andrej.com/plzoo/
* - MiniCaml by ejiro sumii
* - The book by leroy on OCaml contains an interpreter for pascal
* - SICP, EOPL, etc
*
* related work: Lua-ML
*)
(*****************************************************************************)
(* types *)
(*****************************************************************************)
(*****************************************************************************)
(* Helpers *)
(*****************************************************************************)
let lookup_env s h =
Hashtbl.find h s
(* todo: maybe clearer to make i_stmt return a either3 ?
*)
exception ReturnExn of R.rval
let is_true_for_cond_v v =
match v with
| R.RBool b -> b
| _ -> raise Todo
let arith_cmp cmp a b =
let b =
match a, b with
| R.RInt a, R.RInt b -> cmp a b
| _ -> raise Todo
in
R.RBool b
let arith_int op a b =
let i =
match a, b with
| R.RInt a, R.RInt b -> op a b
| _ -> raise Todo
in
R.RInt i
(*****************************************************************************)
(* Main entry points *)
(*****************************************************************************)
(* semi global technique *)
let env = R.default_env ()
let rec interpret_file file =
let (ast2, _stat) = Parse_php.parse file in
let ast = Parse_php.program_of_program2 ast2 in
interpret ast
and interpret asts =
asts |> List.iter (fun ast ->
match ast with
| StmtList xs ->
xs |> List.iter (fun x ->
i_stmt x |> ignore;
);
| FuncDef def ->
let s = Ast.name def.f_name in
Hashtbl.add env.R.functions s def
| ClassDef def ->
raise Todo
| InterfaceDef def ->
raise Todo
| Halt _ -> raise Todo
| NotParsedCorrectly _ ->
pr2 "NotParsedCorrectly";
| FinalDef _ -> ()
)
and i_stmt st =
match st with
| ExprStmt ((e, tok)) ->
raise Todo
| EmptyStmt tok ->
raise Todo
| Block xs_brace ->
let xs = Ast.unbrace xs_brace in
i_stmt_and_defs xs
| If ((tok, eparen, stmt, elseif, elseopt)) ->
let e = Ast.unparen eparen in
let v = i_expr e in
if is_true_for_cond_v v
then
i_stmt stmt
else
(match elseif with
| [] ->
(match elseopt with
| None ->
R.RNull (* ?? *)
| Some (tok, elsest) ->
i_stmt elsest
)
| x::xs ->
raise Todo
)
| IfColon ((v1, v2, v3, v4, v5, v6, v7, v8)) ->
raise Todo
| While ((v1, v2, v3)) ->
raise Todo
| Do ((v1, v2, v3, v4, v5)) ->
raise Todo
| For ((v1, v2, v3, v4, v5, v6, v7, v8, v9)) ->
raise Todo
| Switch ((v1, v2, v3)) ->
raise Todo
| Foreach ((v1, v2, v3, v4, v5, v6, v7, v8)) ->
raise Todo
| Break ((v1, v2, v3)) ->
raise Todo
| Continue ((v1, v2, v3)) ->
raise Todo
| Return ((t1, eopt, t2)) ->
(match eopt with
| None -> R.RNull (* ? *)
| Some e ->
let v = i_expr e in
raise (ReturnExn v)
)
| Throw ((v1, v2, v3)) ->
raise Todo
| Try ((v1, v2, v3, v4)) ->
raise Todo
| Echo ((v1, es, v3)) ->
(* order of evaluation ? left to right ? *)
let vs = es |> List.map i_expr in
vs |> List.iter (fun v ->
B.echo v
);
R.RNull (* ? *)
| InlineHtml v1 ->
raise Todo
| Globals ((v1, v2, v3)) ->
raise Todo
| StaticVars ((v1, v2, v3)) ->
raise Todo
| Ast.Use ((v1, v2, v3)) ->
raise Todo
| Unset ((v1, v2, v3)) ->
raise Todo
| Declare ((v1, v2, v3)) ->
raise Todo
and i_stmt_and_def x =
match x with
| Stmt st -> i_stmt st
| FuncDefNested _ ->
raise Todo
| ClassDefNested _ ->
raise Todo
| InterfaceDefNested _ ->
raise Todo
(* the semantic of a set of statements is the value of a 'return'
* if there is one, or the value of the last executed statements
* (e.g. '1;'), or null ?
*)
and i_stmt_and_defs xs =
match xs with
| [] ->
(* return null ? *)
raise Todo
| x::xs ->
try
let v = i_stmt_and_def x in
(match xs with
| [] ->
v
| _::_ ->
i_stmt_and_defs xs
)
with ReturnExn v ->
v
and i_expr e =
match Ast.untype e with
| Lvalue v ->
let v = i_lvalue v in
v.R.v
| Scalar v ->
i_scalar v
| Assign ((v1, v2, v3)) ->
raise Todo
| AssignRef ((v1, v2, v3, v4)) ->
raise Todo
| AssignNew ((v1, v2, v3, v4, v5, v6)) ->
raise Todo
| AssignOp ((v1, v2, v3)) ->
raise Todo
| Postfix ((v1, v2)) ->
raise Todo
| Infix ((v1, v2)) ->
raise Todo
| Binary ((a, op, b)) ->
(match Ast.unwrap op with
| Arith op ->
let va = i_expr a in
let vb = i_expr b in
(match op with
| Plus -> arith_int (+) va vb
| Minus -> arith_int (-) va vb
| Mul -> arith_int ( * ) va vb
| Div -> arith_int ( / ) va vb
| Mod -> raise Todo
| DecLeft -> raise Todo
| DecRight -> raise Todo
| And -> raise Todo
| Or -> raise Todo
| Xor -> raise Todo
)
| Logical op ->
let va = i_expr a in
(match op with
| AndBool | OrBool ->
raise Todo
| _ ->
let vb = i_expr b in
(match op with
| Inf -> arith_cmp (<) va vb
| Sup -> arith_cmp (>) va vb
| InfEq -> arith_cmp (<=) va vb
| SupEq -> arith_cmp (>=) va vb
| Eq -> raise Todo
| NotEq -> raise Todo
| Identical -> raise Todo
| NotIdentical -> raise Todo
| AndLog -> raise Todo
| OrLog -> raise Todo
| XorLog -> raise Todo
| AndBool | OrBool -> raise Impossible
)
)
| BinaryConcat ->
raise Todo
)
| Unary ((v1, v2)) ->
raise Todo
| CondExpr ((v1, v2, v3, v4, v5)) ->
raise Todo
| ConsList ((v1, v2, v3, v4)) ->
raise Todo
| ConsArray ((v1, v2)) ->
raise Todo
| New ((v1, v2, v3)) ->
raise Todo
| Clone ((v1, v2)) ->
raise Todo
| InstanceOf ((v1, v2, v3)) ->
raise Todo
| Cast ((v1, v2)) ->
raise Todo
| CastUnset ((v1, v2)) ->
raise Todo
| Exit ((v1, v2)) ->
raise Todo
| At ((v1, v2)) ->
raise Todo
| Print ((v1, v2)) ->
raise Todo
| BackQuote ((v1, v2, v3)) ->
raise Todo
| Include ((v1, v2)) ->
raise Todo
| IncludeOnce ((v1, v2)) ->
raise Todo
| Require ((v1, v2)) ->
raise Todo
| RequireOnce ((v1, v2)) ->
raise Todo
| Empty ((v1, v2)) ->
raise Todo
| Isset ((v1, v2)) ->
raise Todo
| Eval ((v1, v2)) ->
raise Todo
| ParenExpr v1 ->
raise Todo
| Lambda _ ->
raise Todo
| EDots _ ->
raise Todo
and i_scalar x =
match x with
| Constant cst ->
i_constant cst
| ClassConstant (qu, name) ->
raise Todo
| Guil _ ->
raise Todo
| HereDoc _ ->
raise Todo
and i_constant x =
match x with
| Int s ->
(* can raise exn *)
let i = int_of_string (Ast.unwrap s) in
R.RInt i
| Double s ->
let f = float_of_string (Ast.unwrap s) in
R.RFloat f
| String s ->
(* TODO interpret string content ? *)
R.RString (Ast.unwrap s)
| CName name ->
raise Todo
| PreProcess _ ->
raise Todo
| XdebugClass _
| XdebugResource ->
failwith "impossible: xdebug constructions"
and i_lvalue x =
match Ast.untype x with
| Var ((dname, scope)) ->
(match !scope with
| Scope_php.NoScope ->
pr2_once "NoScope, have called annotate_scope ?";
let s = Ast.dname dname in
let v = lookup_env s env.R.variables in
v
| Scope_php.Param | Scope_php.Local ->
let s = Ast.dname dname in
let v = lookup_env s env.R.variables in
v
| Scope_php.Global ->
raise Todo
)
| VArrayAccess ((v1, v2)) ->
raise Todo
| VBrace ((v1, v2)) ->
raise Todo
| VBraceAccess ((v1, v2)) ->
raise Todo
| Indirect ((v1, v2)) ->
raise Todo
| FunCallSimple ((qu_opt, name, args)) ->
qu_opt |> Common.do_option (fun _ -> raise Todo);
let s = Ast.name name in
let func = lookup_env s env.R.functions in
let args = Ast.unparen args in
apply_funcall func args
| FunCallVar ((v1, v2, v3)) ->
raise Todo
| VQualifier ((v1, v2)) ->
raise Todo
| MethodCallSimple ((v1, v2, v3, v4)) ->
raise Todo
| ObjAccessSimple ((v1, v2, v3)) ->
raise Todo
| ObjAccess ((v1, v2)) ->
raise Todo
and apply_funcall def args =
def.f_ref |> Common.do_option (fun _ -> failwith "TODO: f_ref");
let params = Ast.unparen def.f_params in
if List.length params <> List.length args
then failwith "TODO: not same number of arguments";
(* adjusting the environment with the new frame *)
Common.zip params args |> List.iter (fun (p,a) ->
p.p_default |> Common.do_option (fun _ -> failwith "TODO: p_default");
p.p_ref |> Common.do_option (fun _ -> failwith "TODO: p_ref");
let v =
match a with
| Arg e -> i_expr e
| ArgRef _ -> raise Todo
in
let s = Ast.dname p.p_name in
Hashtbl.add env.R.variables s { R.v = v };
);
let body = Ast.unbrace def.f_body in
let ret = i_stmt_and_defs body in
(* unset the vars in the environment *)
params |> List.iter (fun p ->
let s = Ast.dname p.p_name in
Hashtbl.remove env.R.variables s
);
{R.v = ret }