Skip to content

Commit a8c2471

Browse files
authored
Merge pull request #105 from The3gs/playground-feature
2 parents 59a8a0e + 5a4e889 commit a8c2471

File tree

13 files changed

+230
-193
lines changed

13 files changed

+230
-193
lines changed

Cargo.toml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@ version = "0.1.0"
44
edition = "2021"
55
repository = "https://github.com/faiface/par-lang"
66

7+
[features]
8+
default = ["playground"]
9+
playground = ["eframe", "egui_code_editor", "rfd"]
10+
711
[dependencies]
812
futures = { version = "0.3.31", features = ["executor", "thread-pool"] }
9-
eframe = { version = "0.32.0", features = ["default", "__screenshot"] }
10-
egui_code_editor = "0.2.17"
13+
eframe = { version = "0.32.0", features = ["default", "__screenshot"], optional = true }
14+
egui_code_editor = { version = "0.2.17", optional = true }
1115
indexmap = "2.7.0"
1216
tokio = { version = "1.43.0", features = ["rt-multi-thread", "macros", "fs", "io-util", "io-std", "signal"] }
1317
stacker = "0.1.19"
14-
rfd = "0.15.2"
18+
rfd = { version = "0.15.2", optional = true }
1519
winnow = { version = "0.7.4", features = [
1620
#"unstable-doc", # build docs locally
1721
#"debug" # debug output/state of parser

src/icombs/net.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ impl core::ops::Add<Rewrites> for Rewrites {
182182
}
183183
}
184184

185+
#[allow(unused)]
185186
impl Rewrites {
186187
pub fn total(&self) -> u128 {
187188
self.commute + self.annihilate + self.signal + self.expand + self.era + self.resp

src/icombs/readback.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ pub(crate) mod private {
5050
self.net.lock()
5151
}
5252

53+
#[cfg(feature = "playground")]
5354
pub(crate) fn net(&self) -> Arc<Mutex<Net>> {
5455
Arc::clone(&self.net)
5556
}
@@ -110,6 +111,7 @@ impl TypedHandle {
110111
}
111112
}
112113

114+
#[allow(dead_code)]
113115
pub enum TypedReadback {
114116
Nat(BigInt),
115117
Int(BigInt),
@@ -407,6 +409,7 @@ impl TypedHandle {
407409
}
408410
}
409411

412+
#[cfg(feature = "playground")]
410413
pub fn net(&self) -> Arc<Mutex<Net>> {
411414
self.net.net()
412415
}

src/language_server/feedback.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl FeedbackBookKeeper {
5555
}
5656

5757
pub fn diagnostic_for_error(err: &CompileError, code: Arc<str>) -> lsp::Diagnostic {
58-
use crate::playground::Error;
58+
use crate::par::build_result::Error;
5959

6060
let (span, message, help, _related_spans) = match err {
6161
CompileError::Compile(Error::Syntax(err)) => (

src/language_server/instance.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use super::io::IO;
22
use crate::location::FileName;
3-
use crate::playground::BuildResult;
3+
use crate::par::build_result::BuildResult;
44
use lsp_types::{self as lsp, Uri};
55
use std::collections::HashMap;
66
use std::fmt::Write;
77

88
#[derive(Debug, Clone)]
99
pub enum CompileError {
10-
Compile(crate::playground::Error),
10+
Compile(crate::par::build_result::Error),
1111
//Types(TypeError<Internal<Name>>),
1212
}
1313

src/main.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
use crate::icombs::readback::{TypedHandle, TypedReadback};
2+
use crate::par::build_result::BuildResult;
23
use crate::par::types::Type;
3-
use crate::playground::{BuildResult, Playground};
4+
#[cfg(feature = "playground")]
5+
use crate::playground::Playground;
46
use crate::spawn::TokioSpawn;
57
use clap::{arg, command, value_parser, Command};
68
use colored::Colorize;
9+
#[cfg(feature = "playground")]
710
use eframe::egui;
811
use futures::task::SpawnExt;
912
use std::fs::File;
13+
#[cfg(feature = "playground")]
1014
use std::io::Write;
1115
use std::path::PathBuf;
1216
use std::sync::Arc;
@@ -15,7 +19,9 @@ mod icombs;
1519
mod language_server;
1620
mod location;
1721
mod par;
22+
#[cfg(feature = "playground")]
1823
mod playground;
24+
#[cfg(feature = "playground")]
1925
mod readback;
2026
mod spawn;
2127
mod test_assertion;
@@ -26,7 +32,11 @@ fn main() {
2632
.subcommand_required(true)
2733
.subcommand(
2834
Command::new("playground")
29-
.about("Start the Par playground")
35+
.about(if cfg!(feature = "playground") {
36+
"Start the Par playground"
37+
} else {
38+
"Disabled in build"
39+
})
3040
.arg(
3141
arg!([file] "Open a Par file in the playground")
3242
.value_parser(value_parser!(PathBuf)),
@@ -90,9 +100,16 @@ fn main() {
90100
}
91101
}
92102

103+
#[cfg(feature = "playground")]
93104
/// String to save on crash. Used by the playground to avoid losing everything on panic.
94105
static CRASH_STR: std::sync::Mutex<Option<String>> = std::sync::Mutex::new(None);
95106

107+
#[cfg(not(feature = "playground"))]
108+
fn run_playground(_: Option<PathBuf>) {
109+
eprintln!("Playground was disabled when building Par")
110+
}
111+
112+
#[cfg(feature = "playground")]
96113
fn run_playground(file: Option<PathBuf>) {
97114
let options = eframe::NativeOptions {
98115
viewport: egui::ViewportBuilder::default().with_inner_size([1000.0, 700.0]),

src/par.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod build_result;
12
pub mod builtin;
23
pub mod language;
34
pub mod lexer;

src/par/build_result.rs

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
use crate::{
2+
icombs::{self, IcCompiled},
3+
par::{language::CompileError, parse::SyntaxError, process::Expression, types::TypeError},
4+
};
5+
use crate::{
6+
location::FileName,
7+
par::{
8+
builtin::import_builtins,
9+
program::{CheckedModule, Definition, Module, ParseAndCompileError, TypeOnHover},
10+
},
11+
};
12+
13+
use std::fmt::Write;
14+
use std::sync::Arc;
15+
16+
#[derive(Debug, Clone)]
17+
pub(crate) enum Error {
18+
Syntax(SyntaxError),
19+
Compile(CompileError),
20+
InetCompile(crate::icombs::compiler::Error),
21+
Type(TypeError),
22+
}
23+
24+
impl Error {
25+
pub fn display(&self, code: Arc<str>) -> String {
26+
match self {
27+
Self::Syntax(error) => {
28+
// Show syntax error with miette's formatting
29+
format!(
30+
"{:?}",
31+
miette::Report::from(error.to_owned()).with_source_code(code)
32+
)
33+
}
34+
35+
Self::Compile(error) => format!("{:?}", error.to_report(code)),
36+
37+
Self::Type(error) => format!("{:?}", error.to_report(code)),
38+
39+
Self::InetCompile(err) => {
40+
format!("inet compilation error: {:?}", err)
41+
}
42+
}
43+
}
44+
}
45+
46+
#[derive(Clone)]
47+
pub enum BuildResult {
48+
None,
49+
SyntaxError {
50+
error: SyntaxError,
51+
},
52+
CompileError {
53+
error: CompileError,
54+
},
55+
TypeError {
56+
#[allow(dead_code)]
57+
pretty: String,
58+
error: TypeError,
59+
},
60+
InetError {
61+
#[allow(dead_code)]
62+
pretty: String,
63+
checked: Arc<CheckedModule>,
64+
type_on_hover: TypeOnHover,
65+
error: icombs::compiler::Error,
66+
},
67+
Ok {
68+
#[allow(dead_code)]
69+
pretty: String,
70+
checked: Arc<CheckedModule>,
71+
type_on_hover: TypeOnHover,
72+
ic_compiled: IcCompiled,
73+
},
74+
}
75+
76+
impl BuildResult {
77+
pub fn error(&self) -> Option<Error> {
78+
match self {
79+
Self::None => None,
80+
Self::SyntaxError { error } => Some(Error::Syntax(error.clone())),
81+
Self::CompileError { error } => Some(Error::Compile(error.clone())),
82+
Self::TypeError { error, .. } => Some(Error::Type(error.clone())),
83+
Self::InetError { error, .. } => Some(Error::InetCompile(error.clone())),
84+
Self::Ok { .. } => None,
85+
}
86+
}
87+
88+
#[cfg(feature = "playground")]
89+
pub fn pretty(&self) -> Option<&str> {
90+
match self {
91+
Self::None => None,
92+
Self::SyntaxError { .. } => None,
93+
Self::CompileError { .. } => None,
94+
Self::TypeError { pretty, .. } => Some(&pretty),
95+
Self::InetError { pretty, .. } => Some(&pretty),
96+
Self::Ok { pretty, .. } => Some(&pretty),
97+
}
98+
}
99+
100+
pub fn checked(&self) -> Option<Arc<CheckedModule>> {
101+
match self {
102+
Self::None => None,
103+
Self::SyntaxError { .. } => None,
104+
Self::CompileError { .. } => None,
105+
Self::TypeError { .. } => None,
106+
Self::InetError { checked, .. } => Some(Arc::clone(checked)),
107+
Self::Ok { checked, .. } => Some(Arc::clone(checked)),
108+
}
109+
}
110+
111+
pub fn type_on_hover(&self) -> Option<&TypeOnHover> {
112+
match self {
113+
Self::None => None,
114+
Self::SyntaxError { .. } => None,
115+
Self::CompileError { .. } => None,
116+
Self::TypeError { .. } => None,
117+
Self::InetError { type_on_hover, .. } => Some(&type_on_hover),
118+
Self::Ok { type_on_hover, .. } => Some(&type_on_hover),
119+
}
120+
}
121+
122+
pub fn ic_compiled(&self) -> Option<&IcCompiled> {
123+
match self {
124+
Self::None => None,
125+
Self::SyntaxError { .. } => None,
126+
Self::CompileError { .. } => None,
127+
Self::TypeError { .. } => None,
128+
Self::InetError { .. } => None,
129+
Self::Ok { ic_compiled, .. } => Some(&ic_compiled),
130+
}
131+
}
132+
133+
pub fn from_source(source: &str, file: FileName) -> Self {
134+
let mut module = match Module::parse_and_compile(source, file) {
135+
Ok(module) => module,
136+
Err(ParseAndCompileError::Parse(error)) => return Self::SyntaxError { error },
137+
Err(ParseAndCompileError::Compile(error)) => return Self::CompileError { error },
138+
};
139+
import_builtins(&mut module);
140+
Self::from_compiled(module)
141+
}
142+
143+
pub fn from_compiled(compiled: Module<Arc<Expression<()>>>) -> Self {
144+
let pretty = compiled
145+
.definitions
146+
.iter()
147+
.map(
148+
|Definition {
149+
span: _,
150+
name,
151+
expression,
152+
}| {
153+
let mut buf = String::new();
154+
write!(&mut buf, "def {} = ", name).expect("write failed");
155+
expression.pretty(&mut buf, 0).expect("write failed");
156+
write!(&mut buf, "\n\n").expect("write failed");
157+
buf
158+
},
159+
)
160+
.collect();
161+
162+
let checked = match compiled.type_check() {
163+
Ok(checked) => Arc::new(checked),
164+
Err(error) => return Self::TypeError { pretty, error },
165+
};
166+
Self::from_checked(pretty, checked)
167+
}
168+
169+
pub fn from_checked(pretty: String, checked: Arc<CheckedModule>) -> Self {
170+
let type_on_hover = TypeOnHover::new(&checked);
171+
let ic_compiled = match icombs::compiler::compile_file(&checked) {
172+
Ok(ic_compiled) => ic_compiled,
173+
Err(error) => {
174+
return Self::InetError {
175+
pretty,
176+
checked,
177+
type_on_hover,
178+
error,
179+
}
180+
}
181+
};
182+
Self::Ok {
183+
pretty,
184+
checked,
185+
type_on_hover,
186+
ic_compiled,
187+
}
188+
}
189+
}

src/par/parse.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ pub fn parse_module(
359359
})
360360
}
361361

362+
#[cfg(feature = "playground")]
362363
pub fn parse_bytes(input: &str, file: &FileName) -> Option<Vec<u8>> {
363364
(literal_bytes_inner, winnow::combinator::eof)
364365
.parse_next(&mut Input::new(&lex(input, file)))

src/par/primitive.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ impl Primitive {
3333
}
3434
}
3535

36+
#[cfg(feature = "playground")]
3637
pub fn pretty_string(&self) -> String {
3738
let mut buf = String::new();
3839
self.pretty(&mut buf, 0).unwrap();

0 commit comments

Comments
 (0)