yavascript.org

What is is?

yavascript (YS) is a development system designed to ease the writing and testing of JavaScript, and other ECMAScript based languages, through the provision of simple and streamlined tools. Using it makes it easier to write modular, reusable and dynamic code, introducing constructs for C-like coding.

Code developed in this manner is instantly testable, 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. Conditional compilation (though '#if' and other constructs) can be used to ensure code can be written once generically and deployed in multiple situations with minimal overhead for generic coding. 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.

During development occsaions may arise where you want to write a piece of code to be used in different ways, or have different values, in different projects or you use the same piece of code over and over again. In other languages these are simple to do, but in JavaScript require either rewriting whole files or using excessive variables and control structures.

How do I use it?

development

Regular JS development involves writing and testing code, often updating instantly in your browser, and frequently with a minification stage on completion. It is exactly the same with YS, any valid JS code is valid YS code, it can be used with any existing library/framework you happen to use and code can be developed entirely without using the additional extensions like any regular code.

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 existing to be 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, omitting code written for flaxibility but not required in this project. 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('$: "'+$+'"')
    					

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 Original project