js: add bool type, add if-else master
authoryu.dongliang <18588496441@163.com>
Thu, 30 Oct 2025 07:42:48 +0000 (15:42 +0800)
committeryu.dongliang <18588496441@163.com>
Thu, 30 Oct 2025 07:42:48 +0000 (15:42 +0800)
13 files changed:
examples/js.html
html/Makefile
js/core/scf_core_types.h
js/core/scf_lex_word.h
js/core/scf_optimizer.c
js/core/scf_type_cast.c
js/core/scf_type_cast.h
js/core/scf_type_cast_bool.c [new file with mode: 0644]
js/doc.c
js/lex/scf_lex.c
js/parse/scf_dfa_expr.c
js/parse/scf_parse.c
ui/Makefile

index 852612d82a53607be200458946093dc673de30d2..be3e6578e112dbed90ebe7f0be19728c3f85abb6 100644 (file)
@@ -8,9 +8,16 @@
 
 <script>
 
-var str = "hello world";
-var p   = /L/gi;
-document.write(str.match(p));
+var s0 = "123";
+var s1 = "124";
+
+document.write(s0 == s1);
+document.write("<br>");
+
+if (s0 == s1)
+       document.write(true);
+else
+       document.write(false);
 
 </script>
 
index b4e15373202490a23c5125c4750f3003279d09d7..71e93d11cae8988761fec4d96d200d14eb186ae4 100644 (file)
@@ -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
 
index c8c01e6fe0f8fa58b3000434761771d1fa2b9761..bcf921c1e68dc6150378b285f587521c73354c49 100644 (file)
@@ -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,
index 807fb975a7a00db8279aa2010b0ae95d6a1995f6..1d056183c7ddc13b2979584863a7dff8b69053b9 100644 (file)
@@ -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,
index 30c79183abe9cdacaf148e3c35fb6ab59f81c213..9cc3516ce52672441e4bfcfa3f375bda7c710cc8 100644 (file)
@@ -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");
index 7f103d9d83a408b821825511d2327b517eab5f23..b119c885d2c5594302d5e15eaa0b3ea91216ac0c 100644 (file)
@@ -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},
index 0ccf5ff05675be6649809bc509bd3f7ae5ce3aeb..4ea173bc4779eb3fb51389a5e373fb5b664cd3f8 100644 (file)
@@ -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 (file)
index 0000000..eb58be3
--- /dev/null
@@ -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;
+}
index 5b6dfdd6fea1a9495aca43a8c1a9bd1c8d441e20..f509019f793d0dbb4bc56683648cbef7fec3315f 100644 (file)
--- 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) {
index ad60e6693a1fd59ab863690f754531c8c3d43e37..2537116113b8bd2078221b2742d366d0dfea60ac 100644 (file)
@@ -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);
index 60348e77e49e62189bf10676c55f752d64fc85c3..ffde7f1f61a9469e41b80bc1a06052a11fcb3b0a 100644 (file)
@@ -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;
index 948e649c9655ca841fcdce417188f5ecd5a89bd8..bad97191352ebc7c8c4ab83890ec0b0726e533e1 100644 (file)
@@ -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;
+                       };
                }
        }
 
index 4c5c5b83ac108162d2dae44d108bbd5dffc76dc4..902c10ff0400a3e10556cdd92445c7b3e738578d 100644 (file)
@@ -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