- Language
- Types
- Variable Declarations
- Statement Terminators
- Boolean Logic
- Equality
- Conditionals / Looping
- Exception Handling
- Builtin Functions
- Array()
- Boolean()
- Dictionary()
- Number()
- String()
- abs()
- acos()
- arc()
- asin()
- assert()
- atan()
- atan2()
- background()
- bezier()
- ceil()
- chr()
- compare()
- contains()
- copy()
- cos()
- count()
- degrees()
- description()
- ellipse()
- ellipseMode()
- exit()
- fill()
- filter()
- floor()
- frameRate()
- globals()
- isNaN()
- line()
- locals()
- log()
- loop()
- lowercase()
- map()
- matches()
- max()
- min()
- noFill()
- noStroke()
- ord()
- popStyle()
- position()
- print()
- pushStyle()
- radians()
- random()
- range()
- rect()
- rectMode()
- redraw()
- replace()
- repr()
- rotate()
- round()
- scale()
- sin()
- size()
- sleep()
- sort()
- sqrt()
- stroke()
- strokeCap()
- strokeJoin()
- strokeWeight()
- sum()
- tan()
- translate()
- trim()
- type()
- uppercase()
KonsolSkript is a simple, dynamic, interpreted scripting language designed for use in the Grafik Konsol application. KonsolSkript is implemented in Objective-C using a Test-Driven Development style (it has decent test coverage) and is open-source.
KonsolSkript is heavily influenced by several popular languages, including Python, JavaScript and XPath. It is intended to be appropriate for simple graphics scripting, but also for users learning their very first programming language
KonsolSkript is object-based (everthing is an Object), but not (exactly) object-oriented – there is no class
construct available to users.
KonsolSkript features functions using the sub
construct (short for subroutine). Functions are first-class objects as in Python or JavaScript, and may be treated like any other variable. A function can be passed as an argument to other functions, can be returned by another function and can be assigned as a value to a variable.
The type of any object can be inspected using the type()
function, which returns the object's type name as a String.
type(false) // 'Boolean' type(1.0) // 'Number' type('') // 'String' type([]) // 'Array' type({}) // 'Dictionary' type(sub(){}) // 'Subroutine'
This type has a single value. There is a single object with this value. This object is accessed through the built-in name null
. It is used to signify the absence of a value in many situations, e.g., it is returned from functions that don’t explicitly return anything. Its truth value is false.
An object of type Boolean can have one of two values, true
and false
.
An or
expression is evaluated by evaluating each operand and converting its value to a Boolean as if by a call to the Boolean
function. The result is true if either value is true and false otherwise. The right operand is not evaluated if the left operand evaluates to true.
An and
expression is evaluated by evaluating each operand and converting its value to a Boolean as if by a call to the Boolean
function. The result is true if both values are true and false otherwise. The right operand is not evaluated if the left operand evaluates to false.
An EqualityExpr (that is not just a RelationalExpr) or a RelationalExpr (that is not just an AdditiveExpr) is evaluated by comparing the objects that result from evaluating the two operands.
When the operator is <=
, <
, >=
or >
, then the objects are compared by converting both objects to numbers and comparing the numbers according to IEEE 754. The <
comparison will be true if and only if the first number is less than the second number. The <=
comparison will be true if and only if the first number is less than or equal to the second number. The >
comparison will be true if and only if the first number is greater than the second number. The >=
comparison will be true if and only if the first number is greater than or equal to the second number.
A number represents a floating-point number. A number can have any double-precision 64-bit format IEEE 754 value. These include a special "Not-a-Number" (NaN
) value, positive Infinity
, and positive and negative zero.
There are three different ways to represent a Number literal. All three types allow underscore separator chars (_
) anywhere within the Number literal – these can be used to enhance readability but have no effect on the literal value.
-
Decimal Number literals consist of an optionally signed sequence of digits with an optional decimal point and fractional part.
42 -280 3.14 47. 1_000_000
-
Hexadecimal Number literals consist of an octothorpe (
#
) followed by a sequence of case-insensitive hexadecimal digits (0-9
,A-F
ora-f
). This is particularly convenient for specifying hex color values (as in CSS) when calling builtin functions which deal with colors.#FFFFFF #00CC77 #00_CC_77
-
Binary Number literals consist of a dollar sign (
$
) followed by a sequence of binary digits (0-1
). This is useful for learning bit-fiddling.$1111 $0101_0000
Arithmetic may only be performed on two Number operands – The operands of a numeric operator are type-checked at runtime. A TypeError
runtime exception is thrown if the first operand is a Number, and the second is not.
The +
operator performs addition.
The -
operator performs subtraction.
The /
operator performs floating-point division according to IEEE 754.
The % operator returns the remainder from a truncating division. For example,
5 % 2 // 1 5 % -2 // 1 -5 % 2 // -1 -5 % -2 // -1
NOTE: This is the same as the %
operator in Java and ECMAScript.
NOTE: This is not the same as the IEEE 754 remainder operation, which returns the remainder from a rounding division.
A String is a sequence of values that represent Unicode code points. All the code points in the range U+0000 - U+10FFFF
can be represented in a String. The built-in function ord()
converts a code point from its String form to an integer in the range 0 - 10FFFF
; chr()
converts an integer in the range 0 - 10FFFF
to the corresponding length 1 String object.
More documentation is obviously needed here, but KonsolSkript is implemented in Objective-C, so various features of KS String objects will obviously mirror aspects of NSString
from Objective-C.
String literals are represented with either single or double quotes:
'"Hello", he said.' "It's ok."
Strings may be concatenated using the +
operator, but both operands must be of type String or a TypeError
will be thrown at runtime.
String formatting is done using the %
operator with a string object on the left-hand side, and a single object or an array of objects on the right-hand side, similar to Python. The available format specifiers are %s
, %d
, %i
, and %f
.
'Number: %d' % 1 // 'Number 1' 'Herr %s, Frau %s' % ['Schröder', 'Müller'] // 'Herr Schröder, Frau Müller' "Float format : %0.2f" % 1.0 // 'Float format : 1.00'
KonsolSkript Arrays are virtually identical to Arrays in Python, with two important differences:
- KonsolSkript Arrays are 1-index based (like XPath), not 0-index based (like Python).
- The following syntax (borrowed from PHP) is used for appending an object to an Array in KS:
var a = ['x'] // create array a[] = 'y' // appends 'y' a[] = 'z' // appends 'z' print(a) // ['x', 'y', 'z']
Python-style Array slicing is fully supported in KonsolSkript Arrays (again, with the difference that KS Arrays are 1-index based).
// …continued from above print(a) // ['x', 'y', 'z'] print(a[1]) // 'x' print(a[2]) // 'y' print(a[-1]) // 'z' print(a[-2]) // 'y' print(a[:]) // ['x', 'y', 'z'] print(a[:1]) // 'x' print(a[3:]) // 'z' print(a[1:2]) // ['x', 'y']
Arrays may be concatenated using the +
operator, but both operands must be of type Array or a TypeError
will be thrown at runtime.
KonsolSkript Dictionaries are virtually identical to Dictionaries in Python, with the exception that accessing a key which is not present in the Dictionary returns null
rather than throwing an exception.
var d = {'foo': 1} d['bar'] = 2 print(d) // {'foo': 1, 'bar': 2} print(d['baz']) // null
The terms "function" and "subroutine" are used completely interchangeably in the context of KonsolSkript. In this context, "subroutine" simply means "function" (all subroutines/functions may return a value). But the official type name is Subroutine
.
Subroutines are first class objects in KonsolSkript, and are very similar to functions in JavaScript as of ECMAScript 5.
sub add(a, b) { return a + b } add(2, 4) // 6 var f = add f(3, 5) // 8 var div = sub (a, b) { return a / b } div(4, 2) // 2
Default parameter values are supported as in Python. Any parameter with a default value is option when calling the function:
sub pow(base, exp=2) { return base ** exp } pow(4) // 16 pow(4, 3) // 64
Statements may be explicitly terminated with a semicolon, or the semicolon may be omitted where new lines are inserted.
var foo = 'bar';
Curly braces are always required around statements within a conditional or looping construct – even when there is only one such statement.
For loops are often used in conjunction with the range()
function inspired by Python – with the difference that the resulting iterable object is 1-index based and inclusive.
The following are identical:
for i in range(3) { // 1,2,3 } for i in range(1, 3) { // 1,2,3 } for i in range(1, 3, 1) { // 1,2,3 }
For loops can be used with any iterable type – String, Array or Dictionary:
for c in 'abc' { // 'a','b','c' } for item in ['x', 'y', 'z'] { // 'x','y','z' } var d = {'foo':1, 'bar':2} for key,val in d { print('%s: %s' % [key, val]) }
Exception handling is supported via the try
/ catch
/ finally
construct, very similar to JavaScript.
var resource = checkOut() try { use(resource) } catch ex { print(ex) } finally { checkIn(resource) }
There is currently no "Exception" type. Any "exception" object thrown by the system is a Dictionary with the following string keys:
'name'
– the name of the exception type.'reason'
– a message describing why the exception occurred.'line'
– the line number where the exception occurred.
There is also a throw
construct available to users for custom exception handling or flow control. Objects of any type may be thrown.
throw 'foo'
Name | Dictionary() |
Description | |
Syntax |
Dictionary Dictionary(dictionary?)
|
Parameters |
dictionary? : |
Returns | Dictionary |
Name | arc() |
Description | |
Syntax |
Void arc(x, y, radius, startAngle, endAngle, clockwise)
|
Parameters |
x : y : radius : startAngle : endAngle : clockwise : |
Returns | Void |
Name | assert() |
Description | |
Syntax |
Void assert(test, message?)
|
Parameters |
test : message? : |
Returns | Void |
Name | bezier() |
Description | |
Syntax |
Void bezier(x1, y1?, x2?, y2?, x3?, y3?, x4?, y4?)
|
Parameters |
x1 : y1? : x2? : y2? : x3? : y3? : x4? : y4? : |
Returns | Void |
Name | contains() |
Description | |
Syntax |
Boolean contains(rectArray, pointArray, rectMode?)
|
Parameters |
rectArray : pointArray : rectMode? : |
Returns | Boolean |
Name | ellipse() |
Description | |
Syntax |
Void ellipse(x, y?, width?, height?)
|
Parameters |
x : y? : width? : height? : |
Returns | Void |
Name | filter() |
Description | |
Syntax |
Array filter(array, function)
|
Parameters |
array : function : |
Returns | Array |
Name | line() |
Description | |
Syntax |
Void line(x1, y1?, x2?, y2?)
|
Parameters |
x1 : y1? : x2? : y2? : |
Returns | Void |
Name | locals() |
Description | |
Syntax |
Dictionary locals(recurse?)
|
Parameters |
recurse? : |
Returns | Dictionary |
Name | map() |
Description | |
Syntax |
Array map(array, function)
|
Parameters |
array : function : |
Returns | Array |
Name | matches() |
Description | |
Syntax |
Boolean matches(input, pattern, flags?)
|
Parameters |
input : pattern : flags? : |
Returns | Boolean |
Name | position() |
Description | |
Syntax |
Number position(sequence, object, compareIdentity?)
|
Parameters |
sequence : object : compareIdentity? : |
Returns | Number |
Name | rect() |
Description | |
Syntax |
Void rect(x, y?, width?, height?)
|
Parameters |
x : y? : width? : height? : |
Returns | Void |
Name | replace() |
Description | |
Syntax |
String replace(input, pattern, replacement, flags?)
|
Parameters |
input : pattern : replacement : flags? : |
Returns | String |
Name | scale() |
Description | |
Syntax |
Void scale(width, height?)
|
Parameters |
width : height? : |
Returns | Void |
Name | sort() |
Description | |
Syntax |
Void sort(array, function?)
|
Parameters |
array : function? : |
Returns | Void |