HomeForumResearchGuide
<< back to How To home

This program reads some input from the command line, then prints it back out.

We begin by creating a new file MyProgram.dn and opening it in a text editor. In this file we declare the component that this source file will describe, declaring the interfaces that the component provides and requires. All behaviour in Dana is described using interfaces.

component provides App requires io.Output out, io.Input in {

}

The App interface has one function called main. Any runnable component will implement App, indicating it has a main method that Dana can launch.

A provided interface function is implemented by writing a function of the same name but with the interface's type name, followed by a colon, preceding that function name. We'll write a simple implementation of the App:main function, which looks like this:

component provides App requires io.Output out, io.Input in {

int App:main(AppParam params[])
	{
	out.print("Enter some text: ")
	char input[] = in.readln()
	
	out.println("You typed '$input'")
	
	return 0
	}
}

This program asks the user for some input, reads that input, and then prints the input back out to the command line. We use the two required interfaces (io.Output and io.Input) to help with this, and both of them are given a variable name (out and in). When we do this, Dana will automatically instantiate this required interface as an object and allow that instance to be globally accessed within this component using the given variable name.

To run this program we first need to compile it. Dana's compiler is a program called dnc. Open a command-prompt in the directory containing your file MyProgram.dn and type:

dnc MyProgram.dn

And press enter. This will compile your component.

We run the program using Dana's interpreter, a program called dana. In the same command-prompt window, type:

dana MyProgram

And press enter. You should be prompted for some input and the program should then write the input back out to the command line. The full list of standard interfaces that you can use in requires directives can be viewed at the Dana API pages along with documentation of each one. In this example we used: