aboutsummaryrefslogtreecommitdiff
path: root/include/cps/Lexer.h
blob: 8984398c50c785b349259d2b0e8cb0a19c0c0870 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

#pragma once
#include <string>
#include <cstdint>

namespace cps {

enum Token {
    tok_eof = -1,

    tok_declare = -2,
    tok_integer_kw = -3,
    tok_input = -4,
    tok_output = -5,
    
    tok_if = -6,
    tok_then = -7,
    tok_else = -8,
    tok_endif = -9,
    
    tok_while = -10,
    tok_do = -11,
    tok_endwhile = -12,
    
    tok_repeat = -13,
    tok_until = -14,
    
    tok_for = -15,
    tok_to = -16,
    tok_step = -17,
    tok_next = -18,

    tok_array = -19,
    tok_of = -20,

    tok_real_kw = -21,
    tok_boolean_kw = -22,
    tok_true = -23,
    tok_false = -24,
    
    tok_and = -25,
    tok_or = -26,
    tok_not = -27,

    tok_string_kw = -30,
    tok_string_literal = -36,
    tok_char_kw = -38,
    tok_char_literal = -39,

    tok_identifier = -100,
    tok_number_int = -101,
    tok_number_real = -102,
    
    tok_assign = -200, // <-
    tok_eq = -201, // =
    tok_ne = -202, // <>
    tok_le = -203, // <=
    tok_ge = -204, // >=

    tok_div = -205,
    tok_mod = -206,

    tok_colon = -300, // :

    tok_function = -400, 
    tok_endfunction = -401, 
    tok_return = -402, 
    tok_returns = -403,
    tok_procedure = -404, 
    tok_endprocedure = -405, 
    tok_call = -406,
    tok_byref = -407,
    tok_byval = -408
};

class Lexer {
    int CurrentLine = 1;
    bool HadError = false;
public:
    std::string IdentifierStr;
    std::string StringVal;
    int64_t NumVal;
    double RealVal;
    char CharVal = '\0';

    int gettok();
    int getLine() const { return CurrentLine; }
    bool hadError() const { return HadError; }
};

}