344{
348
349 using strset_t = std::set<std::string>;
350
351 strset_t girl_names;
352 strset_t boy_names;
353
354 girl_names.insert("jane");
355 girl_names.insert("jill");
356
357 boy_names.insert("jack");
358 boy_names.insert("jim" );
359 boy_names.insert("john");
360
362
363 names_groups.
add_group(
"girls", girl_names);
364 names_groups.
add_group(
"boys", boy_names );
365
366 assert(names_groups.
contains(
"girls"));
367 assert(names_groups.
contains(
"boys" ));
368
369 groups_impl::create <T> group_create (names_groups);
370 groups_impl::add <T> group_add (names_groups);
372 groups_impl::size <T> group_size (names_groups);
373 groups_impl::get <T> group_get (names_groups);
374 groups_impl::erase <T> group_erase (names_groups);
375
377
378 symbol_table_t symbol_table;
379 expression_t expression;
380 parser_t parser;
381
382 symbol_table.add_function("create" , group_create );
383 symbol_table.add_function("add" , group_add );
384 symbol_table.add_function("contains", group_contains);
385 symbol_table.add_function("size" , group_size );
386 symbol_table.add_function("get" , group_get );
387 symbol_table.add_function("erase" , group_erase );
388 symbol_table.add_function("println" , println );
389
390 expression.register_symbol_table(symbol_table);
391
392 const std::string program =
393 " /* Load the groups from symbol table */ "
394 " var girls := create('girls'); "
395 " var boys := create('boys' ); "
396 " "
397 " if (not(girls)) "
398 " { "
399 " println('failed to create girls set!'); "
400 " return []; "
401 " }; "
402 " "
403 " if (not(boys)) "
404 " { "
405 " println('failed to create boys set!'); "
406 " return []; "
407 " }; "
408 " "
409 " var name := 'john'; "
410 " "
411 " if (contains(girls, name)) "
412 " { "
413 " println(name, ' in girls set'); "
414 " } "
415 " else "
416 " { "
417 " println(name, ' NOT in girls set'); "
418 " }; "
419 " "
420 " if (contains(boys, name)) "
421 " { "
422 " println(name, ' in boys set'); "
423 " } "
424 " else "
425 " { "
426 " println(name, ' NOT in boys set'); "
427 " }; "
428 " "
429 " /* Create a group from scratch */ "
430 " var pets := create('pets'); "
431 " "
432 " add(pets, 'buddy'); "
433 " add(pets, 'fluffy', 'snowy'); "
434 " add(pets, 'ziggy', 'monty', 'teddy'); "
435 " "
436 " if (contains(pets, name)) "
437 " { "
438 " println(name, ' in pets set'); "
439 " } "
440 " else "
441 " { "
442 " println(name, ' NOT in pets set'); "
443 " }; "
444 " "
445 " if (contains(pets, name, 'fluffy')) "
446 " { "
447 " println(name, ' or fluffy in pets set'); "
448 " } "
449 " else "
450 " { "
451 " println(name, ' or fluffy NOT in pets set'); "
452 " }; "
453 " "
454 " println('List of \\'pets\\':'); "
455 " "
456 " for (var i := 0; i < size(pets); i += 1) "
457 " { "
458 " println('[', i ,']: ', get(pets,i)); "
459 " }; "
460 " "
461 " if (erase(pets, 'fluffy', 'teddy') != 2) "
462 " { "
463 " println('Failed to erase fluffy and teddy'); "
464 " return []; "
465 " }; "
466 " "
467 " println('List of \\'pets\\':'); "
468 " "
469 " for (var i := 0; i < size(pets); i += 1) "
470 " { "
471 " println('[', i ,']: ', get(pets,i)); "
472 " }; "
473 " ";
474
475 if (!parser.compile(program, expression))
476 {
477 printf("Error: %s\tExpression: %s\n",
478 parser.error().c_str(),
479 program.c_str());
480 return;
481 }
482
483 expression.value();
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504}
bool contains(const std::string &group_name) const
bool add_group(const std::string &group_name, const std::set< T > &set)