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
use crate::runtime::op::{Operation, ScopePersister, SigStatement};
use crate::runtime::{Environment, Event, Scope};
use crate::{ast, lex, lint};
use regex::Regex;
use std::fs::File;
use std::io::{BufRead, Read, Write};
pub struct Interpreter {
node: ast::Seq,
sep: Regex,
implicit_print: bool,
}
pub struct Builder {
filename: Option<String>,
expression: Option<String>,
sep: Option<Regex>,
print: Option<bool>,
}
impl Interpreter {
pub fn process<R: BufRead, W: Write>(&self, sin: &mut R, sout: &mut W) {
let mut iter = sin.lines();
let mut env = Environment::new(sout, &self.node, self.sep.clone());
if cfg!(feature = "envvar") {
env.push(Scope::env());
}
let implicit_print = !self.node.significant();
env.event = Event::Begin;
self.node.perform(&mut env);
while let Some(Ok(line)) = iter.next() {
env.lineno += 1;
env.event = Event::Line(line);
self.node.persist_scope(&mut env);
self.node.perform(&mut env);
if env.finished() {
return;
}
if implicit_print && self.implicit_print {
env.print_event();
}
}
env.event = Event::End;
self.node.perform(&mut env);
}
pub fn lint(&self) -> Vec<lint::LintMessage> {
lint::lint(&self.node)
}
pub fn builder() -> Builder {
Builder {
filename: None,
expression: None,
sep: None,
print: None,
}
}
}
impl Builder {
pub fn filename(&mut self, filename: String) -> &mut Self {
self.filename = Some(filename);
self
}
pub fn expression(&mut self, expression: String) -> &mut Self {
self.expression = Some(expression);
self
}
pub fn sep(&mut self, sep: Regex) -> &mut Self {
self.sep = Some(sep);
self
}
pub fn print(&mut self, print: bool) -> &mut Self {
self.print = Some(print);
self
}
pub fn build(&mut self) -> Result<Interpreter, String> {
let node = match (&self.filename, &self.expression) {
(None, None) => return Err(String::from("Neither an expression or a file was given")),
(Some(_), Some(_)) => {
return Err(String::from(
"Both expression and file should not be given at the same time",
))
}
(Some(ref file), None) => {
let mut file = match File::open(file) {
Ok(f) => f,
Err(err) => {
return Err(format!(
"unable to open file romulus file '{}': {}",
file, err
))
}
};
let mut buf = String::new();
if let Err(err) = file.read_to_string(&mut buf) {
return Err(format!("unable to read romulus file: {}", err));
}
let tokens = lex::lex(buf.as_ref())?;
ast::parse(tokens)?
}
(None, Some(expr)) => {
let tokens = lex::lex(expr.as_ref())?;
ast::parse(tokens)?
}
};
let sep = self
.sep
.clone()
.unwrap_or_else(|| Regex::new(" +").unwrap());
let implicit_print = self.print.unwrap_or(true);
Ok(Interpreter {
node,
sep,
implicit_print,
})
}
}