-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve code generation by hooking into SWIPL FLI
SWIPL provides a FFI interface called the FLI that can make code generation more ergonomic and less fallible. This PR uses that interface. There are some rough edges in the library I used (cc terminusdb-labs/swipl-rs#4), but it's still a lot better than generating text and parsing SWIPL's output as text (which would probably not work if an exception is raised). There is still a bit more that needs to be done in terms of ergonomics and error handling, but this prototype is at a pretty good state at the moment. This commit is a squash of the following commits: - [WIP] Improved codegen - Revamp prelude/builtins, move save state to target/ - Remove unnecessary primitive, unnecessary dependencies, and fix clippy warnings - Undo some parser/deps changes - Revert the previous - Remove merge artifact - Fix code for new AST Co-authored-by: Caden Haustein <code@brightlysalty.33mail.com>
- Loading branch information
1 parent
b7f1cef
commit f0ac83f
Showing
11 changed files
with
427 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use std::env; | ||
use std::path::Path; | ||
use std::process::Command; | ||
|
||
use swipl_info::get_swipl_info; | ||
|
||
fn main() { | ||
println!("cargo:rerun-if-changed=build.rs"); | ||
println!("cargo:rerun-if-changed=mlatu.pl"); | ||
println!("cargo:rustc-link-arg=-Wl,-rpath,{}", get_swipl_info().lib_dir); | ||
|
||
let out_dir = env::var_os("OUT_DIR").unwrap(); | ||
let dest_path = Path::new(&out_dir).join("mlatu.pl.save"); | ||
Command::new("swipl").arg("-o") | ||
.arg(dest_path) | ||
.arg("--goal=true") | ||
.arg("-c") | ||
.arg("mlatu.pl") | ||
.spawn() | ||
.expect("spawn") | ||
.wait() | ||
.expect("wait"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
:- module(mlatu, [rewrite/2, user_equiv/2]). | ||
:- dynamic([user_equiv/2], [multifile, discontiguous]). | ||
|
||
equiv(Xs, Other) :- | ||
user_equiv(Xs, Other) ; | ||
builtin_equiv(Xs, Other). | ||
|
||
builtin_equiv(['d',X|Xs], Other) :- | ||
equiv([X,X|Xs], Other). | ||
|
||
builtin_equiv(['r',_|Xs], Other) :- | ||
equiv(Xs, Other). | ||
|
||
builtin_equiv(['u',Quote|Xs], Other) :- | ||
append(Quote, Xs, NewXs), | ||
equiv(NewXs, Other). | ||
|
||
builtin_equiv(['q',X|Xs], Other) :- | ||
equiv([[X]|Xs], Other). | ||
|
||
builtin_equiv(['c',Quote1,Quote2|Xs], Other) :- | ||
append(Quote1, Quote2, NewQuote), | ||
equiv([NewQuote|Xs], Other). | ||
|
||
builtin_equiv(['s',A,B|Xs], Other) :- | ||
equiv([B,A|Xs],Other). | ||
|
||
builtin_equiv([], []). | ||
builtin_equiv([X|Xs], Other) :- | ||
equiv(Xs, New), | ||
(Xs \= New -> | ||
equiv([X|New], Other) ; | ||
Other = [X|Xs]). | ||
|
||
rewrite(List, Other) :- | ||
equiv(List, Other), | ||
List \= Other. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,12 @@ | ||
q = ( ) const; | ||
swap = q dip; | ||
concat = c; | ||
unquote = u; | ||
quote = q; | ||
swap = s; | ||
remove = r; | ||
dup = d; | ||
|
||
prepend = swap quote swap concat; | ||
p = prepend; | ||
|
||
unquote-before = swap quote cat unquote; | ||
uqb = unquote-before; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,96 @@ | ||
use async_std::fs::OpenOptions; | ||
use async_std::io; | ||
use async_std::io::prelude::*; | ||
use async_std::path::PathBuf; | ||
use async_std::prelude::*; | ||
use mlatu::parse_rules; | ||
use mlatu::{parse_rules, parse_terms, prolog}; | ||
use prolog::util::AssertLocation; | ||
use prolog::{codegen, ContextExt}; | ||
|
||
// TODO: actual CLI, better REPL (w/ rustyline) | ||
// TODO: Bring back structural editing | ||
// BUG: Ctrl-C forwards to SWIPl | ||
#[async_std::main] | ||
async fn main() -> std::io::Result<()> { | ||
// let path = if let Some(filename) = std::env::args().nth(2) { | ||
// PathBuf::from(filename) | ||
// } else { | ||
// PathBuf::from("./prelude.mlt") | ||
// }; | ||
// let mut file = | ||
// OpenOptions::new().read(true).write(true).create(true).open(path).await?; | ||
// let mut s = String::new(); | ||
// let _ = file.read_to_string(&mut s).await?; | ||
// match parse_rules(&s) { | ||
// | Ok(rules) => Editor::new(file, rules).unwrap().run().await, | ||
// | Err(error) => eprintln!("{}", error), | ||
// } | ||
Ok(()) | ||
let engine = mlatu::prolog::init_engine(); | ||
let ctx:prolog::Context<'_, _> = engine.activate().into(); | ||
|
||
let path = if let Some(filename) = std::env::args().nth(2) { | ||
PathBuf::from(filename) | ||
} else { | ||
PathBuf::from("./prelude.mlt") | ||
}; | ||
let mut file = OpenOptions::new().read(true).write(true).create(true).open(path).await?; | ||
let mut s = String::new(); | ||
let _ = file.read_to_string(&mut s).await?; | ||
let rules = match parse_rules(&s) { | ||
| Ok(rules) => rules, | ||
| Err(error) => { | ||
eprintln!("{}", error); | ||
return Ok(()) | ||
}, | ||
}; | ||
|
||
let module = prolog::Module::new("mlatu"); | ||
|
||
let clauses = match codegen::generate(&ctx, &rules) { | ||
| Ok(clauses) => clauses, | ||
| Err(error) => { | ||
eprintln!("Error while compiling rules: {}", error); | ||
return Ok(()) | ||
}, | ||
}; | ||
|
||
if let Err(error) = | ||
clauses.into_iter() | ||
.try_for_each(|clause| ctx.assert(&clause.clause, Some(&module), AssertLocation::Last)) | ||
{ | ||
eprintln!("Error while compiling rules: {}", error); | ||
return Ok(()) | ||
}; | ||
|
||
let stdin = io::stdin(); | ||
let mut stdout = io::stdout(); | ||
|
||
loop { | ||
let mut contents = String::new(); | ||
print!(">>> "); | ||
stdout.flush().await?; | ||
stdin.read_line(&mut contents).await?; | ||
|
||
if contents.trim() == "exit" || contents.trim() == "quit" || contents.is_empty() { | ||
println!("Goodbye!"); | ||
break Ok(()) | ||
} | ||
|
||
let terms = match parse_terms(&contents) { | ||
| Ok(terms) => terms, | ||
| Err(error) => { | ||
eprintln!("{}", error); | ||
continue | ||
}, | ||
}; | ||
|
||
let (list, other) = match codegen::generate_query(&ctx, &terms) { | ||
| Ok(q) => q, | ||
| Err(error) => { | ||
eprintln!("Error while compiling query: {}", error); | ||
continue | ||
}, | ||
}; | ||
|
||
if let Err(error) = ctx.call_once(prolog::pred!(mlatu: rewrite / 2), [&list, &other]) { | ||
eprintln!("Error while executing query: {}", error); | ||
continue | ||
} | ||
// TODO: proper printing | ||
let canon = match ctx.canonical(&other) { | ||
| Some(s) => s, | ||
| None => { | ||
eprintln!("Could not format output"); | ||
continue | ||
}, | ||
}; | ||
|
||
println!("==> {}", canon); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
pub use swipl::fli; | ||
pub use swipl::prelude::*; | ||
|
||
pub mod codegen; | ||
pub mod util; | ||
pub use util::ContextExt; | ||
|
||
static SAVED_STATE:&[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/mlatu.pl.save")); | ||
|
||
#[must_use] | ||
pub fn init_engine() -> Engine { | ||
let bytes = SAVED_STATE.as_ptr(); | ||
// SAFETY: This is called *before* `PL_initialise` (called when creating an | ||
// engine). This data is also valid and the length is correct, as we take the | ||
// pointer and length from a known valid slice. | ||
let len = u64::try_from(SAVED_STATE.len()).expect("length"); | ||
unsafe { | ||
fli::PL_set_resource_db_mem(bytes, len); | ||
} | ||
|
||
Engine::new() | ||
} |
Oops, something went wrong.