Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: rustfmt #624

Merged
merged 1 commit into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions package/origlang-ast/src/after_parse.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! パース時の優先順位が消去された構造体の定義

use derive_more::Display;
use crate::{Identifier, Statement};
use derive_more::Display;

#[derive(Eq, PartialEq, Clone, Debug)]
pub enum Expression {
Expand All @@ -18,7 +18,7 @@ pub enum Expression {
UnitLiteral,
/// 変数
Variable {
ident: Identifier
ident: Identifier,
},
/// 四則演算、比較演算、等価性判定
BinaryOperator {
Expand All @@ -38,11 +38,15 @@ pub enum Expression {
},
Tuple {
expressions: Vec<Self>,
}
},
}

impl Expression {
pub fn binary<Operator: Into<BinaryOperatorKind>>(operator: Operator, lhs: Self, rhs: Self) -> Self {
pub fn binary<Operator: Into<BinaryOperatorKind>>(
operator: Operator,
lhs: Self,
rhs: Self,
) -> Self {
Self::BinaryOperator {
lhs: Box::new(lhs),
rhs: Box::new(rhs),
Expand Down
10 changes: 5 additions & 5 deletions package/origlang-ast/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#![deny(clippy::all)]
#![warn(clippy::pedantic, clippy::nursery)]

use std::fmt::{Display, Formatter};
use crate::after_parse::Expression;
use std::fmt::{Display, Formatter};

pub mod after_parse;

/// 現時点のプログラムとは、プリントするべき式の列である
#[expect(clippy::module_name_repetitions)]
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct RootAst {
pub statement: Vec<Statement>
pub statement: Vec<Statement>,
}

#[derive(Eq, PartialEq, Clone, Debug)]
Expand Down Expand Up @@ -53,7 +53,7 @@ pub enum Statement {
expression: Expression,
},
Block {
inner_statements: Vec<Self>
inner_statements: Vec<Self>,
},
Comment {
content: Comment,
Expand All @@ -67,7 +67,7 @@ pub enum Statement {

#[derive(Eq, PartialEq, Clone, Debug)]
pub struct Comment {
pub content: String
pub content: String,
}

#[derive(Eq, PartialEq, Clone, Debug, Hash)]
Expand Down Expand Up @@ -121,4 +121,4 @@ impl Display for TypeSignature {
}
}
}
}
}
54 changes: 34 additions & 20 deletions package/origlang-cli/src/args.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
// clap issue?: https://github.com/clap-rs/clap/issues/4733
#![warn(clippy::almost_swapped)]

use crate::error::TaskExecutionError;
use crate::task::emit::UnstableEmit;
use crate::task::interpret::Interpret;
use crate::task::repl::Repl;
use crate::task::Task;
use clap::{Parser, Subcommand};
use std::num::NonZeroUsize;
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;
use crate::error::TaskExecutionError;
use crate::task::emit::UnstableEmit;


#[derive(Parser)]
pub struct Args {
#[clap(subcommand)]
sub_command: SubCom
sub_command: SubCom,
}

pub enum ParseSource {
RawSource(String),
FromFile(PathBuf)
FromFile(PathBuf),
}

#[derive(Debug, Error)]
Expand All @@ -37,19 +36,23 @@ impl ParseSource {
pub fn source(&self) -> Result<String, ReadSourceError> {
match self {
Self::RawSource(a) => Ok(a.clone()),
Self::FromFile(path) => {
Ok(std::fs::read_to_string(path)?)
}
Self::FromFile(path) => Ok(std::fs::read_to_string(path)?),
}
}
}


impl Args {
pub fn execute(self) -> Result<(), TaskExecutionError> {

let load_either = |input_file: Option<PathBuf>, input_source: Option<String>| {
input_file.map_or_else(|| input_source.map_or_else(|| panic!("please specify file or source"), ParseSource::RawSource), ParseSource::FromFile)
input_file.map_or_else(
|| {
input_source.map_or_else(
|| panic!("please specify file or source"),
ParseSource::RawSource,
)
},
ParseSource::FromFile,
)
};

match self.sub_command {
Expand All @@ -58,22 +61,33 @@ impl Args {
task.execute(())?;
Ok(())
}
SubCom::Execute { input_file, input_source, max_stack_size_on_main_thread } => {
SubCom::Execute {
input_file,
input_source,
max_stack_size_on_main_thread,
} => {
let task = Interpret;
let source = load_either(input_file, input_source);
if let Some(ssize) = max_stack_size_on_main_thread {
let a = std::thread::scope(move |a| {
std::thread::Builder::new().stack_size(ssize.get()).spawn_scoped(a, move || {
task.execute(source)
}).unwrap().join()
std::thread::Builder::new()
.stack_size(ssize.get())
.spawn_scoped(a, move || task.execute(source))
.unwrap()
.join()
});
a.map_err(TaskExecutionError::ThreadJoin)??;
} else {
task.execute(source)?;
}
Ok(())
}
SubCom::Emit { emit, input_file, input_source, optimize_level } => {
SubCom::Emit {
emit,
input_file,
input_source,
optimize_level,
} => {
let task = UnstableEmit { phase: emit };
let source = load_either(input_file, input_source);

Expand Down
8 changes: 4 additions & 4 deletions package/origlang-cli/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::any::Any;
use thiserror::Error;
use crate::args::ReadSourceError;
use origlang_parser::error::ParserError;
use origlang_typecheck::type_check::error::TypeCheckError;
use origlang_runtime::RuntimeError;
use crate::args::ReadSourceError;
use origlang_typecheck::type_check::error::TypeCheckError;
use std::any::Any;
use thiserror::Error;

#[derive(Error, Debug)]
#[expect(clippy::module_name_repetitions)]
Expand Down
6 changes: 3 additions & 3 deletions package/origlang-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
#![deny(clippy::all)]
#![warn(clippy::pedantic, clippy::nursery)]

use clap::Parser;
use crate::args::Args;
use crate::error::TaskExecutionError;
use clap::Parser;

mod task;
mod args;
mod error;
mod task;

fn main() -> Result<(), TaskExecutionError> {
let args = Args::parse();
env_logger::init();

if let Err(e) = args.execute() {
log::error!("{e}", e = &e);
return Err(e)
return Err(e);
}
Ok(())
}
2 changes: 1 addition & 1 deletion package/origlang-cli/src/task.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod emit;
pub mod interpret;
pub mod repl;
pub mod emit;

pub trait Task {
type Environment;
Expand Down
26 changes: 13 additions & 13 deletions package/origlang-cli/src/task/emit.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use thiserror::Error;
use crate::args::{EmitPhase, OptimizeLevel, ParseSource, ReadSourceError};
use crate::error::TaskExecutionError;
use crate::task::Task;
use origlang_ir::{IntoVerbatimSequencedIR, IR1, IR2};
use origlang_ir_optimizer::lower::{EachStep, LowerStep, TheTranspiler};
use origlang_ir_optimizer::preset::{NoOptimization, SimpleOptimization};
use origlang_lexer::Lexer;
use origlang_parser::parser::Parser;
use origlang_parser::error::ParserError;
use origlang_parser::parser::Parser;
use origlang_typecheck::type_check::error::TypeCheckError;
use origlang_typecheck::type_check::TypeChecker;
use origlang_ir::{IntoVerbatimSequencedIR, IR1, IR2};
use origlang_ir_optimizer::lower::{EachStep, LowerStep, TheTranspiler};
use origlang_ir_optimizer::preset::{NoOptimization, SimpleOptimization};
use crate::args::{EmitPhase, OptimizeLevel, ParseSource, ReadSourceError};
use crate::error::TaskExecutionError;
use crate::task::Task;
use thiserror::Error;

pub struct UnstableEmit {
pub(crate) phase: EmitPhase,
Expand Down Expand Up @@ -47,7 +47,7 @@ impl Task for UnstableEmit {
let a = lexer.next();
println!("{a:?}");
if a.data.is_error() || a.data.is_end() {
return Ok(())
return Ok(());
}
}
}
Expand All @@ -57,15 +57,15 @@ impl Task for UnstableEmit {

if self.phase == EmitPhase::Ast {
println!("{root:#?}");
return Ok(())
return Ok(());
}

let checker = TypeChecker::new();
let expr = checker.check(root)?;

if self.phase == EmitPhase::TypedAst {
println!("{expr:#?}");
return Ok(())
return Ok(());
}

let ir_sequence = expr.into_ir();
Expand All @@ -81,7 +81,7 @@ impl Task for UnstableEmit {
let ir_sequence: Vec<IR1> = the_lower.optimizer().optimize(ir_sequence);

println!("{ir_sequence:#?}");
return Ok(())
return Ok(());
}

let ir_sequence = the_lower.lower(ir_sequence);
Expand All @@ -94,4 +94,4 @@ impl Task for UnstableEmit {

Ok(())
}
}
}
12 changes: 6 additions & 6 deletions package/origlang-cli/src/task/interpret.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::time::Instant;
use origlang_parser::parser::Parser;
use origlang_runtime::{PrintToStdout, Runtime};
use crate::task::Task;
use crate::args::ParseSource;
use crate::error::TaskExecutionError;
use origlang_typecheck::type_check::TypeChecker;
use crate::task::Task;
use origlang_ir::IntoVerbatimSequencedIR;
use origlang_ir_optimizer::lower::{LowerStep, TheTranspiler};
use origlang_ir_optimizer::preset::NoOptimization;
use crate::args::ParseSource;
use origlang_parser::parser::Parser;
use origlang_runtime::{PrintToStdout, Runtime};
use origlang_typecheck::type_check::TypeChecker;
use std::time::Instant;

pub struct Interpret;

Expand Down
33 changes: 19 additions & 14 deletions package/origlang-cli/src/task/repl.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
use std::fmt::{Debug, Display};
use std::io::{stdout, Write};
use std::ops::Range;
use ariadne::{Report, ReportKind, Source};
use crate::task::Task;
use crate::error::TaskExecutionError;
use origlang_platform::CTRL_D_NL;
use origlang_parser::parser::Parser;
use origlang_parser::error::ParserErrorInner;
use origlang_runtime::{PrintToStdout, Runtime};
use origlang_typecheck::type_check::TypeChecker;
use crate::task::Task;
use ariadne::{Report, ReportKind, Source};
use origlang_ir::{IntoVerbatimSequencedIR, IR2};
use origlang_ir_optimizer::lower::{LowerStep, TheTranspiler};
use origlang_ir_optimizer::preset::NoOptimization;
use origlang_parser::error::ParserErrorInner;
use origlang_parser::parser::Parser;
use origlang_platform::CTRL_D_NL;
use origlang_runtime::{PrintToStdout, Runtime};
use origlang_typecheck::type_check::TypeChecker;
use origlang_typesystem_model::TypedRootAst;
use std::fmt::{Debug, Display};
use std::io::{stdout, Write};
use std::ops::Range;

struct Dummy((), Source);

impl ariadne::Cache<()> for Dummy {
type Storage = String;

fn fetch(&mut self, _id: &()) -> Result<&Source, Box<dyn Debug + '_>> {
Ok(&self.1)
}
Expand Down Expand Up @@ -59,7 +59,7 @@ impl Task for Repl {
dbg!(&line);
// FIXME: this is buggy in Windows, causing infinite loop
if line == CTRL_D_NL {
break
break;
}
let parser = Parser::create(line.as_str());
match parser.parse() {
Expand All @@ -70,7 +70,11 @@ impl Task for Repl {
Err(error_message) => {
let error = error_message.kind();
let handled = false;
if let ParserErrorInner::PartiallyParsed { hint: _, intermediate_state: _ } = &error {
if let ParserErrorInner::PartiallyParsed {
hint: _,
intermediate_state: _,
} = &error
{
// TODO: insta-expression eval
// TODO: revisit this
/*
Expand Down Expand Up @@ -114,7 +118,8 @@ impl Task for Repl {
.with_message(error.to_string())
.finish();

d.write(Dummy((), Source::from(line)), std::io::stderr()).expect("TODO: panic message");
d.write(Dummy((), Source::from(line)), std::io::stderr())
.expect("TODO: panic message");
}
}
}
Expand Down
Loading