-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.h
More file actions
159 lines (119 loc) · 3.78 KB
/
Copy pathparse.h
File metadata and controls
159 lines (119 loc) · 3.78 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#ifndef INCLUDE_PARSE
#define INCLUDE_PARSE
//Tokens
//Literals
#define LITERAL 1
//sub_types
#define INTEGER 1
#define CHARACTER 2
#define STRING 3
//Operators
#define OPERATOR 2
//sub_types
#define ASSIGN 1
#define ADD 2
#define SUBTRACT 3
#define MULTIPLY 4
#define DIVIDE 5
#define EQUADD 6
#define EQUSUB 7
#define EQUMUL 8
#define EQUDIV 9
#define EQUALS 10
#define LESSTHAN 11
#define GREATERTHAN 12
#define LESSTHANEQU 13
#define GREATERTHANEQU 14
#define MODULO 15
#define AND 16
#define OR 17
#define XOR 18
#define BITAND 19
#define BITOR 20
#define BITXOR 21
#define ELEMENT 22
#define NOTEQUALS 23
#define LEFTSHIFT 24
#define RIGHTSHIFT 25
//Intermediate optimization operations
//
//These cannot be programmatically used but are internally used by the optimizer
#define LOADSTACK 26
//Control
#define CONTROL 3
//sub_types
#define OPENBRACES 1
#define CLOSEBRACES 2
#define OPENPARENTHESES 3
#define CLOSEPARENTHESES 4
#define SEMICOLON 5
#define COMMA 6
#define CLOSEBRACKET 7
//Key words
#define KEYWORD 4
//sub_types
#define IF 1
#define ELSE 2
#define WHILE 3
#define VAR 4
#define RETURN 5
//Unary operator
#define UNARY 5
//sub_types
#define DEREFERENCE 1
#define REFERENCE 2
#define NEGATE 3
#define NOT 4
#define BITNOT 5
//Identifier
#define IDENTIFIER 6
//End of file
#define END 7
#define RUNFUNCTION 8
typedef struct token token;
//Token definition.
//Tokens encompass LITERALS, OPERATORS, CONTROL CHARACTERS, KEYWORDS, UNARY OPERATORS, and IDENTIFIERS
//
//Each token has a type and a sub_type defined as one of the values at the beginning of this file.
//The type alone gives enough information to handle the following tokens.
//The sub_type gives information required to compile the tokens into an abstract syntax tree.
struct token{
unsigned char type;
unsigned char sub_type;
union{
int int_value;
char char_value;
char *string_value;
};
};
//Determines if a character is a letter.
unsigned char is_alpha(unsigned char c);
//Determines if a character is a digit.
unsigned char is_digit(unsigned char c);
//Increments *c until **c is not a whitespace character.
void skip_whitespace(char **c);
//Interprets an integer string starting at *c and returns the integer value.
//Modifies *c such that it points to the next character after the integer.
int get_integer(unsigned char **c);
//Returns a new string containing the identifier name pointed to by *c.
//Modifies *c such that it points to the next character after the identifier.
char *get_identifier(char **c);
//Prints a token for debugging purposes.
void print_token(token t);
//Gets the first token pointed to by *c.
//Modifies *c such that it points to the next character after that token.
token get_token(char **c);
//Adds a token to the current list of tokens
void add_token(token **token_list, token t, unsigned int *token_index, unsigned int *token_length);
//Validates and tokenizes an expression pointed to by *c.
//Modifies *c such that it points to the next character after that expression.
void parse_expression(char **c, token **token_list, unsigned int *token_index, unsigned int *token_length, token closing_token);
//Validates and tokenizes a statement pointed to by *c.
//Modifies *c such that it points to the next character after that statement.
void parse_statement(char **c, token **token_list, unsigned int *token_index, unsigned int *token_length);
//Validates and tokenizes a block of statements pointed to by *c.
//Modifies *c such that it points to the next character after that block.
void parse_block(char **c, token **token_list, unsigned int *token_index, unsigned int *token_length);
//Validates and tokenizes a full program pointed to by *c.
void parse_program(char **c, token **token_list, unsigned int *token_index, unsigned int *token_length);
#endif