-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdim.rs
More file actions
139 lines (127 loc) · 5.05 KB
/
dim.rs
File metadata and controls
139 lines (127 loc) · 5.05 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
use rusty_common::*;
use rusty_linter::core::ScopeName;
use rusty_parser::*;
use rusty_variant::Variant;
use super::{Instruction, InstructionGenerator, RootPath};
impl InstructionGenerator {
pub fn visit_dim_list(&mut self, item: DimList) {
let DimList { shared, variables } = item;
for dim_var_pos in variables {
self.visit_dim_var_pos(dim_var_pos, false, shared);
}
}
pub fn visit_redim_list(&mut self, item: DimList) {
let DimList { shared, variables } = item;
for dim_var_pos in variables {
self.visit_dim_var_pos(dim_var_pos, true, shared);
}
}
fn visit_dim_var_pos(&mut self, item: DimVarPos, is_redim: bool, shared: bool) {
let Positioned {
element: dim_name,
pos,
} = item;
// check if it is already defined to prevent re-allocation of STATIC variables
let is_in_static_subprogram = self.is_in_static_subprogram();
if is_in_static_subprogram && !is_redim {
debug_assert!(
!shared,
"Should not be possible to have a SHARED variable inside a function/sub"
);
self.push(Instruction::IsVariableDefined(dim_name.clone()), pos);
self.jump_if_false("begin-dim", pos);
self.jump("end-dim", pos);
self.label("begin-dim", pos);
self.generate_dim_name(dim_name, shared, pos);
self.label("end-dim", pos);
} else {
self.generate_dim_name(dim_name, shared, pos);
}
}
}
impl InstructionGenerator {
fn is_in_static_subprogram(&self) -> bool {
if self.current_subprogram == ScopeName::Global {
false
} else {
self.subprogram_info_repository
.get_subprogram_info(&self.current_subprogram)
.is_static
}
}
fn generate_dim_name(&mut self, dim_name: DimVar, shared: bool, pos: Position) {
let (bare_name, dim_type) = dim_name.into();
match dim_type {
DimType::Array(array_dimensions, box_element_type) => {
self.push(Instruction::BeginCollectArguments, pos);
for ArrayDimension { lbound, ubound } in array_dimensions {
if let Some(lbound) = lbound {
let lbound_pos = lbound.pos();
self.generate_expression_instructions(lbound);
self.push(Instruction::PushUnnamedByVal, lbound_pos);
} else {
self.push_load_unnamed_arg(Variant::VInteger(0), pos);
}
let ubound_pos = ubound.pos();
self.generate_expression_instructions(ubound);
self.push(Instruction::PushUnnamedByVal, ubound_pos);
}
let element_type = box_element_type.expression_type();
let opt_q = match &element_type {
ExpressionType::BuiltIn(q) => Some(*q),
ExpressionType::FixedLengthString(_) => Some(TypeQualifier::DollarString),
_ => None,
};
self.push(Instruction::AllocateArrayIntoA(element_type), pos);
self.push(
Instruction::VarPathName(RootPath {
name: Name::new(bare_name, opt_q),
shared,
}),
pos,
);
self.push(Instruction::CopyAToVarPath, pos);
}
DimType::BuiltIn(q, _) => {
self.push(Instruction::AllocateBuiltIn(q), pos);
self.push(
Instruction::VarPathName(RootPath {
name: Name::qualified(bare_name, q),
shared,
}),
pos,
);
self.push(Instruction::CopyAToVarPath, pos);
}
DimType::FixedLengthString(_, len) => {
self.push(Instruction::AllocateFixedLengthString(len), pos);
self.push(
Instruction::VarPathName(RootPath {
name: Name::qualified(bare_name, TypeQualifier::DollarString),
shared,
}),
pos,
);
self.push(Instruction::CopyAToVarPath, pos);
}
DimType::UserDefined(Positioned {
element: user_defined_type_name,
..
}) => {
self.push(
Instruction::AllocateUserDefined(user_defined_type_name),
pos,
);
self.push(
Instruction::VarPathName(RootPath {
name: Name::bare(bare_name),
shared,
}),
pos,
);
self.push(Instruction::CopyAToVarPath, pos);
}
DimType::Bare => panic!("Unresolved type"),
}
}
}