扩展C#与元编程

https://www.cnblogs.com/knat/p/4580393.html

https://www.cnblogs.com/knat/p/4584023.html

扩展C#与元编程(一)

众所周知,Roslyn project已经开源一年多了。简单的说,Roslyn是:1)用C#/VB写的C#/VB的编译器,以及与IDE集成;2)编译器的功能以API的方式暴露出来(即一组DLL)。
Roslyn对大多数开发者来说没啥用处,你关心的是你的应用程序的逻辑与构建而不需要关心编译器是怎么运行的。有时你觉得C#/VB有需要增强的地方,于是你兴致勃勃的跑到Roslyn论坛去发表一个proposal,MS的guy会给你的issue打上Area-Language Design的标签,网友们也许会来讨论一下,然后呢?就没有然后了,这有点像在求雨,下不下、什么时候下、下多大全凭神仙们(MS guys)的"心情"。
求人不如求己,既然Roslyn已经以MIT方式开源,咱们就站在巨人的肩膀上自己动手丰衣足食吧,也就是,扩展C#,做(屌丝级的)complier guy!
众所周知,C#是门general-purpose的编程语言,你想添加的feature既可以是general的,也可以是specific的。比如,XML literal是个general的C# feature,下两图展示了一个specific的feature,让C#支持Windows Workflow Foundation(WF)的activity:



其实,这不该叫"扩展C#",而该叫做"创建一门衍生自C#的DSL",如果你对WF感兴趣,请访问Metah.W: A Workflow Metaprogramming Language
姑且就用"扩展C#"这个叫法。如上两图所示,Metah.W(MW)在C#中加入了activity的构造,在概念上,activity是C# class和function的合体,在图一中,public sealed class摇身一变成为public sealed activity,接着宣告activity的名字,小括号中宣告parameters,如string BookmarkName, string Text,接着宣告可选的return type,如activity Prompt的as int宣告返回类型是int,若无as XXX宣告则返回void。Activity的body中是变量的宣告和statement的使用,statement可以是:1)C# expression statement,如图一中的target = new Random().Next(1, MaxNumber) + 1;;2)well-known statement,如if-else, while, do-while, foreach, try-catch-finally等;3)special statement,如图二中的statemachine, delay。因为activity是class和function的合体,可以调用它并将返回值赋给变量/参数,如guess = new Prompt().Invoke(...)。
将FirstLook.mw送进MW编译器,将产生下面的C#代码:

//FirstLook.mw.cs, generated by the MW compiler

namespace HelloMW.FirstLook

{

public sealed class SequentialNumberGuess : global::System.Activities.Activity

{

public global::System.Activities.InArgument<int> MaxNumber { get; set; }

public global::System.Activities.OutArgument<int> Turns { get; set; }

 

private global::System.Activities.Activity __GetImplementation__()

{

global::System.Activities.Activity __vroot__;

{

var __v__0 = new global::System.Activities.Statements.Sequence();

var target = new global::System.Activities.Variable<int>();

__v__0.Variables.Add(target);

var guess = new global::System.Activities.Variable<int>();

__v__0.Variables.Add(guess);

__v__0.Activities.Add(new global::MetahWActionActivity(__ctx__ =>

{

target.SetEx(__ctx__, new Random().Next(1, MaxNumber.Get(__ctx__)) + 1);

}

));

var __v__1 = new global::System.Activities.Statements.DoWhile();

__v__1.Condition = new global::MetahWFuncActivity<bool>(__ctx__ => guess.Get(__ctx__) != target.Get(__ctx__));

{

var __v__2 = new global::System.Activities.Statements.Sequence();

__v__2.Activities.Add(new Prompt().Initialize(__activity2__ =>

{

__activity2__.BookmarkName = new global::System.Activities.InArgument<string>(new global::MetahWFuncActivity<string>(__ctx__ => "EnterGuess"));

__activity2__.Text = new global::System.Activities.InArgument<string>(new global::MetahWFuncActivity<string>(__ctx__ => "Please enter a number between 1 and " + MaxNumber.Get(__ctx__)));

__activity2__.Result = new global::System.Activities.OutArgument<int>(new global::MetahWLocationActivity<int>(guess));

}

));

__v__2.Activities.Add(new global::MetahWActionActivity(__ctx__ =>

{

Turns.SetEx(__ctx__, __val__ => ++__val__, true);

}

));

var __v__3 = new global::System.Activities.Statements.If();

__v__3.Condition = new global::System.Activities.InArgument<bool>(new global::MetahWFuncActivity<bool>(__ctx__ => guess.Get(__ctx__) != target.Get(__ctx__)));

var __v__4 = new global::System.Activities.Statements.If();

__v__4.Condition = new global::System.Activities.InArgument<bool>(new global::MetahWFuncActivity<bool>(__ctx__ => guess.Get(__ctx__) < target.Get(__ctx__)));

__v__4.Then = new global::MetahWActionActivity(__ctx__ =>

{

Console.WriteLine("Your guess is too low.");

}

);

__v__4.Else = new global::MetahWActionActivity(__ctx__ =>

{

Console.WriteLine("Your guess is too high.");

}

);

__v__3.Then = __v__4;

__v__2.Activities.Add(__v__3);

__v__1.Body = __v__2;

}

__v__0.Activities.Add(__v__1);

__vroot__ = __v__0;

}

return __vroot__;

}

 

private global::System.Func<global::System.Activities.Activity> __implementation__;

protected override global::System.Func<global::System.Activities.Activity> Implementation

{

get

{

return __implementation__ ?? (__implementation__ = __GetImplementation__);

}

set

{

throw new global::System.NotSupportedException();

}

}

}

 

public sealed class Prompt : global::System.Activities.Activity<int>

{

public global::System.Activities.InArgument<string> BookmarkName { get; set; }

public global::System.Activities.InArgument<string> Text { get; set; }

 

private global::System.Activities.Activity __GetImplementation__()

{

global::System.Activities.Activity __vroot__;

var __v__0 = new global::System.Activities.Statements.Sequence();

__v__0.Activities.Add(new global::MetahWActionActivity(__ctx__ =>

{

Console.WriteLine(Text.Get(__ctx__));

}

));

__v__0.Activities.Add(new ReadInt().Initialize(__activity2__ =>

{

__activity2__.BookmarkName = new global::System.Activities.InArgument<string>(new global::MetahWFuncActivity<string>(__ctx__ => BookmarkName.Get(__ctx__)));

__activity2__.Result = new global::System.Activities.OutArgument<int>(new global::MetahWLocationActivity<int>(Result));

}

));

__vroot__ = __v__0;

return __vroot__;

}

 

private global::System.Func<global::System.Activities.Activity> __implementation__;

protected override global::System.Func<global::System.Activities.Activity> Implementation

{

get

{

return __implementation__ ?? (__implementation__ = __GetImplementation__);

}

set

{

throw new global::System.NotSupportedException();

}

}

}

}

这就是元编程,把语法糖解糖的过程,即把高级抽象的描述翻译成低级具体的实现。我觉得,"语法糖"是个肤浅的认识,实际上,多数的"语法糖"都涉及到语义,不仅仅是简单的语法转换。元编程的另一个例子,早期某些C++编译器能将C++代码翻译成等价的C代码,即C++是门元编程语言,它是C的"语法糖"。
日光之下,并无新事。元编程是个非常"古老"的概念,但在每个"时代"它都能玩出耳目一新的花样。
欲知后事如何,请听下回分解。

扩展C#与元编程(二)

如果你对Windows Workflow Foundation(WF)一无所知,当看到扩展C#与元编程(一)中由MW编译器生成的FirstLook.mw.cs时,也许这么在想:我KAO,这是C#版的汇编语言!
WF到底是什么?可以这么认为:WF runtime是高级版的CLR(CLR上的CLR),activity是高级版的MSIL指令(可以勉强这么比喻),Metah.W是高级版的C#。
Activity可以分为两类:primitive activity和composite activity。Primitive activity继承自System.Activities.NativeActivity, System.Activities.NativeActivity<T>等,用来实现流程控制容器,如sequence, if-else, while, foreach, try-catch-finally等:

namespace System.Activities.Statements {

public sealed class Sequence : NativeActivity {

public Sequence();

public Collection<Variable> Variables { get; }

public Collection<Activity> Activities { get; }

//...

}

 

public sealed class If : NativeActivity {

public If();

public InArgument<bool> Condition { get; set; }

public Activity Then { get; set; }

public Activity Else { get; set; }

//...

}

 

public sealed class While : NativeActivity {

public While();

public Collection<Variable> Variables { get; }

public Activity<bool> Condition { get; set; }

public Activity Body { get; set; }

//...

}

 

public sealed class ForEach<T> : NativeActivity {

public ForEach();

public InArgument<IEnumerable<T>> Values { get; set; }

public ActivityAction<T> Body { get; set; }

//...

}

 

public sealed class TryCatch : NativeActivity {

public TryCatch();

public Activity Try { get; set; }

public Collection<Catch> Catches { get; }

public Activity Finally { get; set; }

//...

}

}

WF这个高级CLR令人拍手称快的特性之一是,你可以自定义流程控制容器(可以想象成自定义MSIL指令),比如高大上的状态机:

namespace System.Activities.Statements {

public sealed class StateMachine : NativeActivity {

public StateMachine();

public Collection<Variable> Variables { get; }

public State InitialState { get; set; }

public Collection<State> States { get; }

//...

}

public sealed class State {

public State();

public bool IsFinal { get; set; }

public Collection<Variable> Variables { get; }

public Activity Entry { get; set; }

public Activity Exit { get; set; }

public Collection<Transition> Transitions { get; }

//...

}

public sealed class Transition {

public Transition();

public Activity Trigger { get; set; }

public Activity<bool> Condition { get; set; }

public Activity Action { get; set; }

public State To { get; set; }

//...

}

}

SecondLook.mw展示了如何使用状态机:

一个statemachine至少包含一个common state及一个final state,每个state由唯一的label标识,如InPark, InNeutral等,break关键字标明这是一个final state,statemachine关键字后的goto clause标明initial common state,当流程进入某common state后,首先执行~>所标识的entry statement,接着执行on关键字标识的trigger statement,然后依顺序评估if关键字标识的condition expression,如果某condition评估为true,则执行<~所标识的exit statement和do关键字标识的action statement(SecondLook.mw中未使用),接着跳转到goto关键字所标识的state,如果所有的condition expression都评估为false,则重新执行该state的trigger statement。如果某final state执行完毕,则该statemachine执行完毕。
MW编译器将activity Drive翻译成下面的C#代码:

//SecondLook.mw.cs, generated by MW compiler

namespace HelloMW.SecondLook

