Sunday, May 24, 2015

Cling C++11 REPL

I initially had to use VirtualBox to run Cling but after reinstalling XCode command line tools, Cling runs natively on my Mac.
~/dev/bin/cling-MacOSX-10.9-99de1fe5c0/bin $ ./cling  -Wc++11-extensions -std=c++11

****************** CLING ******************
* Type C++ code and press enter to run it *
*             Type .q to exit             *
*******************************************
[cling]$ #include <vector>
[cling]$ #include <string>
[cling]$ #include <iostream>
[cling]$ using namespace std;
[cling]$ cout << "Hello, World!" << endl; 
Hello, World!
[cling]$ int x = [](int a,int b) -> int { return a + b; }(2,4)
(int) 6
[cling]$ .rawInput
Using raw input
[cling]! int forTheWin() {
[cling]! ?   cout << "Hello, World";
[cling]! ?   return 0;
[cling]! ?   }
[cling]! .rawInput
Not using raw input
[cling]$ forTheWin()
Hello, World(int) 0
[cling]$ 
[cling]$ .rawInput
Using raw input
[cling]! #include <stdio.h>
[cling]! #include <time.h>
[cling]! int getBiggestTime() {
[cling]! ?   time_t biggest = 0x7FFFFFFF;
[cling]! ?   printf("biggest is %s\n", ctime(&biggest)); 
[cling]! ?   return 0;
[cling]! ?   }
[cling]! .rawInput
Not using raw input
[cling]$ getBiggestTime()
biggest is Mon Jan 18 21:14:07 2038
[cling]$ .rawInput
Using raw input
[cling]! ? #include <iostream>
[cling]! ? using std::cout;
[cling]! ? using std::numeric_limits;
[cling]! ? int printTypes() {
[cling]! ?   cout  << "The range for type short is from " << std::numeric_limits::min()   << " to " << std::numeric_limits::max();
[cling]! ?   cout  << "The range for type int is from " << std::numeric_limits::min()  << " to " << std::numeric_limits::max() << endl;
[cling]! ?   cout  << "The range for type long is from " << std::numeric_limits::min()  << " to " << std::numeric_limits::max() << endl;
[cling]! ?   cout  << "The range for type float is from " << std::numeric_limits::min()  << " to "  << std::numeric_limits::max() << endl; 
[cling]! ?   cout  << "The range for type double is from " << std::numeric_limits::min()  << " to " << std::numeric_limits::max() << endl;
[cling]! ?   cout  << "The range for type long double is from " << std::numeric_limits::min()   << " to " << std::numeric_limits::max() << endl;
[cling]! ?   return 0;
[cling]! ? }
[cling]! .rawInput
Not using raw input
[cling]$ printTypes()
The range for type short is from -32768 to 32767The range for type int is from -2147483648 to 2147483647
The range for type long is from -9223372036854775808 to 9223372036854775807
The range for type float is from 1.17549e-38 to 3.40282e+38
The range for type double is from 2.22507e-308 to 1.79769e+308
The range for type long double is from 3.3621e-4932 to 1.18973e+4932
(int) 0
[cling]$
[cling]$ .rawInput
Using raw input
[cling]! #include <iostream>
[cling]! enum Colors {
[cling]! ?   Red, White, Blue
[cling]! ?   } ColorNames;
[cling]! .rawInput
Not using raw input
[cling]$ Colors background = Red;
[cling]$ Colors foreground = Blue;
[cling]$ .rawInput
Using raw input
[cling]! #include <stdio.h>
[cling]! int printColor(Colors c) {
[cling]! ?   switch (c) {
[cling]! ?      case Red: 
[cling]! ?        printf("Red\n"); break;
[cling]! ?      case White: 
[cling]! ?        printf("White\n"); break;
[cling]! ?      case Blue: 
[cling]! ?        printf("Blue\n"); break;
[cling]! ?      default: 
[cling]! ?        printf("not red or white or blue\n"); break;
[cling]! ?      }
[cling]! ?   return 0;
[cling]! ?   }
[cling]! .rawInput
Not using raw input
[cling]$ printColor(Red)
Red
(int) 0
[cling]$ printColor(Blue)
Blue
(int) 0
[cling]$ #include <iostream>
[cling]$ std::cout << Red << std::endl;
0
[cling]$ std::cout << Blue << std::endl;
2
[cling]$