-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestffi.php
More file actions
44 lines (37 loc) · 1.22 KB
/
Copy pathtestffi.php
File metadata and controls
44 lines (37 loc) · 1.22 KB
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
<?php
ini_set("display_errors", "on");
error_reporting(E_ALL);
$cdef = \FFI::cdef(file_get_contents("target/testffi.h"), "target/debug/libexpr_parser.so");
$expression = $cdef->parse_arithmetic("-6-(4+5)+(5+5)*(4-4)");
printExpression($expression);
$cdef->destroy($expression);
class ExpressionKind {
const Add = 0;
const Subtract = 1;
const Multiply = 2;
const Divide = 3;
const UnaryMinus = 4;
const Value = 5;
}
function printExpression($expression) {
switch ($expression->expression_type) {
case ExpressionKind::Add:
case ExpressionKind::Subtract:
case ExpressionKind::Multiply:
case ExpressionKind::Divide:
$operations = ["+", "-", "*", "/"];
print "(";
printExpression($expression->data->pair_operands->left);
print $operations[$expression->expression_type];
printExpression($expression->data->pair_operands->right);
print ")";
break;
case ExpressionKind::UnaryMinus:
print "-";
printExpression($expression->data->single_operand);
break;
case ExpressionKind::Value:
print $expression->data->value;
break;
}
}