📄 wsstree.c
字号:
ws_src_error(compiler, expr->line, "invalid amount of arguments for `%s.%s': " "expected %u, got %u", expr->u.call.base, expr->u.call.name, num_args, expr->u.call.arguments->num_items); return; } /* Emit assembler. */ ws_asm_link(compiler, ws_asm_call_lib(compiler, expr->line, findex, lindex)); } break; default: ws_fatal("ws_expr_linearize(): unknown call expression type %x", expr->u.call.type); break; } break; case WS_EXPR_SYMBOL: { WsNamespace *ns = ws_variable_lookup(compiler, expr->u.symbol); if (ns == NULL) { /* An unknown identifier. */ ws_src_error(compiler, expr->line, "unknown variable `%s'", expr->u.symbol); return; } /* Create a load instruction for the variable. */ ws_asm_link(compiler, ws_asm_variable(compiler, expr->line, WS_ASM_P_LOAD_VAR, ns->vindex)); } break; case WS_EXPR_CONST_INVALID: ws_asm_link(compiler, ws_asm_ins(compiler, expr->line, WS_ASM_CONST_INVALID)); break; case WS_EXPR_CONST_TRUE: ws_asm_link(compiler, ws_asm_ins(compiler, expr->line, WS_ASM_CONST_TRUE)); break; case WS_EXPR_CONST_FALSE: ws_asm_link(compiler, ws_asm_ins(compiler, expr->line, WS_ASM_CONST_FALSE)); break; case WS_EXPR_CONST_INTEGER: if (expr->u.integer.ival == 0) ins = ws_asm_ins(compiler, expr->line, WS_ASM_CONST_0); else if (expr->u.integer.ival == 1 && expr->u.integer.sign == 1) ins = ws_asm_ins(compiler, expr->line, WS_ASM_CONST_1); else { WsUInt16 cindex; WsInt32 ival; if (expr->u.integer.sign >= 0) { if (expr->u.integer.ival > (WsUInt32) WS_INT32_MAX) ws_src_error(compiler, expr->line, "integer literal too large"); ival = expr->u.integer.ival; } else { if (expr->u.integer.ival > (WsUInt32) WS_INT32_MAX + 1) ws_src_error(compiler, expr->line, "integer too small"); ival = - (WsInt32) expr->u.integer.ival; } if (!ws_bc_add_const_int(compiler->bc, &cindex, ival)) { ws_error_memory(compiler); return; } ins = ws_asm_load_const(compiler, expr->line, cindex); } ws_asm_link(compiler, ins); break; case WS_EXPR_CONST_FLOAT: { WsUInt16 cindex; if (!ws_bc_add_const_float(compiler->bc, &cindex, expr->u.fval)) { ws_error_memory(compiler); return; } ws_asm_link(compiler, ws_asm_load_const(compiler, expr->line, cindex)); } break; case WS_EXPR_CONST_STRING: if (expr->u.string.len == 0) ins = ws_asm_ins(compiler, expr->line, WS_ASM_CONST_ES); else { WsUInt16 cindex; if (!ws_bc_add_const_utf8_string(compiler->bc, &cindex, expr->u.string.data, expr->u.string.len)) { ws_error_memory(compiler); return; } ins = ws_asm_load_const(compiler, expr->line, cindex); } ws_asm_link(compiler, ins); break; }}/* Constructors. */static WsExpression *expr_alloc(WsCompiler *compiler, WsExpressionType type, WsUInt32 line){ WsExpression *expr = ws_f_calloc(compiler->pool_stree, 1, sizeof(*expr)); if (expr == NULL) ws_error_memory(compiler); else { expr->type = type; expr->line = line; } 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_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_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_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_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_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_postfix_var(WsCompilerPtr compiler, WsUInt32 line, WsBool addp, char *variable){ WsExpression *expr = expr_alloc(compiler, WS_EXPR_POSTFIX_VAR, line); if (expr) { expr->u.postfix_var.addp = addp; expr->u.postfix_var.variable = ws_f_strdup(compiler->pool_stree, variable); if (expr->u.postfix_var.variable == NULL) ws_error_memory(compiler); } ws_lexer_free_block(compiler, variable); 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_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_invalid(WsCompiler *compiler, WsUInt32 line){ return expr_alloc(compiler, WS_EXPR_CONST_INVALID, line);}WsExpression *ws_expr_const_true(WsCompiler *compiler, WsUInt32 line){ return expr_alloc(compiler, WS_EXPR_CONST_TRUE, line);}WsExpression *ws_expr_const_false(WsCompiler *compiler, WsUInt32 line){ return expr_alloc(compiler, WS_EXPR_CONST_FALSE, line);}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_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_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;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -