Skip to content

Commit

Permalink
feat: run now inlines STD as a package preload
Browse files Browse the repository at this point in the history
  • Loading branch information
sigmasoldi3r committed Dec 18, 2023
1 parent 2e86685 commit 278fc2a
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 58 deletions.
87 changes: 48 additions & 39 deletions src/assets/std.saturn
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,37 @@ class Tuple {
}
}

let ext = {
fn split(sep) = (self) => {
sep = sep ?? "%s";
let t = [];
for frag in self->gmatch("([^" ++ sep ++ "]+)") {
t[#?t+1] = frag;
}
return t;
};
fn join(sep) = (self) => table.concat(self, sep);
fn map(f) = (self) => {
let t = [];
for (k, v) in Object.entries(self) {
t[k] = f(v, k, self);
}
return t;
};
fn reduce(f, seed) = (self) => {
for (k, v) in Object.entries(self) {
seed = f(seed, v, k, self);
}
return seed;
};
return {
split,
join,
map,
reduce
};
};

// The RTTI module
class rtti {
fn arguments(*args) {
Expand Down Expand Up @@ -145,13 +176,8 @@ fn abstract() {
}

// STD Custom operators
fn `|>`(left, right) {
return right(left);
}

fn `<|`(left, right) {
return left(right);
}
fn `|>`(left, right) = right(left);
fn `<|`(left, right) = left(right);

fn `::`(arr, elem) {
<extern "Lua">
Expand Down Expand Up @@ -179,40 +205,20 @@ fn `:::`(left, right) {
panic("Operator ::: not supported on this platform");
}

fn `+?`(left, right) {
return left and (left + right);
}
fn `+??`(left, right) {
return left and (left + right) or right;
}
fn `+?`(left, right) = left and (left + right);
fn `+??`(left, right) = left and (left + right) or right;

fn `++?`(left, right) {
return left and (left ++ right);
}
fn `++??`(left, right) {
return left and (left ++ right) or right;
}
fn `++?`(left, right) = left and (left ++ right);
fn `++??`(left, right) = left and (left ++ right) or right;

fn `-?`(left, right) {
return left and (left - right);
}
fn `-??`(left, right) {
return left and (left - right) or right;
}
fn `-?`(left, right) = left and (left - right);
fn `-??`(left, right) = left and (left - right) or right;

fn `*?`(left, right) {
return left and (left * right);
}
fn `*??`(left, right) {
return left and (left * right) or right;
}
fn `*?`(left, right) = left and (left * right);
fn `*??`(left, right) = left and (left * right) or right;

fn `/?`(left, right) {
return left and (left / right);
}
fn `/??`(left, right) {
return left and (left / right) or right;
}
fn `/?`(left, right) = left and (left / right);
fn `/??`(left, right) = left and (left / right) or right;

fn forward_iterator(from, to, step) {
let i = from;
Expand Down Expand Up @@ -252,6 +258,8 @@ fn `..`(from, target) {
return backward_iterator(from, to, step);
}

fn `->>`(self, method) = method(self);

return {
Object,
Tuple,
Expand All @@ -266,6 +274,7 @@ return {
`:::`, `..`, `|>`, `<|`,
`+?`, `+??`, `++?`, `++??`,
`-?`, `-??`, `*?`, `*??`,
`/?`, `/??`, `::`
}
`/?`, `/??`, `::`, `->>`
},
ext
};
64 changes: 45 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{
use clap::Parser;
use code::info::InputFileInfo;
use errors::report_error;
use lua::visitor::LuaEmitter;
use parser::Script;
use runtime::RuntimeError;

Expand All @@ -26,7 +27,7 @@ mod tests;

#[derive(Parser, Clone)]
#[command(name = "Saturnus")]
#[command(version = "v1.1.1")]
#[command(version = "v0.2.0")]
#[command(author = "Pablo B. <pablobc.1995@gmail.com>")]
#[command(
about = "Saturnus: A modern language that compiles to Lua",
Expand Down Expand Up @@ -76,8 +77,9 @@ struct Args {
indent: usize,
#[arg(long, help = "Skips the std library")]
no_std: bool,
#[arg(long, help = "Inline the std library in each script")]
inline_std: bool,
// Now std is inlined atop the entry point!
// #[arg(long, help = "Inline the std library in each script")]
// inline_std: bool,
#[arg(
long,
help = "Outputs the saturnus code to stdout preprocessed but without compiling"
Expand Down Expand Up @@ -122,10 +124,7 @@ struct CompilationOptions {
// }
// }

fn try_run(options: CompilationOptions, input: String, indent: String) -> Result<(), RuntimeError> {
let compiler = lua::visitor::LuaEmitter::new(InputFileInfo {
full_path: PathBuf::from(&options.in_path),
});
fn precompile_std(compiler: &dyn Visitor) -> Result<(String, md5::Digest), RuntimeError> {
// Precompile STD
let std_src = include_str!("assets/std.saturn");
let std_src = Script::parse(std_src.to_owned()).unwrap();
Expand All @@ -134,17 +133,46 @@ fn try_run(options: CompilationOptions, input: String, indent: String) -> Result
.unwrap()
.collect();
let crc = md5::compute(std_src.as_bytes());
Ok((std_src, crc))
}

if !options.args.no_std {
let mut path = std::path::PathBuf::new();
path.push(&options.out_path);
path.pop();
path.push("std.lua");
let r = std::fs::read_to_string(&path).map(|r| md5::compute(r.as_bytes()));
if r.is_err() || r.unwrap() != crc {
std::fs::write(&path, std_src).unwrap();
}
fn runtime_eval(script: &Script, compiler: &dyn Visitor, args: &Args) -> Result<(), RuntimeError> {
let mut src = compiler
.visit_script(Builder::new(" "), &script)
.map_err(|err| RuntimeError::CompilationError(err))?
.collect();
if !args.no_std {
let (std_src, _) = precompile_std(compiler)?;
src = format!(
"package.preload[\"std\"] = function()\n{}\nend;\n{}",
std_src, src
);
}
let lua = rlua::Lua::new();
lua.context(move |ctx| -> rlua::Result<()> {
ctx.load(&src).eval()?;
Ok(())
})
.map_err(|err| RuntimeError::EvaluationError(err))?;
Ok(())
}

fn try_run(options: CompilationOptions, input: String, indent: String) -> Result<(), RuntimeError> {
let compiler = lua::visitor::LuaEmitter::new(InputFileInfo {
full_path: PathBuf::from(&options.in_path),
});

// We won't pop out that pesky "std.lua" file anymore!
// if !options.args.no_std {
// let mut path = std::path::PathBuf::new();
// path.push(&options.out_path);
// path.pop();
// path.push("std.lua");
// let r = std::fs::read_to_string(&path).map(|r| md5::compute(r.as_bytes()));
// if r.is_err() || r.unwrap() != crc {
// std::fs::write(&path, std_src).unwrap();
// }
// }

// scrap_modules(&mut compiler.module_mapping, &options.args.modules);

Expand Down Expand Up @@ -177,9 +205,7 @@ fn try_run(options: CompilationOptions, input: String, indent: String) -> Result
out_file.write_all(output.as_bytes()).unwrap();
}
} else {
let host: runtime::RuntimeHost =
runtime::RuntimeHost::new(indent.clone(), Box::new(compiler));
host.evaluate(&script)?;
runtime_eval(&script, &compiler, &args)?;
}

Ok(())
Expand Down
5 changes: 5 additions & 0 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ pub struct EvaluationOutput {
/// the virtual machine, parser and such.
///
/// This host will take care of evaluating the incoming Saturnus code.
#[deprecated(
since = "v0.2.0",
note = "We have to rethink how runtime is evaluated! - Saturnus's primary VM will be always Lua."
)]
pub struct RuntimeHost {
host: rlua::Lua,
compiler: Box<dyn Visitor>,
Expand All @@ -46,6 +50,7 @@ impl RuntimeHost {
self.evaluate(&parsed)
}

#[deprecated]
pub fn evaluate(&self, script: &Script) -> Result<EvaluationOutput, RuntimeError> {
let code = self
.compiler
Expand Down

0 comments on commit 278fc2a

Please sign in to comment.