aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 41b4422dbff37e4a94b8e48e9f1989695a428eb7 (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
#[macro_use]
extern crate pest_derive;
extern crate from_pest;
#[macro_use]
extern crate pest_ast;
extern crate pest;

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

// mod ast {
//     use super::parser::Rule;
//     use pest::Span;

//     fn span_into_str(span: Span) -> &str {
//         span.as_str()
//     }

//     #[derive(Debug, FromPest)]
//     #[pest_ast(rule(Rule::program))]
//     pub struct Program {
//         pub statements: Vec<Statement>,
//         pub eoi: EOI,
//     }

//     #[derive(Debug, FromPest)]
//     #[pest_ast(rule(Rule::statement))]
//     pub enum Statement {
//         Definition(Definition),
//         Expression(Expression),
//     }

//     #[derive(Debug, FromPest)]
//     #[pest_ast(rule(Rule::definition))]
//     pub struct Definition {
//         pub symbols: Vec<Symbol>,
//         pub expression: Expression,
//     }

//     #[derive(Debug, FromPest, Clone)]
//     #[pest_ast(rule(Rule::expression))]
//     pub enum Expression {
//         IntegerLiteral(IntegerLiteral),
//         Symbol(Symbol),
//         SubExpressions(Vec<Expression>),
//     }

//     #[derive(Debug, FromPest, Copy, Clone)]
//     #[pest_ast(rule(Rule::integer_literal))]
//     pub struct IntegerLiteral {
//         #[pest_ast(outer(with(span_into_str), with(str::parse::<i64>), with(Result::unwrap)))]
//         pub value: i64,
//     }

//     #[derive(Debug, FromPest, Clone)]
//     #[pest_ast(rule(Rule::symbol))]
//     pub struct Symbol {
//         #[pest_ast(outer(with(span_into_str), with(String::from)))]
//         pub value: String,
//     }

//     #[derive(Debug, FromPest, Copy, Clone)]
//     #[pest_ast(rule(Rule::EOI))]
//     pub struct EOI;
// }

// mod ir {
//     use super::ast;

//     pub fn left_associate_exprs(program: &ast::Program) -> ast::Program {
//         fn associate(expr: &ast::Expression) -> ast::Expression {
//             match expr.nodes.len() {
//                 1 | 2 => expr.clone(),
//                 3.. => {
//                     let inner_node = ast::ExpressionNode::SubNodes(expr.nodes[0..2].to_vec());
//                     let rest = expr.nodes[2..].to_vec();
//                     let mut new_nodes = vec![inner_node];
//                     new_nodes.extend(rest);
//                     let outer_node = ast::Expression { nodes: new_nodes };
//                     associate(&outer_node)
//                 }
//                 _ => unreachable!(),
//             }
//         }

//         let statements = program
//             .statements
//             .iter()
//             .map(|s| match s {
//                 ast::Statement::Expression(expr) => ast::Statement::Expression(associate(expr)),
//                 ast::Statement::Definition(def) => ast::Statement::Definition(ast::Definition {
//                     symbols: def.symbols.clone(),
//                     expression: associate(&def.expression),
//                 }),
//             })
//             .collect();

//         ast::Program {
//             statements,
//             eoi: program.eoi.clone(),
//         }
//     }
// }

// mod runtime {
//     use super::ast;
//     use std::collections::HashMap;
//     use std::rc::Rc;

//     #[derive(Debug)]
//     pub enum BuiltinFn {
//         Const(i64),
//     }

//     #[derive(Debug)]
//     pub enum Function {
//         Builtin(BuiltinFn),
//     }

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

//         // symbol_table.insert(String::from("const"));

//         // fn apply_function(function: &Function, arg: &Function) -> Function {
//         //     match function {
//         //         Function::Builtin(builtin_fn) => match builtin_fn {
//         //             BuiltinFn::Const(value) => Function::Builtin(BuiltinFn::Const(*value)),
//         //         },
//         //     }
//         // }

//         fn evaluate_nonary(
//             table: &HashMap<String, Rc<Function>>,
//             lhs: &ast::ExpressionNode,
//         ) -> Rc<Function> {
//             match lhs {
//                 ast::ExpressionNode::IntegerLiteral(literal) => {
//                     Rc::new(Function::Builtin(BuiltinFn::Const(literal.value)))
//                 }
//                 ast::ExpressionNode::Symbol(symbol) => match symbol.value.as_str() {
//                     "math-zero" => Rc::new(Function::Builtin(BuiltinFn::Const(0))),
//                     sym => (*table.get(sym).expect("symbol not found")).clone(),
//                 },
//                 _ => todo!("_ case in evaluate_nonary"),
//             }
//         }

//         fn evaluate_unary(
//             table: &HashMap<String, Rc<Function>>,
//             lhs: &ast::ExpressionNode,
//             rhs: &ast::ExpressionNode,
//         ) -> Rc<Function> {
//             let rhs_evaled = evaluate_nonary(&table, rhs);
//             todo!("evaluate_unary")
//         }

//         for statement in &ast.statements {
//             let res: Rc<Function> = match statement {
//                 ast::Statement::Expression(expression) => match expression.nodes.len() {
//                     1 => evaluate_nonary(&symbol_table, &expression.nodes[0]),
//                     2 => evaluate_unary(&symbol_table, &expression.nodes[0], &expression.nodes[1]),
//                     _ => unreachable!("expr arity > 2"),
//                 },
//                 ast::Statement::Definition(_) => {
//                     panic!("unsupported statement type: definition")
//                 }
//             };

//             println!("Result: {:#?}", res)
//         }
//     }
// }

fn main() {
    // use ast::Program;
    use from_pest::FromPest;
    use pest::Parser;
    use std::fs;

    let unparsed_file = fs::read_to_string("samples/sample1.code").expect("cannot read file");
    let mut parse_tree =
        parser::Parser::parse(parser::Rule::program, &unparsed_file).expect("unsuccessful parse");

    println!("parse tree = {:#?}", parse_tree);
    // let syntax_tree: Program = Program::from_pest(&mut parse_tree).expect("infallible");
    // println!("syntax tree = {:#?}", syntax_tree);

    // let ir_tree = ir::left_associate_exprs(&syntax_tree);
    // println!("ir tree = {:#?}", ir_tree);

    // runtime::evaluate(&ir_tree);

    // let tokens = program.

    // for token in program.tokens() {
    //     println!("{:?}", token);
    // }

    // println!("{}", program)

    // for statement in program.into_inner() {
    //     match statement.as_rule() {
    //         Rule::statement => {
    //             println!("{}", statement.as_str());
    //         }
    //         Rule::EOI => (),
    //         _ => unreachable!(),
    //     }
    // }

    // println!("Sum of fields: {}", field_sum);
    // println!("Number of records: {}", record_count);
}