Amo/src/main.rs

95 lines
2.5 KiB
Rust
Raw Normal View History

#![feature(try_trait_v2)]
2022-04-06 13:23:46 +00:00
use std::{fs::File, io::Read, collections::LinkedList, process::exit};
2022-03-10 17:56:38 +00:00
use logos::Logos;
2022-04-06 13:23:46 +00:00
use parser::{program::Program, Parsable, WrappedLexer};
2022-03-10 17:56:38 +00:00
use crate::ir::evaluation::{EvaluateError, display_program};
2022-04-25 01:18:52 +00:00
2022-03-10 17:56:38 +00:00
mod token;
2022-04-06 13:23:46 +00:00
mod parser;
mod ir;
2022-03-10 17:56:38 +00:00
fn main() -> std::io::Result<()> {
2022-04-25 16:35:15 +00:00
let mut input_file = std::io::stdin();
2022-03-10 17:56:38 +00:00
let mut input = String::with_capacity(4096);
input_file.read_to_string(&mut input)?;
2022-04-06 13:23:46 +00:00
let lexer = token::Token::lexer(&input);
let mut wrapped_lexer = WrappedLexer::new(lexer);
let program = match Program::parse(&mut wrapped_lexer) {
Err(e) => {
let location = e.location;
eprintln!("Parse error at {location:?}!");
eprintln!("Expected one of:");
for token in e.expected {
eprintln!(" - {token:?}");
}
exit(1);
}
Ok(p) => p
};
2022-03-10 17:56:38 +00:00
2022-04-06 13:23:46 +00:00
println!("Parse successful!!");
2022-04-24 01:52:30 +00:00
for decl in &program.0 {
2022-04-06 13:23:46 +00:00
println!("{decl:?}\n");
}
2022-04-24 01:52:30 +00:00
println!("Building IR...");
match program.gen_ir() {
2022-04-25 01:18:52 +00:00
Ok(program) => {
2022-04-24 01:52:30 +00:00
println!(
"IR built successfully!\n{}",
2022-04-25 01:18:52 +00:00
program.iter()
.map(|decl| format!("\n\n{decl}\n\n"))
2022-04-24 01:52:30 +00:00
.collect::<String>(),
2022-04-25 01:18:52 +00:00
);
match display_program(program) {
2022-04-25 01:18:52 +00:00
Ok(value) =>
println!("Evaluated successfully! Got:\n{value}"),
2022-04-25 01:18:52 +00:00
Err(EvaluateError::UndefinedValue(v)) =>
println!("Hit an error: {v} is undefined"),
Err(EvaluateError::EvaluatingZeroLengthExpr) =>
println!("Hit an error: Tried to evaluate a series of zero instructions"),
Err(EvaluateError::ArgumentCountMismatch(op, real, ex)) =>
println!("Problem while evaluating operation {op:?}: expected {ex} arguments, got {real}"),
Err(EvaluateError::FunctionArgumentCountMismatch(real, ex)) =>
println!("Problem while evaluating a function: expected {ex} arguments, got {real}"),
Err(EvaluateError::TypeMismatch(a, b)) =>
println!("Type mismatch between {a:?} and {b:?}!"),
Err(EvaluateError::NoMain) =>
println!("Huh, there's no main method"),
Err(EvaluateError::MainHasArgs) =>
println!("Your main method has args, but that's not allowed"),
Err(EvaluateError::IncompleteConditional) =>
println!("Uh oh, a conditional somewhere isn't complete!"),
2022-04-25 01:18:52 +00:00
}
}
2022-04-24 01:52:30 +00:00
Err(e) =>
println!("Oh noes! {e:?}"),
}
2022-03-10 17:56:38 +00:00
Ok(())
2022-03-10 01:16:37 +00:00
}
2022-04-06 13:23:46 +00:00
pub fn cons<T>(mut v: Vec<T>, e: T) -> Vec<T> {
v.push(e);
v
}
pub fn cons_ll<T>(mut v: LinkedList<T>, e: T) -> LinkedList<T> {
v.push_back(e);
v
}
pub fn join<T>(mut a: LinkedList<T>, mut b: LinkedList<T>) -> LinkedList<T> {
a.append(&mut b);
a
}
2022-04-24 01:52:30 +00:00
pub fn join_vec<T>(mut a: Vec<T>, mut b: Vec<T>) -> Vec<T> {
a.append(&mut b);
a
}