How to create a program from the program's code rather than from it's filename - typescript-compiler-api

Using the typescript compiler api I can easily create a Program with
let program = ts.createProgram([file], {});
where file is the filename, but is there a way to do this with the actual code instead?

To do this, you will need to provide a custom ts.CompilerHost. On that interface you can provide a getSourceFile method that provides a source file with the text you want.
Doing this is a lot of work though. It might be faster to use ts-morph/bootstrap which will handle a lot of this for you.

Related

Defining new language based on C++

I would like to add a new language (called 'kiwi') into the Brackets code editor which is based on C++. It uses the exact same rules but has additional keywords.
I've already done the part of adding the additional keywords with separate syntax highlighting directly on the clike.js file but i don't really like directly modifying the def for C++
Can someone explain to me how I can achieve this? I don't really understand the difference between using def() and CodeMirror.defineMIME(). If this new language will take cpp/hpp input files, how will the editor switch from C++ -> kiwi?
Thanks in advance
Patching your local copy of the code, as you've done, might be perfectly fine for your needs. (And if you run from a Git copy of the source, it's easy to pull down updates without losing your local diffs).
If you want to do it a "cleaner" way, you can write a Brackets extension to define the new language - this way the change is easily shareable with others, and updating Brackets is even easier.
The way you'd do this roughly follows the Defining a new language docs:
Write an extension to package up your code that will define the new language (below)
The CM mode "clike" is already loaded, so you don't need to worry about that
Call CodeMirror.defineMIME() to set up a clike configuration with the right list of keywords - using a new mimetype name of your choosing. (Looking at the def() code in clike.js, I don't think the extra stuff it does is especially relevant for Brackets).
Call LanguageManager.defineLanguage() to tell Brackets about your new language mode (and what file extensions map to it, etc.). You should be able to mostly copy how the C++ mode is defined here - except with your new MIME name instead.
You should read CodeMirror documentation to find out about writing your modes.
But in short you can define your own type of clike language, by using CodeMirror.defineMIME().
You also asked what's the difference between def() and CodeMirror.defineMIME(). If you look at the code you can see that def() function is calling CodeMirror.defineMIME() at the end, so I believe it is just a more readable way of defining the type.
Also it seems that it's impossible to define more than one language on the same extension type (not 100% sure).

How to create a closure from String in Dart?

How to use dart-mirror API to create a anonymous closure dynamically?
Like as the interpreter, compile the code during run-time.
var funcstr='bool (String s){ return (s==null); }';
var func=parseStr(funcstr);
// func(s)-> s==null;
var r=func('false');
// r=false;
so, how to do with "parseStr"?
my project:
http://github.com/stevehsu77/surebet
At the moment there is no way to do this. Dart has no eval and no code generation at runtime.
But it is something Gilad Bracha (the language spec lead of Dart) wants to have (https://groups.google.com/a/dartlang.org/forum/#!topic/misc/6O4g7eEHgOU) at least for the development environment.
Also
We’d like to support more powerful reflective features in the future. These would include mirror builders, designed to allow programs to extend and modify themselves, and a mirror-based debugging API as well.
https://www.dartlang.org/articles/reflection-with-mirrors/
So it'll probably be supported some time in the future. But right now it's not possible.
As mentioned above, Dart does not have eval, however it is possible to load new source code in another isolate using spawnUri().
I am not sure if there are any examples of how to use this. Perhaps post a message on the dart discussion group.
Using isolates and spawnUri() is quite a different than using eval, so it may not be the right fit for your project.

How to call calabash xml from a Java program

I try to set up a web based application using spring and xslt. Since i always use xslt in a pipelining style, i would like to use calabash. Is there a possibility to call calabash from Java? I read thru the documentation on http://xmlcalabash.com but there is only a description how to use it from command line. I also tired to find some javadoc on githup but wasn't successful. Obviously, there is the Main class with the main() method and i could supply the command line parameters as a string array...
I wonder if there is a better way to do it.
I looked into this recently too. I took a pragmatic approach where I call Main.run(), and pass in a string array that I generate from a (File)Properties object. It doesn't allow passing in file inputs as streams or sources however, they must reside on the file-system.
Likely there are nicer ways. You could for instance look into http://expath.org/ . There should be sources of that project. The webapp modules (formerly known as servlex?) seems to provide XMLCalabash integration.
HTH!

Inject data in an swf at compile time

Is it possible to inject data, for example a collection of assets (video, images...), in an swf at compile time?
We have a flex application that needs to be able to export an swf at runtime that contains all the necessary data, because it needs to run as a standalone application (on- and offline).
The idea so far was to create a server side script that calls the flex compiler and feed it the data it needs. This approach seems to work fine using the [Embed] tag for single files, but it gets kind of messy when trying to inject collections of data that vary in length for each exported swf.
We could generate an mxml file with one embedded variable for each asset and include it at compile time, but that approach seems for from ideal.
We've looked into some actionscript bytecode libraries, but those do not seem to be fit for this.
Any suggestions or other approaches for this kind of problem?
Thx,
Bert
[Embed] is definitely the way to go. You can generate an AS file that has lots of embeds. While the generated code might be a bit ugly, you can generate a nicer api too. For example, if you want an array, generate code like this:
[Embed(...)]
private var img_0:Class;
[Embed(...)]
private var img_1:Class;
[Embed(...)]
private var img_2:Class;
public var images:Array = [img_0, img_1, img_2];
That way the only ugliness is in private variables only the code generator will see. The public variable is an array.
I'm not sure why you need to do that but i guess i'll try to generate the needed actionscript file from template or something like that and then call the flex compiler.
Hope this helps you...
try as3swfit is able to generate an swf file from an empty ByteArrayand afaik it's possible to insert embedded graphics there
Like Sam said, [Embed] could work, but what are you trying to load? My first reaction is that recompiling for every request would be a big drag on the server.

Parse a large JSON file in ActionScript 3

I need to parse a large trace file (up to 200-300 MB) in a Flex application. I started using JSON instead of XML hoping to avoid these problems, but it did not help much. When the file is bigger than 50MB, JSON decoder can't handle it (I am using the as3corelib).
I have doing some research and I found some options:
Try to split the file: I would really like to avoid this; I don't want to change the current format of the trace files and, in addition, it would be very uncomfortable to handle.
Use a database: I was thinking of writing the trace into a SQLite database and then reading from there, but that would force me to modify the program that creates the trace file.
From your experience, what do you think of these options? Are there better options?
The program that writes the trace file is in C++.
Using AMF will give you much smaller data sizes for transfer because it is a binary, not text format. That is the best option. But, you'll need some middleware to translate the C++ program's output into AMF data.
Check out James Ward's census application for more information about benchmarks when sharing data:
http://www.jamesward.com/census/
http://www.jamesward.com/2009/06/17/blazing-fast-data-transfer-in-flex/
Maybe you could parse the file into chunks, without splitting the file itself. That supposes some work on the as3 core lib Json parser, but it should be doable, I think.
I found this library which is a lot faster than the official one: https://github.com/mherkender/actionjson
I am using it now and works perfectly. It also has asynchronous decoder and encoder

Resources