|
| 1 | +use crate::compiler::ast::{Expr, JsonElement, Stmt}; |
| 2 | +use crate::compiler::lexer::{Token, TokenPos}; |
| 3 | + |
| 4 | +struct Template { |
| 5 | + name: String, // Can be identifiers or operator names (like `+`) |
| 6 | + args: Vec<String>, |
| 7 | + expr: Expr, |
| 8 | +} |
| 9 | + |
| 10 | +pub struct Compiler { |
| 11 | + templates: Vec<Template>, |
| 12 | + template_args: Vec<(String, JsonElement)>, |
| 13 | + |
| 14 | + output_functions: Vec<(String, JsonElement)>, |
| 15 | + |
| 16 | + had_error: bool, panic_mode: bool, |
| 17 | +} |
| 18 | + |
| 19 | +impl Compiler { |
| 20 | + pub fn new() -> Compiler { |
| 21 | + Compiler { |
| 22 | + templates: vec![], |
| 23 | + template_args: vec![], |
| 24 | + |
| 25 | + output_functions: vec![], |
| 26 | + |
| 27 | + had_error: false, panic_mode: false, |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + pub fn compile(mut self, statements: Vec<Stmt>) -> Vec<(String, JsonElement)> { |
| 32 | + for stmt in statements { |
| 33 | + self.compile_statement(stmt); |
| 34 | + } |
| 35 | + |
| 36 | + self.output_functions |
| 37 | + } |
| 38 | + |
| 39 | + fn compile_statement(&mut self, stmt: Stmt) { |
| 40 | + match stmt { |
| 41 | + Stmt::Template { name, args, expr } => self.compile_template(name, args, expr), |
| 42 | + Stmt::Function { name, expr } => self.compile_function(name, expr), |
| 43 | + Stmt::Error => {}, |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + fn compile_template(&mut self, name: Token, args: Vec<Token>, expr: Expr) { |
| 48 | + if self.templates.iter().any(|template| *template.name == *name.source() && template.args.len() == args.len()) { |
| 49 | + self.error_at(Some(*name.start()), "Tried to define multiple templates with the same name", false); |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + self.templates.push(Template { |
| 54 | + name: name.source().to_owned(), |
| 55 | + args: args.iter().map(|arg| arg.source().to_owned()).collect(), |
| 56 | + expr |
| 57 | + }) |
| 58 | + } |
| 59 | + |
| 60 | + fn compile_function(&mut self, name: Token, expr: Expr) { |
| 61 | + if self.output_functions.iter().any(|(function_name, _)| *function_name == *name.source()) { |
| 62 | + self.error_at(Some(*name.start()), "Tried to define multiple density functions with the same name", false); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + let expr = self.compile_expr(expr); |
| 67 | + self.output_functions.push((name.source().to_owned(), expr)); |
| 68 | + } |
| 69 | + |
| 70 | + fn compile_expr(&mut self, expr: Expr) -> JsonElement { |
| 71 | + match expr { |
| 72 | + Expr::ConstantFloat(value) => JsonElement::ConstantFloat(value), |
| 73 | + Expr::ConstantInt(value) => JsonElement::ConstantInt(value), |
| 74 | + Expr::ConstantString(value) => JsonElement::ConstantString(value), |
| 75 | + Expr::Identifier(id) => { |
| 76 | + if let Some((_, element)) = self.template_args.iter().rfind(|(name, _)| *name == *id.source()) { |
| 77 | + element.clone() |
| 78 | + } else { |
| 79 | + self.error_at(Some(*id.start()), &format!("Unresolved reference: {}", id.source()), false); |
| 80 | + JsonElement::ConstantString(id.source().to_owned()) |
| 81 | + } |
| 82 | + }, |
| 83 | + Expr::Group(expr) => self.compile_expr(*expr), |
| 84 | + Expr::UnaryOperator { operator, expr } => { |
| 85 | + let compiled_expr = self.compile_expr(*expr); |
| 86 | + |
| 87 | + self.evaluate_template(None, operator, vec![compiled_expr]) |
| 88 | + }, |
| 89 | + Expr::BinaryOperator { left, operator, right } => { |
| 90 | + let compiled_left = self.compile_expr(*left); |
| 91 | + let compiled_right = self.compile_expr(*right); |
| 92 | + |
| 93 | + self.evaluate_template(None, operator, vec![compiled_left, compiled_right]) |
| 94 | + }, |
| 95 | + Expr::FunctionCall { receiver, name, args } => { |
| 96 | + let compiled_receiver = receiver.map(|receiver| self.compile_expr(*receiver)); |
| 97 | + let compiled_args = args.into_iter().map(|arg| self.compile_expr(arg)).collect(); |
| 98 | + |
| 99 | + self.evaluate_template(compiled_receiver, name, compiled_args) |
| 100 | + }, |
| 101 | + Expr::Member { name, .. } => { |
| 102 | + self.error_at(Some(*name.start()), &format!("Unresolved reference: {}", name.source()), false); |
| 103 | + JsonElement::Error |
| 104 | + }, |
| 105 | + Expr::Object(fields) => { |
| 106 | + JsonElement::Object(fields.into_iter().map(|(name, field)| (name.source().to_owned(), self.compile_expr(field))).collect()) |
| 107 | + }, |
| 108 | + Expr::Array(elements) => { |
| 109 | + JsonElement::Array(elements.into_iter().map(|element| self.compile_expr(element)).collect()) |
| 110 | + }, |
| 111 | + Expr::Error => JsonElement::Error, |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + fn evaluate_template(&mut self, receiver: Option<JsonElement>, name: Token, args: Vec<JsonElement>) -> JsonElement { |
| 116 | + if let Some(_) = receiver { |
| 117 | + self.error_at(Some(*name.start()), "Member functions are not implemented yet", false); |
| 118 | + } |
| 119 | + |
| 120 | + let template = match self.templates.iter().find(|template| *template.name == *name.source() && template.args.len() == args.len()) { |
| 121 | + Some(template) => template, |
| 122 | + None => { |
| 123 | + self.error_at(Some(*name.start()), &format!("Unresolved function call: {}", name.source()), false); |
| 124 | + return JsonElement::Error; |
| 125 | + }, |
| 126 | + }; |
| 127 | + |
| 128 | + let arg_count = template.args.len(); |
| 129 | + |
| 130 | + for i in 0..arg_count { |
| 131 | + let name = template.args[i].clone(); |
| 132 | + let element = args[i].clone(); |
| 133 | + |
| 134 | + self.template_args.push((name, element)); |
| 135 | + } |
| 136 | + |
| 137 | + let expr = self.compile_expr(template.expr.clone()); |
| 138 | + self.template_args.truncate(self.template_args.len().saturating_sub(arg_count)); |
| 139 | + |
| 140 | + expr |
| 141 | + } |
| 142 | + |
| 143 | + pub fn had_error(&self) -> bool { |
| 144 | + self.had_error |
| 145 | + } |
| 146 | + |
| 147 | + fn error_at(&mut self, pos: Option<TokenPos>, message: &str, panic: bool) { |
| 148 | + Self::error_at_impl(&mut self.had_error, &mut self.panic_mode, pos, message, panic); |
| 149 | + } |
| 150 | + |
| 151 | + fn error_at_impl(had_error: &mut bool, panic_mode: &mut bool, pos: Option<TokenPos>, message: &str, panic: bool) { |
| 152 | + if *panic_mode { |
| 153 | + return; |
| 154 | + } else if panic { |
| 155 | + *panic_mode = true; |
| 156 | + } |
| 157 | + |
| 158 | + if let Some(pos) = pos { |
| 159 | + eprint!("{} Error: ", pos); |
| 160 | + } else { |
| 161 | + eprint!("Error': "); |
| 162 | + } |
| 163 | + |
| 164 | + eprintln!("{}", message); |
| 165 | + *had_error = true; |
| 166 | + } |
| 167 | +} |
0 commit comments