aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: b64379023434fd1f41fb59b16e3c437ac6058914 (plain)
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#[macro_use]
extern crate pest_derive;
#[macro_use]
extern crate pest;

mod parser {
    #[derive(Parser)]
    #[grammar = "grammar.pest"]
    pub struct Parser;
}

mod ast {
    use super::parser::Rule;
    use std::rc::Rc;

    pub fn from_parse_tree(parse_tree: &mut pest::iterators::Pairs<Rule>) -> Program {
        let root = parse_tree.next().unwrap();

        match root.as_rule() {
            Rule::program => {
                let statements_with_eoi: Vec<pest::iterators::Pair<Rule>> =
                    root.into_inner().collect();
                let statements_split = statements_with_eoi.split_last().unwrap();
                let statements = statements_split.1.to_vec();

                statements
                    .iter()
                    .map(|s| Statement::from_pair(s.clone()))
                    .collect()
            }
            _ => panic!("[ast] first Pair is not a program"),
        }
    }

    pub type Program = Vec<Statement>;

    #[derive(Debug)]
    pub enum Statement {
        Definition {
            symbol: Symbol,
            expression: Rc<Expression>,
        },
        Expression(Rc<Expression>),
    }

    impl Statement {
        fn from_pair(pair: pest::iterators::Pair<Rule>) -> Statement {
            let def_or_expr = pair.into_inner().next().unwrap();

            fn definition_from_pair(pair: pest::iterators::Pair<Rule>) -> Statement {
                let mut inner = pair.into_inner();
                let symbol = inner.next().unwrap().as_span().as_str();
                let expression = inner.next().unwrap();
                let expression_inners: Vec<ExpressionInner> = expression
                    .into_inner()
                    .map(ExpressionInner::from_pair)
                    .collect();

                Statement::Definition {
                    symbol: String::from(symbol),
                    expression: expression_vec_to_tuple(&expression_inners),
                }
            }

            match def_or_expr.as_rule() {
                Rule::definition => definition_from_pair(def_or_expr),
                Rule::expression => {
                    let expression_inners: Vec<ExpressionInner> = def_or_expr
                        .into_inner()
                        .map(ExpressionInner::from_pair)
                        .collect();
                    Statement::Expression(expression_vec_to_tuple(&expression_inners))
                }
                rule => panic!("[ast] can't make a statement from {:#?}", rule),
            }
        }
    }

    #[derive(Debug, Clone)]
    pub enum ExpressionInner {
        Symbol(Symbol),
        IntegerLiteral(IntegerLiteral),
        StringLiteral(StringLiteral),
        Expression(Rc<Expression>),
    }

    impl ExpressionInner {
        fn from_pair(pair: pest::iterators::Pair<Rule>) -> ExpressionInner {
            match pair.as_rule() {
                Rule::symbol => ExpressionInner::Symbol(string_from_pair(pair)),
                Rule::integer_literal => ExpressionInner::IntegerLiteral(integer_from_pair(pair)),
                Rule::string_literal => {
                    let s_with_quotes = string_from_pair(pair);
                    let mut chars = s_with_quotes.chars();
                    chars.next();
                    chars.next_back();
                    let s = chars.as_str();
                    ExpressionInner::StringLiteral(String::from(s))
                }
                Rule::expression => {
                    let expression_inners: Vec<ExpressionInner> =
                        pair.into_inner().map(ExpressionInner::from_pair).collect();
                    ExpressionInner::Expression(expression_vec_to_tuple(&expression_inners))
                }
                rule => panic!("[ast] can't make an expression element from {:#?}", rule),
            }
        }
    }

    #[derive(Debug)]
    pub enum Expression {
        Unary(ExpressionInner),
        Binary(ExpressionInner, ExpressionInner),
    }

    fn expression_vec_to_tuple(v: &Vec<ExpressionInner>) -> Rc<Expression> {
        match v.len() {
            1 => Rc::new(Expression::Unary(v[0].clone())),
            2 => Rc::new(Expression::Binary(v[0].clone(), v[1].clone())),
            _ => {
                let combined = ExpressionInner::Expression(Rc::new(Expression::Binary(
                    v[0].clone(),
                    v[1].clone(),
                )));

                let mut new_v = vec![combined];
                for e in &v[2..] {
                    new_v.push(e.clone());
                }

                expression_vec_to_tuple(&new_v)
            }
        }
    }

