RSS

بایگانی دسته‌ها: Programming

PrettyPrint کردن کد با NRefactory

همانطور که در قبل ذکر آن رفت، یکی‌ از کاربرد‌های NRefactory در Reformat کردن یا اصطلاحا PrintPrint کردن کد است. از این قابلیت در Code Generation می‌توان بسیار سود برد.
ما به راحتی‌ با کنار گذاشتن دغدغه مربوط به چینش کد، قطعات مورد نیاز را به هم میچسبانیم. در نهایت مثلا کدی خواهیم داشت در یک خط. حال این کد دستی‌ تولید شده را به NRefactory میدهیم و خروجی‌ با ظاهر مناسب و مورد نظر ( مثلا KRStyle ) را تحویل میگیریم.

یک نمونه ساده از پیاده سازی چنین عملیاتی را میتوانید از اینجا دانلود کنید.

پی‌‌‌نوشت : باینری‌ها را به صورت جدا و در اینجا آپلود کرده‌ام که اگر کد‌های قبلی‌ رو دانلود کردین نیازی به دانلود دوباره نداشته باشید. فقط میمونه بحث Reference دادن به اونها که دیگه بر عهد خودتون.

اصلاحیه : نسخه قبلی‌ که نوشته بودم یه اشکالی‌ داشت؛ اینکه من اشتباها از Formatter استفاده کرده بودم. از این قابلیت بیشتر برای موارد داخل خود IDE مثل زمانی که Copy-Paste می‌کنیم یا زمانی‌ که IDE خودش برامون یک سری قسمت‌ها رو پر می‌کنه استفاده می‌شه.

در رابطه با کاری که ما می‌خواهیم انجام بدیم، روش درست استفاده از Output visitor‌ها هستش. به این صورت که اول کد رو پارس می‌کنیم، سپس درخت رو میدیم به CSharpOutputVisitor و … بوم !

 
بیان دیدگاه

نوشته شده توسط در سپتامبر 27, 2013 در Coding, Programming

 

برچسب‌ها: , , ,

گفتی NRefactory چی بود ؟

 

همانطور که در پست قبل هم به آن اشاره شد NRefactory یکی‌ از پروژه‌های جانبی SherpDevelop می‌باشد. SharpDevelop در کنار پروژه دیگری موسوم به MonoDevelop دو مورد از جایگزین‌های Open source برای Visual Studio محسوب میشوند که از پارسر Mono برای پارس و کامپایل کردن کد‌های سی‌ شارپ استفاده می‌کند.

استفاده از این کتابخانه در محیط این IDE بسیار متنوع بوده، برای مثال می‌توان از استفاده‌های این کتابخانه برای ایجاد قابلیت IntelliSense ( تشخیص و تکمیل و تصحیح سینتکس ) و یا PrettyPrint و Reformat کردن کد را نام برد.

NRefactory ، پارسری برای سی‌ شارپ :

از نظر تکنیکی‌ کتابخانه NRefactory یک Front-end parser برای سی‌ شارپ محسوب میشود. این کتابخانه با پارس کردن کد‌های یک برنامه به زبان سی‌ شارپ و یا ویژوال بیسیک، Abstract Syntax Tree مرتبط با آن کد را در اختیار ما قرار میدهد. علاوه بر این NRefactory این قابلیت را داشت که گره‌های موجود در این درخت را Resolve کند و Semantic آنها را نیز در اختیار ما بگذرد.

NRefactory پروژه‌ای با تقریبا صد هزار خط کد برنامه سی‌ شارپ است. به رغم حجیم بودن کد اما، این پروژه هم همانند بسیاری از پروژه‌های اپن سورس دیگر، تقریبا مستندات خاصی‌ برای آن وجود ندارد. با این وجود سه هزار و اندی unit test که در سورس اصلی‌ برنامه وجود دارد، منبع بسیار مناسبی برای آشنایی با چند و چون کار با این کتابخانه وسیع است. هرچند در ابتدا ارتباط برقرار کردن با این نوع از مستند‌سازی و استفاده از آن کمی‌ مشکل مینماید، اما پس از چندی این منبع عظیم و به روز، مناسب‌ترین و مطمئن‌ترین مکان برای آشنایی با قابلیت‌های کتابخانه و کم و کیف طرز کار آن تبدیل خواهد شد.

چرا Roslyn نه ؟

Roslyn همانند NRefactory یک Front-end parser برای سی‌ شارپ است که طراحی و پیاده‌سازی آن توسط مایکروسافت در حال پیگیری است. از جمله مشکلاتی که این کتابخانه دارد یکی‌ Stable نبودن و نرسیدن به نسخه نهایی‌ آن است، دیگری اینکه درختی که این برنامه از پارس کردن کد در اختیار ما قرار میدهد برخلاف NRefactory درختی Immutable است و نمی‌توان توسط آن کد را دستخوش تغییر کرد. این کتابخانه بیشتر برای بررسی‌ آماری کد مناسب به نظر می‌آید.

 

برای آشنایی با چگونگی‌ کلیت کار، میتوانید این قطعه کد را دانلود و مورد برسی‌ قرار دهید. هرچند در ابتدا مطالعه این مقاله که به دست یکی‌ از نویسندگان کتابخانه NRefactory نوشت شده، و همچنین آشنایی ابتدائی با Visitor Pattern پیشنهاد میشود. ( در آینده چنانچه فرصت شده به توضیح Visitor Pattern خواهم پرداخت )

 
بیان دیدگاه

نوشته شده توسط در سپتامبر 27, 2013 در Fa, Programming

 

برچسب‌ها: , , ,

NRefactory : Front-end parser for C#

NRefactroy is an open source library for parsing C# source codes, or more precisely a front-end parser for C#.
It can parse your C# codes and provide you with an abstract syntax tree. It can also resolve this parse tree and give you semantic of nodes in the syntax tree.
What makes it more interesting is that the syntax tree produced by this framework is mutable, which means you can change it’s node’s contents; unlike other products, such as not-yet-stable Microsoft® “Roslyn” CTP.
You can use this library to change your source code, analyze it, or reformat it (pretty print);NRefactory project is actually a part of bigger project, SharpDevelop : A free IDE for C#. SharpDevelop takes advantage of this library for IntelliSense implementation and some other stuff.

At the beginning, it may take a while till you become comfortable with representations, APIs and how to interpret and manipulate data structures. To start, take a look this article, wrote by one the authors of NRefactory. In this prologue you can find all preliminary information you need to know in order to dive in.
After that IMHO best practice is to fool around huge unit tests that exist in project itself. It’ll be your best guide through all difficulties and misunderstandings ( there’s about 3000 of theme! ).

I also wrote the simplest possible program with this library that you can download from here. The purpose of this program is only to show you how to glue all the things together and make it work.
Enjoy Parsing.

 
بیان دیدگاه

نوشته شده توسط در سپتامبر 22, 2013 در En, Programming

 

برچسب‌ها: , , ,

Prolog : How to implement if-then structure

How to implement if-then-else in prolog ?
This was a question posted on stack overflow and I though it might be good to say a few words about ot in here.

There is nothing as if or else in a logical programming languages, all you have is some stupid rules and facts 🙂 and you job as a programmer in this environment is not to tell the language how to do the thing ( if foo then bar ), but to describe the problem. You state some facts, establish some rules and based on that ( unification ) the language interpreter will search and find the solutions for any given problem that fits into your description ( backtrack ).

Suppose we want to find the union of two sets . How do we make decisions ? let’s see :

union( [], X, X ).
union( [H|T], X, List ) :- member( H, X ), union( T, X, List ), !.
union( [H|T], X, [H|List] ) :- union( T, X, List ).

You can see we did not use conditions, we just setup different ruls for different conditions.

As another example take a look at this code . In here we want to exclude all the elements of the first list from the second one. We have no condition :

diffLst( _, [], [] ).
diffLst( [], X, X ).
diffLst( [H|T], [H|Rest], Result ) :- diffLst(T, Rest, Result). % Ignore same items
diffLst( Exc, [I|Rest], [I|Result] ) :- [H|_] = Exc, H > I, diffLst( Exc, Rest, Result ).
diffLst( [H|T], Inc, Result ) :- [I|_] = Inc, H < I, diffLst( T, Inc, Result ).

That’s the stuff, isn’t it ?

 
بیان دیدگاه

نوشته شده توسط در آگوست 13, 2012 در Programming

 

برچسب‌ها: , ,

Bomb defusing

In here, we have executable file of a bomb. you’re mission is to defuse the bomb by enrtering correct keyword required in each phase. There’s total of 6 phases plus …

It helps you to learn how to use gdb and objdump in Linux, also gives you a great intuition about how doses gcc compiles you program.

This program has been taken from here. genuinely it was a practice for the book. ( quite interesting book, can help you a lot if you want to go deep in computers. )

Here’s some links that might help you to start your journey :
RMS’s gdb Debugger Tutorial
An introductory video to gdb

https://i0.wp.com/newsimg.bbc.co.uk/media/images/47434000/jpg/_47434056_008856877-1.jpg

 
بیان دیدگاه

نوشته شده توسط در آگوست 6, 2012 در Programming

 

برچسب‌ها: , ,