struct expr *expr_binary(enum expr_binary_type type, struct expr const *lhs, struct expr const *rhs) { struct expr *expr = expr_alloc(EXPR_BINARY); expr->binary.type = type; expr->binary.lhs = expr_copy(lhs); expr->binary.rhs = expr_copy(rhs); return expr; }
WsExpression *ws_expr_const_float(WsCompiler *compiler, WsUInt32 line, WsFloat fval) { WsExpression *expr = expr_alloc(compiler, WS_EXPR_CONST_FLOAT, line); if (expr) expr->u.fval = fval; return expr; }
WsExpression *ws_expr_comma(WsCompilerPtr compiler, WsUInt32 line, WsExpression *left, WsExpression *right) { WsExpression *expr = expr_alloc(compiler, WS_EXPR_COMMA, line); if (expr) { expr->u.comma.left = left; expr->u.comma.right = right; } return expr; }
WsExpression *ws_expr_const_integer(WsCompiler *compiler, WsUInt32 line, WsUInt32 ival) { WsExpression *expr = expr_alloc(compiler, WS_EXPR_CONST_INTEGER, line); if (expr) { expr->u.integer.sign = 1; expr->u.integer.ival = ival; } return expr; }
WsExpression *ws_expr_logical(WsCompilerPtr compiler, WsUInt32 line, int type, WsExpression *left, WsExpression *right) { WsExpression *expr = expr_alloc(compiler, WS_EXPR_LOGICAL, line); if (expr) { expr->u.logical.type = type; expr->u.logical.left = left; expr->u.logical.right = right; } return expr; }
WsExpression *ws_expr_binary(WsCompilerPtr compiler, WsUInt32 line, int type, WsExpression *left, WsExpression *right) { WsExpression *expr = expr_alloc(compiler, WS_EXPR_BINARY, line); if (expr) { expr->u.binary.type = type; expr->u.binary.left = left; expr->u.binary.right = right; } return expr; }
WsExpression *ws_expr_conditional(WsCompilerPtr compiler, WsUInt32 line, WsExpression *e_cond, WsExpression *e_then, WsExpression *e_else) { WsExpression *e = expr_alloc(compiler, WS_EXPR_CONDITIONAL, line); if (e) { e->u.conditional.e_cond = e_cond; e->u.conditional.e_then = e_then; e->u.conditional.e_else = e_else; } return e; }
WsExpression *ws_expr_unary_var(WsCompilerPtr compiler, WsUInt32 line, WsBool addp, char *variable) { WsExpression *expr = expr_alloc(compiler, WS_EXPR_UNARY_VAR, line); if (expr) { expr->u.unary_var.addp = addp; expr->u.unary_var.variable = ws_f_strdup(compiler->pool_stree, variable); if (expr->u.unary_var.variable == NULL) ws_error_memory(compiler); } ws_lexer_free_block(compiler, variable); return expr; }
WsExpression *ws_expr_symbol(WsCompiler *compiler, WsUInt32 line, char *identifier) { WsExpression *expr = expr_alloc(compiler, WS_EXPR_SYMBOL, line); if (expr) { expr->u.symbol = ws_f_strdup(compiler->pool_stree, identifier); if (expr->u.symbol == NULL) ws_error_memory(compiler); } ws_lexer_free_block(compiler, identifier); return expr; }
WsExpression *ws_expr_const_string(WsCompiler *compiler, WsUInt32 line, WsUtf8String *string) { WsExpression *expr = expr_alloc(compiler, WS_EXPR_CONST_STRING, line); if (expr) { expr->u.string.len = string->len; expr->u.string.data = ws_f_memdup(compiler->pool_stree, string->data, string->len); if (expr->u.string.data == NULL) ws_error_memory(compiler); } ws_lexer_free_utf8(compiler, string); return expr; }
WsExpression *ws_expr_assign(WsCompilerPtr compiler, WsUInt32 line, char *identifier, int op, WsExpression *expr) { WsExpression *e = expr_alloc(compiler, WS_EXPR_ASSIGN, line); if (e) { e->u.assign.identifier = ws_f_strdup(compiler->pool_stree, identifier); if (e->u.assign.identifier == NULL) ws_error_memory(compiler); e->u.assign.op = op; e->u.assign.expr = expr; } /* Free the identifier symbol since it allocated from the system heap. */ ws_lexer_free_block(compiler, identifier); return e; }
WsExpression *ws_expr_unary(WsCompilerPtr compiler, WsUInt32 line, int type, WsExpression *expression) { WsExpression *expr; /* Handle negative integers here as a special case of constant folding, * in order to get -2147483648 right. */ if (type == WS_ASM_UMINUS && expression->type == WS_EXPR_CONST_INTEGER) { expression->u.integer.sign = - expression->u.integer.sign; return expression; } expr = expr_alloc(compiler, WS_EXPR_UNARY, line); if (expr) { expr->u.unary.type = type; expr->u.unary.expr = expression; } return expr; }
WsExpression *ws_expr_call(WsCompiler *compiler, WsUInt32 line, int type, char *base, char *name, WsList *arguments) { WsExpression *expr = expr_alloc(compiler, WS_EXPR_CALL, line); if (expr) { expr->u.call.type = type; expr->u.call.base = ws_f_strdup(compiler->pool_stree, base); expr->u.call.name = ws_f_strdup(compiler->pool_stree, name); expr->u.call.arguments = arguments; if ((base && expr->u.call.base == NULL) || (name && expr->u.call.name == NULL)) ws_error_memory(compiler); } ws_lexer_free_block(compiler, base); ws_lexer_free_block(compiler, name); return expr; }
WsExpression *ws_expr_const_invalid(WsCompiler *compiler, WsUInt32 line) { return expr_alloc(compiler, WS_EXPR_CONST_INVALID, line); }
struct expr *expr_list(struct expr_list const *expr_list) { struct expr *expr = expr_alloc(EXPR_LIST); expr->list = expr_list_copy(expr_list); return expr; }
struct expr *expr_dict(struct expr_list const *expr_list) { struct expr *expr = expr_alloc(EXPR_DICT); expr->dict = expr_list_copy(expr_list); return expr; }
struct expr *expr_attr(struct expr const *lhs, char const *rhs) { struct expr *expr = expr_alloc(EXPR_ATTR); expr->attr.expr = expr_copy(lhs); expr->attr.identifier = strdup(rhs); return expr; }
struct expr *expr_number(int number) { struct expr *expr = expr_alloc(EXPR_NUMBER); expr->number = number; return expr; }
struct expr *expr_identifier(char const *identifier) { struct expr *expr = expr_alloc(EXPR_IDENTIFIER); expr->identifier = strdup(identifier); return expr; }
WsExpression *ws_expr_const_false(WsCompiler *compiler, WsUInt32 line) { return expr_alloc(compiler, WS_EXPR_CONST_FALSE, line); }
struct expr *expr_unary(enum expr_unary_type type, struct expr const *arg) { struct expr *expr = expr_alloc(EXPR_UNARY); expr->unary.type = type; expr->unary.arg = expr_copy(arg); return expr; }
struct expr *expr_string(char const *string) { struct expr *expr = expr_alloc(EXPR_STRING); expr->string = strdup(string); return expr; }
WsExpression *ws_expr_const_true(WsCompiler *compiler, WsUInt32 line) { return expr_alloc(compiler, WS_EXPR_CONST_TRUE, line); }
/** * Parse a complex expression and return a * signal_expression that describes it. */ struct signal_expression *texpr (void) { struct signal_expression *ex, *ex1, *ex2; const char *t; t = tnext (); if (!t) return NULL; ex = expr_alloc (); if (teq (t, "at")) { ex->op = SIG_TIME; ex->u.timer = tconst (); } else if (teq (t, "after")) { ex->op = SIG_TIMEDIFF; ex->u.timer = tconst (); } else { tunget (t); ex->op = SIG_SIGNO; ex->u.signo = tsigno (); simlog (SLC_DEBUG, "Signo changed = 0x%X\n", ex->u.signo); t = tnext (); if (t) { if (teq (t, "is")) { ex1 = ex; ex2 = expr_alloc (); ex2->op = SIG_CONST; ex2->u.value = tconst (); ex = expr_alloc (); ex->op = SIG_EQ; ex->u.binary.left = ex1; ex->u.binary.right = ex2; } } } t = tnext (); if (t) { ex1 = ex; ex2 = texpr (); ex = expr_alloc (); ex->u.binary.left = ex1; ex->u.binary.right = ex2; if (teq (t, "and")) ex->op = SIG_AND; else if (teq (t, "or")) ex->op = SIG_OR; } return ex; }