Angel \”Java\” Lopez on Blog

September 12, 2011

AjScript: Javascript-Alike interpreter in C# (1) Base Interface

Filed under: .NET, AjScript, C Sharp, JavaScript, Open Source Projects, Programming Languages — ajlopez @ 11:12 am

Next Post

I’m working on implementing a C# interpreter of AjScript: a Javascript-alike language. The code in progress is published at:

https://github.com/ajlopez/AjScript

Initially it was at my AjCodeKatas project in Google Code, but now I prefer GitHub: it able me to have more projects. Google Code has a limit in the number of projects, so I was forced to put little projects in the AjCodeKata. Now, GitHub frees me from that limitation. And GitHub with its public forks and pull request has its charm.

The idea is to have an interpreter with the “good parts” of Javascript. I should discuss with myself about inheritance support. But a key point that it supports is: access to native .NET objects. You can write var obj = new System.IO.DirectoryInfo(‘.’) directly in AjScript. And you can invoke AjScript code from a .NET application.

But now, I will present the base interfaces of this interpreter. First, IObject, the interface that every AjScript object should implement:

 

public interface IObject
{
    IFunction Function { get; }
    object GetValue(string name);
    void SetValue(string name, object value);
    ICollection<string> GetNames();
    object Invoke(string name, object[] parameters);
    object Invoke(ICallable method, object[] parameters);
}

Interesting, every object has a Function property: that function is the “class” of the object, the function used in the new command.

To keep the variable names and their values, I have an IContext interface:

public interface IContext
{
    IContext RootContext { get; }
    ReturnValue ReturnValue { get; set;  }
    void SetValue(string name, object value);
    object GetValue(string name);
    void DefineVariable(string name);
}

Note that a context could have a parent context, in order to support nested context. ReturnValue property is used to detect the return command that could have executed many levels inside the current execution.

Any AjScript method implements ICallable:

public interface ICallable
{
    int Arity { get; }
    IContext Context { get; }
    object Invoke(IContext context, object @this, object[] arguments);
}

Note that functions are IObject and ICallable but with the power of create a new object:

public interface IFunction : ICallable, IObject
{
    object NewInstance(object[] parameters);
}

Upcoming posts: examples, closures, native object management, lexer and parser, test and TDD (I wrote the interpreter using TDD, I have > 80% code coverage).

Pending work: implements Function with arguments, prototype with .constructor and other properties, typeof, Javascript original “classes” like Number and others, decide what features of Javascript are “in” and what ones are “out”. Suggestions?

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

1 Comment »


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Shocking Blue Green Theme Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 56 other followers

%d bloggers like this: