I really like F# but I feel like it's not succint and short enough. I want to go further. I do have an idea of how I'd like to improve it but I have no experience in making compilers so I thought I'd make it a scripting language. Then I realized that I could make it a scripting language and interpret it using F# but still get pretty much 100% performance thanks to F# having the inline
option. Am I right? Is it really possible to make a script interpreter in F# that would go through my script and turn it into lots of functors and stuff and so get really good performance?

- 400
- 1
- 3
- 13
-
Most languages are implemented in other languges. So yes you could interpret a scripting language in F# – t3dodson Jun 11 '15 at 22:09
-
Yes but usually when a language is scripted it is a lot slower. The main point of my question was if it might be in theory possible to get good performance by doing it this way. Lua (one of the fastest scripted langauges) for example compiles its code into bytecode and that makes it faster. I thought if by reading the code and turning it into inline functions I could get good performance. – Preza8 Jun 11 '15 at 22:15
-
Sure you could compile a scripted language to IL code (what F# is when compiled). You would need to ensure a one - to -one mapping for language features to scripting features. i.e. don't add things to the scripting languge that are not possible in .net. – t3dodson Jun 11 '15 at 22:26
-
I probably didn't make myself clear on what is the purpose of this supposed to be. I'll try to explain it this way - the interpreter doesn't compile the code into anything, it just loads it, it keeps the code's representation in memory and then that is possible to be used. It would have to load and "compile" again every time it is run or save using serialization. – Preza8 Jun 11 '15 at 22:31
-
1Unless your script is valid F# you would have to compile it to IL if you want it to run at native F# speed in general. – t3dodson Jun 11 '15 at 22:49
-
F# IS a scripting language. https://msdn.microsoft.com/en-us/library/vstudio/dd233175(v=vs.120).aspx "Scripts use the file extension .fsx or .fsscript. Instead of compiling source code and then later running the compiled assembly, you can just run fsi.exe and specify the filename of the script of F# source code, and F# interactive reads the code and executes it in real time." – Petr Jun 11 '15 at 22:55
-
I meant writing in a langauge that I would create myself and then parse it myself and interpret it using F#. – Preza8 Jun 11 '15 at 22:59
-
@Preza8 again interpreted languages do not run at full speed compared to their compiled cousins. You have to compile the script to IL to achieve the same performance. – t3dodson Jun 11 '15 at 23:01
-
I'm probably misusing a term or 2. I'm gonna give up on this question. – Preza8 Jun 11 '15 at 23:05
1 Answers
I really like F# but I feel like it's not succinct and short enough. I want to go further. I do have an idea of how I'd like to improve it but I have no experience in making compilers so I thought I'd make it a scripting language.
F# supports scripting scenarios via F# Interactive, so I'd recommend considering an internal DSL first, or suggesting features on the F# Language UserVoice page.
Then I realized that I could make it a scripting language and interpret it using F# but still get pretty much 100% performance thanks to F# having the inline option. Am I right?
Depending on the scenario, interpreted code may be fast enough, for example if 99% of your application's time is spent waiting on network, database or graphics rendering, the overall cost of interpreting the code may be negligible. This is less true for compute based operations. F#'s inline functions can help with performance tuning but are unlikely to provide a global panacea.
Is it really possible to make a script interpreter in F#
As a starting point, it is possible to write an interpreter for vanilla F# code. You could for example use F#'s quotation mechanism to get an abstract syntax tree (AST) for a code fragment or entire module and then evaluate it. Here's a small F# snippet that evaluates a small subset of F# code quotations: http://fssnip.net/h1
Alternatively you could design your own language from scratch...
Is it really possible to make a script interpreter in F# that would go through my script and turn it into lots of functors and stuff and so get really good performance?
Yes, you could design your own scripting language, defining an AST using the F# type system, then writing a parser that transforms script code into the AST representation, and finally interpreting the AST.
Parser
There are a number of options for parsing including:
- active patterns & regex, for example evaluating cells in a spreadsheet
- FsLex & FsYacc, for example to parse SQL
- FParsec, a parser combinator library, for example to parse Small Basic
I'd recommend starting with FParsec, it's got a good tutorial, plenty of samples and gives basic error messages for free based on your code.
Small Examples
Here's a few simple example interpreters using FParsec to get you started:
- Turtle - http://fssnip.net/nM
- Minimal Logo language - http://fssnip.net/nN
- Small Basic - http://fssnip.net/le
Fun Basic
A while back I wrote my own simple programming language with F#, based on Microsoft's Small Basic with interesting extensions like support for tuples and pattern matching. It's called Fun Basic, has an IDE with code completion and is available free on the Windows Store. The Windows Store version is interpreted (due to restrictions on emitting code) and the performance is adequate. There is also a compiler version for the desktop which runs on Windows, Mac and Linux.
Is it really possible to make a script interpreter in F#
So I guess, the answer is YES, if you'd like to learn more there's a free recording of a talk I did at NDC London last year on how to Write Your Own Compiler in 24 Hours
I'd also recommend picking up Peter Sestoft's Programming Language Concepts book which has a chapter on building your own functional language.

- 6,513
- 25
- 40