summaryrefslogtreecommitdiff
path: root/masm/lex.h
blob: 8da6558403f6eb1640461197a2474e329be6ffc1 (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
107
108
109
110
111
112
113
114
115
116
/* Copyright (c) 2024 Freya Murphy */

#ifndef __LEX_H__
#define __LEX_H__

#include <mlimits.h>
#include <stdio.h>
#include <stdint.h>

/// represents a non null
/// terminated string
struct string {
	char *str;
	uint32_t len;
	uint32_t size;
	bool allocated;
};

/* initalize a string */
void string_init(struct string *string);
/* free a string */
void string_free(struct string *string);
/* clone a string, leave the old one */
int string_clone(struct string *dst, const struct string *const src);
/* move a string, delete the old one */
void string_move(struct string *dst, struct string *src);
/* pushes a char onto a string */
int string_push(struct string *string, char c);
/* load a string from the bss (not allocated) */
void string_bss(struct string *string, char *src);

enum token_type {
	/// has no associated
	/// data
	TOK_COMMA,
	TOK_EQUAL,
	TOK_LPAREN,
	TOK_RPAREN,
	TOK_EOF,
	TOK_NL,

	/// uses number
	TOK_NUMBER,

	/// uses string
	TOK_REG,
	TOK_IDENT,
	TOK_LABEL,
	TOK_STRING,
	TOK_DIRECTIVE,
};

/// represents a token
/// returned from the lexer
struct token {
	/// type
	enum token_type type;

	/// position
	int x, y;
	/// pos in bytes
	int off;

	/// data
	union {
		int64_t number;
		struct string string;
	};
};

/* frees a token*/
void token_free(struct token *token);

/// holds the data
/// for the current lexer
struct lexer {
	// the currently
	// open file
	FILE *file;

	// the last character peeked
	int peek;

	// the current position
	int x, y;
};

/// holds a previous state of a
/// lexer, which allows rebounding
struct lexer_state {
	long offset;
	int peek;
	int x;
	int y;
};

/* initalize a lexer */
int lexer_init(const char *file, struct lexer *lexer);

/* free the lexer */
void lexer_free(struct lexer *lexer);

/* lexes the next token, returns M_ERROR on error,
 * and TOK_EOF on EOF */
int lexer_next(struct lexer *lexer, struct token *token);

/* token type to string */
char *token_str(enum token_type);

/* save the state of a lexer */
void lexer_save(struct lexer *lexer, struct lexer_state *state);

/* load a different state into a lexer */
void lexer_load(struct lexer *lexer, const struct lexer_state *state);

#endif /* __LEX_H__ */