Scripting coming soon...

Post Reply
User avatar
leigh
Site Admin
Posts: 182
Joined: Wed Sep 02, 2009 12:29 pm

Scripting coming soon...

Post by leigh » Sat Jul 24, 2010 6:42 pm

In an upcoming release of ClicksAndWhistles (v3.1) a full scripting capability will be available in the form of "CAWScript" ClicksAndWhistles' new scripting language. There will also be the ability to write plugins directly in C++ potentially allowing the use of other standard scripting languages.

For the curious a draft version of the CAWScript User Guide can be seen here: http://www.clicksandwhistles.com/CAWScript.htm.

A screenshot of CAWScript rendering the Mandelbrot set - not IRC related, just for fun :): http://clicksandwhistles.com/temp/threads%20(CAWScript).png.

Below is a complete CAWScript program that the CAWScript engine can currently compile and run:

Code: Select all

// CAWScript test script

using stdio;
using math;
using time;
#import gui

void print_test()
{
   print_line("Welcome to CAWScript");
   message_box("イルカ", "CAWScript Test Script", ButtonsOk|IconInformation);
   print("-- print test --\n");
   print_line("newline escape characters: \n...\n...\n...");
   print_line("tab escape characters: \t...\t...\t...");
   print_line("quote escape characters: \"...\"...\"...");
   print_line("escape escape characters: \\...\\...\\...");
}

namespace foo
{
   void ns_func() { print_line("foo::ns_func()"); }
   namespace bar
   {
      void ns_func() { print_line("foo::bar::ns_func()"); }
   }
}

void namespace_test()
{
   print_line("-- namespace test --");
   foo::ns_func();
   foo::bar::ns_func();
   using foo;
   ns_func();
}

void wildcard_test()
{
   print_line("-- wildcard test --");
   while(true)
   {
      print("enter wildcard (blank to end): ");
      string wildcard = input_line();
      if (wildcard.empty())
         return;
      print("enter name: ");
      string name = input_line();
      if (name === wildcard)
         print_line("name matches wildcard");
      if (name !== wildcard)
         print_line("name does not match wildcard");   
   }
}

void if_test()
{
   print_line("-- if test --");
   print("enter a number: ");
   int n;
   print_line("the square of " + (n = input()) + " is " + n * n);
   if (n % 2 == 0)
   {
      print(n + " is even and ");
      if (n % 20 == 0)
         print_line("is divisible by 20");
      else if (n % 10 == 0)
         print_line("is divisible by 10");
      else
         print_line("is not divisible by 10 or 20");
   }
   else
      print_line(n + " is odd");
}

void function_with_optional_parameters(int a, [int b, int c[]])
{
   b instanceof int ? print_line("argument b present") : print_line("argument b omitted");
   c instanceof int[] ? print_line("argument c present, c[0] = " + c[0] + ", c[1] = " + c[1]) : print_line("argument c omitted");
}

void var_test(var v)
{
   print_line("-- var test --");
   print_line("passed: " + v);
   v = true;
   print_line("to bool: " + v);
   v = 42;
   print_line("to int (42 * 2): " + v * 2);
   v = "it works.";
   print_line("to string: " + v);
}

void loop_test()
{
   print_line("-- loop test --");
   int n = 0;
   print("for loop test:");
   for(int n = 0; n < 10; ++n)
   {
      if (n == 2)
         continue;
      print(" " + n);
      if (n == 4)
         break;
   }
   print("\nwhile loop test:");
   while(++n < 10)
   {
      if (n == 2)
         continue;
      print(" " + n);
      if (n == 4)
         break;
   }
   print("\ndo/while loop test:");
   n = 0;
   do
   {
      if (n == 2)
         continue;
      print(" " + n);
      if (n == 4)
         break;
   } while(++n < 10);
   print("\nnested break test:");
   while(true)
   {
      while(true)
      {
         while(true)
         {
            break 3;
         }
      }
   }
   print_line(" ok");
}

