Skip to content

Commit

Permalink
Merge pull request #75 from sezna/alex/general-updates
Browse files Browse the repository at this point in the history
some general test updates
  • Loading branch information
sezna authored Jun 29, 2024
2 parents 7b90c61 + 16cbd8d commit aa75348
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 108 deletions.
4 changes: 3 additions & 1 deletion petr-bind/src/binder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,9 @@ impl Binder {
petr_ast::AstNode::ImportStatement(stmt) => stmt.bind(binder),
});
let exports = BTreeMap::from_iter(exports);
// TODO do I need to track this module id?
// we don't need to track this module ID -- it just needs to exist,
// and all modules are iterated over in later stages of the compiler.
// So we can safely ignore the return value here.
let _module_id = binder.modules.insert(Module {
root_scope: scope_id,
exports,
Expand Down
21 changes: 8 additions & 13 deletions petr-bind/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,14 @@ impl Bind for Expression {
bindings,
expression,
expr_id,
}) => {
binder.with_scope(ScopeKind::ExpressionWithBindings, |binder, scope_id| {
for binding in bindings.iter() {
let binding_id = binder.insert_binding(binding.clone());
binder.insert_into_current_scope(binding.name.id, Item::Binding(binding_id));
}
// TODO: functions get inserted as Items with scopes, so we should probably
// insert bound expressions as an Item with their own scope, not sure how yet.
expression.bind(binder);
binder.insert_expression(*expr_id, scope_id);
//
})
},
}) => binder.with_scope(ScopeKind::ExpressionWithBindings, |binder, scope_id| {
for binding in bindings.iter() {
let binding_id = binder.insert_binding(binding.clone());
binder.insert_into_current_scope(binding.name.id, Item::Binding(binding_id));
}
expression.bind(binder);
binder.insert_expression(*expr_id, scope_id);
}),
_ => (),
}
}
Expand Down
2 changes: 1 addition & 1 deletion petr-ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ impl Lowerer {
let func_label = self.new_function_label();
let mut buf = vec![];
self.with_variable_context(|ctx| -> Result<_, _> {
// TODO: func should have type checked types...not just the AST type
// Pop parameters off the stack in reverse order -- the last parameter for the function
// will be the first thing popped off the stack
// When we lower a function call, we push them onto the stack from first to last. Since
Expand Down Expand Up @@ -223,6 +222,7 @@ impl Lowerer {
buf.append(&mut expr);
Ok(buf)
}),
TypeConstructor { .. } => todo!(),
}
}

Expand Down
21 changes: 0 additions & 21 deletions petr-parse/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,6 @@ pub struct Parser {
// the tuple is the file name and content
source_map: IndexMap<SourceId, (&'static str, &'static str)>,
help: Vec<String>,
/// whether or not to continue advancing if one source file ends
/// TODO can maybe remove this now that modules aren't spanned items
file_barrier: bool,
}

impl Parser {
Expand Down Expand Up @@ -211,7 +208,6 @@ impl Parser {
peek: None,
source_map,
help: Default::default(),
file_barrier: false,
expr_id_assigner: 0,
}
}
Expand Down Expand Up @@ -335,11 +331,6 @@ impl Parser {
}

