Website Summary Files Commits Branches @commit @diff @stats
path: root / spasm / src / asm / lexdump.cpp
blob: b609124a46c5602abb7451f2654b3be7c55b52fa (plain) (blame)
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
#include <iomanip>
#include <iostream>

#include <cassert>

#include "lexer.hpp"
#include "token.hpp"

using namespace SpasmImpl::ASM::Lexer;

#define TOKENS    \
    TOK(push)     \
    TOK(pop)      \
    TOK(dup)      \
    TOK(print)    \
    TOK(read)     \
    TOK(plus)     \
    TOK(minus)    \
    TOK(multiply) \
    TOK(divide)   \
    TOK(modulus)  \
    TOK(gotrue)   \
    TOK(gofalse)  \
    TOK(go)       \
    TOK(call)     \
    TOK(ret)      \
    TOK(load)     \
    TOK(store)    \
    TOK(label)    \
    TOK(ident)    \
    TOK(integer)  \
    TOK(xinteger) \
    TOK(endinput)

class TokenDumper : public TokenStream
{
   public:
    TokenDumper() : end_reached(false) {}
    void push_token(const Token& token)
    {
        Token::Token_type ttype = token.type();
        std::cout.width(8);
        std::cout << token.lineno();
        std::cout << ": " << token_name[ttype];
        if (ttype == Token::Integer || ttype == Token::XInteger)
            std::cout << '|' << token.value_int() << '|';
        else if (ttype == Token::Ident)
            std::cout << '|' << token.value_str() << '|';
        std::cout << std::endl;
        if (token.type() == Token::EndInput)
            end_reached = true;
    }

    bool input_end() const { return end_reached; }

   private:
    bool end_reached;

    static const char* token_name[24];
};

const char* TokenDumper::token_name[24] = {"halt",
#define TOK(x) #x,
                                           TOKENS
#undef TOK
                                           "notused!!!"};

int main()
{
    Lexer lex(std::cin);
    TokenDumper td;
    Token token;

    lex.buffer_init();

    while (!td.input_end())
    {
        lex.read(1);
        lex.tokenize(td);
    }

    return 0;
}