void map_test()
{
   print_line("-- map test --");
   map m = new map;
   m[1] = "one";
   m["two"] = 2;
   print_line(m[1]);
   print_line(m["two"]);
}

void speed_test()
{
   print_line("-- speed test --\nPlease wait...");
   int iterations = 1000000;
   int n = 0;
   double startTime = uptime();
   while(n < iterations)
   {
      ++n;
   }
   print_line(iterations + " iterations took " + (uptime() - startTime) + " seconds");
}

void integer_constant_test()
{
   print_line("-- integer constant test --");
   print_line("0xFFFFFFFF = " + 0xFFFFFFFF);
   print_line("0xFFFFFFFFL = " + 0xFFFFFFFFL);
   print_line("hex(-1) = " + hex(-1));
   print_line("hex(-1L) = " + hex(-1L));
}

void switch_test()
{
   print_line("-- switch test --");
   for (int i = 1; i < 4; ++i)
   {
      var v;
      switch(i)
      {
      case 1:
         v = 123;
         break;
      case 2:
         v = "abc";
         break;
      case 3:
         v = 1.01;
         break;
      }
      switch(v)
      {
      case 123L:
         print_line(v + " is an integer");
         break;
      case "abc":
         print_line(v + " is a string");
         break;
      default:
         print_line(v + " is a neither an integer nor a string");
         break;
      }
      switch(v)
      {
      case 123:
      case "abc":
         print_line(v + " is an integer or a string");
         break;
      default:
         print_line(v + " is a neither an integer nor a string");
         break;
      }
   }
}

const pure_function(const first, const second)
{
   return first + second;
}

void caw_main()
{
   print_test();
   
   namespace_test();
   
   wildcard_test();
   
   print_line("-- pure function test --");
   const number1 = 10;
   const number2 = 32;
   print_line("sum of " + number1 + " and " + number2 + " = " + pure_function(number1, number2));
   
   if_test();

   var_test("hello, world!");
   
   map_test();
   
   speed_test();
   
   integer_constant_test();
   
   loop_test();
   
   print_line("-- misc tests --");
   
   function_defined_after_call();
   
   int rolls = 6;
   while(rolls-- > 0)
   {
      int dice_roll = random(1, 6);
      print_line("dice roll: " +
         (dice_roll == 1 ? "one" :
         dice_roll == 2 ? "two" :
         dice_roll == 3 ? "three" :
         dice_roll == 4 ? "four" :
         dice_roll == 5 ? "five" : "six") + " (" + dice_roll + ")");
   }
   
   function_with_optional_parameters(1);
   function_with_optional_parameters(1, 2);
   print_line("-- array test --");
   int array[] = { 1, 1110000 + random(100) };
   function_with_optional_parameters(1, 2, array);
   int array2[] = new int[random(5, 10)];
   print_line("array2 has " + array2.size() + " elements");
   for (int i = 0; i != array2.size(); ++i)
   {
      array2[i] = i * 100;
      print_line("array2[" + i + "] = " + array2[i]);
   }
      
   handle h;
   print_line("handle is " + (string)h);
   
   print_line("\"a\" != \"b\"? " + (string)("a" != "b"));   
   
   int a = 100;
   print_line("sin of " + a + " = " + sin(a));
   print_line("100 + 100 / 2 = " + (a + a / 2));
   print_line("(100 + 100)/2 = " + ((a + a)/2));
   
   int n = 1;
   {
      int n = 1;
      ++n;
      print_line("scope test: inner n = " + n);   
   }
   print_line("scope test: outer n = " + n);   
   
   switch_test();
   
   const pi = 3.1415926535897932384626433832795;
   const piSquared = pi * pi;
   print_line("const test: pi = " + pi + ", pi squared = " + piSquared);
}

void function_defined_after_call()
{
   print_line("function defined after call");
}

As you can see CAWScript is very "C" like in appearance but the final released version will have OOP support allowing IRC events to be handled through the use of overridable class methods.

/Leigh
Post Reply