December 29th, 2009Colorize your Scheme! with the answer of “what is (String[ ] args)?”
In this post, I will introduce a new and simple syntax highlighting approach for functional languages. On the other hand, I will briefly mention the famous parameter of Java main method “(String[ ] args)”.
In functional languages like Lisp and Scheme (I don’t know if there is any other:) ), there are full of parenthesis and it is hard to follow. Thus I wrote a little piece of code in parallel to my understanding of the code written in these languages. It is kind of a Stack implementation in my mind, each opening parenthesis corresponds to a closing one and I think, most of the people understand the code in the way I did. So I tried to colorize the code in that manner, rather than colorizing the keywords (because almost every word is a keyword in these languages). Codes in the same depth have the same color. In the example below there are limited number of colors but it is enough to add new colors to the constant array at the beginning to see the colors in the code generated. Let’s see Java code for colorizing:
import java.io.File;
import java.util.Scanner;
public class ColorizeScheme{
public static String[] COLORS = {"#000099", "#990000", "#009900", "#000000"};
public static int colorCounter = 0;
public static void main(String[] args){
File file = new File(args[0]);
try{
Scanner scan = new Scanner(file);
String string ="";
String output ="";
while(scan.hasNextLine()){
//read input file, line by line.
string = scan.nextLine();
for(int i = 0; i < string.length(); i++){
/**
* If next char is an opening parenthesis add a font color code to the beginning
* if a closing parenthesis, close font code
* if a tab character change with html indentation
* if there exists more than one blank following each other, put (blank character)
* else simply put the character.
*/
if ((char)string.charAt(i) == '(' || (char)string.charAt(i) == '['){
output += "<font color=" + COLORS[colorCounter] + ">" + (char)string.charAt(i);
increaseCounter();
}
else if((char)string.charAt(i) == ')' || (char)string.charAt(i) == ']'){
output += (char)string.charAt(i) + "</font>";
decreaseCounter();
}
else if((char)string.charAt(i) == '\t'){
output += " ";
}
/** What am I doing in the following line after && ?
* I prevent an exception so called "ArrayIndexOutOfBoundsException"
* If i is representing the last char in the string
* I do not check i+1'st char which does not exist
*/
else if((char)string.charAt(i) == ' ' && ((i+1)!=string.length())?(char)string.charAt(i+1) == ' ':false){
output += " ";
}
else
output += (char)string.charAt(i);
}
//put html end of line <br>
output += "<br>\n";
}
//print the output
System.out.println(output);
}
catch(Exception e){
e.printStackTrace();
}
}
public static void decreaseCounter(){
if(colorCounter == 0)
colorCounter = COLORS.length-1;
else
colorCounter--;
}
public static void increaseCounter(){
if(colorCounter == COLORS.length-1)
colorCounter = 0;
else
colorCounter++;
}
}
Now, (String[ ] args). Why do we need to give parameters to main method while it is automatically called and how do we pass parameters?
Actually main method is called with the parameters given in the command line. If you have ever used command line on Linux or simply cmd.exe in Windows, you should have used arguments with the application called in the command line. I did not add a simple user interface to this code to talk about these arguments.
You can compile the file above using javac command (java compiler:)). Go to the folder where you put this file and write the following command:
javac ColorizeScheme.java
Now, you will see a file named ColorizeScheme.class in the same folder which is ready to be executed. In the following line, you see how to run this file:
java ColorizeScheme my-scheme.scheme
This will print the colorized HTML code based on the file named as my-scheme.scheme. How did we access my-scheme.scheme argument and become able to read the file named like that?. The answer is by taking first argument passed to the main method: args[0]. Number of arguments can be more than one, of course. But here in this example, only one argument is used.
Lastly, the output is printed to the command line but we want to see the result in the browser. You need to add the file name with a “>” in the front:
java ColorizeScheme my-scheme.scheme > my-scheme.htm
Now the output is written to the file “my-scheme.htm”. You can see the result by double clicking my-scheme.htm.
I tried my code with the scheme code in this page. And the result is this.