-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod_v1.rs
More file actions
170 lines (163 loc) · 6.88 KB
/
Copy pathmod_v1.rs
File metadata and controls
170 lines (163 loc) · 6.88 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
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
use self::{
env::{Env, GEnv, LEnv},
value::{Value, Function},
};
use crate::parser::ast::*;
use std::{rc::Rc, collections::HashMap, cell::RefCell, borrow::BorrowMut};
mod env;
pub mod value;
impl Block {
// Interprétation d'un bloc
fn interp<'ast, 'genv>(&'ast self, env: &'ast mut Env<'ast, 'genv>) -> Value<'ast> {
//might need to create a new env here ?
self.body.interp(env);
self.ret.interp(env)
}
}
impl Stat_ {
// Interprétation d'une instruction
fn interp<'ast, 'genv>(&'ast self, env: &mut Env<'ast, 'genv>) -> () {
match self {
Self::Nop => (),
Self::Seq(e,e_) => {
e.interp(env);
e_.interp(env);
},
Self::StatFunctionCall(fc) => { fc.interp(env); },
Self::Assign(var, e) => {
let v = e.interp(env);
if let Some(c) = env.locals.lookup(var) {
c.replace(v);
return;
} else if let Some(c) = env.globals.0.get_mut(var) {
c.replace(v);
return;
}
panic!("Var {} couldn't be found...", var);
},
Self::WhileDoEnd(cond, e) => {
while cond.interp(env) != Value::Bool(false) {
e.interp(env);
}
},
Self::If(cond, e, e_) => {
if cond.interp(env) == Value::Bool(true) {
e.interp(env);
} else {
e_.interp(env);
}
},
}
}
}
impl FunctionCall {
// Interprétation d'un appel de fonction
fn interp<'ast, 'genv>(&'ast self, env: &mut Env<'ast, 'genv>) -> Value<'ast> {
match self.name.interp(env) {
Value::Function(f) => f.interp(env),
_ => Value::Nil,
}
}
}
impl Exp_ {
// Interprétation d'une expression
fn interp<'ast, 'genv>(&'ast self, env: &'ast mut Env<'ast, 'genv>) -> Value<'ast> {
match self {
Self::Nil => Value::Nil,
Self::False => Value::Bool(false),
Self::True => Value::Bool(true),
Self::Number(n) => Value::Number(*n),
Self::LiteralString(str) => Value::String(str.clone()),
Self::Var(var) => {
if let Some(v) = env.locals.lookup(&var) {
return v.borrow().clone();
}
env.globals.lookup(&var)
},
Self::ExpFunctionCall(fc) => fc.interp(env),
Self::FunctionDef(fb) => {
let f = Function::new(env, &fb.params, &fb.body);
Value::Function(f)
},
Self::BinOp(bop, e, e_) => {
let v = e.interp(env);
let v_ = e_.interp(env);
match bop {
BinOp::Addition => match(v,v_){
(Value::Number(n), Value::Number(n_)) => Value::Add(n,n_),
_ => panic!("cannot interpret '{} + {}' because not both numeric values", v,v_),
},
BinOp::Subtraction => match (v,v_){
(Value::Number(n), Value::Number(n_)) => Value::Sub(n,n_),
_ => panic!("cannot interpret '{} - {}' because not both numeric values", v,v_),
},
BinOp::Multiplication => match (v,v_){
(Value::Number(n), Value::Number(n_)) => Value::Mul(n,n_),
_ => panic!("cannot interpret '{} * {}' because not both numeric values", v,v_),
},
BinOp::Equality => Value::Bool(v == v_),
BinOp::Inequality => Value::Bool(v != v_),
//shall test all these logical stuff
BinOp::Less => match (v,v_){
(Value::Number(n), Value::Number(n_)) => Value::Bool(v < v_),
_ => panic!("cannot interpret '{} < {}' because not both numeric values", v,v_),
},
BinOp::LessEq => match (v,v_){
(Value::Number(n), Value::Number(n_)) => Value::Bool(v <= v_),
_ => panic!("cannot interpret '{} <= {}' because not both numeric values", v,v_),
},
BinOp::Greater => match (v,v_){
(Value::Number(n), Value::Number(n_)) => Value::Bool(v > v_),
_ => panic!("cannot interpret '{} > {}' because not both numeric values", v,v_),
},
BinOp::GreaterEq => match (v,v_){
(Value::Number(n), Value::Number(n_)) => Value::Bool(v >= v_),
_ => panic!("cannot interpret '{} >= {}' because not both numeric values", v,v_),
},
BinOp::LogicalAnd => match (v,v_){
(Value::Bool(b), Value::Bool(b_)) => Value::Bool(b && b_),
_ => panic!("cannot interpret '{} && {}' because not both boolean values", v,v_),
},
BinOp::LogicalOr => match (v,v_){
(Value::Bool(b), Value::Bool(b_)) => Value::Bool(b || b_),
_ => panic!("cannot interpret '{} || {}' because not both boolean values", v,v_),
},
}
},
Self::UnOp(uop, e) => match uop {
UnOp::UnaryMinus => {
match e.interp(env) {
Value::Number(n) => Value::Number(-n),
_ => panic!("UnaryMinus excpects a numeric value"),
}
}
UnOp::Not => {
match e.interp(env) {
Value::Bool(b) => Value::Bool(!b),
_ => panic!("Not excpects a numeric value"),
}
}
},
Self::Table(tab) => {
let mut table = HashMap::new();
for(k, v) in tab {
let key = k.interp(env);
let val = v.interp(env);
table.insert(key, val);
}
Value::Table(table)
}
}
}
}
// Point d'entrée principal de l'interpréteur
pub fn run(ast: &Block) {
let mut globals = GEnv(HashMap::new());
let printid = "print".to_owned();
globals.0.insert(&printid, Value::Function(Function::Print));
let mut env = Env {
locals: Rc::new(LEnv::Nil),
globals: &mut globals,
};
ast.interp(&mut env);
}