From: yu.dongliang <18588496441@163.com> Date: Thu, 30 Oct 2025 07:42:48 +0000 (+0800) Subject: js: add bool type, add if-else X-Git-Url: http://baseworks.info/?a=commitdiff_plain;h=8582f47b698cac5d11a55ee11062351edd475b69;p=abc.git js: add bool type, add if-else --- diff --git a/examples/js.html b/examples/js.html index 852612d..be3e657 100644 --- a/examples/js.html +++ b/examples/js.html @@ -8,9 +8,16 @@ diff --git a/html/Makefile b/html/Makefile index b4e1537..71e93d1 100644 --- a/html/Makefile +++ b/html/Makefile @@ -92,6 +92,7 @@ CFILES += ../js/core/scf_type_cast_u32.c CFILES += ../js/core/scf_type_cast_u64.c CFILES += ../js/core/scf_type_cast_float.c CFILES += ../js/core/scf_type_cast_double.c +CFILES += ../js/core/scf_type_cast_bool.c CFILES += ../js/core/scf_operator_handler_3ac.c diff --git a/js/core/scf_core_types.h b/js/core/scf_core_types.h index c8c01e6..bcf921c 100644 --- a/js/core/scf_core_types.h +++ b/js/core/scf_core_types.h @@ -209,7 +209,7 @@ enum scf_core_types SCF_VAR_U8, SCF_VAR_VOID, SCF_VAR_VAR, - SCF_VAR_BIT, + SCF_VAR_BOOL, SCF_VAR_U2, SCF_VAR_U3, SCF_VAR_U4, diff --git a/js/core/scf_lex_word.h b/js/core/scf_lex_word.h index 807fb97..1d05618 100644 --- a/js/core/scf_lex_word.h +++ b/js/core/scf_lex_word.h @@ -129,10 +129,7 @@ enum scf_lex_words SCF_LEX_WORD_KEY_INT64, // int64_t SCF_LEX_WORD_KEY_UINT8, // uint8_t - SCF_LEX_WORD_KEY_BIT, // bit - SCF_LEX_WORD_KEY_BIT2, // bit2_t - SCF_LEX_WORD_KEY_BIT3, // bit3_t - SCF_LEX_WORD_KEY_BIT4, // bit4_t + SCF_LEX_WORD_KEY_BOOL, // bool SCF_LEX_WORD_KEY_UINT16, // uint16_t SCF_LEX_WORD_KEY_UINT32, // uint32_t SCF_LEX_WORD_KEY_UINT64, // uint64_t @@ -165,6 +162,7 @@ enum scf_lex_words // const literal value SCF_LEX_WORD_CONST_CHAR, + SCF_LEX_WORD_CONST_BOOL, SCF_LEX_WORD_CONST_INT, SCF_LEX_WORD_CONST_U32, diff --git a/js/core/scf_optimizer.c b/js/core/scf_optimizer.c index 30c7918..9cc3516 100644 --- a/js/core/scf_optimizer.c +++ b/js/core/scf_optimizer.c @@ -115,7 +115,7 @@ int scf_optimize(scf_ast_t* ast, scf_vector_t* functions) if (!f->node.define_flag) continue; - if (strcmp(f->node.w->text->data, "__toString")) + if (strcmp(f->node.w->text->data, "__js_main")) continue; printf("\n"); diff --git a/js/core/scf_type_cast.c b/js/core/scf_type_cast.c index 7f103d9..b119c88 100644 --- a/js/core/scf_type_cast.c +++ b/js/core/scf_type_cast.c @@ -2,9 +2,9 @@ static int type_update[] = { + SCF_VAR_BOOL, SCF_VAR_I8, SCF_VAR_CHAR, - SCF_VAR_BIT, SCF_VAR_U8, SCF_VAR_I16, @@ -27,7 +27,7 @@ static int type_update[] = static scf_type_cast_t base_type_casts[] = { {"char", -1, SCF_VAR_CHAR, scf_cast_to_u8}, - {"bit", -1, SCF_VAR_BIT, scf_cast_to_u8}, + {"bool", -1, SCF_VAR_BOOL, scf_cast_to_bool}, {"u8", -1, SCF_VAR_U8, scf_cast_to_u8}, {"u16", -1, SCF_VAR_U16, scf_cast_to_u16}, {"u32", -1, SCF_VAR_U32, scf_cast_to_u32}, diff --git a/js/core/scf_type_cast.h b/js/core/scf_type_cast.h index 0ccf5ff..4ea173b 100644 --- a/js/core/scf_type_cast.h +++ b/js/core/scf_type_cast.h @@ -29,6 +29,8 @@ int scf_cast_to_u16(scf_ast_t* ast, scf_variable_t** pret, scf_variable_t* src); int scf_cast_to_u32(scf_ast_t* ast, scf_variable_t** pret, scf_variable_t* src); int scf_cast_to_u64(scf_ast_t* ast, scf_variable_t** pret, scf_variable_t* src); +int scf_cast_to_bool(scf_ast_t* ast, scf_variable_t** pret, scf_variable_t* src); + int scf_cast_to_float(scf_ast_t* ast, scf_variable_t** pret, scf_variable_t* src); int scf_cast_to_double(scf_ast_t* ast, scf_variable_t** pret, scf_variable_t* src); diff --git a/js/core/scf_type_cast_bool.c b/js/core/scf_type_cast_bool.c new file mode 100644 index 0000000..eb58be3 --- /dev/null +++ b/js/core/scf_type_cast_bool.c @@ -0,0 +1,45 @@ +#include"scf_type_cast.h" + +int scf_cast_to_bool(scf_ast_t* ast, scf_variable_t** pret, scf_variable_t* src) +{ + if (!pret || !src) + return -EINVAL; + + scf_type_t* t = scf_block_find_type_type(ast->current_block, SCF_VAR_BOOL); + + scf_variable_t* r = SCF_VAR_ALLOC_BY_TYPE(src->w, t, src->const_flag, 0, NULL); + + switch (src->type) { + case SCF_VAR_FLOAT: + r->data.u32 = !!(uint32_t)src->data.f; + break; + + case SCF_VAR_DOUBLE: + r->data.u32 = !!(uint32_t)src->data.d; + break; + + case SCF_VAR_CHAR: + case SCF_VAR_I8: + case SCF_VAR_U8: + case SCF_VAR_I16: + case SCF_VAR_U16: + case SCF_VAR_I32: + case SCF_VAR_U32: + case SCF_VAR_I64: + case SCF_VAR_U64: + r->data.u32 = !!src->data.u32; + break; + + default: + if (src->nb_pointers > 0) + r->data.u32 = !!src->data.u32; + else { + scf_variable_free(r); + return -EINVAL; + } + break; + }; + + *pret = r; + return 0; +} diff --git a/js/doc.c b/js/doc.c index 5b6dfdd..f509019 100644 --- a/js/doc.c +++ b/js/doc.c @@ -54,6 +54,14 @@ struct Object return 0; } + int __init(Object* this, bool b) + { + this->i64 = b; + this->type = JS_Boolean; + printf("this: %p, this->i64: %ld, b: %d\n\n", this, this->i64, b); + return 0; + } + int __init(Object* this, const char* s) { int len = strlen(s); @@ -141,6 +149,17 @@ struct Object return res; } + Object* Boolean(Object* this, bool b) + { + Object* res = new Object(); + if (!res) + return NULL; + + res->i64 = b; + res->type = JS_Boolean; + return res; + } + Object* Boolean(Object* this, Object* that) { Object* res = new Object(); @@ -342,6 +361,14 @@ struct Object abc_html_write(this, s); } + void write(Object* this, bool b) + { + if (b) + abc_html_write(this, "true"); + else + abc_html_write(this, "false"); + } + void write(Object* this, int64_t i) { abc_html_write_i(this, i); @@ -354,10 +381,50 @@ struct Object Object* operator+(Object* this, Object* that) { - Object* res = new Object(this->d + that->d); + Object* res = new Object(); + if (res) { + if (JS_Number == this->type && JS_Number == that->type) { + res->d = this->d + that->d; + res->type = JS_Number; + } else { + int len0 = this->__toStringLen(); + int len1 = that->__toStringLen(); + + res->str = scf__auto_malloc(len0 + len1 + 1); + if (res->str) { + int n0 = this->__toString(res->str); + + that->__toString(res->str + n0); + res->type = JS_String; + } + } + } + return res; + } + + Object* operator-(Object* this, Object* that) + { + if (JS_Number != this->type || JS_Number != that->type) + return NULL; + + Object* res = new Object(this->d - that->d); return res; } + bool operator==(Object* this, Object* that) + { + if (JS_Number == this->type && JS_Number == that->type) + return this->d == that->d; + + if (JS_Boolean == this->type && JS_Boolean == that->type) + return this->i64 == that->i64; + + if (JS_String == this->type && JS_String == that->type) + return !strcmp(this->str, that->str); + + return 0; + } + void __release(Object* this) { if (this->members) { diff --git a/js/lex/scf_lex.c b/js/lex/scf_lex.c index ad60e66..2537116 100644 --- a/js/lex/scf_lex.c +++ b/js/lex/scf_lex.c @@ -33,10 +33,7 @@ static scf_key_word_t key_words[] = {"int", SCF_LEX_WORD_KEY_INT}, {"float", SCF_LEX_WORD_KEY_FLOAT}, {"double", SCF_LEX_WORD_KEY_DOUBLE}, - {"bit", SCF_LEX_WORD_KEY_BIT}, - {"bit2_t", SCF_LEX_WORD_KEY_BIT2}, - {"bit3_t", SCF_LEX_WORD_KEY_BIT3}, - {"bit4_t", SCF_LEX_WORD_KEY_BIT4}, + {"bool", SCF_LEX_WORD_KEY_BOOL}, {"int8_t", SCF_LEX_WORD_KEY_INT8}, {"int1_t", SCF_LEX_WORD_KEY_INT1}, @@ -383,6 +380,18 @@ static int _lex_identity(scf_lex_t* lex, scf_lex_word_t** pword, scf_char_t* c0) if (w) w->data.u64 = 0; + } else if (!strcmp(s->data, "true")) { + + w = scf_lex_word_alloc(lex->file, lex->nb_lines, lex->pos, SCF_LEX_WORD_CONST_BOOL); + if (w) + w->data.u64 = 1; + + } else if (!strcmp(s->data, "false")) { + + w = scf_lex_word_alloc(lex->file, lex->nb_lines, lex->pos, SCF_LEX_WORD_CONST_BOOL); + if (w) + w->data.u64 = 0; + } else if (!strcmp(s->data, "__LINE__")) { w = scf_lex_word_alloc(lex->file, lex->nb_lines, lex->pos, SCF_LEX_WORD_CONST_U64); diff --git a/js/parse/scf_dfa_expr.c b/js/parse/scf_dfa_expr.c index 60348e7..ffde7f1 100644 --- a/js/parse/scf_dfa_expr.c +++ b/js/parse/scf_dfa_expr.c @@ -350,6 +350,9 @@ static int _expr_action_number(scf_dfa_t* dfa, scf_vector_t* words, void* data) type = SCF_VAR_U64; break; + case SCF_LEX_WORD_CONST_BOOL: + type = SCF_VAR_BOOL; + break; default: scf_loge("unknown number type\n"); return SCF_DFA_ERROR; diff --git a/js/parse/scf_parse.c b/js/parse/scf_parse.c index 948e649..bad9719 100644 --- a/js/parse/scf_parse.c +++ b/js/parse/scf_parse.c @@ -23,6 +23,7 @@ scf_base_type_t base_types[] = {SCF_VAR_VOID, "void", 1}, {SCF_VAR_VAR, "var", 1}, + {SCF_VAR_BOOL, "bool", 1}, {SCF_VAR_INT, "int", 4}, {SCF_VAR_FLOAT, "float", 4}, @@ -2335,15 +2336,19 @@ int scf_parse_compile(scf_parse_t* parse, const char* arch) for (j = 0; j < file->nb_nodes; ) { e = file->nodes[j]; - if (SCF_OP_EXPR != e->type) { - j++; - continue; - } + switch (e->type) { + case SCF_OP_EXPR: + case SCF_OP_IF: + ret = scf_node_add_child((scf_node_t*)f, e); + if (ret < 0) + return ret; - ret = scf_node_add_child((scf_node_t*)f, e); - if (ret < 0) - return ret; - scf_node_del_child(file, e); + scf_node_del_child(file, e); + break; + default: + j++; + break; + }; } } diff --git a/ui/Makefile b/ui/Makefile index 4c5c5b8..902c10f 100644 --- a/ui/Makefile +++ b/ui/Makefile @@ -143,6 +143,7 @@ CFILES += ../js/core/scf_type_cast_u32.c CFILES += ../js/core/scf_type_cast_u64.c CFILES += ../js/core/scf_type_cast_float.c CFILES += ../js/core/scf_type_cast_double.c +CFILES += ../js/core/scf_type_cast_bool.c CFILES += ../js/core/scf_operator_handler_3ac.c