Skip to content

Commit

Permalink
Merge pull request #116 from sezna/alex/fix-wasm-panics
Browse files Browse the repository at this point in the history
Switch `function` keyword to `fn`; use semicolons for let decls; fix numerous panics in wasm playground
  • Loading branch information
sezna authored Jul 21, 2024
2 parents 5153249 + 95beb4b commit cad2382
Show file tree
Hide file tree
Showing 28 changed files with 423 additions and 356 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion pete/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ thiserror = "1.0"
toml = "0.8"
termcolor = { version = "1.4" }
petr-pkg = { "path" = "../petr-pkg" }

stdlib = { "path" = "../stdlib" }
40 changes: 27 additions & 13 deletions pete/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{
};

use clap::Parser as ClapParser;
use error::PeteError;
use petr_api::*;
use petr_pkg::BuildPlan;
use petr_resolve::Dependency;
Expand All @@ -19,6 +20,8 @@ pub mod error {
TomlSeriatlize(#[from] toml::ser::Error),
#[error(transparent)]
Pkg(#[from] petr_pkg::error::PkgError),
#[error("Failed to lower code")]
FailedToLower,
}
}

Expand Down Expand Up @@ -159,12 +162,24 @@ pub fn compile(
// parse
// construct an interner for symbols, which will be used throughout the whole compilation.
let parser = Parser::new(buf);
let (ast, mut parse_errs, mut interner, mut source_map) = parser.into_result();
let (ast, mut parse_errs, interner, source_map) = parser.into_result();

timings.end("parse user code");
timings.start("parse dependencies");

let mut dependencies = Vec::with_capacity(build_plan.items.len());
let mut dependencies = Vec::with_capacity(build_plan.items.len() + 1);

// add the stdlib
let parser = Parser::new_with_existing_interner_and_source_map(stdlib::stdlib(), interner, source_map);
let (dep_ast, mut new_parse_errs, mut interner, mut source_map) = parser.into_result();
parse_errs.append(&mut new_parse_errs);

dependencies.push(Dependency {
key: "stdlib".to_string(),
name: "std".into(),
dependencies: vec![],
ast: dep_ast,
});

for item in build_plan.items {
let (lockfile, buf, _build_plan) = load_project_and_dependencies(&item.path_to_source)?;
Expand Down Expand Up @@ -198,31 +213,30 @@ pub fn compile(
timings.end("parse dependencies");
timings.end("parsing stage");

render_errors(parse_errs, &source_map);
// errs.append(&mut parse_errs);
// resolve symbols
timings.start("symbol resolution");
let (resolution_errs, resolved) = petr_resolve::resolve_symbols(ast, interner, dependencies);
timings.end("symbol resolution");

// TODO impl diagnostic for resolution errors
if !resolution_errs.is_empty() {
dbg!(&resolution_errs);
}
// errs.append(&mut resolution_errs);

timings.start("type check");
// type check
let (type_errs, type_checker) = petr_typecheck::type_check(resolved);

timings.end("type check");

render_errors(type_errs, &source_map);

timings.start("lowering");
let lowerer: Lowerer = Lowerer::new(type_checker);
let lowerer: Lowerer = match Lowerer::new(type_checker) {
Ok(l) => l,
Err(e) => {
eprintln!("Failed to lower: {:?}", e);
return Err(PeteError::FailedToLower);
},
};
timings.end("lowering");

render_errors(parse_errs, &source_map);
render_errors(type_errs, &source_map);
render_errors(resolution_errs, &source_map);
Ok(lowerer)
}

Expand Down
2 changes: 1 addition & 1 deletion petr-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{
};

pub use petr_fmt::{format_sources, Formattable, FormatterConfig, FormatterContext};
pub use petr_ir::Lowerer;
pub use petr_ir::{Lowerer, LoweringError};
pub use petr_parse::Parser;
#[cfg(not(feature = "no_std"))]
pub use petr_pkg::{manifest::find_manifest, BuildPlan};
Expand Down
12 changes: 6 additions & 6 deletions petr-bind/src/binder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,33 +600,33 @@ mod tests {
#[test]
fn bind_function_decl() {
check(
"function add(a in 'Int, b in 'Int) returns 'Int + 1 2",
"fn add(a in 'Int, b in 'Int) returns 'Int + 1 2",
expect![[r#"
__Scopes__
0: Root (parent none):
test: Module Module { root_scope: ScopeId(1), exports: {} }
1: Module test (parent scopeid0):
add: Function FunctionId(0)
2: Function (parent scopeid1):
a: FunctionParameter Named(Identifier { id: SymbolId(3), span: Span { source: SourceId(0), span: SourceSpan { offset: SourceOffset(19), length: 3 } } })
b: FunctionParameter Named(Identifier { id: SymbolId(3), span: Span { source: SourceId(0), span: SourceSpan { offset: SourceOffset(30), length: 3 } } })
a: FunctionParameter Named(Identifier { id: SymbolId(3), span: Span { source: SourceId(0), span: SourceSpan { offset: SourceOffset(13), length: 3 } } })
b: FunctionParameter Named(Identifier { id: SymbolId(3), span: Span { source: SourceId(0), span: SourceSpan { offset: SourceOffset(24), length: 3 } } })
"#]],
);
}

#[test]
fn bind_list_new_scope() {
check(
"function add(a in 'Int, b in 'Int) returns 'Int [ 1, 2, 3, 4, 5, 6 ]",
"fn add(a in 'Int, b in 'Int) returns 'Int [ 1, 2, 3, 4, 5, 6 ]",
expect![[r#"
__Scopes__
0: Root (parent none):
test: Module Module { root_scope: ScopeId(1), exports: {} }
1: Module test (parent scopeid0):
add: Function FunctionId(0)
2: Function (parent scopeid1):
a: FunctionParameter Named(Identifier { id: SymbolId(3), span: Span { source: SourceId(0), span: SourceSpan { offset: SourceOffset(19), length: 3 } } })
b: FunctionParameter Named(Identifier { id: SymbolId(3), span: Span { source: SourceId(0), span: SourceSpan { offset: SourceOffset(31), length: 3 } } })
a: FunctionParameter Named(Identifier { id: SymbolId(3), span: Span { source: SourceId(0), span: SourceSpan { offset: SourceOffset(13), length: 3 } } })
b: FunctionParameter Named(Identifier { id: SymbolId(3), span: Span { source: SourceId(0), span: SourceSpan { offset: SourceOffset(25), length: 3 } } })
"#]],
);
}
Expand Down
20 changes: 10 additions & 10 deletions petr-fmt/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct FormatterConfig {
tab_size: usize,
max_line_length: usize,
put_fn_args_on_new_lines: bool,
put_trailing_commas_on_let_bindings: bool,
put_trailing_semis_on_let_bindings: bool,
backup: bool,
}

Expand Down Expand Up @@ -60,8 +60,8 @@ impl FormatterConfig {
self.put_fn_args_on_new_lines
}

pub fn put_trailing_commas_on_let_bindings(&self) -> bool {
self.put_trailing_commas_on_let_bindings
pub fn put_trailing_semis_on_let_bindings(&self) -> bool {
self.put_trailing_semis_on_let_bindings
}

pub fn backup(&self) -> bool {
Expand All @@ -81,7 +81,7 @@ impl FormatterConfig {
tab_size: self.tab_size,
max_line_length: self.max_line_length,
put_fn_args_on_new_lines: self.put_fn_args_on_new_lines,
put_trailing_commas_on_let_bindings: self.put_trailing_commas_on_let_bindings,
put_trailing_semis_on_let_bindings: self.put_trailing_semis_on_let_bindings,
backup: self.backup,
}
}
Expand All @@ -105,7 +105,7 @@ pub struct FormatterConfigBuilder {
tab_size: usize,
max_line_length: usize,
put_fn_args_on_new_lines: bool,
put_trailing_commas_on_let_bindings: bool,
put_trailing_semis_on_let_bindings: bool,
backup: bool,
}

Expand Down Expand Up @@ -211,12 +211,12 @@ impl FormatterConfigBuilder {
}
}

pub fn put_trailing_commas_on_let_bindings(
pub fn put_trailing_semis_on_let_bindings(
self,
put_trailing_commas_on_let_bindings: bool,
put_trailing_semis_on_let_bindings: bool,
) -> Self {
Self {
put_trailing_commas_on_let_bindings,
put_trailing_semis_on_let_bindings,
..self
}
}
Expand All @@ -241,7 +241,7 @@ impl FormatterConfigBuilder {
tab_size: self.tab_size,
max_line_length: self.max_line_length,
put_fn_args_on_new_lines: self.put_fn_args_on_new_lines,
put_trailing_commas_on_let_bindings: self.put_trailing_commas_on_let_bindings,
put_trailing_semis_on_let_bindings: self.put_trailing_semis_on_let_bindings,
backup: self.backup,
}
}
Expand All @@ -261,7 +261,7 @@ impl Default for FormatterConfigBuilder {
tab_size: 2,
max_line_length: 80,
put_fn_args_on_new_lines: false,
put_trailing_commas_on_let_bindings: false,
put_trailing_semis_on_let_bindings: false,
backup: false,
}
}
Expand Down
11 changes: 3 additions & 8 deletions petr-fmt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,7 @@ impl Formattable for FunctionDeclaration {
ctx: &mut FormatterContext,
) -> FormattedLines {
let mut lines: Vec<Line> = Vec::new();
let mut buf: String = if self.visibility == Visibility::Exported {
"Function "
} else {
"function "
}
.to_string();
let mut buf: String = if self.visibility == Visibility::Exported { "export fn " } else { "fn " }.to_string();

buf.push_str(&ctx.interner.get(self.name.id));

Expand Down Expand Up @@ -245,11 +240,11 @@ impl Formattable for ExpressionWithBindings {
lines.append(&mut expr_lines[1..].to_vec());
}
// add comma to the end of the last line
if !is_last || ctx.config.put_trailing_commas_on_let_bindings() {
if !is_last || ctx.config.put_trailing_semis_on_let_bindings() {
let last_line = lines.last().expect("invariant");
let last_line_indentation = last_line.indentation;
let mut last_line_content = last_line.content.to_string();
last_line_content.push(',');
last_line_content.push(';');
*(lines.last_mut().expect("invariant")) = Line {
content: Rc::from(last_line_content),
indentation: last_line_indentation,
Expand Down
Loading

0 comments on commit cad2382

Please sign in to comment.