pub fn advance(&mut self) -> SpannedItem<Token> {
if self.file_barrier {
if let Token::NewFile(_) = self.peek().item() {
return self.lexer.span().with_item(Token::Eof);
}
}
if let Some(tok) = self.peek.take() {
return tok;
}
Expand Down Expand Up @@ -476,18 +467,6 @@ impl Parser {
pub fn source_map(&self) -> &IndexMap<SourceId, (&'static str, &'static str)> {
&self.source_map
}

/// stops advancing if a new file is found
/// required so we don't accidentally create spans that cross files
fn with_file_barrier<T>(
&mut self,
f: impl Fn(&mut Parser) -> T,
) -> T {
self.file_barrier = true;
let res = f(self);
self.file_barrier = false;
res
}
}

struct Checkpoint {
Expand Down
4 changes: 2 additions & 2 deletions petr-parse/src/parser/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ fn let_bindings() {
module test =
Func makes_function_call(
c ∈ 'int
) -> 'int
) -> 'int
let a = 1,
b = 20
call fn_call(var(a), var(b), var(c))
Expand Down Expand Up @@ -216,7 +216,7 @@ fn let_bindings_trailing_comma() {
module test =
Func makes_function_call(
c ∈ 'int
) -> 'int
) -> 'int
let a = 1,
b = 20
call fn_call(var(a), var(b), var(c))
Expand Down
23 changes: 0 additions & 23 deletions petr-pkg/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,26 +101,3 @@ pub fn find_manifest(path: Option<PathBuf>) -> Result<Manifest, Box<dyn std::err
let manifest = toml::from_str(&manifest_content)?;
Ok(manifest)
}

#[test]
fn what_is_my_manifest_format() {
let manifest = Manifest {
name: String::from("test package"),
author: Some("Alex Hansen <[email protected]>".into()),
license: Some("MIT".into()),
formatter: Default::default(),
dependencies: BTreeMap::from_iter(vec![(
"std".to_string(),
Dependency::Git(crate::GitDependency {
git: "https://github.com/sezna/petr-std".into(),
branch: None,
tag: None,
rev: None,
}),
)]),
};

let manifest_str = toml::to_string(&manifest).unwrap();
println!("manifest is: {}", manifest_str);
panic!()
}
94 changes: 53 additions & 41 deletions petr-resolve/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,10 +436,11 @@ impl Resolve for Expression {
_ => unreachable!(),
}
},
// TODO
Expression::TypeConstructor => {
// type constructor expressions are placeholders for the function body
// of a type constructor function
// Type constructor expressions themselves don't actually do anything.
// The function parameters and return types
// of the function are what get type checked -- there is no fn body, and this
// TypeConstructor expression is what represents that.
Expr::new(ExprKind::TypeConstructor)
},
Expression::IntrinsicCall(intrinsic) => {
Expand Down Expand Up @@ -570,7 +571,8 @@ impl Resolve for petr_utils::Path {
Item::Module(id) if self.identifiers.len() > 1 => id,
Item::Function(f, _) if self.identifiers.len() == 1 => return Some(either::Either::Left(*f)),
Item::Type(t) if self.identifiers.len() == 1 => return Some(either::Either::Right(*t)),
_ => todo!("push error -- import path is not a module"),
Item::Import { path, alias: _ } if self.identifiers.len() == 1 => return path.resolve(resolver, binder, scope_id),
a => todo!("push error -- import path is not a module {a:?}"),
};

let mut rover = binder.get_module(*first_item);
Expand Down Expand Up @@ -684,7 +686,7 @@ mod tests {
x.intrinsic,
x.args.iter().map(|x| x.to_string(resolver)).collect::<Vec<_>>().join(", ")
),
ExprKind::TypeConstructor => todo!(),
ExprKind::TypeConstructor => "Type constructor".into(),
ExprKind::ExpressionWithBindings { .. } => todo!(),
}
}
Expand Down Expand Up @@ -782,14 +784,14 @@ mod tests {
function foo(a in 'MyType) returns 'MyType [1, 2, 3]
"#,
expect![[r#"
_____FUNCTIONS_____
#0 a() -> named type MyType "<error>"
#1 b() -> named type MyType "<error>"
#2 foo( a: named type MyType, ) -> named type MyType "[Literal(Integer(1)), Literal(Integer(2)), Literal(Integer(3))]"
_____TYPES_____
#0 MyType
"#]],
_____FUNCTIONS_____
#0 a() -> named type MyType "Type constructor"
#1 b() -> named type MyType "Type constructor"
#2 foo( a: named type MyType, ) -> named type MyType "[Literal(Integer(1)), Literal(Integer(2)), Literal(Integer(3))]"
_____TYPES_____
#0 MyType
"#]],
);
}

Expand All @@ -805,14 +807,14 @@ mod tests {
type MyType = a | b
"#,
expect![[r#"
_____FUNCTIONS_____
#0 foo( a: named type MyType, ) -> named type MyType "[Literal(Integer(1)), Literal(Integer(2)), Literal(Integer(3))]"
#1 a() -> named type MyType "<error>"
#2 b() -> named type MyType "<error>"
_____TYPES_____
#0 MyType
"#]],
_____FUNCTIONS_____
#0 foo( a: named type MyType, ) -> named type MyType "[Literal(Integer(1)), Literal(Integer(2)), Literal(Integer(3))]"
#1 a() -> named type MyType "Type constructor"
#2 b() -> named type MyType "Type constructor"
_____TYPES_____
#0 MyType
"#]],
)
}

Expand All @@ -829,15 +831,15 @@ mod tests {
type MyType = a | b
"#,
expect![[r#"
_____FUNCTIONS_____
#0 foo() -> named type MyType "FunctionCall(functionid1)"
#1 bar( a: named type MyType, ) -> named type MyType "[Literal(Integer(1)), Literal(Integer(2)), Literal(Integer(3))]"
#2 a() -> named type MyType "<error>"
#3 b() -> named type MyType "<error>"
_____TYPES_____
#0 MyType
"#]],
_____FUNCTIONS_____
#0 foo() -> named type MyType "FunctionCall(functionid1)"
#1 bar( a: named type MyType, ) -> named type MyType "[Literal(Integer(1)), Literal(Integer(2)), Literal(Integer(3))]"
#2 a() -> named type MyType "Type constructor"
#3 b() -> named type MyType "Type constructor"
_____TYPES_____
#0 MyType
"#]],
)
}

Expand All @@ -854,15 +856,15 @@ mod tests {
type MyType = a | b
"#,
expect![[r#"
_____FUNCTIONS_____
#0 foo() -> named type MyType "FunctionCall(functionid1)"
#1 bar( a: named type MyType, ) -> named type MyType "[Literal(Integer(1)), Literal(Integer(2)), Literal(Integer(3))]"
#2 a() -> named type MyType "<error>"
#3 b() -> named type MyType "<error>"
_____TYPES_____
#0 MyType
"#]],
_____FUNCTIONS_____
#0 foo() -> named type MyType "FunctionCall(functionid1)"
#1 bar( a: named type MyType, ) -> named type MyType "[Literal(Integer(1)), Literal(Integer(2)), Literal(Integer(3))]"
#2 a() -> named type MyType "Type constructor"
#3 b() -> named type MyType "Type constructor"
_____TYPES_____
#0 MyType
"#]],
)
}
#[test]
Expand All @@ -879,7 +881,12 @@ mod tests {
"#,
],
expect![[r#""#]],
expect![[r#"
_____FUNCTIONS_____
#0 exported_func( a: int, ) -> int "a: int"
#1 foo() -> int "FunctionCall(functionid0)"
_____TYPES_____
"#]],
)
}

Expand All @@ -897,7 +904,12 @@ mod tests {
"#,
],
expect![[r#""#]],
expect![[r#"
_____FUNCTIONS_____
#0 exported_func( a: int, ) -> int "a: int"
#1 foo() -> int "FunctionCall(functionid0)"
_____TYPES_____
"#]],
)
}
}
Loading

0 comments on commit aa75348

Please sign in to comment.