ToyScript

From IPRE Wiki
Jump to: navigation, search

Exploring languages for the Dynamic Language Runtime.

This page is designed to explore creating a new language for the DLR. This works on .NET for Windows and Mono for Windows and the other operating systems.

This information can help in porting languages, like Scheme, to the DLR. To checkout a version of Scheme under development:

svn export http://svn.cs.brynmawr.edu/Myro/trunk/Pyjama/modules/Scheme
cd Scheme
make pjscheme
==> (a b c 1 2 -3.14 #f \"hello there\" #\\newline (e [f . x] . 4) ())
==> (exit)

Background Reading

DLR

  1. http://blogs.msdn.com/mmaly/archive/2008/01/14/building-a-dlr-language-trees.aspx - Overview of ToyScript
  2. http://blogs.msdn.com/mmaly/archive/tags/Dynamic+Language+Runtime/default.aspx - Aspects of the DLR (read from bottom to top)

AST

  1. http://www.bitwisemag.com/2/DLR-Build-Your-Own-Language
  2. http://devhawk.net/2008/01/29/Practical+F+Parsing+Recursion+And+Predicate+Functions.aspx
  3. http://www.knowing.net/PermaLink,guid,526e4ccd-39e8-4932-b9a4-d15e6c82bfbf.aspx
  4. http://blogs.gotdotnet.com/mmaly/archive/2008/01/14/building-a-dlr-language-trees.aspx

Setup

  1. First, you'll need .NET or Mono.
  2. Next, you'll need Microsoft's OSI-approved Scripting tools. Currently, the best way to get that is from the current IronPython build. I'll put a link here to just get the DLLs.
  3. Next you'll need the demo source code: Media:ToyScript.zip

Building

Unzip the zip file, which should put its contents into a subdirectory named ToyScript. In the directory you'll see:

Makefile
Ast
  Assignment.cs
  Binary.cs
  Block.cs
  Call.cs
  Constant.cs
  Def.cs
  Empty.cs
  Expression.cs
  ExpressionStatement.cs
  If.cs
  Import.cs
  Index.cs
  Member.cs
  Named.cs
  New.cs
  Operator.cs
  Parentesized.cs
  Print.cs
  Return.cs
  Statement.cs
  ToyNode.cs
  Var.cs
  While.cs
StringExtensions.cs
Token.cs
TokenKind.cs
ToyBinder.cs
ToyCommandLine.cs
ToyConsole.cs
ToyFunction.cs
ToyGenerator.cs
ToyHelpers.cs
ToyLanguageContext.cs
ToyParser.cs
ToyScope.cs
ToyTokenizer.cs

If you run make in the subdirectory, you'll create ToyScript.exe which can be run with:

mono ToyScript.exe

A test program for ToyScript:

import System

x = 2*5
x+2
y = 64/2
x+y

dt = System.DateTime
dt.Now
dt.Now.Year

m = System.Math
m.PI

m.Sin(2)

i=0
while i < 10 {
  i = i + 1
  if i > 8 { System.Math }
}

if (i > 5) {
  42
}

def f() {
  return 111;
}

print f()

And another test program:

def test() {
   import System
   var ht = new System.Collections.Hashtable()
   var sw = new System.Diagnostics.Stopwatch()
   var start = sw.Start
   var stop  = sw.Stop
   var i = 0;
   start()
   
   while (i < 1000000) {
       ht["Key"] = "value"
       ht["Key"]
       i = i + 1
   }

   stop()

   print "Results"
   print i
   print "***************************"
   print sw.ElapsedMilliseconds
   print sw.Elapsed
}

test()