About FluentScript

FluentScript is a dynamic interpreted language built in C# and part of the CommonLibrary.NET core project.
It is slated for initial release in 0.9.8 Alpha. It combines some features from different languages w/ it's own unique features/semantics.
It is available in ComLib.Lang namespace that is part of the project.

FluentScript has it's own codeplex page: http://fluentscript.codeplex.com

Goals

  • Extensible language via Plugins
  • Fluent like syntax to make code more readable and resemble english
  • Optionally enforce Limits ( # of loops, prevent recursion )
  • Alias keywords/reserved words to make your language more configurable
  • Dynamic interpreted language
  • Configurable interpreter
  • Similar to JavaScript
  • Hook into C# code

Setup


Date: 12/23/2011
Version: 0.9.8.1
Core source: _Core\Lang\Interpreter.cs
Example full: http://commonlibrarynet.codeplex.com/SourceControl/changeset/view/71524#1225564
Example: <Samples_Dir>\Example_Scripting.cs
using ComLib;
using ComLib.Lang;

// Create instance of interpreter.
var i = new Interpreter();

// 1. Example 1 - execute script and get variable.
i.Execute(" var fullname = 'kishore' + ' reddy';");
string name = i.Scope.Get<string>("fullname");



// 2. Example 2 - execute script with functions
i.Execute(" function min(a, b) { if(a < b) return a; return b; } var minval = min(2,3);");
double minval = i.Scope.Get<double>("minval");



// 3. Example 3 - register custom type and execute script.
i.Context.Types.Register(typeof(User), null);
i.Execute("var user = new User('kishore', 'reddy'); var fullname = user.FullName();");
string fullname = i.Scope.Get<string>("fullname");



// 4. Example 4 - get the result of the script execution.
i.Execute("var list1 = [1,2,3,4,5]; var total = list1[1] + list1[2];");
var total = i.Scope.Get<double>("total");
var result = i.Result;
Console.WriteLine(string.Format("Success: {0}, StartTime: {1}, EndTime: {2}", result.Success, result.StartTime.ToShortTimeString(), result.EndTime.ToShortTimeString()));



// 5. Example 5 - set limits on the interpreter.
// This set the total number of loops that can be run to 5. ( just as an example ).
// An LimitException occurrs in the interpreter.
i.Context.Settings.MaxLoopLimit = 5;
i.Execute("var count = 0; for(var ndx = 1; ndx <= 6; ndx++) { count = ndx; }");
var count = i.Scope.Get<double>("count");
Console.WriteLine(string.Format("Success: {0}, Message: {1}", i.Result.Success, i.Result.Message));



// 6. Example 6 - register plugins which extend the syntax and functionality
i.Context.Plugins.Register(new ComLib.Lang.Extensions.DatePlugin());
i.Execute("var date = January 1st 2011 at 9:30 am;");
var date = i.Scope.Get<DateTime>("date");
Console.WriteLine("Success: {0}, Date: {1}", i.Result.Success, date.ToShortDateString());
Back to top

Plugins

1. Aggregate

Description: Provides sum, min, max, avg, count aggregate functions for lists.
Date: 1/11/2012
Version: 0.9.8.2
Core source: _Core\Lang\Plugins\Parser\AggregatePlugin.cs
Example full: http://commonlibrarynet.codeplex.com/SourceControl/changeset/view/71818#1289916
Example: <Samples_Dir>\AggregatePlugin.cs
// Aggregate plugin allows sum, min, max, avg, count aggregate functions to 
// be applied to lists of objects.

var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var result = 0;

// Example 1: Using format sum of <expression>
result = sum of numbers;
result = avg of numbers;
result = min of numbers;
result = max of numbers;
result = count of numbers;

// Example 2: Using format sum(<expression>)
result = sum( numbers );
result = avg( numbers );
result = min( numbers );
result = max( numbers );
result = count( numbers );    
Back to top

2. Bool

Description: Provides synonyms yes/no, on/off for bool values true/false.
Date: 1/11/2012
Version: 0.9.8.2
Core source: _Core\Lang\Plugins\Parser\BoolPlugin.cs
Example full: http://commonlibrarynet.codeplex.com/SourceControl/changeset/view/71818#1290454
Example: <Samples_Dir>\BoolPlugin.cs
// Bool plugin allows aliases for true/false

var result = on;
var result = off;
var result = yes;
var result = no;
Back to top

3. Compare

Description: Provides synonyms for comparison operators
Date: 1/11/2012
Version: 0.9.8.2
Core source: _Core\Lang\Plugins\Parser\ComparePlugin.cs
Example full: http://commonlibrarynet.codeplex.com/SourceControl/changeset/view/71818#1290453
Example: <Samples_Dir>\ComparePlugin.cs
    
// Compare plugin allows word aliases for the comparison operators. See list below
// 
// ALIAS:                FOR:
// "less than",          "<" 
// "before",             "<" 
// "below",              "<" 
// "is below",           "<" 
// "is before",          "<"
// "more than",          ">" 
// "after",              ">" 
// "above",              ">" 
// "is after",           ">" 
// "is above",           ">" 
// "less than equal",    "<="
// "less than equal to", "<="
// "more than equal",    ">="
// "more than equal to", ">="
// "is",                 "=="
// "is equal",           "=="
// "is equal to",        "=="
// "equals",             "=="
// "equal to",           "=="
// "not",                "!="
// "not equal",          "!="
// "not equal to",       "!="
// "is not",             "!="
// "is not equal to",    "!=" 

// Example 1: Using <
if a less than b then
if a before b    then 
if a below  b    then
if a is before b then
if a is below b  then

// Example 2: Using <=
if less than equal then
if less than equal to then

// Example 2: Using >
if a more than b then
if a after b     then 
if a above b     then
if a is after b  then
if a is above b  then    
Back to top

4. Date

Description: Provides word based representation for dates e.g December 21st 2011
Date: 1/11/2012
Version: 0.9.8.2
Core source: _Core\Lang\Plugins\Parser\DatePlugin.cs
Example full: http://commonlibrarynet.codeplex.com/SourceControl/changeset/view/71818#1289913
Example: <Samples_Dir>\DatePlugin.cs
// Date plugin allows date expressions in a friendly format like January 1 2001;
// Following formats are supported.

var date = January 1st 2012;
var date = Jan
date = jan 10
date = Jan 10 2012
date = Jan 10, 2012
date = Jan 10th
date = Jan 10th 2012
date = Jan 10th, 2012
date = January 10
date = January 10, 2012
date = January 10th
date = January 10th, 2012
date = January 10th 2012 at 9:20 am; 

if today is before December 25th 2011 then
    print Still have time to buy gifts
Back to top

5. Day

Description: Provides alias and word based representation for days e.g Tuesday, Saturday, today, tommorrow
Date: 1/11/2012
Version: 0.9.8.2
Core source: _Core\Lang\Plugins\Parser\DayPlugin.cs
Example full: http://commonlibrarynet.codeplex.com/SourceControl/changeset/view/71818#1289914
Example: <Samples_Dir>\DayPlugin.cs
// Day plugin allows days of the week to be used as words. 
// lowercase and uppercase days are supported:
// 1. Monday - Sunday
// 2. monday - sunday

var day = Monday;
day = monday;

if tommorrow is Saturday then
    print Thank god it's Friday
Back to top

6. Env

Description: Provides access to environment variables
Date: 1/11/2012
Version: 0.9.8.2
Core source: _Core\Lang\Plugins\Parser\EnvPlugin.cs
Example full: http://commonlibrarynet.codeplex.com/SourceControl/changeset/view/71818#1289918
Example: <Samples_Dir>\EnvPlugin.cs
// Env plugin allows direct access to environment variables via the "env" object.

// Example 1: Access to user variables only via the ".user" property of env.
result = env.user.path;

// Example 2: Access to system variables via the ".sys" property of env.
result = env.sys.path;

// Example 3: Access to environment variable without specifying sys or user.
result = env.path;
result = env.SystemRoot;
Back to top

7. Fluent

Description: Provides ability to call methods in a fluent way with method before class name. e.g. activate user 'kreddy'
Date: 1/11/2012
Version: 0.9.8.2
Core source: _Core\Lang\Plugins\Parser\FluentPlugin.cs
Example full: http://commonlibrarynet.codeplex.com/SourceControl/changeset/view/71818#1289919
Example: <Samples_Dir>\FluentPlugin.cs
 
 // Fluent plugin allows methods to be called in different ways instead of the 
 // typicall <class>.<method>( <arg1> ,<arg2 etc )

 // Supported ways of calling methods:
 // 1. <method> <class> .
 // 2. <method> <class> <arg1> <arg2> .
 // 3. <class>  <arg1>  <method> .
 // 4. <method> <class> <arg1_name> : <arg1_value>, <arg2_name> : <arg2_value> .
 // 5. <method> <class> <arg1>, 
 
 // Examples
 activate user         
 delete file "c:\temp.txt".
 file "c:\temp.txt" exists.
 run program "msbuild.exe", solution: 'comlib.sln', mode: "debug", style: "rebuild all".
 
 // The above calls translate to:
 // 1. user.activate();
 // 2. File.Delete("c:\temp.txt");
 // 3. File.Exists("c:\temp.txt")
 // 4. Program.Run("msbuild.exe", solution: "comlib.sln', mode: "debug", style: "rebuild all");
Back to top

8. Holiday

Description: Provides holiday names e.g. Christmas, Independence Day, Christmas Eve, New Years
Date: 1/11/2012
Version: 0.9.8.2
Core source: _Core\Lang\Plugins\Parser\HolidayPlugin.cs
Example full: http://commonlibrarynet.codeplex.com/SourceControl/changeset/view/71818#1289952
Example: <Samples_Dir>\HolidayPlugin.cs
// Holiday plugin allows references to dates using Holiday names such as:
// Christmas
// Independence day
// Valentines day
// New Years

if today is New Years 2012 then 
    print happy new year!
Back to top

9. Linq

Description: Provides partial support for linq-like queries on lists.
Date: 1/11/2012
Version: 0.9.8.2
Core source: _Core\Lang\Plugins\Parser\LinqPlugin.cs
Example full: http://commonlibrarynet.codeplex.com/SourceControl/changeset/view/71818#1290456
Example: <Samples_Dir>\LinqPlugin.cs
// Linq plugin is a Light-weight and partial version of Linq style queries and comprehensions
// NOTE: This has limited functionality as of this release.
var books = [ 
                { name: 'book 1', pages: 200, author: 'homey' },
                { name: 'book 2', pages: 120, author: 'kdog' },
                { name: 'book 3', pages: 140, author: 'homeslice' }
            ];
 
// Case 1: start with source <books> and system auto creates variable <book>
var favorites = books where book.pages < 150 and book.author == 'kdog';

// Case 2: using from <variable> in <source>
var favorities = from book in books where book.pages < 150 and book.author == 'kdog';
Back to top

10. Money

Description: Provides dollar sign $ support for numbers.
Date: 1/11/2012
Version: 0.9.8.2
Core source: _Core\Lang\Plugins\Parser\MoneyPlugin.cs
Example full: http://commonlibrarynet.codeplex.com/SourceControl/changeset/view/71818#1289917
Example: <Samples_Dir>\MoneyPlugin.cs
// Money plugin simply allows the $ to be prefixed to numbers.

var salary = $225.50;
if salary is more than $160 then
    print I worked overtime.
Back to top

11. Print

Description: Provides functionality to print to console a line of text without wrapping around quotes.
Date: 1/11/2012
Version: 0.9.8.2
Core source: _Core\Lang\Plugins\Parser\PrintPlugin.cs
Example full: http://commonlibrarynet.codeplex.com/SourceControl/changeset/view/71818#1290455
Example: <Samples_Dir>\PrintPlugin.cs
// Print plugin derives from the "TakeoverPlugin"
// Takeovers are keywords that consume the entire line of text in the script
// after the keyword. 
// In this case of the Print plugin, it consume the rest of the line and you
// don't need to wrap text around quotes.

var language = 'fluentscript';
print No need for quotes in #{language} if text to print is on one line    
Back to top

12. Round

Description: Provides rounding of numbers using round, round up, round down.
Date: 1/11/2012
Version: 0.9.8.2
Core source: _Core\Lang\Plugins\Parser\RoundPlugin.cs
Example full: http://commonlibrarynet.codeplex.com/SourceControl/changeset/view/71818#1292482
Example: <Samples_Dir>\RoundPlugin.cs
// Round plugin provides functions to round, round up or round down numbers.

var a = 2.3;

// Rounds the number using standing round technique of .4
var b = round 2.3;

// Gets rounded up to 3
var c = round up 2.3; 

// Gets rounded down to 2
var d = round down 2.3;
Back to top

13. Run

Description: Provides alternative ways of calling a function.
Date: 1/11/2012
Version: 0.9.8.2
Core source: _Core\Lang\Plugins\Parser\RunPlugin.cs
Example full: http://commonlibrarynet.codeplex.com/SourceControl/changeset/view/71818#1289922
Example: <Samples_Dir>\RunPlugin.cs
// Run plugin provides alternate way to call a function for fluid syntax.
// Notes: 
// 1. The keyword "function" can be aliased with the word "step"
// 2. The name of a function can be in quotes with spaces.

// This is a function with 0 parameters so parentheses are not required
step Cleanup
{
    // do something here.
}
 

// This is a function with string for name and 0 parameters so parentheses are not required
step 'Clean up'
{
    // do something here.
}

// Example 1: Call function normally
Cleanup();

// Example 2: Call function using Run keyword
run Cleanup();

// Example 3: Call function using run without parenthesis for function name.
run Cleanup;

// Example 4: Call function with spaces in name using run with quotes for function name.    
run 'Clean up';

// Example 5: Call function with spaces using run and keyword.
run step 'Clean up';
Back to top

14. Swap

Description: Provides ability to swap values of 2 variables in 1 statement e.g swap a with b;
Date: 1/11/2012
Version: 0.9.8.2
Core source: _Core\Lang\Plugins\Parser\SwapPlugin.cs
Example full: http://commonlibrarynet.codeplex.com/SourceControl/changeset/view/71818#1289912
Example: <Samples_Dir>\SwapPlugin.cs
// Swap plugin provides 1 line statement to swap variables.

var a = 1, b = 2;

// Swap values in 1 statement.
// Instead of needing a third variable.
swap a with b;
Back to top

15. Time

Description: Provides time representation in format 12:30 pm
Date: 1/11/2012
Version: 0.9.8.2
Core source: _Core\Lang\Plugins\Parser\TimePlugin.cs
Example full: http://commonlibrarynet.codeplex.com/SourceControl/changeset/view/71818#1289920
Example: <Samples_Dir>\TimePlugin.cs
// Time plugin provides a convenient way to represent time in fluent syntax.

var t = 12:30 pm;

if t is 12:30 pm then
    print it's time to go to lunch!
Back to top

16. Uri

Description: Provides url and folder representations as strings without quotes.
Date: 1/11/2012
Version: 0.9.8.2
Core source: _Core\Lang\Plugins\Parser\UriPlugin.cs
Example full: http://commonlibrarynet.codeplex.com/SourceControl/changeset/view/71818#1293481
Example: <Samples_Dir>\UriPlugin.cs
// Uri plugin allows you urls and file paths without surrounding them in 
// quotes as long as there are no spaces. These are interpreted as strings.

var url1 = www.yahoo.com;
var url2 = http://www.google.com;
var url3 = http://www.yahoo.com?user=kishore%20&id=123;
var file1 = c:\users\kishore\settings.ini;
var file2 = c:/data/blogposts.xml;

// Since this file has a space in it... you have to surround in quotes.
var file3 = 'c:/data/blog posts.xml';
Back to top


Limits

Limit Default Value Description Example
Maximum Consequetive Expressions 7 Limits the number of consequtive expressions 8 + 9 / result * add(2, total)0
Maximum Consequetive MemberAccess 5 Limits the number of consequtive member access a.b.c.d.e.f.g;
Maximum Exceptions 10 Limits the number of total exceptions that can occur
Maximum Function Params 10 Limits maximum parameters that can be supplied to a function/method call add(a,b,c,d,e,f )
Maximum Loop Limit 200 Limits the total number of loops that can be run accross different for/while statements
Maximum CallStack 15 Limits the call stack depth f1 -> f2 -> f3 -> f4 -> f5 -> f6 -> f7 -> f8
Maximum Statements 200 Limits the number of total statements in the script var/if/while/for/func call are statements
Maximum Script Length 20000 Limits the length of the script in chars

Last edited Jan 11, 2012 at 4:16 PM by kishore_reddy, version 18

Comments

No comments yet.