yavascript.org

What is is?

Frequently during code development occsaions arise where you want a single piece of code to behave differently depending on options or targets. This is simple to do in other languages, but not in JavaScript - it has no constants and no concept of files or modules. Regular JS development involves writing code using monolithic libraries, included through HTML "script" tags (entirely outside the JS language itself). JQuery claims to be small, but is still nearly 10000 lines because there's no simple way to split it up, and it is written for speed and not clarity.

YavaScript (YS) is a development system designed to ease the writing and testing of JavaScript, and other ECMAScript based languages*, by adding C-like pre-processor directives such as "#define" and "#include". Your code becomes more modular, so easier to manage; and simper to write through the use of conditional compilation and macros.

Code developed in this manner is instantly testable - just reload and go, all major browsers support YS through the use of an additional javascript library, and once development of a script is complete a compiler is used to compact all relevant code. yavascript was designed with minimum impact in mind, it is an extension to the language, not a replacement or paradigm alteration.

* YS is theoretically compatible with any flavour of ECMAScript, but has only been ported to web based systems for the time being.

Update: Since this project was first released, Google have released their compeating "closure" compiler for Javascript; and coffeescript and dart have also been released to fill similar gaps. At this point this code is basically abandonware, but is still available for posterity. The downloads are almost exactly as they were when I first submitted this at university, the only difference is that the writeup has been updated to .docx, and has had page and section numbers added.

How do I use it?

development

Any valid JS code is valid YS code, so the modify-reload-test cycle remains the same. The big differences are in how you are developing the code. Small libraries doing one or two simple things can be included as required, tested with no extra effort to import them in to a web page. Once a page is complete, the yavascript compiler can be run to combine all the source code files in to one - but importantly this stage is not required during development (or ever).

You can extend your code using the constructs explained below. During development these are interpreted by the JS library and turned into real JS code, with all required files together and with only the required chunks of code being parsed. This system can be a little slow, but it is designed for development, requiring no additional steps over regular development.

Deployment

Once your code is complete the (optimising) compiler takes it and minifies only the parts you want (including dead code elimination). For example, you could use prototype's 'Hash' object without the rest of the library, despite the fact that it's all there during development and you know nothing about separating it out. The compiler will output a single .js file with all the code you wrote and used, negating the need to load multiple files with excess code you're not even using.

Note that prototype is not optimised for YS, but the above descriptions are for examples.

Compiling

yavascript -f "input.ys" -o "output.js" -t web
			

Constructs

The syntax should be familiar to C coders, but for everyone else there are a few simple constructs:

#include

#include 'other_file.ys'
			

Loads another file for use in the current code. Uses XMLHttpRequest (or equivalent) so requires a server to load from. Imports all the code from the other file into the current location in the file, making it all instantly available.

#define

#define NAME 'value'
			

Defines a constant for use in code, instances of the name are replaced textually by the value, allowing code to be defined as well as constants, unlike variables.

#if

#if 10 < SOME_CONSTANT
	// Regular code
#endif
			

Includes the code within the block into the final javascript only if the constant condition is true. The condition may use values defined through #define.

#endif

Ends a block of code dependant on a '#if'.

#else

#if 10 < SOME_CONSTANT
	// Regular code
#else
	// Other code
#endif
			

Includes the code in the #else/#endif block only if the #if condition was false.

elseif

There is no #elseif, instead use #else and #if

#if 10 < SOME_CONSTANT
	// Regular code
#else
	#if 2 < SOME_CONSTANT
		// Other code
	#else
		// Final code
	#endif
#endif
			

#ifdef

#define SOME_CONSTANT (10) // Defined
#ifdef SOME_CONSTANT
	// Regular code
#endif
			

Used in the same way as #if, but is true when the given constant (from #define) is defined.

#ifndef

//#define SOME_CONSTANT (10) // Not defined
#ifndef SOME_CONSTANT
	// Regular code
#endif
			

Used in the same way as #if, but is true when the given constant (from #define) is NOT defined.

Examples

  1. Original

    #include 'prototype.js'
    
    #define LOOP_SIZE (10)
    
    #define LOOP for (var i = 0; i != LOOP_SIZE; ++i)
    
    LOOP
    {
    	alert('Value: ' + i);
    }
    					

    Compiled

    /* prototype code goes here */for(var i=0;i!=10;++i)alert('Value: '+i)
    					
  2. Original

    #define VALUE (10)
    
    #if VALUE < 10
    	alert("small");
    #else
    	#if VALUE < 20
    		alert("medium");
    	#else
    		alert("large");
    	#endif
    #endif
    					

    Compiled

    alert('medium')
    					
  3. Original

    #define USE_JQUERY (true)
    
    #ifdef USE_JQUERY
    	#include 'jquery.js'
    #else
    	#include 'prototype.js'
    #endif
    
    alert("$: \"" + $ + "\"");
    					

    Compiled

    /* jQuery code goes here */alert('$: "'+$+'"')
    					
  4. Larger Example

    THIS page demonstrates a small web app built using some yavascript libraries (the full source of which can be browsed here). Note that this takes a while to load due to the time required to parse and compile the Yavascript code to Javascript.

    A precompiled version is available HERE

Project origin

This project was originally developed for my third year project in university, but I felt it was sufficiently useful to be released for anyone to use. Technically the uni own the IP but it's released under the MPL so it's here for you to use as you see fit.

This is the code as I gave it in, there's still a lot of work to be done, so this should be considered 0.1beta. Also, the compiler is only built for windows so far.

Download

Development package with example code Full source code Submitted writeup

GIT

The original SVN hosting has now been moved over to GIT here:

GitHub