#!/usr/local/bin/tsc # This is a test script that shows off the features of TOMScript # # There are several test programs, all encapsulated in functions # The functions are run at the bottom of this file. # Some quick notes about the language: # # - Functions must be declared before they are used # # - variables and arrays are dynamically typed and allocated on assignment # eg: x[2] = 3; will autosize the array # # x = "hello"; # x[2] = 4; # x = 3; # x = 4.5; this will change the type....be careful, # strings and arrays are not freed from memory # # - There is a range(x,y,z) command, as in Python, which creates an array # from x to y with a step size z. The x and z params are optional # # - There is a sizeof() command, which will return the size of an array, # string or, variable (always 1) Note: this is a statement, not an # expression. It cannot be used inline. # # - A "foreach x in y" statement, which assigns x to each element of the # array, y, and iterates # # - if, while and switch statements, which all function just like in C # # - Expressions and operators: + - * / % & | ^ ! == != < <= > >= # all should function just like in C # # - Multi-dimentional arrays. The statement: x[2][3] = 4; will allocate # an 2x3 array. A subsequent statement: x[5][6] = 3; will resize # the array. # # - String concatenation. Variables in a concatenation will be turned into # a string. # e.g.: x = "The value of y is: " + y + " and z is: " + z; # ######################################## # Quick sort functions func swap(arr,a,b) { tmp = arr[a]; arr[a] = arr[b]; arr[b] = tmp; } func partition(a,left, right, pivotIdx) { pivotValue = a[pivotIdx]; swap(a,pivotIdx,right); storeIndex = left; i = left; while (i < right) { if (a[i] <= pivotValue) { swap(a, storeIndex, i); storeIndex = storeIndex + 1; } i = i + 1; } swap(a,right,storeIndex); return storeIndex; } func qsort(arr,left, right) { if (right > left) { pivotIndex = (right-left)/2 + left; pivotNewIndex = partition(arr,left, right, pivotIndex); qsort(arr,left, pivotNewIndex-1); qsort(arr,pivotNewIndex+1, right); } } ########################################### # Factorial functions func recurseFact(x) { if (x > 0) return x * recurseFact(x-1); else return 1; } func factorial(x) { y = 1; foreach i in range(2,x) { y = y * i; } return y; } ########################################### # Switch example func switchTest(x) { switch(x) { case 1: println "one"; break; case 2: println "two"; break; case 3: println "three, fallthru"; # no break case 4: println "four, fallthru"; # no break default: # default case can be anywhere in switch println "default"; } println "done."; } ########################################## # Test routines func runFactorialTest() { z = { 0, 1, 2, 3, 5, 7, 8, 10}; foreach x in z { println "Factorial of " + x + " is " + factorial(x); } foreach x in z { println "Recursive Factorial of " + x + " is " + recurseFact(x); } } func runQuicksortTest() { arr = { 5 , 87, 33 , 15, 7 , 67, 1, 34, 12, 72, 1}; #arr = range(10000,1,-1); #arr = range(1,10000,1); println "Before:"; foreach i in arr { print i + " "; } println ""; size = sizeof(arr); qsort(arr,0,size-1); println "After:"; foreach i in arr { print i + " "; } } func runSwitchTest() { arr = {1,2,3,4,5}; foreach x in arr { switchTest(x); } } ###################################### # Heres where we run the tests println "Switch statement Test:"; runSwitchTest(); println "Factorial Test:"; runFactorialTest(); println "Quicksort Test:"; runQuicksortTest();