Skip to content

Commit

Permalink
macros and scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Aug 17, 2022
1 parent 603ec4e commit bcec513
Show file tree
Hide file tree
Showing 13 changed files with 260 additions and 50 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ ctor = "0.1.21"
predicates = "2.1.1"
rand = "0.5.5"
itertools = "0.10.3"
glob = "0.3.0"
2 changes: 2 additions & 0 deletions eo-tests/org/eolang/reo/fibonacci.eo
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

[] > fibonacci

8 > expected

f 6 > @

[x] > f
Expand Down
29 changes: 29 additions & 0 deletions eo-tests/org/eolang/reo/simple/sum.eo
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# The MIT License (MIT)
#
# Copyright (c) 2022 Yegor Bugayenko
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

+package org.eolang.reo.simple

[] > sum

43 > expected

42.plus 1 > @
4 changes: 2 additions & 2 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ impl Data {
self.bytes.len() == 0
}

pub fn as_int(&self) -> Result<u64> {
pub fn as_int(&self) -> Result<i64> {
let a : &[u8; 8] = &self.bytes
.as_slice()
.try_into()
.context(format!("There is no data, can't make INT"))?;
Ok(u64::from_be_bytes(*a))
Ok(i64::from_be_bytes(*a))
}

pub fn as_float(&self) -> Result<f64> {
Expand Down
65 changes: 54 additions & 11 deletions src/gmi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@
use crate::universe::Universe;
use std::fs;
use std::collections::HashMap;
use std::path::Path;
use anyhow::{anyhow, Context, Result};
use std::str::FromStr;
use lazy_static::lazy_static;
use regex::Regex;
use crate::data::Data;
use itertools::Itertools;
use glob::glob;
use crate::da;
use crate::org::eolang::register;

/// Collection of GMIs, which can be deployed to a `Universe`.
pub struct Gmi {
Expand All @@ -36,8 +40,11 @@ pub struct Gmi {

impl Gmi {
/// Read them from a file.
pub fn from_file(file: &str) -> Result<Gmi> {
return Gmi::from_string(fs::read_to_string(file)?);
pub fn from_file(file: &Path) -> Result<Gmi> {
return Gmi::from_string(
fs::read_to_string(file)
.context(format!("Can't read from \"{}\"", file.display()))?
);
}

/// Read them from a string.
Expand All @@ -55,7 +62,7 @@ impl Gmi {
let lines = txt.split("\n").map(|t| t.trim()).filter(|t| !t.is_empty());
for (pos, t) in lines.enumerate() {
self.deploy_one(t, uni).context(
format!("Failure at the line no.{}: \"{}\"", pos, t)
format!("Failure at the line no.{}: '{}'", pos, t)
)?;
}
Ok(())
Expand All @@ -74,7 +81,7 @@ impl Gmi {
"(^|\\.)\\$"
).unwrap();
}
let cap = LINE.captures(line).context(format!("Can't parse \"{}\"", line))?;
let cap = LINE.captures(line).context(format!("Can't parse '{}'", line))?;
let args : Vec<&str> = ARGS.captures_iter(&cap[2])
.map(|c| c.get(1).unwrap().as_str())
.collect();
Expand Down Expand Up @@ -107,6 +114,11 @@ impl Gmi {
let v = self.parse(&args[0], uni)?;
uni.data(v, Self::parse_data(&args[1])?);
},
"ATOM" => {
let v = self.parse(&args[0], uni)?;
let m = &args[1];
uni.atom(v, m);
},
_cmd => {
return Err(anyhow!("Unknown GMI: {}", _cmd))
}
Expand Down Expand Up @@ -134,14 +146,14 @@ impl Gmi {
} else {
let (t, tail) = d.splitn(2, "/")
.collect_tuple()
.context(format!("Strange data format: \"{}\"", d))?;
.context(format!("Strange data format: '{}'", d))?;
match t {
"string" => Data::from_string(tail.to_string()),
"int" => Data::from_int(i64::from_str(tail)?),
"float" => Data::from_float(f64::from_str(tail)?),
"bool" => Data::from_bool(tail == "true"),
_ => {
return Err(anyhow!("Unknown type of data \"{}\"", t))
return Err(anyhow!("Unknown type of data '{}'", t))
}
}
};
Expand All @@ -155,7 +167,7 @@ impl Gmi {
if s.chars().next().unwrap() == '$' {
Ok(*self.vars.entry(tail.to_string()).or_insert(uni.next_id()))
} else {
Ok(u32::from_str(tail.as_str()).context(format!("Parsing of \"{}\" failed", s))?)
Ok(u32::from_str(tail.as_str()).context(format!("Parsing of '{}' failed", s))?)
}
}
}
Expand All @@ -173,15 +185,46 @@ fn deploys_simple_commands() {
DATA('ν2', 'd0 bf d1 80 d0 b8 d0 b2 d0 b5 d1 82');
".to_string()
).unwrap().deploy_to(uni).unwrap();
assert_eq!("привет", uni.dataize(0, "foo.Δ").unwrap().as_string().unwrap());
assert_eq!("привет", da!(uni, "Φ.foo").as_string().unwrap());
}

#[test]
fn deploys_fibonacci() -> Result<()> {
fn deploys_and_runs() -> Result<()> {
for f in glob("eo-tests/**/*.eo")? {
let p = f?;
let path = p.as_path();
let app = path.file_name()
.context(format!("Can't find file name in '{}'", path.display()))?
.to_str()
.context(format!("Can't turn path '{}' into string", path.display()))?
.split(".")
.nth(0)
.context(format!("Can't find body name in '{}'", path.display()))?;
let pkg = path.parent()
.context(format!("Can't get parent from '{}'", path.display()))?
.to_str()
.context(format!("Can't get str from '{}'", path.display()))?
.splitn(2, "/")
.nth(1)
.context(format!("Can't take path from '{}'", path.display()))?;
deploys_and_runs_one_app(
app,
Path::new(format!("target/eo/gmi/{}/{}.gmi", pkg, app).as_str())
)?;
}
Ok(())
}

#[cfg(test)]
fn deploys_and_runs_one_app(name: &str, path: &Path) -> Result<()> {
let uni : &mut Universe = &mut Universe::empty();
uni.add(0);
Gmi::from_file("target/eo/gmi/org/eolang/reo/fibonacci.gmi")?.deploy_to(uni)?;
register(uni);
Gmi::from_file(path)?.deploy_to(uni)?;
println!("{uni:?}");
assert_eq!(8, uni.dataize(0, "fibonacci.f.Δ")?.as_int()?);
assert_eq!(
da!(uni, format!("Φ.{}.expected", name)).as_int()?,
da!(uni, format!("Φ.{}", name)).as_int()?
);
Ok(())
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
pub mod data;
pub mod gmi;
pub mod universe;
pub mod org;
pub mod macros;
pub mod scripts;

#[cfg(test)]
use simple_logger::SimpleLogger;
Expand Down
26 changes: 26 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2022 Yegor Bugayenko
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#[macro_export]
macro_rules! da {
($uni:expr, $loc:expr) => {
$uni.dataize(format!("{}.Δ", $loc).as_str()).unwrap()
};
}
11 changes: 11 additions & 0 deletions src/org/eolang/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

use crate::universe::Universe;
use anyhow::Result;
use crate::da;
use crate::scripts::copy_of_int;

/// EO atom `int.plus`
pub fn int_plus(uni: &mut Universe, v: u32) -> Result<u32> {
let rho = da!(uni, format!("ν{}.ρ", v)).as_int()?;
let x = da!(uni, format!("ν{}.α0", v)).as_int()?;
Ok(copy_of_int(uni, rho + x))
}

#[test]
fn simple() {
Expand Down
30 changes: 30 additions & 0 deletions src/org/eolang/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) 2022 Yegor Bugayenko
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

mod int;

use crate::org::eolang::int::int_plus;
use crate::universe::Universe;

/// Register all atoms in the universe.
pub fn register(uni: &mut Universe) {
uni.register("int.plus", int_plus);
}

21 changes: 21 additions & 0 deletions src/org/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) 2022 Yegor Bugayenko
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

pub mod eolang;
37 changes: 37 additions & 0 deletions src/scripts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2022 Yegor Bugayenko
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

use crate::data::Data;
use crate::universe::Universe;

pub fn copy_of_int(uni: &mut Universe, data: i64) -> u32 {
let e = uni.next_id();
uni.reff(e, 0, "𝜉.int", "i");
let i = uni.next_id();
uni.add(i);
let e2 = uni.next_id();
uni.copy(e, i, e2);
let d = uni.next_id();
uni.add(d);
let e3 = uni.next_id();
uni.bind(e3, i, d, "Δ");
uni.data(d, Data::from_int(data));
i
}

0 comments on commit bcec513

Please sign in to comment.