25d48dd522e8 — Leonard Ritter tip 2 days ago
more work on blocktree, test_node
4 files changed, 271 insertions(+), 50 deletions(-)

M bin/tukan
M lib/tukan/main.sc
M lib/tukan/view/blocktree.sc
M testing/test_node.sc
M bin/tukan +1 -6
@@ 1,9 1,4 @@ 
 #!/usr/bin/env scopes
 
 import ..lib.tukan.use
-import tukan.kernel
-
-tukan.kernel.boot;
-
-
-
+import tukan.main

          
M lib/tukan/main.sc +3 -3
@@ 15,9 15,9 @@ using import .module
 
 using import .view.events
 
-#using import .view.blocktree
-#using import .view.shader
-#using import .view.hexedit
+using import .view.blocktree
+using import .view.shader
+using import .view.hexedit
 
 #-------------------------------------------------------------------------------
 

          
M lib/tukan/view/blocktree.sc +7 -6
@@ 4,6 4,7 @@ using import struct
 using import enum
 using import Option
 using import Array
+using import String
 using import Set
 using import Box
 using import Rc

          
@@ 401,16 402,16 @@ struct DemoNode
     let WeakType = RcType.WeakType
     parent : WeakType
     children : (GrowingArray RcType)
-    _name : string
+    _name : String
     width = 100.0
     selected = false
     value = 0
 
     inline... new
-    case (name : string,)
+    case (name : String,)
         RcType
             _name = name
-    case (parent : RcType, name : string, )
+    case (parent : RcType, name : String, )
         let self =
             RcType
                 parent = parent

          
@@ 428,7 429,7 @@ struct DemoNode
         self._name
 
     inline __repr (self)
-        deref self._name
+        self._name as string
 
     fn child-index (self child)
         for i elem in (enumerate self.children)

          
@@ 550,8 551,8 @@ inline ()
         if (WithBegin "Blocktree Test" show-code)
             global blocktree : RcBlockTree
             WithBlockTree blocktree
-            walk-children root
-        #
+            #walk-children root
+            #
             global width = 150.0
             do
                 if (WithBlockTreeNode "Testnode 1" width)

          
M testing/test_node.sc +260 -35
@@ 13,6 13,39 @@ let Id = u32
 let NoId = (Id 0)
 let NoType = NoId
 
+#
+    fundamental idea:
+        all processing flow organized around single I/O wait blackbox,
+        therefore purely functional
+
+    sync options:
+
+    sequencer pattern that repeats in custom unit of time?
+
+    supply audio frames
+        per sample
+        per N samples
+
+    supply video frames
+        per pixel
+        per full frame
+
+    supply network packets
+        per received packet
+        conditional source linked to periodic event?
+
+    supply filestream
+
+    always pull timecode from audio?
+
+    custom sync sources?
+        every N audio samples
+        every N video frames
+        every N nanoseconds
+        per input event
+        ?
+
+
 unlet Builtin
 enum Builtin : u32
     ### Sources

          
@@ 20,11 53,20 @@ enum Builtin : u32
     # TypeInt 32 false
     Samplerate
 
+    # TypeVector (TypeInt 32 false) 2
+    ScreenSize
+
+    # TypeVector (TypeInt 32 false) 2
+    ScreenCoord
+
     ### Sinks
 
-    # TypeVector
+    # TypeVector (TypeReal 32) 2
     Audio
 
+    # TypeVector (TypeReal 32) 4
+    Color
+
 enum Op : u32
     None = 0
 

          
@@ 36,8 78,8 @@ enum Op : u32
     # Op.TypeBool NoType
     TypeBool
 
-    # Op.TypeFloat NoType <width : i32>
-    TypeFloat
+    # Op.TypeReal NoType <width : i32>
+    TypeReal
 
     # Op.TypeVector NoType <comp-type : Id> <count : i32>
     TypeVector

          
@@ 45,6 87,9 @@ enum Op : u32
     # Op.TypeArray NoType <element-type : Id> <count : i32>
     TypeArray
 
+    # Op.TypeStruct NoType <type : Id> ...
+    TypeStruct
+
     ### Constants
 
     # Op.ConstantTrue <bool-type : Id>

          
@@ 59,33 104,69 @@ enum Op : u32
     # Op.ConstantComposite <type : Id> <constituent : Id> ...
     ConstantComposite
 
-    # Op.Source <type : Id> <source : Id>
-    Source
+    ### Interface
+
+    # Op.Builtin <type : Id> <name : Builtin>
+    Builtin
+
+    # Op.State <type : Id> <default-const : Id> <name... : String>
+    State
+
+    # Op.Store NoType <value : Id> <target : Id>
+    Store
+
+    # Op.Audio NoType <value : Id>
+    Audio
+
+    # Op.Video NoType <value : Id>
+    Video
+
+    ### Composites
+
+    # Op.CompositeInsert <type : Id> <value : Id> <target : Id> <index : u32> ...
+    CompositeInsert
+
+    # Op.CompositeConstruct <type : Id> <value : Id> ...
+    CompositeConstruct
+
+    ### Instructions
 
-    # Op.Sink NoType <value : Id> <target : Id>
-    Sink
+    # Op.ConvertIntToReal <type : Id> <source : Id>
+    ConvertIntToReal
+
+    # Op.ConvertRealToInt <type : Id> <source : Id>
+    ConvertRealToInt
+
+    # Op.* <type : Id> <value : Id>
+    Sin
+    Cos
+
+    # Op.* <type : Id> <a : Id> <b : Id>
+    FAdd
+    FMul
+    FDiv
+
+fn... append-operand (self, operand : Id)
+    'append self operand
+    ;
+case (self, text : String)
+    numbytes := (countof text)
+    let word =
+        fold (word = 0:u32) for i c in (enumerate text)
+            i0 := (i as u32 & 3:u32)
+            word := word | (c as u32 << (i0 * 8:u32))
+            if (i0 == 3:u32)
+                'append self word
+                0:u32
+            else word
+    'append self word
+    ;
 
 struct Node
     typeId : Id
     opCode : Op
     operands : (Array Id)
 
-    fn... append (self, operand : Id)
-        'append self.operands operand
-        ;
-    case (self, text : String)
-        numbytes := (countof text)
-        let word =
-            fold (word = 0:u32) for i c in (enumerate text)
-                i0 := (i as u32 & 3:u32)
-                word := word | (c as u32 << (i0 * 8:u32))
-                if (i0 == 3:u32)
-                    'append self.operands word
-                    0:u32
-                else word
-        'append self.operands word
-        ;
-
     fn __hash (self)
         h := (hash self.typeId self.opCode)
         fold (h = h) for operand in self.operands

          
@@ 112,7 193,7 @@ struct Node
                 local ops : (Array Id)
                 va-map
                     inline (value)
-                        'append ops value
+                        append-operand ops value
                     ...
                 ops
 

          
@@ 146,9 227,9 @@ struct UVMIRBuilder
             Node NoType Op.TypeInt
                 width as u32; signed as u32
 
-    fn... floatType (self, width : i32)
+    fn... realType (self, width : i32)
         'nodeId self
-            Node NoType Op.TypeFloat
+            Node NoType Op.TypeReal
                 width as u32
 
     fn boolType (self)

          
@@ 161,7 242,11 @@ struct UVMIRBuilder
 
     fn... arrayType (self, elemId : Id, count : i32)
         'nodeId self
-            Node NoType Op.TypeVector elemId (count as u32)
+            Node NoType Op.TypeArray elemId (count as u32)
+
+    fn... structType (self, ...)
+        'nodeId self
+            Node NoType Op.TypeArray ...
 
     fn... constInt (self, typeId : Id, value : u32)
         'nodeId self

          
@@ 189,33 274,77 @@ struct UVMIRBuilder
         'nodeId self
             Node typeId Op.ConstantComposite ...
 
-    fn... source (self, typeId : Id, sourceId : Id)
+    fn... builtin (self, typeId : Id, name : Builtin)
         'nodeId self
-            Node typeId Op.Source sourceId
+            Node typeId Op.Builtin name
+
+    fn... store (self, node : Id, targetId : Id)
+        'nodeId self
+            Node NoType Op.Store node targetId
 
-    fn... sink (self, node : Id, targetId : Id)
+    fn... intToReal (self, typeId : Id, node : Id)
+        'nodeId self
+            Node typeId Op.ConvertIntToReal node
+
+    fn... realToInt (self, typeId : Id, node : Id)
         'nodeId self
-            Node NoType Op.Sink node targetId
+            Node typeId Op.ConvertRealToInt node
+
+    fn... state (self, typeId : Id, default : Id, name : String)
+        'nodeId self
+            Node typeId Op.State default name
 
     fn... getType (self, node : Id)
         copy ((self.nodes @ node) . typeId)
     fn... getOp (self, node : Id)
         copy ((self.nodes @ node) . opCode)
 
+    inline unary_op (op)
+        fn... (self, x : Id)
+            'nodeId self
+                Node (getType self x) op x
+
+    inline binary_op (op)
+        fn... (self, a : Id, b : Id)
+            'nodeId self
+                Node (getType self a) op a b
+    let
+        sin = (unary_op Op.Sin)
+        cos = (unary_op Op.Cos)
+        fadd = (binary_op Op.FAdd)
+        fmul = (binary_op Op.FMul)
+        fdiv = (binary_op Op.FDiv)
+
+    fn... compositeInsert (self, value : Id, target : Id, ...)
+        'nodeId self
+            Node (getType self target) Op.CompositeInsert value target ...
+
+    fn... compositeConstruct (self, typeId : Id, ...)
+        'nodeId self
+            Node typeId Op.CompositeConstruct ...
+
     fn... tostring
     case (self, nodeId : Id, result : (mutable &String))
         fn idstr (id)
             if (id == 0) "none"
             else
                 "%" .. (tostring id)
