示例#1
0
void let_command(simple_line_t * line, simple_variable_t variable_map[26])
{
	char * tokens[3] = {NULL, NULL, NULL};
	int result = 0;
	char * str = malloc(strlen(line->rest) + 1);
	strcpy(str, line->rest);
	
	/* The first token should be a variable name. */
	
	tokens[0] = strtok(str, " ");
	
	check_variable_name(tokens[0], line);
	
	/* The second token should literally be "=". */
	
	tokens[1] = strtok(NULL, " ");
	
	if (strcmp(tokens[1], "=") != 0)
	{
		simple_error(SYNTAX_ERROR, line, "invalid syntax for assignment statement; are you missing an = sign?");
	}
	
	/* The third "token" is the entire expression to evaluate. */
	
	tokens[2] = strchr(strchr(str, 0) + 1, 0) + 1;
	
	if (tokens[2] == NULL || strcmp(tokens[2], "\n") == 0 || strcmp(tokens[2], "\0") == 0)
	{
		simple_error(SYNTAX_ERROR, line, "it appears you're missing an expression to assign to the provided variable");
	}
	
	result = exp_evaluate(line, tokens[2], variable_map);
	simple_assign_variable(variable_map, tokens[0], result, line);
}
示例#2
0
void initialize_parser(void)
{
   int i;

   st.globalvars = table_create(TABLESIZE);
   st.classvars = table_create(TABLESIZE);
   st.localvars = table_create(TABLESIZE);
   st.missingvars = table_create(TABLESIZE);

   /* Add function names to table of global identifiers */
   for (i=0; i < numfuncs; i++)
   {
      id_type id = (id_type) SafeMalloc(sizeof(id_struct));
      id->name = Functions[i].name;
      id->type = I_FUNCTION;
      id->idnum = i;  /* For functions, idnum is just index in table, not a real id # */
      if (table_insert(st.globalvars, (void *) id, id_hash, id_compare) != 0)
         simple_error("Duplicate built-in function name %s", id->name);
   }

   /* Add builtin identifiers to appropriate symbol tables */
   for (i=0; i < numbuiltins; i++)
      switch (BuiltinIds[i].type)
      {
      case I_MISSING:
         if (table_insert(st.missingvars, (void *) &BuiltinIds[i], id_hash, id_compare) != 0)
            simple_error("Duplicate builtin identifier name %s", BuiltinIds[i].name);
         break;

      case I_PROPERTY:
         if (table_insert(st.globalvars, (void *) &BuiltinIds[i], id_hash, id_compare) != 0)
            simple_error("Duplicate builtin identifier name %s", BuiltinIds[i].name);
         break;

      default:
         simple_error("Bad type on builtin identifier %s", BuiltinIds[i].name);
         break;
      }

   st.maxid = IDBASE; /* Base for user-defined ids; builtins have lower #s */
   st.maxresources = RESOURCEBASE;
   st.maxlocals = -1; /* So that first local is numbered 0 */
   st.maxclassvars = -1; /* So that first class variable is numbered 0 */
   // XXX not needed because of self
#if 0
   st.maxproperties = -1; /* So that first property is numbered 0 */
#endif

   st.recompile_list = NULL;
   st.constants = NULL;
   st.num_strings = 0;
   st.strings = NULL;
   st.override_classvars = NULL;
}
示例#3
0
文件: stats.cpp 项目: nilsen3/hw1-1
int main() {
    cout<<"Enter three integers:\n";
    int inte1;
    int inte2;
    int inte3;
    cin>>inte1>>inte2>>inte3;
    if (! cin){
        simple_error("Could not read the numbers");
    }
    double avg;
    avg=(inte1+inte2+inte3)/3.0;
    int max=inte1;
    int min=inte1;
    if (inte2>max){
        max=inte2;
    }
    if (inte3>max) {
        max = inte3;
    }
    if (inte2<min) {
        min = inte2;
    }
    if (inte3<min) {
        min = inte3;
    }
    cout<<"min:  "<<min<<"\n";
    cout<<"max:  "<<max<<"\n";
    cout<<"avg:  "<<avg;
}
示例#4
0
int main()
try {
    cout <<"Think of a number between 1 and 128\n\n";
    int max = 128;
    int min = 1;
    int number = 64;
    for (int i=0; i<7; i++){
        cout << "Is the number you are thinking of greater than " << number << " (Yes or No)\n";
        string answer;
        cin >> answer;
        if (answer == no) {
            max = number;
            number = number - (number - (min - 1)) / 2.0;
        }
        else if (answer == yes) {
            min = number + 1;
            number = max - (max - (min - 1)) / 2.0;
        }

        else
            simple_error("Did not recognize answer");
    }
cout << "the number you are thinking of is " << min;
}
catch (runtime_error &e) {
    cerr << "runtime error: " << e.what() << '\n';
    return 1;
}
示例#5
0
int main() {

	cout << "Please type recipient name than press enter:\n";
	string first_name;
	cin >> first_name;

	cout << "Please type friend name than press enter:\n";
	string friend_name;
	cin >> friend_name;

	cout << "Please specify " << friend_name << "'s sex (type 'm' if mail or 'f' if female):\n";
	char friend_sex=0;
	cin >> friend_sex;

	cout << "Please enter recipient age:\n";
	int age = -1;
	cin >> age;
	if (age <= 0 or age > 110) {
		simple_error("Are you kidding?");
	}

	cout << "Dear " << first_name << ",\n";
	cout << "\n";
	cout << "Whazup?\n";
	cout << "Have you seen " << friend_name << " recently?\n";
	if (friend_sex == 'm') {
		cout << "If you see " << friend_name << ", tell him to call me.\n";
	}
	if (friend_sex == 'f') {
		cout << "If you see " << friend_name << ", tell her to call me.\n";
	}
}
示例#6
0
int main(){

  // definitions and declarations
  string first_name;
  string friend_name;
  char friend_sex = '0';
  int recip_age = 0;
  cout << "Enter the name of the person you want to write to: ";
  cin >> first_name;
  cout << "Can you type the name of one of your near and dear friends? ";
  cin >> friend_name;
  cout << "If you friend is female type 'f', if your friend is male type 'm'. ";
  cin >> friend_sex;
  cout << "How old is the person you are writing to (in years)? ";
  cin >> recip_age;
  if(recip_age <= 0 | recip_age >= 110)
    simple_error("You're kidding about the age! I am going to terminate this now!");

  // The letter template
  cout << "\n\nDear " << first_name << ",\n\n"
    << "\tHow are you doing? I am fine. I miss you tons. Can you believe how long it has been? I can hardly believe it. "
    << "Have you seen " << friend_name << " lately?";
  if(friend_sex == 'm')
    cout << " If you see " << friend_name << " please ask him to call me.";
  if(friend_sex == 'f')
    cout << " If you see " << friend_name << " please ask her to call me.";
  cout << " I hear you just had a birthday and you are " << recip_age << " years old!";
  if(recip_age > 0 & recip_age < 12)
    cout << " Next year you will be " << recip_age + 1 << '.';
  if(recip_age == 17)
    cout << " Next year you will be able to vote!";
  if(recip_age > 70 & recip_age < 110)
    cout << " I hope you are enjoying retirement.";
  cout << "\n\nYours sincerely,\n\n";
}
示例#7
0
文件: lab2.cpp 项目: hjtree0825/lab2
/* This function takes three inputs (an operation and two operands and returns a result.
 * The operation is in the form of a character, either +, -, *, or /, representing math
 * operations. The operands can be either integers or doubles, but the program will assume
 * doubles, which can process most integers. It will then print/return the result of the operation.
 */
double problem2() {
    char oper;
    double op_a, op_b, result;
    cout << "Please enter an operation for (a OPERATION b): ";
    cin >> oper;
    cout << "Please enter the first operand: ";
    cin >> op_a;
    cout << "Please enter the second operand: ";
    cin >> op_b;
    switch(oper) {
        case '+':
            result = op_a+op_b;
            break;
        case '-':
            result = op_a-op_b;
            break;
        case '*':
            result = op_a*op_b;
            break;
        case '/':
            result = op_a/op_b;
            break;
        default:
            simple_error("An invalid operation was entered!");
    }
    cout << "The result is " << result << endl;
    return result;
}
示例#8
0
int load_add_message(char *message_name, int message_id)
{
   id_type id = (id_type) SafeMalloc(sizeof(id_struct));
   message_handler_type m = (message_handler_type) SafeMalloc(sizeof(message_handler_struct));
   message_header_type h = (message_header_type) SafeMalloc(sizeof(message_header_struct));

   id->name = strdup(message_name);
   id->idnum = message_id;
   id->type = I_MESSAGE;
   id->ownernum = st.curclass;
   id->source = DBASE;

   h->message_id = id;
   h->params = NULL;

   m->header = h;
   m->locals = NULL;
   m->body = NULL;

   /* Add message to message list of current class */
   if (current_class == NULL)
      simple_error("Message appears outside of class in database file");
   else
      current_class->messages = list_add_item(current_class->messages, (void *) m);

   current_message = h;
   st.curmessage = message_id;
   /* OK if message already in table; just ignore return value */
   table_insert(st.globalvars, (void *) id, id_hash, id_compare);
   return True;
}
示例#9
0
int perform_comparison(simple_line_t * line, int a, int b, enum SIMPLE_CONDITIONAL conditional)
{
	switch (conditional)
	{
		case LESS_THAN_STRICT:
			return a < b;
		
		case LESS_THAN:
			return a <= b;
		
		case EQUALS:
			return a == b;
		
		case GREATER_THAN:
			return a >= b;
		
		case GREATER_THAN_STRICT:
			return a > b;
		
		default:
		case INVALID_CONDITIONAL:
			simple_error(MYSTERY, line, "somehow tried to evaluate an INVALID_CONDITIONAL comparison");
			return 0;
	}
}
示例#10
0
int main()
{
    cout<<"Please enter the name of the person you want to write to, followed by 'Enter':\n";
    string first_name;
    cin>> first_name;
    cout<< "Dear " << first_name << ",\n";
    cout<<"Miss you.\n";
    cout<<"Please enter the first name of your cat:\n";
    string cat_name;
    cin>> cat_name;
    cout<<"did you feed " << cat_name << " before you left this morning?\n";
    cout<<"Please input Y for 'Yes' or N for 'N'\n";
    char food = 0;
    cin>> food;
    if (food == 89)
        cout<<"Good, I won't feed him then!\n";
    else (food == 78);
        cout<<"Ok I'll feed him now then!\n";
    cout<<"How old is the cat again?\n";
    cout<<"Please enter cat's age in numbers:\n";
    int cat_age = 0;
    cin>> cat_age;
    if (cat_age<= 0 || cat_age > 100)
        simple_error("You're Kidding!");
    else
        cout<<"I thought he was " << ++cat_age << " !\n";
    cout<<"See you soon x x\n";
    return 0;
}
示例#11
0
文件: lab2.cpp 项目: atang618/lab2
// Takes an operation followed by two operands and outputs the result.
int operation_two() {
    string operation;
    double first;
    double second;
    cout << "\nPlease enter an operation and two operands: \n";
    cin >> operation >> first >> second;
    if (! cin) {
        simple_error("Invalid input");
    }
    else {
        if (operation == "+") {
            cout << first << " + " << second << " = " << first + second;
        }
        else if (operation == "-") {
            cout << first << " - " << second << " = " << first - second;
        }
        else if (operation == "*") {
            cout << first << " * " << second << " = " << first * second;
        }
        else if (operation == "/") {
            cout << first << " / " << second << " = " << first / second;
        }
    }
    return 0;
}
示例#12
0
/*
 * save_kodbase: Writes out the new database file based on class list
 *              passed in from parser.  Returns True iff successful.
 */
int save_kodbase()
{
   FILE *kodbase;
   int numexternals;
   list_type external_list;

   if ((kodbase = fopen(basefile, "wt")) == NULL)
   {
      simple_error("Unable to open database file %s", basefile);
      return False;
   }
  
   /* First, write out status line of # of identifiers & resources */
   fprintf(kodbase, "T %d %d\n", st.maxid, st.maxresources);

   /* Loop over all classes, and write them out */
   save_class_list(kodbase, st.classes);

   /* Write out unresolved externals */
   external_list = table_get_all(st.missingvars);
   numexternals = save_externals(kodbase, external_list);
   
   if (numexternals != 0)
      simple_warning("%d unresolved externals", numexternals);

   fclose(kodbase);
   return True;
}
示例#13
0
文件: lab2.cpp 项目: hjtree0825/lab2
/* This function prompts the user for an integer input of an age and repeats it back.
 * If the friend is under 12, it will be also say their next age.
 * If the friend is 17, it will say that they will be able to vote.
 * If the friend is over 70, it will compliment them on their life's work.
 */
void problem45() {
    int age;
    cout << "Please enter your friend's age: ";
    cin >> age;
    if (!cin) // check if something other than an age was entered.
        simple_error("An invalid age was entered.");
    if (age <= 0 || age >= 110)
        simple_error("you're kidding!");
    else
        cout << "I hear you just had a birthday and you are " << age << " years old." << endl;
    if (age < 12)
        cout << "Next year you will be " << (age+1) << "!" << endl;
    else if (age == 17)
        cout << "Next year you will be able to vote!" << endl;
    else if (age > 70)
        cout << "I hope you're enjoying retirement!" << endl;
}
示例#14
0
void database_error(const char *fmt, ...)
{
   va_list marker;

   fprintf(stderr, "%s(%d): ", basefile, kodbase_line);
   va_start(marker, fmt);
   simple_error(fmt, marker);
   va_end(marker);
}
示例#15
0
void goto_command(simple_file_t * file, simple_line_t * line, simple_variable_t variable_map[26])
{
	int goto_destination = -1;
	
	/* Use the is_number function from the loader module to check that a positive integer has been provided. */
	if (!is_number(line->rest))
	{
		simple_error(SYNTAX_ERROR, line, "the goto destination is not a positive integer");
	}
	
	goto_destination = find_line_index(file, atoi(line->rest));
	
	if (goto_destination < 0)
	{
		simple_error(SYNTAX_ERROR, line, "no line with the provided number is present in the file");
	}
	
	/* Now set our destination. */
	file->next_line = goto_destination;
}
示例#16
0
文件: lab2.cpp 项目: hjtree0825/lab2
/* This function takes two inputs (a name and a gender) and returns a string depending on gender.
 */
