forked from rustcoreutils/posixutils-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstructions.rs
175 lines (162 loc) · 4.34 KB
/
instructions.rs
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//
// Copyright (c) 2024 Hemi Labs, Inc.
//
// This file is part of the posixutils-rs project covered under
// the MIT License. For the full license text, please see the LICENSE
// file in the root directory of this project.
// SPDX-License-Identifier: MIT
//
use std::rc::Rc;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum BuiltinFunction {
Length,
Sqrt,
Scale,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Register {
IBase,
OBase,
Scale,
}
#[derive(Clone, Debug, PartialEq)]
pub enum StmtInstruction {
Break,
Quit,
Return,
ReturnExpr(ExprInstruction),
If {
condition: ConditionInstruction,
instruction_count: usize,
body: Vec<StmtInstruction>,
},
While {
condition: ConditionInstruction,
instruction_count: usize,
body: Vec<StmtInstruction>,
},
For {
init: ExprInstruction,
condition: ConditionInstruction,
update: ExprInstruction,
instruction_count: usize,
body: Vec<StmtInstruction>,
},
String(String),
Expr(ExprInstruction),
DefineFunction {
name: char,
function: Function,
},
}
#[derive(Clone, Debug, PartialEq)]
pub enum NamedExpr {
VariableNumber(char),
ArrayItem {
name: char,
index: Box<ExprInstruction>,
},
}
#[derive(Clone, Debug, PartialEq)]
pub enum FunctionArgument {
Expr(ExprInstruction),
ArrayVariable(char),
}
#[derive(Clone, Debug, PartialEq)]
pub enum ExprInstruction {
Number(String),
Named(NamedExpr),
GetRegister(Register),
Builtin {
function: BuiltinFunction,
arg: Box<ExprInstruction>,
},
PreIncrement(NamedExpr),
PreDecrement(NamedExpr),
PostIncrement(NamedExpr),
PostDecrement(NamedExpr),
Call {
name: char,
args: Vec<FunctionArgument>,
},
Assignment {
named: NamedExpr,
value: Box<ExprInstruction>,
},
SetRegister {
register: Register,
value: Box<ExprInstruction>,
},
UnaryMinus(Box<ExprInstruction>),
Add(Box<ExprInstruction>, Box<ExprInstruction>),
Sub(Box<ExprInstruction>, Box<ExprInstruction>),
Mul(Box<ExprInstruction>, Box<ExprInstruction>),
Div(Box<ExprInstruction>, Box<ExprInstruction>),
Mod(Box<ExprInstruction>, Box<ExprInstruction>),
Pow(Box<ExprInstruction>, Box<ExprInstruction>),
}
#[derive(Clone, Debug, PartialEq)]
pub enum ConditionInstruction {
Expr(ExprInstruction),
Eq(ExprInstruction, ExprInstruction),
Ne(ExprInstruction, ExprInstruction),
Lt(ExprInstruction, ExprInstruction),
Leq(ExprInstruction, ExprInstruction),
Gt(ExprInstruction, ExprInstruction),
Geq(ExprInstruction, ExprInstruction),
}
#[derive(Clone, Debug, PartialEq)]
pub enum Variable {
Number(char),
Array(char),
}
/// A bc function.
/// # Body and Source Locations
/// Statements in `body` map to source locations as
/// described in the documentation for `Program`.
#[derive(Clone, Debug, PartialEq)]
pub struct Function {
pub name: char,
pub parameters: Rc<[Variable]>,
pub locals: Rc<[Variable]>,
pub body: Rc<[StmtInstruction]>,
pub source_locations: Rc<[usize]>,
pub file: Rc<str>,
}
impl Default for Function {
fn default() -> Self {
Function {
name: '\0',
parameters: Rc::new([]),
locals: Rc::new([]),
body: Rc::new([]),
source_locations: Rc::new([]),
file: Rc::from(""),
}
}
}
/// A bc program
/// # Instructions and Source Locations
/// The `source_locations` field is a vector containing the line
/// numbers of the statements in the program. The index of a statement
/// is its index inside `instructions` if these were flattened into a
/// single vector, meaning every instruction inside a compound statement
/// (`if`, `while` or `for`) is also counted. `DefineFunction` does not
/// count as a statement in this context.
#[derive(Debug)]
pub struct Program {
pub file: Rc<str>,
pub instructions: Vec<StmtInstruction>,
pub source_locations: Vec<usize>,
}
// This is used only for testing purposes
#[cfg(test)]
impl From<Vec<StmtInstruction>> for Program {
fn from(instructions: Vec<StmtInstruction>) -> Self {
Program {
file: Rc::from(""),
instructions,
source_locations: vec![],
}
}
}