{

class Drive : global::System.Activities.Activity

{

private global::System.Activities.Activity __GetImplementation__()

{

global::System.Activities.Activity __vroot__;

{

var __v__0 = new global::System.Activities.Statements.Sequence();

var isMoved = new global::System.Activities.Variable<bool>();

__v__0.Variables.Add(isMoved);

__v__0.Activities.Add(new global::MetahWActionActivity(__ctx__ =>

{

isMoved.SetEx(__ctx__, false);

}

 

));

var __v__1 = new global::System.Activities.Statements.While();

__v__1.Condition = new global::MetahWFuncActivity<bool>(__ctx__ => !isMoved.Get(__ctx__));

{

var __v__2 = new global::System.Activities.Statements.Sequence();

{

var __v__3 = new global::System.Activities.Statements.StateMachine();

var action = new global::System.Activities.Variable<DriveAction>();

__v__3.Variables.Add(action);

var __v__4 = new global::System.Activities.Statements.State();

var __v__5 = new global::System.Activities.Statements.Transition();

var __v__6 = new global::System.Activities.Statements.State();

var __v__7 = new global::System.Activities.Statements.Transition();

var __v__8 = new global::System.Activities.Statements.Transition();

var __v__9 = new global::System.Activities.Statements.Transition();

var __v__10 = new global::System.Activities.Statements.State();

var __v__11 = new global::System.Activities.Statements.Transition();

var __v__12 = new global::System.Activities.Statements.State();

var __v__13 = new global::System.Activities.Statements.Transition();

var __v__14 = new global::System.Activities.Statements.State();

{

__v__3.States.Add(__v__4);

__v__4.Entry = new global::MetahWActionActivity(__ctx__ =>

{

Console.WriteLine("Enter InPark");

}

 

);

__v__4.Exit = new global::MetahWActionActivity(__ctx__ =>

{

Console.WriteLine("Exit InPark");

}

 

);

__v__5.Trigger = new GetDriveAction().Initialize(__activity2__ =>

{

__activity2__.Result = new global::System.Activities.OutArgument<global::HelloMW.SecondLook.DriveAction>(new global::MetahWLocationActivity<global::HelloMW.SecondLook.DriveAction>(action));

}

 

);

__v__5.Condition = new global::MetahWFuncActivity<bool>(__ctx__ => action.Get(__ctx__) == DriveAction.Neutral);

__v__4.Transitions.Add(__v__5);

}

 

{

__v__3.States.Add(__v__6);

__v__6.Entry = new global::MetahWActionActivity(__ctx__ =>

{

Console.WriteLine("Enter InNeutral");

}

 

);

__v__6.Exit = new global::MetahWActionActivity(__ctx__ =>

{

Console.WriteLine("Exit InNeutral");

}

 

);

__v__7.Trigger = new GetDriveAction().Initialize(__activity2__ =>

{

__activity2__.Result = new global::System.Activities.OutArgument<global::HelloMW.SecondLook.DriveAction>(new global::MetahWLocationActivity<global::HelloMW.SecondLook.DriveAction>(action));

}

 

);

__v__7.Condition = new global::MetahWFuncActivity<bool>(__ctx__ => action.Get(__ctx__) == DriveAction.Forward);

__v__6.Transitions.Add(__v__7);

__v__8.Trigger = __v__7.Trigger;

__v__8.Condition = new global::MetahWFuncActivity<bool>(__ctx__ => action.Get(__ctx__) == DriveAction.Reverse);

__v__6.Transitions.Add(__v__8);

__v__9.Trigger = __v__7.Trigger;

__v__9.Condition = new global::MetahWFuncActivity<bool>(__ctx__ => action.Get(__ctx__) == DriveAction.TurnOff);

__v__6.Transitions.Add(__v__9);

}

 

{

__v__3.States.Add(__v__10);

__v__10.Entry = new global::MetahWActionActivity(__ctx__ =>

{

Console.WriteLine("Enter InForward");

isMoved.SetEx(__ctx__, true);

}

 

);

__v__10.Exit = new global::MetahWActionActivity(__ctx__ =>

{

Console.WriteLine("Exit InForward");

}

 

);

__v__11.Trigger = new GetDriveAction().Initialize(__activity2__ =>

{

__activity2__.Result = new global::System.Activities.OutArgument<global::HelloMW.SecondLook.DriveAction>(new global::MetahWLocationActivity<global::HelloMW.SecondLook.DriveAction>(action));

}

 

);

__v__11.Condition = new global::MetahWFuncActivity<bool>(__ctx__ => action.Get(__ctx__) == DriveAction.Neutral);

__v__10.Transitions.Add(__v__11);

}

 

{

__v__3.States.Add(__v__12);

__v__12.Entry = new global::MetahWActionActivity(__ctx__ =>

{

Console.WriteLine("Enter InReverse");

isMoved.SetEx(__ctx__, true);

}

 

);

__v__12.Exit = new global::MetahWActionActivity(__ctx__ =>

{

Console.WriteLine("Exit InReverse");

}

 

);

__v__13.Trigger = new GetDriveAction().Initialize(__activity2__ =>

{

__activity2__.Result = new global::System.Activities.OutArgument<global::HelloMW.SecondLook.DriveAction>(new global::MetahWLocationActivity<global::HelloMW.SecondLook.DriveAction>(action));

}

 

);

__v__13.Condition = new global::MetahWFuncActivity<bool>(__ctx__ => action.Get(__ctx__) == DriveAction.Neutral);

__v__12.Transitions.Add(__v__13);

}

 

{

__v__3.States.Add(__v__14);

__v__14.IsFinal = true;

__v__14.Entry = new global::MetahWActionActivity(__ctx__ =>

{

Console.WriteLine("TurnedOff");

}

 

);

}

 

__v__3.InitialState = __v__4;

__v__5.To = __v__6;

__v__7.To = __v__10;

__v__8.To = __v__12;

__v__9.To = __v__14;

__v__11.To = __v__6;

__v__13.To = __v__6;

__v__2.Activities.Add(__v__3);

}

 

__v__2.Activities.Add(new global::MetahWActionActivity(__ctx__ =>

{

Console.WriteLine("isMoved: " + isMoved.Get(__ctx__));

}

 

));

__v__1.Body = __v__2;

}

 

__v__0.Activities.Add(__v__1);

__vroot__ = __v__0;

}

 

return __vroot__;

}

 

private global::System.Func<global::System.Activities.Activity> __implementation__;

protected override global::System.Func<global::System.Activities.Activity> Implementation

{

get

{

return __implementation__ ?? (__implementation__ = __GetImplementation__);

}

 

set

{

throw new global::System.NotSupportedException();

}

}

}

}

