This repository has been archived by the owner on Nov 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexpr.c
162 lines (125 loc) · 4.95 KB
/
expr.c
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
/*
+--------------------------------------------------------------------------+
| Zephir Language |
+--------------------------------------------------------------------------+
| Copyright (c) 2013-2014 Zephir Team and contributors |
+--------------------------------------------------------------------------+
| This source file is subject the MIT license, that is bundled with |
| this package in the file LICENSE, and is available through the |
| world-wide-web at the following url: |
| http://zephir-lang.com/license.html |
| |
| If you did not receive a copy of the MIT license and are unable |
| to obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+--------------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <php.h>
#include "php_zephir.h"
#include "zephir.h"
#include "utils.h"
#include "symtable.h"
#include "errors.h"
#include "fcall.h"
#include "kernel/main.h"
#include "operators/arithmetical.h"
#include "operators/comparison.h"
zephir_compiled_expr *zephir_expr(zephir_context *context, zval *expr TSRMLS_DC)
{
zval *type, *value, *left_expr;
zephir_compiled_expr *compiled_expr, *compiled_expr_left;
zephir_variable *variable;
_zephir_array_fetch_string(&type, expr, "type", strlen("type") + 1 TSRMLS_CC);
if (Z_TYPE_P(type) != IS_STRING) {
zephir_error(expr, "Expression is corrupt");
}
if (!memcmp(Z_STRVAL_P(type), SS("bool"))) {
_zephir_array_fetch_string(&value, expr, SS("value") TSRMLS_CC);
if (Z_TYPE_P(value) != IS_STRING) {
return NULL;
}
compiled_expr = emalloc(sizeof(zephir_compiled_expr));
compiled_expr->type = ZEPHIR_T_TYPE_BOOL;
if (!memcmp(Z_STRVAL_P(value), SS("true"))) {
compiled_expr->value = LLVMConstInt(LLVMInt8Type(), 1, 0);
} else {
compiled_expr->value = LLVMConstInt(LLVMInt8Type(), 0, 0);
}
return compiled_expr;
}
if (!memcmp(Z_STRVAL_P(type), SS("int"))) {
_zephir_array_fetch_string(&value, expr, SS("value") TSRMLS_CC);
if (Z_TYPE_P(value) != IS_STRING) {
return NULL;
}
compiled_expr = emalloc(sizeof(zephir_compiled_expr));
compiled_expr->type = ZEPHIR_T_TYPE_INTEGER;
#if ZEPHIR_32
compiled_expr->value = LLVMConstInt(LLVMInt32Type(), strtol(Z_STRVAL_P(value), NULL, 10), 0);
#else
compiled_expr->value = LLVMConstInt(LLVMInt64Type(), strtol(Z_STRVAL_P(value), NULL, 10), 0);
#endif
return compiled_expr;
}
if (!memcmp(Z_STRVAL_P(type), SS("double"))) {
_zephir_array_fetch_string(&value, expr, SS("value") TSRMLS_CC);
if (Z_TYPE_P(value) != IS_STRING) {
return NULL;
}
compiled_expr = emalloc(sizeof(zephir_compiled_expr));
compiled_expr->type = ZEPHIR_T_TYPE_DOUBLE;
compiled_expr->value = LLVMConstReal(LLVMDoubleType(), zend_strtod(Z_STRVAL_P(value), NULL));
return compiled_expr;
}
if (!memcmp(Z_STRVAL_P(type), SS("string"))) {
_zephir_array_fetch_string(&value, expr, SS("value") TSRMLS_CC);
if (Z_TYPE_P(value) != IS_STRING) {
return NULL;
}
compiled_expr = emalloc(sizeof(zephir_compiled_expr));
compiled_expr->type = ZEPHIR_T_TYPE_STRING;
compiled_expr->value = LLVMBuildGlobalStringPtr(context->builder, Z_STRVAL_P(value), "");
return compiled_expr;
}
if (!memcmp(Z_STRVAL_P(type), SS("variable"))) {
_zephir_array_fetch_string(&value, expr, SS("value") TSRMLS_CC);
if (Z_TYPE_P(value) != IS_STRING) {
return NULL;
}
variable = zephir_symtable_get_variable_for_read(context->symtable, Z_STRVAL_P(value), Z_STRLEN_P(value), context, expr);
compiled_expr = emalloc(sizeof(zephir_compiled_expr));
compiled_expr->type = ZEPHIR_T_TYPE_VAR;
compiled_expr->variable = variable;
return compiled_expr;
}
if (!memcmp(Z_STRVAL_P(type), SS("add"))) {
return zephir_operator_arithmetical_add(context, expr TSRMLS_CC);
}
if (!memcmp(Z_STRVAL_P(type), SS("mul"))) {
return zephir_operator_arithmetical_mul(context, expr TSRMLS_CC);
}
if (!memcmp(Z_STRVAL_P(type), SS("div"))) {
return zephir_operator_arithmetical_div(context, expr TSRMLS_CC);
}
if (!memcmp(Z_STRVAL_P(type), SS("greater"))) {
return zephir_operator_comparison_greater(context, expr TSRMLS_CC);
}
if (!memcmp(Z_STRVAL_P(type), SS("less"))) {
return zephir_operator_comparison_less(context, expr TSRMLS_CC);
}
if (!memcmp(Z_STRVAL_P(type), SS("fcall"))) {
return zephir_fcall_compile(context, expr TSRMLS_CC);
}
if (!memcmp(Z_STRVAL_P(type), SS("list"))) {
_zephir_array_fetch_string(&left_expr, expr, SS("left") TSRMLS_CC);
if (Z_TYPE_P(left_expr) != IS_ARRAY) {
return NULL;
}
return zephir_expr(context, left_expr TSRMLS_CC);
}
zephir_error(expr, "Unknown expression \"%s\"", Z_STRVAL_P(type));
return NULL;
}