Phantasmal syntax question

Discussion in 'KONTAKT: Sampling, Programming & Scripting' started by Levitanus, Jun 19, 2018.

  1. Levitanus

    Levitanus Active Member

    I beg You don't ask me what is it and why am I do this ;)

    How would You feel, writing in Python this code:
    Code:
    If(a < b)(
        message('inside if'),
        Else(a == b)(
            message('inside else')
        )
    )
    print('--------\n')
    
    If(a < b)(
        message('inside if'),
        Else()(
            message('inside else')
        )
    )
    print('--------\n')
    
    Select(a)(
        Case(1)(
            message('inside case %s' % 1)
        ),
        Case(2, 4)(
            message('inside case %s to %s' % (2, 4))
        )
    )
    print('--------\n')
    
    For(a, array)(
        message(a)
    )
    with traditional ksp output:
    Code:
    if (a < b)
        message("inside if")
    else
        if (a = b)
            message("inside else")
        end if
    end if
    --------
    
    if (a < b)
        message("inside if")
    else
        message("inside else")
    end if
    --------
    
    select(a)
        case 1
            message("inside case 1")
        case 2 to 4
            message("inside case 2 to 4")
    end select
    --------
    
    a := 0
    while(a <= 3)
        message(array[a])
        inc(a)
    end while
    generally, I solved almost every task to look "Pythonic", but conditions are stumbling block. I feel this syntax the most accepptible. But it's very C-like...
    Attached test source Python code to play with. It isn't working with range yet, as it would force to rewrite "test" class to something more complex.
     

    Attached Files:

  2. EvilDragon

    EvilDragon KSP Wizard

    5,540
    3,328
    May 25, 2010
    Croatia
    You do know that you can use "else if <condition>" with SublimeKSP?

    Also, don't try to make KSP syntax look like Python. You will just hurt yourself. :)
     
  3. OP
    OP
    Levitanus

    Levitanus Active Member

    just the firtst indent block)
     
  4. OP
    OP
    Levitanus

    Levitanus Active Member

    generally, I try to look python syntax like ksp ;)
    At least, it will provide less usage of macro-like operations (including a lot of macro.init), enable file IO for the preprocessor, bring normal import to code. But as always, I may be wrong. Just now it's just holiday-light-scripting ;)
     
  5. OP
    OP
    Levitanus

    Levitanus Active Member

    Code:
            x = kInt()
            self.assertEqual(x.val, 0)
            self.assertEqual(x.name(), '$kInt0')
            self.assertEqual(x._generate_init(), ['declare $kInt0'])
    
            y = kInt(2)
            self.assertEqual(y.val, 2)
            self.assertEqual(y.name(), '$kInt1')
            self.assertEqual(y._generate_init(), ['declare $kInt1 := 2'])
    
            z = kInt(3, 'z')
            self.assertEqual(z.val, 3)
            self.assertEqual(z.name(), '$z')
            self.assertEqual(z._generate_init(), ['declare $z := 3'])
    
            IName.set_compact(True)
            myvar = kInt(1, 'myvar')
            self.assertEqual(myvar.name(), '$cra4x')
            myvar_preserved = kInt(2, 'myvar_preserved', preserve=True)
            self.assertEqual(myvar_preserved.name(), '$myvar_preserved')
            IName.set_compact(False)
            myvar.set_compiled(True)
    
            myvar <<= myvar_preserved
            self.assertEqual(Output().get()[-1],
                             '$cra4x := $myvar_preserved')
            self.assertEqual(myvar.val, '$cra4x')
            self.assertEqual(myvar._get_runtime(), 2)
            myvar <<= myvar_preserved + 1
            self.assertEqual(Output().get()[-1],
                             '$cra4x := $myvar_preserved + 1')
            self.assertEqual(myvar._get_runtime(), 3)
            myvar *= 2
            self.assertEqual(Output().get()[-1],
                             '$cra4x := $cra4x * 2')
            self.assertEqual(myvar._get_runtime(), 6)
    example of conditions syntax (soon will be improoved):
    Code:
            with If((x == y) & (x != 2)):
                check()
                x += 1
                if KSP.is_under_test():
                    self.assertEqual(x(), 1)
            with If(y == 1):
                check()
                x += 1
                if KSP.is_under_test():
                    self.assertEqual(x(), 2)
                with If(y != 2):
                    check()
                    x += 1
                    if KSP.is_under_test():
                        self.assertEqual(x(), 2)
            with Else((x != y) & (x == 1)):
                check()
                x += 1
                if KSP.is_under_test():
                    self.assertEqual(x(), 2)
            with Else():
                check()
                y += 1
                if KSP.is_under_test():
                    self.assertEqual(y, 3)
    output:
    Code:
    if($x = $y and $x # 2)
        $x := $x + 1
    end if
    if($y = 1)
        $x := $x + 1
        if($y # 2)
            $x := $x + 1
        end if
    else
        if($x # $y and $x = 1)
            $x := $x + 1
        else
            $y := $y + 1
        end if
    end if
    Hope to release alpha soon) Couple things and built-ins integration have left
     
    Last edited: Aug 6, 2018
  6. Silence-is-Golden

    Silence-is-Golden ^^

    1,218
    617
    Jun 8, 2015
    You guys talk a secret language unknown to me.....
     
  7. OP
    OP
    Levitanus

    Levitanus Active Member

    as far as I'm ready, it would not be so secret) Just tide to sit in the hunter-house and look to code by myself)
     
    Silence-is-Golden likes this.
  8. OP
    OP
    Levitanus

    Levitanus Active Member

    final loops and conditions representation:
    Code:
    if condition:
    
    x = kInt(1, 'x')
    y = kInt(2, 'y')
    with If((x < y) | (x == 1)):
        check()
        x <<= y
        with If(y == 2):
            check()
            y += 1
        with Else((x == 1) & (y == 2)):
            check()
            y += 1
        with Else():
            check()
            x -= 1
    out:
    Code:
    if($x < $y or $x = 1)
        $x := $y
        if($y = 2)
            $y := $y + 1
        else
            if($x = 1 and $y = 2)
                $y := $y + 1
            else
                $x := $x - 1
            end if
        end if
    end if
    outside the compiplation process (unit-testing):
    Code:
    x = kInt(1, 'x')
    y = kInt(2, 'y')
    out = list()
    
    with If(x < y):
        check()
        out.append('True')
    self.assertEqual(out.pop(), 'True')
    with If(x == y):
        check()
        raise Exception('has to be False')
    with Else(x > y):
        check()
        raise Exception('has to be False')
    with Else():
        out.append('True')
    self.assertEqual(out.pop(), 'True')

    case condition:
    Code:
    x = kInt(2, 'x')
    y = kInt(1, 'y')
    
    code = list()
    Output().set(code)
    with Select(x):
        with Case(1):
            check()
            y += 1
        with Case(2):
            check()
            y += 2
            with Select(y):
                with Case(2):
                    check()
                    y += 1
                with Case(3):
                    check()
                    CondFalse()
    with self.assertRaises(KspCondError):
        with Case(3):
            pass
    out:
    Code:
    select($x)
            case(1)
                $y := $y + 1
            case(2)
                $y := $y + 2
        select($y)
            case(2)
                $y := $y + 1
            case(3)
        end select
    end select


    ForEach loops:
    Code:
    self.arrX = kArrInt([1, 3, 4, 6, 8], 'arrX', size=5)
    self.x = kInt(name='x')
    self.code = list()
    
    with For(arr=self.arrX) as seq:
        for idx, val in enumerate(seq):
            self.x <<= val
    out:
    Code:
    inc($_for_loop_curr_idx)
    %_for_loop_idx[$_for_loop_curr_idx] := 0
    while(%_for_loop_idx[$_for_loop_curr_idx] < 5)
        $x := %arrX[%_for_loop_idx[$_for_loop_curr_idx]]
        inc(%_for_loop_idx[$_for_loop_curr_idx])
    end while
    dec($_for_loop_curr_idx)
    Code:
    arrY = kArrInt(name='arrY', sequence=[1, 2, 3, 6])
    y = kInt(name='y')
    break_indicies = [0, 2, 3, 3, 3]
    with For(arr=self.arrX) as seq:
        for idx, val in enumerate(seq):
            self.x <<= val
            with For(arr=arrY) as seq_y:
                for idx2, val2 in enumerate(seq_y):
                    with If(self.x == val2):
                        check()
                        y <<= val2
                        with self.subTest():
                            self.assertEqual(
                                idx2, break_indicies[idx])
                    with Else():
                        check()
                        Break()
    out:
    Code:
    inc($_for_loop_curr_idx)
    %_for_loop_idx[$_for_loop_curr_idx] := 0
    while(%_for_loop_idx[$_for_loop_curr_idx] < 5)
        $x := %arrX[%_for_loop_idx[$_for_loop_curr_idx]]
        inc($_for_loop_curr_idx)
        %_for_loop_idx[$_for_loop_curr_idx] := 0
        while(%_for_loop_idx[$_for_loop_curr_idx] < 4)
            if($x = %arrY[%_for_loop_idx[$_for_loop_curr_idx]])
                $y := %arrY[%_for_loop_idx[$_for_loop_curr_idx]]
            else
                %_for_loop_idx[$_for_loop_curr_idx] := 4
            end if
            inc(%_for_loop_idx[$_for_loop_curr_idx])
        end while
        dec($_for_loop_curr_idx)
        inc(%_for_loop_idx[$_for_loop_curr_idx])
    end while
    dec($_for_loop_curr_idx)

    ForRange loops:
    Code:
    x = kInt(name='x')
    with For(20) as name:
        for idx, i in enumerate(name):
            x <<= i
            if not KSP.is_compiled():
                with self.subTest():
                    self.assertEqual(i, idx)
    out:
    Code:
    inc($_for_loop_curr_idx)
    %_for_loop_idx[$_for_loop_curr_idx] := 0
    while(%_for_loop_idx[$_for_loop_curr_idx] < 20)
        $x := %_for_loop_idx[$_for_loop_curr_idx]
        %_for_loop_idx[$_for_loop_curr_idx] := %_for_loop_idx[$_for_loop_curr_idx] + 1
    end while
    dec($_for_loop_curr_idx)
    Code:
    x = kInt(name='x')
    with For(2, 5) as name:
        for idx, i in enumerate(name):
            x <<= i
            if not KSP.is_compiled():
                with self.subTest():
                    self.assertEqual(i, idx + 2)
    out:
    Code:
    inc($_for_loop_curr_idx)
    %_for_loop_idx[$_for_loop_curr_idx] := 2
    while(%_for_loop_idx[$_for_loop_curr_idx] < 5)
        $x := %_for_loop_idx[$_for_loop_curr_idx]
        %_for_loop_idx[$_for_loop_curr_idx] := %_for_loop_idx[$_for_loop_curr_idx] + 1
    end while
    dec($_for_loop_curr_idx)
    Code:
    x = kInt(name='x')
    with For(2, 20, 2) as name:
        for idx, i in enumerate(name):
            x <<= i
            if not KSP.is_compiled():
                with self.subTest():
                    self.assertEqual(i, (idx) * 2 + 2)
    out:
    Code:
    inc($_for_loop_curr_idx)
    %_for_loop_idx[$_for_loop_curr_idx] := 2
    while(%_for_loop_idx[$_for_loop_curr_idx] < 20)
        $x := %_for_loop_idx[$_for_loop_curr_idx]
        %_for_loop_idx[$_for_loop_curr_idx] := %_for_loop_idx[$_for_loop_curr_idx] + 2
    end while
    dec($_for_loop_curr_idx)
    While loop:
    Code:
    with While() as w:
        while w(lambda x=self.x, y=self.y: x != y):
            with If(self.y != 10):
                check()
                self.y <<= 10
            self.x += 1
    out:
    Code:
    while($x # $y)
        if($y # 10)
            $y := 10
        end if
        $x := $x + 1
    end while
     
  9. OP
    OP
    Levitanus

    Levitanus Active Member

    finally, the functions!

    Python:
    Code:
    class TestFunc:
    
        def __init__(self):
            self.int = 3
    
        @func
        def method(self, arg=kOut(int)):
            '''sets input variable to self int, and returns it
            If out variable is not assigned just do nothing'''
            arg <<= self.int
    
        @on.note
        def test_calls(self):
            '''will be placed inside the on note callback'''
            arr = kArrInt([1, 2, 3, 4, 5])
    
            @func
            def foo(loc: kArg(int, 5)=arr):
                '''useless, but noticeble example:
                array passed as argument, and then in for-loop
                every item of it is set by the self.method'''
                with For(arr=loc) as seq:
                    for item in seq:
                        self.method(item)
    
            # SublimeKSP taskfunc analogue
            foo()
            message('')
            # SublimeKSP function analogue within true local variables
            foo(inline=True)
    
    Non-compacted output:
    Code:
    on init
        declare %_stack_functions_int_arr[32000]
        declare %_stack_functions_int_idx[100]
        declare $_stack_functions_int_pointer := -1
        declare !_stack_functions_str_arr[32000]
        declare %_stack_functions_str_idx[100]
        declare $_stack_functions_str_pointer := -1
        declare ?_stack_functions_real_arr[32000]
        declare %_stack_functions_real_idx[100]
        declare $_stack_functions_real_pointer := -1
        declare $x
        declare %kArrInt0[5] := (1, 2, 3, 4, 5)
        declare $_for_loop_curr_idx := -1
        declare %_for_loop_idx[20]
    end on
    
    function __main__TestFunc__method
        %_stack_functions_int_arr[0 + %_stack_functions_int_idx[$_stack_functions_int_pointer]] := 3
    end function
    
    function __main__TestFunc__test_calls__foo
        inc($_for_loop_curr_idx)
        %_for_loop_idx[$_for_loop_curr_idx] := 0
        while(%_for_loop_idx[$_for_loop_curr_idx] < 5)
            inc($_stack_functions_int_pointer)
            %_stack_functions_int_idx[$_stack_functions_int_pointer] := %_stack_functions_int_idx[$_stack_functions_int_pointer - 1] + 5
            %_stack_functions_int_arr[0 + %_stack_functions_int_idx[$_stack_functions_int_pointer]] := %%_stack_functions_int_arr[0]
            call __main__TestFunc__method
            %%_stack_functions_int_arr[0] := %_stack_functions_int_arr[0 + %_stack_functions_int_idx[$_stack_functions_int_pointer]]
            dec($_stack_functions_int_pointer)
            inc(%_for_loop_idx[$_for_loop_curr_idx])
        end while
        dec($_for_loop_curr_idx)
    end function
    
    on note
        inc($_stack_functions_int_pointer)
        inc($_for_loop_curr_idx)
        %_for_loop_idx[$_for_loop_curr_idx] := 0
        while(%_for_loop_idx[$_for_loop_curr_idx] < 5)
            %_stack_functions_int_arr[0 + %_stack_functions_int_idx[$_stack_functions_int_pointer]] := %kArrInt0[%_for_loop_idx[$_for_loop_curr_idx]]
            inc(%_for_loop_idx[$_for_loop_curr_idx])
        end while
        dec($_for_loop_curr_idx)
        call __main__TestFunc__test_calls__foo
        dec($_stack_functions_int_pointer)
        message('')
        inc($_stack_functions_int_pointer)
        inc($_for_loop_curr_idx)
        %_for_loop_idx[$_for_loop_curr_idx] := 0
        while(%_for_loop_idx[$_for_loop_curr_idx] < 5)
            %_stack_functions_int_arr[0 + %_stack_functions_int_idx[$_stack_functions_int_pointer]] := %kArrInt0[0]
            inc(%_for_loop_idx[$_for_loop_curr_idx])
        end while
        dec($_for_loop_curr_idx)
        inc($_for_loop_curr_idx)
        %_for_loop_idx[$_for_loop_curr_idx] := 0
        while(%_for_loop_idx[$_for_loop_curr_idx] < 5)
            inc($_stack_functions_int_pointer)
            %_stack_functions_int_idx[$_stack_functions_int_pointer] := %_stack_functions_int_idx[$_stack_functions_int_pointer - 1] + 5
            %_stack_functions_int_arr[0 + %_stack_functions_int_idx[$_stack_functions_int_pointer]] := %%_stack_functions_int_arr[0]
            call __main__TestFunc__method
            %%_stack_functions_int_arr[0] := %_stack_functions_int_arr[0 + %_stack_functions_int_idx[$_stack_functions_int_pointer]]
            dec($_stack_functions_int_pointer)
            inc(%_for_loop_idx[$_for_loop_curr_idx])
        end while
        dec($_for_loop_curr_idx)
        dec($_stack_functions_int_pointer)
    end on
    compacted:
    Code:
    on init
      declare %5c4ew[32000]
      declare %nmvwh[100]
      declare $xapjk := -1
      declare !vswql[32000]
      declare %id3go[100]
      declare $geaqj := -1
      declare ?d1et3[32000]
      declare %dxg0b[100]
      declare $jwuhp := -1
      declare $w1ukf
      declare %bgk2w[5] := (1, 2, 3, 4, 5)
      declare $mrkwa := -1
      declare %mkucx[20]
    end on
    
    function __main__TestFunc__method
      %5c4ew[0+%nmvwh[$xapjk]] := 3
    end function
    
    function __main__TestFunc__test_calls__foo
      inc($mrkwa)
      %mkucx[$mrkwa] := 0
      while (%mkucx[$mrkwa]<5)
        inc($xapjk)
        %nmvwh[$xapjk] := %nmvwh[$xapjk-1]+5
        %5c4ew[0+%nmvwh[$xapjk]] := %5c4ew[0]
        call __main__TestFunc__method
        %5c4ew[0] := %5c4ew[0+%nmvwh[$xapjk]]
        dec($xapjk)
        inc(%mkucx[$mrkwa])
      end while
      dec($mrkwa)
    end function
    
    on note
      inc($xapjk)
      inc($mrkwa)
      %mkucx[$mrkwa] := 0
      while (%mkucx[$mrkwa]<5)
        %5c4ew[0+%nmvwh[$xapjk]] := %bgk2w[%mkucx[$mrkwa]]
        inc(%mkucx[$mrkwa])
      end while
      dec($mrkwa)
      call __main__TestFunc__test_calls__foo
      dec($xapjk)
      message("")
      inc($xapjk)
      inc($mrkwa)
      %mkucx[$mrkwa] := 0
      while (%mkucx[$mrkwa]<5)
        %5c4ew[0+%nmvwh[$xapjk]] := %bgk2w[0]
        inc(%mkucx[$mrkwa])
      end while
      dec($mrkwa)
      inc($mrkwa)
      %mkucx[$mrkwa] := 0
      while (%mkucx[$mrkwa]<5)
        inc($xapjk)
        %nmvwh[$xapjk] := %nmvwh[$xapjk-1]+5
        %5c4ew[0+%nmvwh[$xapjk]] := %5c4ew[0]
        call __main__TestFunc__method
        %5c4ew[0] := %5c4ew[0+%nmvwh[$xapjk]]
        dec($xapjk)
        inc(%mkucx[$mrkwa])
      end while
      dec($mrkwa)
      dec($xapjk)
    end on
    
    
    I know it looks a little bit scary, but if something like that would be imlemented "by hands", it will look very close
     
  10. EvilDragon

    EvilDragon KSP Wizard

    5,540
    3,328
    May 25, 2010
    Croatia
    Totally not getting the point of this here, I must say. :) Seems to totally go above my head as to why would anyone bother make python code compile into KSP, but whatever makes you happy, I guess. :grin:
     
    d.healey likes this.
  11. OP
    OP
    Levitanus

    Levitanus Active Member

    Well, I'll provide the features comparison table after finishing the alpha and sent it to You for tests :2thumbs:
     
    Last edited: Aug 21, 2018
  12. OP
    OP
    Levitanus

    Levitanus Active Member

    Well, I have almost finished! Finish line)
    Now I'm very wondering of what can represent boolean in KSP?
    It's clean, that True is any natural number.
    What about false and null? The KSP itself counts -1 as null and 0 as false, as I understand, but we know this is not the best solution. Any ideas?
     
  13. EvilDragon

    EvilDragon KSP Wizard

    5,540
    3,328
    May 25, 2010
    Croatia
    Not always. For example, find_zone(), find_group(), find_mod() and find_target() all erroneously return 0 if no group/zone with searched name is found. Instead of returning -1 (because 0 is a valid group/zone/modulator index)!

    Just use 0 for false and 1 for true. Not sure what you should do for null. It's up in the air :grin:
     
    Levitanus likes this.
  14. OP
    OP
    Levitanus

    Levitanus Active Member

    More than, sometimes compiler needs to put exactly value, that can be -1, but sometimes I need check if object val is null, for example.
    Current piece of code:
    Code:
    with For(len(cls._ids)) as seq:
        for idx in seq:
            for par, arr in arrays.items():
                arr_val = arr[idx]
                # here is point: maybe there is point to set control
                # par to -1, but generally, if value is None
                # it has not be assigned
                with If(arr_val != -1):
                    params[par].set(cls._ids[idx], arr_val)
    
    output:
    inc($_for_loop_curr_idx)
    %_for_loop_idx[$_for_loop_curr_idx] := 0
    while(%_for_loop_idx[$_for_loop_curr_idx] < 1)
        if(%_MyControl_x[%_for_loop_idx[$_for_loop_curr_idx]] # -1)
            set_control_par(%_MyControl_ids[%_for_loop_idx[$_for_loop_curr_idx]], $CONTRL_PAR_POS_X, ...
                %_MyControl_x[%_for_loop_idx[$_for_loop_curr_idx]])
        end if
        %_for_loop_idx[$_for_loop_curr_idx] := %_for_loop_idx[$_for_loop_curr_idx] + 1
    end while
    dec($_for_loop_curr_idx)
    I think the point is to not introducing exceptions in KSP)
    Or someone just handled 'em wrong way :grin:
     
    Last edited: Aug 27, 2018
  15. OP
    OP
    Levitanus

    Levitanus Active Member

    well, I have logical issue:
    When control par is set at first time, it's not being really set with set_control_par, but just assigned to object for being proceed automatic in for loop.
    Also, gui system allows to use methods grid, pack and place (pretty seemed to tkInter)
    here the code of pack method:
    Code:
    def pack(self, sticky: str=''):
            '''puts widget in the borders of parent.
            sticky can cosists of 'nswe'.
            with one side selected places border of widget to the side.
            with 'ne', 'nw', 'se', 'sw' places widget to the corner
            with 'ns', 'we', 'nswe', and similar combinations stretches
                widget to borders of parent'''
            if not self._parent:
                raise RuntimeError('has to be set to parent')
    
            p_x = self._parent.x
            p_y = self._parent.y
            p_w = self._parent.width
            p_h = self._parent.height
    
            def center_w():
                out = ((p_x + p_w) / 2) - (self.width / 2)
                if isinstance(out, float):
                    out = int(out)
                return out
    
            def center_h():
                out = ((p_y + p_h) / 2) - (self.height / 2)
                if isinstance(out, float):
                    out = int(out)
                return out
    
            if 'e' in sticky:
                if 'w' in sticky:
                    self.width <<= p_w
                else:
                    self.x <<= p_x + p_w - self.width
            if 's' in sticky:
                if 'n' in sticky:
                    self.height <<= p_h
                else:
                    self.y <<= p_y + p_h - self.height
            if 'w' in sticky:
                self.x <<= p_x
            if 'n' in sticky:
                self.y <<= p_y
            if 's' not in sticky and 'n' not in sticky:
                self.y <<= center_h()
            if 'e' not in sticky and 'w' not in sticky:
                self.x <<= center_w()
    So if all flags are used (e.g. 'nsew'), control will be the same size and position as parent, and it will not costs additional lines of code.
    now look at the hands:
    Code:
    self.assertEqual(ch_placed.x, 10)
            self.assertEqual(ch_placed.y, 20 + y_sh)
            self.assertEqual(ch_placed.width, 100)
            self.assertEqual(ch_placed.height, 50)
    
            button = kButton(parent=ch_placed)
            button.pack(sticky='nsew')
    button dimentions are: x=10, y=25, width=100, height=50
    Code:
    button = kButton(parent=ch_placed)
    button.pack(sticky='ns')
    button dimentions are: x=56, y=25, width=-1, height=50
    because width is not set.

    but if assign width/height before pack, lines will be added.
    What is better to do? Leave as it is, make approximation of default dimentions of every control type (like 85x18 for buttons), anything else?
     
    Last edited: Sep 5, 2018
  16. OP
    OP
    Levitanus

    Levitanus Active Member

    well, decided to use class defaults:
    Code:
    self.assertEqual(ch_placed.x, 10)
    self.assertEqual(ch_placed.y, 25)
    self.assertEqual(ch_placed.width, 100)
    self.assertEqual(ch_placed.height, 50)
    
    button = kButton(parent=ch_placed)
    button.pack(sticky='ns')
    x=12, y=25, width=85, height=50
     
  17. OP
    OP
    Levitanus

    Levitanus Active Member

    Wooeee! It works!

    python:
    Code:
    @note
    def note_cb():
        message(EVENT_NOTE)
    
    
    @init
    def null_msg():
        message('')
    
    
    class TestScript(DevTest):
    
        @func
        def switch(self, ids: kArg(int, 6), on_id: int):
            with For(arr=ids) as seq:
                for item in seq:
                    set_control_par(item, 'value', 0)
            set_control_par(on_id, 'value', 1)
            return
    
        def runTest(self):
            script = kScript('file')
            with self.assertRaises(RuntimeError):
                script._generate_code()
    
            def foo():
                mw = kMainWindow()
                buttons_area = kWidget(parent=mw)
                buttons_area.place_pct(20, 10, 50, 80)
                ba_bg = kLabel(parent=buttons_area)
                ba_bg.text <<= ''
                ba_bg.pack(sticky='nswe')
                buttons_area.add_grid(3, 2, 20, 20, 20, 20)
                buttons = [kButton(parent=buttons_area) for b in range(3 * 2)]
    
                def b_callback(control):
                    self.switch(kButton.ids, control.id)
                    message(control.id)
                for idx, b in enumerate(buttons):
                    b.grid(idx % 3, idx // 3)
                    print(f'{b._name} col = {idx // 3}, row = {idx % 2}')
                    b.bound_callback(b_callback)
                with For(arr=kButton.ids) as seq:
                    for item in seq:
                        set_control_par_str(item, 'text', 'mybutton')
    
            script.main = foo
    KSP:
    Code:
    on init
        declare %_stack_functions_int_arr[32000]
        declare %_stack_functions_int_idx[100]
        declare $_stack_functions_int_pointer := -1
        declare !_stack_functions_str_arr[32000]
        declare %_stack_functions_str_idx[100]
        declare $_stack_functions_str_pointer := -1
        declare ?_stack_functions_real_arr[32000]
        declare %_stack_functions_real_idx[100]
        declare $_stack_functions_real_pointer := -1
        declare %_kLabel_ids[1]
        declare %_all_ui_ids[7]
        declare ui_label $control0 (1, 1)
        %_all_ui_ids[0] := get_ui_id($control0)
        %_kLabel_ids[0] := %_all_ui_ids[0]
        declare %_kButton_ids[6]
        declare ui_button $control1
        %_all_ui_ids[1] := get_ui_id($control1)
        %_kButton_ids[0] := %_all_ui_ids[1]
        declare ui_button $control2
        %_all_ui_ids[2] := get_ui_id($control2)
        %_kButton_ids[1] := %_all_ui_ids[2]
        declare ui_button $control3
        %_all_ui_ids[3] := get_ui_id($control3)
        %_kButton_ids[2] := %_all_ui_ids[3]
        declare ui_button $control4
        %_all_ui_ids[4] := get_ui_id($control4)
        %_kButton_ids[3] := %_all_ui_ids[4]
        declare ui_button $control5
        %_all_ui_ids[5] := get_ui_id($control5)
        %_kButton_ids[4] := %_all_ui_ids[5]
        declare ui_button $control6
        %_all_ui_ids[6] := get_ui_id($control6)
        %_kButton_ids[5] := %_all_ui_ids[6]
        declare $_for_loop_curr_idx := -1
        declare %_for_loop_idx[20]
        declare %_all_x_params[7] := (-1, 146, 238, 330, 146, 238, 330)
        declare %_all_y_params[7] := (-1, 30, 30, 30, 50, 50, 50)
        declare %_all_width_params[7] := (-1, 92, 92, 92, 92, 92, 92)
        declare %_all_height_params[7] := (-1, 20, 20, 20, 20, 20, 20)
        inc($_for_loop_curr_idx)
        %_for_loop_idx[$_for_loop_curr_idx] := 0
        while(%_for_loop_idx[$_for_loop_curr_idx] < 7)
            if(%_all_x_params[%_for_loop_idx[$_for_loop_curr_idx]] # -1)
                set_control_par(%_all_ui_ids[%_for_loop_idx[$_for_loop_curr_idx]], $CONTROL_PAR_POS_X, %_all_x_params[%_for_loop_idx[$_for_loop_curr_idx]])
            end if
            if(%_all_y_params[%_for_loop_idx[$_for_loop_curr_idx]] # -1)
                set_control_par(%_all_ui_ids[%_for_loop_idx[$_for_loop_curr_idx]], $CONTROL_PAR_POS_Y, %_all_y_params[%_for_loop_idx[$_for_loop_curr_idx]])
            end if
            if(%_all_width_params[%_for_loop_idx[$_for_loop_curr_idx]] # -1)
                set_control_par(%_all_ui_ids[%_for_loop_idx[$_for_loop_curr_idx]], $CONTROL_PAR_WIDTH, %_all_width_params[%_for_loop_idx[$_for_loop_curr_idx]])
            end if
            if(%_all_height_params[%_for_loop_idx[$_for_loop_curr_idx]] # -1)
                set_control_par(%_all_ui_ids[%_for_loop_idx[$_for_loop_curr_idx]], $CONTROL_PAR_HEIGHT, %_all_height_params[%_for_loop_idx[$_for_loop_curr_idx]])
            end if
            %_for_loop_idx[$_for_loop_curr_idx] := %_for_loop_idx[$_for_loop_curr_idx] + 1
        end while
        dec($_for_loop_curr_idx)
        set_control_par($INST_ICON_ID,$CONTROL_PAR_HIDE,$HIDE_WHOLE_CONTROL)
        set_ui_width_px(633)
        set_ui_height_px(100)
        set_control_par_str(%_all_ui_ids[0], $CONTROL_PAR_TEXT, "")
        set_control_par(%_all_ui_ids[0], $CONTROL_PAR_WIDTH, 316)
        set_control_par(%_all_ui_ids[0], $CONTROL_PAR_HEIGHT, 80)
        set_control_par(%_all_ui_ids[0], $CONTROL_PAR_POS_X, 126)
        set_control_par(%_all_ui_ids[0], $CONTROL_PAR_POS_Y, 10)
        inc($_for_loop_curr_idx)
        %_for_loop_idx[$_for_loop_curr_idx] := 0
        while(%_for_loop_idx[$_for_loop_curr_idx] < 6)
            set_control_par_str(%_kButton_ids[%_for_loop_idx[$_for_loop_curr_idx]], $CONTROL_PAR_TEXT, "mybutton")
            inc(%_for_loop_idx[$_for_loop_curr_idx])
        end while
        dec($_for_loop_curr_idx)
        message("")
    end on
    function __main__TestScript__switch
        inc($_for_loop_curr_idx)
        %_for_loop_idx[$_for_loop_curr_idx] := 0
        while(%_for_loop_idx[$_for_loop_curr_idx] < 6)
            set_control_par(%_stack_functions_int_arr[%_for_loop_idx[$_for_loop_curr_idx] + 0 + %_stack_functions_int_idx[$_stack_functions_int_pointer]], $CONTROL_PAR_VALUE, 0)
            inc(%_for_loop_idx[$_for_loop_curr_idx])
        end while
        dec($_for_loop_curr_idx)
        set_control_par(%_stack_functions_int_arr[6 + %_stack_functions_int_idx[$_stack_functions_int_pointer]], $CONTROL_PAR_VALUE, 1)
    end function
    
    on note
        message($EVENT_NOTE)
    end on
    on ui_control($control1)
        inc($_stack_functions_int_pointer)
        inc($_for_loop_curr_idx)
        %_for_loop_idx[$_for_loop_curr_idx] := 0
        while(%_for_loop_idx[$_for_loop_curr_idx] < 6)
            %_stack_functions_int_arr[0 + %_stack_functions_int_idx[$_stack_functions_int_pointer]] := %_kButton_ids[%_for_loop_idx[$_for_loop_curr_idx]]
            inc(%_for_loop_idx[$_for_loop_curr_idx])
        end while
        dec($_for_loop_curr_idx)
        %_stack_functions_int_arr[6 + %_stack_functions_int_idx[$_stack_functions_int_pointer]] := %_all_ui_ids[1]
        call __main__TestScript__switch
        dec($_stack_functions_int_pointer)
        message(%_all_ui_ids[1])
    end on
    on ui_control($control2)
        inc($_stack_functions_int_pointer)
        inc($_for_loop_curr_idx)
        %_for_loop_idx[$_for_loop_curr_idx] := 0
        while(%_for_loop_idx[$_for_loop_curr_idx] < 6)
            %_stack_functions_int_arr[0 + %_stack_functions_int_idx[$_stack_functions_int_pointer]] := %_kButton_ids[%_for_loop_idx[$_for_loop_curr_idx]]
            inc(%_for_loop_idx[$_for_loop_curr_idx])
        end while
        dec($_for_loop_curr_idx)
        %_stack_functions_int_arr[6 + %_stack_functions_int_idx[$_stack_functions_int_pointer]] := %_all_ui_ids[2]
        call __main__TestScript__switch
        dec($_stack_functions_int_pointer)
        message(%_all_ui_ids[2])
    end on
    on ui_control($control3)
        inc($_stack_functions_int_pointer)
        inc($_for_loop_curr_idx)
        %_for_loop_idx[$_for_loop_curr_idx] := 0
        while(%_for_loop_idx[$_for_loop_curr_idx] < 6)
            %_stack_functions_int_arr[0 + %_stack_functions_int_idx[$_stack_functions_int_pointer]] := %_kButton_ids[%_for_loop_idx[$_for_loop_curr_idx]]
            inc(%_for_loop_idx[$_for_loop_curr_idx])
        end while
        dec($_for_loop_curr_idx)
        %_stack_functions_int_arr[6 + %_stack_functions_int_idx[$_stack_functions_int_pointer]] := %_all_ui_ids[3]
        call __main__TestScript__switch
        dec($_stack_functions_int_pointer)
        message(%_all_ui_ids[3])
    end on
    on ui_control($control4)
        inc($_stack_functions_int_pointer)
        inc($_for_loop_curr_idx)
        %_for_loop_idx[$_for_loop_curr_idx] := 0
        while(%_for_loop_idx[$_for_loop_curr_idx] < 6)
            %_stack_functions_int_arr[0 + %_stack_functions_int_idx[$_stack_functions_int_pointer]] := %_kButton_ids[%_for_loop_idx[$_for_loop_curr_idx]]
            inc(%_for_loop_idx[$_for_loop_curr_idx])
        end while
        dec($_for_loop_curr_idx)
        %_stack_functions_int_arr[6 + %_stack_functions_int_idx[$_stack_functions_int_pointer]] := %_all_ui_ids[4]
        call __main__TestScript__switch
        dec($_stack_functions_int_pointer)
        message(%_all_ui_ids[4])
    end on
    on ui_control($control5)
        inc($_stack_functions_int_pointer)
        inc($_for_loop_curr_idx)
        %_for_loop_idx[$_for_loop_curr_idx] := 0
        while(%_for_loop_idx[$_for_loop_curr_idx] < 6)
            %_stack_functions_int_arr[0 + %_stack_functions_int_idx[$_stack_functions_int_pointer]] := %_kButton_ids[%_for_loop_idx[$_for_loop_curr_idx]]
            inc(%_for_loop_idx[$_for_loop_curr_idx])
        end while
        dec($_for_loop_curr_idx)
        %_stack_functions_int_arr[6 + %_stack_functions_int_idx[$_stack_functions_int_pointer]] := %_all_ui_ids[5]
        call __main__TestScript__switch
        dec($_stack_functions_int_pointer)
        message(%_all_ui_ids[5])
    end on
    on ui_control($control6)
        inc($_stack_functions_int_pointer)
        inc($_for_loop_curr_idx)
        %_for_loop_idx[$_for_loop_curr_idx] := 0
        while(%_for_loop_idx[$_for_loop_curr_idx] < 6)
            %_stack_functions_int_arr[0 + %_stack_functions_int_idx[$_stack_functions_int_pointer]] := %_kButton_ids[%_for_loop_idx[$_for_loop_curr_idx]]
            inc(%_for_loop_idx[$_for_loop_curr_idx])
        end while
        dec($_for_loop_curr_idx)
        %_stack_functions_int_arr[6 + %_stack_functions_int_idx[$_stack_functions_int_pointer]] := %_all_ui_ids[6]
        call __main__TestScript__switch
        dec($_stack_functions_int_pointer)
        message(%_all_ui_ids[6])
    end on
     
  18. OP
    OP
    Levitanus

    Levitanus Active Member

    Working on the first library within the package. Fixed some bugs, made an indentation, and pasting docs and comments to the result code :)
    Also, some new preprocessor classes were designed. If I can implement nkitool within Python, the package will consist some sort of instrument editor.
    2018-11-01_13-56-20.png
    2018-11-01_14-07-03.png
     
    Last edited: Nov 1, 2018

Share This Page