geordi - C++ eval bot

Geordi

Table of Contents

  1. Introduction
  2. Using geordi
    1. Request syntax
    2. Flags
    3. Operational notes
    4. Prelude
    5. Rules
    6. FAQ
    7. Minimizing clutter
    8. Edit commands
    9. Example snippets
  3. Setting up a geordi
    1. Download
    2. Install
    3. Run
    4. NickServ identification
    5. Nickless requests
    6. Auto-reconnect
    7. Connecting to multiple networks
    8. Censoring phrases

1. Introduction

Geordi is an IRC bot program that compiles and runs C++ code snippets. It is intended to be used as a demonstration tool when teaching or discussing C++ on IRC.

Geordi is public domain, free software.


2. Using geordi


2.1 Request syntax

Geordi has 3 main request syntaxes, demonstrated by example below.

  1. Printing things

    To print things, stream things to geordi as if he were std::cout:

    <Eelis> geordi << 3 * 2 << " foo " << (9 > 4)
    <geordi> 6 foo true

    To accommodate the use of nick completion, "geordi" may be followed by a colon or comma.

    The code may be followed by a semicolon followed by more code, which is then placed before the definition of main (where the printing statement is put):

    <Eelis> geordi << f(3); int f(int x) { return x; }
    <geordi> 3
  2. Statement blocks

    To execute a block of statements, say for example:

    <Eelis> geordi { string s = "foo"; cout << s; }
    <geordi> foo

    Here, too, "geordi" may be followed by a colon or comma.

    The closing brace may be followed by more code, which is then placed before the definition of main (where the statement block is put):

    <Eelis> geordi { cout << f(3); } int f(int x) { return x; }
    <geordi> 3
  3. Programs

    To execute programs, say for example:

    <Eelis> geordi: void f(int x) { cout << x; } int main() { f(3); }
    <geordi> 3

    Here, a colon or comma after "geordi" is required if the next non-space character is a letter, an asterisk, a slash, or a single quote.

Line breaks

Whereas in ordinary C++ backslashes (outside of character/string literals) splice physical source lines to form a logical source line, in geordi snippets this behavior is inverted; backslashes split the physical source line into logical source lines.

Translation unit breaks

Two consecutive newlines (i.e. \\) split the snippet into multiple translation units.


2.2 Flags

In all three syntaxes described in the previous section, flags may be passed before any code. For the first two syntaxes, that means before << and {, respectively.

-c / --compile-only

Only compile; do not assemble, link, or run.

<Eelis> geordi -c void f (string & x) { reverse(x.begin(), x.end()); }
<geordi> Success

This better expresses your intent in demonstrations and produces a faster and more appropriate response from geordi. With -c, a main function is optional.

-w / --no-warn

Suppress warnings.

--no-using-std

Omits the otherwise implicitly added "using namespace std;".

-h / --help

Shows URL for this manual.

-v / --version

Shows the compiler version.

--make-type <type description>

Shows the type described in C++ syntax.

<Eelis> geordi --make-type pointer to function returning a reference to an array of three pointers to functions taking integers and returning doubles
<geordi> double(*(&(*)())[3])(int)

--precedence <expression>

Shows the given expression with added parentheses to illustrate precedence and associativity of operators.

<Eelis> geordi --precedence a + b - c ? d : e+++f
<geordi> ((a + b) - c) ? d : ((e++) + f)

--resume / -r

Adds the previous snippet's code, except for its main function (if any).

<Eelis> geordi << f(); int f() { return 3; }
<geordi> 3
<Eelis> geordi -r << f() + g(); double g() { return 1.2; }
<geordi> 4.2
<Eelis> geordi, show
<geordi> << f() + g(); int f() { return 3; } double g() { return 1.2; }

--clang

Use Clang instead of GCC.

--1998 / --2003 / --2011 / --2014

Use version of the C++ standard published in the specified year.

If none is specified, geordi will encourage the compiler to approximate what the next C++ standard might look like.


2.3 Operational notes


2.4 Prelude

The implicitly included prelude includes all standard library headers, some namespace usings and aliases, and assorted miscellaneous utilities.

Descriptions of a few selected utilities follow.

2.4.1 Range and tuple printing

Ranges (such as containers) and tuples have been made streamable:

   <Eelis> geordi { vector<int> v { 3, 5, 9, 4, 1 }; cout << v; }
   <geordi> [3, 5, 9, 4, 1]

See more_ostreaming.hpp for details.

2.4.2 Displaying types

Streaming TYPE<T> prints the type T.

TYPE(e) yields a string representation in C++ syntax of the static type of the expression e.

TYPE_DESC works the same, but yields string representations in verbose English.

Examples:

   <Eelis> geordi << TYPE(&system)
   <geordi> int (*)(const char*)
   <Eelis> geordi << TYPE_DESC< int(&(*)())[5] >
   <geordi> pointer to a function taking no arguments and
     returning a reference to an array of 5 integers

See type_strings.hpp for details.

2.4.3 Tracked objects

tracked::B is a simple class whose constructors, destructor, assignment operator, etc. print a short message when called. Objects of type tracked::B are also kept track of in a central registry, and leaks are reported if the program exits while tracked::B objects are still around. For example:

   <Eelis> geordi { using tracked::B; B x, * y = new B(x);
     swap(x, *y); y->f(); }
   <geordi> B0* new(B) B1*(B0) B0=>B2* B1=>B0 B2=>B1 B2~
     B1.f() B0~ leaked: B1. Aborted

See tracked.hpp for a full list of tracked operations and additional tracked classes.

2.4.4 Comma-delimited output

Geordi overloads the comma-operator to make it easy to print comma-delimited values:

   <Eelis> geordi << 3, "oi", sin(3.9)
   <geordi> 3, oi, -0.687766

2.4.5 Base-2 (binary) integer output.

Geordi adds bin to the std::hex/std::oct/std::dec family of I/O manipulators, with the semantics you would expect:

   <Eelis> geordi << showbase << bin << 38
   <geordi> 0b100110

See bin_iomanip.hpp for details.

2.4.6 Labeled value display

   <Eelis> geordi { int x = 45, y = x * x; cout << SHOW(x), SHOW(y),
     SHOW(y - x); }
   <geordi> x = 45, y = 2025, y - x = 1980

2.4.7 [Begin, end)-range shorthand

The RANGE macro expands RANGE(r) to (boost::begin(r)), (boost::end(r)) , which saves some typing for common cases.

   <Eelis> geordi { int a [40]; fill(RANGE(a), 6); cout << a[31]; }
   <geordi> 6

(See Boost.Range.)

2.4.8 BARK

BARK prints the name and signature of the function in which it appears:

   <Eelis> geordi { f(3.2); f('x'); } void f(int) { BARK; } void f(double) { BARK; }
   <geordi> f(double) f(int)

2.5 Rules

  1. Don't spam (i)

    Only pass geordi snippets when you're trying to demonstrate something to somebody (except in #geordi, where you are free to experiment as much as you like).

  2. Don't spam (ii)

    If you make a mistake in your snippet when trying to demonstrate something, give yourself at most one more attempt to get it right. If you fail to get your snippet right in your second attempt, then /join #geordi, work out your snippet there until it works, and then show it in the other channel.


2.6 FAQ

  1. How can geordi safely allow execution of arbitrary code?

    Geordi uses Docker, seccomp, and ordinary resource limits and permissions

  2. What language is geordi written in?

    Haskell.

  3. Can you make your Freenode geordi join channel #foo?

    First, you can always run your own geordi instance, see Download.

    That said, if #foo is a serious programming channel on Freenode that is not just a place to hang out, /join #geordi and talk to Eelis.

  4. Where can I play around with geordi?

    /join #geordi. Please, please, do not spam other channels!

  5. Is geordi suitable for small benchmarks?

    NO! Geordi uses various costly compiler flags and runtime aids to help diagnose ill-behaved code, making any kind of performance measure meaningless.

  6. I have another question/suggestion.

    On Freenode, /join #geordi and/or /msg Eelis. Alternatively, mail to <name-of-this-software>@contacts.eelis.net .


2.7 Minimizing clutter

Non-syntax-highlighted code with multiple statements all on a single line quickly becomes hard to read. Here are some tips to shave a few tokens off your snippets:

  1. Use the shorthand syntaxes whenever possible. When you must use the "geordi: ..." syntax, don't list main's parameters if you don't need them. Finally, return 0; is implicit for main.

  2. Use the prelude utilities when appropriate.
  3. Don't use std:: qualification; namespace std has been using'd. Yes, this is normally considered poor style, but our unique spatial constraints justify its use for geordi.

  4. Don't bother with endl or flush; geordi only outputs the first line anyway, and cout is flushed automatically at the end of the program.

  5. Use struct instead of class to avoid having to type public: for bases and members.


2.8 Edit commands

Geordi provides a set of editing commands that modify and then retry the last snippet.

2.8.1 Syntax:

    command = (insert | append | prepend | erase | replace | move | swap | use | make | "fix")*

    insert = ("insert" | "add") (... positions* | wrapping ("around" substrs*)*)
    append = "append" ... [positions*]
    prepend = "prepend" (options | ...) [positions*]
    erase = ("erase" | "remove" | "delete" | "cut" | "omit" | "kill") (substrs | options)*
    replace = "replace" (substrs* ("with" | "by") ... | options ("with" | "by") options)*
    move = "move" (substrs "to" position)*
    swap = "swap" (substr "and" substr)*

    use = "use" (options | ... [relative])*
    make = "make" declarator-id* type-description

    wrapping = ... "and" ... | "parentheses" | "parens" | "braces" | "curlies"
      | ("square" | "angle" | "curly" | "round") "brackets"
      | ("single" | "double") "quotes"

    position = limit | befaft (substr [relative])
    positions = in-clause | befaft substrs

    substr = "everything" | [ordinal] (named-entity | ...)
    substrs = (("everything" | rankeds (named-entity | ...)) [relative])*

    befaft = "before" | "after"
    limit = "begin" | "front" | "end" | "back"
    ordinal = "first" | ("second" | "third" | etc) ["last"] | "last"
    rankeds = "all" [("except" | "but") ordinal*] | ["each" | "every" | "any" | ordinal*]

    in-clause = "in" ([ordinal] (named-entity | declarator-id) [relative])*
    named-entity = ("declaration" | "body") "of" declarator-id
    relative = between | befaft [ranked] substr | in-clause
    between = "between" (bound "and" relative-bound | ordinal "and" ordinal ...)
    range = ([["everything"] "from"] bound | "everything") ("till" | "until") relative-bound
    bound = limit | [befaft] substr
    relative-bound = limit | [befaft] substr [relative]

Ellipsis denote simple verbatim strings without escaping, quoting, globbing, etc. The notation  x*  stands for  x ["and" x*] .

2.8.2 Examples:

<Johnny> geordi: string s = "foo"; cin << s; }
<geordi> error: expected constructor, destructor, or type conversion before '<<' token
<Eelis> geordi: prepend { and replace cin with cout
<geordi> foo
<Johnny> Ah, I see!

Fixing longer snippets this way is easier and more to the point than copy&paste-ing.

The last snippet can be edited and re-edited indefinitely. Use the "show" command to show the snippet in its current form:

<Johnny> So, what does the snippet look like with those changes?
<Eelis> geordi: show
<geordi> {string s = "foo"; cout << s;}
<Johnny> Ah, I see!

More examples:

geordi, wrap parentheses around everything and erase second semicolon
geordi, insert << flush before second last << and move int i; to before int j = 3 * i;
geordi, move everything after int main() to front and erase int main()

2.8.3 The "use" edit command

For each given string, the "use" edit command searches the previous snippet for the substring that most resembles the given string, and replaces the former with the latter. This can be used to succinctly fix typos or make small adjustments:

<Johnny> geordi: -c class X { void f() }; void main() { statc X x(); x->f(); }
<geordi> error: expected ';' before '}' token
<Eelis> geordi: use void f(); }
<geordi> error: '::main' must return 'int'
<Eelis> geordi: use int main
<geordi> error: 'statc' was not declared in this scope
<Eelis> geordi: use static
<geordi> error: 'static' specified invalid for function 'x' declared out of global scope
<Eelis> geordi: use X x;
<geordi> error: base operand of '->' has non-pointer type 'X'
<Eelis> geordi: use x.f
<geordi> error: 'void X::f()' is private
<Eelis> geordi: use struct
<geordi> Success
<Eelis> geordi: show
<geordi> -c struct X { void f(); }; int main() { static X x; x.f(); }

Alternatively, these edits could have been specified with a single command:

<Eelis> geordi: use void f(); } and int main and static and X x; and x.f and struct

Geordi and the other users in the channel cannot read your mind, and the heuristics they use to determine which substring to replace are only "best effort". To prevent confusion (among geordi and the other users alike), it is therefore recommended to keep the modifications-to-context ratio of patterns low by being liberal with context, by not trying to simultaneously make multiple changes in a short code fragment, and by not trying to replace partial tokens (the heuristics have a strong bias in favour of whole-token replacements).

2.8.4 The "fix" edit command

If geordi prints "(fix known)" at the end of an error, it means the compiler told geordi how the error might be fixed, and you can ask geordi to apply that fix:

<Eelis> geordi: struct A {} int main(){ cout << sizeof(A); }
<geordi> error: expected ';' after struct definition (fix known)
<Eelis> geordi: fix
<geordi> 1
<Eelis> geordi: show
<geordi> struct A {}; int main(){ cout << sizeof(A); }

2.8.5 The "make" edit command

The "make" edit command changes properties (including type) of declarations:

<Eelis> geordi -c struct X { int i; virtual void f(X * p) const = 0; };
<geordi> Success
<Eelis> geordi, make f nonconst inline nonvirtual and make i a static reference to long and make p a const pointer to const
<geordi> Success
<Eelis> geordi, show
<geordi> -c struct X { long static int & i; inline void f(const X *const p) ; };

