Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Extra libraries, Code snippets, applications etc. » U++ Esc Interpreter, Esc Macros and templates » bug in Esc manual page?
Re: bug in Esc manual page? [message #2247 is a reply to message #2112] Wed, 05 April 2006 10:45 Go to previous messageGo to previous message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
OK, reference/EscDev is the "introductory" Esc example. It does not cover "objects" and uses just trivial script interpretation (without extensions, as with usc)

I will yet have to find some nice simple examples to demonstrate those....

#include <Esc/Esc.h>

void SIC_Print(EscEscape& e)
{
	if(e[0].IsArray())
		Cout() << (String) e[0];
	else
	if(e[0].IsNumber())
		Cout() << e[0].GetNumber();
	else
	if(!e[0].IsVoid())
		e.ThrowError("invalid argument to 'Print'");
}

void SIC_Input(EscEscape& e)
{
	e = ReadStdIn();
}

void SIC_InputNumber(EscEscape& e)
{
	e = atof(ReadStdIn());
}

CONSOLE_APP_MAIN
{
	ArrayMap<String, EscValue> global;
	Escape(global, "Print(x)", SIC_Print);
	Escape(global, "Input()", SIC_Input);
	Escape(global, "InputNumber()", SIC_InputNumber);
	StdLib(global);
	try {
		Scan(global, LoadFile(GetDataFile("script.esc")));
		Execute(global, "main", INT_MAX);
	}
	catch(CParser::Error e) {
		Cout() << "ERROR: " << e << "\n";
	}
}


demonstration Esc script (script.esc):

getnumber()
{
	for(;;) {
		h = InputNumber();
		if(h > 0)
			return h;
		Print("Please enter a positive number!\n");
	}
}

main() {
	Print("What is your name?\n");
	name = Input();
	Print("Hi " + name + "!\n");
	Print("How tall are you? (in cm please!)\n");
	h = getnumber();
	Print("What is your weight? (in kg please!)\n");
	w = getnumber();
	bmi = w / (h * h / 10000);
	Print("Your body mass index is " + to_string(bmi) + "\n");
	Print(name + ", ");
	if(bmi >= 35)
		Print("you are morbidly fat!!!");
	else
	if(bmi >= 30)
		Print("you are fat!!!");
	else
	if(bmi >= 25)
		Print("you are overweight!");
	else
	if(bmi >= 18.5)
		Print("your weight is OK.");
	else
		Print("your are too slim!");
	Print("\nThanks for a nice chat!\n");
}


Mirek
 
Read Message BugQuestion.gif
Read Message
Read Message
Read Message
Read Message
Previous Topic: *.upt Empty lines produced in "Preview" - a bug?
Next Topic: REPL
Goto Forum:
  


Current Time: Sun Apr 28 21:41:18 CEST 2024

Total time taken to generate the page: 0.03391 seconds