Skip to content

Basic grammars of Qliphoth

hello31337 edited this page Jan 12, 2020 · 3 revisions

The grammar of Qliphoth language is somewhat resemble to Ruby, Python, or Basic. You can use SGX features without SGXSDK's very heavy burdens, if you use BI-SGX and Qliphoth.

Contents

Data type

Currently, you only can use decimal double value for user-defined local/global variables. You can also use decimal int value, but it will be converted to double when interpreter execute the code.

Exceptionally, some built-in functions support string argument. For details, see dedicated wiki page.

a = 100 // internally converted to double
a = 0200 // "0" will be ignored
a = 3.400
println "Execution Complete." // some built-in functions allow to use string values for arguments.

Comment out

Use // to comment out.

var testval //this is comment out test.

Variable declaration/Assignment

local non-array variable

Implicit

a = 200
b = 300

Explicit

var a, b
var c

a = 100
b = 200
c = 300

global non-array variable

Basic is similar to local variables, but append $ symbol to the head of variable name.

var $a, $b //explicit

$a = 100
$b = 200

$c = 300 // implicit

array variable

Local/global rule is same as non-array variable, but you must user var for array declaration.

var ary1[10], $ary[5] // Index is available in the range of 0 to 10 in this case

ary1[0] = 500
$ary2[4] = 400
$ary2[6] = -9999 // Error. BoF is detected.

Multi dimensional array is currently not supported.

Force explicit declaration

You can force explicit declaration of variable as with FORTRAN's IMPLICIT NONE by describing as following:

option "var"

var a, b

Option must be written in the first line of your source code.

User-defined functions

Declare function

Use func to declare user functions. end is required for the end of function definition.

func add(a,b)
    return a+b
end

total = add(100, 200)

return value

Return values must be double type. If there is no return sentence, 1.0 is automatically returned. If you don't assign the function's return value to any of caller's variables, the result value will be truncated.

func add(a,b)
    println a+b
end

add(300, 400)

Operators

unary operators

  • +: positive
  • -: begative
  • !: negate

binary operators

  • +: addition
  • -: subtraction
  • *: multiplication
  • /: division
  • \: divide and cast the result to integer
  • %: modulo
  • <: less
  • <=: less or equal
  • >: greater
  • >=: greater or equal
  • ==: equal
  • !=: not equal
  • &&: AND
  • ||: OR

Assignment

Value assignment is a STATEMENT, not an EXPRESSION.

a = 100 // OK. This is assignment statement.

if(b=a) < 1000 // Error. "b=a" is expression, so this description is invalid.

"if" sentence

Use if to start, elif, else to add conditional block, end to terminate if sentence.

if flag == 0
    println "false"
elif flag == 1
    println "true"
else
    println "error"
end

"while" sentence

Use while to start, end to terminate while sentence.

i = 0

while i < 10
    println "i: ", i
    i = i + 1
end

"for" sentence

Use for to start, to to designate range, step to designate loop condition value's increment (or decrement) step, and end to terminate for sentence.

i = 0

for i = 0 to 9
   println i // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
end

for i = 0 to 9 step 2
    println i // 0, 2, 4, 6, 8
end

for i = 10 to 5 step -1
    println i // 10, 9, 8, 7, 6, 5
end

"break" sentence

describe "break" only

Immediately exit the loop.

i = 0

while 1
    break
    i = i + 1 // This won't be reached
end

describe "break" with condition

Append ? <condition> after "break" sentence for conditionally break.

while 1
    break ? i <= 4000
end

"return" sentence

There are also conditional and non-conditional type of return sentence. If you omit return value, 1.0 is automatically returned.

return // return 1.0
return ? i < 100 // return 1.0 if i is less than 100
return 500 // return 500
return i ? i > 3000 // return i if i is greater than 3000

"exit" sentence

Terminate interpreter immediately.

main function

If you describe main function, BI-SGX's interpreter execute code from the first line of main function.

func sub1()
    return 2
end

func main() // start from here
    a = sub1()
end

Main function cannot be called by other functions and cannot receive any arguments.

If you don't describe main function, BI-SGX interpreter execute code from the first line of code except for user function definition.

func sub2()
    return 3
end

var a // start from here
a = sub2()

Basic code example

list up prime number

func is_prime(n)
    return 0 ? n < 2
    return 1 ? n == 2
    return 0 ? n%2 == 0
    i = 3
    while i*i <= n
        if n%i == 0
            return 0
        end
        i = i + 2
    end
    return 1
end

func main()
    for n = 1 to 1000
        if is_prime(n)
            print n, " "
        end
    end
    println ""
end

detect BoF error

func main()
    var a, b
    var c[5]
    a = 0
    b = 20

    while 1
        a = a + 1
        c[a] = b
        break ? a > 10
    end

    println "The value of a is: ", a
end

Acknowledgement

As mentioned at README.md, BI-SGX's interpreter grammar and specification is owe to following book:
ISBN978-4-7973-6881-9「明快入門 インタプリタ開発」

BI-SGX interpreter's source code is mainly based on this book, and I manipulated to SGX-available and added various features for bioinformatic analysis.