aboutsummaryrefslogtreecommitdiff
path: root/asm/lexer.hpp
blob: 4c4889cfdecbaf7417f9e0bbdb8b5ff7b6eb89e4 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* Copyright (C) 2024 Aryadev Chavali

 * You may distribute and modify this code under the terms of the
 * GPLv2 license.  You should have received a copy of the GPLv2
 * license with this file.  If not, please write to:
 * aryadev@aryadevchavali.com.

 * Created: 2024-04-14
 * Author: Aryadev Chavali
 * Description: Lexer for assembly language
 */

#ifndef LEXER_HPP
#define LEXER_HPP

#include <ostream>
#include <string>
#include <tuple>
#include <vector>

enum class token_type_t
{
  PP_CONST,     // %const(<symbol>)...
  PP_USE,       // %use <string>
  PP_END,       // %end
  PP_REFERENCE, // $<symbol>
  GLOBAL,
  STAR,
  LITERAL_NUMBER,
  LITERAL_CHAR,
  LITERAL_STRING,
  NOOP,
  HALT,
  PUSH,
  POP,
  PUSH_REG,
  MOV,
  DUP,
  MALLOC,
  MALLOC_STACK,
  MSET,
  MSET_STACK,
  MGET,
  MGET_STACK,
  MDELETE,
  MSIZE,
  NOT,
  OR,
  AND,
  XOR,
  EQ,
  LT,
  LTE,
  GT,
  GTE,
  PLUS,
  SUB,
  MULT,
  PRINT,
  JUMP_ABS,
  JUMP_STACK,
  JUMP_IF,
  CALL,
  CALL_STACK,
  RET,
  SYMBOL,
};

const char *token_type_as_cstr(token_type_t type);

struct token_t
{
  token_type_t type;
  size_t column, line;
  std::string content;

  token_t();
  token_t(token_type_t, std::string, size_t col = 0, size_t line = 0);
};

std::ostream &operator<<(std::ostream &, token_t &);

enum class lerr_type_t
{
  OK = 0,
  INVALID_CHAR_LITERAL,
  INVALID_CHAR_LITERAL_ESCAPE_SEQUENCE,
  INVALID_STRING_LITERAL,
  INVALID_NUMBER_LITERAL,
  INVALID_PREPROCESSOR_DIRECTIVE,
  UNKNOWN_LEXEME,
};

struct lerr_t
{
  size_t col, line;
  lerr_type_t type;

  lerr_t(lerr_type_t type = lerr_type_t::OK, size_t col = 0, size_t line = 0);
};

std::ostream &operator<<(std::ostream &, lerr_t &);

lerr_t tokenise_buffer(std::string_view, std::vector<token_t *> &);

#endif