-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvm_stack.h
114 lines (96 loc) · 1.62 KB
/
vm_stack.h
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
#ifndef _VM_STACK_H_
#define _VM_STACK_H_
#include "square.h"
#define MAXSIZE 256
squ_value* stack[MAXSIZE];
void push(squ_value* obj);
squ_value* pop();
int sp = 0;
void
push(squ_value* obj)
{
if(sp < MAXSIZE)
{
stack[sp++] = obj;
}
else
{
printf("stack full");
}
}
squ_value*
pop()
{
if(sp > 0)
{
return stack[--sp];
}
else
{
printf("stack empty");
return 0;
}
}
void pop_top()
{
pop();
}
void binary_add()
{
squ_value* first = pop();
squ_value* second = pop();
squ_value* result;
result->t = SQU_VALUE_INT;
result->v.i = first->v.i + second->v.i;
push(result);
}
void binary_sub()
{
squ_value* first = pop();
squ_value* second = pop();
squ_value* result;
result->t = SQU_VALUE_INT;
result->v.i = first->v.i - second->v.i;
push(result);
}
void binary_mult()
{
squ_value* first = pop();
squ_value* second = pop();
squ_value* result;
result->t = SQU_VALUE_INT;
result->v.i = first->v.i * second->v.i;
push(result);
}
void binary_div()
{
squ_value* first = pop();
squ_value* second = pop();
squ_value* result;
result->t = SQU_VALUE_INT;
result->v.i = first->v.i / second->v.i;
push(result);
}
void print_newline()
{
printf("\n");
}
void print_item()
{
squ_value* item = pop();
switch(item->t)
{
case SQU_VALUE_STRING:
puts(item->v.s);
break;
case SQU_VALUE_INT:
printf("%d", item->v.i);
break;
case SQU_VALUE_DOUBLE:
printf("%f", item->v.d);
break;
default:
break;
}
}
#endif /* _VM_STACK_H_ */