86f86c81d891 — Leonard Ritter a month ago
* compiler: work on VA, COUNTOF, SLICE
M lib/scopes/compiler/noir/renoir.sc +49 -0
@@ 2,6 2,8 @@ 
 #
     RENOIR: Rule Engine for NOIR
 
+memcpy_ := memcpy
+
 unlet exit
 using import C.stdio
 using import switcher compiler.basics

          
@@ 2082,6 2084,53 @@ FACTOR_TYPES := do
                         else
                             return false
 
+    struct INDEXDATA < FactorType
+        @@ memo
+        inline match-configs (cls)
+            using cls.Bits
+            pass fully-defined index sym
+
+        index : Module.Id.IndexAttr
+        sym : Module.Id.SXSymbol
+
+        inline next (cls reducer factor state builder index sym)
+            from cls let Bits
+            module := @builder._module
+            if (state.init == 0)
+                state.init = 1
+            else
+                return false
+            unit := reducer.unit
+            'dispatch-match cls factor
+                inline "#hidden" (mask)
+                    static-if (Bits.index in mask)
+                        sym := try! downcast (copy sym) Module.Id.SXSymbol
+                        H := unit.symbol @ sym . data
+                        ptr sz := 'data H
+                        if (sz <= (sizeof u64))
+                            local k : u64
+                            memcpy_
+                                &k as ~rawstring
+                                ptr
+                                sz
+                            if (k <= MAX_INDEX_ATTR)
+                                index = 'wrap Module.Id.IndexAttr k
+                                return true
+                        return false
+                    elseif (Bits.sym in mask)
+                        local k := index as u64
+                        H := 'from-data Unit.StringType (&k as rawstring) (sizeof u64)
+                        sym = 'wrap Module.Id.SXSymbol (('symbol unit H) as u64)
+                        return true
+                    else # verify
+                        sym := try! downcast (copy sym) Module.Id.SXSymbol
+                        local k := index as u64
+                        try
+                            'try-symbol-from-data unit (&k as rawstring) (sizeof u64)
+                        then (s)
+                            return (('wrap Module.Id.SXSymbol s) == sym)
+                        else
+                            return false
 
     struct INSTANCE < FactorType
         @@ memo

          
M lib/scopes/compiler/noir/rules-reducer.sx +92 -0
@@ 554,6 554,8 @@ pass-elide-constant
         FOLLOW $ctx $arg_ $arg
         NOT
             KINDOF $arg Instruction
+        NOT
+            DEP $ctx $id _ _
     ;
         ALIAS $ctx $id $arg_
 DEP-fold

          
@@ 600,6 602,96 @@ DEP-fold
                 EQ true $mode Ordered
             ;
                 NDEP $ctx $id $src_ $mode
+VA
+    # proper reduction of variadic lists containing other variadic lists
+    ;
+        OP $ctx $id VA
+        ARGCOUNT $ctx $id $n
+        ARG $ctx $id $i $arg_
+        FOLLOW $ctx $arg_ $arg
+        OP $ctx $arg VA
+        ADD $i+1 $i 1
+    ;
+        RULE # in last place
+            ;
+                EQ true $n $i+1
+            ;
+                ARGCOUNT $ctx $id $i
+                RULE
+                    ;
+                        ARG $ctx $arg $k $arg2
+                        ADD $i+k $i $k
+                    ;
+                        ARG $ctx $id $i+k $arg2
+        RULE # before last place
+            ;
+                EQ false $n $i+1
+            ;
+                RULE #
+                    ;
+                        ARG $ctx $arg 0 $arg2
+                    ;
+                        ARG $ctx $id $i $arg2
+                RULE
+                    ;
+                        ARGCOUNT $ctx $arg 0
+                    ;
+                        ARG $ctx $id $i none
+SLICE-convert-const-to-indexattr
+    ;
+        OP $ctx $fx SLICE
+        RANGE $i 1 3
+        ARG $ctx $fx $i $arg_
+        FOLLOW $ctx $arg_ $arg
+        CONST $arg
+        CONSTDATA $arg $argd
+        INDEXDATA $argi $argd
+    ;
+        ARG $ctx $fx $i $argi
+SLICE-VA
+    ;
+        OP $ctx $fx SLICE
+        ARG $ctx $fx 0 $id_
+        FOLLOW $ctx $id_ $id
+        OP $ctx $id VA
+        NOT
+            ARG $ctx $id _ $arg_
+            FOLLOW $ctx $arg_ $arg
+            OP $ctx $arg $op
+            METAOP? $op true
+        ARGCOUNT $ctx $id $n
+        ARG $ctx $fx 1 $begin_
+        FOLLOW $ctx $begin_ $begin
+        KINDOF $begin IndexAttr
+        ARG $ctx $fx 2 $end_
+        FOLLOW $ctx $end_ $end
+        KINDOF $end IndexAttr
+    ;
+        OP $ctx $fx VA
+        ARGCOUNT $ctx $fx 0
+        RULE
+            ;
+                RANGE $i $begin $end
+                ARG $ctx $id $i $arg
+            ;
+                ARG $ctx $fx _ $arg
+COUNTOF-VA
+    # count elements in a reduced VA
+    ;
+        OP $ctx $fx COUNTOF
+        ARG $ctx $fx 0 $id_
+        FOLLOW $ctx $id_ $id
+        OP $ctx $id VA
+        NOT
+            ARG $ctx $id _ $arg_
+            FOLLOW $ctx $arg_ $arg
+            OP $ctx $arg $op
+            METAOP? $op true
+        ARGCOUNT $ctx $id $n
+    ;
+        ALIAS $ctx $fx $n
+
+
 #5|UNUSED-elide
     # rewrite unused effects to UNUSED
     ;

          
M testing/noir/hello.sc +11 -2
@@ 4,8 4,17 @@ printf-type = fntype auto s32 ptr
 printf_ = vatype printf-type
 printf_s32 = vatype printf-type s32
 
-macro test x y... :
-    pass 0
+TRACE
+    x := VA 0 1 2 3 4 5 6 7 8 9
+
+TRACE
+    SLICE x 3 7
+
+z := (VA)
+x := VA 1 2 3
+y := VA x x
+TRACE
+    COUNTOF y
 
 fn main : s32
     argc : s32