    pub type IntegerLiteral = i64;
    fn integer_from_pair(pair: pest::iterators::Pair<Rule>) -> i64 {
        let s = pair.as_span().as_str();
        s.parse().unwrap()
    }

    pub type StringLiteral = String;
    fn string_from_pair(pair: pest::iterators::Pair<Rule>) -> String {
        String::from(pair.as_span().as_str())
    }

    pub type Symbol = String;
}

mod runtime {
    use super::ast;
    use std::collections::HashMap;
    use std::fmt;
    use std::rc::Rc;
    use std::sync::atomic::{AtomicUsize, Ordering};

    #[derive(Debug, Clone)]
    pub enum BuiltinFunction {
        IntegerIncrement,
        IntegerAdd,
        IntegerAdd1(i64),
    }

    #[derive(Debug, Clone)]
    pub enum Value {
        Integer(i64),
        String(String),
        Var(usize),
        Function(usize, Rc<Value>),
        BuiltinFunction(BuiltinFunction),
    }

    impl fmt::Display for Value {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            match self {
                Value::Integer(value) => write!(f, "{} :: Integer", value),
                Value::String(value) => write!(f, "{} :: String", value),
                Value::Var(v) => write!(f, "{} :: Unbound variable", v),
                Value::Function(_, _) => {
                    write!(f, "Function :: Function")
                }
                Value::BuiltinFunction(_) => write!(f, "Built-in function :: Function"),
            }
        }
    }

    static VAR_ID_INC: AtomicUsize = AtomicUsize::new(0);
    const B_INTEGER_INCREMENT: &str = "int.increment";
    const B_INTEGER_ADD: &str = "int.add";

    fn advance_v() -> usize {
        let v = VAR_ID_INC.load(Ordering::Relaxed);
        VAR_ID_INC.store(v + 1, Ordering::Relaxed);
        v
    }

    fn try_builtin_symbol_to_value(symbol: &ast::Symbol) -> Option<Value> {
        match symbol.as_str() {
            "true" => {
                let x = advance_v();
                let y = advance_v();
                Some(Value::Function(
                    x,
                    Rc::new(Value::Function(y, Rc::new(Value::Var(x)))),
                ))
            }
            "false" => {
                let x = advance_v();
                let y = advance_v();
                Some(Value::Function(
                    x,
                    Rc::new(Value::Function(y, Rc::new(Value::Var(y)))),
                ))
            }
            "id" => {
                let v = advance_v();
                Some(Value::Function(v, Rc::new(Value::Var(v))))
            }
            B_INTEGER_INCREMENT => Some(Value::BuiltinFunction(BuiltinFunction::IntegerIncrement)),
            B_INTEGER_ADD => Some(Value::BuiltinFunction(BuiltinFunction::IntegerAdd)),
            _ => None,
        }
    }

    fn try_apply_function(
        func_rc: Rc<Value>,
        arg_rc: Rc<Value>,
        bound_v: Option<usize>,
    ) -> Rc<Value> {
        let func = &*func_rc;
        let arg = &*arg_rc;
        match func {
            Value::Function(func_v, body_rc) => {
                let v1 = bound_v.unwrap_or(*func_v);
                let body = &**body_rc;
                match body {
                    Value::Var(v2) => {
                        if v1 == *v2 {
                            arg_rc
                        } else {
                            Rc::clone(body_rc)
                        }
                    }
                    Value::Function(body_v, _) => {
                        let new_body = try_apply_function(Rc::clone(body_rc), arg_rc, Some(v1));
                        Rc::new(Value::Function(*body_v, new_body))
                    }
                    _ => Rc::clone(body_rc),
                }
            }
            Value::BuiltinFunction(builtin) => match builtin {
                BuiltinFunction::IntegerIncrement => match arg {
                    Value::Integer(value) => Rc::new(Value::Integer(value + 1)),
                    _ => panic!(
                        "[runtime] tried to apply non-integer value to {}",
                        B_INTEGER_INCREMENT
                    ),
                },
                BuiltinFunction::IntegerAdd => match arg {
                    Value::Integer(value) => {
                        Rc::new(Value::BuiltinFunction(BuiltinFunction::IntegerAdd1(*value)))
                    }
                    _ => panic!(
                        "[runtime] tried to apply non-integer value to {}",
                        B_INTEGER_ADD
                    ),
                },
                BuiltinFunction::IntegerAdd1(other) => match arg {
                    Value::Integer(value) => Rc::new(Value::Integer(other + value)),
                    _ => panic!(
                        "[runtime] tried to apply non-integer value to {}",
                        B_INTEGER_ADD
                    ),
                },
            },
            _ => func_rc,
        }
    }

    fn evaluate_expr_inner_unary(
        symbol_table: &HashMap<String, Rc<Value>>,
        inner: &ast::ExpressionInner,
    ) -> Rc<Value> {
        match inner {
            ast::ExpressionInner::IntegerLiteral(value) => Rc::new(Value::Integer(*value)),
            ast::ExpressionInner::StringLiteral(value) => Rc::new(Value::String(value.clone())),
            ast::ExpressionInner::Symbol(value) => {
                let builtin_value = try_builtin_symbol_to_value(value);
                if builtin_value.is_some() {
                    return Rc::new(builtin_value.unwrap());
                }

                let table_lookup_value = symbol_table.get(value);
                if table_lookup_value.is_some() {
                    let lookup_rc = table_lookup_value.unwrap();
                    return Rc::clone(lookup_rc);
                }

                panic!("[runtime] symbol not defined: {:#?}", value);
            }
            ast::ExpressionInner::Expression(value_rc) => {
                let sub_expr = &**value_rc;
                evaluate_expr(symbol_table, sub_expr)
            }
        }
    }

    fn evaluate_expr_inner_binary(
        symbol_table: &HashMap<String, Rc<Value>>,
        lhs: &ast::ExpressionInner,
        rhs: &ast::ExpressionInner,
    ) -> Rc<Value> {
        match lhs {
            ast::ExpressionInner::Expression(value_rc) => {
                let sub_expr = &**value_rc;

                let evaled_lhs = evaluate_expr(symbol_table, sub_expr);
                let evaled_rhs = evaluate_expr_inner_unary(symbol_table, rhs);
                try_apply_function(evaled_lhs, evaled_rhs, None)
            }
            ast::ExpressionInner::Symbol(value) => {
                let lhs_value_rc: Rc<Value>;

                if let Some(builtin) = try_builtin_symbol_to_value(value) {
                    lhs_value_rc = Rc::new(builtin);
                } else if let Some(lookup) = symbol_table.get(value) {
                    lhs_value_rc = Rc::clone(lookup);
                } else {
                    panic!("[runtime] symbol not defined: {:#?}", value);
                }

                let evaled_rhs = evaluate_expr_inner_unary(symbol_table, rhs);
                try_apply_function(lhs_value_rc, evaled_rhs, None)
            }
            other => unreachable!(
                "[runtime] this should not be on the left side of a binary expression: {:#?}",
                other
            ),
        }
    }

    fn evaluate_expr(
        symbol_table: &HashMap<String, Rc<Value>>,
        expression: &ast::Expression,
    ) -> Rc<Value> {
        match expression {
            ast::Expression::Unary(inner) => evaluate_expr_inner_unary(symbol_table, inner),
            ast::Expression::Binary(inner1, inner2) => {
                evaluate_expr_inner_binary(symbol_table, inner1, inner2)
            }
        }
    }

    pub fn evaluate(program: &ast::Program) {
        let mut symbol_table: HashMap<String, Rc<Value>> = HashMap::new();

        for statement in program {
            match statement {
                ast::Statement::Definition { symbol, expression } => {
                    println!("[runtime] defining symbol: {:#?}", symbol);
                    let value = evaluate_expr(&symbol_table, expression);
                    symbol_table.insert(symbol.clone(), value);
                }
                ast::Statement::Expression(expression) => {
                    println!("[runtime] evaluating free-standing expression");
                    let value = evaluate_expr(&symbol_table, expression);
                    println!("Result: {}", value);
                }
            }
        }

        println!(
            "[runtime] evaluation done, symbol_table state dump: {:#?}",
            symbol_table
        );
    }
}

fn main() {
    use ast::Program;
    use pest::Parser;
    use std::env;
    use std::fs;

    let script_path = env::args().nth(1).expect("no script file specified");

    let unparsed_file = fs::read_to_string(script_path).expect("cannot read file");
    let parse_tree_result = parser::Parser::parse(parser::Rule::program, &unparsed_file);

    if parse_tree_result.is_err() {
        println!("{}", parse_tree_result.unwrap_err());
        return;
    }

    let mut parse_tree = parse_tree_result.unwrap();

    // println!("parse tree = {:#?}", parse_tree);
    let syntax_tree: Program = ast::from_parse_tree(&mut parse_tree);
    // println!("syntax tree = {:#?}", syntax_tree);

    runtime::evaluate(&syntax_tree);
}