Newer
Older
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
use crate::parse::ast::{
BinaryOp, DeclareFunction, ExpressionList, Program, UnaryOp, ValueExpression, VoidExpression,
};
use crate::runtime::executor::Visitor;
use crate::runtime::value::{ForgeValue, UnsupportedOperation};
use std::collections::HashMap;
use std::error::Error;
use std::fmt::{Debug, Display, Formatter};
#[derive(Clone, Default)]
pub struct SimpleExecutor {
data: HashMap<String, ForgeValue>,
vtable: HashMap<String, DeclareFunction>,
}
#[derive(Clone, Debug, PartialEq)]
pub enum RuntimeError {
Unsupported(&'static str),
BadOperands(UnsupportedOperation),
}
impl From<UnsupportedOperation> for RuntimeError {
fn from(value: UnsupportedOperation) -> Self {
Self::BadOperands(value)
}
}
impl Display for RuntimeError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
RuntimeError::Unsupported(expr) => {
write!(
f,
"[Runtime] Encountered an unsupported expression: {}",
expr
)
}
RuntimeError::BadOperands(unsupported) => write!(f, "[Runtime] {}", unsupported),
}
}
}
impl Error for RuntimeError {}
impl SimpleExecutor {
fn evaluate_expression_list(
&mut self,
list: &ExpressionList,
) -> Result<ForgeValue, RuntimeError> {
let mut last_val = Ok(ForgeValue::Null);
for expr in &list.expressions {
last_val = Ok(self.evaluate_expression(expr)?);
}
if list.is_void {
Ok(ForgeValue::Null)
} else {
last_val
}
}
}
impl Visitor for SimpleExecutor {
type Output = ForgeValue;
type Error = RuntimeError;
fn evaluate_value_expression(
&mut self,
expression: &ValueExpression,
) -> Result<Self::Output, Self::Error> {
match expression {
ValueExpression::Unary { operator, operand } => {
let value = self.evaluate_value_expression(operand.as_ref())?;
match operator {
UnaryOp::Negate => Ok(value.invert()),
UnaryOp::Not => Ok(!value),
}
}
ValueExpression::Binary { operator, lhs, rhs } => {
let lhs = self.evaluate_value_expression(lhs.as_ref())?;
let rhs = self.evaluate_value_expression(rhs.as_ref())?;
match operator {
BinaryOp::Add => Ok(lhs + rhs),
BinaryOp::Subtract => Ok((lhs - rhs)?),
BinaryOp::Divide => Ok((lhs / rhs)?),
BinaryOp::Multiply => Ok((lhs * rhs)?),
BinaryOp::Modulo => Ok((lhs % rhs)?),
BinaryOp::Equals => Ok((lhs == rhs).into()),
}
}
ValueExpression::Grouped(group) => self.evaluate_value_expression(group.inner.as_ref()),
ValueExpression::Block(_) => Err(RuntimeError::Unsupported("Block")),
ValueExpression::Literal(lit) => Ok(ForgeValue::from(lit.clone())),
ValueExpression::DeclareIdentifier(_) => {
Err(RuntimeError::Unsupported("DeclareIdentifier"))
}
ValueExpression::Assignment(_) => Err(RuntimeError::Unsupported("Assignment")),
ValueExpression::ConditionalBlock(_) => {
Err(RuntimeError::Unsupported("ConditionalBlock"))
}
ValueExpression::Identifier(_) => Err(RuntimeError::Unsupported("Identifier")),
ValueExpression::FunctionCall(_) => Err(RuntimeError::Unsupported("FunctionCall")),
ValueExpression::DeclareFunction(_) => {
Err(RuntimeError::Unsupported("DeclareFunction"))
}
}
}
fn evaluate_void_expression(
&mut self,
expression: &VoidExpression,
) -> Result<Self::Output, Self::Error> {
Err(RuntimeError::Unsupported("Void Expression"))
}
fn evaluate_program(&mut self, program: &Program) -> Result<Self::Output, Self::Error> {
self.evaluate_expression_list(&program.0)
}
}
#[cfg(test)]
mod interpreter_test {
use crate::parse::parse_program;
use crate::runtime::executor::simple::{RuntimeError, SimpleExecutor};
use crate::runtime::executor::Visitor;
use crate::runtime::value::ForgeValue;
#[test]
fn the_basics() {
let add_numbers = parse_program("1 + 1").expect("Failed to parse");
let add_strings = parse_program("\"foo\" + \" \" + \"bar\"").expect("Failed to parse");
let concat_string_num = parse_program("\"#\" + 1").expect("Failed to parse");
let bad_mult = parse_program("false * 123").expect("Failed to parse");
let mut vm = SimpleExecutor::default();
assert_eq!(vm.evaluate_program(&add_numbers), Ok(ForgeValue::from(2)));
assert_eq!(
vm.evaluate_program(&add_strings),
Ok(ForgeValue::from("foo bar"))
);
assert_eq!(
vm.evaluate_program(&concat_string_num),
Ok(ForgeValue::from("#1"))
);
assert!(matches!(
vm.evaluate_program(&bad_mult),
Err(RuntimeError::BadOperands(_))
))
}
#[test]
fn combos() {
let add_numbers = parse_program("1 + -1").expect("Failed to parse");
let mut vm = SimpleExecutor::default();
assert_eq!(vm.evaluate_program(&add_numbers), Ok(ForgeValue::from(0)));
}
}