void problem3() {
    enum Gender {male, female, other, unknown};
    Gender friend_gender = unknown;
    char in_gender;
    string friend_name;

    cout << "Please enter your friend's name: ";
    cin >> friend_name;
    cout << "Please enter your friend's gender: ";
    cin >> in_gender;

    switch(in_gender) {
        case 'm':
            friend_gender = male;
            break;
        case 'f':
            friend_gender = female;
            break;
        case 'o':
            friend_gender = other;
            break;
        default:
            simple_error("You didnt enter a gender I know about!");
    }

    cout << "If you see " << friend_name;
    switch(friend_gender) {
        case male:
            cout << " please ask him to call me." << endl;
            break;
        case female:
            cout << " please ask her to call me." << endl;
            break;
        case other:
            cout << " please ask h'x to call me." << endl;
            break;
        default:
            simple_error("Something isn't right!");
    }
}
示例#17
0
int CompareArguments(void *arg1, void *arg2)
{
   arg_type a1 = (arg_type) arg1;
   arg_type a2 = (arg_type) arg2;

   if (a1->type != ARG_SETTING || a2->type != ARG_SETTING)
   {
      simple_error("Internal error--bad argument type in CompareArguments");
      return 0;
   }

   return (a1->value.setting_val->id->idnum < a2->value.setting_val->id->idnum);
}
示例#18
0
void database_error(const char *fmt, ...)
{
   va_list marker;

   printf("%s(%d): ", basefile, kodbase_line);
   va_start(marker, fmt);
   simple_error(fmt, marker);
   va_end(marker);
/*
   vprintf(fmt, marker);
   printf("\n");
*/
}
示例#19
0
char *GetStringFromResource(resource_type r, int j)
{
   switch (r->resource[j]->type)
   {
      case C_STRING:
         return r->resource[j]->value.stringval;
      case C_FNAME:
         return r->resource[j]->value.fnameval;
      default:
         simple_error("Unknown resource type (%d) encountered",
            r->resource[j]->type);
         return NULL;
   }
}
示例#20
0
int main()
{
  // Chapter 3 Drill

	cout << "Enter the name of the person you want to write to: ";
	string first_name = "";
	cin >> first_name;
	cout << "Dear " << first_name << ", \n";

	cout << "\tHow are you? I am fine. I miss you.\n";

	cout << "What is the name of your other friend? ";
	string friend_name = "";
	cin >> friend_name;
	cout << "Have you seen " << friend_name << " lately?\n";

	char friend_sex = 0;
	cout << "What's " << friend_name << "'s gender? (m for male, f for female) ";
	cin >> friend_sex;

	if (friend_sex == 'm')
		cout << "If you see " << friend_name << " please ask him to call me.\n";

	if (friend_sex == 'f')
		cout << "If you see " << friend_name << " please ask her to call me.\n";

	cout << "What is your age? ";
	int age = 0;
	cin >> age;

	if (age <= 0 || age >= 110)
		simple_error("You're kidding!\n");

	cout << "I hear you just had a birthday and you are " << age << " years old.\n";

	if (age < 12)
		cout << "Next year you will be " << age + 1 << ".\n";

	if (age == 17)
		cout << "Next year you will be able to vote.\n";

	if (age > 70)
		cout << "I hope you are enjoying retirement.\n";


	cout << "Yours sincerely, \n\nNyle\n";
  
  return 0;
}
示例#21
0
文件: lab2.cpp 项目: atang618/lab2
// Test an integer value to determine if it is odd or even.
int odd_even() {
    int test;
    cout << "Please enter an integer value: \n";
    cin >> test;
    if (! cin) {
        simple_error("Input must be an integer");
    }
    else {
        if (test %2 == 0) {
            cout << "The value of " <<test<<" is an even number.";
        }
        else {
            cout << "The value of " <<test<<" is an odd number.";
        }
    }
    return 0;
}
示例#22
0
int main()
{
  cout << "Enter the name of the person you want to write to: ";
  string first_name;
  cin >> first_name;

  cout << "Dear " << first_name << ",\n";
  cout << "\tThis is some random text blah blah.\n";

  cout << "Enter the name of another friend: ";
  string friend_name;
  cin >> friend_name;

  cout << "Have you seen " << friend_name << " lately?\n";

  char friend_sex = 0;
  cout << "Enter an 'm' if the friend is male or 'f' if the friend is female: ";
  cin >> friend_sex;

  if (friend_sex == 'm')
    cout << "If you see " << friend_name << " please ask him to call me.\n";
  if (friend_sex == 'f')
    cout << "If you see " << friend_name << " please ask her to call me.\n";

  int age = 0;
  cout << "Enter the age of " << first_name << ": ";
  cin >> age;

  cout << "I hear you just had a birthday and you are " << age << " years old.\n";

  if (age <= 0 || age >= 110)
    simple_error("you're kidding!");

  if (age > 0 && age < 12)
    cout << "Next year you will be " << age+1 << ".\n";

  if (age == 17)
    cout << "Next year you will be able to vote.\n";

  if (age > 70)
    cout << "I hope you enjoy retirement.\n";

  cout << "Yours sincerely.\n\n";

  return 0;
}
示例#23
0
int main() {
		
	vector<string> names;
	vector<int> scores;
	
	string name;
	int score;	
	
	// read input
	cout << "Please enter set of names and scores: i.e. John 17 Jane 1 etc\n";
	cout << "Enter 'NoName 0' to stop input\n";
	bool stop_input = false;
	while (!stop_input){
		// read input data
		cin >> name >> score;
		if (is_stop_input(name, score)) {
			stop_input = true;
		} else {
			// terminate if nonunique name was provided
			if (!is_name_unique(name, names)) {
				simple_error("Name entered is not unique: " + name + ". Please provide unique names");
			}
			// store input in containers
			names.push_back(name);
			scores.push_back(score);
			output_successful_input(name, score);
		}
	}
	// write pairs name - score
	output_names_and_scores(names, scores);
	
	// ask user for names and print scores for the names
	cout << "OK, now give me the name - and I'll show you the score for it\n";
	int index;
	while (cin >> name) {
		index = get_name_index(name, names);
		if (-1 != index) {
			cout << "(" << name << "," << scores[index] << ")\n";
		} else {
			cout << "Name " << name << " not found!\n";
		}
	}

}
示例#24
0
int main() {
    cout << "Enter the name of the person you want to write to: ";
    string first_name;
    cin >> first_name;
    cout << "\n" << "Enter their age: ";
    int age = 0;
    do {
        cin >> age;
        if (age < 0 || age > 110) {
            simple_error("you're kidding!");
        }
    } while (age < 0 || age > 110);
    cout << '\n' << "Enter a friend's name: ";
    string friend_name;
    cin >> friend_name;
    cout << "\n" << "Enter your friend's gender (m/f): ";
    char gender = 0;
    cin >> gender;

    cout << "Dear " << first_name << ",\n"
         << "How are you?  I am fine.  I miss you.\n"
         << "Have you seen " << friend_name << " lately?\n";
    string him_her = "";
    if (gender == 'm' || gender == 'M') {
        him_her = "him";
    } else if (gender == 'f' || gender == 'F') {
        him_her = "her";
    }
    cout << "If you see " << friend_name << " please ask " << him_her << " to call me.\n"
         << "I hear you just had a birthday and you are " << age << " years old.\n";
    if (age < 12) {
        cout << "Next year you'll be " << age + 1 << ".\n";
    }
    if (age == 17) {
        cout << "Next your you'll be able to vote.\n";
    }
    if (age > 70) {
        cout << "I hope you're enjoying retirement.\n";
    }
    cout << "Yours sincerely,\n\nDavid Wilkins\n";

    return 0;
}
示例#25
0
int load_add_property(char *prop_name, int property_id)
{
   id_type id = (id_type) SafeMalloc(sizeof(id_struct));
   property_type p = (property_type) SafeMalloc(sizeof(property_struct));

   id->name = strdup(prop_name);
   id->idnum = property_id;
   id->type = I_PROPERTY;
   id->ownernum = st.curclass;
   id->source = DBASE;

   /* Make fake property to insert into property list */
   p->id = id;
   p->rhs = NULL;

   /* Add property to property list of current class */
   if (current_class == NULL)
      simple_error("Property appears outside of class in database file");
   current_class->properties = list_add_item(current_class->properties, (void *) p);

   return True;
}
示例#26
0
int load_add_classvar(char *name, int classvar_id)
{
   id_type id = (id_type) SafeMalloc(sizeof(id_struct));
   classvar_type c = (classvar_type) SafeMalloc(sizeof(classvar_struct));

   id->name = strdup(name);
   id->idnum = classvar_id;
   id->type = I_CLASSVAR;
   id->ownernum = st.curclass;
   id->source = DBASE;

   /* Make fake classvar to insert into list */
   c->id = id;
   c->rhs = NULL;

   /* Add to list of current class */
   if (current_class == NULL)
      simple_error("Classvar appears outside of class in database file");
   current_class->classvars = list_add_item(current_class->classvars, (void *) c);

   return True;
}
示例#27
0
int load_add_parameter(char *parm_name, int parm_id)
{
   id_type id = (id_type) SafeMalloc(sizeof(id_struct));
   param_type p = (param_type) SafeMalloc(sizeof(param_struct));

   id->name = strdup(parm_name);
   id->idnum = parm_id;
   id->type = I_PARAMETER;
   id->ownernum = st.curmessage;
   id->source = DBASE;

   p->lhs = id;
   p->rhs = NULL;

   if (current_message == NULL)
      simple_error("Message appears outside of class in database file");
   else
      current_message->params = list_add_item(current_message->params, (void *) p);

   /* OK if parameter already in table; just ignore return value */
   table_insert(st.globalvars, (void *) id, id_hash, id_compare);
   return True;
}
示例#28
0
int main()
{
    cout << "Enter the name of the person you want to write to:\n";
    string first_name;
    cin >> first_name;
    string message = "Dear " + first_name + ",\n"
        + "   How are you? I am fine. I miss you.\n";

    cout << "Enter the name of a mutual friend:\n";
    string friend_name;
    cin >> friend_name;
    message += "Have you seen " + friend_name + " lately?\n";
    char friend_sex = 0;
    cout << "Enter 'm' if mutual friend is male, or 'f' for female:\n";
    cin >> friend_sex;
    if (friend_sex == 'm')
        message += "If you see " + friend_name + " please ask him to call me.\n";
    if (friend_sex == 'f')
        message += "If you see " + friend_name + " please ask her to call me.\n";

    cout << "Enter the age of the recipient:\n";
    int age;
    cin >> age;
    message += "I hear you just had a birthday and are " + to_string(age) + " years old.\n";
    if (age <= 0 || age >= 110)
        simple_error("You're kidding!\n");
    if (age < 12)
        message += "Next year you will be " + to_string(age + 1) + " years old.\n";
    if (age == 17)
        message += "Next year you will be able to vote!\n";
    if (age > 70)
        message += "I hope you are enjoying retirement.\n";
    message += "Yours truly,\n\n\nSomeone\n";    

    cout << message;
    return 0;
}
static dbus_bool_t
generate_special (DBusMessageDataIter   *iter,
                  DBusString            *data,
                  DBusValidity          *expected_validity)
{
  int item_seq;
  DBusMessage *message;
  int pos;
  dbus_int32_t v_INT32;

  _dbus_assert (_dbus_string_get_length (data) == 0);
  
  message = NULL;
  pos = -1;
  v_INT32 = 42;
  item_seq = iter_get_sequence (iter);

  if (item_seq == 0)
    {
      message = simple_method_call ();
      if (!dbus_message_append_args (message,
                                     DBUS_TYPE_INT32, &v_INT32,
                                     DBUS_TYPE_INT32, &v_INT32,
                                     DBUS_TYPE_INT32, &v_INT32,
                                     DBUS_TYPE_INVALID))
        _dbus_assert_not_reached ("oom");
                                     
      _dbus_header_get_field_raw (&message->header,
                                  DBUS_HEADER_FIELD_SIGNATURE,
                                  NULL, &pos);
      generate_from_message (data, expected_validity, message);
      
      /* set an invalid typecode */
      _dbus_string_set_byte (data, pos + 1, '$');

      *expected_validity = DBUS_INVALID_UNKNOWN_TYPECODE;
    }
  else if (item_seq == 1)
    {
      char long_sig[DBUS_MAXIMUM_TYPE_RECURSION_DEPTH+2];
      const char *v_STRING;
      int i;
      
      message = simple_method_call ();
      if (!dbus_message_append_args (message,
                                     DBUS_TYPE_INT32, &v_INT32,
                                     DBUS_TYPE_INT32, &v_INT32,
                                     DBUS_TYPE_INT32, &v_INT32,
                                     DBUS_TYPE_INVALID))
        _dbus_assert_not_reached ("oom");

      i = 0;
      while (i < (DBUS_MAXIMUM_TYPE_RECURSION_DEPTH + 1))
        {
          long_sig[i] = DBUS_TYPE_ARRAY;
          ++i;
        }
      long_sig[i] = DBUS_TYPE_INVALID;

      v_STRING = long_sig;
      if (!_dbus_header_set_field_basic (&message->header,
                                         DBUS_HEADER_FIELD_SIGNATURE,
                                         DBUS_TYPE_SIGNATURE,
                                         &v_STRING))
        _dbus_assert_not_reached ("oom");
      
      _dbus_header_get_field_raw (&message->header,
                                  DBUS_HEADER_FIELD_SIGNATURE,
                                  NULL, &pos);
      generate_from_message (data, expected_validity, message);
      
      *expected_validity = DBUS_INVALID_EXCEEDED_MAXIMUM_ARRAY_RECURSION;
    }
  else if (item_seq == 2)
    {
      char long_sig[DBUS_MAXIMUM_TYPE_RECURSION_DEPTH*2+4];
      const char *v_STRING;
      int i;
      
      message = simple_method_call ();
      if (!dbus_message_append_args (message,
                                     DBUS_TYPE_INT32, &v_INT32,
                                     DBUS_TYPE_INT32, &v_INT32,
                                     DBUS_TYPE_INT32, &v_INT32,
                                     DBUS_TYPE_INVALID))
        _dbus_assert_not_reached ("oom");

      i = 0;
      while (i <= (DBUS_MAXIMUM_TYPE_RECURSION_DEPTH + 1))
        {
          long_sig[i] = DBUS_STRUCT_BEGIN_CHAR;
          ++i;
        }

      long_sig[i] = DBUS_TYPE_INT32;
      ++i;

      while (i < (DBUS_MAXIMUM_TYPE_RECURSION_DEPTH*2 + 3))
        {
          long_sig[i] = DBUS_STRUCT_END_CHAR;
          ++i;
        }
      long_sig[i] = DBUS_TYPE_INVALID;
      
      v_STRING = long_sig;
      if (!_dbus_header_set_field_basic (&message->header,
                                         DBUS_HEADER_FIELD_SIGNATURE,
                                         DBUS_TYPE_SIGNATURE,
                                         &v_STRING))
        _dbus_assert_not_reached ("oom");
      
      _dbus_header_get_field_raw (&message->header,
                                  DBUS_HEADER_FIELD_SIGNATURE,
                                  NULL, &pos);
      generate_from_message (data, expected_validity, message);
      
      *expected_validity = DBUS_INVALID_EXCEEDED_MAXIMUM_STRUCT_RECURSION;
    }
  else if (item_seq == 3)
    {
      message = simple_method_call ();
      if (!dbus_message_append_args (message,
                                     DBUS_TYPE_INT32, &v_INT32,
                                     DBUS_TYPE_INT32, &v_INT32,
                                     DBUS_TYPE_INT32, &v_INT32,
                                     DBUS_TYPE_INVALID))
        _dbus_assert_not_reached ("oom");
                                     
      _dbus_header_get_field_raw (&message->header,
                                  DBUS_HEADER_FIELD_SIGNATURE,
                                  NULL, &pos);
      generate_from_message (data, expected_validity, message);
      
      _dbus_string_set_byte (data, pos + 1, DBUS_STRUCT_BEGIN_CHAR);
      
      *expected_validity = DBUS_INVALID_STRUCT_STARTED_BUT_NOT_ENDED;
    }
  else if (item_seq == 4)
    {
      message = simple_method_call ();
      if (!dbus_message_append_args (message,
                                     DBUS_TYPE_INT32, &v_INT32,
                                     DBUS_TYPE_INT32, &v_INT32,
                                     DBUS_TYPE_INT32, &v_INT32,
                                     DBUS_TYPE_INVALID))
        _dbus_assert_not_reached ("oom");
                                     
      _dbus_header_get_field_raw (&message->header,
                                  DBUS_HEADER_FIELD_SIGNATURE,
                                  NULL, &pos);
      generate_from_message (data, expected_validity, message);
      
      _dbus_string_set_byte (data, pos + 1, DBUS_STRUCT_END_CHAR);
      
      *expected_validity = DBUS_INVALID_STRUCT_ENDED_BUT_NOT_STARTED;
    }
  else if (item_seq == 5)
    {
      message = simple_method_call ();
      if (!dbus_message_append_args (message,
                                     DBUS_TYPE_INT32, &v_INT32,
                                     DBUS_TYPE_INT32, &v_INT32,
                                     DBUS_TYPE_INT32, &v_INT32,
                                     DBUS_TYPE_INVALID))
        _dbus_assert_not_reached ("oom");
                                     
      _dbus_header_get_field_raw (&message->header,
                                  DBUS_HEADER_FIELD_SIGNATURE,
                                  NULL, &pos);
      generate_from_message (data, expected_validity, message);
      
      _dbus_string_set_byte (data, pos + 1, DBUS_STRUCT_BEGIN_CHAR);
      _dbus_string_set_byte (data, pos + 2, DBUS_STRUCT_END_CHAR);
      
      *expected_validity = DBUS_INVALID_STRUCT_HAS_NO_FIELDS;
    }
  else if (item_seq == 6)
    {
      message = simple_method_call ();
      generate_from_message (data, expected_validity, message);
      
      _dbus_string_set_byte (data, TYPE_OFFSET, DBUS_MESSAGE_TYPE_INVALID);
      
      *expected_validity = DBUS_INVALID_BAD_MESSAGE_TYPE;
    }
  else if (item_seq == 7)
    {
      /* Messages of unknown type are considered valid */
      message = simple_method_call ();
      generate_from_message (data, expected_validity, message);
      
      _dbus_string_set_byte (data, TYPE_OFFSET, 100);
      
      *expected_validity = DBUS_VALID;
    }
  else if (item_seq == 8)
    {
      char byte_order;

      message = simple_method_call ();
      byte_order = _dbus_header_get_byte_order (&message->header);
      generate_from_message (data, expected_validity, message);
      
      _dbus_marshal_set_uint32 (data, BODY_LENGTH_OFFSET,
                                DBUS_MAXIMUM_MESSAGE_LENGTH / 2 + 4,
                                byte_order);
      _dbus_marshal_set_uint32 (data, FIELDS_ARRAY_LENGTH_OFFSET,
                                DBUS_MAXIMUM_MESSAGE_LENGTH / 2 + 4,
                                byte_order);
      *expected_validity = DBUS_INVALID_MESSAGE_TOO_LONG;
    }
  else if (item_seq == 9)
    {
      const char *v_STRING = "not a valid bus name";
      message = simple_method_call ();

      if (!_dbus_header_set_field_basic (&message->header,
                                         DBUS_HEADER_FIELD_SENDER,
                                         DBUS_TYPE_STRING, &v_STRING))
        _dbus_assert_not_reached ("oom");
      
      generate_from_message (data, expected_validity, message);

      *expected_validity = DBUS_INVALID_BAD_SENDER;
    }
  else if (item_seq == 10)
    {
      message = simple_method_call ();

      if (!dbus_message_set_interface (message, DBUS_INTERFACE_LOCAL))
        _dbus_assert_not_reached ("oom");
      
      generate_from_message (data, expected_validity, message);

      *expected_validity = DBUS_INVALID_USES_LOCAL_INTERFACE;
    }
  else if (item_seq == 11)
    {
      message = simple_method_call ();

      if (!dbus_message_set_path (message, DBUS_PATH_LOCAL))
        _dbus_assert_not_reached ("oom");
      
      generate_from_message (data, expected_validity, message);

      *expected_validity = DBUS_INVALID_USES_LOCAL_PATH;
    }
  else if (item_seq == 12)
    {
      /* Method calls don't have to have interface */
      message = simple_method_call ();

      if (!dbus_message_set_interface (message, NULL))
        _dbus_assert_not_reached ("oom");
      
      generate_from_message (data, expected_validity, message);
      
      *expected_validity = DBUS_VALID;
    }
  else if (item_seq == 13)
    {
      /* Signals require an interface */
      message = simple_signal ();

      if (!dbus_message_set_interface (message, NULL))
        _dbus_assert_not_reached ("oom");
      
      generate_from_message (data, expected_validity, message);
      
      *expected_validity = DBUS_INVALID_MISSING_INTERFACE;
    }
  else if (item_seq == 14)
    {
      message = simple_method_return ();

      if (!_dbus_header_delete_field (&message->header, DBUS_HEADER_FIELD_REPLY_SERIAL))
        _dbus_assert_not_reached ("oom");
      
      generate_from_message (data, expected_validity, message);
      
      *expected_validity = DBUS_INVALID_MISSING_REPLY_SERIAL;
    }
  else if (item_seq == 15)
    {
      message = simple_error ();

      if (!dbus_message_set_error_name (message, NULL))
        _dbus_assert_not_reached ("oom");
      
      generate_from_message (data, expected_validity, message);
      
      *expected_validity = DBUS_INVALID_MISSING_ERROR_NAME;
    }
  else if (item_seq == 16)
    {
      char long_sig[DBUS_MAXIMUM_TYPE_RECURSION_DEPTH*4+10];
      const char *v_STRING;
      int i;
      int n_begins;
      
      message = simple_method_call ();
      if (!dbus_message_append_args (message,
                                     DBUS_TYPE_INT32, &v_INT32,
                                     DBUS_TYPE_INT32, &v_INT32,
                                     DBUS_TYPE_INT32, &v_INT32,
                                     DBUS_TYPE_INVALID))
        _dbus_assert_not_reached ("oom");

      i = 0;
      while (i <= (DBUS_MAXIMUM_TYPE_RECURSION_DEPTH*3 + 3))
        {
          long_sig[i] = DBUS_TYPE_ARRAY;
          ++i;
          long_sig[i] = DBUS_DICT_ENTRY_BEGIN_CHAR;
          ++i;
          long_sig[i] = DBUS_TYPE_INT32;
          ++i;
        }
      n_begins = i / 3;

      long_sig[i] = DBUS_TYPE_INT32;
      ++i;
      
      while (n_begins > 0)
        {
          long_sig[i] = DBUS_DICT_ENTRY_END_CHAR;
          ++i;
          n_begins -= 1;
        }
      long_sig[i] = DBUS_TYPE_INVALID;
      
      v_STRING = long_sig;
      if (!_dbus_header_set_field_basic (&message->header,
                                         DBUS_HEADER_FIELD_SIGNATURE,
                                         DBUS_TYPE_SIGNATURE,
                                         &v_STRING))
        _dbus_assert_not_reached ("oom");
      
      _dbus_header_get_field_raw (&message->header,
                                  DBUS_HEADER_FIELD_SIGNATURE,
                                  NULL, &pos);
      generate_from_message (data, expected_validity, message);
      
      *expected_validity = DBUS_INVALID_EXCEEDED_MAXIMUM_DICT_ENTRY_RECURSION;
    }
  else if (item_seq == 17)
    {
      message = simple_method_call ();
      if (!dbus_message_append_args (message,
                                     DBUS_TYPE_INT32, &v_INT32,
                                     DBUS_TYPE_INT32, &v_INT32,
                                     DBUS_TYPE_INT32, &v_INT32,
                                     DBUS_TYPE_INVALID))
        _dbus_assert_not_reached ("oom");
                                     
      _dbus_header_get_field_raw (&message->header,
                                  DBUS_HEADER_FIELD_SIGNATURE,
                                  NULL, &pos);
      generate_from_message (data, expected_validity, message);

      _dbus_string_set_byte (data, pos + 1, DBUS_TYPE_ARRAY);
      _dbus_string_set_byte (data, pos + 2, DBUS_DICT_ENTRY_BEGIN_CHAR);
      
      *expected_validity = DBUS_INVALID_DICT_ENTRY_STARTED_BUT_NOT_ENDED;
    }
  else if (item_seq == 18)
    {
      message = simple_method_call ();
      if (!dbus_message_append_args (message,
                                     DBUS_TYPE_INT32, &v_INT32,
                                     DBUS_TYPE_INT32, &v_INT32,
                                     DBUS_TYPE_INT32, &v_INT32,
                                     DBUS_TYPE_INVALID))
        _dbus_assert_not_reached ("oom");
                                     
      _dbus_header_get_field_raw (&message->header,
                                  DBUS_HEADER_FIELD_SIGNATURE,
                                  NULL, &pos);
      generate_from_message (data, expected_validity, message);
      
      _dbus_string_set_byte (data, pos + 1, DBUS_DICT_ENTRY_END_CHAR);
      
      *expected_validity = DBUS_INVALID_DICT_ENTRY_ENDED_BUT_NOT_STARTED;
    }
  else if (item_seq == 19)
    {
      message = simple_method_call ();
      if (!dbus_message_append_args (message,
                                     DBUS_TYPE_INT32, &v_INT32,
                                     DBUS_TYPE_INT32, &v_INT32,
                                     DBUS_TYPE_INT32, &v_INT32,
                                     DBUS_TYPE_INVALID))
        _dbus_assert_not_reached ("oom");
                                     
      _dbus_header_get_field_raw (&message->header,
                                  DBUS_HEADER_FIELD_SIGNATURE,
                                  NULL, &pos);
      generate_from_message (data, expected_validity, message);

      _dbus_string_set_byte (data, pos + 1, DBUS_TYPE_ARRAY);
      _dbus_string_set_byte (data, pos + 2, DBUS_DICT_ENTRY_BEGIN_CHAR);
      _dbus_string_set_byte (data, pos + 3, DBUS_DICT_ENTRY_END_CHAR);
      
      *expected_validity = DBUS_INVALID_DICT_ENTRY_HAS_NO_FIELDS;
    }
  else if (item_seq == 20)
    {
      /* 64 levels of nesting is OK */
      message = message_with_nesting_levels(64);

      generate_from_message (data, expected_validity, message);

      *expected_validity = DBUS_VALID;
    }
  else if (item_seq == 21)
    {
      /* 65 levels of nesting is not OK */
      message = message_with_nesting_levels(65);

      generate_from_message (data, expected_validity, message);

      *expected_validity = DBUS_INVALID_NESTED_TOO_DEEPLY;
    }
  else
    {
      return FALSE;
    }

  if (message)
    dbus_message_unref (message);

  iter_next (iter);
  return TRUE;
}
示例#30
0
/*
 * codegen_statement: Generate code for a single statement.
 *    numlocals should be # of local variables for message excluding temps.
 *   Returns highest # local variable used in code for statement.
 */
