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
use std::fmt;
use std::collections::HashMap;
use serde::{Serialize, Deserialize};
use yolol_number::prelude::*;
use crate::types::ast::value::LiteralValue;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Environment
{
pub name: String,
pub version: String,
pub next_line: i64,
pub error: String,
local_context: HashMap<String, LiteralValue>,
global_context: HashMap<String, LiteralValue>
}
impl Environment
{
pub fn new(name: &str) -> Environment
{
let name = String::from(name);
let version = String::from(env!("CARGO_PKG_VERSION"));
let next_line = 1;
let local_context = HashMap::new();
let global_context = HashMap::new();
Environment {
name,
version,
next_line,
error: String::new(),
local_context,
global_context,
}
}
pub fn set_next_line(&mut self, num: YololNumber)
{
self.next_line = num.bound().get_value();
}
}
impl fmt::Display for Environment
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
{
let mut out_string = format!("Environment from Yoloxide version {}\n", self.version);
out_string += &format!("Name: {}, next line: {}.\n", self.name, self.next_line);
out_string += "\n";
out_string += "Local context:\n";
for (key, value) in &self.local_context
{
out_string += &format!("Key: '{}', Value: '{}'\n", key, value);
}
out_string += "\n";
out_string += "Global context:\n";
for (key, value) in &self.global_context
{
out_string += &format!("Key: '{}', Value: '{}'\n", key, value);
}
write!(f, "{}", out_string)
}
}
pub trait ContextMap
{
fn get_val(&self, ident: &str) -> LiteralValue;
fn set_val(&mut self, ident: String, value: LiteralValue);
}
impl ContextMap for Environment
{
fn get_val(&self, ident: &str) -> LiteralValue
{
if let Some(':') = ident.chars().next()
{
self.global_context.get(ident)
.unwrap_or(&LiteralValue::get_false())
.clone()
}
else
{
self.local_context.get(ident)
.unwrap_or(&LiteralValue::get_false())
.clone()
}
}
fn set_val(&mut self, ident: String, value: LiteralValue)
{
if let Some(':') = ident.chars().next()
{
self.global_context.insert(ident, value);
}
else
{
self.local_context.insert(ident, value);
}
}
}