-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.h
More file actions
147 lines (117 loc) · 3.19 KB
/
Copy pathparser.h
File metadata and controls
147 lines (117 loc) · 3.19 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
# pragma once
# ifndef PARSER_H
# define PARSER_H
# include "base.h"
/// float literal
struct Literal {
long double value;
};
/// only variables now
struct Identifier {
char *name;
};
/// Binary operation
/// Expr2 := Expression Op2 Expression.
/// left or right hand depends on operator? (maybe separate in future)
struct Expr2 {
struct Expression *lhs;
struct Expression *rhs;
char *op;
// +, -, *, /, &&, ||, ==, !=, <, <=, >, >=, etc.
// =, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=, etc.
};
/// Unary operation (not implemented now)
/// Expr1 := Op1 Expression
struct Expr1 {
struct Expression *expr;
char *op;
// possible unary operators: +, -, !, ~, etc.
};
///
/// Built-in math functions.
///
/// Builtin := func_name ( Expression )
///
/// now provided: abs, sin, cos, tan, asin, acos, atan, sqrt, log, log10, exp, ceil, floor, round, etc.
/// {@see {get_func}}
///
struct Builtin {
long double (*func)(struct Interpreter *, long double);
char *name;
struct Expression *expr;
};
///
/// Any Expression.
/// Expression := Literal | Identifier | Expr2 | Expr1 | Builtin
struct Expression {
enum DataTag tag;
union {
struct Literal *literal;
struct Identifier *identifier;
struct Expr2 *expr2;
struct Builtin *builtin;
struct Expr1 *expr1;
};
};
/// Too difficult to implement, just ignore it
// // struct Print {
// // char *message;
// // struct Expression expr;
// // };
//
// struct Function {
// struct Identifier func;
// struct Expression *expr; // ends with GNull
// };
//
// struct Return {
// struct Expression *expr;
// };
/// Code block
struct Block {
struct Statement **stmts; // ends with GNull
};
/// While loop
struct While {
struct Expression *cond;
struct Block *block;
};
/// If statement
struct If {
struct Expression *cond;
struct Block *then_block;
struct Block *else_block; // may be GNull
};
/// Any statement
struct Statement {
enum DataTag tag;
union {
struct Expression *expr;
struct Builtin *builtin;
// struct Print print;
// struct Function function;
// struct Return ret;
struct Block *block;
struct If *if_stmt;
struct While *while_stmt;
};
};
struct Parser {
struct Block *result_block;
enum Error error;
};
struct Parser *Parser_create();
void Parser_refresh(struct Parser *parser);
void Parser_delete(struct Parser *parser);
void Expr_delete(struct Expression *expr);
void Block_delete(struct Block *block);
void Statement_delete(struct Statement *stmt);
void parse_file(struct Parser *parser, struct TokenData *tokens); // free tokens
struct Block *parse_block(struct Parser *parser, struct TokenData *tokens, int inner); // read from { to } or GNull
struct Statement *parse_statement(struct Parser *parser, struct TokenData *tokens); // read until TokenNewline
struct Expression *parse_expression(struct Parser *parser, struct TokenData *tokens, int inner);
// read until TokenNewline
void print_Statement(const struct Statement *statement);
void print_Block(const struct Block *block);
void print_Expression(const struct Expression *expression);
# endif //PARSER_H