1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Phantasmal syntax question

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

  1. Levitanus

    Levitanus Senior 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

    4,777
    2,603
    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 Senior Member

    just the firtst indent block)
     
  4. OP
    OP
    Levitanus

    Levitanus Senior 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 Senior 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,131
    553
    Jun 8, 2015
    You guys talk a secret language unknown to me.....
     
  7. OP
    OP
    Levitanus

    Levitanus Senior 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 Senior 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
     

Share This Page