Free Script 3.0

========================================================
Operator
========================================================

operator precedence
	=> <full expression>
	a[b]
	a(b)
	+ - !
	* / \ %
	+ - ++ // ++: join strings or arrays
	<< >>
	..
	== != < <= > >=
	^
	&&
	||

// operator prefix + is a function name which will be translated to @`$operator:prefix+`
function operator prefix +(switch obj);

property opeartor[](switch container, switch index);
property operator[](container of Array, index of Literal){ ... }
property operator[](container of Map, index of Object){ ... }

Special Names:
	$operator:xxx
	$getter:xxx
	$setter:xxx
	$parent

========================================================
Built-in types
========================================================

Object
	Literal
		Integer
		Real
		String
		Char
		Boolean
	Container
		Map
		Array
	Function
	Type
	Context
	Null

========================================================
Expression
========================================================

a[b]
a(b)
x=>x+1
x=>y=>x+y
(x, y)=>x+y
()=>x+y
function(x, y){statements;}
function{statement;}
a.b
(a)
a+b

array:			[1,2,3,4]
map:			{key1:value1, key2:value2}
enumerable:		@[x1, x2, x3:xs]

name
@keyword
@"complex-name"
@`complex-name`

null
1
1.1
"abc"
`abc``def` // syntax suger for: "abc`def"

value // new value in property setter
this
base(BaseClass)

// this should only be there for referencing fields/symbols

========================================================
Declaration
========================================================

var variable [= expression]; // create a variable
const variable = expression; // create a readonly variable

enum Season
{
	Spring,
	Summer,
	Autumn,
	Winter
}

function name[(a,b,c)]
{
	body
}

property variable4
{
	get=>expression;
	get{ body }

	set=>expression;
	set{ body }
}

struct Point // value class
{
   x=0;
   y=0;
   z;
}

var p = new Point{z=0};

class Derived : Base1, Base2
{
	function new(parameters)
		:Base1(arguments1)
		,Base2(arguments2)
	{ ... }

	function Method1 ...
	function Method2 ...
	const variable1 ...			// readonly
	var variable2 ...
	property variable3 ...
	shared variable4 ...		// static and readonly
}

// class's const and var names cannot be conflict with base classes

var d = new Derived(arguments){variable2=null};

namespace System.Collections
{
}

using System.Collections;

extension Enumerable
{
	// can only have function, property, shared
    function Select(selector){ ... }
}

class Array
{
	extent Enumerable;
}

extent class Array
{
	// can only have function, property, shared
	extent Enumerable;
}

========================================================
Attributes
========================================================

/*
multiple attributes can be put before

declaration
member
parameter
*/

#Class(parameters)	// construct an attribute value for a declaration from type ClassAttribute
#Class				// syntax suger for: #Class()

#DefaultEntry("http://*:80")
class Website
{
	#Get("/index.htm")
	function Index()
	{
		return `<html> ... </html>`;
	}

	// /hello/vczh.htm?language=zh-cn
	#Get("/hello/{name}.htm")
	function Hello(name, #query language)
	{
		...
	}
}

function main()
{
	new HttpServer(new Website()).Run();
}

========================================================
Statement
========================================================

expression;
pattern = expression;
reference = expression;

if(condition) statement [else statement]
while(condition) statement
do statement while(condition);
foreach(pattern in xs) statement

switch(expression)
{
	case pattern: statement
	case value: statement
	...
	default: statement // fall in non-existing default branch throws MatchFailedException
}

break;
continue;
return [expression];

try
{
}
catch([pattern])
{
}
finally
{
}

========================================================
Continuation
========================================================

function TakeUntil(xs, p) Enumerator
{
	foreach(x in xs)
	{
		if(!p(x)) return;
		yield x;
	}
}

function Flatten(tree) Enumerator
{
	yield tree;
	join tree.children.Select(Flatten);
}

function Flatten(tree)
{
	return @[tree : tree.children.Select(Flatten)];
}

function button1_Click(sender, args) Async
{
    button1.Enabled=false;
	textBox1.Text=join DownloadAsync("url");
	button1.Enabled=true;
}

function InvokeExpression() Parser
{
    var name = join Expression();
	join Token("(");

	var arguments=new List;
	Parser.Try() yield
	{
		arguments.Add(join Expression());
		Parser.Repeat() yield
		{
			join Token(",");
			arguments.Add(join Expression());
		}
	}

	join Token(")");
	return new InvokeExpression
	{
		Name=name,
		Arguments=arguments
	};
}

function InvokeExpression() Parser
{
	return new InvokeExpression
	{
		Name=join Expression(),
		Arguments= join
			Token("(") >>
				Parser.Repeat(Token(",") >> Expression())
				<< Token(")"),
	};
}

yield expression;
return [expression];
join expression // one expression statement can contains up to one join expression

expression yield [(arguments)] {...} // syntax suger for: join expression(function (arguments) Continuation{...});

========================================================
Double Dispatch
========================================================

function Intersect(switch obj1, switch obj2);

function Intersect(obj1 of Rectangle, obj2 of Ellipse)
{
	...
}

function Intersect(obj1 of Rectangle, obj2 of Rectangle)
{
	...
}

...

// switch and match function should appear in the same scope
// namespaces under the same name is the same scope
// class and extension class is the same scope
// class and extension is not the same scope

========================================================
Pattern Matching
========================================================

@[x1, x2, x3:xs]
[x1, "a", "b"]
name of Type
[a of Rectangle, b of Ellipse]
1
a.b

========================================================
Refactoring
========================================================

GetAppDomain().Assemblies.Types
GetAppDomain().FindType("...")
typeof(Type)
...

========================================================
Context
========================================================

structures:

object
{
	static_symbols_table,
	fields ...
}

context
{
	static_symbols_table,
	parent_context,
	fields ...
}

array
{
	count,
	items ...
}

map
{
    count,
	(key, value) ... // ordered
}

function
{
	function_symbol,
	this_object,
}

////////////////////////////////////////////

static_symbols_table
{
	name,
	base_count,						// context: 0
	symbol_count,
	field_count,
	index_count,
	base_tables ...,				// base_count
	(name, symbols) ...,			// symbol_count, function, property, shared-variable
	field_names ...,				// field_count
	(name, (table, index)) ...,		// index_count, symbol0 == field0, table==(null: object itself, otherwise: table)
}

function_symbol
{
	instruction_entry,
	context,
}

property_symbol
{
	getter_function_index,
	setter_function_index,
}