summary refs log tree commit diff
path: root/src/readchar.c
blob: 51e178453f2a04347da3a6f73f8f78d46d684145 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "3cl.h"
#include "readchar.h"

#include <stdbool.h>
#include <string.h>

#include "utils.h"


static const char *const space    = " \n\t";
static const char *const brackets = "{[(?;)]}";
static const char *const instr    = "^+-*~%=!$&<>@#:";
static const char *const alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

char ccl_readchar(struct CCL *ccl, struct CCLFrame *frame, enum CCLRCFlags flags)
{
    bool iscomment;

    char chr;
    for (;(chr = ccl->code[frame->ep]) != '\0'; ++frame->ep)
    {
        if (iscomment && chr == '\n')
            iscomment = false;
        if (iscomment)
            continue;
        if (chr == '/')
            iscomment = true;

        if (strchr(space, chr))
            continue;

        if (strchr(brackets, chr))
            if (!(flags & CCL_RC_BRACK) && flags & CCL_RC_DIE)
                goto invalid;
            else
                goto ok;
        else if (strchr(instr, chr))
            if (!(flags & CCL_RC_IS) && flags & CCL_RC_DIE)
                goto invalid;
            else
                goto ok;
        else if (strchr(alphabet, chr))
            if (!(flags & CCL_RC_ALP) && flags & CCL_RC_DIE)
                goto invalid;
            else
                goto ok;
        else if (chr == '_')
            if (!(flags & CCL_RC_US) && flags & CCL_RC_DIE)
                goto invalid;
            else
                goto ok;
        else
            goto invalid;
    }
ok:
    return chr;
invalid:
    die(1, "Invalid symbol at %d", frame->ep);
}