As a lot of people pointed out in this question, Lisp is mostly used as a learning experience. Nevertheless, it would be great if I could somehow use my Lisp algorithms and combine them with my C# programs.
In college my profs never could tell me how to use my Lisp routines in a program (no, not writing a GUI in Lisp, thank you).
So how can I?
Try these .Net implementations of Lisp:
IronScheme
IronScheme will aim to be a R6RS
conforming Scheme implementation based
on the Microsoft DLR.
L Sharp .NET
L Sharp .NET is a powerful Lisp-like
scripting language for .NET. It uses a
Lisp dialect similar to Arc but
tightly integrates with the .NET
Framework which provides a rich set of
libraries.
Clojure is a Lisp-1 that is compiled on-the-fly to Java bytecode, leading to very good runtime performance. You can use Clojure, and cross-compile it to a .NET assembly using IKVM's ikvmc. Of course, when used in .NET, Clojure happily generates .NET IL, leading to the same kind of compiled-code performance you can expect when using it on a JVM.
If it's merely the routines you want to use you might try LSharp, which lets you have Lisp expressions in .NET:
http://www.lsharp.org/
The other way around (using .NET from Lisp) would be RDNZL:
http://www.weitz.de/rdnzl/
The .Net 1.1 SDK contains a LISP compiler example. See SDK\v1.1\Tool Developers Guide\Samples\clisp
I know this is a really old question. But I'll try to provide an answer from my own experience and perspective.
To those like us who love the pureness and elegance and simplicity of Scheme/Lisp, I hope this gives you some encouragement and inspiration how they can be very useful in real production :)
I recently open-sourced a Scheme-like interpreter from work called schemy, written in C# (~1500 line of code). And here's the motivation and how it is useful -
Without going into too much detail, I was building a web API server, whose request handling logic is desired to be plug-and-play by other developers/data scientists. There was a clear demand for separation of concern here - the server does not care much about the request handling logic, but it needs to know which requests it can handle and where to find and load the logic for the handlers.
So instead of putting handlers implementation in the server application, the server only provides re-usable "blocks" that can be chained together based on some criteria and logic to form a pipeline, i.e., handlers defined via configuration. We tried JSON/XML to describe such a pipeline and quickly realized that I was essentially building an abstract syntax tree parser.
This was when I realized this was a demand for a lightweight, s-expression based small language. Hence I implemented the embeddable schemy interpreter.
I put an example command handling application here, which captures the essence of the design philosophy for the web server I mentioned above. It works like so:
It extends an embedded Schemy interpreter with some functions implemented
in C#.
It finds .ss scripts which defines a command processing pipeline by using
those implemented functions.
The server finds and persists the composes pipeline from a script by
looking for the symbol EXECUTE which should be of type Func<object, object>.
When a command request comes in, it simply invokes the corresponding
command processor (the one defined by EXECUTE), and responses with the
result.
Finally, here's a complex example script, that provides an online man-page lookup via this TCP command server:
; This script will be load by the server as command `man`. The command
; is consistent of the following functions chained together:
;
; 1. An online man-page look up - it detects the current operating system and
; decides to use either a linux or freebsd man page web API for the look up.
;
; 2. A string truncator `truncate-string` - it truncates the input string, in
; this case the output of the man-page lookup, to the specified number of
; characters.
;
; The client of the command server connects via raw RCP protocol, and can issue
; commands like:
;
; man ls
;
; and gets response of the truncated corresponding online manpage content.
(define EXECUTE
(let ((os (get-current-os))
(max-length 500))
(chain ; chain functions together
(cond ; pick a manpage lookup based on OS
((equal? os "freebsd") (man-freebsd))
((equal? os "linux") (man-linux))
(else (man-freebsd)))
(truncate-string max-length)))) ; truncate output string to a max length
With this script loaded by the command server, a TCP client can issue commands man <unix_command> to the server:
$ ncat 127.0.0.1 8080
man ls
LS(1) FreeBSD General Commands Manual LS(1)
NAME
ls -- list directory contents
SYNOPSIS
ls [--libxo] [-ABCFGHILPRSTUWZabcdfghiklmnopqrstuwxy1,] [-D format]
[file ...]
DESCRIPTION
For each operand that names a file of a type other than directory, ls
displays its name as well as any requested, associated information. For
each operand that names a file of type directory, ls displays the names
of files contained within that directory, as well as any requested,
Perhaps you should take a look at L#. I don't know if it is what you are looking for (haven't touched Lisp since university) but it might be worth to check out.
http://www.lsharp.org/
There is also DotLisp.
Related
Ok, so I was wondering how one would go about creating a program, that creates a second program(Like how most compression programs can create self extracting self excutables, but that's not what I need).
Say I have 2 programs. Each one containing a class. The one program I would use to modify and fill the class with data. The second file would be a program that also had the class, but empty, and it's only purpose is to access this data in a specific way. I don't know, I'm thinking if the specific class were serialized and then "injected" into the second file. But how would one be able to do that? I've found modifying files that were already compiled fascinating, though I've never been able to make changes that didn't cause errors.
That's just a thought. I don't know what the solution would be, that's just something that crossed my mind.
I'd prefer some information in say c or c++ that's cross-platform. The only other language I'd accept is c#.
also
I'm not looking for 3-rd party library's, or things such as Boost. If anything a shove in the right direction could be all I need.
++also
I don't want to be using a compiler.
Jalf actually read what I wrote
That's exactly what I would like to know how to do. I think that's fairly obvious by what I asked above. I said nothing about compiling the files, or scripting.
QUOTE "I've found modifying files that were already compiled fascinating"
Please read and understand the question first before posting.
thanks.
Building an executable from scratch is hard. First, you'd need to generate machine code for what the program would do, and then you need to encapsulate such code in an executable file. That's overkill unless you want to write a compiler for a language.
These utilities that generate a self-extracting executable don't really make the executable from scratch. They have the executable pre-generated, and the data file is just appended to the end of it. Since the Windows executable format allows you to put data at the end of the file, caring only for the "real executable" part (the exe header tells how big it is - the rest is ignored).
For instance, try to generate two self-extracting zip, and do a binary diff on them. You'll see their first X KBytes are exactly the same, what changes is the rest, which is not an executable at all, it's just data. When the file is executed, it looks what is found at the end of the file (the data) and unzips it.
Take a look at the wikipedia entry, go to the external links section to dig deeper:
http://en.wikipedia.org/wiki/Portable_Executable
I only mentioned Windows here but the same principles apply to Linux. But don't expect to have cross-platform results, you'll have to re-implement it to each platform. I couldn't imagine something that's more platform-dependent than the executable file. Even if you use C# you'll have to generate the native stub, which is different if you're running on Windows (under .net) or Linux (under Mono).
Invoke a compiler with data generated by your program (write temp files to disk if necessary) and or stored on disk?
Or is the question about the details of writing the local executable format?
Unfortunately with compiled languages such as C, C++, Java, or C#, you won't be able to just ``run'' new code at runtime, like you can do in interpreted languages like PHP, Perl, and ECMAscript. The code has to be compiled first, and for that you will need a compiler. There's no getting around this.
If you need to duplicate the save/restore functionality between two separate EXEs, then your best bet is to create a static library shared between the two programs, or a DLL shared between the two programs. That way, you write that code once and it's able to be used by as many programs as you want.
On the other hand, if you're really running into a scenario like this, my main question is, What are you trying to accomplish with this? Even in languages that support things like eval(), self modifying code is usually some of the nastiest and bug-riddled stuff you're going to find. It's worse even than a program written completely with GOTOs. There are uses for self modifying code like this, but 99% of the time it's the wrong approach to take.
Hope that helps :)
I had the same problem and I think that this solves all problems.
You can put there whatever code and if correct it will produce at runtime second executable.
--ADD--
So in short you have some code which you can hard-code and store in the code of your 1st exe file or let outside it. Then you run it and you compile the aforementioned code. If eveything is ok you will get a second executable runtime- compiled. All this without any external lib!!
Ok, so I was wondering how one would
go about creating a program, that
creates a second program
You can look at CodeDom. Here is a tutorial
Have you considered embedding a scripting language such as Lua or Python into your app? This will give you the ability to dynamically generate and execute code at runtime.
From wikipedia:
Dynamic programming language is a term used broadly in computer science to describe a class of high-level programming languages that execute at runtime many common behaviors that other languages might perform during compilation, if at all. These behaviors could include extension of the program, by adding new code, by extending objects and definitions, or by modifying the type system, all during program execution. These behaviors can be emulated in nearly any language of sufficient complexity, but dynamic languages provide direct tools to make use of them.
Depending on what you call a program, Self-modifying code may do the trick.
Basically, you write code somewhere in memory as if it were plain data, and you call it.
Usually it's a bad idea, but it's quite fun.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
Is there a ready-to-use C# interpreter out there, that is does not rely on runtime compilation?
My requirements are :
A scripting engine
Must Handle C# syntax
Must work on medium-trust environments
Must not use runtime compilation (CodeDomProvider ...)
Open source (or at least free of charge both for personal and professional use)
If this is not clear, I need something like Jint (http://jint.codeplex.com/), but which allows me to write C# scripts instead of JavaScript ones.
Thanks for your help.
Have you looked at paxScript.NET?
Check out the Mono project. They recently demoed CsharpRepl which sounds like what you're after. The PDC 2008 video here.
Update:
On a close look it seems like using Mono.CSharp service to evaluate scripts won't be possible. Currently it is linked to the Mono runtime and they don't expect it to run in a medium trust environment. See this discussion for more info.
On alternative possibility is to include the Mono C# compiler (sources here) in your project and use it to generate assemblies that you load from the file system. It you are worried about the resources required to load all those assemblies you might have to load them in a separate AppDomain.
I need to evaluate 10000+ small
scripts that are all differents,
compiling all of them would be just
dramatically slow
Interpretting these would be even more painfully slow. We have a similar issue that we address as follows:
We use the Gold Parser project to parse source code and convert it to an XML based 'generic language'. We run this through a transform that generates VB.Net source code (simply because it's case insensitive). We then compile these using the .Net runtime into a standalone DLL, and call this using heavily restricted access.
It sounds as though you are creating something like a dynamic website where people can create custom modules or snippets of functionality, but using C# to do this introduces a couple of main problems; C# has to be compiled, and the only way around this is to interpet it at runtime, and this is unfeasible, and even if you do compile each snippet then you end up with 10,000 DLLs, which is impractical and unusable.
If your snippets are rarely changing, then I would consider programatically wrapping them into a single set of source, with each having a unique name, then compile them in a single shot (or as a timed process every 10mins?). This is what we do, as it also allows 'versioning' of peoples sessions so they continue using the version of DLL they had at the start of their session, but when every session stops using an old version then it's removed.
If your snippets change regularly throughout the day then I would suggest you look at an interpretted scripting language instead, even PHP, and mix your languages depending on the functionality you require. Products such as CScript and LinqPad all use the CodeDomProvider, because you have to have IMSL somewhere if you want to program compiled logic.
The only other option is to write your own interpretter and use reflection to access all the other libraries you need to access, but this is extremely complex and horrible.
As your requirements are effectively unachievable, I would suggest you take a step back and figure out a way of removing one or more restrictions. Whether you find a FullTrust environment to compile your snippets in, remove the need for full code support (i.e. move to interpretted code snippet support), or even change the whole framework to something non .Net.
LINQPad can work as a code snippet IDE. The application is very small and lightweight. It is free (as in beer) but not open-source. Autocompletion costs extra but not much ($19).
Edit: after reading over the comments in this post a little more carefully, I don't think LINQPad is what you want. You need something that can programmatically evaluate thousands of little scripts dynamically, right? I did this at work using Iron Ruby very easily. If you're willing to use a DLR language, this would probably be more feasible. I also did some similar work with some code that could evaluate a C# lambda expression passed in as a string but that was extremely limited.
I have written an open source project, Dynamic Expresso, that can convert text expression written using a C# syntax into delegates (or expression tree). Expressions are parsed and transformed into Expression Trees without using compilation or reflection.
You can write something like:
var interpreter = new Interpreter();
var result = interpreter.Eval("8 / 2 + 2");
or
var interpreter = new Interpreter()
.SetVariable("service", new ServiceExample());
string expression = "x > 4 ? service.SomeMethod() : service.AnotherMethod()";
Lambda parsedExpression = interpreter.Parse(expression,
new Parameter("x", typeof(int)));
parsedExpression.Invoke(5);
My work is based on Scott Gu article http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx .
or http://www.csscript.net/
Oleg was writing a good intro at code project
It doesn't handle exact C# syntax, but PowerShell is so well enmeshed with the .NET framework and is such a mature product, I think you would be unwise to ignore it as at least a possible solution. Most server products being put out by Microsoft are now supporting PowerShell for their scripting interface including Microsoft Exchange and Microsoft SQL Server.
I believe Mono has mint, an interpreter they use before implementing the JIT for a given platform. While the docs in the official site (e.g. Runtime) say it's just an intermediate state before consolidating the jitting VM, I'm pretty sure it was there the last time I compiled it on Linux. I can't quite check it right now, unfortunately, but maybe it's in the direction you want.
bungee# is the thing that you want, in a short time, bungee sharp will be an open source project in
http://www.crssoft.com/Services/Bungee
. you can create scripts with the same c# syntaxt. there is no assembly creation when you run the script, interpretation is done on the fly, so the performance is high. all the keywords are available like c#. I hope u will like it very much..
I faced the same problem. In one project I was looking to provide a generic way to specify conditions controlling when a certain letter has to be generated. In another project the conditions were controlling how cases were assigned to queues. In both of them The following solution worked perfectly:
The Language for the snippets - I chose JScript so that I do not have to worry about variable types.
The Compilation - yes it requires full trust, but you can place your code in a separate assembly and give it full trust. Do not forget to mark it with AllowPartiallyTrustedCaller attribute.
Number of code snippets - I treated every snippet as a method, not a class. This way multiple methods can be combined into a single assembly
Disk usage - I did all compilation in memory without saving the assembly to disk. It also helps if you need to reload it.
All of this works in production without any problems
Edit
Just to clarify 'snippet' - The conditions I am talking about are just boolean expressions. I programatically add additional text to turn it to methods and methods to compilable classes.
Also I can do the same with C# although I still think JScript is better for code snippets
And BTW my code is open source feel free to browse. Just keep in mind there is a lot of code there unrelated to this discussion. Let me know if you need help to locate the pieces concerning the topic
This one works really well
c# repl and interactive interpreter
Is Snippet Compiler something you looking for?
I wrote an application which makes use of a meta-parser generated using CSharpCC (a port of JavaCC). Everything works fine and very good I can say.
For the nature of the project, I would like to have more flexibility on the possibility to extend the syntax of the meta-language used by the application.
Do you know any existing libraries (or articles describing the process of implementation) for Java or C# which I could use to programatically implement my own parser, without being forced to rely to a static syntax?
Thank you very much for the support.
Would Scala's combinator parsers do the trick for you? Since Scala compiles to Java bytecode, anything you write could be called from your Java code however you please.
Take a look at the way that the JNode command-line interface handles parsing of command line arguments. Each command 'registers' descriptors for the arguments it is expecting. The command line syntax is specified separately in XML descriptors, allowing users to tailor a command's syntax to meet their needs.
This is underpinned by a framework of Argument classes that are basically context sensitive token recognizers, and a two level grammar / parser. The parser 'prepares' a user-friendly form of a command syntax into something like BNF, then does a naive backtracking parse, accepting the first complete parse that it finds.
The downside of the current implementation is that the parser is inefficient, and probably impractical for parsing input that is more than 20 or so tokens, depending on the syntax. (We have ideas for improving performance, but a real fix is probably not possible without a major redesign ... and banning potentially ambiguous command syntaxes.)
(Aside: one motivation for this is to support intelligent command argument completion. To do this, the parser runs in a "completion" mode in which it explores all possible (partial) parses, noting its state when it encounters the token / position that the user is trying to complete. Where appropriate, the corresponding Argument classes are then asked to provide context sensitive completions for the current "word".)
The parser (written in C#) used in the Heron language (a simple object-oriented language) is relatively simple and stable, and should be easy to modify for your needs. You can download the source here.
I need to build a scripting interface for my C# program that does system level testing of embedded firmware.
My application contains libraries to fully interact with the devices. There are separate libraries for initiating actions, getting output and tracking success/failure. My application also has a GUI for managing multiple devices and assigning many scripts to be run.
For the testers (non-programmers, but technical), I need to provide a scripting interface that will allow them to come up with different scenarios for testing and run them. They are just going to call my APIs and then return a result to my program (pass/fail and message).
A very basic example of what I want:
TURN_POWER_ON
TUNE_FREQUENCY frequency
WAIT 5
IF GET_FREQUENCY == frequency
REPORT_PASS "Successfully tuned to " + frequency
ELSE
REPORT_FAIL "Failed to tune to " + frequency
ENDIF
TURN_POWER_OFF
Where the reporting, power and frequency functions are provided by my C# libraries.
Will something like IronRuby or IronPython be good for this, or should I just build my own very basic language?
Does the Ruby/Python code get messy when trying to include a bunch of .NET compiled assemblies? I want it to be easy to learn and code for non-programmers and programmers alike.
EDIT:
Thanks for all the great responses. I chose IronPython as the answer since it had the most support, but I'll spend a bit of time with each of IronPython, Boo and IronRuby to see what the testers would prefer to write scripts in.
I've heard very good things about IronPython for exactly this type of scenario. I'd certainly risk spending a few hours on a quick proof-of-concept, to see how it pans out.
Michael Foord will happily wax lyrical about the success of IronPython in similar cases (most specifically for him, for spreadsheet savvy users), and his book covers (IIRC) a few pointers about hosting it from .NET.
You may want to look at Boo, another managed language that runs on the CLR, and which is particularly well suited to build DSL and make your applications scriptable.
The compilation pipeline is directly extensible from the language itself.
Reading the Boo Manifesto is a good starting point if you want to learn more about it.
[Edit] I forgot to mention that Ayende Rahien is writing a complete book on the topic:
Building Domain Specific Languages in Boo
It might be worth considering PowerShell for this sort of task. That can call into .Net just as any of the DLR languages, and has a more natural language type chunking for tasks in its cmdlet (command-let) concept. You have to write the cmdlets in a compiled language at v1 -- in v2 which is being rolled out starting with Win7 and working to older releases in the next few months (v2 for Vista/Win2k8 is at RC now), you can build those in PowerShell directly.
I agree with Marc G, though it's worth mentioning that the general concept is a Domain Specific Langugage. While IronRuby/IronPython aren't strictly domain-specific, they are full-featured and it would leave you to get on with your implementation.
Visual Studio has the DSL tools, and there's the 'M' Grammar stuff you can look into.
But yeah, IronPython.
From the DSL you're going for, I'd recommend to use CUCUMBER with IronRuby.
With Cucumber, testers write tests that look something like that:
Scenario: See all vendors
Given I am logged in as a user in the administrator role
And There are 3 vendors
When I go to the manage vendors page
Then I should see the first 3 vendor names
It is very easy to make this language fit your needs.
Just google "Cucumber and IronRuby" and you'll find several guides and blog posts to get you started.
We are using embedded Iron Python for pricing formula in one of our project. This is how a real sample on how it looks like.
E_DOCUMENT_CHECK = DCPAV * ADS_NUM
E_SPECIFIC_TAX = STV
E_RESOURCE_DEV = RDV
E_LP_ISSUANCE = LPIV
E_ANNUAL_FEES = APFCV * SA * SIDES_NUM
E_SERVICE_FEES= MAX(
MINSFV,
E_DOCUMENT_CHECK+E_SPECIFIC_TAX+E_RESOURCE_DEV+E_LP_ISSUANCE+E_ANNUAL_FEES)
TOTAL= E_DOCUMENT_CHECK+E_SPECIFIC_TAX+E_RESOURCE_DEV+E_LP_ISSUANCE+E_ANNUAL_FEES+E_SERVICE_FEES
It is really straightforward to implement. The Max() function for example is just one of the custom C# method that we import to the IronPython engine and it looks natural to use in a configuration settings.
You could just use C# itself as the scripting language as described here CrowsProgramming - Runtime Scripting in .Net
IronRuby is the most powerful for creating domain-specific languages, because it's syntax is much more flexible and forgiving than python's (your users are going to screw the whitespace up and get annoyed by the mandatory () to call methods).
You could write your sample script in IronRuby and it would look like this:
TURN_POWER_ON
TUNE_FREQUENCY frequency
WAIT 5
if GET_FREQUENCY == frequency
REPORT_PASS "Successfully tuned to " + frequency
else
REPORT_FAIL "Failed to tune to " + frequency
end
TURN_POWER_OFF
Here's a sample of a DSL our Testers are currently using to write automated tests against our UI
window = find_window_on_desktop "OurApplication"
logon_button = window.find "Logon"
logon_button.click
list = window.find "ItemList"
list.should have(0).rows
add_button = window.find "Add new item"
add_button.click
list.should have(1).rows
However, as things stand right now IronPython is much more mature and has much better performance than IronRuby, so you may prefer to use that.
I'd strongly recommend going with either IronPython or IronRuby over creating your own custom language... You'll save an unimaginable amount of effort (and bugs)
Irony could also be a good candidate here.
Here is an example which creates animation for the given image on the fly.
Set camera size: 400 by 300 pixels.
Set camera position: 100, 100.
Move 200 pixels right.
Move 100 pixels up.
Move 250 pixels left.
Move 50 pixels down.
The tutorial for creating such DSL is here:Writing Your First Domain Specific Language
It is an old tutorial but the author still maintains his library here: Github Irony
I found a fairly complex function in a greasemonkey script that I would like to use in my C# app. Basically I am parsing a page and I need to collect all or 4 members of var avar = {}; (i haven't done this yet but they are all strings using var avar.name = "val")
Then I need to call the gm func which returns a string and takes in 3 strings. How can I call the function in C#? I am using .NET 3.5
I'm assuming that you are after some code-reuse on the server-side or in some other freestanding app that processes HTML data.
You can compile (at least a subset of) JavaScript in .net using the Microsoft.JScript.JScriptCodeProvider class -- though note that the class warns
This API supports the .NET Framework
infrastructure and is not intended to
be used directly from your code.
Once compiled the assembly generated (as specified by the CompilerParameters supplied to the provider) should be dynamically loadable. It would be advisable to examine the generated assembly with a tool like Reflector to see what it is you've actually generated, in terms of classes and namespaces.
Disclaimer -- I've only ever used this technique with the CSharpCodeProvider acting on C# source, but I would expect there to be a reasonable level of compatibility across .net languages for this sort of thing.
EDIT -- For an example of compiling JavaScript from C# see this blog post on Verifying JavaScript syntax using C#.
First, you probably want to consider exactly why you're trying to do this. Is it that you want to use the algorithm from the JS in C#? If so, go ahead. If you want to use C# in client-side code (i.e. the browser), go investigate Silverlight instead.
Second, I'm not sure that what you're trying to do is actually possible. Depending on what youre trying to achieve, you may be better off translating the Javascript from the Greasemonkey app into C# 3.5 (assuming that the script's licensing conditions allow this), for use in your app.
The translation shouldn't be hugely difficult - C# has been getting more and more like JS in the last few versions. Just watch out for the "var" keyword; it means something slightly different in C# to what it means in JS (contrast "type inference" in C# with "dynamic typing" in JS).
Of course, maintaining both versions of the code after you've done this will be tricky and painful. I recommend keeping 1 authoritative version of the code if you can.
Good luck!
Can you provide more information about your script and what you want to accomplish? Most Greasemonkey scripts interact with the DOM via the use of Javascript. You can run Javascript in C# but the DOM will not be available to you.