Closed kekyo closed 1 year ago
Would it be possible to manipulate also types with this approach?
@robkuz Sure.
We can manipulate all F#'s type declaration.
For example:
I don't think this is flexible enough for a general plugin, what I would want is to transform the AST and splice into type holes, or transform sections of annotated code entirely. Then again maybe I misunderstood the example, maybe a more through example would enlighten me.
@7sharp9 Hi, thanks reply. Do you need more example for understanding, or need a example for deeper? (Please you should say me if I mistake understand it :)
I'll dig overview a little. I think about what the translator good at:
I can tell it imagine very narrow situation for use. But I feel the translator will be a lot of application possibility. These examples below can fix by the translator. And maybe we make the middle ware for it.
It interface implementation are a lot of methods invented.
The translator can fix for general problems, but it's invalid way. I feel it uses:
This is macros of SyntaxTrees by plugins. It is not something we plan to add to F#, see #210 for example and all the discussions there
Fsc can manipulate AST at compile time (wanna approve)
Overview
I propose "the AST translator" is usable and easier metaprogramming at F# world.
It's handle manipulation for untyped AST at compile time, these AST nodes are: ParsedInput, SynExpr, SynPat... etc.
For example, a basic F# code fragment:
We will be able to compile by fsc with the AST translator. Uses sample "FunctionLoggingTranslator" translator (complete code, following link)
Made result same as:
I forked and implemented it from visualfsharp repo. See my forked repo 'ast-translator-test' branch.
I wanna approve it and finally merge main repo. Of course, there is preparation to correct the pointed out problem.
Description
The AST translator is full-customizable for untyped AST at compilation time. Therefore we can handle additional features with no runtime costs (compare to quotation expressions and monadic structures)
We'll receive better way:
We'll be able to use for:
Details
How to use this way
We can easy to use AST translators:
ITranslator<ICompilerConfig, ErrorLogger, ParsedInput>
and implements theTranslate
function.Translate ICompilerConfig -> ErrorLogger -> ParsedInput -> ParsedInput
--translator:<path>
argument.Overall architecture
The AST translator uses F# untyped AST node types at FSharp.Compiler.Private.
--translator:<file>.
Load these translator assemblies.TranslatorAttribute
attribute type applied at translator assembly.TranslatorAttribute.TargetType
.)Translate
function.ParsedInput
.Featuring middle ware
(The concept not evaluate any code fragments, but I'm clear for this idea)
We can use the AST translator now, but untyped ASTs manipulates difficulty for common users. I'm expecting to develop middle ware library by communities.
The middle ware is fully AST translator but it requires additional translation information from source code. To give an example, we wanna insert arbitrary enter-exit code fragment:
The (imaginary) middle ware AST translator find
InsertBeforeAttribute
andInsertAfterAttribute
annotated functions and insert debug output to use these functions. We can write it translator (exactly, but difficult) and it'll make results:The middle ware translator gets additional translation information via:
ICompilerConfig
interface gives information for the fsc commandline options.AST translator assembly loads way
The AST translator assembly will load by fsc's command line option
--translator:<file>.
ITaskItem[]
symboledTranslatorPath.
TranslatorPath
element for auto configuration. It means most users only append the nuget package into thier project and done!TODOs
ITranslator<...>
has capable it senario. We can change third type argParsedInput
to typed ASTs.FSharp.Compiler.Private.
FSharp.Compiler.Service
too.FSharp.Core
orFSharp.Compiler.Core
named like new interface assembly. Structure below:Background
First way
The way was beginning from runtime validation logger using quotation expression (backend by FSharp.Quatation.Compiler)
But it has a problem for very slow at first runtime execution (cause internal compilation process), we can't pay it cost for business.
Second way
We're starting the fscx project. It's first approach for AST manipulation mechanizm using FSharp.Compiler.Service.
It project mades better results for the purpose. But it had these problems:
Now
Finally, fscx project was suspended (for only business reason.) I wanna finish it and I was thinking about what's more appropriate in what form.
I reimplement from scratch in the F#'s repo direct without FCS.
Extra information
Estimated cost (XS, S, M, L, XL, XXL): M
Related suggestions:
Affidavit (please submit!)
Please tick this by placing a cross in the box:
Please tick all that apply: