-
Notifications
You must be signed in to change notification settings - Fork 5
/
10do_while.c
95 lines (67 loc) · 2.2 KB
/
10do_while.c
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
#include "common.h"
struct sDoWhileNode
{
sNode*% mExpressionNode;
sBlock*% mBlock;
int sline;
string sname;
};
sDoWhileNode*% sDoWhileNode*::initialize(sDoWhileNode*% self, sNode*% expression_node, sBlock* block, sInfo* info)
{
self.sline = info.sline;
self.sname = string(info.sname);
self.mExpressionNode = clone expression_node;
self.mBlock = clone block;
return self;
}
bool sDoWhileNode*::terminated()
{
return true;
}
bool sDoWhileNode*::compile(sDoWhileNode* self, sInfo* info)
{
sBlock* block = self.mBlock;
add_come_code(info, "do {\n");
transpile_block(block, null, null, info);
/// compile expression ///
sNode* expression_node = self.mExpressionNode;
if(!expression_node.compile->(info)) {
return false;
}
CVALUE*% conditional_value = get_value_from_stack(-1, info);
dec_stack_ptr(1, info);
static int num_while_condtional = 0;
add_come_code_at_function_head(info, "_Bool _do_while_condtional%d;\n", ++num_while_condtional);
add_come_code(info, "} while(_do_while_condtional%d=%s,", num_while_condtional, conditional_value.c_value);
free_right_value_objects(info, comma:true);
add_come_code(info, "_do_while_condtional%d);\n", num_while_condtional);
transpiler_clear_last_code(info);
return true;
}
int sDoWhileNode*::sline(sDoWhileNode* self, sInfo* info)
{
return self.sline;
}
string sDoWhileNode*::sname(sDoWhileNode* self, sInfo* info)
{
return string(self.sname);
}
sNode*% string_node(char* buf, char* head, int head_sline, sInfo* info) version 10
{
if(buf === "do") {
string sname = clone info->sname;
int sline = info->sline;
sBlock*% block = parse_block(info);
string buf2 = parse_word(info);
if(buf2 !== "while") {
err_msg(info, "require while");
exit(2);
}
expected_next_character('(', info);
/// expression ///
sNode*% expression_node = expression(info);
expected_next_character(')', info);
return new sNode(new sDoWhileNode(expression_node, block, info));
}
return inherit(buf, head,head_sline, info);
}