repl.h 993 Bytes
Newer Older
1
2
3
4
5
6
7
8
/*-----------------------------------------------------------------------------
 *  Author: Nelso G. Jost (nelsojost@gmail.com)
 *  License: GPLv2
 *  Purpose: Read-Eval-Print-Loop functionality
 *---------------------------------------------------------------------------*/
#ifndef REPL_H
#define REPL_H

9
10
11
#define  HISTORY_MAX  10

class REPL {};
12
13
14
15

typedef struct
{
    const char *name;
16
    void (*func)(REPL *);
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    const char *prototype;
    const char *help;
} REPL_COMMAND;


class REPL
{
public:
    REPL(REPL_COMMAND command_map[]);

    String prompt=">";

    void run(void);
    void print_help(void);
    void log(String msg);
32
    String get_arg(int index);
33
34
35

private:
    REPL_COMMAND * _fp_map;
36
37
38
    String _command_buffer="";
    String _history[HISTORY_MAX];
    int _history_index=-1;
39
40
41
42
43
44

    void _backspace(void);
    void _clear_line(void);

    void _history_previous(void);
    void _history_next(void);
45
    void _history_save(void);
46
47
48
49
50

    int _execute_command(void);
};

#endif