small refactoring
This commit is contained in:
parent
00e2260bb3
commit
f393c583b8
1 changed files with 6 additions and 25 deletions
31
src/main.rs
31
src/main.rs
|
|
@ -1,13 +1,15 @@
|
||||||
// src/main.rs
|
|
||||||
mod ast;
|
mod ast;
|
||||||
mod dsl;
|
mod dsl;
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use ast::{
|
use ast::{
|
||||||
ExprAst, ExprAst::Atom, ExprAst::Composition as C, ExprAst::Junction as J, Prim::AddPair,
|
ExprAst,
|
||||||
Prim::Cos, Prim::Input, Prim::Sin, Prim::Triple, ast_input, hash_ast,
|
ExprAst::{Atom, Composition as C, Junction as J},
|
||||||
|
Prim::{AddPair, Cos, Input, Sin, Triple},
|
||||||
|
ast_input, hash_ast,
|
||||||
};
|
};
|
||||||
|
|
||||||
use dsl::{Composition, Expr, Function, Junction};
|
use dsl::{Composition, Expr, Function, Junction};
|
||||||
|
|
||||||
// ========= тестовые функции =========
|
// ========= тестовые функции =========
|
||||||
|
|
@ -22,46 +24,25 @@ fn triple(x: f64) -> f64 {
|
||||||
3.0 * x
|
3.0 * x
|
||||||
}
|
}
|
||||||
|
|
||||||
// (a,b) -> a + b
|
/// (a,b) -> a + b
|
||||||
fn add_pair(p: (f64, f64)) -> f64 {
|
fn add_pair(p: (f64, f64)) -> f64 {
|
||||||
p.0 + p.1
|
p.0 + p.1
|
||||||
}
|
}
|
||||||
|
|
||||||
// ========= демонстрация =========
|
|
||||||
|
|
||||||
// AST для sin(cos(x) + 3*x)
|
// AST для sin(cos(x) + 3*x)
|
||||||
fn pupa() -> ExprAst {
|
fn pupa() -> ExprAst {
|
||||||
// cos(x)
|
|
||||||
let cos_x = C(Box::new(ast_input()), Box::new(Atom(Cos)));
|
let cos_x = C(Box::new(ast_input()), Box::new(Atom(Cos)));
|
||||||
|
|
||||||
// 3 * x
|
|
||||||
let triple_x = C(Box::new(ast_input()), Box::new(Atom(Triple)));
|
let triple_x = C(Box::new(ast_input()), Box::new(Atom(Triple)));
|
||||||
|
|
||||||
// (cos(x), 3*x)
|
|
||||||
let j = J(Box::new(cos_x), Box::new(triple_x));
|
let j = J(Box::new(cos_x), Box::new(triple_x));
|
||||||
|
|
||||||
// AddPair ∘ Junction => cos(x) + 3*x
|
|
||||||
let add = C(Box::new(j), Box::new(Atom(AddPair)));
|
let add = C(Box::new(j), Box::new(Atom(AddPair)));
|
||||||
|
|
||||||
// Sin ∘ (AddPair ∘ Junction)
|
|
||||||
C(Box::new(add), Box::new(Atom(Sin)))
|
C(Box::new(add), Box::new(Atom(Sin)))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn lupa() -> ExprAst {
|
fn lupa() -> ExprAst {
|
||||||
use ast::*;
|
|
||||||
// cos(x)
|
|
||||||
let cos_x = C(Box::new(ast::ast_input()), Box::new(Atom(Cos)));
|
let cos_x = C(Box::new(ast::ast_input()), Box::new(Atom(Cos)));
|
||||||
|
|
||||||
// 3 * x
|
|
||||||
let triple_x = C(Box::new(ast_input()), Box::new(Atom(Triple)));
|
let triple_x = C(Box::new(ast_input()), Box::new(Atom(Triple)));
|
||||||
|
|
||||||
// (cos(x), 3*x)
|
|
||||||
let j = J(Box::new(triple_x), Box::new(cos_x));
|
let j = J(Box::new(triple_x), Box::new(cos_x));
|
||||||
|
|
||||||
// AddPair ∘ Junction => cos(x) + 3*x
|
|
||||||
let add = C(Box::new(j), Box::new(Atom(AddPair)));
|
let add = C(Box::new(j), Box::new(Atom(AddPair)));
|
||||||
|
|
||||||
// Sin ∘ (AddPair ∘ Junction)
|
|
||||||
C(Box::new(add), Box::new(Atom(Sin)))
|
C(Box::new(add), Box::new(Atom(Sin)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue