Skip to content

Commit

Permalink
Merge pull request #299 from KisaragiEffective/feat/arg/not-panic
Browse files Browse the repository at this point in the history
  • Loading branch information
KisaragiEffective authored Oct 22, 2023
2 parents 68b588e + ce9fc0a commit f9a83a6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
21 changes: 16 additions & 5 deletions package/origlang-cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
use std::fs::File;
use std::io::{BufReader, Read};
use std::path::PathBuf;
use std::string::FromUtf8Error;
use clap::{Parser, Subcommand};
use strum::EnumString;
use thiserror::Error;
use crate::task::interpret::Interpret;
use crate::task::repl::Repl;
use crate::task::Task;
Expand All @@ -24,14 +26,23 @@ pub enum ParseSource {
FromFile(PathBuf)
}

#[derive(Debug, Error)]
pub enum ReadSourceError {
#[error("Invalid UTF-8 was given: {0}")]
MalformedUtf8Sequence(#[from] FromUtf8Error),
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
}

impl ParseSource {
pub fn source(&self) -> String {
pub fn source(&self) -> Result<String, ReadSourceError> {
match self {
Self::RawSource(a) => a.clone(),
Self::RawSource(a) => Ok(a.clone()),
Self::FromFile(path) => {
let mut buf = String::new();
BufReader::new(File::open(path).unwrap()).read_to_string(&mut buf).unwrap();
buf
let mut buf = Vec::new();
BufReader::new(File::open(path).unwrap()).read_to_end(&mut buf)?;
let src = String::from_utf8(buf)?;
Ok(src)
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions package/origlang-cli/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ use thiserror::Error;
use origlang_compiler::parser::SimpleErrorWithPos;
use origlang_compiler::type_check::error::TypeCheckError;
use origlang_runtime::RuntimeError;
use crate::args::ReadSourceError;

#[derive(Error, Debug)]
#[allow(clippy::module_name_repetitions)]
pub enum TaskExecutionError {
#[error("Failed to read source: {0}")]
Source(#[from] ReadSourceError),
#[error("{0}")]
Generic(#[from] SimpleErrorWithPos),
#[error("{0}")]
Expand Down
7 changes: 5 additions & 2 deletions package/origlang-cli/src/task/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use origlang_compiler::type_check::TypeChecker;
use origlang_ir::{IR1, IR2, IntoVerbatimSequencedIR};
use origlang_ir_optimizer::lower::{EachStep, LowerStep, TheTranspiler};
use origlang_ir_optimizer::preset::{NoOptimization, SimpleOptimization};
use crate::args::{EmitPhase, OptimizeLevel, ParseSource};
use crate::args::{EmitPhase, OptimizeLevel, ParseSource, ReadSourceError};
use crate::error::TaskExecutionError;
use crate::task::Task;

Expand All @@ -16,6 +16,8 @@ pub struct UnstableEmit {

#[derive(Error, Debug)]
pub enum EmitError {
#[error("source: {0}")]
Source(#[from] ReadSourceError),
#[error("parser: {0}")]
Parser(#[from] SimpleErrorWithPos),
#[error("type check: {0}")]
Expand All @@ -27,6 +29,7 @@ impl From<EmitError> for TaskExecutionError {
match value {
EmitError::Parser(e) => Self::Generic(e),
EmitError::TypeCheck(e) => Self::TypeCheck(e),
EmitError::Source(e) => Self::Source(e),
}
}
}
Expand All @@ -36,7 +39,7 @@ impl Task for UnstableEmit {
type Error = EmitError;

fn execute(&self, (environment, optimize_level): Self::Environment) -> Result<(), Self::Error> {
let src = environment.source();
let src = environment.source()?;
if self.phase == EmitPhase::LexerToken {
let lexer = Lexer::create(&src);
loop {
Expand Down
2 changes: 1 addition & 1 deletion package/origlang-cli/src/task/interpret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl Task for Interpret {
fn execute(&self, environment: Self::Environment) -> Result<(), Self::Error> {
let i = Instant::now();
eprintln!("init: {:?}", i.elapsed());
let source = environment.source();
let source = environment.source()?;
eprintln!("source: {:?}", i.elapsed());
let parser = Parser::create(source.as_str());
eprintln!("parser.ctor: {:?}", i.elapsed());
Expand Down

0 comments on commit f9a83a6

Please sign in to comment.