"汇编语言代码"总是冗长无趣的,所以有了Metah.W这样的高级语言。下面是可能的执行结果:

Enter InPark

!action: Neutral

Exit InPark

Enter InNeutral

!action: Reverse

Exit InNeutral

Enter InReverse

!action: TurnOff

!action: Forward

!action: Neutral

Exit InReverse

Enter InNeutral

!action: Forward

Exit InNeutral

Enter InForward

!action: Forward

!action: Forward

!action: Reverse

!action: Neutral

Exit InForward

Enter InNeutral

!action: TurnOff

Exit InNeutral

TurnedOff

isMoved: True

请按任意键继续. . .

Composite activity直接继承自System.Activities.Activity或System.Activities.Activity<T>,它由其它primitive activity和/或composite activity组合而成。Metah.W和WF designer只能创作composite activity。

我不知道大家怎么看待微软这家公司,多数时候MS是家专业但缺乏想象力的公司,有时它能创造出一些令人眼前一亮的作品,C#从第二版开始就一直闪亮,WF是个极富想象力的技术,不过,WF现在还是个藏在深山中的璞玉,希望此文能激起你研究WF的兴趣。关于
Metah.W: A Workflow Metaprogramming的更多信息请访问:https://github.com/knat/Metah
待续。

 

扩展C#与元编程的更多相关文章

  1. 扩展C#与元编程(二)

    如果你对Windows Workflow Foundation(WF)一无所知,当看到扩展C#与元编程(一)中由MW编译器生成的FirstLook.mw.cs时,也许这么在想:我KAO,这是C#版的汇 ...

  2. 扩展C#与元编程(一)

    众所周知,Roslyn project已经开源一年多了.简单的说,Roslyn是:1)用C#/VB写的C#/VB的编译器,以及与IDE集成:2)编译器的功能以API的方式暴露出来(即一组DLL). R ...

  3. C++模板元编程(C++ template metaprogramming)

    实验平台:Win7,VS2013 Community,GCC 4.8.3(在线版) 所谓元编程就是编写直接生成或操纵程序的程序,C++ 模板给 C++ 语言提供了元编程的能力,模板使 C++ 编程变得 ...

  4. atitit.元编程总结 o99

    atitit.元编程总结 o99.doc 1. 元编程(Metaprogramming) 1 2. 元编程的历史and发展 1 3. 元类型and元数据 1 4. 元编程实现方式 2 4.1. 代码生 ...

  5. C++模板元编程 - 3 逻辑结构,递归,一点列表的零碎,一点SFINAE

    本来想把scanr,foldr什么的都写了的,一想太麻烦了,就算了,模板元编程差不多也该结束了,离开学还有10天,之前几天部门还要纳新什么的,写不了几天代码了,所以赶紧把这个结束掉,明天继续抄轮子叔的 ...

  6. effective c++ Item 48 了解模板元编程

    1. TMP是什么? 模板元编程(template metaprogramming TMP)是实现基于模板的C++程序的过程,它能够在编译期执行.你可以想一想:一个模板元程序是用C++实现的并且可以在 ...

  7. Java元编程及其应用

    首先,我们且不说元编程是什么,他能做什么.我们先来谈谈生产力. 同样是实现一个投票系统,一个是python程序员,基于django-framework,用了半小时就搭建了一个完整系统,另外一个是标准的 ...

  8. Java 元编程及其应用

    Java 元编程及其应用 首先,我们且不说元编程是什么,他能做什么.我们先来谈谈生产力. 同样是实现一个投票系统,一个是python程序员,基于django-framework,用了半小时就搭建了一个 ...

  9. ES6中的元编程-Proxy & Reflect

    前言 ES6已经出来好久了,但是工作中比较常用的只有let const声明,通过箭头函数改this指向,使用promise + async 解决异步编程,还有些数据类型方法...所以单独写一篇文章学习 ...

随机推荐

  1. U3d学习001-RollBox例子

    1.世界坐标系和局部坐标系(参照物坐标系)——以参照物为父物体节点  2.刚体组件:       获得GetComponent<Rigidbody>();     移动AddForce(n ...

  2. OpenCV-Python基本功能

    一.图像读取/保存 import cv2 img = cv2.imread("name.png") cv2.imwrite('save.jpg', img) #显示图像 cv2.i ...

  3. unhandledException

    处理未捕获的异常是每个应用程序起码有的功能,C#在AppDomain提供了UnhandledException 事件来接收未捕获到的异常的通知.常见的应用如下: static void Main(st ...

  4. IIS 修改并发连接数

    http://www.cnblogs.com/dudumao/p/4078687.html

  5. Error running app: Default Activity not found ; 安卓程序运行不了,也不报错。

    我最近copy一个工程,写完了去运行时不能运行,项目不报错,就是运行的地方有个叉号:尝试很多办法后准备重新New一个时发现:"10:17 Error running app: Default ...

  6. windows下mysql5.7 root密码重置

    1.在mysql根目录下新建配置文件my.ini(因为我安装的mysql-5.7.24安装完成后未看到该配置文件,所以新建一个,有的直接修改即可) [mysqld] skip-grant-tables ...

  7. Centos7 开放80,3306端口解决办法

    所有扯iptables的文章都是扯淡!!! centos 7 默认防火墙由firewalld来管理!关iptables屁事! 以开放80端口为例,执行以下命令: 开放80端口:firewall-cmd ...

  8. Docker的安装和启动

    2.Docker安装与启动 2.1安装环境说明 Docker官方建议在Ubuntu中安装,因为Docker是基于Ubuntu发布的,而且一般Docker出现的问题Ubuntu是最先更新或者打补丁的.在 ...

  9. uva-10282-枚举

    题意:语言翻译, 直接map即可 #include "pch.h" #include <string> #include<iostream> #includ ...

  10. CentOS之——CentOS7安装iptables防火墙

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/50779761 CentOS7默认的防火墙不是iptables,而是firewall ...