Property descriptions generally follow the same syntax as with --make-type, with some additions.

"make" edit commands currently have a number of severe limitations:


2.9 Example snippets


3 Setting up a geordi


3.1 Download

Geordi can be downloaded from github.


3.2 Install

geordi runs as a Docker container.

To build the image from which the container will be instantiated, in the directory where Dockerfile is, say:

  sudo docker build -t geordi .

Since this involves building GCC and Clang from source, it may take some time.


3.3 Run

To run a local interactive geordi session, say:

  sudo docker run -it geordi

To run the IRC bot, edit irc-config and then say:

  sudo docker run -i geordi geordi-irc < irc-config

3.4 NickServ identification

To have the bot identify to NickServ, change the following line in irc-config:

  , nick_pass = Nothing

into:

  , nick_pass = Just "mypassword"

3.5 Nickless requests

To have the bot respond to nickless requests (e.g. "<< 3") in channels #foo, #bar, and #bas, use:

  , allow_nickless_requests_in = ["#foo", "#bar", "#bas"]

3.6 Auto-reconnect

Geordi does not auto-reconnect. For that, just use something like

  while true; do sudo docker run -i geordi geordi-irc < irc-config; sleep 120; done

3.7 Connecting to multiple networks

Make config files for the different networks, and run an instance for each network.


3.8 Censoring phrases

Some networks automatically kick or ban clients that utter certain phrases (like botnet commands). To prevent a geordi bot from uttering these, list regexes for them in irc-config. E.g.:

  , censor = ["some naughty phrase", "some wicked utterance"]