int codegen_statement(stmt_type s, int numlocals)
{
   opcode_type opcode;
   int our_maxtemp = numlocals; /* highest numbered temporary required for this statement alone */

   /* Save line # debugging information */
   if (debug_bof && s->lineno != 0)
   {
      DebugLine *d = (DebugLine *) SafeMalloc(sizeof(DebugLine));
      d->lineno = s->lineno;
      d->offset = FileCurPos(outfile);
      debug_lines = list_add_item(debug_lines, d);
   }
   
   memset(&opcode, 0, sizeof(opcode));  /* Set opcode to all zeros */
   switch (s->type)
   {
   case S_ASSIGN:
   {
      assign_stmt_type stmt = s->value.assign_stmt_val;

      /* Place result directly in lhs */
      our_maxtemp = flatten_expr(stmt->rhs, stmt->lhs, numlocals);
      break;
   }

   case S_CALL:
      our_maxtemp = codegen_call(s->value.call_stmt_val, NULL, numlocals);
      break;

   case S_PROP:
      opcode.command = RETURN;
      opcode.dest = PROPAGATE;
      OutputOpcode(outfile, opcode);
      break;

   case S_RETURN:
      our_maxtemp = codegen_return(s->value.return_stmt_val, numlocals);
      break;

   case S_IF:
      our_maxtemp = codegen_if(s->value.if_stmt_val, numlocals);
      break;

   case S_FOREACH:
      our_maxtemp = codegen_foreach(s->value.foreach_stmt_val, numlocals);
      break;

   case S_FOR:
      our_maxtemp = codegen_for(s->value.for_stmt_val, numlocals);
      break;

   case S_SWITCH:
      our_maxtemp = codegen_switch(s->value.switch_stmt_val, numlocals);
      break;

   case S_WHILE:
      our_maxtemp = codegen_while(s->value.while_stmt_val, numlocals);
      break;

   case S_DOWHILE:
      our_maxtemp = codegen_dowhile(s->value.while_stmt_val, numlocals);
      break;

   case S_BREAK:
      /* Goto end of loop */
      opcode.command = GOTO;
      opcode.source1 = 0;
      opcode.source2 = GOTO_UNCONDITIONAL;
      OutputOpcode(outfile, opcode);
      
      /* Add to list of gotos to be backpatched later, and leave space */
      current_loop->break_list = 
	 list_add_item(current_loop->break_list, (void *) FileCurPos(outfile));
      OutputInt(outfile, 0);
      break;

   case S_CONTINUE:
      /* Goto top of loop */
      opcode.command = GOTO;
      opcode.source1 = 0;
      opcode.source2 = GOTO_UNCONDITIONAL;
      OutputOpcode(outfile, opcode);

      /* In for loops, continue statements actually jump forward, but in while loops
       * they jump backward.  Save address of goto for backpatching; if we are
       * in a for loop, the offset written out below will be written over during
       * backpatching in codegen_foreach().
       */
      current_loop->for_continue_list = 
	 list_add_item(current_loop->for_continue_list, (void *) FileCurPos(outfile));

      OutputGotoOffset(outfile, FileCurPos(outfile), current_loop->toppos);
      break;

   default:
      simple_error("Unknown statement type (%d) encountered", s->type);
      break;
   }
   return our_maxtemp;
}