+        fn outidstr (id)
+            if (id == 0) "none"
+            else
+                .. "(" (tostring id) ")"
         inline write (...)
             va-map
                 inline (s)
                     'append result s
                 ...
         node := self.nodes @ nodeId
-        write (idstr nodeId) " ="
-        write " " (repr node.opCode)
+        switch node.opCode
+        case Op.Store
+            write (outidstr nodeId) "= "
+        default
+            write (idstr nodeId) " = "
+        write (repr node.opCode)
         if (node.typeId != NoType)
             write " " (idstr node.typeId)
         local idx = 0

          
@@ 227,6 356,9 @@ struct UVMIRBuilder
         inline write-id ()
             let operand = (readop)
             write " " (idstr operand)
+        inline write-builtin ()
+            let builtin = (readop)
+            write " " (repr (bitcast builtin Builtin))
         inline write-i32 ()
             let operand = (readop)
             write " " (repr (operand as i32))

          
@@ 236,10 368,19 @@ struct UVMIRBuilder
         inline write-bool ()
             let operand = (readop)
             write " " (repr (? (operand != 0) true false))
+        inline write-string ()
+            write " "
+            while (idx < operand-count)
+                let word = (readop)
+                for c in (range 4:u32)
+                    byte := (word >> (c * 8:u32)) as i8
+                    if (byte == 0:i8)
+                        break;
+                    write byte
         switch node.opCode
         case Op.TypeInt
             write-i32; write-bool;
-        case Op.TypeFloat
+        case Op.TypeReal
             write-i32;
         case Op.TypeVector
             write-id;

          
@@ 248,12 389,39 @@ struct UVMIRBuilder
             switch ('getOp self node.typeId)
             case Op.TypeInt
                 write-i32;
-            case Op.TypeFloat
+            case Op.TypeReal
                 write-f32;
             default;
         case Op.ConstantComposite
             for i in (range idx operand-count)
                 write-id;
+        case Op.CompositeConstruct
+            for i in (range idx operand-count)
+                write-id;
+        case Op.Builtin
+            write-builtin;
+        pass Op.ConvertIntToReal
+        pass Op.Sin
+        pass Op.Cos
+        do
+            write-id;
+        pass Op.FAdd
+        pass Op.FMul
+        pass Op.FDiv
+        do
+            write-id;
+            write-id;
+        case Op.Store
+            write-id;
+            write-id;
+        case Op.State
+            write-id;
+            write-string;
+        case Op.CompositeInsert
+            write-id;
+            write-id;
+            for i in (range idx operand-count)
+                write-i32;
         default;
         for i in (range idx operand-count)
             let operand = (node.operands @ i)

          
@@ 272,6 440,63 @@ struct UVMIRBuilder
 
 static-if main-module?
     local builder : UVMIRBuilder
+    from (methodsof builder) let builtin integerType realType store
+        \ getType vectorType intToReal state constFloat fadd fmul fdiv sin cos
+        \ constComposite compositeInsert compositeConstruct
+
+    let
+        U32 = (integerType 32 false)
+        F32 = (realType 32)
+    let
+        F32x2 = (vectorType F32 2)
+        F32x4 = (vectorType F32 4)
+        U32x2 = (vectorType U32 2)
+
+    let
+        cf_0 = (constFloat F32 0.0)
+        cf_1 = (constFloat F32 1.0)
+        cf_440 = (constFloat F32 440.0)
+        cf_tau = (constFloat F32 tau)
+
+    let
+        b_samplerate =
+            builtin U32 Builtin.Samplerate
+        b_screensize =
+            builtin U32x2 Builtin.ScreenSize
+        b_screencoord =
+            builtin U32x2 Builtin.ScreenCoord
+        b_audio =
+            builtin F32x2 Builtin.Audio
+        b_color =
+            builtin F32x4 Builtin.Color
+        g_phase =
+            state F32 cf_0 "phase"
+
+    tmp :=
+        fdiv
+            intToReal F32x2 b_screensize
+            intToReal F32x2 b_screencoord
+    store
+        compositeConstruct F32x4 tmp cf_0 cf_1
+        b_color
+
+    store
+        fadd g_phase
+            fdiv (fmul cf_tau cf_440)
+                intToReal F32 b_samplerate
+        g_phase
+
+    tmp := (sin g_phase)
+    store
+        compositeConstruct F32x2 tmp tmp
+        b_audio
+
+    print
+        'tostring builder
+
+#static-if main-module?
+    local builder : UVMIRBuilder
+
     test
         ==
             'integerType builder 32 false